LRDE Tiger Compiler  1.34a $Id: 7fef12e1f5fa43449d667a0eec1d837c40fc1202 $
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
object::Binder Class Reference

Computing bindings with support for objects. More...

#include <binder.hh>

Inheritance diagram for object::Binder:
Collaboration diagram for object::Binder:

Public Types

typedef ::bind::Binder super_type
 Super class type.

Public Member Functions

virtual void operator() (ast::ForExp &e) override
virtual void operator() (ast::SimpleVar &e) override
 Visit a Variable instantiation.
virtual void operator() (ast::NameTy &e) override
 Visit a type name.
virtual void operator() (ast::ClassTy &e) override
 Visit a class definition.
template<class D >
void visit_dec_header (D &e)
 Check a Function declaration header.
template<class D >
void visit_dec_body (D &e)
 Check a Function declaration body.
virtual void operator() (ast::VarDec &) override
virtual void operator() (ast::FunctionDecs &) override
 Visit a chunk of Function declarations.
virtual void operator() (ast::MethodDec &) override
 No longer used.
const misc::errorerror_get () const
 The error handler.
virtual void operator() (ast::LetExp &e) override
virtual void operator() (typename Const< Ast >::type &e)
 The entry point: visit e.
virtual void operator() (typename Const< ClassTy >::type &)=0
virtual void operator() (typename Const< MethodCallExp >::type &)=0
virtual void operator() (typename Const< MethodDec >::type &)=0
virtual void operator() (typename Const< ObjectExp >::type &)=0
template<class E >
void operator() (E *e)
 Helper to visit nodes manipulated via a pointer.
Visit Variable related nodes.
virtual void operator() (typename Const< SimpleVar >::type &e) override
virtual void operator() (typename Const< FieldVar >::type &e) override
virtual void operator() (typename Const< SubscriptVar >::type &e) override
virtual void operator() (typename Const< CastVar >::type &e) override
Visit Expression related nodes.
virtual void operator() (typename Const< NilExp >::type &e) override
virtual void operator() (typename Const< IntExp >::type &e) override
virtual void operator() (typename Const< StringExp >::type &e) override
virtual void operator() (typename Const< CallExp >::type &e) override
virtual void operator() (typename Const< OpExp >::type &e) override
virtual void operator() (typename Const< RecordExp >::type &e) override
virtual void operator() (typename Const< SeqExp >::type &e) override
virtual void operator() (typename Const< AssignExp >::type &e) override
virtual void operator() (typename Const< IfExp >::type &e) override
virtual void operator() (typename Const< WhileExp >::type &e) override
virtual void operator() (typename Const< ForExp >::type &e) override
virtual void operator() (typename Const< BreakExp >::type &) override
virtual void operator() (typename Const< LetExp >::type &e) override
virtual void operator() (typename Const< ArrayExp >::type &e) override
virtual void operator() (typename Const< CastExp >::type &e) override
virtual void operator() (typename Const< FieldInit >::type &e) override
Visit Declaration related nodes.

Visiting declarations is simple, but there are many clauses. This is because in Tiger, the declarations are processed by chunks (a chunk of Function declarations, then Var or Type, then ...). So we have to explain

  • How to visit a list of chunks;
  • how to visit chunks of function, var, or type declarations;
  • how to visit a single function, var, or type declaration.
virtual void operator() (typename Const< DecsList >::type &e) override
 Visit a list of function, type and/or variables declarations.
virtual void operator() (typename Const< Decs >::type &e)
 Visit a Decs chunks.
virtual void operator() (typename Const< VarDecs >::type &e) override
 Visit Var declarations.
virtual void operator() (typename Const< VarDec >::type &e) override
virtual void operator() (typename Const< FunctionDec >::type &e) override
virtual void operator() (typename Const< TypeDec >::type &e) override
template<typename DecsType >
void decs_visit (typename Const< DecsType >::type &e)
 Visit a chunk (i.e., a list of Function, Var, and Type decs).
