tipc
A TIP to LLVM compiler
Public Member Functions | Static Public Member Functions | List of all members
TypeInference Class Reference

Perform type inference and checking. More...

#include <TypeInference.h>

Public Member Functions

 TypeInference (SymbolTable *s, std::shared_ptr< Unifier > u)
 
std::shared_ptr< TipTypegetInferredType (ASTDeclNode *node)
 Returns the type expression inferred for the given ASTDeclNode. More...
 
void print (std::ostream &os)
 Print type inference results to output stream. More...
 

Static Public Member Functions

static std::shared_ptr< TypeInferencerun (ASTProgram *ast, bool polyInf, CallGraph *cg, SymbolTable *symbols)
 Generate and solve type constraints and report any errors. More...
 

Detailed Description

Perform type inference and checking.

This class provides the check method to run type inference/checking on a given program. The inferred types for names declared in the program can then be accessed.

Constructor & Destructor Documentation

◆ TypeInference()

TypeInference::TypeInference ( SymbolTable s,
std::shared_ptr< Unifier u 
)
inline

Member Function Documentation

◆ getInferredType()

TypeInference::getInferredType ( ASTDeclNode node)

Returns the type expression inferred for the given ASTDeclNode.

After type checking completes the inferred types can be accessed for declared names. Note that it is possible to declare a variable, but never use it, in which case the inferred type will be a free type variable. A managed pointer is returned for exactly this case – when a fresh variable is generated and returned. In other cases, the resulting type will be shared with those computed during inference – hence a shared pointer.

See also
TipType
ASTDeclNode
Parameters
nodeAn AST declaration node.
Returns
A shared pointer to the inferred type for the AST node.

◆ print()

void TypeInference::print ( std::ostream &  os)

Print type inference results to output stream.

◆ run()

TypeInference::run ( ASTProgram ast,
bool  polyInf,
CallGraph cg,
SymbolTable symbols 
)
static

Generate and solve type constraints and report any errors.

Visits the AST generating type constraints for each expression and solves the resulting constraints. If a term unification error is detected a UnificationError, a subtype of SemanticError, is raised. If an access to a field whose definition is absent is detected a SemanticError is raised. If no errors are found, then the inferred types for all symbols are recorded in the symbol table.

See also
UnificationError
SemanticError
Parameters
astThe program AST
polyInfFlag indicating whether to perform polymorphic or monomorphic inference
cgThe program call graph
symbolsThe symbol table

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