AstVisitor.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #include "AstVisitor.h"
  2. #include "Ast.h"
  3. #include "ErrorReporting.h"
  4. #include "Context.h"
  5. #include <typeinfo>
  6. #include "Util.h"
  7. using namespace qlow;
  8. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::Class& ast, sem::Scope& scope)
  9. {
  10. //auto c = std::make_unique<sem::Class>();
  11. //c->name = ast.name;
  12. //return c;
  13. throw "shouldn't be called";
  14. }
  15. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::FeatureDeclaration& ast, sem::Scope& scope)
  16. {
  17. // not needed, because
  18. throw "shouldn't be called";
  19. }
  20. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::FieldDeclaration& ast, sem::Scope& scope)
  21. {
  22. auto f = std::make_unique<sem::Field>(scope.getContext());
  23. f->name = ast.name;
  24. auto type = scope.getType(ast.type.get());
  25. if (type != sem::NO_TYPE) {
  26. f->type = type;
  27. }
  28. else {
  29. throw SemanticError(SemanticError::UNKNOWN_TYPE,
  30. ast.type->asString(),
  31. ast.type->pos
  32. );
  33. }
  34. return f;
  35. }
  36. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::MethodDefinition& ast, sem::Scope& scope)
  37. {
  38. auto returnType = scope.getType(ast.type.get());
  39. if (returnType == sem::NO_TYPE) {
  40. throw SemanticError(SemanticError::UNKNOWN_TYPE,
  41. ast.type->asString(),
  42. ast.type->pos
  43. );
  44. }
  45. auto m = std::make_unique<sem::Method>(scope, returnType);
  46. m->name = ast.name;
  47. m->astNode = &ast;
  48. for (auto& arg : ast.arguments) {
  49. auto var = arg->accept(*this, scope);
  50. if (dynamic_cast<sem::Variable*>(var.get())) {
  51. std::unique_ptr<sem::Variable> variable =
  52. unique_dynamic_cast<sem::Variable>(std::move(var));
  53. variable->isParameter = true;
  54. m->arguments.push_back(variable.get());
  55. std::string& varname = variable->name;
  56. m->scope.putVariable(varname, std::move(variable));
  57. }
  58. else {
  59. throw "internal error creating argument";
  60. }
  61. }
  62. return m;
  63. //throw " std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::MethodDefinition& ast, sem::Scope& scope) shouldn't be called";
  64. }
  65. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::VariableDeclaration& ast, sem::Scope& scope)
  66. {
  67. auto v = std::make_unique<sem::Variable>(scope.getContext());
  68. v->name = ast.name;
  69. auto type = scope.getType(ast.type.get());
  70. if (type != sem::NO_TYPE) {
  71. v->type = type;
  72. }
  73. else {
  74. throw SemanticError(SemanticError::UNKNOWN_TYPE,
  75. ast.type->asString(),
  76. ast.type->pos
  77. );
  78. }
  79. return v;
  80. }
  81. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::Statement& ast, sem::Scope& scope)
  82. {
  83. printf("at: %d:%d to %d:%d\n", ast.pos.first_line, ast.pos.first_column, ast.pos.last_line, ast.pos.last_column);
  84. printf("type: %s\n", typeid(ast).name());
  85. throw "visit(Statement) shouldn't be called";
  86. }
  87. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::DoEndBlock& ast, sem::Scope& scope)
  88. {
  89. sem::LocalScope* lscope = dynamic_cast<sem::LocalScope*>(&scope);
  90. if (!lscope)
  91. throw "error: non-method scope inside method";
  92. auto body = std::make_unique<sem::DoEndBlock>(*lscope);
  93. for (auto& statement : ast.statements) {
  94. if (ast::LocalVariableStatement* nvs = dynamic_cast<ast::LocalVariableStatement*>(statement.get()); nvs) {
  95. auto type = body->scope.getType(nvs->type.get());
  96. if (type == sem::NO_TYPE)
  97. throw SemanticError(SemanticError::UNKNOWN_TYPE,
  98. nvs->type->asString(),
  99. nvs->type->pos);
  100. auto var = std::make_unique<sem::Variable>(scope.getContext(), type, nvs->name);
  101. body->scope.putVariable(nvs->name, std::move(var));
  102. continue;
  103. }
  104. auto v = statement->accept(*this, body->scope);
  105. if (dynamic_cast<sem::MethodCallExpression*>(v.get()) != nullptr) {
  106. body->statements.push_back(
  107. std::make_unique<sem::FeatureCallStatement>(
  108. unique_dynamic_cast<sem::MethodCallExpression>(std::move(v))));
  109. }
  110. else {
  111. body->statements.push_back(unique_dynamic_cast<sem::Statement>(std::move(v)));
  112. }
  113. }
  114. return body;
  115. }
  116. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::IfElseBlock& ast, sem::Scope& scope)
  117. {
  118. auto condition = ast.condition->accept(*this, scope);
  119. auto ifB = ast.ifBlock->accept(*this, scope);
  120. auto eB= ast.elseBlock->accept(*this, scope);
  121. if (!dynamic_cast<sem::DoEndBlock*>(ifB.get())
  122. || !dynamic_cast<sem::DoEndBlock*>(eB.get())
  123. || !dynamic_cast<sem::Expression*>(condition.get()))
  124. throw "internal error, invalid if block";
  125. auto condExpr = unique_dynamic_cast<sem::Expression>(std::move(condition));
  126. auto ifBBlock = unique_dynamic_cast<sem::DoEndBlock>(std::move(ifB));
  127. auto eBBlock= unique_dynamic_cast<sem::DoEndBlock>(std::move(eB));
  128. auto ieb = std::make_unique<sem::IfElseBlock>(std::move(condExpr), std::move(ifBBlock), std::move(eBBlock));
  129. return ieb;
  130. }
  131. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::WhileBlock& ast, sem::Scope& scope)
  132. {
  133. auto condition = ast.condition->accept(*this, scope);
  134. auto body = ast.body->accept(*this, scope);
  135. if (!dynamic_cast<sem::DoEndBlock*>(body.get()) ||
  136. !dynamic_cast<sem::Expression*>(condition.get()))
  137. throw "internal error, invalid while block";
  138. auto condExpr = unique_dynamic_cast<sem::Expression>(std::move(condition));
  139. auto bodyblock = unique_dynamic_cast<sem::DoEndBlock>(std::move(body));
  140. auto wb = std::make_unique<sem::WhileBlock>(std::move(condExpr), std::move(bodyblock));
  141. return wb;
  142. }
  143. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::Expression& ast, sem::Scope& scope)
  144. {
  145. throw "visit(Expression) shouldn't be called";
  146. }
  147. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::FeatureCall& ast, sem::Scope& scope)
  148. {
  149. std::unique_ptr<sem::Expression> target = nullptr;
  150. if (ast.target) {
  151. target = unique_dynamic_cast<sem::Expression>(
  152. ast.target->accept(*this, scope));
  153. }
  154. sem::Method* method;
  155. sem::Variable* var;
  156. if (target) {
  157. auto& targetType = scope.getContext().getType(target->type);
  158. auto& typeScope = targetType.getTypeScope();
  159. method = typeScope.getMethod(ast.name);
  160. var = typeScope.getVariable(ast.name);
  161. }
  162. else {
  163. method = scope.getMethod(ast.name);
  164. var = scope.getVariable(ast.name);
  165. }
  166. if (target) {
  167. if (var) {
  168. return std::make_unique<sem::FieldAccessExpression>(std::move(target), dynamic_cast<sem::Field*>(var));
  169. }
  170. else if (method) {
  171. auto fce = std::make_unique<sem::MethodCallExpression>(
  172. std::move(target), method);
  173. if (ast.arguments.size() != method->arguments.size())
  174. throw SemanticError(SemanticError::WRONG_NUMBER_OF_ARGUMENTS, ast.name, ast.pos);
  175. for (size_t i = 0; i < ast.arguments.size(); i++) {
  176. auto& arg = ast.arguments[i];
  177. auto& argTypeShouldHave = method->arguments[i]->type;
  178. auto argument = arg->accept(*this, scope);
  179. if (sem::Expression* expr =
  180. dynamic_cast<sem::Expression*>(argument.get()); expr) {
  181. if (expr->type != argTypeShouldHave)
  182. throw SemanticError(SemanticError::TYPE_MISMATCH,
  183. "argument passed to function has wrong type",
  184. // TODO rewrite types
  185. //expr->type->asString() + "' instead of '" +
  186. //argTypeShouldHave->asString() + "'",
  187. arg->pos
  188. );
  189. fce->arguments.push_back(
  190. unique_dynamic_cast<sem::Expression>(std::move(argument)));
  191. }
  192. else {
  193. throw "internal error: non-expression passed as function parameter";
  194. }
  195. }
  196. return fce;
  197. }
  198. else {
  199. throw SemanticError(SemanticError::FEATURE_NOT_FOUND, ast.name, ast.pos);
  200. }
  201. }
  202. else if (var) {
  203. if (sem::Field* field = dynamic_cast<sem::Field*>(var); field) {
  204. auto* thisExpr = scope.getVariable("this");
  205. if (!thisExpr)
  206. throw "no this found";
  207. return std::make_unique<sem::FieldAccessExpression>(std::make_unique<sem::LocalVariableExpression>(thisExpr), field);
  208. }
  209. else {
  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. sem::Context& context = scope.getContext();
  317. auto leftEval = unique_dynamic_cast<sem::Expression>(ast.left->accept(*this, scope));
  318. auto rightEval = unique_dynamic_cast<sem::Expression>(ast.right->accept(*this, scope));
  319. auto& leftType = context.getType(leftEval->type);
  320. auto& rightType = context.getType(rightEval->type);
  321. auto& scop = leftType.getTypeScope();
  322. sem::Method* operationMethod = scop.resolveMethod(
  323. ast.opString, { rightEval->type }
  324. );
  325. #ifdef DEBUGGING
  326. Printer::getInstance() << "looked for operation method for operator " <<
  327. ast.opString << std::endl;
  328. #endif
  329. if (!operationMethod) {
  330. throw SemanticError(SemanticError::OPERATOR_NOT_FOUND,
  331. "operator " + ast.opString + " not found for types '" +
  332. leftType.asString() + "' and '" + rightType.asString() + "'",
  333. ast.opPos);
  334. }
  335. auto ret = std::make_unique<sem::BinaryOperation>(context, leftEval->type, &ast);
  336. ret->operationMethod = operationMethod;
  337. ret->opString = ast.opString;
  338. ret->left = std::move(leftEval);
  339. ret->right = std::move(rightEval);
  340. return ret;
  341. }
  342. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::NewExpression& ast, sem::Scope& scope)
  343. {
  344. auto ret = std::make_unique<sem::NewExpression>(scope.getContext(), scope.getType(ast.type.get()));
  345. return ret;
  346. //return nullptr;
  347. }
  348. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::NewArrayExpression& ast, sem::Scope& scope)
  349. {
  350. auto ret = std::make_unique<sem::NewArrayExpression>(scope.getContext(), scope.getType(ast.type.get()));
  351. return ret;
  352. }
  353. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::CastExpression& ast, sem::Scope& scope)
  354. {
  355. auto expr = unique_dynamic_cast<sem::Expression>(ast.expression->accept(*this, scope));
  356. auto type = scope.getType(ast.targetType.get());
  357. return std::make_unique<sem::CastExpression>(
  358. std::move(expr), type, &ast);
  359. }