AstVisitor.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. variable->isParameter = true;
  53. m->arguments.push_back(variable.get());
  54. std::string varname = variable->name;
  55. m->scope.putVariable(varname, std::move(variable));
  56. }
  57. else {
  58. throw "internal error creating argument";
  59. }
  60. }
  61. return m;
  62. //throw " std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::MethodDefinition& ast, sem::Scope& scope) shouldn't be called";
  63. }
  64. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::VariableDeclaration& ast, sem::Scope& scope)
  65. {
  66. auto v = std::make_unique<sem::Variable>();
  67. v->name = ast.name;
  68. auto type = scope.getType(*ast.type);
  69. if (type) {
  70. v->type = std::move(type);
  71. }
  72. else {
  73. throw SemanticError(SemanticError::UNKNOWN_TYPE,
  74. ast.type->asString(),
  75. ast.type->pos
  76. );
  77. }
  78. return v;
  79. }
  80. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::Statement& ast, sem::Scope& scope)
  81. {
  82. printf("at: %d:%d to %d:%d\n", ast.pos.first_line, ast.pos.first_column, ast.pos.last_line, ast.pos.last_column);
  83. printf("type: %s\n", typeid(ast).name());
  84. throw "visit(Statement) shouldn't be called";
  85. }
  86. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::DoEndBlock& ast, sem::Scope& scope)
  87. {
  88. sem::LocalScope* lscope = dynamic_cast<sem::LocalScope*>(&scope);
  89. if (!lscope)
  90. throw "error: non-method scope inside method";
  91. auto body = std::make_unique<sem::DoEndBlock>(*lscope);
  92. for (auto& statement : ast.statements) {
  93. if (ast::LocalVariableStatement* nvs = dynamic_cast<ast::LocalVariableStatement*>(statement.get()); nvs) {
  94. auto type = body->scope.getType(*nvs->type);
  95. if (!type)
  96. throw SemanticError(SemanticError::UNKNOWN_TYPE,
  97. nvs->type->asString(),
  98. nvs->type->pos);
  99. auto var = std::make_unique<sem::Variable>(std::move(type), nvs->name);
  100. body->scope.putVariable(nvs->name, std::move(var));
  101. continue;
  102. }
  103. auto v = statement->accept(*this, body->scope);
  104. if (dynamic_cast<sem::MethodCallExpression*>(v.get()) != nullptr) {
  105. body->statements.push_back(
  106. std::make_unique<sem::FeatureCallStatement>(
  107. unique_dynamic_cast<sem::MethodCallExpression>(std::move(v))));
  108. }
  109. else {
  110. body->statements.push_back(unique_dynamic_cast<sem::Statement>(std::move(v)));
  111. }
  112. }
  113. return body;
  114. }
  115. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::IfElseBlock& ast, sem::Scope& scope)
  116. {
  117. auto condition = ast.condition->accept(*this, scope);
  118. auto ifB = ast.ifBlock->accept(*this, scope);
  119. auto eB= ast.elseBlock->accept(*this, scope);
  120. if (!dynamic_cast<sem::DoEndBlock*>(ifB.get())
  121. || !dynamic_cast<sem::DoEndBlock*>(eB.get())
  122. || !dynamic_cast<sem::Expression*>(condition.get()))
  123. throw "internal error, invalid if block";
  124. auto condExpr = unique_dynamic_cast<sem::Expression>(std::move(condition));
  125. auto ifBBlock = unique_dynamic_cast<sem::DoEndBlock>(std::move(ifB));
  126. auto eBBlock= unique_dynamic_cast<sem::DoEndBlock>(std::move(eB));
  127. auto ieb = std::make_unique<sem::IfElseBlock>(std::move(condExpr), std::move(ifBBlock), std::move(eBBlock));
  128. return ieb;
  129. }
  130. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::WhileBlock& ast, sem::Scope& scope)
  131. {
  132. auto condition = ast.condition->accept(*this, scope);
  133. auto body = ast.body->accept(*this, scope);
  134. if (!dynamic_cast<sem::DoEndBlock*>(body.get()) ||
  135. !dynamic_cast<sem::Expression*>(condition.get()))
  136. throw "internal error, invalid while block";
  137. auto condExpr = unique_dynamic_cast<sem::Expression>(std::move(condition));
  138. auto bodyblock = unique_dynamic_cast<sem::DoEndBlock>(std::move(body));
  139. auto wb = std::make_unique<sem::WhileBlock>(std::move(condExpr), std::move(bodyblock));
  140. return wb;
  141. }
  142. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::Expression& ast, sem::Scope& scope)
  143. {
  144. throw "visit(Expression) shouldn't be called";
  145. }
  146. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::FeatureCall& ast, sem::Scope& scope)
  147. {
  148. std::unique_ptr<sem::Expression> target = nullptr;
  149. if (ast.target) {
  150. target = unique_dynamic_cast<sem::Expression>(
  151. ast.target->accept(*this, scope));
  152. }
  153. sem::Method* method;
  154. sem::Variable* var;
  155. if (target) {
  156. method = target->type->getScope().getMethod(ast.name);
  157. var = target->type->getScope().getVariable(ast.name);
  158. }
  159. else {
  160. method = scope.getMethod(ast.name);
  161. var = scope.getVariable(ast.name);
  162. }
  163. if (target) {
  164. if (var) {
  165. return std::make_unique<sem::FieldAccessExpression>(std::move(target), dynamic_cast<sem::Field*>(var));
  166. }
  167. else if (method) {
  168. auto fce = std::make_unique<sem::MethodCallExpression>(
  169. std::move(target), method);
  170. if (ast.arguments.size() != method->arguments.size())
  171. throw SemanticError(SemanticError::WRONG_NUMBER_OF_ARGUMENTS, ast.name, ast.pos);
  172. for (size_t i = 0; i < ast.arguments.size(); i++) {
  173. auto& arg = ast.arguments[i];
  174. auto& argTypeShouldHave = method->arguments[i]->type;
  175. auto argument = arg->accept(*this, scope);
  176. if (sem::Expression* expr =
  177. dynamic_cast<sem::Expression*>(argument.get()); expr) {
  178. if (!expr->type->equals(*argTypeShouldHave))
  179. throw SemanticError(SemanticError::TYPE_MISMATCH,
  180. "argument passed to function has wrong type: '" +
  181. expr->type->asString() + "' instead of '" +
  182. argTypeShouldHave->asString() + "'",
  183. arg->pos
  184. );
  185. fce->arguments.push_back(
  186. unique_dynamic_cast<sem::Expression>(std::move(argument)));
  187. }
  188. else {
  189. throw "internal error: non-expression passed as function parameter";
  190. }
  191. }
  192. return fce;
  193. }
  194. else {
  195. throw SemanticError(SemanticError::FEATURE_NOT_FOUND, ast.name, ast.pos);
  196. }
  197. }
  198. else if (var) {
  199. if (sem::Field* field = dynamic_cast<sem::Field*>(var); field) {
  200. auto* thisExpr = scope.getVariable("this");
  201. if (!thisExpr)
  202. throw "no this found";
  203. Logger::getInstance().debug() << "feature call " << var->toString() << " is a field\n";
  204. return std::make_unique<sem::FieldAccessExpression>(std::make_unique<sem::LocalVariableExpression>(thisExpr), field);
  205. }
  206. else {
  207. Logger::getInstance().debug() << "feature call " << var->toString() << " is not a field\n";
  208. return std::make_unique<sem::LocalVariableExpression>(var);
  209. }
  210. }
  211. else if (method) {
  212. auto fce = std::make_unique<sem::MethodCallExpression>(nullptr, method);
  213. for (auto& arg : ast.arguments) {
  214. auto argument = arg->accept(*this, scope);
  215. if (dynamic_cast<sem::Expression*>(argument.get())) {
  216. fce->arguments.push_back(unique_dynamic_cast<sem::Expression>(std::move(argument)));
  217. }
  218. else {
  219. throw "internal error: non-expression passed as function parameter";
  220. }
  221. }
  222. fce->callee = method;
  223. return fce;
  224. }
  225. else {
  226. #ifdef DEBUGGING
  227. printf("var not found: %s\n", ast.name.c_str());
  228. printf("current scope: %s\n", scope.toString().c_str());
  229. #endif
  230. throw SemanticError(SemanticError::FEATURE_NOT_FOUND, ast.name, ast.pos);
  231. }
  232. }
  233. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::AssignmentStatement& ast, sem::Scope& scope)
  234. {
  235. auto as = std::make_unique<sem::AssignmentStatement>();
  236. // as->value = unique_dynamic_cast<sem::Expression>(visit(*ast.expr, classes));
  237. // as->target = unique_dynamic_cast<sem::Expression>(visit(*ast.target, classes));
  238. as->value = unique_dynamic_cast<sem::Expression>(ast.expr->accept(*this, scope));
  239. as->target = unique_dynamic_cast<sem::Expression>(ast.target->accept(*this, scope));
  240. if (as->target->type->equals(*as->value->type)) {
  241. return as;
  242. }
  243. else {
  244. throw SemanticError(
  245. SemanticError::TYPE_MISMATCH,
  246. "Can't assign expression of type '" + as->value->type->asString() +
  247. "' to value of type '" + as->target->type->asString() + "'.",
  248. ast.pos
  249. );
  250. }
  251. }
  252. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::ReturnStatement& ast, sem::Scope& scope)
  253. {
  254. auto shouldReturn = scope.getReturnableType();
  255. if (shouldReturn == nullptr) {
  256. if (ast.expr == nullptr)
  257. return std::make_unique<sem::ReturnStatement>();
  258. else
  259. throw SemanticError(
  260. SemanticError::INVALID_RETURN_TYPE,
  261. "This method should not return any value.",
  262. ast.expr->pos
  263. );
  264. }
  265. else if (ast.expr == nullptr) {
  266. throw SemanticError(
  267. SemanticError::INVALID_RETURN_TYPE,
  268. "This method should return a value.",
  269. ast.pos
  270. );
  271. }
  272. auto returnValue = unique_dynamic_cast<sem::Expression>(ast.expr->accept(*this, scope));
  273. if (!shouldReturn->equals(*returnValue->type)) {
  274. throw SemanticError(
  275. SemanticError::INVALID_RETURN_TYPE,
  276. "return value must be of type '" + shouldReturn->asString() + "' (not '" +
  277. returnValue->type->asString() + "')",
  278. ast.expr->pos
  279. );
  280. }
  281. auto as = std::make_unique<sem::ReturnStatement>();
  282. as->value = std::move(returnValue);
  283. return as;
  284. }
  285. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::LocalVariableStatement& ast, sem::Scope& scope)
  286. {
  287. throw "shouldn't be called";
  288. }
  289. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(
  290. ast::AddressExpression& ast, sem::Scope& scope)
  291. {
  292. auto target = unique_dynamic_cast<sem::Expression>(ast.target->accept(*this, scope));
  293. auto& targetType = target->type;
  294. if (!target->isLValue()) {
  295. throw NotLValue(targetType->asString(), ast.pos);
  296. }
  297. return std::make_unique<sem::AddressExpression>(std::move(target));
  298. }
  299. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::IntConst& ast, sem::Scope& scope)
  300. {
  301. return std::make_unique<sem::IntConst>(ast.value);
  302. }
  303. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::UnaryOperation& ast, sem::Scope& scope)
  304. {
  305. auto argument = unique_dynamic_cast<sem::Expression>(ast.expr->accept(*this, scope));
  306. auto ret = std::make_unique<sem::UnaryOperation>(argument->type);
  307. // TODO not a feasible assumption
  308. ret->opString = ast.opString;
  309. ret->side = ast.side;
  310. ret->arg = std::move(argument);
  311. return ret;
  312. }
  313. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::BinaryOperation& ast, sem::Scope& scope)
  314. {
  315. auto leftEval = unique_dynamic_cast<sem::Expression>(ast.left->accept(*this, scope));
  316. auto rightEval = unique_dynamic_cast<sem::Expression>(ast.right->accept(*this, scope));
  317. sem::Method* operationMethod = leftEval->type->getScope().resolveMethod(
  318. ast.opString, { rightEval->type }
  319. );
  320. Logger::getInstance().debug() << "looked for operation method for operator " <<
  321. ast.opString << std::endl;
  322. if (!operationMethod) {
  323. throw SemanticError(SemanticError::OPERATOR_NOT_FOUND,
  324. "operator " + ast.opString + " not found for types '" +
  325. leftEval->type->asString() + "' and '" + rightEval->type->asString() + "'",
  326. ast.opPos);
  327. }
  328. auto ret = std::make_unique<sem::BinaryOperation>(leftEval->type, &ast);
  329. ret->operationMethod = operationMethod;
  330. ret->opString = ast.opString;
  331. ret->left = std::move(leftEval);
  332. ret->right = std::move(rightEval);
  333. return ret;
  334. }
  335. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::NewArrayExpression& ast, sem::Scope& scope)
  336. {
  337. auto ret = std::make_unique<sem::NewArrayExpression>(scope.getType(*ast.type));
  338. return ret;
  339. }
  340. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::CastExpression& ast, sem::Scope& scope)
  341. {
  342. auto expr = unique_dynamic_cast<sem::Expression>(ast.expression->accept(*this, scope));
  343. auto type = scope.getType(*ast.targetType);
  344. return std::make_unique<sem::CastExpression>(
  345. std::move(expr), std::move(type), &ast);
  346. }