tipc
A TIP to LLVM compiler
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
TypeConstraintVisitor Class Reference

Visitor generates type constraints and processes them. More...

#include <TypeConstraintVisitor.h>

Inheritance diagram for TypeConstraintVisitor:
Inheritance graph
[legend]
Collaboration diagram for TypeConstraintVisitor:
Collaboration graph
[legend]

Public Member Functions

 TypeConstraintVisitor ()=delete
 
 TypeConstraintVisitor (SymbolTable *st, std::shared_ptr< ConstraintHandler > handler)
 
bool visit (ASTFunction *element) override
 
void endVisit (ASTAccessExpr *element) override
 Type constraints for field access. More...
 
void endVisit (ASTAllocExpr *element) override
 Type constraints for heap allocation. More...
 
void endVisit (ASTAssignStmt *element) override
 Type rules for assignments. More...
 
void endVisit (ASTBinaryExpr *element) override
 Type constraints for binary operator. More...
 
void endVisit (ASTDeRefExpr *element) override
 Type constraints for pointer dereference. More...
 
void endVisit (ASTErrorStmt *element) override
 Type constraints for error statement. More...
 
void endVisit (ASTFunAppExpr *element) override
 Type constraints for function application. More...
 
void endVisit (ASTFunction *element) override
 Type constraints for function definition. More...
 
void endVisit (ASTIfStmt *element) override
 Type constraints for if statement. More...
 
void endVisit (ASTInputExpr *element) override
 Type constraints for input statement. More...
 
void endVisit (ASTNullExpr *element) override
 Type constraints for null literal. More...
 
void endVisit (ASTNumberExpr *element) override
 Type constraints for numeric literal. More...
 
void endVisit (ASTOutputStmt *element) override
 Type constraints for output statement. More...
 
void endVisit (ASTRecordExpr *element) override
 Type constraints for record expression. More...
 
void endVisit (ASTRefExpr *element) override
 Type constraints for address of. More...
 
void endVisit (ASTWhileStmt *element) override
 Type constraints for while loop. More...
 
- Public Member Functions inherited from ASTVisitor
virtual bool visit (ASTProgram *element)
 
virtual void endVisit (ASTProgram *element)
 
virtual bool visit (ASTNumberExpr *element)
 
virtual bool visit (ASTVariableExpr *element)
 
virtual void endVisit (ASTVariableExpr *element)
 
virtual bool visit (ASTBinaryExpr *element)
 
virtual bool visit (ASTInputExpr *element)
 
virtual bool visit (ASTFunAppExpr *element)
 
virtual bool visit (ASTAllocExpr *element)
 
virtual bool visit (ASTRefExpr *element)
 
virtual bool visit (ASTDeRefExpr *element)
 
virtual bool visit (ASTNullExpr *element)
 
virtual bool visit (ASTFieldExpr *element)
 
virtual void endVisit (ASTFieldExpr *element)
 
virtual bool visit (ASTRecordExpr *element)
 
virtual bool visit (ASTAccessExpr *element)
 
virtual bool visit (ASTDeclNode *element)
 
virtual void endVisit (ASTDeclNode *element)
 
virtual bool visit (ASTDeclStmt *element)
 
virtual void endVisit (ASTDeclStmt *element)
 
virtual bool visit (ASTAssignStmt *element)
 
virtual bool visit (ASTWhileStmt *element)
 
virtual bool visit (ASTIfStmt *element)
 
virtual bool visit (ASTOutputStmt *element)
 
virtual bool visit (ASTReturnStmt *element)
 
virtual void endVisit (ASTReturnStmt *element)
 
virtual bool visit (ASTErrorStmt *element)
 
virtual bool visit (ASTBlockStmt *element)
 
virtual void endVisit (ASTBlockStmt *element)
 

Protected Member Functions

std::shared_ptr< TipTypeastToVar (ASTNode *n)
 

Protected Attributes

std::shared_ptr< ConstraintHandlerconstraintHandler
 
SymbolTablesymbolTable
 

Detailed Description

Visitor generates type constraints and processes them.

This visitor performs a post-order visit of the program AST. It constructs type constraints for each node that requires a type judgement. The constraints are then processed by a concrete implemntation of A ConstraintHandler. This provides flexibility in using the visitor - it can simply record the constraints or it can solve them on the fly.

Constructor & Destructor Documentation

◆ TypeConstraintVisitor() [1/2]

TypeConstraintVisitor::TypeConstraintVisitor ( )
delete

◆ TypeConstraintVisitor() [2/2]

TypeConstraintVisitor::TypeConstraintVisitor ( SymbolTable st,
std::shared_ptr< ConstraintHandler handler 
)

Construct a TypeConstraintVisitor from the given SymbolTable and ConstraintHandler.

Subclasses define the handler (eg. TypeConstraintUnifyVisitor defines the ConstraintUnfier as its handler) and pass it up to the base class during construction. This allows us to not call virtual methods in the constructor.

Scott Meyers defines this pattern in his Effective C++ series. You can read more about it here, https://www.aristeia.com/EC3E/3E_item9.pdf.

Member Function Documentation

◆ astToVar()

std::shared_ptr< TipType > TypeConstraintVisitor::astToVar ( ASTNode n)
protected

◆ endVisit() [1/16]

void TypeConstraintVisitor::endVisit ( ASTAccessExpr element)
overridevirtual

