| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 | 
							- #include "Semantic.h"
 
- #include "Type.h"
 
- #include "Ast.h"
 
- #include "AstVisitor.h"
 
- #include "CodegenVisitor.h"
 
- #include "Util.h"
 
- #include <memory>
 
- using namespace qlow::sem;
 
- namespace qlow
 
- {
 
- namespace sem
 
- {
 
- std::pair<std::unique_ptr<Context>, std::unique_ptr<GlobalScope>>
 
-     createFromAst(const qlow::ast::Ast& ast)
 
- {
 
-     std::unique_ptr<Context> context = std::make_unique<Context>();
 
-     Printer& printer = Printer::getInstance();
 
-     auto& objects = ast.getObjects();
 
- #ifdef DEBUGGING
 
-     printf("starting building semantic representation (%d objects)\n", (int) objects.size());
 
- #endif
 
-     // create classes
 
-     std::unique_ptr<sem::GlobalScope> globalScope = std::make_unique<sem::GlobalScope>(*context);
 
-     for (auto& astObject : objects) {
 
-         if (auto* cls = dynamic_cast<ast::Class*>(astObject.get()); cls) {
 
-             globalScope->classes[cls->name] = std::make_unique<sem::Class>(cls, *globalScope);
 
-         }
 
-         else if (auto* function = dynamic_cast<ast::MethodDefinition*>(astObject.get()); function) {
 
-             globalScope->functions[function->name] = std::make_unique<sem::Method>(function, *globalScope);
 
-         }
 
-     }
 
- #ifdef DEBUGGING
 
-     printf("created symbol table entries for all classes\n");
 
- #endif 
 
-     
 
-     StructureVisitor av;
 
-     
 
-     // create all methods and fields
 
-     for (auto& [name, semClass] : globalScope->classes) {
 
-         for (auto& feature : semClass->astNode->features) {
 
-             
 
-             if (auto* field = dynamic_cast<qlow::ast::FieldDeclaration*> (feature.get()); field) {
 
-                 if (semClass->fields.find(field->name) != semClass->fields.end()) // throw, if field already exists
 
-                     throw SemanticError(SemanticError::DUPLICATE_FIELD_DECLARATION, field->name, field->pos);
 
-                 
 
-                 // otherwise add to the fields list
 
-                 semClass->fields[field->name] = unique_dynamic_cast<Field>(field->accept(av, semClass->scope));
 
-             }
 
-             else if (auto* method = dynamic_cast<qlow::ast::MethodDefinition*> (feature.get()); method) {
 
-                 if (semClass->methods.find(method->name) != semClass->methods.end()) // throw, if method already exists
 
-                     throw SemanticError(SemanticError::DUPLICATE_METHOD_DEFINITION, method->name, method->pos);
 
-                 
 
-                 // otherwise add to the methods list
 
-                 semClass->methods[method->name] = unique_dynamic_cast<Method>(method->accept(av, semClass->scope));
 
-             }
 
-             else {
 
-                 // if a feature is neither a method nor a field, something went horribly wrong
 
-                 throw "internal error, feature neither field nor method";
 
-             }
 
-         }
 
-     }
 
-     
 
-     for (auto& [name, method] : globalScope->functions) {
 
-         auto returnType = globalScope->getType(method->astNode->type.get());
 
-         if (returnType != nullptr) {
 
-             method->returnType = returnType;
 
-         }
 
-         else {
 
-             if (method->astNode->type == nullptr) {
 
-                 throw SemanticError(SemanticError::UNKNOWN_TYPE,
 
-                             method->name,
 
-                             method->astNode->pos);
 
-             }
 
-             else {
 
-                 throw SemanticError(SemanticError::UNKNOWN_TYPE,
 
-                             method->astNode->type->asString(),
 
-                             method->astNode->type->pos);
 
-             }
 
-         }
 
-         
 
-         // otherwise add to the methods list
 
-         globalScope->functions[method->name] = unique_dynamic_cast<Method>(method->astNode->accept(av, *globalScope));
 
-     }
 
-     
 
- #ifdef DEBUGGING
 
-     printf("created all methods and fields\n");
 
- #endif
 
-     
 
-     for (auto& [name, semClass] : globalScope->classes) {
 
-         for (auto& [name, method] : semClass->methods) {
 
-             if (method->astNode->body) { // if not declaration
 
-                 method->containingClass = semClass.get();
 
-                 method->generateThisExpression();
 
-                 method->body = unique_dynamic_cast<sem::DoEndBlock>(method->astNode->body->accept(av, method->scope));
 
-             }
 
-         }
 
-     }
 
-     for (auto& [name, method] : globalScope->functions) {
 
-         if (method->astNode->body) { // if not declaration
 
-             method->body = unique_dynamic_cast<sem::DoEndBlock>(method->astNode->body->accept(av, method->scope));
 
-         }
 
-     }
 
-     
 
- #ifdef DEBUGGING
 
-     printf("created all method bodies\n");
 
- #endif
 
-     
 
-     return { std::move(context), std::move(globalScope) };
 
- }
 
- }
 
- }
 
- std::string Class::toString(void) const
 
- {
 
-     std::string val = "Class[";
 
-     
 
-     // add fields
 
-     for (auto& field : fields)
 
-         val += field.second->toString() + ", ";
 
-     if (!fields.empty())
 
-         val = val.substr(0, val.length() - 2);
 
-     
 
-     // add methods
 
-     for (auto& method : methods)
 
-         val += method.second->toString() + ", ";
 
-     if (!methods.empty())
 
-         val = val.substr(0, val.length() - 2);
 
-     
 
-     val += " (";
 
-     val += std::to_string(this->astNode->pos.first_line) + ", ";
 
-     val += std::to_string(this->astNode->pos.first_column);
 
-     val += " )";
 
-     
 
-     return val + "]";
 
- }
 
