tipc
A TIP to LLVM compiler
|
Visitor generates type constraints and processes them. More...
#include <TypeConstraintVisitor.h>
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... | |
![]() | |
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< TipType > | astToVar (ASTNode *n) |
Protected Attributes | |
std::shared_ptr< ConstraintHandler > | constraintHandler |
SymbolTable * | symbolTable |
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.
|
delete |
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.
|
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.
|
overridevirtual |
Type constraints for heap allocation.
Type Rules for "alloc E": [[alloc E]] = &[[E]]
Reimplemented from ASTVisitor.
|
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.
|
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.
|
overridevirtual |
Type constraints for pointer dereference.
Type Rules for "*E": [[E]] = &[[*E]]
Reimplemented from ASTVisitor.
|
overridevirtual |
Type constraints for error statement.
Type rules for "error E": [[E]] = int
Reimplemented from ASTVisitor.
|
overridevirtual |
Type constraints for function application.
Type Rules for "E(E1, ..., En)": [[E]] = ([[E1]], ..., [[En]]) -> [[E(E1, ..., En)]]
Reimplemented from ASTVisitor.
|
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.
|
overridevirtual |
Type constraints for if statement.
Type rules for "if (E) S1 else S2": [[E]] = int
Reimplemented from ASTVisitor.
|
overridevirtual |
Type constraints for input statement.
Type rules for "input": [[input]] = int
Reimplemented from ASTVisitor.
|
overridevirtual |
Type constraints for null literal.
Type Rules for "null": [[null]] = & \alpha
Reimplemented from ASTVisitor.
|
overridevirtual |
Type constraints for numeric literal.
Type rules for "I": [[I]] = int
Reimplemented from ASTVisitor.
|
overridevirtual |
Type constraints for output statement.
Type rules for "output E": [[E]] = int
Reimplemented from ASTVisitor.
|
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.
|
overridevirtual |
Type constraints for address of.
Type Rules for "&X": [[&X]] = &[[X]]
Reimplemented from ASTVisitor.
|
overridevirtual |
Type constraints for while loop.
Type rules for "while (E) S": [[E]] = int
Reimplemented from ASTVisitor.
|
overridevirtual |
Reimplemented from ASTVisitor.
|
protected |
|
protected |