AstVisitor.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #include "AstVisitor.h"
  2. #include "Ast.h"
  3. #include "ErrorReporting.h"
  4. #include <typeinfo>
  5. #include "Util.h"
  6. using namespace qlow;
  7. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::Class& ast, sem::Scope& scope)
  8. {
  9. //auto c = std::make_unique<sem::Class>();
  10. //c->name = ast.name;
  11. //return c;
  12. throw "shouldn't be called";
  13. }
  14. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::FeatureDeclaration& ast, sem::Scope& scope)
  15. {
  16. // not needed, because
  17. throw "shouldn't be called";
  18. }
  19. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::FieldDeclaration& ast, sem::Scope& scope)
  20. {
  21. auto f = std::make_unique<sem::Field>();
  22. f->name = ast.name;
  23. auto type = scope.getType(*ast.type);
  24. if (type) {
  25. f->type = type;
  26. }
  27. else {
  28. throw SemanticError(SemanticError::UNKNOWN_TYPE,
  29. ast.type->asString(),
  30. ast.type->pos
  31. );
  32. }
  33. return f;
  34. }
  35. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::MethodDefinition& ast, sem::Scope& scope)
  36. {
  37. auto returnType = scope.getType(*ast.type);
  38. if (!returnType) {
  39. throw SemanticError(SemanticError::UNKNOWN_TYPE,
  40. ast.type->asString(),
  41. ast.type->pos
  42. );
  43. }
  44. auto m = std::make_unique<sem::Method>(scope, returnType);
  45. m->name = ast.name;
  46. m->astNode = &ast;
  47. for (auto& arg : ast.arguments) {
  48. auto var = arg->accept(*this, scope);
  49. if (dynamic_cast<sem::Variable*>(var.get())) {
  50. std::unique_ptr<sem::Variable> variable =
  51. unique_dynamic_cast<sem::Variable>(std::move(var));
  52. m->arguments.push_back(variable.get());
  53. std::string varname = variable->name;
  54. m->scope.putVariable(varname, std::move(variable));
  55. }
  56. else {
  57. throw "internal error creating argument";
  58. }
  59. }
  60. return m;
  61. //throw " std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::MethodDefinition& ast, sem::Scope& scope) shouldn't be called";
  62. }
  63. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::VariableDeclaration& ast, sem::Scope& scope)
  64. {
  65. auto v = std::make_unique<sem::Variable>();
  66. v->name = ast.name;
  67. auto type = scope.getType(*ast.type);
  68. if (type) {
  69. v->type = std::move(type);
  70. }
  71. else {
  72. throw SemanticError(SemanticError::UNKNOWN_TYPE,
  73. ast.type->asString(),
  74. ast.type->pos
  75. );
  76. }
  77. return v;
  78. }
  79. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::Statement& ast, sem::Scope& scope)
  80. {
  81. printf("at: %d:%d to %d:%d\n", ast.pos.first_line, ast.pos.first_column, ast.pos.last_line, ast.pos.last_column);
  82. printf("type: %s\n", typeid(ast).name());
  83. throw "visit(Statement) shouldn't be called";
  84. }
  85. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::DoEndBlock& ast, sem::Scope& scope)
  86. {
  87. sem::LocalScope* lscope = dynamic_cast<sem::LocalScope*>(&scope);
  88. if (!lscope)
  89. throw "error: non-method scope inside method";
  90. auto body = std::make_unique<sem::DoEndBlock>(*lscope);
  91. for (auto& statement : ast.statements) {
  92. if (ast::LocalVariableStatement* nvs = dynamic_cast<ast::LocalVariableStatement*>(statement.get()); nvs) {
  93. auto type = body->scope.getType(*nvs->type);
  94. if (!type)
  95. throw SemanticError(SemanticError::UNKNOWN_TYPE,
  96. nvs->type->asString(),
  97. nvs->type->pos);
  98. auto var = std::make_unique<sem::Variable>(std::move(type), nvs->name);
  99. body->scope.putVariable(nvs->name, std::move(var));
  100. continue;
  101. }
  102. auto v = statement->accept(*this, body->scope);
  103. if (dynamic_cast<sem::MethodCallExpression*>(v.get()) != nullptr) {
  104. body->statements.push_back(
  105. std::make_unique<sem::FeatureCallStatement>(
  106. unique_dynamic_cast<sem::MethodCallExpression>(std::move(v))));
  107. }
  108. else {
  109. body->statements.push_back(unique_dynamic_cast<sem::Statement>(std::move(v)));
  110. }
  111. }
  112. return body;
  113. }
  114. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::IfElseBlock& ast, sem::Scope& scope)
  115. {
  116. auto condition = ast.condition->accept(*this, scope);
  117. auto ifB = ast.ifBlock->accept(*this, scope);
  118. auto eB= ast.elseBlock->accept(*this, scope);
  119. if (!dynamic_cast<sem::DoEndBlock*>(ifB.get())
  120. || !dynamic_cast<sem::DoEndBlock*>(eB.get())
  121. || !dynamic_cast<sem::Expression*>(condition.get()))
  122. throw "internal error, invalid if block";
  123. auto condExpr = unique_dynamic_cast<sem::Expression>(std::move(condition));
  124. auto ifBBlock = unique_dynamic_cast<sem::DoEndBlock>(std::move(ifB));
  125. auto eBBlock= unique_dynamic_cast<sem::DoEndBlock>(std::move(eB));
  126. auto ieb = std::make_unique<sem::IfElseBlock>(std::move(condExpr), std::move(ifBBlock), std::move(eBBlock));
  127. return ieb;
  128. }
  129. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::WhileBlock& ast, sem::Scope& scope)
  130. {
  131. auto condition = ast.condition->accept(*this, scope);
  132. auto body = ast.body->accept(*this, scope);
  133. if (!dynamic_cast<sem::DoEndBlock*>(body.get()) ||
  134. !dynamic_cast<sem::Expression*>(condition.get()))
  135. throw "internal error, invalid while block";
  136. auto condExpr = unique_dynamic_cast<sem::Expression>(std::move(condition));
  137. auto bodyblock = unique_dynamic_cast<sem::DoEndBlock>(std::move(body));
  138. auto wb = std::make_unique<sem::WhileBlock>(std::move(condExpr), std::move(bodyblock));
  139. return wb;
  140. }
  141. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::Expression& ast, sem::Scope& scope)
  142. {
  143. throw "visit(Expression) shouldn't be called";
  144. }
  145. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::FeatureCall& ast, sem::Scope& scope)
  146. {
  147. std::unique_ptr<sem::Expression> target = nullptr;
  148. if (ast.target) {
  149. target = unique_dynamic_cast<sem::Expression>(
  150. ast.target->accept(*this, scope));
  151. }
  152. sem::Method* method;
  153. sem::Variable* var;
  154. if (target) {
  155. method = target->type->getScope().getMethod(ast.name);
  156. var = target->type->getScope().getVariable(ast.name);
  157. }
  158. else {
  159. method = scope.getMethod(ast.name);
  160. var = scope.getVariable(ast.name);
  161. }
  162. if (target) {
  163. if (var) {
  164. return std::make_unique<sem::FieldAccessExpression>(std::move(target), dynamic_cast<sem::Field*>(var));
  165. }
  166. else if (method) {
  167. auto fce = std::make_unique<sem::MethodCallExpression>(
  168. std::move(target), method);
  169. if (ast.arguments.size() != method->arguments.size())
  170. throw SemanticError(SemanticError::WRONG_NUMBER_OF_ARGUMENTS, ast.name, ast.pos);
  171. for (size_t i = 0; i < ast.arguments.size(); i++) {
  172. auto& arg = ast.arguments[i];
  173. auto& argTypeShouldHave = method->arguments[i]->type;
  174. auto argument = arg->accept(*this, scope);
  175. if (sem::Expression* expr =
  176. dynamic_cast<sem::Expression*>(argument.get()); expr) {
  177. if (!expr->type->equals(*argTypeShouldHave))
  178. throw SemanticError(SemanticError::TYPE_MISMATCH,
  179. "argument passed to function has wrong type: '" +
  180. expr->type->asString() + "' instead of '" +
  181. argTypeShouldHave->asString() + "'",
  182. arg->pos
  183. );
  184. fce->arguments.push_back(
  185. unique_dynamic_cast<sem::Expression>(std::move(argument)));
  186. }
  187. else {
  188. throw "internal error: non-expression passed as function parameter";
  189. }
  190. }
  191. return fce;
  192. }
  193. else {
  194. throw SemanticError(SemanticError::FEATURE_NOT_FOUND, ast.name, ast.pos);
  195. }
  196. }
  197. else if (var) {
  198. if (sem::Field* field = dynamic_cast<sem::Field*>(var); field) {
  199. auto* thisExpr = scope.getVariable("this");
  200. if (!thisExpr)
  201. throw "no this found";
  202. return std::make_unique<sem::FieldAccessExpression>(std::make_unique<sem::LocalVariableExpression>(thisExpr), field);
  203. }
  204. else {
  205. return std::make_unique<sem::LocalVariableExpression>(var);
  206. }
  207. }
  208. else if (method) {
  209. auto fce = std::make_unique<sem::MethodCallExpression>(nullptr, method);
  210. for (auto& arg : ast.arguments) {
  211. auto argument = arg->accept(*this, scope);
  212. if (dynamic_cast<sem::Expression*>(argument.get())) {
  213. fce->arguments.push_back(unique_dynamic_cast<sem::Expression>(std::move(argument)));
  214. }
  215. else {
  216. throw "internal error: non-expression passed as function parameter";
  217. }
  218. }
  219. fce->callee = method;
  220. return fce;
  221. }
  222. else {
  223. #ifdef DEBUGGING
  224. printf("var not found: %s\n", ast.name.c_str());
  225. printf("current scope: %s\n", scope.toString().c_str());
  226. #endif
  227. throw SemanticError(SemanticError::FEATURE_NOT_FOUND, ast.name, ast.pos);
  228. }
  229. }
  230. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::AssignmentStatement& ast, sem::Scope& scope)
  231. {
  232. auto as = std::make_unique<sem::AssignmentStatement>();
  233. // as->value = unique_dynamic_cast<sem::Expression>(visit(*ast.expr, classes));
  234. // as->target = unique_dynamic_cast<sem::Expression>(visit(*ast.target, classes));
  235. as->value = unique_dynamic_cast<sem::Expression>(ast.expr->accept(*this, scope));
  236. as->target = unique_dynamic_cast<sem::Expression>(ast.target->accept(*this, scope));
  237. return as;
  238. }
  239. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::ReturnStatement& ast, sem::Scope& scope)
  240. {
  241. auto as = std::make_unique<sem::ReturnStatement>();
  242. as->value = unique_dynamic_cast<sem::Expression>(ast.expr->accept(*this, scope));
  243. return as;
  244. }
  245. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::LocalVariableStatement& ast, sem::Scope& scope)
  246. {
  247. throw "shouldn't be called";
  248. }
  249. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::IntConst& ast, sem::Scope& scope)
  250. {
  251. return std::make_unique<sem::IntConst>(ast.value);
  252. }
  253. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::UnaryOperation& ast, sem::Scope& scope)
  254. {
  255. auto argument = unique_dynamic_cast<sem::Expression>(ast.expr->accept(*this, scope));
  256. auto ret = std::make_unique<sem::UnaryOperation>(argument->type);
  257. // TODO not a feasible assumption
  258. ret->opString = ast.opString;
  259. ret->side = ast.side;
  260. ret->arg = std::move(argument);
  261. return ret;
  262. }
  263. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::BinaryOperation& ast, sem::Scope& scope)
  264. {
  265. auto leftEval = unique_dynamic_cast<sem::Expression>(ast.left->accept(*this, scope));
  266. auto rightEval = unique_dynamic_cast<sem::Expression>(ast.right->accept(*this, scope));
  267. sem::Method* operationMethod = leftEval->type->getScope().resolveMethod(
  268. ast.opString, { rightEval->type }
  269. );
  270. Logger::getInstance().debug() << "looked for operation method for operator " <<
  271. ast.opString << std::endl;
  272. if (!operationMethod) {
  273. throw SemanticError(SemanticError::OPERATOR_NOT_FOUND,
  274. std::string("operator ") + ast.opString + " not found for types '" +
  275. leftEval->type->asString() + "' and '" + rightEval->type->asString() + "'",
  276. ast.opPos);
  277. }
  278. auto ret = std::make_unique<sem::BinaryOperation>(leftEval->type, &ast);
  279. ret->operationMethod = operationMethod;
  280. ret->opString = ast.opString;
  281. ret->left = std::move(leftEval);
  282. ret->right = std::move(rightEval);
  283. return ret;
  284. }
  285. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::NewArrayExpression& ast, sem::Scope& scope)
  286. {
  287. auto ret = std::make_unique<sem::NewArrayExpression>(scope.getType(*ast.type));
  288. return ret;
  289. }
  290. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::CastExpression& ast, sem::Scope& scope)
  291. {
  292. auto expr = unique_dynamic_cast<sem::Expression>(ast.expression->accept(*this, scope));
  293. auto type = scope.getType(*ast.targetType);
  294. return std::make_unique<sem::CastExpression>(
  295. std::move(expr), std::move(type), &ast);
  296. }