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

Computing bindings with support for overloaded functions. More...

#include <binder.hh>

Inheritance diagram for overload::Binder:
Collaboration diagram for overload::Binder:

Public Types

typedef bind::Binder super_type
 Super class type.

Public Member Functions

virtual void operator() (ast::CallExp &e) override
 Visit a function call.
void visit_dec_header (ast::FunctionDec &e)
 Check a function declaration header.
virtual void operator() (ast::FunctionDecs &e) override
 Visit a chunk of function declarations.
overfun_bindings_typeoverfun_bindings_get ()
 Return the function bindings.
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)
template<class D >
void visit_dec_header (D &e)
 Check a Function or Type declaration header.
template<class D >
void visit_dec_body (D &e)
 Check a Function or Type declaration body.

Protected Member Functions

template<typename NodeType , typename DefType >
void def_default (NodeType &e, DefType *def)
template<typename E >
void accept (E *e)
Overriden scope manipulations.
virtual void scope_begin () override
 Open a new var, fun, and type scope.
virtual void scope_end () override
 Close the latest var, fun, and type scope.
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)

Private Types

typedef OverTable
< ast::FunctionDec
overtable_type

Private Attributes

overtable_type overfuns_
 The environment of (overloaded) functions.
overfun_bindings_typeoverfun_bindings_ = new overfun_bindings_type()
 The potential function bindings.

Detailed Description

Computing bindings with support for overloaded functions.

This visitor inherits from Binder, but redefines methods dealing with function declarations and uses, to allow overloading.

As overloading requires some knowledge of the type of the arguments of a function, no real function binding is done here. We store all potential function declarations (``homonyms'') for each function call, and we'll let the overload::TypeChecker decide later.

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().

template<template< typename > class Const>
virtual void ast::GenVisitor< Const >::operator() ( typename Const< ClassTy >::type &  )
pure virtualinherited
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
void overload::Binder::operator() ( ast::CallExp &  e)
overridevirtual

Visit a function call.

References overload::OverTable< T >::get(), overfuns_, and bind::Binder::undeclared().

template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< Ast >::type &  e)
virtualinherited
void overload::Binder::operator() ( ast::FunctionDecs e)
overridevirtual

Visit a chunk of function declarations.

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
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< SubscriptVar >::type &  e)
overridevirtualinherited
virtual void bind::Binder::operator() ( ast::LetExp &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< CastVar >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
virtual void ast::GenObjectVisitor< Const >::operator() ( typename Const< ClassTy >::type &  e)
overridevirtualinherited
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>
template<class E >
void ast::GenVisitor< Const >::operator() ( E *  e)
inherited

Helper to visit nodes manipulated via a pointer.

template<template< typename > class Const>
virtual void ast::GenObjectVisitor< Const >::operator() ( typename Const< MethodCallExp >::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< ObjectExp >::type &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< IntExp >::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
overfun_bindings_type * overload::Binder::overfun_bindings_get ( )

Return the function bindings.

References overfun_bindings_.

Referenced by overload::bind().

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
void overload::Binder::scope_begin ( )
overrideprotectedvirtual

Open a new var, fun, and type scope.

Reimplemented from bind::Binder.

References overfuns_, overload::OverTable< T >::scope_begin(), and bind::Binder::scope_begin().

void overload::Binder::scope_end ( )
overrideprotectedvirtual

Close the latest var, fun, and type scope.

Reimplemented from bind::Binder.

References overfuns_, overload::OverTable< T >::scope_end(), and bind::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 operator()().

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

Check a Function or Type declaration body.

Reimplemented in object::Binder.

void overload::Binder::visit_dec_header ( ast::FunctionDec e)

Check a function declaration header.

References ast::Dec::name_get(), overfuns_, and overload::OverTable< T >::put().

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

Check a Function or Type declaration header.

Reimplemented in object::Binder.

Member Data Documentation

overfun_bindings_type* overload::Binder::overfun_bindings_ = new overfun_bindings_type()
private

The potential function bindings.

Referenced by overfun_bindings_get().

overtable_type overload::Binder::overfuns_
private

The environment of (overloaded) functions.

Referenced by operator()(), scope_begin(), scope_end(), and visit_dec_header().


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