LRDE Tiger Compiler
1.34a $Id: 7fef12e1f5fa43449d667a0eec1d837c40fc1202 $
|
#include <type-checker.hh>
Public Types | |
typedef type::TypeChecker | super_type |
Superclass. |
Public Member Functions | |
TypeChecker (overfun_bindings_type *overfun_bindings) | |
virtual | ~TypeChecker () |
const misc::error & | error_get () const |
The error handler. | |
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
| |
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 | |
const Type * | type (ast::Typable &e) |
const Record * | type (const ast::fields_type &e) |
const Record * | type (const ast::VarDecs &e) |
virtual void | operator() (ast::SimpleVar &e) override |
virtual void | operator() (ast::NilExp &) override |
virtual void | operator() (ast::IntExp &) override |
virtual void | operator() (ast::StringExp &) override |
virtual void | operator() (ast::RecordExp &e) override |
virtual void | operator() (ast::OpExp &e) override |
virtual void | operator() (ast::VarDec &e) override |
Visit a single Variable Declaration. | |
virtual void | operator() (ast::NameTy &e) override |
virtual void | operator() (ast::RecordTy &e) override |
virtual void | operator() (ast::ArrayTy &e) override |
template<> | |
void | visit_dec_header (ast::FunctionDec &e) |
Visit the lhs of an ast::FunctionDec. | |
template<> | |
void | visit_dec_header (ast::TypeDec &e) |
Visit the lhs of an ast::TypeDec. | |
template<> | |
void | visit_dec_body (ast::FunctionDec &e) |
Visit the rhs of an ast::FunctionDec. | |
template<> | |
void | visit_dec_body (ast::TypeDec &e) |
Visit the rhs of an ast::TypeDec. | |
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 | error (const ast::Ast &loc, const std::string &msg, const T &exp) |
template<typename T , typename U > | |
void | error_and_recover (T &loc, const std::string &msg, const U &exp) |
void | type_mismatch (const ast::Ast &loc, const std::string &exp1, const Type &type1, const std::string &exp2, const Type &type2) |
Checking types. | |
void | check_type (ast::Exp &e, const std::string &s, const Type &t) |
Check the type of an Exp. | |
void | check_type (ast::Dec &e, const std::string &s, const Type &t) |
void | check_types (const ast::Ast &loc, const std::string &exp1, const Type &type1, const std::string &exp2, const Type &type2) |
Check the type compatibility. | |
void | check_types (const ast::Ast &loc, const std::string &exp1, ast::Typable &type1, const std::string &exp2, ast::Typable &type2) |
Setting types. | |
template<typename NodeType > | |
void | type_default (NodeType &e, const type::Type *type) |
template<typename NodeType > | |
void | created_type_default (NodeType &e, const type::Type *type) |
Same as type_default, but for a created type. | |
template<typename NodeType > | |
void | type_set (NodeType &e, const type::Type *type) |
Type and Function declarations. | |
virtual void | operator() (ast::FunctionDecs &e) override |
Visit a chunk of function declarations. | |
virtual void | operator() (ast::FunctionDec &) override |
No longer used. | |
virtual void | operator() (ast::TypeDecs &e) override |
Visit a chunk of type declarations. | |
virtual void | operator() (ast::TypeDec &) override |
No longer used. | |
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. | |
template<typename Routine_Type , typename Routine_Node > | |
void | visit_routine_body (Routine_Node &e) |
Generic type-checking of a routine's body. |
Protected Attributes | |
misc::error | error_ |
Error handler. | |
misc::set< const ast::VarDec * > | var_read_only_ |
Set of for index variable definitions, which are read only. |
Private Attributes | |
overfun_bindings_type * | overfun_bindings_ |
Perform type checking, allowing function overload, and compute the bindings of the functions.
Superclass.
overload::TypeChecker::TypeChecker | ( | overfun_bindings_type * | overfun_bindings | ) |
|
virtual |
|
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.
|
protectedinherited |
Check the type of an Exp.
e | the expression to check |
s | the string to refer to e in error messages (e.g., "index", "left operand" etc.). |
t | the expected type |
On failure, report the error, and set the type of e to t.
References type::TypeChecker::type(), and type::TypeChecker::type_mismatch().
|
protectedinherited |
References type::TypeChecker::type(), and type::TypeChecker::type_mismatch().
|
protectedinherited |
Check the type compatibility.
An error message is output if types are incompatible.
loc | a node which location is used to report errors |
exp1 | the syntactic category of the first entity, (used in error messages) |
type1 | the first type |
exp2 | same as above |
type2 | same as above |
Referenced by type::TypeChecker::check_types(), and object::TypeChecker::visit_dec_header().
|
protectedinherited |
References type::TypeChecker::check_types(), and type::TypeChecker::type().
|
protectedinherited |
Same as type_default, but for a created type.
Referenced by object::TypeChecker::operator()(), and type::TypeChecker::operator()().
|
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.
|
protectedinherited |
Be sure to read the documentation of bind::Binder::decs_visit.
When type-checking a function (or a type) we need two traversals: one to register the function's type (to enable mutual recursions) and a second one to check the bodies. This is why there are actually three methods: decs_visit, visit_dec_header, and visit_dec_body. 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.
|
protectedinherited |
Report an error.
References type::TypeChecker::error_, ast::Ast::location_get(), and misc::error::type.
Referenced by type::TypeChecker::error_and_recover(), object::TypeChecker::operator()(), type::TypeChecker::type(), and object::TypeChecker::visit_dec_header().
|
protectedinherited |
Report an error.
loc | the node whose location is used in the error message |
msg | the error message |
exp | the culprit. It must support << output |
References type::TypeChecker::error_, ast::Ast::location_get(), and misc::error::type.
|
protectedinherited |
Report an error, and recover from it.
loc | the node whose location is used in the error message and whose type is set to integer as an attempt to recover. |
msg | the error message |
exp | the culprit. It must support << output |
References type::TypeChecker::error().
Referenced by object::TypeChecker::operator()().
|
inherited |
The error handler.
References type::TypeChecker::error_.
Referenced by type::types_check(), overload::types_check(), and object::types_check().
|
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 >.
|
pure virtualinherited |
Implemented in ast::GenNonObjectVisitor< Const >, and ast::GenObjectVisitor< Const >.
|
virtualinherited |
The entry point: visit e.
Reimplemented from ast::GenVisitor< Const >.
Referenced by callgraph::CallGraphVisitor::operator()(), object::Renamer::operator()(), and translate::Translator::operator()().
|
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 >.
References unreached.
|
inherited |
Helper to visit nodes manipulated via a pointer.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
References unreached.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
References unreached.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
References unreached.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
References unreached.
|
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 >.
|
overridevirtualinherited |
Implements ast::GenVisitor< Const >.
|
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 >.
|
overrideprotectedvirtualinherited |
Reimplemented in object::TypeChecker.
References ast::SimpleVar::def_get(), and type::TypeChecker::type_default().
Referenced by object::TypeChecker::operator()().
|
overrideprotectedvirtualinherited |
References type::Nil::instance(), and type::TypeChecker::type_default().
|
overrideprotectedvirtualinherited |
References type::TypeChecker::type_default().
|
overrideprotectedvirtualinherited |
References type::TypeChecker::type_default().
|
overrideprotectedvirtualinherited |
|
overrideprotectedvirtualinherited |
|
overrideprotectedvirtualinherited |
Visit a chunk of function declarations.
Reimplemented in object::TypeChecker, and object::TypeChecker.
|
overrideprotectedvirtualinherited |
No longer used.
References unreached.
|
overrideprotectedvirtualinherited |
Visit a chunk of type declarations.
Reimplemented in object::TypeChecker, and object::TypeChecker.
|
overrideprotectedvirtualinherited |
No longer used.
References unreached.
|
overrideprotectedvirtualinherited |
Visit a single Variable Declaration.
Reimplemented in object::TypeChecker.
|
overrideprotectedvirtualinherited |
Reimplemented in object::TypeChecker.
References assertion, ast::NameTy::def_get(), ast::NameTy::name_get(), and type::TypeChecker::type_default().
|
overrideprotectedvirtualinherited |
|
overrideprotectedvirtualinherited |
|
protectedinherited |
Run this visitor on e, and return its type.
Note that it is also guaranteed that type_ is set to this type. More generally, using type allows to avoid calling accept directly.
Referenced by type::TypeChecker::check_type(), type::TypeChecker::check_types(), object::TypeChecker::operator()(), type::TypeChecker::operator()(), type::TypeChecker::type(), and object::TypeChecker::visit_dec_members().
|
protectedinherited |
References type::TypeChecker::error(), and type::TypeChecker::type().
|
protectedinherited |
References ast::AnyDecs< D >::decs_get(), type::Record::field_add(), and type::TypeChecker::type().
|
protectedinherited |
Set the type of a node, if it isn't already set (an existing type won't be overwritten).
e | the bound node |
type | the type |
Referenced by object::TypeChecker::operator()(), and type::TypeChecker::operator()().
|
protectedinherited |
Report a type_mismatch between two entities.
loc | the guilty node, used to report the location. |
exp1 | the string denoting the first exp. |
type1 | its type |
exp2 | similarly |
type2 | similarly |
References misc::decendl(), type::TypeChecker::error_, misc::iendl(), misc::incendl(), ast::Ast::location_get(), and misc::error::type.
Referenced by type::TypeChecker::check_type().
|
protectedinherited |
Set the type of a node (overwriting allowed).
e | the bound node |
type | the type |
|
protectedinherited |
|
protectedinherited |
Visit the rhs of an ast::FunctionDec.
|
protectedinherited |
Visit the rhs of an ast::TypeDec.
|
protectedinherited |
|
protectedinherited |
Visit the lhs of an ast::FunctionDec.
|
protectedinherited |
Visit the lhs of an ast::TypeDec.
|
protectedinherited |
Generic type-checking of a routine's body.
|
protectedinherited |
Error handler.
Referenced by type::TypeChecker::error(), type::TypeChecker::error_get(), and type::TypeChecker::type_mismatch().
|
private |
|
protectedinherited |
Set of for index variable definitions, which are read only.