Semantic.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. using namespace qlow::sem;
  8. namespace qlow
  9. {
  10. namespace sem
  11. {
  12. std::unique_ptr<GlobalScope>
  13. createFromAst(const std::vector<std::unique_ptr<qlow::ast::AstObject>>& objects)
  14. {
  15. Logger& logger = Logger::getInstance();
  16. #ifdef DEBUGGING
  17. printf("starting building semantic representation (%d objects)\n", objects.size());
  18. #endif
  19. // create classes
  20. std::unique_ptr<sem::GlobalScope> globalScope = std::make_unique<sem::GlobalScope>();
  21. for (auto& astObject : objects) {
  22. if (auto* cls = dynamic_cast<ast::Class*>(astObject.get()); cls) {
  23. globalScope->classes[cls->name] = std::make_unique<sem::Class>(cls, *globalScope);
  24. }
  25. else if (auto* function = dynamic_cast<ast::MethodDefinition*>(astObject.get()); function) {
  26. globalScope->functions[function->name] = std::make_unique<sem::Method>(function, *globalScope);
  27. }
  28. }
  29. #ifdef DEBUGGING
  30. printf("created symbol table entries for all classes\n");
  31. #endif
  32. StructureVisitor av;
  33. // create all methods and fields
  34. for (auto& [name, semClass] : globalScope->classes) {
  35. for (auto& feature : semClass->astNode->features) {
  36. if (auto* field = dynamic_cast<qlow::ast::FieldDeclaration*> (feature.get()); field) {
  37. if (semClass->fields.find(field->name) != semClass->fields.end()) // throw, if field already exists
  38. throw SemanticError(SemanticError::DUPLICATE_FIELD_DECLARATION, field->name, field->pos);
  39. // otherwise add to the fields list
  40. semClass->fields[field->name] = unique_dynamic_cast<Field>(field->accept(av, semClass->scope));
  41. }
  42. else if (auto* method = dynamic_cast<qlow::ast::MethodDefinition*> (feature.get()); method) {
  43. if (semClass->methods.find(method->name) != semClass->methods.end()) // throw, if method already exists
  44. throw SemanticError(SemanticError::DUPLICATE_METHOD_DEFINITION, method->name, method->pos);
  45. // otherwise add to the methods list
  46. semClass->methods[method->name] = unique_dynamic_cast<Method>(method->accept(av, semClass->scope));
  47. }
  48. else {
  49. // if a feature is neither a method nor a field, something went horribly wrong
  50. throw "internal error, feature neither field nor method";
  51. }
  52. }
  53. }
  54. for (auto& [name, method] : globalScope->functions) {
  55. auto returnType = globalScope->getType(*method->astNode->type);
  56. if (returnType) {
  57. method->returnType = returnType;
  58. }
  59. else {
  60. SemanticError se(SemanticError::UNKNOWN_TYPE,
  61. method->astNode->type->asString(),
  62. method->astNode->type->pos);
  63. }
  64. // otherwise add to the methods list
  65. globalScope->functions[method->name] = unique_dynamic_cast<Method>(method->astNode->accept(av, *globalScope));
  66. }
  67. #ifdef DEBUGGING
  68. printf("created all methods and fields\n");
  69. #endif
  70. for (auto& [name, semClass] : globalScope->classes) {
  71. for (auto& [name, method] : semClass->methods) {
  72. if (method->astNode->body) // if not declaration
  73. method->body = unique_dynamic_cast<sem::DoEndBlock>(av.visit(*method->astNode->body, method->scope));
  74. }
  75. }
  76. for (auto& [name, method] : globalScope->functions) {
  77. if (method->astNode->body) // if not declaration
  78. method->body = unique_dynamic_cast<sem::DoEndBlock>(av.visit(*method->astNode->body, method->scope));
  79. }
  80. #ifdef DEBUGGING
  81. printf("created all method bodies\n");
  82. #endif
  83. return globalScope;
  84. }
  85. }
  86. }
  87. SemanticObject::~SemanticObject(void)
  88. {
  89. }
  90. std::string SemanticObject::toString(void) const
  91. {
  92. return "SemanticObject [" + util::toString(this) + "]";
  93. }
  94. std::string Class::toString(void) const
  95. {
  96. std::string val = "Class[";
  97. // add fields
  98. for (auto& field : fields)
  99. val += field.second->toString() + ", ";
  100. if (!fields.empty())
  101. val = val.substr(0, val.length() - 2);
  102. // add methods
  103. for (auto& method : methods)
  104. val += method.second->toString() + ", ";
  105. if (!methods.empty())
  106. val = val.substr(0, val.length() - 2);
  107. val += " (";
  108. val += std::to_string(this->astNode->pos.first_line) + ", ";
  109. val += std::to_string(this->astNode->pos.first_column);
  110. val += " )";
  111. return val + "]";
  112. }
  113. std::string Variable::toString(void) const
  114. {
  115. return "Variable[" + this->name + "]";
  116. }
  117. std::string Field::toString(void) const
  118. {
  119. return "Field[" + this->name + "]";
  120. }
  121. std::string Method::toString(void) const
  122. {
  123. return "Method[" + this->name + "]";
  124. }
  125. #define COMMA ,
  126. #define ACCEPT_DEFINITION(ClassName, Visitor, ReturnType, Arg) \
  127. ReturnType ClassName::accept(Visitor& v, Arg arg) \
  128. { \
  129. return v.visit(*this, arg); \
  130. }
  131. ACCEPT_DEFINITION(LocalVariableExpression, ExpressionCodegenVisitor, std::pair<llvm::Value* COMMA Type*>, llvm::IRBuilder<>&)
  132. ACCEPT_DEFINITION(BinaryOperation, ExpressionCodegenVisitor, std::pair<llvm::Value* COMMA Type*>, llvm::IRBuilder<>&)
  133. ACCEPT_DEFINITION(NewArrayExpression, ExpressionCodegenVisitor, std::pair<llvm::Value* COMMA Type*>, llvm::IRBuilder<>&)
  134. ACCEPT_DEFINITION(UnaryOperation, ExpressionCodegenVisitor, std::pair<llvm::Value* COMMA Type*>, llvm::IRBuilder<>&)
  135. ACCEPT_DEFINITION(FeatureCallExpression, ExpressionCodegenVisitor, std::pair<llvm::Value* COMMA Type*>, llvm::IRBuilder<>&)
  136. ACCEPT_DEFINITION(IntConst, ExpressionCodegenVisitor, std::pair<llvm::Value* COMMA Type*>, llvm::IRBuilder<>&)
  137. ACCEPT_DEFINITION(AssignmentStatement, StatementVisitor, llvm::Value*, qlow::gen::FunctionGenerator&)
  138. ACCEPT_DEFINITION(DoEndBlock, StatementVisitor, llvm::Value*, qlow::gen::FunctionGenerator&)
  139. ACCEPT_DEFINITION(IfElseBlock, StatementVisitor, llvm::Value*, qlow::gen::FunctionGenerator&)
  140. ACCEPT_DEFINITION(WhileBlock, StatementVisitor, llvm::Value*, qlow::gen::FunctionGenerator&)
  141. ACCEPT_DEFINITION(ReturnStatement, StatementVisitor, llvm::Value*, qlow::gen::FunctionGenerator&)
  142. ACCEPT_DEFINITION(FeatureCallStatement, StatementVisitor, llvm::Value*, qlow::gen::FunctionGenerator&)
  143. std::string AssignmentStatement::toString(void) const
  144. {
  145. return "AssignmentStatement[" + this->target->toString() + " := " +
  146. this->value->toString() + "]";
  147. }
  148. std::string ReturnStatement::toString(void) const
  149. {
  150. return "ReturnStatement[" + this->value->toString() + "]";
  151. }
  152. std::string LocalVariableExpression::toString(void) const
  153. {
  154. return "LocalVariableExpression[" + var->name + "]";
  155. }
  156. std::string UnaryOperation::toString(void) const
  157. {
  158. return "UnaryOperation[" + arg->toString() + "]";
  159. }
  160. std::string BinaryOperation::toString(void) const
  161. {
  162. return "BinaryOperation[" + left->toString() + ", " +
  163. right->toString() + "]";
  164. }
  165. std::string NewArrayExpression::toString(void) const
  166. {
  167. return "NewArrayExpression[" + arrayType->toString() + "; " +
  168. length->toString() + "]";
  169. }
  170. std::string FeatureCallExpression::toString(void) const
  171. {
  172. return "FeatureCallExpression[" + callee->toString() + "]";
  173. }
  174. std::string FeatureCallStatement::toString(void) const
  175. {
  176. return "FeatureCallStatement[" + expr->callee->toString() + "]";
  177. }