Semantic.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #include "Semantic.h"
  2. #include "Type.h"
  3. #include "Ast.h"
  4. #include "AstVisitor.h"
  5. #include "CodegenVisitor.h"
  6. #include "Util.h"
  7. #include <memory>
  8. using namespace qlow::sem;
  9. namespace qlow
  10. {
  11. namespace sem
  12. {
  13. std::pair<std::unique_ptr<Context>, std::unique_ptr<GlobalScope>>
  14. createFromAst(const qlow::ast::Ast& ast)
  15. {
  16. std::unique_ptr<Context> context = std::make_unique<Context>();
  17. Printer& printer = Printer::getInstance();
  18. auto& objects = ast.getObjects();
  19. #ifdef DEBUGGING
  20. printf("starting building semantic representation (%d objects)\n", (int) objects.size());
  21. #endif
  22. // create classes
  23. std::unique_ptr<sem::GlobalScope> globalScope = std::make_unique<sem::GlobalScope>(*context);
  24. for (auto& astObject : objects) {
  25. if (auto* cls = dynamic_cast<ast::Class*>(astObject.get()); cls) {
  26. globalScope->classes[cls->name] = std::make_unique<sem::Class>(cls, *globalScope);
  27. }
  28. else if (auto* function = dynamic_cast<ast::MethodDefinition*>(astObject.get()); function) {
  29. globalScope->functions[function->name] = std::make_unique<sem::Method>(function, *globalScope);
  30. }
  31. }
  32. #ifdef DEBUGGING
  33. printf("created symbol table entries for all classes\n");
  34. #endif
  35. StructureVisitor av;
  36. // create all methods and fields
  37. for (auto& [name, semClass] : globalScope->classes) {
  38. for (auto& feature : semClass->astNode->features) {
  39. if (auto* field = dynamic_cast<qlow::ast::FieldDeclaration*> (feature.get()); field) {
  40. if (semClass->fields.find(field->name) != semClass->fields.end()) // throw, if field already exists
  41. throw SemanticError(SemanticError::DUPLICATE_FIELD_DECLARATION, field->name, field->pos);
  42. // otherwise add to the fields list
  43. semClass->fields[field->name] = unique_dynamic_cast<Field>(field->accept(av, semClass->scope));
  44. }
  45. else if (auto* method = dynamic_cast<qlow::ast::MethodDefinition*> (feature.get()); method) {
  46. if (semClass->methods.find(method->name) != semClass->methods.end()) // throw, if method already exists
  47. throw SemanticError(SemanticError::DUPLICATE_METHOD_DEFINITION, method->name, method->pos);
  48. // otherwise add to the methods list
  49. semClass->methods[method->name] = unique_dynamic_cast<Method>(method->accept(av, semClass->scope));
  50. }
  51. else {
  52. // if a feature is neither a method nor a field, something went horribly wrong
  53. throw "internal error, feature neither field nor method";
  54. }
  55. }
  56. }
  57. for (auto& [name, method] : globalScope->functions) {
  58. auto returnType = globalScope->getType(method->astNode->type.get());
  59. if (returnType != nullptr) {
  60. method->returnType = returnType;
  61. }
  62. else {
  63. if (method->astNode->type == nullptr) {
  64. throw SemanticError(SemanticError::UNKNOWN_TYPE,
  65. method->name,
  66. method->astNode->pos);
  67. }
  68. else {
  69. throw SemanticError(SemanticError::UNKNOWN_TYPE,
  70. method->astNode->type->asString(),
  71. method->astNode->type->pos);
  72. }
  73. }
  74. // otherwise add to the methods list
  75. globalScope->functions[method->name] = unique_dynamic_cast<Method>(method->astNode->accept(av, *globalScope));
  76. }
  77. #ifdef DEBUGGING
  78. printf("created all methods and fields\n");
  79. #endif
  80. for (auto& [name, semClass] : globalScope->classes) {
  81. for (auto& [name, method] : semClass->methods) {
  82. if (method->astNode->body) { // if not declaration
  83. method->containingClass = semClass.get();
  84. method->generateThisExpression();
  85. method->body = unique_dynamic_cast<sem::DoEndBlock>(method->astNode->body->accept(av, method->scope));
  86. }
  87. }
  88. }
  89. for (auto& [name, method] : globalScope->functions) {
  90. if (method->astNode->body) { // if not declaration
  91. method->body = unique_dynamic_cast<sem::DoEndBlock>(method->astNode->body->accept(av, method->scope));
  92. }
  93. }
  94. #ifdef DEBUGGING
  95. printf("created all method bodies\n");
  96. #endif
  97. return { std::move(context), std::move(globalScope) };
  98. }
  99. }
  100. }
  101. std::string Class::toString(void) const
  102. {
  103. std::string val = "Class[";
  104. // add fields
  105. for (auto& field : fields)
  106. val += field.second->toString() + ", ";
  107. if (!fields.empty())
  108. val = val.substr(0, val.length() - 2);
  109. // add methods
  110. for (auto& method : methods)
  111. val += method.second->toString() + ", ";
  112. if (!methods.empty())
  113. val = val.substr(0, val.length() - 2);
  114. val += " (";
  115. val += std::to_string(this->astNode->pos.first_line) + ", ";
  116. val += std::to_string(this->astNode->pos.first_column);
  117. val += " )";
  118. return val + "]";
  119. }
  120. std::string Variable::toString(void) const
  121. {
  122. return "Variable[" + this->name + "]";
  123. }
  124. std::string Field::toString(void) const
  125. {
  126. return "Field[" + this->name + "]";
  127. }
  128. void Method::generateThisExpression(void)
  129. {
  130. auto te = std::make_unique<ThisExpression>(this);
  131. thisExpression = te.get();
  132. scope.putVariable(this->thisExpression->name, std::move(te));
  133. }
  134. std::string Method::toString(void) const
  135. {
  136. return "Method[" + this->name + "]";
  137. }
  138. #define COMMA ,
  139. #define ACCEPT_DEFINITION(ClassName, Visitor, ReturnType, Arg) \
  140. ReturnType ClassName::accept(Visitor& v, Arg arg) \
  141. { \
  142. return v.visit(*this, arg); \
  143. }
  144. ACCEPT_DEFINITION(LocalVariableExpression, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
  145. ACCEPT_DEFINITION(BinaryOperation, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
  146. ACCEPT_DEFINITION(CastExpression, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
  147. ACCEPT_DEFINITION(NewExpression, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
  148. ACCEPT_DEFINITION(NewArrayExpression, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
  149. ACCEPT_DEFINITION(UnaryOperation, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
  150. ACCEPT_DEFINITION(MethodCallExpression, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
  151. ACCEPT_DEFINITION(FieldAccessExpression, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
  152. ACCEPT_DEFINITION(AddressExpression, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
  153. ACCEPT_DEFINITION(IntConst, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
  154. ACCEPT_DEFINITION(ThisExpression, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
  155. ACCEPT_DEFINITION(Expression, LValueVisitor, llvm::Value*, qlow::gen::FunctionGenerator&)
  156. ACCEPT_DEFINITION(LocalVariableExpression, LValueVisitor, llvm::Value*, qlow::gen::FunctionGenerator&)
  157. ACCEPT_DEFINITION(FieldAccessExpression, LValueVisitor, llvm::Value*, qlow::gen::FunctionGenerator&)
  158. ACCEPT_DEFINITION(AssignmentStatement, StatementVisitor, llvm::Value*, qlow::gen::FunctionGenerator&)
  159. ACCEPT_DEFINITION(DoEndBlock, StatementVisitor, llvm::Value*, qlow::gen::FunctionGenerator&)
  160. ACCEPT_DEFINITION(IfElseBlock, StatementVisitor, llvm::Value*, qlow::gen::FunctionGenerator&)
  161. ACCEPT_DEFINITION(WhileBlock, StatementVisitor, llvm::Value*, qlow::gen::FunctionGenerator&)
  162. ACCEPT_DEFINITION(ReturnStatement, StatementVisitor, llvm::Value*, qlow::gen::FunctionGenerator&)
  163. ACCEPT_DEFINITION(FeatureCallStatement, StatementVisitor, llvm::Value*, qlow::gen::FunctionGenerator&)
  164. std::string AssignmentStatement::toString(void) const
  165. {
  166. return "AssignmentStatement[" + this->target->toString() + " := " +
  167. this->value->toString() + "]";
  168. }
  169. std::string ReturnStatement::toString(void) const
  170. {
  171. return "ReturnStatement[" + this->value->toString() + "]";
  172. }
  173. std::string LocalVariableExpression::toString(void) const
  174. {
  175. return "LocalVariableExpression[" + var->name + "]";
  176. }
  177. std::string UnaryOperation::toString(void) const
  178. {
  179. return "UnaryOperation[" + arg->toString() + "]";
  180. }
  181. std::string BinaryOperation::toString(void) const
  182. {
  183. return "BinaryOperation[" + left->toString() + ", " +
  184. right->toString() + "]";
  185. }
  186. std::string CastExpression::toString(void) const
  187. {
  188. // TODO remove optional unwrapping
  189. return "CastExpression[" + expression->toString() + " to " +
  190. targetType->asString() + "]";
  191. }
  192. std::string NewExpression::toString(void) const
  193. {
  194. // TODO remove optional unwrapping
  195. return "NewExpression[" + type->asString() + "]";
  196. }
  197. std::string NewArrayExpression::toString(void) const
  198. {
  199. // TODO remove optional unwrapping
  200. return "NewArrayExpression[" + elementType->asString() + "; " +
  201. length->toString() + "]";
  202. }
  203. std::string MethodCallExpression::toString(void) const
  204. {
  205. if (this->target)
  206. return "MethodCallExpression[" + target->toString() + "." + callee->toString() + "]";
  207. else
  208. return "MethodCallExpression[" + callee->toString() + "]";
  209. }
  210. std::string FieldAccessExpression::toString(void) const
  211. {
  212. if (this->target)
  213. return "FieldAccessExpression[" + target->toString() + "." + accessed->toString() + "]";
  214. else
  215. return "FieldAccessExpression[" + accessed->toString() + "]";
  216. }
  217. std::string FeatureCallStatement::toString(void) const
  218. {
  219. return "FeatureCallStatement[" + expr->callee->toString() + "]";
  220. }