Semantic.h 6.7 KB

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