Ast.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. // =============================================================================
  2. //
  3. // This file is part of the qlow compiler.
  4. //
  5. // Copyright (C) 2014-2015 Nicolas Winkler
  6. //
  7. // This program is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation, either version 3 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. //
  20. // =============================================================================
  21. #ifndef QLOW_AST_H
  22. #define QLOW_AST_H
  23. #include <string>
  24. #include <vector>
  25. #include <memory>
  26. #include <utility>
  27. #include <map>
  28. #include "Visitor.h"
  29. #include "Util.h"
  30. #include "ErrorReporting.h"
  31. namespace qlow
  32. {
  33. struct CodePosition;
  34. class StructureVisitor;
  35. namespace ast
  36. {
  37. class Ast;
  38. // base class
  39. struct AstObject;
  40. struct Class;
  41. struct Type;
  42. struct ClassType;
  43. struct ArrayType;
  44. struct PointerType;
  45. struct FeatureDeclaration;
  46. struct FieldDeclaration;
  47. struct MethodDefinition;
  48. struct VariableDeclaration;
  49. struct ArgumentDeclaration;
  50. struct Statement;
  51. struct DoEndBlock;
  52. struct IfElseBlock;
  53. struct WhileBlock;
  54. struct Expression;
  55. struct FeatureCall;
  56. struct AssignmentStatement;
  57. struct ReturnStatement;
  58. struct LocalVariableStatement;
  59. struct AddressExpression;
  60. struct IntConst;
  61. struct Operation;
  62. struct UnaryOperation;
  63. struct BinaryOperation;
  64. struct NewArrayExpression;
  65. struct CastExpression;
  66. }
  67. namespace sem
  68. {
  69. struct SemanticObject;
  70. struct Class;
  71. class Scope;
  72. // template<typename T>
  73. // using SymbolTable = std::map<std::string, std::unique_ptr<T>>;
  74. }
  75. }
  76. class qlow::ast::Ast
  77. {
  78. OwningList<AstObject> objects;
  79. public:
  80. inline const OwningList<AstObject>& getObjects(void) const { return objects; }
  81. inline OwningList<AstObject>& getObjects(void) { return objects; }
  82. void merge(Ast&& other);
  83. };
  84. struct qlow::ast::AstObject :
  85. public Visitable<std::unique_ptr<sem::SemanticObject>, sem::Scope&, StructureVisitor>
  86. {
  87. CodePosition pos;
  88. inline AstObject(const CodePosition& cp) :
  89. pos{ cp } {}
  90. virtual ~AstObject(void);
  91. };
  92. struct qlow::ast::Class : public AstObject
  93. {
  94. std::string name;
  95. OwningList<FeatureDeclaration> features;
  96. inline Class(const std::string& name, OwningList<FeatureDeclaration>& features, const CodePosition& cp) :
  97. AstObject{ cp },
  98. name{ name }, features(std::move(features))
  99. {
  100. }
  101. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&) override;
  102. };
  103. struct qlow::ast::Type : public AstObject
  104. {
  105. inline Type(const CodePosition& cp) :
  106. AstObject{ cp }
  107. {
  108. }
  109. virtual std::string asString(void) const = 0;
  110. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor&, sem::Scope&) override;
  111. };
  112. struct qlow::ast::ClassType : public ast::Type
  113. {
  114. std::string typeName;
  115. inline ClassType(const std::string& typeName, const CodePosition& cp) :
  116. Type{ cp },
  117. typeName{ typeName }
  118. {
  119. }
  120. inline ClassType(std::string&& typeName, const CodePosition& cp) :
  121. Type{ cp },
  122. typeName{ std::move(typeName) }
  123. {
  124. }
  125. inline std::string asString(void) const override { return typeName; }
  126. };
  127. struct qlow::ast::ArrayType : public ast::Type
  128. {
  129. std::unique_ptr<ast::Type> arrayType;
  130. inline ArrayType(std::unique_ptr<ast::Type> arrayType, const CodePosition& cp) :
  131. Type{ cp },
  132. arrayType{ std::move(arrayType) }
  133. {
  134. }
  135. inline std::string asString(void) const override {
  136. return std::string("[") + arrayType->asString() + "]";
  137. }
  138. };
  139. struct qlow::ast::PointerType : public ast::Type
  140. {
  141. std::unique_ptr<ast::Type> derefType;
  142. inline PointerType(std::unique_ptr<ast::Type> derefType, const CodePosition& cp) :
  143. Type{ cp },
  144. derefType{ std::move(derefType) }
  145. {
  146. }
  147. inline std::string asString(void) const override {
  148. return derefType->asString() + "*";
  149. }
  150. };
  151. struct qlow::ast::FeatureDeclaration : public AstObject
  152. {
  153. std::string name;
  154. std::unique_ptr<ast::Type> type;
  155. inline FeatureDeclaration(std::unique_ptr<ast::Type> type, const std::string& name, const CodePosition& cp) :
  156. AstObject{ cp },
  157. name{ name },
  158. type{ std::move(type) }
  159. {
  160. }
  161. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  162. };
  163. struct qlow::ast::FieldDeclaration : public FeatureDeclaration
  164. {
  165. inline FieldDeclaration(std::unique_ptr<ast::Type> type, const std::string& name, const CodePosition& cp) :
  166. FeatureDeclaration{ std::move(type), name, cp }
  167. {
  168. }
  169. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  170. };
  171. struct qlow::ast::MethodDefinition : public FeatureDeclaration
  172. {
  173. OwningList<ArgumentDeclaration> arguments;
  174. /// pointer to method body. If this is a null pointer, the method has only
  175. /// been declared and not defined (with extern)
  176. std::unique_ptr<DoEndBlock> body;
  177. inline MethodDefinition(std::unique_ptr<ast::Type> type, const std::string& name,
  178. std::unique_ptr<DoEndBlock> body, const CodePosition& cp) :
  179. FeatureDeclaration{ std::move(type), name, cp },
  180. body{ std::move(body) }
  181. {
  182. }
  183. inline MethodDefinition(std::unique_ptr<ast::Type> type, const std::string& name,
  184. OwningList<ArgumentDeclaration>&& arguments, std::unique_ptr<DoEndBlock> body, const CodePosition& cp) :
  185. FeatureDeclaration{ std::move(type), name, cp },
  186. arguments(std::move(arguments)),
  187. body{ std::move(body) }
  188. {
  189. }
  190. inline MethodDefinition(std::unique_ptr<ast::Type> type, const std::string& name,
  191. const CodePosition& cp) :
  192. FeatureDeclaration{ std::move(type), name, cp },
  193. body{ nullptr }
  194. {
  195. }
  196. inline MethodDefinition(std::unique_ptr<ast::Type> type, const std::string& name,
  197. OwningList<ArgumentDeclaration>&& arguments, const CodePosition& cp) :
  198. FeatureDeclaration{ std::move(type), name, cp },
  199. arguments(std::move(arguments)),
  200. body{ nullptr }
  201. {
  202. }
  203. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  204. };
  205. struct qlow::ast::VariableDeclaration : public AstObject
  206. {
  207. std::unique_ptr<ast::Type> type;
  208. std::string name;
  209. inline VariableDeclaration(std::unique_ptr<ast::Type> type, std::string&& name, const CodePosition& cp) :
  210. AstObject{ cp },
  211. type{ std::move(type) },
  212. name{ std::move(name) }
  213. {
  214. }
  215. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  216. };
  217. struct qlow::ast::ArgumentDeclaration :
  218. public VariableDeclaration
  219. {
  220. inline ArgumentDeclaration(std::unique_ptr<ast::Type> type, std::string&& name, const CodePosition& cp) :
  221. VariableDeclaration{ std::move(type), std::move(name), cp }
  222. {
  223. }
  224. //virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  225. };
  226. struct qlow::ast::Statement : public virtual AstObject
  227. {
  228. inline Statement(const CodePosition& cp) :
  229. AstObject{ cp } {}
  230. virtual ~Statement(void);
  231. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  232. };
  233. struct qlow::ast::DoEndBlock : public Statement
  234. {
  235. OwningList<Statement> statements;
  236. inline DoEndBlock(OwningList<Statement>&& statements, const CodePosition& cp) :
  237. AstObject{ cp },
  238. Statement{ cp },
  239. statements(std::move(statements))
  240. {
  241. }
  242. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  243. };
  244. struct qlow::ast::IfElseBlock : public Statement
  245. {
  246. std::unique_ptr<Expression> condition;
  247. std::unique_ptr<DoEndBlock> ifBlock;
  248. std::unique_ptr<DoEndBlock> elseBlock;
  249. inline IfElseBlock(std::unique_ptr<Expression> condition,
  250. std::unique_ptr<DoEndBlock> ifBlock,
  251. std::unique_ptr<DoEndBlock> elseBlock,
  252. const CodePosition& cp) :
  253. AstObject{ cp },
  254. Statement{ cp },
  255. condition{ std::move(condition) },
  256. ifBlock{ std::move(ifBlock) },
  257. elseBlock{ std::move(elseBlock) }
  258. {
  259. }
  260. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  261. };
  262. struct qlow::ast::WhileBlock : public Statement
  263. {
  264. std::unique_ptr<Expression> condition;
  265. std::unique_ptr<DoEndBlock> body;
  266. inline WhileBlock(std::unique_ptr<Expression> condition,
  267. std::unique_ptr<DoEndBlock> body,
  268. const CodePosition& cp) :
  269. AstObject{ cp },
  270. Statement{ cp },
  271. condition{ std::move(condition) },
  272. body{ std::move(body) }
  273. {
  274. }
  275. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  276. };
  277. struct qlow::ast::Expression : public virtual AstObject
  278. {
  279. inline Expression(const CodePosition& cp) :
  280. AstObject{ cp } {}
  281. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  282. };
  283. struct qlow::ast::FeatureCall : public Expression, public Statement
  284. {
  285. std::unique_ptr<Expression> target;
  286. std::string name;
  287. OwningList<Expression> arguments;
  288. inline FeatureCall(std::unique_ptr<Expression> target, const std::string& name, const CodePosition& cp) :
  289. AstObject{ cp },
  290. Expression{ cp }, Statement{ cp },
  291. target(std::move(target)), name(name)
  292. {
  293. }
  294. inline FeatureCall(std::unique_ptr<Expression> target, const std::string& name,
  295. OwningList<Expression>&& arguments, const CodePosition& cp) :
  296. AstObject{ cp },
  297. Expression{ cp }, Statement{ cp },
  298. target(std::move(target)), name(name), arguments(std::move(arguments))
  299. {
  300. }
  301. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  302. };
  303. struct qlow::ast::ReturnStatement : public Statement
  304. {
  305. std::unique_ptr<Expression> expr;
  306. inline ReturnStatement(std::unique_ptr<Expression>&& expr, const CodePosition& cp) :
  307. AstObject{ cp },
  308. Statement{ cp },
  309. expr{ std::move(expr) }
  310. {
  311. }
  312. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  313. };
  314. struct qlow::ast::AssignmentStatement : public Statement
  315. {
  316. std::unique_ptr<Expression> target;
  317. std::unique_ptr<Expression> expr;
  318. inline AssignmentStatement(std::unique_ptr<Expression>&& target, std::unique_ptr<Expression>&& expr, const CodePosition& cp) :
  319. AstObject{ cp },
  320. Statement{ cp },
  321. target{ std::move(target) }, expr{ std::move(expr) }
  322. {
  323. }
  324. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  325. };
  326. struct qlow::ast::LocalVariableStatement : public Statement
  327. {
  328. std::string name;
  329. std::unique_ptr<ast::Type> type;
  330. inline LocalVariableStatement(std::string&& name, std::unique_ptr<Type> type, const CodePosition& cp) :
  331. AstObject{ cp },
  332. Statement{ cp },
  333. name{ name },
  334. type{ std::move(type) }
  335. {
  336. }
  337. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  338. };
  339. struct qlow::ast::AddressExpression : public Expression
  340. {
  341. std::unique_ptr<Expression> target;
  342. inline AddressExpression(std::unique_ptr<Expression> target,
  343. const CodePosition& cp) :
  344. AstObject{ cp },
  345. Expression{ cp },
  346. target{ std::move(target) }
  347. {
  348. }
  349. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  350. };
  351. struct qlow::ast::IntConst : public Expression
  352. {
  353. unsigned long long value;
  354. IntConst(unsigned long long v, const CodePosition& p) :
  355. AstObject(p),
  356. Expression(p),
  357. value{ v } {}
  358. IntConst(std::string&& val, const CodePosition& p);
  359. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  360. };
  361. struct qlow::ast::Operation : public Expression
  362. {
  363. std::string opString;
  364. CodePosition opPos;
  365. inline Operation(const std::string& opString, const CodePosition& cp,
  366. const CodePosition& opPos) :
  367. AstObject{ cp },
  368. Expression{ cp },
  369. opString{ opString },
  370. opPos{ opPos }
  371. {
  372. }
  373. };
  374. struct qlow::ast::UnaryOperation : public Operation
  375. {
  376. enum Side
  377. {
  378. PREFIX,
  379. SUFFIX,
  380. };
  381. Side side;
  382. std::unique_ptr<Expression> expr;
  383. inline UnaryOperation(std::unique_ptr<Expression> expr, Side side,
  384. const std::string& op, const CodePosition& cp,
  385. const CodePosition& opPos
  386. ) :
  387. AstObject{ cp },
  388. Operation{ op, cp, opPos },
  389. side{ side },
  390. expr{ std::move(expr) }
  391. {
  392. }
  393. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  394. };
  395. struct qlow::ast::BinaryOperation : public Operation
  396. {
  397. std::unique_ptr<Expression> left;
  398. std::unique_ptr<Expression> right;
  399. inline BinaryOperation(std::unique_ptr<Expression> left,
  400. std::unique_ptr<Expression> right,
  401. const std::string& op,
  402. const CodePosition& cp,
  403. const CodePosition& opPos
  404. ) :
  405. AstObject{ cp },
  406. Operation{ op, cp, opPos },
  407. left{ std::move(left) },
  408. right{ std::move(right) }
  409. {
  410. }
  411. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  412. };
  413. struct qlow::ast::NewArrayExpression : public Expression
  414. {
  415. std::unique_ptr<ast::Type> type;
  416. std::unique_ptr<Expression> length;
  417. inline NewArrayExpression(std::unique_ptr<ast::Type> type,
  418. std::unique_ptr<Expression> length,
  419. const CodePosition& cp) :
  420. AstObject{ cp },
  421. Expression{ cp },
  422. type{ std::move(type) },
  423. length{ std::move(length) }
  424. {
  425. }
  426. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  427. };
  428. struct qlow::ast::CastExpression : public Expression
  429. {
  430. std::unique_ptr<ast::Expression> expression;
  431. std::unique_ptr<ast::Type> targetType;
  432. inline CastExpression(std::unique_ptr<ast::Expression> expression,
  433. std::unique_ptr<ast::Type> targetType,
  434. const CodePosition& cp) :
  435. AstObject{ cp },
  436. Expression{ cp },
  437. expression{ std::move(expression) },
  438. targetType{ std::move(targetType) }
  439. {
  440. }
  441. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  442. };
  443. #endif // QLOW_AST_H