Type constraints for field access.

Type rule for "E.X": [[E]] = { f1:v1, ..., fn:vn } where fi is the ith field in the program's global record and vi = [[E.X]] if fi = X and \alpha otherwise

Reimplemented from ASTVisitor.

◆ endVisit() [2/16]

void TypeConstraintVisitor::endVisit ( ASTAllocExpr element)
overridevirtual

Type constraints for heap allocation.

Type Rules for "alloc E": [[alloc E]] = &[[E]]

Reimplemented from ASTVisitor.

◆ endVisit() [3/16]

void TypeConstraintVisitor::endVisit ( ASTAssignStmt element)
overridevirtual

Type rules for assignments.

Type rules for "E1 = E": [[E1]] = [[E2]]

Type rules for "*E1 = E2": [[E1]] = &[[E2]]

Note that these are slightly more general than the rules in the SPA book. The first allows for record expressions on the left hand side and the second allows for more complex assignments, e.g., "**p = &x"

Reimplemented from ASTVisitor.

◆ endVisit() [4/16]

void TypeConstraintVisitor::endVisit ( ASTBinaryExpr element)
overridevirtual

Type constraints for binary operator.

Type rules for "E1 op E2": [[E1 op E2]] = int and if "op" is not equality or disequality [[E1]] = [[E2]] = int otherwise [[E1]] = [[E2]]

Reimplemented from ASTVisitor.

◆ endVisit() [5/16]

void TypeConstraintVisitor::endVisit ( ASTDeRefExpr element)
overridevirtual

Type constraints for pointer dereference.

Type Rules for "*E": [[E]] = &[[*E]]

Reimplemented from ASTVisitor.

◆ endVisit() [6/16]

void TypeConstraintVisitor::endVisit ( ASTErrorStmt element)
overridevirtual

Type constraints for error statement.

Type rules for "error E": [[E]] = int

Reimplemented from ASTVisitor.

◆ endVisit() [7/16]

void TypeConstraintVisitor::endVisit ( ASTFunAppExpr element)
overridevirtual

Type constraints for function application.

Type Rules for "E(E1, ..., En)": [[E]] = ([[E1]], ..., [[En]]) -> [[E(E1, ..., En)]]

Reimplemented from ASTVisitor.

◆ endVisit() [8/16]

void TypeConstraintVisitor::endVisit ( ASTFunction element)
overridevirtual

Type constraints for function definition.

Type rules for "main(X1, ..., Xn) { ... return E; }": [[X1]] = [[Xn]] = [[E]] = int To express this we will equate all type variables to int.

Type rules for "X(X1, ..., Xn) { ... return E; }": [[X]] = ([[X1]], ..., [[Xn]]) -> [[E]]

Reimplemented from ASTVisitor.

◆ endVisit() [9/16]

void TypeConstraintVisitor::endVisit ( ASTIfStmt element)
overridevirtual

Type constraints for if statement.

Type rules for "if (E) S1 else S2": [[E]] = int

Reimplemented from ASTVisitor.

◆ endVisit() [10/16]

void TypeConstraintVisitor::endVisit ( ASTInputExpr element)
overridevirtual

Type constraints for input statement.

Type rules for "input": [[input]] = int

Reimplemented from ASTVisitor.

◆ endVisit() [11/16]

void TypeConstraintVisitor::endVisit ( ASTNullExpr element)
overridevirtual

Type constraints for null literal.

Type Rules for "null": [[null]] = & \alpha

Reimplemented from ASTVisitor.

◆ endVisit() [12/16]

void TypeConstraintVisitor::endVisit ( ASTNumberExpr element)
overridevirtual

Type constraints for numeric literal.

Type rules for "I": [[I]] = int

Reimplemented from ASTVisitor.

◆ endVisit() [13/16]

void TypeConstraintVisitor::endVisit ( ASTOutputStmt element)
overridevirtual

Type constraints for output statement.

Type rules for "output E": [[E]] = int

Reimplemented from ASTVisitor.

◆ endVisit() [14/16]

void TypeConstraintVisitor::endVisit ( ASTRecordExpr element)
overridevirtual

Type constraints for record expression.

Type rule for "{ X1:E1, ..., Xn:En }": [[{ X1:E1, ..., Xn:En }]] = { f1:v1, ..., fn:vn } where fi is the ith field in the program's global record and vi = [[Ei]] if fi = Xi and \alpha otherwise

Reimplemented from ASTVisitor.

◆ endVisit() [15/16]

void TypeConstraintVisitor::endVisit ( ASTRefExpr element)
overridevirtual

Type constraints for address of.

Type Rules for "&X": [[&X]] = &[[X]]

Reimplemented from ASTVisitor.

◆ endVisit() [16/16]

void TypeConstraintVisitor::endVisit ( ASTWhileStmt element)
overridevirtual

Type constraints for while loop.

Type rules for "while (E) S": [[E]] = int

Reimplemented from ASTVisitor.

◆ visit()

bool TypeConstraintVisitor::visit ( ASTFunction element)
overridevirtual

Reimplemented from ASTVisitor.

Member Data Documentation

◆ constraintHandler

std::shared_ptr<ConstraintHandler> TypeConstraintVisitor::constraintHandler
protected

◆ symbolTable

SymbolTable* TypeConstraintVisitor::symbolTable
protected

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