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

Desugar some object structures while duplicating an Ast. More...

#include <desugar-visitor.hh>

Inheritance diagram for object::DesugarVisitor:
Collaboration diagram for object::DesugarVisitor:

Public Types

typedef astclone::Cloner super_type
 Superclass.
typedef std::list< const
type::Class * > 
classes_type
 A list of classes (e.g., useful to represent a set of subclasses).

Public Member Functions

 DesugarVisitor (const class_names_type &class_names)
 Build a DesugarVisitor.
virtual ~DesugarVisitor ()
 Destroy a DesugarVisitor.
virtual void operator() (const ast::DecsList &e) override
virtual ast::exps_typerecurse_args (const ast::exps_type &actuals, const type::Record &formals)
 Desugar actuals arguments in routine calls.
ast::Astresult_get ()
template<typename T >
T * recurse (const T &t)
template<typename T >
T * recurse (const T *const t)
template<typename CollectionType >
CollectionType * recurse_collection (const CollectionType &c)
 Clone a collection object.
virtual void operator() (const ast::ArrayExp &) override
virtual void operator() (const ast::ArrayTy &) override
virtual void operator() (const ast::BreakExp &) override
virtual void operator() (const ast::CastExp &) override
virtual void operator() (const ast::CastVar &) override
virtual void operator() (const ast::ClassTy &) override
virtual void operator() (const ast::Field &) override
virtual void operator() (const ast::FieldInit &) override
virtual void operator() (const ast::ForExp &) override
virtual void operator() (const ast::IfExp &) override
virtual void operator() (const ast::IntExp &) override
virtual void operator() (const ast::LetExp &) override
virtual void operator() (const ast::MethodDec &) override
virtual void operator() (const ast::NilExp &) override
virtual void operator() (const ast::OpExp &) override
virtual void operator() (const ast::RecordExp &) override
virtual void operator() (const ast::RecordTy &) override
virtual void operator() (const ast::SeqExp &) override
virtual void operator() (const ast::SimpleVar &) override
virtual void operator() (const ast::StringExp &) override
virtual void operator() (const ast::SubscriptVar &) override
virtual void operator() (const ast::TypeDec &) override
virtual void operator() (const ast::WhileExp &) 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.
template<typename DecsType >
void decs_visit (const DecsType &e)
 Visit a chunk (i.e., a list of Function, Var, and Type decs).
Desugar class declarations.
virtual void operator() (const ast::TypeDecs &e) override
 Desugar class declarations.
virtual void operator() (const ast::FunctionDec &e) override
 Handle the builtin Object.
virtual void operator() (const ast::NameTy &e) override
 Desugar class names.
Desugar class instantiations and object usage.
virtual void operator() (const ast::VarDec &e) override
 Desugar polymorphic initializations.
virtual void operator() (const ast::ObjectExp &e) override
 Desugar manifest objects.
virtual void operator() (const ast::AssignExp &e) override
 Desugar polymorphic assignments.
virtual void operator() (const ast::CallExp &e) override
 Add object casts around arguments when needed.
Desugar accesses to class members.
virtual void operator() (const ast::FieldVar &e) override
 Desugar accesses to attributes.
virtual void operator() (const ast::MethodCallExp &e) override
 Desugar calls to methods.
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

Protected Member Functions

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

Protected Attributes

ast::Astresult_
 The cloned Ast.

Private Attributes

class_names_type class_names_
 The names of the classes defined in the program.
parse::Tweast types_
 TWEAST of desugared types.
parse::Tweast class_ids_
 TWEAST of class ids.

Code generation.

typedef misc::map< const
type::Class *, std::string > 
field_inits_type
 The type of a list of initializations for the field of a variant.
std::string type_symbol (const type::Type *type)
 Return the name of a type.
std::string upcast_fun_name (const type::Class *from, const type::Class *to)
std::string downcast_fun_name (const type::Class *from, const type::Class *to)
std::string dispatch_fun_name (const type::Class *owner, const type::Method *method)
void adapt_type (ast::Exp *&source_exp, const type::Class *source_type, const type::Class *target_type)
ast::Expvariant_exp (const type::Class *static_type, const std::string &exact_type, const field_inits_type &inits)
 Generate a variant expression.
ast::Expvariant_exp (const type::Class *static_type, const type::Class *dynamic_type, const field_inits_type &inits)
 Syntactic sugar for the previous routine.
parse::Tweastvariant_ty (const type::Class *class_type)
 Generate code for a variant type on a TWEAST.
parse::Tweastcast_function (const std::string &name, const type::Class *source, const type::Class *target, const type::Class *exact_type)
 Generate a conversion routine.
parse::Tweastupcast_function (const type::Class *source, const type::Class *target)
parse::Tweastdowncast_function (const type::Class *source, const type::Class *target)
ast::Expdispatch_switch (const type::Class *class_type, const type::Method *method)
 Generate an expression looking like a switch expression, to dispatch a method call.
parse::Tweastmethod_call (const misc::symbol &class_name, const misc::symbol &method_name, const std::string &target, const ast::VarDecs::Ds &formals)
 Generate a (static) call to a desugared method.

