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

#include <type-checker.hh>

Inheritance diagram for type::TypeChecker:
Collaboration diagram for type::TypeChecker:

Public Types

typedef ast::DefaultVisitor super_type

Public Member Functions

 TypeChecker ()
 Construction.
const misc::errorerror_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

  • 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

const Typetype (ast::Typable &e)
const Recordtype (const ast::fields_type &e)
const Recordtype (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_body (ast::FunctionDec &e)
 Visit the rhs 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::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.
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.
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.

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.

Member Typedef Documentation

Constructor & Destructor Documentation

type::TypeChecker::TypeChecker ( )

Construction.

Reimplemented in object::TypeChecker.

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 type::TypeChecker::check_type ( ast::Exp e,
const std::string &  s,
const Type t 
)
protected

Check the type of an Exp.

Parameters
ethe expression to check
sthe string to refer to e in error messages (e.g., "index", "left operand" etc.).
tthe expected type

On failure, report the error, and set the type of e to t.

References type(), and type_mismatch().

void type::TypeChecker::check_type ( ast::Dec e,
const std::string &  s,
const Type t 
)
protected

References type(), and type_mismatch().

void type::TypeChecker::check_types ( const ast::Ast loc,
const std::string &  exp1,
const Type type1,
const std::string &  exp2,
const Type type2 
)
protected

Check the type compatibility.

An error message is output if types are incompatible.

Parameters
loca node which location is used to report errors
exp1the syntactic category of the first entity, (used in error messages)
type1the first type
exp2same as above
type2same as above

Referenced by check_types(), and object::TypeChecker::visit_dec_header().

void type::TypeChecker::check_types ( const ast::Ast loc,
const std::string &  exp1,
ast::Typable &  type1,
const std::string &  exp2,
ast::Typable &  type2 
)
protected

References check_types(), and type().

template<typename NodeType >
void type::TypeChecker::created_type_default ( NodeType &  e,
const type::Type type 
)
protected

Same as type_default, but for a created type.

Referenced by object::TypeChecker::operator()(), and operator()().

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<class D >
void type::TypeChecker::decs_visit ( ast::AnyDecs< D > &  e)
protected

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.

void type::TypeChecker::error ( const ast::Ast loc,
const std::string &  msg 
)
protected
template<typename T >
void type::TypeChecker::error ( const ast::Ast loc,
const std::string &  msg,
const T &  exp 
)
protected

Report an error.

Parameters
locthe node whose location is used in the error message
msgthe error message
expthe culprit. It must support << output

References error_, ast::Ast::location_get(), and misc::error::type.

template<typename T , typename U >
void type::TypeChecker::error_and_recover ( T &  loc,
const std::string &  msg,
const U &  exp 
)
protected

Report an error, and recover from it.

Parameters
locthe node whose location is used in the error message and whose type is set to integer as an attempt to recover.
msgthe error message
expthe culprit. It must support << output

References error().

Referenced by object::TypeChecker::operator()().

const misc::error & type::TypeChecker::error_get ( ) const

The error handler.

References error_.

Referenced by type::types_check(), overload::types_check(), and object::types_check().

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
template<template< typename > class Const>
void ast::GenDefaultVisitor< Const >::operator() ( typename Const< Ast >::type &  e)
virtualinherited
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
template<template< typename > class Const>
void ast::GenNonObjectVisitor< Const >::operator() ( typename Const< ClassTy >::type &  e)
overridevirtualinherited

Implements ast::GenVisitor< Const >.

References unreached.

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

Implements ast::GenVisitor< Const >.

References unreached.

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
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
void type::TypeChecker::operator() ( ast::SimpleVar e)
overrideprotectedvirtual
void type::TypeChecker::operator() ( ast::NilExp e)
overrideprotectedvirtual
void type::TypeChecker::operator() ( ast::IntExp e)
overrideprotectedvirtual

References type_default().

void type::TypeChecker::operator() ( ast::StringExp &  e)
overrideprotectedvirtual

References type_default().

virtual void type::TypeChecker::operator() ( ast::RecordExp &  e)
overrideprotectedvirtual
virtual void type::TypeChecker::operator() ( ast::OpExp e)
overrideprotectedvirtual
void type::TypeChecker::operator() ( ast::FunctionDecs e)
overrideprotectedvirtual

Visit a chunk of function declarations.

Reimplemented in object::TypeChecker, and object::TypeChecker.

void type::TypeChecker::operator() ( ast::FunctionDec )
overrideprotectedvirtual

No longer used.

References unreached.

virtual void type::TypeChecker::operator() ( ast::TypeDecs e)
overrideprotectedvirtual

Visit a chunk of type declarations.

Reimplemented in object::TypeChecker, and object::TypeChecker.

void type::TypeChecker::operator() ( ast::TypeDec )
overrideprotectedvirtual

No longer used.

References unreached.

void type::TypeChecker::operator() ( ast::VarDec e)
overrideprotectedvirtual

Visit a single Variable Declaration.

Reimplemented in object::TypeChecker.

void type::TypeChecker::operator() ( ast::NameTy e)
overrideprotectedvirtual
void type::TypeChecker::operator() ( ast::RecordTy &  e)
overrideprotectedvirtual
void type::TypeChecker::operator() ( ast::ArrayTy e)
overrideprotectedvirtual
const Type * type::TypeChecker::type ( ast::Typable &  e)
protected

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 check_type(), check_types(), object::TypeChecker::operator()(), operator()(), type(), and object::TypeChecker::visit_dec_members().

const Record * type::TypeChecker::type ( const ast::fields_type e)
protected

References error(), and type().

const Record * type::TypeChecker::type ( const ast::VarDecs e)
protected
template<typename NodeType >
void type::TypeChecker::type_default ( NodeType &  e,
const type::Type type 
)
protected

Set the type of a node, if it isn't already set (an existing type won't be overwritten).