- std::string Variable::toString(void) const
 
- {
 
-     return "Variable[" + this->name + "]";
 
- }
 
- std::string Field::toString(void) const
 
- {
 
-     return "Field[" + this->name + "]";
 
- }
 
- void Method::generateThisExpression(void)
 
- {
 
-     auto te = std::make_unique<ThisExpression>(this);
 
-     thisExpression = te.get();
 
-     scope.putVariable(this->thisExpression->name, std::move(te));
 
- }
 
- std::string Method::toString(void) const
 
- {
 
-     return "Method[" + this->name + "]";
 
- }
 
- #define COMMA ,
 
- #define ACCEPT_DEFINITION(ClassName, Visitor, ReturnType, Arg) \
 
- ReturnType ClassName::accept(Visitor& v, Arg arg) \
 
- { \
 
-     return v.visit(*this, arg); \
 
- }
 
- ACCEPT_DEFINITION(LocalVariableExpression, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
 
- ACCEPT_DEFINITION(BinaryOperation, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
 
- ACCEPT_DEFINITION(CastExpression, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
 
- ACCEPT_DEFINITION(NewExpression, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
 
- ACCEPT_DEFINITION(NewArrayExpression, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
 
- ACCEPT_DEFINITION(UnaryOperation, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
 
- ACCEPT_DEFINITION(MethodCallExpression, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
 
- ACCEPT_DEFINITION(FieldAccessExpression, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
 
- ACCEPT_DEFINITION(AddressExpression, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
 
- ACCEPT_DEFINITION(IntConst, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
 
- ACCEPT_DEFINITION(ThisExpression, ExpressionCodegenVisitor, llvm::Value*, llvm::IRBuilder<>&)
 
- ACCEPT_DEFINITION(Expression, LValueVisitor, llvm::Value*, qlow::gen::FunctionGenerator&)
 
- ACCEPT_DEFINITION(LocalVariableExpression, LValueVisitor, llvm::Value*, qlow::gen::FunctionGenerator&)
 
- ACCEPT_DEFINITION(FieldAccessExpression, LValueVisitor, llvm::Value*, qlow::gen::FunctionGenerator&)
 
- ACCEPT_DEFINITION(AssignmentStatement, StatementVisitor, llvm::Value*, qlow::gen::FunctionGenerator&) 
 
- ACCEPT_DEFINITION(DoEndBlock, StatementVisitor, llvm::Value*, qlow::gen::FunctionGenerator&) 
 
- ACCEPT_DEFINITION(IfElseBlock, StatementVisitor, llvm::Value*, qlow::gen::FunctionGenerator&) 
 
- ACCEPT_DEFINITION(WhileBlock, StatementVisitor, llvm::Value*, qlow::gen::FunctionGenerator&) 
 
- ACCEPT_DEFINITION(ReturnStatement, StatementVisitor, llvm::Value*, qlow::gen::FunctionGenerator&) 
 
- ACCEPT_DEFINITION(FeatureCallStatement, StatementVisitor, llvm::Value*, qlow::gen::FunctionGenerator&) 
 
- std::string AssignmentStatement::toString(void) const
 
- {
 
-     return "AssignmentStatement[" + this->target->toString() + " := " +
 
-         this->value->toString() + "]";
 
- }
 
- std::string ReturnStatement::toString(void) const
 
- {
 
-     return "ReturnStatement[" + this->value->toString() + "]";
 
- }
 
- std::string LocalVariableExpression::toString(void) const
 
- {
 
-     return "LocalVariableExpression[" + var->name + "]";
 
- }
 
- std::string UnaryOperation::toString(void) const
 
- {
 
-     return "UnaryOperation[" + arg->toString() + "]";
 
- }
 
- std::string BinaryOperation::toString(void) const
 
- {
 
-     return "BinaryOperation[" + left->toString() + ", " +
 
-         right->toString() + "]";
 
- }
 
- std::string CastExpression::toString(void) const
 
- {
 
-     // TODO remove optional unwrapping
 
-     return "CastExpression[" + expression->toString() + " to " +
 
-         targetType->asString() + "]";
 
- }
 
- std::string NewExpression::toString(void) const
 
- {
 
-     // TODO remove optional unwrapping
 
-     return "NewExpression[" + type->asString() + "]";
 
- }
 
- std::string NewArrayExpression::toString(void) const
 
- {
 
-     // TODO remove optional unwrapping
 
-     return "NewArrayExpression[" + elementType->asString() + "; " +
 
-         length->toString() + "]";
 
- }
 
- std::string MethodCallExpression::toString(void) const
 
- {
 
-     if (this->target)
 
-         return "MethodCallExpression[" + target->toString() + "." + callee->toString() + "]";
 
-     else
 
-         return "MethodCallExpression[" + callee->toString() + "]";
 
- }
 
- std::string FieldAccessExpression::toString(void) const
 
- {
 
-     if (this->target)
 
-         return "FieldAccessExpression[" + target->toString() + "." + accessed->toString() + "]";
 
-     else
 
-         return "FieldAccessExpression[" + accessed->toString() + "]";
 
- }
 
- std::string FeatureCallStatement::toString(void) const
 
- {
 
-     return "FeatureCallStatement[" + expr->callee->toString() + "]";
 
- }
 
 
  |