Semantic.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 "Context.h"
  11. #include <llvm/IR/Value.h>
  12. #include <llvm/IR/IRBuilder.h>
  13. #include <llvm/IR/BasicBlock.h>
  14. namespace qlow
  15. {
  16. namespace sem
  17. {
  18. std::pair<std::unique_ptr<Context>, std::unique_ptr<GlobalScope>>
  19. createFromAst(const qlow::ast::Ast& ast);
  20. struct Class;
  21. struct Variable;
  22. struct Field;
  23. struct Method;
  24. struct Statement;
  25. struct Expression;
  26. struct ThisExpression;
  27. struct DoEndBlock;
  28. struct IfElseBlock;
  29. struct WhileBlock;
  30. struct FeatureCallStatement;
  31. struct AssignmentStatement;
  32. struct ReturnStatement;
  33. struct LocalVariableExpression;
  34. struct AddressExpression;
  35. struct ArrayAccessExpression;
  36. struct Operation;
  37. struct UnaryOperation;
  38. struct BinaryOperation;
  39. struct CastExpression;
  40. struct NewExpression;
  41. struct NewArrayExpression;
  42. struct MethodCallExpression;
  43. struct FieldAccessExpression;
  44. struct IntConst;
  45. }
  46. class ExpressionCodegenVisitor;
  47. class LValueVisitor;
  48. class StatementVisitor;
  49. namespace gen
  50. {
  51. class FunctionGenerator;
  52. }
  53. }
  54. struct qlow::sem::Class : public SemanticObject
  55. {
  56. qlow::ast::Class* astNode;
  57. std::string name;
  58. bool isReferenceType;
  59. SymbolTable<Field> fields;
  60. SymbolTable<Method> methods;
  61. ClassScope scope;
  62. Type* classType;
  63. /// \brief generated during llvm code generation, not availab
  64. llvm::Type* llvmType;
  65. inline Class(qlow::ast::Class* astNode,
  66. GlobalScope& globalScope) :
  67. SemanticObject{ globalScope.getContext() },
  68. astNode{ astNode },
  69. name{ astNode->name },
  70. isReferenceType{ astNode->isReferenceType },
  71. scope{ globalScope, this },
  72. classType{ globalScope.getContext().getClassType(this) },
  73. llvmType{ nullptr }
  74. {
  75. }
  76. inline Class(const std::string& nativeName,
  77. GlobalScope& globalScope) :
  78. SemanticObject{ globalScope.getContext() },
  79. astNode{ nullptr },
  80. name{ nativeName },
  81. scope{ globalScope, this },
  82. llvmType{ nullptr }
  83. {
  84. }
  85. virtual std::string toString(void) const override;
  86. };
  87. struct qlow::sem::Variable : public SemanticObject
  88. {
  89. Type* type;
  90. std::string name;
  91. bool isParameter;
  92. /// if this is a local variable, this stores a reference to the llvm
  93. /// instance of this variable. If it is a parameter, the parameter value
  94. llvm::Value* allocaInst;
  95. inline Variable(Context& context) :
  96. SemanticObject{ context } {}
  97. inline Variable(Context& context, Type* type, const std::string& name) :
  98. SemanticObject{ context },
  99. type{ type },
  100. name{ name },
  101. allocaInst { nullptr }
  102. {
  103. }
  104. virtual std::string toString(void) const override;
  105. };
  106. struct qlow::sem::Field : public Variable
  107. {
  108. inline Field(Context& context) :
  109. Variable{ context } {}
  110. int llvmStructIndex;
  111. virtual std::string toString(void) const override;
  112. };
  113. struct qlow::sem::Method : public SemanticObject
  114. {
  115. Class* containingClass;
  116. Type* returnType;
  117. std::vector<Variable*> arguments;
  118. std::string name;
  119. ast::MethodDefinition* astNode;
  120. ThisExpression* thisExpression;
  121. std::unique_ptr<DoEndBlock> body;
  122. bool isExtern;
  123. LocalScope scope;
  124. llvm::Function* llvmNode;
  125. inline Method(Scope& parentScope,
  126. Type* returnType, bool isExtern) :
  127. SemanticObject{ parentScope.getContext() },
  128. containingClass{ nullptr },
  129. returnType{ returnType },
  130. thisExpression{ nullptr },
  131. body{ nullptr },
  132. isExtern{ isExtern },
  133. scope{ parentScope, this }
  134. {
  135. }
  136. inline Method(ast::MethodDefinition* astNode, Scope& parentScope) :
  137. SemanticObject{ parentScope.getContext() },
  138. containingClass{ nullptr },
  139. returnType{ parentScope.getReturnableType() },
  140. name{ astNode->name },
  141. astNode{ astNode },
  142. thisExpression{ nullptr },
  143. body{ nullptr },
  144. isExtern{ astNode->isExtern() },
  145. scope{ parentScope, this }
  146. {
  147. }
  148. void generateThisExpression(void);
  149. std::string getMangledName(void) const;
  150. virtual std::string toString(void) const override;
  151. };
  152. struct qlow::sem::ThisExpression : public Variable
  153. {
  154. Method* method;
  155. inline ThisExpression(Method* method) :
  156. Variable{
  157. method->context,
  158. method->context.getClassType(method->containingClass),
  159. "this"
  160. },
  161. method{ method }
  162. {
  163. }
  164. llvm::Value* accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2);
  165. };
  166. struct qlow::sem::Statement : public SemanticObject, public Visitable<llvm::Value*, gen::FunctionGenerator, qlow::StatementVisitor>
  167. {
  168. inline Statement(Context& context) :
  169. SemanticObject{ context } {}
  170. virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) = 0;
  171. };
  172. struct qlow::sem::DoEndBlock : public Statement
  173. {
  174. LocalScope scope;
  175. OwningList<Statement> statements;
  176. inline DoEndBlock(LocalScope& parentScope) :
  177. Statement{ parentScope.getContext() },
  178. scope{ parentScope } {}
  179. virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) override;
  180. };
  181. struct qlow::sem::IfElseBlock : public Statement
  182. {
  183. std::unique_ptr<Expression> condition;
  184. std::unique_ptr<DoEndBlock> ifBlock;
  185. std::unique_ptr<DoEndBlock> elseBlock;
  186. inline IfElseBlock(std::unique_ptr<Expression> condition,
  187. std::unique_ptr<DoEndBlock> ifBlock,
  188. std::unique_ptr<DoEndBlock> elseBlock) :
  189. Statement{ ifBlock->context },
  190. condition{ std::move(condition) },
  191. ifBlock{ std::move(ifBlock) },
  192. elseBlock{ std::move(elseBlock) }
  193. {
  194. }
  195. virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) override;
  196. };
  197. struct qlow::sem::WhileBlock : public Statement
  198. {
  199. std::unique_ptr<Expression> condition;
  200. std::unique_ptr<DoEndBlock> body;
  201. inline WhileBlock(std::unique_ptr<Expression> condition,
  202. std::unique_ptr<DoEndBlock> body) :
  203. Statement{ body->context },
  204. condition{ std::move(condition) },
  205. body{ std::move(body) }
  206. {
  207. }
  208. virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) override;
  209. };
  210. struct qlow::sem::AssignmentStatement : public Statement
  211. {
  212. std::unique_ptr<Expression> target;
  213. std::unique_ptr<Expression> value;
  214. inline AssignmentStatement(Context& context) :
  215. Statement{ context } {}
  216. virtual std::string toString(void) const override;
  217. virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) override;
  218. };
  219. struct qlow::sem::ReturnStatement : public Statement
  220. {
  221. std::unique_ptr<Expression> value;
  222. inline ReturnStatement(Context& context) :
  223. Statement{ context } {}
  224. virtual std::string toString(void) const override;
  225. virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) override;
  226. };
  227. struct qlow::sem::Expression :
  228. public SemanticObject,
  229. public Visitable<llvm::Value*,
  230. llvm::IRBuilder<>,
  231. qlow::ExpressionCodegenVisitor>,
  232. public Visitable<llvm::Value*,
  233. qlow::gen::FunctionGenerator,
  234. qlow::LValueVisitor>
  235. {
  236. Type* type;
  237. CodePosition pos;
  238. inline Expression(Context& context, Type* type, const CodePosition& pos) :
  239. SemanticObject{ context },
  240. type{ type },
  241. pos{ pos }
  242. {
  243. }
  244. inline virtual bool isLValue(void) const { return false; }
  245. virtual llvm::Value* accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override = 0;
  246. virtual llvm::Value* accept(LValueVisitor& visitor, qlow::gen::FunctionGenerator&) override;
  247. };
  248. struct qlow::sem::Operation : public Expression
  249. {
  250. std::string opString;
  251. inline Operation(Context& context, Type* type, const CodePosition& pos) :
  252. Expression{ context, type, pos }
  253. {
  254. }
  255. };
  256. struct qlow::sem::LocalVariableExpression : public Expression
  257. {
  258. Variable* var;
  259. inline LocalVariableExpression(Variable* var, const CodePosition& pos) :
  260. Expression{ var->context, var->type, pos },
  261. var{ var }
  262. {
  263. }
  264. inline virtual bool isLValue(void) const override { return true; }
  265. virtual llvm::Value* accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  266. virtual llvm::Value* accept(LValueVisitor& visitor, qlow::gen::FunctionGenerator&) override;
  267. virtual std::string toString(void) const override;
  268. };
  269. struct qlow::sem::AddressExpression : public Expression
  270. {
  271. std::unique_ptr<sem::Expression> target;
  272. inline AddressExpression(std::unique_ptr<sem::Expression> target) :
  273. Expression{ target->context, nullptr /*context.createPointerType(target->type)*/, CodePosition{} },
  274. target{ std::move(target) }
  275. {
  276. }
  277. virtual llvm::Value* accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  278. };
  279. struct qlow::sem::ArrayAccessExpression : public Expression
  280. {
  281. std::unique_ptr<sem::Expression> array;
  282. std::unique_ptr<sem::Expression> index;
  283. inline ArrayAccessExpression(std::unique_ptr<sem::Expression> array,
  284. std::unique_ptr<sem::Expression> index,
  285. const CodePosition& pos) :
  286. Expression{ array->context, array->type->getArrayOf(), pos },
  287. array{ std::move(array) },
  288. index{ std::move(index) }
  289. {
  290. }
  291. virtual llvm::Value* accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  292. virtual llvm::Value* accept(LValueVisitor& visitor, qlow::gen::FunctionGenerator&) override;
  293. virtual std::string toString(void) const override;
  294. };
  295. struct qlow::sem::BinaryOperation : public Operation
  296. {
  297. std::unique_ptr<Expression> left;
  298. std::unique_ptr<Expression> right;
  299. ast::BinaryOperation* astNode;
  300. /// method that is called to execute the operator
  301. sem::Method* operationMethod;
  302. inline BinaryOperation(Context& context,
  303. Type* type, ast::BinaryOperation* astNode) :
  304. Operation{ context, type , astNode->pos},
  305. astNode{ astNode }
  306. {
  307. }
  308. virtual llvm::Value* accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  309. virtual std::string toString(void) const override;
  310. };
  311. struct qlow::sem::CastExpression : public Expression
  312. {
  313. std::unique_ptr<Expression> expression;
  314. Type* targetType;
  315. ast::CastExpression* astNode;
  316. bool isImplicit;
  317. bool isNativeCast;
  318. inline CastExpression(std::unique_ptr<Expression> expression,
  319. Type* type,
  320. ast::CastExpression* astNode,
  321. bool isNative,
  322. const CodePosition& pos) :
  323. Expression{ expression->context, type, pos },
  324. expression{ std::move(expression) },
  325. targetType{ type },
  326. astNode{ astNode },
  327. isImplicit{ astNode == nullptr },
  328. isNativeCast{ isNative }
  329. {
  330. }
  331. virtual llvm::Value* accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  332. virtual std::string toString(void) const override;
  333. };
  334. struct qlow::sem::NewExpression : public Expression
  335. {
  336. inline NewExpression(Context& context, Type* type, const CodePosition& pos) :
  337. Expression{ context, type, pos }
  338. {
  339. }
  340. virtual llvm::Value* accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  341. virtual std::string toString(void) const override;
  342. };
  343. struct qlow::sem::NewArrayExpression : public Expression
  344. {
  345. Type* elementType;
  346. std::unique_ptr<Expression> length;
  347. inline NewArrayExpression(Type* elementType, std::unique_ptr<Expression> length, const CodePosition& pos) :
  348. Expression{ length->context, length->context.getArrayType(elementType), pos },
  349. elementType{ elementType },
  350. length{ std::move(length) }
  351. {
  352. }
  353. virtual llvm::Value* accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  354. virtual std::string toString(void) const override;
  355. };
  356. struct qlow::sem::UnaryOperation : public Operation
  357. {
  358. qlow::ast::UnaryOperation::Side side;
  359. std::unique_ptr<Expression> arg;
  360. inline UnaryOperation(Context& context, Type* type, const CodePosition& pos) :
  361. Operation{ context, type, pos }
  362. {
  363. }
  364. virtual llvm::Value* accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  365. virtual std::string toString(void) const override;
  366. };
  367. struct qlow::sem::MethodCallExpression : public Expression
  368. {
  369. Method* callee;
  370. std::unique_ptr<Expression> target;
  371. OwningList<Expression> arguments;
  372. inline MethodCallExpression(std::unique_ptr<Expression> target,
  373. Method* callee, const CodePosition& pos) :
  374. Expression{ callee->context, callee->returnType, pos },
  375. callee{ callee },
  376. target{ std::move(target) }
  377. {
  378. }
  379. virtual llvm::Value* accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  380. virtual std::string toString(void) const override;
  381. };
  382. struct qlow::sem::FieldAccessExpression : public Expression
  383. {
  384. sem::Field* accessed;
  385. std::unique_ptr<Expression> target;
  386. //OwningList<Expression> arguments;
  387. inline FieldAccessExpression(std::unique_ptr<Expression> target,
  388. Field* accessed, const CodePosition& pos) :
  389. Expression{ target->context, accessed->type, pos },
  390. accessed{ accessed },
  391. target{ std::move(target) }
  392. {
  393. }
  394. inline virtual bool isLValue(void) const override { return true; }
  395. virtual llvm::Value* accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  396. virtual llvm::Value* accept(LValueVisitor& visitor, qlow::gen::FunctionGenerator&) override;
  397. virtual std::string toString(void) const override;
  398. };
  399. struct qlow::sem::IntConst : public Expression
  400. {
  401. unsigned long long value;
  402. inline IntConst(Context& context, unsigned long long value, const CodePosition& pos) :
  403. Expression{ context, context.getNativeType(NativeType::NType::INTEGER), pos },
  404. value{ value }
  405. {
  406. }
  407. virtual llvm::Value* accept(ExpressionCodegenVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  408. virtual std::string toString(void) const override;
  409. };
  410. struct qlow::sem::FeatureCallStatement : public Statement
  411. {
  412. std::unique_ptr<MethodCallExpression> expr;
  413. inline FeatureCallStatement(std::unique_ptr<MethodCallExpression> expr) :
  414. Statement{ expr->context },
  415. expr{ std::move(expr) } {}
  416. virtual std::string toString(void) const override;
  417. virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) override;
  418. };
  419. #endif // QLOW_SEMANTIC_H