Parameters
ethe bound node
typethe type

Referenced by object::TypeChecker::operator()(), and operator()().

void type::TypeChecker::type_mismatch ( const ast::Ast loc,
const std::string &  exp1,
const Type type1,
const std::string &  exp2,
const Type type2 
)
protected

Report a type_mismatch between two entities.

Parameters
locthe guilty node, used to report the location.
exp1the string denoting the first exp.
type1its type
exp2similarly
type2similarly

References misc::decendl(), error_, misc::iendl(), misc::incendl(), ast::Ast::location_get(), and misc::error::type.

Referenced by check_type().

template<typename NodeType >
void type::TypeChecker::type_set ( NodeType &  e,
const type::Type type 
)
protected

Set the type of a node (overwriting allowed).

Parameters
ethe bound node
typethe type
template<class D >
void type::TypeChecker::visit_dec_body ( D &  e)
protected

Check a Function or Type declaration body.

template<>
void type::TypeChecker::visit_dec_body ( ast::FunctionDec e)
protected

Visit the rhs of an ast::FunctionDec.

template<>
void type::TypeChecker::visit_dec_body ( ast::TypeDec e)
protected

Visit the rhs of an ast::TypeDec.

template<class D >
void type::TypeChecker::visit_dec_header ( D &  e)
protected

Check a Function or Type declaration header.

template<>
void type::TypeChecker::visit_dec_header ( ast::FunctionDec e)
protected

Visit the lhs of an ast::FunctionDec.

template<>
void type::TypeChecker::visit_dec_header ( ast::TypeDec e)
protected

Visit the lhs of an ast::TypeDec.

template<typename Routine_Type , typename Routine_Node >
void type::TypeChecker::visit_routine_body ( Routine_Node &  e)
protected

Generic type-checking of a routine's body.

Member Data Documentation

misc::error type::TypeChecker::error_
protected

Error handler.

Referenced by error(), error_get(), and type_mismatch().

misc::set<const ast::VarDec*> type::TypeChecker::var_read_only_
protected

Set of for index variable definitions, which are read only.


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