Visit Type related nodes.
virtual void operator() (typename Const< NameTy >::type &e) override
virtual void operator() (typename Const< RecordTy >::type &e) override
virtual void operator() (typename Const< ArrayTy >::type &e) override
Visit Field related nodes.
virtual void operator() (typename Const< Field >::type &e) override
Object-related visits.
virtual void operator() (typename Const< ClassTy >::type &e) override
virtual void operator() (typename Const< MethodDecs >::type &e) override
virtual void operator() (typename Const< MethodDec >::type &e) override
virtual void operator() (typename Const< MethodCallExp >::type &e) override
virtual void operator() (typename Const< ObjectExp >::type &e) override
Type and Function declarations
template<class D >
void decs_visit (ast::AnyDecs< D > &e)

Protected Member Functions

template<typename NodeType , typename DefType >
void def_default (NodeType &e, DefType *def)
template<typename E >
void accept (E *e)
Error handling
void error (const ast::Ast &loc, const std::string &msg)
 Report an error.
template<typename T >
void undeclared (const std::string &k, const T &e)
template<typename T >
void redefinition (const T &e1, const T &e2)
Handling the environment
virtual void scope_begin ()
 Open a new var, fun, and type scope.
virtual void scope_end ()
 Close the latest var, fun, and type scope.

Private Attributes

bool within_class_ty_ = false
bool within_method_dec_ = false

Detailed Description

Computing bindings with support for objects.

Member Typedef Documentation

Super class type.

Member Function Documentation

template<template< typename > class Const>
template<typename E >
void ast::GenVisitor< Const >::accept ( E *  e)
protectedinherited

A convenient shortcut for recurring code like this:

if (e)
e->accept(*this);

However, the drawback of this approach is that it doesn't take care of the constness, and any const violation will be reported within the body of this method, not at its corresponding call site.

We cannot use the misc/select_const.hh approach here, since the compiler cannot resolve a function overloaded or specialized on an associated type of a template. E.g., writing accept like this:

template <typename E>
void accept(typename Const<E>::type* e);

won't work directly. Of course, one can help the compiler, providing it with E

accept<ast::NameTy>(e.result_get());

but this is painful.

template<class D >
void bind::Binder::decs_visit ( ast::AnyDecs< D > &  e)
inherited

When traversing a function (or a type) we both have to bind its body (i.e., we need to enter a new scope and push the arguments in it), and we have to store the function's declaration in the current scope (so that other functions can call it). We first introduce the function's name in the outer environment so that the function can call itself recursively. In the mean time, we also check for uniqueness. Then, as a second step, we process the contents of all the functions belonging to the current chunk. Therefore there are three methods:

  • decs_visit () performs the double traversal, composed of the two following functions
  • visit_dec_header () visit the declaration to register the function in the current environment,
  • visit_dec_body () visits the body. Of course for functions it also inserts the current arguments in the variable table.

It turns out that decs_visit can be written for both functions and types, but of course, traversals of headers and bodies differ. Check a set of definitions: unique names, browse headers, then bodies.

template<template< typename > class Const>
template<typename DecsType >
void ast::GenDefaultVisitor< Const >::decs_visit ( typename Const< DecsType >::type &  e)
inlineinherited

Visit a chunk (i.e., a list of Function, Var, and Type decs).

It is exactly the same in the three cases, so the code is factored via a template method.

template<typename NodeType , typename DefType >
void bind::Binder::def_default ( NodeType &  e,
DefType *  def 
)
protectedinherited

Set the definition site of a node, if it isn't already set (an existing site won't be overwritten).

Parameters
ethe bound node
defthe definition
void bind::Binder::error ( const ast::Ast loc,
const std::string &  msg 
)
protectedinherited

Report an error.

const misc::error & bind::Binder::error_get ( ) const
inherited

The error handler.

Referenced by overload::bind().

void object::Binder::operator() ( ast::ForExp e)
overridevirtual

References bind::Binder::operator()(), and within_class_ty_.

Referenced by operator()().

void object::Binder::operator() ( ast::SimpleVar e)
overridevirtual

Visit a Variable instantiation.

template<template< typename > class Const>
virtual void ast::GenVisitor< Const >::operator() ( typename Const< ClassTy >::type &  )
pure virtualinherited
void object::Binder::operator() ( ast::NameTy e)
overridevirtual

Visit a type name.

