Semantic.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. llvm::Type* llvmType;
  59. inline Class(qlow::ast::Class* astNode, GlobalScope& globalScope) :
  60. astNode{ astNode }, name{ astNode->name }, scope{ globalScope, this }
  61. {
  62. }
  63. virtual std::string toString(void) const override;
  64. };
  65. struct qlow::sem::Variable : public SemanticObject
  66. {
  67. Type type;
  68. std::string name;
  69. /// if this is a local variable, this stores a reference to the llvm
  70. /// instance of this variable.
  71. llvm::AllocaInst* allocaInst;
  72. Variable(void) = default;
  73. inline Variable(Type type, std::string& name) :
  74. type{ type }, name{ name }, allocaInst { nullptr } {}
  75. virtual std::string toString(void) const override;
  76. };
  77. struct qlow::sem::Field : public Variable
  78. {
  79. Type type;
  80. std::string name;
  81. virtual std::string toString(void) const override;
  82. };
  83. struct qlow::sem::Method : public SemanticObject
  84. {
  85. Class* containingType;
  86. Type returnType;
  87. std::string name;
  88. ast::MethodDefinition* astNode;
  89. std::unique_ptr<DoEndBlock> body;
  90. LocalScope scope;
  91. llvm::Function* llvmNode;
  92. inline Method(Scope& parentScope, const Type& returnType) :
  93. returnType{ returnType }, scope{ parentScope } {}
  94. virtual std::string toString(void) const override;
  95. };
  96. struct qlow::sem::DoEndBlock : public SemanticObject
  97. {
  98. LocalScope scope;
  99. OwningList<Statement> statements;
  100. inline DoEndBlock(Scope& parentScope) :
  101. scope{ parentScope } {}
  102. };
  103. struct qlow::sem::Statement : public SemanticObject, public Visitable<llvm::Value*, gen::FunctionGenerator, qlow::StatementVisitor>
  104. {
  105. virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) = 0;
  106. };
  107. struct qlow::sem::AssignmentStatement : public Statement
  108. {
  109. std::unique_ptr<Expression> target;
  110. std::unique_ptr<Expression> value;
  111. virtual std::string toString(void) const override;
  112. virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) override;
  113. };
  114. struct qlow::sem::Expression : public SemanticObject, public Visitable<llvm::Value*, llvm::IRBuilder<>, qlow::ExpressionVisitor>
  115. {
  116. };
  117. struct qlow::sem::Operation : public Expression
  118. {
  119. ast::Operation::Operator op;
  120. };
  121. struct qlow::sem::LocalVariableExpression : public Expression
  122. {
  123. Variable* var;
  124. virtual llvm::Value* accept(ExpressionVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  125. virtual std::string toString(void) const override;
  126. };
  127. struct qlow::sem::BinaryOperation : public Operation
  128. {
  129. std::unique_ptr<Expression> left;
  130. std::unique_ptr<Expression> right;
  131. virtual llvm::Value* accept(ExpressionVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  132. virtual std::string toString(void) const override;
  133. };
  134. struct qlow::sem::UnaryOperation : public Operation
  135. {
  136. qlow::ast::UnaryOperation::Side side;
  137. std::unique_ptr<Expression> arg;
  138. virtual llvm::Value* accept(ExpressionVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  139. virtual std::string toString(void) const override;
  140. };
  141. struct qlow::sem::FeatureCallExpression : public Expression
  142. {
  143. Method* callee;
  144. OwningList<Expression> arguments;
  145. virtual llvm::Value* accept(ExpressionVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  146. virtual std::string toString(void) const override;
  147. };
  148. struct qlow::sem::IntConst : public Expression
  149. {
  150. unsigned long long value;
  151. inline IntConst(unsigned long long value) :
  152. value{ value }
  153. {
  154. }
  155. virtual llvm::Value* accept(ExpressionVisitor& visitor, llvm::IRBuilder<>& arg2) override;
  156. };
  157. struct qlow::sem::FeatureCallStatement : public Statement
  158. {
  159. std::unique_ptr<FeatureCallExpression> expr;
  160. inline FeatureCallStatement(std::unique_ptr<FeatureCallExpression> expr) :
  161. expr{ std::move(expr) } {}
  162. virtual std::string toString(void) const override;
  163. virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) override;
  164. };
  165. class qlow::sem::SemanticException
  166. {
  167. std::string message;
  168. qlow::CodePosition where;
  169. public:
  170. enum ErrorCode
  171. {
  172. UNKNOWN_TYPE,
  173. DUPLICATE_CLASS_DEFINITION,
  174. DUPLICATE_FIELD_DECLARATION,
  175. DUPLICATE_METHOD_DEFINITION,
  176. FEATURE_NOT_FOUND,
  177. };
  178. ErrorCode errorCode;
  179. public:
  180. inline SemanticException(ErrorCode ec, const std::string& arg, const
  181. qlow::CodePosition& where) :
  182. message{ arg }, where{ where }, errorCode{ ec }
  183. {}
  184. std::string getMessage(void) const;
  185. };
  186. #endif // QLOW_SEMANTIC_H