Semantic.cpp 8.7 KB

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