template<template< typename > class Const>
virtual void ast::GenVisitor< Const >::operator() ( typename Const< MethodCallExp >::type &  )
pure virtualinherited
template<template< typename > class Const>
virtual void ast::GenVisitor< Const >::operator() ( typename Const< MethodDec >::type &  )
pure virtualinherited
template<template< typename > class Const>
virtual void ast::GenVisitor< Const >::operator() ( typename Const< ObjectExp >::type &  )
pure virtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< Ast >::type &  e)
virtualinherited
void object::Binder::operator() ( ast::VarDec e)
overridevirtual
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< SimpleVar >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< FieldVar >::type &  e)
overridevirtualinherited
void object::Binder::operator() ( ast::FunctionDecs e)
overridevirtual

Visit a chunk of Function declarations.

template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< SubscriptVar >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< CastVar >::type &  e)
overridevirtualinherited
virtual void bind::Binder::operator() ( ast::LetExp &  e)
overridevirtualinherited

Referenced by operator()().

template<template< typename > class Const>
virtual void ast::GenObjectVisitor< Const >::operator() ( typename Const< ClassTy >::type &  e)
overridevirtualinherited
void object::Binder::operator() ( ast::MethodDec e)
overridevirtual
template<template< typename > class Const>
virtual void ast::GenObjectVisitor< Const >::operator() ( typename Const< MethodDecs >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
virtual void ast::GenObjectVisitor< Const >::operator() ( typename Const< MethodDec >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< NilExp >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
virtual void ast::GenObjectVisitor< Const >::operator() ( typename Const< MethodCallExp >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
template<class E >
void ast::GenVisitor< Const >::operator() ( E *  e)
inherited

Helper to visit nodes manipulated via a pointer.

template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< IntExp >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
virtual void ast::GenObjectVisitor< Const >::operator() ( typename Const< ObjectExp >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< StringExp >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< CallExp >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< OpExp >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< RecordExp >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< SeqExp >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< AssignExp >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< IfExp >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< WhileExp >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< ForExp >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< BreakExp >::type &  )
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< LetExp >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< ArrayExp >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< CastExp >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< FieldInit >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< DecsList >::type &  e)
overridevirtualinherited

Visit a list of function, type and/or variables declarations.

Implements ast::GenVisitor< Const >.

References misc::for_each().

template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< Decs >::type &  e)
virtualinherited

Visit a Decs chunks.

template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< VarDecs >::type &  e)
overridevirtualinherited

Visit Var declarations.

Implements ast::GenVisitor< Const >.

template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< VarDec >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< FunctionDec >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< TypeDec >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< NameTy >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< RecordTy >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< ArrayTy >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< Field >::type &  e)
overridevirtualinherited
template<typename T >
void bind::Binder::redefinition ( const T &  e1,
const T &  e2 
)
protectedinherited

Report a symbol redefinition.

Parameters
e1the first occurrence, the original
e2the invalid duplicate
virtual void bind::Binder::scope_begin ( )
protectedvirtualinherited

Open a new var, fun, and type scope.

Reimplemented in overload::Binder.

Referenced by operator()(), and overload::Binder::scope_begin().

virtual void bind::Binder::scope_end ( )
protectedvirtualinherited

Close the latest var, fun, and type scope.

Reimplemented in overload::Binder.

Referenced by operator()(), and overload::Binder::scope_end().

template<typename T >
void bind::Binder::undeclared ( const std::string &  k,
const T &  e 
)
protectedinherited

Report an undefined symbol.

Parameters
kthe kind of this node (function, variable, type)
ethe node using an undefined name

Referenced by overload::Binder::operator()().

template<class D >
void object::Binder::visit_dec_body ( D &  e)

Check a Function declaration body.

Reimplemented from bind::Binder.

template<class D >
void object::Binder::visit_dec_header ( D &  e)

Check a Function declaration header.

Reimplemented from bind::Binder.

Member Data Documentation

bool object::Binder::within_class_ty_ = false
private

Are we (immediately) within a class definition?

This predicate is used to prevent the bindings of VarDecs that are direct children of a ClassTy and which represent attributes of that class definition; the binding of these attributes is deferred to the type-checking phase (as for records' fields). This predicate should be false within a non-ClassTy node declaring variables, even if that node belongs (indirectly) to the subtree of a ClassTy.

Referenced by operator()().

bool object::Binder::within_method_dec_ = false
private

Are we (immediately) within a method definition?

This predicate is used to determine whether `self' is a valid variable. This is only the case for nodes that belong to the subtree of a MethodDec node, except for FunctionDec and ClassTy subtrees, which ``reset'' the context.

Referenced by operator()().


The documentation for this class was generated from the following files: