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

Translate an ast::Ast into High level tree::Tree code. More...

#include <translator.hh>

Inheritance diagram for translate::Translator:
Collaboration diagram for translate::Translator:

Public Types

typedef ast::DefaultConstVisitor super_type
 Super class.

Public Member Functions

 Translator ()
virtual ~Translator ()
virtual void operator() (const ast::Ast &e) override
 Run the translation.
tree::Fragmentsfragments_get ()
 Return the translation. Ownership is transfered to the receiver.
rExp translate (const ast::Ast &node)
 Run this visitor on node, and return its translation.
template<typename T >
std::list< rExptranslate (const std::list< T * > &es)
 The list of translations of es members.
virtual void operator() (const ast::VarDec &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.
Forbid copying this visitor: do not implement the copy

ctor nor the assignment operator.

Copying is forbidden because deep-copy is not implemented, and the dtor deletes the level_ attribute, which is allocated by the ctor.

 Translator (const Translator &)
Translatoroperator= (const Translator &)
Lvalues
virtual void operator() (const ast::SimpleVar &e) override
virtual void operator() (const ast::FieldVar &e) override
virtual void operator() (const ast::SubscriptVar &e) override
virtual void operator() (const ast::CastVar &e) override
Expressions
virtual void operator() (const ast::NilExp &) override
virtual void operator() (const ast::IntExp &e) override
virtual void operator() (const ast::StringExp &e) override
virtual void operator() (const ast::RecordExp &e) override
virtual void operator() (const ast::CallExp &e) override
virtual void operator() (const ast::OpExp &e) override
virtual void operator() (const ast::SeqExp &e) override
virtual void operator() (const ast::AssignExp &e) override
virtual void operator() (const ast::IfExp &e) override
virtual void operator() (const ast::WhileExp &e) override
virtual void operator() (const ast::ForExp &e) override
virtual void operator() (const ast::BreakExp &) override
virtual void operator() (const ast::LetExp &e) override
virtual void operator() (const ast::ArrayExp &e) override
virtual void operator() (const ast::CastExp &e) override
Chunks of declarations
virtual void operator() (const ast::VarDecs &e) override
virtual void operator() (const ast::TypeDecs &) override
virtual void operator() (const ast::FunctionDecs &e) override
Function Declaration
virtual void operator() (const ast::FunctionDec &) override
void visit_function_dec_header (const ast::FunctionDec &e)
void visit_function_dec_body (const ast::FunctionDec &e)
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.

The methods should not be used, since this visitor is for the non-object flavor of the language.

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

Protected Member Functions

template<typename E >
void accept (E *e)

Protected Attributes

rExp exp_
 The translation of the last visited node.
std::stack< std::list< rExp > > exps_
 The translations of all the pending declarations.
tree::Fragmentsfragments_
 The fragments translated so far (functions and string literals).
Levellevel_
 The current level (related to FunctionDec nestings).
std::map< const ast::Exp
*, temp::Label
loop_end_label_
 For each loop, the address immediately after it.
std::map< const ast::Ast
*, const Access * > 
var_access_
 Access for each "variable" (VarDec, Field).
std::map< const ast::Ast
*, const Level * > 
fun_level_
 Level for each function.
std::map< const ast::Ast
*, temp::Label
fun_label_
 Label for each function.

Detailed Description

Translate an ast::Ast into High level tree::Tree code.

Member Typedef Documentation

Constructor & Destructor Documentation

translate::Translator::Translator ( )
translate::Translator::Translator ( const Translator )
translate::Translator::~Translator ( )
virtual

References fun_level_, and level_.

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

tree::Fragments * translate::Translator::fragments_get ( )

Return the translation. Ownership is transfered to the receiver.

References fragments_.

Referenced by translate::translate().

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
void translate::Translator::operator() ( const ast::Ast e)
overridevirtual

Run the translation.

References exp_, and translate().

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

The entry point: visit e.

Reimplemented from ast::GenVisitor< Const >.

Referenced by callgraph::CallGraphVisitor::operator()(), object::Renamer::operator()(), and operator()().

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
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< CastVar >::type &  e)
overridevirtualinherited
void translate::Translator::operator() ( const ast::SimpleVar e)
overridevirtual
template<template< typename > class Const>
void ast::GenNonObjectVisitor< Const >::operator() ( typename Const< ClassTy >::type &  e)
overridevirtualinherited

Implements ast::GenVisitor< Const >.

References unreached.

void translate::Translator::operator() ( const ast::FieldVar &  e)
overridevirtual
void translate::Translator::operator() ( const ast::SubscriptVar e)
overridevirtual
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< NilExp >::type &  e)
overridevirtualinherited
virtual void translate::Translator::operator() ( const ast::CastVar e)
overridevirtual
template<template< typename > class Const>
void ast::GenNonObjectVisitor< Const >::operator() ( typename Const< MethodDecs >::type &  e)
overridevirtualinherited

Implements ast::GenVisitor< Const >.

References unreached.

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

Implements ast::GenVisitor< Const >.

References unreached.

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::GenNonObjectVisitor< Const >::operator() ( typename Const< MethodCallExp >::type &  e)
overridevirtualinherited

Implements ast::GenVisitor< Const >.

References unreached.

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

Implements ast::GenVisitor< Const >.

References unreached.

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
void translate::Translator::operator() ( const ast::NilExp )
overridevirtual

References exp_, and translate::nil_exp().

template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< IfExp >::type &  e)
overridevirtualinherited
void translate::Translator::operator() ( const ast::IntExp e)
overridevirtual
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
void translate::Translator::operator() ( const ast::StringExp &  e)
overridevirtual
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
void translate::Translator::operator() ( const ast::RecordExp &  e)
overridevirtual
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< CastExp >::type &  e)
overridevirtualinherited
void translate::Translator::operator() ( const ast::CallExp &  e)
overridevirtual
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< FieldInit >::type &  e)
overridevirtualinherited
void translate::Translator::operator() ( const ast::OpExp e)
overridevirtual
void translate::Translator::operator() ( const ast::SeqExp &  e)
overridevirtual
void translate::Translator::operator() ( const ast::AssignExp &  e)
overridevirtual
void translate::Translator::operator() ( const ast::IfExp &  e)
overridevirtual
void translate::Translator::operator() ( const ast::WhileExp e)
overridevirtual
void translate::Translator::operator() ( const ast::ForExp e)
overridevirtual
void translate::Translator::operator() ( const ast::BreakExp &  e)
overridevirtual
void translate::Translator::operator() ( const ast::LetExp &  e)
overridevirtual
void translate::Translator::operator() ( const ast::ArrayExp &  e)
overridevirtual
virtual void translate::Translator::operator() ( const ast::CastExp e)
overridevirtual
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().

void translate::Translator::operator() ( const ast::VarDecs e)
overridevirtual
virtual void translate::Translator::operator() ( const ast::TypeDecs )
overridevirtual
virtual void translate::Translator::operator() ( const ast::FunctionDecs e)
overridevirtual
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< Decs >::type &  e)
virtualinherited

Visit a Decs chunks.

void translate::Translator::operator() ( const ast::FunctionDec )
overridevirtual

References unreached.

void translate::Translator::operator() ( const ast::VarDec e)
overridevirtual
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
Translator& translate::Translator::operator= ( const Translator )
rExp translate::Translator::translate ( const ast::Ast node)

Run this visitor on node, and return its translation.

It is also guaranteed that exp_ is set to it. More generally, this routine saves from direct calls to accept.

References ast::Ast::accept(), and exp_.

Referenced by operator()(), translate(), and visit_function_dec_body().

template<typename T >
std::list< rExp > translate::Translator::translate ( const std::list< T * > &  es)

The list of translations of es members.

References translate().

void translate::Translator::visit_function_dec_header ( const ast::FunctionDec e)

Member Data Documentation

rExp translate::Translator::exp_
protected

The translation of the last visited node.

Referenced by operator()(), translate(), and visit_function_dec_body().

std::stack<std::list<rExp> > translate::Translator::exps_
protected

The translations of all the pending declarations.

Referenced by operator()().

tree::Fragments* translate::Translator::fragments_
protected

The fragments translated so far (functions and string literals).

Referenced by fragments_get(), and visit_function_dec_body().

std::map<const ast::Ast*, temp::Label> translate::Translator::fun_label_
protected

Label for each function.

Referenced by operator()(), visit_function_dec_body(), and visit_function_dec_header().

std::map<const ast::Ast*, const Level*> translate::Translator::fun_level_
protected
Level* translate::Translator::level_
protected

The current level (related to FunctionDec nestings).

Referenced by operator()(), visit_function_dec_body(), visit_function_dec_header(), and ~Translator().

std::map<const ast::Exp*, temp::Label> translate::Translator::loop_end_label_
protected

For each loop, the address immediately after it.

Referenced by operator()().

std::map<const ast::Ast*, const Access*> translate::Translator::var_access_
protected

Access for each "variable" (VarDec, Field).

Referenced by operator()(), and visit_function_dec_body().


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