Semantic.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #ifndef QLOW_SEMANTIC_H
  2. #define QLOW_SEMANTIC_H
  3. #include <string>
  4. #include <map>
  5. #include "Util.h"
  6. #include "Ast.h"
  7. #include "Visitor.h"
  8. #include "Scope.h"
  9. #include "Type.h"
  10. #include <llvm/IR/Value.h>
  11. #include <llvm/IR/IRBuilder.h>
  12. #include <llvm/IR/BasicBlock.h>
  13. namespace qlow
  14. {
  15. namespace sem
  16. {
  17. std::unique_ptr<GlobalScope>
  18. createFromAst(const std::vector<std::unique_ptr<qlow::ast::AstObject>>& objects);
  19. struct Class;
  20. struct Variable;
  21. struct Field;
  22. struct Method;
  23. struct Statement;
  24. struct Expression;
  25. struct DoEndBlock;
  26. struct IfElseBlock;
  27. struct WhileBlock;
  28. struct FeatureCallStatement;
  29. struct AssignmentStatement;
  30. struct ReturnStatement;
  31. struct LocalVariableExpression;
  32. struct Operation;
  33. struct UnaryOperation;
  34. struct BinaryOperation;
  35. struct NewArrayExpression;
  36. struct FeatureCallExpression;
  37. struct IntConst;
  38. class SemanticException;
  39. }
  40. class ExpressionCodegenVisitor;
  41. class StatementVisitor;
  42. namespace gen
  43. {
  44. class FunctionGenerator;
  45. }
  46. }
  47. struct qlow::sem::Class : public SemanticObject
  48. {
  49. qlow::ast::Class* astNode;
  50. std::string name;
  51. SymbolTable<Field> fields;
  52. SymbolTable<Method> methods;
  53. ClassScope scope;
  54. /// \brief generated during llvm code generation, not availab
  55. llvm::Type* llvmType;
  56. inline Class(qlow::ast::Class* astNode, GlobalScope& globalScope) :
  57. astNode{ astNode },
  58. name{ astNode->name },
  59. scope{ globalScope, this },
  60. llvmType{ nullptr }
  61. {
  62. }
  63. inline Class(const std::string& nativeName, GlobalScope& globalScope) :
  64. astNode{ nullptr },
  65. name{ nativeName },
  66. scope{ globalScope, this },
  67. llvmType{ nullptr }
  68. {
  69. }
  70. virtual std::string toString(void) const override;
  71. };
  72. struct qlow::sem::Variable : public SemanticObject
  73. {
  74. Type* type;
  75. std::string name;
  76. /// if this is a local variable, this stores a reference to the llvm
  77. /// instance of this variable. If it is a parameter, the parameter value
  78. llvm::Value* allocaInst;
  79. Variable(void) = default;
  80. inline Variable(Type* type, std::string& name) :
  81. type{ type }, name{ name }, allocaInst { nullptr } {}
  82. virtual std::string toString(void) const override;
  83. };
  84. struct qlow::sem::Field : public Variable
  85. {
  86. virtual std::string toString(void) const override;
  87. };
  88. struct qlow::sem::Method : public SemanticObject
  89. {
  90. Class* containingType;
  91. Type* returnType;
  92. std::vector<Variable*> arguments;
  93. std::string name;
  94. ast::MethodDefinition* astNode;
  95. std::unique_ptr<DoEndBlock> body;
  96. LocalScope scope;
  97. llvm::Function* llvmNode;
  98. inline Method(Scope& parentScope, Type* returnType) :
  99. returnType{ returnType },
  100. scope{ parentScope },
  101. body{ nullptr }
  102. {
  103. }
  104. inline Method(ast::MethodDefinition* astNode, Scope& parentScope) :
  105. astNode{ astNode },
  106. name{ astNode->name },
  107. scope{ parentScope },
  108. body{ nullptr }
  109. {
  110. }
  111. virtual std::string toString(void) const override;
  112. };
  113. struct qlow::sem::Statement : public SemanticObject, public Visitable<llvm::Value*, gen::FunctionGenerator, qlow::StatementVisitor>
  114. {
  115. virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) = 0;
  116. };
  117. struct qlow::sem::DoEndBlock : public Statement
  118. {
  119. LocalScope scope;
  120. OwningList<Statement> statements;
  121. inline DoEndBlock(Scope& parentScope) :
  122. scope{ parentScope } {}
  123. virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) override;
  124. };
  125. struct qlow::sem::IfElseBlock : public Statement
  126. {
  127. std::unique_ptr<Expression> condition;
  128. std::unique_ptr<DoEndBlock> ifBlock;
  129. std::unique_ptr<DoEndBlock> elseBlock;
  130. inline IfElseBlock(std::unique_ptr<Expression> condition,
  131. std::unique_ptr<DoEndBlock> ifBlock,
  132. std::unique_ptr<DoEndBlock> elseBlock) :
  133. condition{ std::move(condition) },
  134. ifBlock{ std::move(ifBlock) },
  135. elseBlock{ std::move(elseBlock) }
  136. {
  137. }
  138. virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) override;
  139. };
  140. struct qlow::sem::WhileBlock : public Statement
  141. {
  142. std::unique_ptr<Expression> condition;
  143. std::unique_ptr<DoEndBlock> body;
  144. inline WhileBlock(std::unique_ptr<Expression> condition,
  145. std::unique_ptr<DoEndBlock> body) :
  146. condition{ std::move(condition) },
  147. body{ std::move(body) }
  148. {
  149. }
  150. virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) override;
  151. };
  152. struct qlow::sem::AssignmentStatement : public Statement
  153. {
  154. std::unique_ptr<Expression> target;
  155. std::unique_ptr<Expression> value;
  156. virtual std::string toString(void) const override;
  157. virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) override;
  158. };
  159. struct qlow::sem::ReturnStatement : public Statement
  160. {
  161. std::unique_ptr<Expression> value;
  162. virtual std::string toString(void) const override;
  163. virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) override;
  164. };
  165. struct qlow::sem::Expression :
  166. public SemanticObject,
  167. public Visitable<std::pair<llvm::Value*, sem::Type*>,
  168. llvm::IRBuilder<>,
  169. qlow::ExpressionCodegenVisitor>
  170. {
  171. std::unique_ptr<sem::Type> type;
  172. inline Expression(std::unique_ptr<Type> type) :
  173. type{ std::move(type) }
  174. {
  175. }
  176. };
  177. struct qlow::sem::Operation : public Expression
  178. {
  179. ast::Operation::Operator op;
  180. };
  181. struct qlow::sem::LocalVariableExpression : public Expression
  182. {
  183. Variable* var;
  184. virtual std::pair<llvm::Value*, sem::Type*> accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  185. virtual std::string toString(void) const override;
  186. };
  187. struct qlow::sem::BinaryOperation : public Operation
  188. {
  189. std::unique_ptr<Expression> left;
  190. std::unique_ptr<Expression> right;
  191. virtual std::pair<llvm::Value*, sem::Type*> accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  192. virtual std::string toString(void) const override;
  193. };
  194. struct qlow::sem::NewArrayExpression : public Expression
  195. {
  196. std::unique_ptr<Type> arrayType;
  197. std::unique_ptr<Expression> length;
  198. virtual std::pair<llvm::Value*, sem::Type*> accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  199. virtual std::string toString(void) const override;
  200. };
  201. struct qlow::sem::UnaryOperation : public Operation
  202. {
  203. qlow::ast::UnaryOperation::Side side;
  204. std::unique_ptr<Expression> arg;
  205. virtual std::pair<llvm::Value*, sem::Type*> accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  206. virtual std::string toString(void) const override;
  207. };
  208. struct qlow::sem::FeatureCallExpression : public Expression
  209. {
  210. Method* callee;
  211. OwningList<Expression> arguments;
  212. virtual std::pair<llvm::Value*, sem::Type*> accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  213. virtual std::string toString(void) const override;
  214. };
  215. struct qlow::sem::IntConst : public Expression
  216. {
  217. unsigned long long value;
  218. inline IntConst(unsigned long long value) :
  219. Expression{ std::make_unique<NativeType>(NativeType::INTEGER) },
  220. value{ value }
  221. {
  222. }
  223. virtual std::pair<llvm::Value*, sem::Type*> accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  224. };
  225. struct qlow::sem::FeatureCallStatement : public Statement
  226. {
  227. std::unique_ptr<FeatureCallExpression> expr;
  228. inline FeatureCallStatement(std::unique_ptr<FeatureCallExpression> expr) :
  229. expr{ std::move(expr) } {}
  230. virtual std::string toString(void) const override;
  231. virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) override;
  232. };
  233. #endif // QLOW_SEMANTIC_H