Detailed Description

Desugar some object structures while duplicating an Ast.

Member Typedef Documentation

A list of classes (e.g., useful to represent a set of subclasses).

typedef misc::map<const type::Class*, std::string> object::DesugarVisitor::field_inits_type
private

The type of a list of initializations for the field of a variant.

Constructor & Destructor Documentation

object::DesugarVisitor::DesugarVisitor ( const class_names_type class_names)

Build a DesugarVisitor.

object::DesugarVisitor::~DesugarVisitor ( )
virtual

Destroy a DesugarVisitor.

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.

void object::DesugarVisitor::adapt_type ( ast::Exp *&  source_exp,
const type::Class source_type,
const type::Class target_type 
)
private

Check if the type source_type of source_exp matches target_type, and generate a type conversion wrapper if needed.

References parse::parse(), and upcast_fun_name().

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

parse::Tweast * object::DesugarVisitor::cast_function ( const std::string &  name,
const type::Class source,
const type::Class target,
const type::Class exact_type 
)
private

Generate a conversion routine.

Parameters
namethe type of the generated function
sourcethe type of the converted value
targetthe target type of the conversion
exact_typethe exact type of the returned variant
Returns
the TWEAST on which the code is generated

References class_names_, misc::put(), type::Class::subclasses_get(), type::Class::super_get(), and variant_exp().

Referenced by downcast_function(), and upcast_function().

template<typename DecsType >
void astclone::Cloner::decs_visit ( const DecsType &  e)
inherited

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.

References assertion.

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.

std::string object::DesugarVisitor::dispatch_fun_name ( const type::Class owner,
const type::Method method 
)
private

Return the name of the dispatch function for method, defined in class owner.

References class_names_, and type::Method::name_get().

Referenced by operator()().

ast::Exp * object::DesugarVisitor::dispatch_switch ( const type::Class class_type,
const type::Method method 
)
private

Generate an expression looking like a switch expression, to dispatch a method call.

Parameters
class_typethe static type of the class owning the method
methodthe called method
Returns
the generated expression

References class_names_, ast::AnyDecs< D >::decs_get(), type::Method::def_get(), downcast_fun_name(), ast::FunctionDec::formals_get(), method_call(), type::Method::name_get(), parse::parse(), and type::Class::subclasses_get().

Referenced by operator()().

std::string object::DesugarVisitor::downcast_fun_name ( const type::Class from,
const type::Class to 
)
private

Return the name of the downcast function between types from and to.

References class_names_.

Referenced by dispatch_switch(), and downcast_function().

parse::Tweast * object::DesugarVisitor::downcast_function ( const type::Class source,
const type::Class target 
)
private

Syntactic sugar for object::DesugarVisitor::cast_function, used to generate a downcast function.

References cast_function(), and downcast_fun_name().

Referenced by operator()().

parse::Tweast * object::DesugarVisitor::method_call ( const misc::symbol class_name,
const misc::symbol method_name,
const std::string &  target,
const ast::VarDecs::Ds formals 
)
private

Generate a (static) call to a desugared method.

Note
The dynamic dispatch must have been performed before using this generator.
Parameters
class_namethe class where the method resides
method_namethe name of the method
targetthe name of the target (object)
formalsthe actual arguments (other than the target)
Returns
the TWEAST on which the code is generated

Referenced by dispatch_switch().

void object::DesugarVisitor::operator() ( const ast::DecsList e)
overridevirtual

Special version of the visit of a DecsList allowing the transformation of an ast::Decs to either a single ast::Decs or to an ast::DecsList.

Reimplemented from astclone::Cloner.

References ast::DecsList::decs_get(), ast::Ast::location_get(), and astclone::Cloner::result_.

template<template< typename > class Const>
virtual void ast::GenVisitor< Const >::operator() ( typename Const< ClassTy >::type &  )
pure virtualinherited
void object::DesugarVisitor::operator() ( const ast::NameTy e)
overridevirtual
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 astclone::Cloner::operator() ( const ast::ArrayExp &  e)
overridevirtualinherited

Referenced by operator()().

void astclone::Cloner::operator() ( const ast::ArrayTy e)
overridevirtualinherited
void object::DesugarVisitor::operator() ( const ast::ObjectExp &  e)
overridevirtual

Desugar manifest objects.

Reimplemented from astclone::Cloner.

void astclone::Cloner::operator() ( const ast::BreakExp &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< Ast >::type &  e)
virtualinherited
void object::DesugarVisitor::operator() ( const ast::AssignExp &  e)
overridevirtual
void astclone::Cloner::operator() ( const ast::CastExp e)
overridevirtualinherited
void object::DesugarVisitor::operator() ( const ast::CallExp &  e)
overridevirtual

Add object casts around arguments when needed.

Reimplemented from astclone::Cloner.

References misc::symbol::name_get(), recurse_args(), and astclone::Cloner::result_.

