LRDE Tiger Compiler
1.34a $Id: 7fef12e1f5fa43449d667a0eec1d837c40fc1202 $
|
Desugar some object structures while duplicating an Ast. More...
#include <desugar-visitor.hh>
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_type * | recurse_args (const ast::exps_type &actuals, const type::Record &formals) |
Desugar actuals arguments in routine calls. | |
ast::Ast * | result_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
| |
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::Ast * | result_ |
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::Exp * | variant_exp (const type::Class *static_type, const std::string &exact_type, const field_inits_type &inits) |
Generate a variant expression. | |
ast::Exp * | variant_exp (const type::Class *static_type, const type::Class *dynamic_type, const field_inits_type &inits) |
Syntactic sugar for the previous routine. | |
parse::Tweast * | variant_ty (const type::Class *class_type) |
Generate code for a variant type on a TWEAST. | |
parse::Tweast * | cast_function (const std::string &name, const type::Class *source, const type::Class *target, const type::Class *exact_type) |
Generate a conversion routine. | |
parse::Tweast * | upcast_function (const type::Class *source, const type::Class *target) |
parse::Tweast * | downcast_function (const type::Class *source, const type::Class *target) |
ast::Exp * | dispatch_switch (const type::Class *class_type, const type::Method *method) |
Generate an expression looking like a switch expression, to dispatch a method call. | |
parse::Tweast * | method_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. |
Desugar some object structures while duplicating an Ast.
typedef std::list<const type::Class*> object::DesugarVisitor::classes_type |
A list of classes (e.g., useful to represent a set of subclasses).
|
private |
The type of a list of initializations for the field of a variant.
Superclass.
object::DesugarVisitor::DesugarVisitor | ( | const class_names_type & | class_names | ) |
Build a DesugarVisitor.
|
virtual |
Destroy a DesugarVisitor.
|
protectedinherited |
A convenient shortcut for recurring code like 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:
won't work directly. Of course, one can help the compiler, providing it with E
but this is painful.
|
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().
|
private |
Generate a conversion routine.
name | the type of the generated function |
source | the type of the converted value |
target | the target type of the conversion |
exact_type | the exact type of the returned variant |
References class_names_, misc::put(), type::Class::subclasses_get(), type::Class::super_get(), and variant_exp().
Referenced by downcast_function(), and upcast_function().
|
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.
|
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.
|
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()().
|
private |
Generate an expression looking like a switch expression, to dispatch a method call.
class_type | the static type of the class owning the method |
method | the called method |
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()().
|
private |
Return the name of the downcast function between types from and to.
References class_names_.
Referenced by dispatch_switch(), and downcast_function().
|
private |
Syntactic sugar for object::DesugarVisitor::cast_function, used to generate a downcast function.
References cast_function(), and downcast_fun_name().
Referenced by operator()().
|
private |
Generate a (static) call to a desugared method.
class_name | the class where the method resides |
method_name | the name of the method |
target | the name of the target (object) |
formals | the actual arguments (other than the target) |
Referenced by dispatch_switch().
|
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_.
|
pure virtualinherited |
Implemented in ast::GenNonObjectVisitor< Const >, and ast::GenObjectVisitor< Const >.
|
overridevirtual |
Desugar class declarations.
Reimplemented from astclone::Cloner.
References adapt_type(), assertion, ast::FunctionDec::body_get(), class_ids_, class_names_, ast::AnyDecs< D >::decs_get(), dispatch_fun_name(), dispatch_switch(), downcast_function(), ast::FunctionDec::formals_get(), type::Class::has_data(), type::Class::id_get(), ast::VarDec::init_get(), type::Class::meths_get(), ast::Dec::name_get(), parse::parse(), astclone::Cloner::recurse(), astclone::Cloner::result_, ast::FunctionDec::result_get(), type::Class::subclasses_get(), type::Class::super_get(), ast::VarDec::type_name_get(), types_, upcast_function(), variant_exp(), and variant_ty().
|
overridevirtual |
Handle the builtin Object.
Reimplemented from astclone::Cloner.
References ast::FunctionDec::body_get(), class_ids_, type::Class::id_get(), ast::Dec::name_get(), type::Class::object_instance(), astclone::Cloner::operator()(), parse::parse(), misc::put(), astclone::Cloner::result_, ast::FunctionDec::result_get(), ast::DecsList::splice_back(), types_, upcast_fun_name(), variant_exp(), and variant_ty().
|
overridevirtual |
Desugar class names.
Reimplemented from astclone::Cloner.
References class_names_, ast::Ast::location_get(), astclone::Cloner::operator()(), and astclone::Cloner::result_.
|
pure virtualinherited |
Implemented in ast::GenNonObjectVisitor< Const >, and ast::GenObjectVisitor< Const >.
|
pure virtualinherited |
Implemented in ast::GenNonObjectVisitor< Const >, and ast::GenObjectVisitor< Const >.
|
pure virtualinherited |
Implemented in ast::GenNonObjectVisitor< Const >, and ast::GenObjectVisitor< Const >.
|
overridevirtual |
Desugar polymorphic initializations.
Reimplemented from astclone::Cloner.
References adapt_type(), ast::VarDec::init_get(), type::Nil::instance(), ast::Ast::location_get(), ast::Dec::name_get(), astclone::Cloner::operator()(), postcondition, astclone::Cloner::recurse(), astclone::Cloner::result_, and ast::VarDec::type_name_get().
|
overridevirtualinherited |
Referenced by operator()().
|
overridevirtualinherited |
|
overridevirtual |
Desugar manifest objects.
Reimplemented from astclone::Cloner.
|
overridevirtualinherited |
|
virtualinherited |
The entry point: visit e.
Reimplemented from ast::GenVisitor< Const >.
Referenced by callgraph::CallGraphVisitor::operator()(), object::Renamer::operator()(), and translate::Translator::operator()().
|
overridevirtual |
Desugar polymorphic assignments.
Reimplemented from astclone::Cloner.
References adapt_type(), type::Nil::instance(), astclone::Cloner::operator()(), parse::parse(), astclone::Cloner::recurse(), and astclone::Cloner::result_.
|
overridevirtualinherited |
|
overridevirtual |
Add object casts around arguments when needed.
Reimplemented from astclone::Cloner.
References misc::symbol::name_get(), recurse_args(), and astclone::Cloner::result_.
|
overridevirtualinherited |
|
overridevirtualinherited |
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Reimplemented in desugar::DesugarVisitor.
References ast::ForExp::body_get(), ast::ForExp::hi_get(), ast::Ast::location_get(), astclone::Cloner::recurse(), astclone::Cloner::result_, and ast::ForExp::vardec_get().
|
overridevirtual |
Desugar accesses to attributes.
Reimplemented from astclone::Cloner.
References assertion, class_names_, misc::symbol::name_get(), astclone::Cloner::operator()(), parse::parse(), astclone::Cloner::recurse(), astclone::Cloner::result_, and type::Class::super_get().
|
overridevirtualinherited |
|
overridevirtualinherited |
References ast::Ast::location_get(), astclone::Cloner::result_, and ast::IntExp::value_get().
|
overridevirtualinherited |
|
overridevirtual |
Desugar calls to methods.
Reimplemented from astclone::Cloner.
References adapt_type(), dispatch_fun_name(), astclone::Cloner::recurse(), recurse_args(), and astclone::Cloner::result_.
|
inherited |
Helper to visit nodes manipulated via a pointer.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
References ast::Ast::location_get(), and astclone::Cloner::result_.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Reimplemented in desugar::DesugarVisitor.
References ast::OpExp::left_get(), ast::Ast::location_get(), ast::OpExp::oper_get(), astclone::Cloner::recurse(), astclone::Cloner::result_, and ast::OpExp::right_get().
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
References ast::SimpleVar::name_get(), and astclone::Cloner::result_.
|
overridevirtualinherited |
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
|
overridevirtualinherited |
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Visit a list of function, type and/or variables declarations.
Implements ast::GenVisitor< Const >.
References misc::for_each().
|
virtualinherited |
Visit a Decs chunks.
|
overridevirtualinherited |
Visit Var declarations.
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
inherited |
References assertion.
Referenced by operator()(), astclone::Cloner::operator()(), and recurse_args().
|
inherited |
References assertion.
|
virtual |
Desugar actuals arguments in routine calls.
actuals | the actual arguments of the initial AST |
formals | the formal arguments of the routine |
References type::Type::actual(), adapt_type(), type::Record::begin(), type::Record::end(), type::Nil::instance(), and astclone::Cloner::recurse().
Referenced by operator()().
|
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()().
|
inherited |
References astclone::Cloner::result_.
Referenced by astclone::clone(), inlining::inline_expand(), inlining::prune(), desugar::raw_bound_checks_add(), desugar::raw_desugar(), and object::raw_desugar().
|
private |
Return the name of a type.
type | either a builtin type or a type::Named |
References class_names_.
|
private |
Return the name of the upcast function between types from and to.
References class_names_.
Referenced by adapt_type(), operator()(), and upcast_function().
|
private |
Syntactic sugar for object::DesugarVisitor::cast_function, used to generate an upcast function.
References cast_function(), and upcast_fun_name().
Referenced by operator()().
|
private |
Generate a variant expression.
static_type | the type of the class whose variant is built |
exact_type | the exact type of the data stored in the variant |
inits | the initalization value of the variant (must be of type dynamic_type). |
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().
|
private |
Syntactic sugar for the previous routine.
References class_names_, and variant_exp().
|
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.
class_type | the type of the class whose variant is built |
References class_names_, type::Class::subclasses_get(), and type::Class::super_get().
Referenced by operator()().
|
private |
TWEAST of class ids.
Referenced by operator()().
|
private |
The names of the classes defined in the program.
Referenced by cast_function(), dispatch_fun_name(), dispatch_switch(), downcast_fun_name(), operator()(), type_symbol(), upcast_fun_name(), variant_exp(), and variant_ty().
|
protectedinherited |
The cloned Ast.
Referenced by operator()(), astclone::Cloner::operator()(), and astclone::Cloner::result_get().
|
private |
TWEAST of desugared types.
Referenced by operator()().