AstVisitor.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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);
  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>(scope.getContext());
  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>(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. Logger::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. Logger::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. throw SemanticError(
  279. SemanticError::INVALID_RETURN_TYPE,
  280. //TODO rewrite
  281. "wrong return value",
  282. //"return value must be of type '" + shouldReturn->asString() + "' (not '" +
  283. //returnValue->type->asString() + "')",
  284. ast.expr->pos
  285. );
  286. }
  287. auto as = std::make_unique<sem::ReturnStatement>(scope.getContext());
  288. as->value = std::move(returnValue);
  289. return as;
  290. }
  291. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::LocalVariableStatement& ast, sem::Scope& scope)
  292. {
  293. throw "shouldn't be called";
  294. }
  295. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(
  296. ast::AddressExpression& ast, sem::Scope& scope)
  297. {
  298. auto target = unique_dynamic_cast<sem::Expression>(ast.target->accept(*this, scope));
  299. auto& targetType = target->type;
  300. if (!target->isLValue()) {
  301. // TODO rewrite
  302. //throw NotLValue(targetType->asString(), ast.pos);
  303. throw NotLValue("type", ast.pos);
  304. }
  305. return std::make_unique<sem::AddressExpression>(std::move(target));
  306. }
  307. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::IntConst& ast, sem::Scope& scope)
  308. {
  309. return std::make_unique<sem::IntConst>(scope.getContext(), ast.value);
  310. }
  311. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::UnaryOperation& ast, sem::Scope& scope)
  312. {
  313. auto argument = unique_dynamic_cast<sem::Expression>(ast.expr->accept(*this, scope));
  314. auto ret = std::make_unique<sem::UnaryOperation>(scope.getContext(), argument->type);
  315. // TODO not a feasible assumption
  316. ret->opString = ast.opString;
  317. ret->side = ast.side;
  318. ret->arg = std::move(argument);
  319. return ret;
  320. }
  321. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::BinaryOperation& ast, sem::Scope& scope)
  322. {
  323. auto leftEval = unique_dynamic_cast<sem::Expression>(ast.left->accept(*this, scope));
  324. auto rightEval = unique_dynamic_cast<sem::Expression>(ast.right->accept(*this, scope));
  325. throw "TODO implement";
  326. /*
  327. sem::Method* operationMethod = leftEval->type->getScope().resolveMethod(
  328. ast.opString, { rightEval->type }
  329. );
  330. Logger::getInstance().debug() << "looked for operation method for operator " <<
  331. ast.opString << std::endl;
  332. if (!operationMethod) {
  333. throw SemanticError(SemanticError::OPERATOR_NOT_FOUND,
  334. "operator " + ast.opString + " not found for types '" +
  335. leftEval->type->asString() + "' and '" + rightEval->type->asString() + "'",
  336. ast.opPos);
  337. }
  338. auto ret = std::make_unique<sem::BinaryOperation>(leftEval->type, &ast);
  339. ret->operationMethod = operationMethod;
  340. ret->opString = ast.opString;
  341. ret->left = std::move(leftEval);
  342. ret->right = std::move(rightEval);
  343. return ret;*/
  344. }
  345. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::NewArrayExpression& ast, sem::Scope& scope)
  346. {
  347. auto ret = std::make_unique<sem::NewArrayExpression>(scope.getContext(), scope.getType(*ast.type));
  348. return ret;
  349. }
  350. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::CastExpression& ast, sem::Scope& scope)
  351. {
  352. auto expr = unique_dynamic_cast<sem::Expression>(ast.expression->accept(*this, scope));
  353. auto type = scope.getType(*ast.targetType);
  354. return std::make_unique<sem::CastExpression>(
  355. std::move(expr), std::move(type), &ast);
  356. }