AstVisitor.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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>(scope.getContext());
  22. f->name = ast.name;
  23. auto type = scope.getType(ast.type.get());
  24. if (type != sem::NO_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.get());
  38. if (returnType == sem::NO_TYPE) {
  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>(scope.getContext());
  67. v->name = ast.name;
  68. auto type = scope.getType(ast.type.get());
  69. if (type != sem::NO_TYPE) {
  70. v->type = 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.get());
  95. if (type == sem::NO_TYPE)
  96. throw SemanticError(SemanticError::UNKNOWN_TYPE,
  97. nvs->type->asString(),
  98. nvs->type->pos);
  99. auto var = std::make_unique<sem::Variable>(scope.getContext(), 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. // TODO rewrite types
  156. if (target) {
  157. //method = target->type->getScope().getMethod(ast.name);
  158. //var = target->type->getScope().getVariable(ast.name);
  159. }
  160. else {
  161. method = scope.getMethod(ast.name);
  162. var = scope.getVariable(ast.name);
  163. }
  164. if (target) {
  165. if (var) {
  166. return std::make_unique<sem::FieldAccessExpression>(std::move(target), dynamic_cast<sem::Field*>(var));
  167. }
  168. else if (method) {
  169. auto fce = std::make_unique<sem::MethodCallExpression>(
  170. std::move(target), method);
  171. if (ast.arguments.size() != method->arguments.size())
  172. throw SemanticError(SemanticError::WRONG_NUMBER_OF_ARGUMENTS, ast.name, ast.pos);
  173. for (size_t i = 0; i < ast.arguments.size(); i++) {
  174. auto& arg = ast.arguments[i];
  175. auto& argTypeShouldHave = method->arguments[i]->type;
  176. auto argument = arg->accept(*this, scope);
  177. if (sem::Expression* expr =
  178. dynamic_cast<sem::Expression*>(argument.get()); expr) {
  179. if (expr->type != argTypeShouldHave)
  180. throw SemanticError(SemanticError::TYPE_MISMATCH,
  181. "argument passed to function has wrong type",
  182. // TODO rewrite types
  183. //expr->type->asString() + "' instead of '" +
  184. //argTypeShouldHave->asString() + "'",
  185. arg->pos
  186. );
  187. fce->arguments.push_back(
  188. unique_dynamic_cast<sem::Expression>(std::move(argument)));
  189. }
  190. else {
  191. throw "internal error: non-expression passed as function parameter";
  192. }
  193. }
  194. return fce;
  195. }
  196. else {
  197. throw SemanticError(SemanticError::FEATURE_NOT_FOUND, ast.name, ast.pos);
  198. }
  199. }
  200. else if (var) {
  201. if (sem::Field* field = dynamic_cast<sem::Field*>(var); field) {
  202. auto* thisExpr = scope.getVariable("this");
  203. if (!thisExpr)
  204. throw "no this found";
  205. //Printer::getInstance().debug() << "feature call " << var->toString() << " is a field\n";
  206. return std::make_unique<sem::FieldAccessExpression>(std::make_unique<sem::LocalVariableExpression>(thisExpr), field);
  207. }
  208. else {
  209. //Printer::getInstance().debug() << "feature call " << var->toString() << " is not a field\n";
  210. return std::make_unique<sem::LocalVariableExpression>(var);
  211. }
  212. }
  213. else if (method) {
  214. auto fce = std::make_unique<sem::MethodCallExpression>(nullptr, method);
  215. for (auto& arg : ast.arguments) {
  216. auto argument = arg->accept(*this, scope);
  217. if (dynamic_cast<sem::Expression*>(argument.get())) {
  218. fce->arguments.push_back(unique_dynamic_cast<sem::Expression>(std::move(argument)));
  219. }
  220. else {
  221. throw "internal error: non-expression passed as function parameter";
  222. }
  223. }
  224. fce->callee = method;
  225. return fce;
  226. }
  227. else {
  228. #ifdef DEBUGGING
  229. printf("var not found: %s\n", ast.name.c_str());
  230. printf("current scope: %s\n", scope.toString().c_str());
  231. #endif
  232. throw SemanticError(SemanticError::FEATURE_NOT_FOUND, ast.name, ast.pos);
  233. }
  234. }
  235. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::AssignmentStatement& ast, sem::Scope& scope)
  236. {
  237. auto as = std::make_unique<sem::AssignmentStatement>(scope.getContext());
  238. // as->value = unique_dynamic_cast<sem::Expression>(visit(*ast.expr, classes));
  239. // as->target = unique_dynamic_cast<sem::Expression>(visit(*ast.target, classes));
  240. as->value = unique_dynamic_cast<sem::Expression>(ast.expr->accept(*this, scope));
  241. as->target = unique_dynamic_cast<sem::Expression>(ast.target->accept(*this, scope));
  242. if (as->target->type == as->value->type) {
  243. return as;
  244. }
  245. else {
  246. throw SemanticError(
  247. SemanticError::TYPE_MISMATCH,
  248. "Can't assign expression of type to type.",
  249. // TODO rewrite
  250. //"Can't assign expression of type '" + as->value->type->asString() +
  251. //"' to value of type '" + as->target->type->asString() + "'.",
  252. ast.pos
  253. );
  254. }
  255. }
  256. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::ReturnStatement& ast, sem::Scope& scope)
  257. {
  258. auto shouldReturn = scope.getReturnableType();
  259. if (shouldReturn == sem::NO_TYPE) {
  260. if (ast.expr == nullptr)
  261. return std::make_unique<sem::ReturnStatement>(scope.getContext());
  262. else
  263. throw SemanticError(
  264. SemanticError::INVALID_RETURN_TYPE,
  265. "This method should not return any value.",
  266. ast.expr->pos
  267. );
  268. }
  269. else if (ast.expr == nullptr) {
  270. throw SemanticError(
  271. SemanticError::INVALID_RETURN_TYPE,
  272. "This method should return a value.",
  273. ast.pos
  274. );
  275. }
  276. auto returnValue = unique_dynamic_cast<sem::Expression>(ast.expr->accept(*this, scope));
  277. if (shouldReturn != returnValue->type) {
  278. auto should = scope.getContext().getTypeString(shouldReturn);
  279. auto is = scope.getContext().getTypeString(returnValue->type);
  280. throw SemanticError::invalidReturnType(should, is, ast.expr->pos);
  281. }
  282. auto as = std::make_unique<sem::ReturnStatement>(scope.getContext());
  283. as->value = std::move(returnValue);
  284. return as;
  285. }
  286. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::LocalVariableStatement& ast, sem::Scope& scope)
  287. {
  288. throw "shouldn't be called";
  289. }
  290. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(
  291. ast::AddressExpression& ast, sem::Scope& scope)
  292. {
  293. auto target = unique_dynamic_cast<sem::Expression>(ast.target->accept(*this, scope));
  294. auto targetType = target->type;
  295. if (!target->isLValue()) {
  296. throw NotLValue(scope.getContext().getTypeString(targetType), ast.pos);
  297. }
  298. return std::make_unique<sem::AddressExpression>(std::move(target));
  299. }
  300. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::IntConst& ast, sem::Scope& scope)
  301. {
  302. return std::make_unique<sem::IntConst>(scope.getContext(), ast.value);
  303. }
  304. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::UnaryOperation& ast, sem::Scope& scope)
  305. {
  306. auto argument = unique_dynamic_cast<sem::Expression>(ast.expr->accept(*this, scope));
  307. auto ret = std::make_unique<sem::UnaryOperation>(scope.getContext(), argument->type);
  308. // TODO not a feasible assumption
  309. ret->opString = ast.opString;
  310. ret->side = ast.side;
  311. ret->arg = std::move(argument);
  312. return ret;
  313. }
  314. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::BinaryOperation& ast, sem::Scope& scope)
  315. {
  316. auto leftEval = unique_dynamic_cast<sem::Expression>(ast.left->accept(*this, scope));
  317. auto rightEval = unique_dynamic_cast<sem::Expression>(ast.right->accept(*this, scope));
  318. throw SemanticError(SemanticError::OPERATOR_NOT_FOUND, "TODO implement", ast.pos);
  319. /*
  320. sem::Method* operationMethod = leftEval->type->getScope().resolveMethod(
  321. ast.opString, { rightEval->type }
  322. );
  323. Printer::getInstance().debug() << "looked for operation method for operator " <<
  324. ast.opString << std::endl;
  325. if (!operationMethod) {
  326. throw SemanticError(SemanticError::OPERATOR_NOT_FOUND,
  327. "operator " + ast.opString + " not found for types '" +
  328. leftEval->type->asString() + "' and '" + rightEval->type->asString() + "'",
  329. ast.opPos);
  330. }
  331. auto ret = std::make_unique<sem::BinaryOperation>(leftEval->type, &ast);
  332. ret->operationMethod = operationMethod;
  333. ret->opString = ast.opString;
  334. ret->left = std::move(leftEval);
  335. ret->right = std::move(rightEval);
  336. return ret;*/
  337. }
  338. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::NewArrayExpression& ast, sem::Scope& scope)
  339. {
  340. auto ret = std::make_unique<sem::NewArrayExpression>(scope.getContext(), scope.getType(ast.type.get()));
  341. return ret;
  342. }
  343. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::CastExpression& ast, sem::Scope& scope)
  344. {
  345. auto expr = unique_dynamic_cast<sem::Expression>(ast.expression->accept(*this, scope));
  346. auto type = scope.getType(ast.targetType.get());
  347. return std::make_unique<sem::CastExpression>(
  348. std::move(expr), type, &ast);
  349. }