void astclone::Cloner::operator() ( const ast::CastVar e)
overridevirtualinherited
void astclone::Cloner::operator() ( const ast::ClassTy e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< SimpleVar >::type &  e)
overridevirtualinherited
void astclone::Cloner::operator() ( const ast::Field e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< FieldVar >::type &  e)
overridevirtualinherited
void astclone::Cloner::operator() ( const ast::FieldInit 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 astclone::Cloner::operator() ( const ast::ForExp e)
overridevirtualinherited
void object::DesugarVisitor::operator() ( const ast::FieldVar &  e)
overridevirtual
void astclone::Cloner::operator() ( const ast::IfExp &  e)
overridevirtualinherited
void astclone::Cloner::operator() ( const ast::IntExp e)
overridevirtualinherited
void astclone::Cloner::operator() ( const ast::LetExp &  e)
overridevirtualinherited
void object::DesugarVisitor::operator() ( const ast::MethodCallExp &  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
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
void astclone::Cloner::operator() ( const ast::NilExp 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
void astclone::Cloner::operator() ( const ast::OpExp 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
void astclone::Cloner::operator() ( const ast::RecordExp &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< AssignExp >::type &  e)
overridevirtualinherited
void astclone::Cloner::operator() ( const ast::RecordTy &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< IfExp >::type &  e)
overridevirtualinherited
void astclone::Cloner::operator() ( const ast::SeqExp &  e)
overridevirtualinherited
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< WhileExp >::type &  e)
overridevirtualinherited
void astclone::Cloner::operator() ( const ast::SimpleVar e)
overridevirtualinherited
void astclone::Cloner::operator() ( const ast::StringExp &  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 astclone::Cloner::operator() ( const ast::SubscriptVar e)
overridevirtualinherited
void astclone::Cloner::operator() ( const ast::TypeDec e)
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
void astclone::Cloner::operator() ( const ast::WhileExp 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 >
T * astclone::Cloner::recurse ( const T &  t)
inherited
template<typename T >
T * astclone::Cloner::recurse ( const T *const  t)
inherited

References assertion.

ast::exps_type * object::DesugarVisitor::recurse_args ( const ast::exps_type actuals,
const type::Record formals 
)
virtual

Desugar actuals arguments in routine calls.

Parameters
actualsthe actual arguments of the initial AST
formalsthe formal arguments of the routine
Returns
the desugared actual arguments

References type::Type::actual(), adapt_type(), type::Record::begin(), type::Record::end(), type::Nil::instance(), and astclone::Cloner::recurse().

Referenced by operator()().

template<typename CollectionType >
CollectionType * astclone::Cloner::recurse_collection ( const CollectionType &  c)
inherited

Clone a collection object.

Using overloading for this method is tempting, but it would lead to the same prototype than the first recurse method.

A partial specialization for std::list<T> would work, but is not allowed by C++ standard. As a consequence, we are stuck to using different names.

References assertion.

Referenced by astclone::Cloner::operator()().

std::string object::DesugarVisitor::type_symbol ( const type::Type type)
private

Return the name of a type.

Parameters
typeeither a builtin type or a type::Named
Returns
the name of the type

References class_names_.

std::string object::DesugarVisitor::upcast_fun_name ( const type::Class from,
const type::Class to 
)
private

Return the name of the upcast function between types from and to.

References class_names_.

Referenced by adapt_type(), operator()(), and upcast_function().

parse::Tweast * object::DesugarVisitor::upcast_function ( const type::Class source,
const type::Class target 
)
private

Syntactic sugar for object::DesugarVisitor::cast_function, used to generate an upcast function.

References cast_function(), and upcast_fun_name().

Referenced by operator()().

ast::Exp * object::DesugarVisitor::variant_exp ( const type::Class static_type,
const std::string &  exact_type,
const field_inits_type inits 
)
private

Generate a variant expression.

Parameters
static_typethe type of the class whose variant is built
exact_typethe exact type of the data stored in the variant
initsthe initalization value of the variant (must be of type dynamic_type).
Returns
the generated variant expression

References class_names_, misc::map< T, N >::end(), misc::map< T, N >::find(), parse::parse(), type::Class::subclasses_get(), and type::Class::super_get().

Referenced by cast_function(), operator()(), and variant_exp().

ast::Exp * object::DesugarVisitor::variant_exp ( const type::Class static_type,
const type::Class dynamic_type,
const field_inits_type inits 
)
private

Syntactic sugar for the previous routine.

References class_names_, and variant_exp().

parse::Tweast * object::DesugarVisitor::variant_ty ( const type::Class class_type)
private

Generate code for a variant type on a TWEAST.

Contrary to object::DesugarVisitor::variant_exp, we cannot directly generate an expression, since the client TWEAST won't accept an ast::Ty* as a metavariable.

Parameters
class_typethe type of the class whose variant is built
Returns
the TWEAST on which the code is generated

References class_names_, type::Class::subclasses_get(), and type::Class::super_get().

Referenced by operator()().

Member Data Documentation

parse::Tweast object::DesugarVisitor::class_ids_
private

TWEAST of class ids.

Referenced by operator()().

class_names_type object::DesugarVisitor::class_names_
private
ast::Ast* astclone::Cloner::result_
protectedinherited
parse::Tweast object::DesugarVisitor::types_
private

TWEAST of desugared types.

Referenced by operator()().


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