Semantic.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 CastExpression;
  36. struct NewArrayExpression;
  37. struct FeatureCallExpression;
  38. struct IntConst;
  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. std::shared_ptr<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(std::shared_ptr<Type> type, const std::string& name) :
  81. type{ std::move(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. std::shared_ptr<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, std::shared_ptr<Type> returnType) :
  99. returnType{ std::move(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<llvm::Value*,
  168. llvm::IRBuilder<>,
  169. qlow::ExpressionCodegenVisitor>
  170. {
  171. std::shared_ptr<sem::Type> type;
  172. inline Expression(std::shared_ptr<Type> type) :
  173. type{ std::move(type) }
  174. {
  175. }
  176. };
  177. struct qlow::sem::Operation : public Expression
  178. {
  179. std::string opString;
  180. inline Operation(std::shared_ptr<Type> type) :
  181. Expression{ std::move(type) }
  182. {
  183. }
  184. };
  185. struct qlow::sem::LocalVariableExpression : public Expression
  186. {
  187. Variable* var;
  188. inline LocalVariableExpression(Variable* var) :
  189. Expression{ var->type },
  190. var{ var }
  191. {
  192. }
  193. virtual llvm::Value* accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  194. virtual std::string toString(void) const override;
  195. };
  196. struct qlow::sem::BinaryOperation : public Operation
  197. {
  198. std::unique_ptr<Expression> left;
  199. std::unique_ptr<Expression> right;
  200. ast::BinaryOperation* astNode;
  201. /// method that is called to execute the operator
  202. sem::Method* operationMethod;
  203. inline BinaryOperation(std::shared_ptr<Type> type, ast::BinaryOperation* astNode) :
  204. Operation{ std::move(type) },
  205. astNode{ astNode }
  206. {
  207. }
  208. virtual llvm::Value* accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  209. virtual std::string toString(void) const override;
  210. };
  211. struct qlow::sem::CastExpression : public Expression
  212. {
  213. std::unique_ptr<Expression> expression;
  214. std::shared_ptr<Type> targetType;
  215. ast::CastExpression* astNode;
  216. inline CastExpression(std::unique_ptr<Expression> expression,
  217. std::shared_ptr<Type> type,
  218. ast::CastExpression* astNode) :
  219. Expression{ type },
  220. expression{ std::move(expression) },
  221. targetType{ std::move(type) },
  222. astNode{ astNode }
  223. {
  224. }
  225. virtual llvm::Value* accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  226. virtual std::string toString(void) const override;
  227. };
  228. struct qlow::sem::NewArrayExpression : public Expression
  229. {
  230. std::shared_ptr<Type> arrayType;
  231. std::unique_ptr<Expression> length;
  232. inline NewArrayExpression(std::shared_ptr<Type> arrayType) :
  233. Expression{ std::make_shared<ArrayType>(arrayType) },
  234. arrayType{ std::move(arrayType) }
  235. {
  236. }
  237. virtual llvm::Value* accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  238. virtual std::string toString(void) const override;
  239. };
  240. struct qlow::sem::UnaryOperation : public Operation
  241. {
  242. qlow::ast::UnaryOperation::Side side;
  243. std::unique_ptr<Expression> arg;
  244. inline UnaryOperation(std::shared_ptr<Type> type) :
  245. Operation{ std::move(type) }
  246. {
  247. }
  248. virtual llvm::Value* accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  249. virtual std::string toString(void) const override;
  250. };
  251. struct qlow::sem::FeatureCallExpression : public Expression
  252. {
  253. Method* callee;
  254. std::unique_ptr<Expression> target;
  255. OwningList<Expression> arguments;
  256. inline FeatureCallExpression(std::unique_ptr<Expression> target,
  257. Method* callee) :
  258. Expression{ callee->returnType }
  259. {
  260. }
  261. virtual llvm::Value* accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  262. virtual std::string toString(void) const override;
  263. };
  264. struct qlow::sem::IntConst : public Expression
  265. {
  266. unsigned long long value;
  267. inline IntConst(unsigned long long value) :
  268. Expression{ std::make_shared<NativeType>(NativeType::INTEGER) },
  269. value{ value }
  270. {
  271. }
  272. virtual llvm::Value* accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  273. };
  274. struct qlow::sem::FeatureCallStatement : public Statement
  275. {
  276. std::unique_ptr<FeatureCallExpression> expr;
  277. inline FeatureCallStatement(std::unique_ptr<FeatureCallExpression> expr) :
  278. expr{ std::move(expr) } {}
  279. virtual std::string toString(void) const override;
  280. virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) override;
  281. };
  282. #endif // QLOW_SEMANTIC_H