Semantic.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 <llvm/IR/Value.h>
  10. #include <llvm/IR/IRBuilder.h>
  11. #include <llvm/IR/BasicBlock.h>
  12. namespace qlow
  13. {
  14. namespace sem
  15. {
  16. std::unique_ptr<GlobalScope>
  17. createFromAst(const std::vector<std::unique_ptr<qlow::ast::Class>>& classes);
  18. struct SemanticObject;
  19. struct Class;
  20. struct Variable;
  21. struct Field;
  22. struct Method;
  23. struct DoEndBlock;
  24. struct Statement;
  25. struct Expression;
  26. struct FeatureCallStatement;
  27. struct AssignmentStatement;
  28. struct LocalVariableExpression;
  29. struct Operation;
  30. struct UnaryOperation;
  31. struct BinaryOperation;
  32. struct FeatureCallExpression;
  33. struct IntConst;
  34. class SemanticException;
  35. }
  36. class ExpressionVisitor;
  37. class StatementVisitor;
  38. namespace gen
  39. {
  40. class FunctionGenerator;
  41. }
  42. }
  43. struct qlow::sem::SemanticObject
  44. {
  45. virtual ~SemanticObject(void);
  46. /**
  47. * \brief converts the object to a readable string for debugging purposes.
  48. */
  49. virtual std::string toString(void) const;
  50. };
  51. struct qlow::sem::Class : public SemanticObject
  52. {
  53. qlow::ast::Class* astNode;
  54. std::string name;
  55. SymbolTable<Field> fields;
  56. SymbolTable<Method> methods;
  57. ClassScope scope;
  58. /// \brief generated during llvm code generation, not availab
  59. llvm::Type* llvmType;
  60. inline Class(qlow::ast::Class* astNode, GlobalScope& globalScope) :
  61. astNode{ astNode },
  62. name{ astNode->name },
  63. scope{ globalScope, this },
  64. llvmType{ nullptr }
  65. {
  66. }
  67. virtual std::string toString(void) const override;
  68. };
  69. struct qlow::sem::Variable : public SemanticObject
  70. {
  71. Type type;
  72. std::string name;
  73. /// if this is a local variable, this stores a reference to the llvm
  74. /// instance of this variable.
  75. llvm::AllocaInst* allocaInst;
  76. Variable(void) = default;
  77. inline Variable(Type type, std::string& name) :
  78. type{ type }, name{ name }, allocaInst { nullptr } {}
  79. virtual std::string toString(void) const override;
  80. };
  81. struct qlow::sem::Field : public Variable
  82. {
  83. Type type;
  84. std::string name;
  85. virtual std::string toString(void) const override;
  86. };
  87. struct qlow::sem::Method : public SemanticObject
  88. {
  89. Class* containingType;
  90. Type returnType;
  91. std::string name;
  92. ast::MethodDefinition* astNode;
  93. std::unique_ptr<DoEndBlock> body;
  94. LocalScope scope;
  95. llvm::Function* llvmNode;
  96. inline Method(Scope& parentScope, const Type& returnType) :
  97. returnType{ returnType }, scope{ parentScope } {}
  98. virtual std::string toString(void) const override;
  99. };
  100. struct qlow::sem::DoEndBlock : public SemanticObject
  101. {
  102. LocalScope scope;
  103. OwningList<Statement> statements;
  104. inline DoEndBlock(Scope& parentScope) :
  105. scope{ parentScope } {}
  106. };
  107. struct qlow::sem::Statement : public SemanticObject, public Visitable<llvm::Value*, gen::FunctionGenerator, qlow::StatementVisitor>
  108. {
  109. virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) = 0;
  110. };
  111. struct qlow::sem::AssignmentStatement : public Statement
  112. {
  113. std::unique_ptr<Expression> target;
  114. std::unique_ptr<Expression> value;
  115. virtual std::string toString(void) const override;
  116. virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) override;
  117. };
  118. struct qlow::sem::Expression :
  119. public SemanticObject,
  120. public Visitable<std::pair<llvm::Value*, sem::Type>,
  121. llvm::IRBuilder<>,
  122. qlow::ExpressionVisitor>
  123. {
  124. };
  125. struct qlow::sem::Operation : public Expression
  126. {
  127. ast::Operation::Operator op;
  128. };
  129. struct qlow::sem::LocalVariableExpression : public Expression
  130. {
  131. Variable* var;
  132. virtual std::pair<llvm::Value*, sem::Type> accept(ExpressionVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  133. virtual std::string toString(void) const override;
  134. };
  135. struct qlow::sem::BinaryOperation : public Operation
  136. {
  137. std::unique_ptr<Expression> left;
  138. std::unique_ptr<Expression> right;
  139. virtual std::pair<llvm::Value*, sem::Type> accept(ExpressionVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  140. virtual std::string toString(void) const override;
  141. };
  142. struct qlow::sem::UnaryOperation : public Operation
  143. {
  144. qlow::ast::UnaryOperation::Side side;
  145. std::unique_ptr<Expression> arg;
  146. virtual std::pair<llvm::Value*, sem::Type> accept(ExpressionVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  147. virtual std::string toString(void) const override;
  148. };
  149. struct qlow::sem::FeatureCallExpression : public Expression
  150. {
  151. Method* callee;
  152. OwningList<Expression> arguments;
  153. virtual std::pair<llvm::Value*, sem::Type> accept(ExpressionVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  154. virtual std::string toString(void) const override;
  155. };
  156. struct qlow::sem::IntConst : public Expression
  157. {
  158. unsigned long long value;
  159. inline IntConst(unsigned long long value) :
  160. value{ value }
  161. {
  162. }
  163. virtual std::pair<llvm::Value*, sem::Type> accept(ExpressionVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  164. };
  165. struct qlow::sem::FeatureCallStatement : public Statement
  166. {
  167. std::unique_ptr<FeatureCallExpression> expr;
  168. inline FeatureCallStatement(std::unique_ptr<FeatureCallExpression> expr) :
  169. expr{ std::move(expr) } {}
  170. virtual std::string toString(void) const override;
  171. virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) override;
  172. };
  173. class qlow::sem::SemanticException
  174. {
  175. std::string message;
  176. qlow::CodePosition where;
  177. public:
  178. enum ErrorCode
  179. {
  180. UNKNOWN_TYPE,
  181. DUPLICATE_CLASS_DEFINITION,
  182. DUPLICATE_FIELD_DECLARATION,
  183. DUPLICATE_METHOD_DEFINITION,
  184. FEATURE_NOT_FOUND,
  185. };
  186. ErrorCode errorCode;
  187. public:
  188. inline SemanticException(ErrorCode ec, const std::string& arg, const
  189. qlow::CodePosition& where) :
  190. message{ arg }, where{ where }, errorCode{ ec }
  191. {}
  192. std::string getMessage(void) const;
  193. };
  194. #endif // QLOW_SEMANTIC_H