tipc
A TIP to LLVM compiler
ASTNode.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "llvm/IR/Function.h"
4 #include "llvm/IR/Module.h"
5 #include "llvm/IR/Value.h"
6 #include <memory>
7 #include <ostream>
8 #include <string>
9 #include <vector>
10 
11 // Forward declare the visitor to resolve circular dependency
12 class ASTVisitor;
13 
34 class ASTNode {
35  int line = 0;
36  int column = 0;
37 
38 public:
39  virtual ~ASTNode() = default;
40 
52  virtual void accept(ASTVisitor *visitor) = 0;
53 
64  virtual llvm::Value *codegen() = 0;
65 
74  virtual std::vector<std::shared_ptr<ASTNode>> getChildren() { return {}; }
75  void setLocation(int l, int c) {
76  line = l;
77  column = c;
78  }
79  int getLine() { return line; }
80  int getColumn() { return column; }
81 
82  friend std::ostream &operator<<(std::ostream &os, const ASTNode &obj) {
83  return obj.print(os);
84  }
85 
86 protected:
87  virtual std::ostream &print(std::ostream &out) const = 0;
88 };
Abstract base class for all AST nodes.
Definition: ASTNode.h:34
void setLocation(int l, int c)
Definition: ASTNode.h:75
friend std::ostream & operator<<(std::ostream &os, const ASTNode &obj)
Definition: ASTNode.h:82
int getLine()
Definition: ASTNode.h:79
virtual ~ASTNode()=default
virtual llvm::Value * codegen()=0
Perform code generation and return an LLVM value the code.
int getColumn()
Definition: ASTNode.h:80
virtual std::vector< std::shared_ptr< ASTNode > > getChildren()
Return all of the children for the node.
Definition: ASTNode.h:74
virtual void accept(ASTVisitor *visitor)=0
Visit the children of this node and apply the visitor.
virtual std::ostream & print(std::ostream &out) const =0
Base class for AST visitors.
Definition: ASTVisitor.h:23