tipc
A TIP to LLVM compiler
Public Member Functions | Protected Member Functions | Friends | List of all members
ASTNode Class Referenceabstract

Abstract base class for all AST nodes. More...

#include <ASTNode.h>

Inheritance diagram for ASTNode:
Inheritance graph
[legend]

Public Member Functions

virtual ~ASTNode ()=default
 
virtual void accept (ASTVisitor *visitor)=0
 Visit the children of this node and apply the visitor. More...
 
virtual llvm::Value * codegen ()=0
 Perform code generation and return an LLVM value the code. More...
 
virtual std::vector< std::shared_ptr< ASTNode > > getChildren ()
 Return all of the children for the node. More...
 
void setLocation (int l, int c)
 
int getLine ()
 
int getColumn ()
 

Protected Member Functions

virtual std::ostream & print (std::ostream &out) const =0
 

Friends

std::ostream & operator<< (std::ostream &os, const ASTNode &obj)
 

Detailed Description

Abstract base class for all AST nodes.

ASTNodes define the elements of the program in a tree structured form. A key concept to undestand is that of "ownership". Every node in the tree is "owned" by its parent. The parent maintains a "shared pointer" to each of its children. This greatly simplifies memory management as the tree can be reclaimed once its root is no longer needed. It is important to understand that the "shared pointer" is only used to manage the lifetime of the child nodes. The values of those pointers can be passed around freely to other parts of the compiler that want to view the ASTNodes.

Each node has information about the line and column in the source program on which the element begins. Subtypes define "<<" operator that print a custom representation of the ASTNode, by overriding the print method.

There are two virtual methods that are used in a generic visitor and for code generation pass.

Constructor & Destructor Documentation

◆ ~ASTNode()

virtual ASTNode::~ASTNode ( )
virtualdefault

Member Function Documentation

◆ accept()

ASTNode::accept ( ASTVisitor visitor)
pure virtual

Visit the children of this node and apply the visitor.

This virtual function is overridden by each ASTNode subtype in order to visit each of its child nodes, by calling their accept methods. The visitor parameter defines the operations that are applied to each node that is visited; a subtype of ASTVisitor defines those operations.

Parameters
visitorThe subtype of ASTVisitor that carries out per-ASTNode work.

Implemented in ASTWhileStmt, ASTVariableExpr, ASTReturnStmt, ASTRefExpr, ASTRecordExpr, ASTProgram, ASTOutputStmt, ASTNumberExpr, ASTNullExpr, ASTInputExpr, ASTIfStmt, ASTFunction, ASTFunAppExpr, ASTFieldExpr, ASTErrorStmt, ASTDeRefExpr, ASTDeclStmt, ASTDeclNode, ASTBlockStmt, ASTBinaryExpr, ASTAssignStmt, ASTAllocExpr, and ASTAccessExpr.

◆ codegen()

ASTNode::codegen ( )
pure virtual

Perform code generation and return an LLVM value the code.

This virtual function is overridden by each ASTNode subtype in order to generate the executable code. This function does not use the visitor due to the fact that a high-degree of control on the ordering of the nodes is required.

Returns
LLVM value holding an representation of the generated code.

Implemented in ASTWhileStmt, ASTVariableExpr, ASTReturnStmt, ASTRefExpr, ASTRecordExpr, ASTOutputStmt, ASTNumberExpr, ASTNullExpr, ASTInputExpr, ASTIfStmt, ASTFunction, ASTFunAppExpr, ASTFieldExpr, ASTErrorStmt, ASTDeRefExpr, ASTDeclStmt, ASTDeclNode, ASTBlockStmt, ASTBinaryExpr, ASTAssignStmt, ASTAllocExpr, and ASTAccessExpr.

◆ getChildren()

ASTNode::getChildren ( )
inlinevirtual

Return all of the children for the node.

This is not a pure virtual function so subclasses can selectively override it.

Returns
a collection of the nodes children.

Reimplemented in ASTWhileStmt, ASTReturnStmt, ASTRefExpr, ASTRecordExpr, ASTProgram, ASTOutputStmt, ASTIfStmt, ASTFunction, ASTFunAppExpr, ASTFieldExpr, ASTErrorStmt, ASTDeRefExpr, ASTDeclStmt, ASTBlockStmt, ASTBinaryExpr, ASTAssignStmt, ASTAllocExpr, and ASTAccessExpr.

◆ getColumn()

int ASTNode::getColumn ( )
inline

◆ getLine()

int ASTNode::getLine ( )
inline

◆ print()

virtual std::ostream& ASTNode::print ( std::ostream &  out) const
protectedpure virtual

◆ setLocation()

void ASTNode::setLocation ( int  l,
int  c 
)
inline

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const ASTNode obj 
)
friend

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