Scope.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #ifndef QLOW_SEM_SCOPE_H
  2. #define QLOW_SEM_SCOPE_H
  3. #include <optional>
  4. #include <map>
  5. #include <memory>
  6. namespace qlow
  7. {
  8. namespace ast
  9. {
  10. struct Type;
  11. }
  12. namespace sem
  13. {
  14. /*!
  15. * \note contains owning pointers to elements
  16. */
  17. template<typename T>
  18. using SymbolTable = std::map<std::string, std::unique_ptr<T>>;
  19. struct Class;
  20. struct Method;
  21. struct Variable;
  22. class Scope;
  23. class GlobalScope;
  24. class NativeScope;
  25. class ClassScope;
  26. class LocalScope;
  27. class Type;
  28. class NativeType;
  29. }
  30. }
  31. class qlow::sem::Scope
  32. {
  33. public:
  34. virtual ~Scope(void);
  35. virtual Variable* getVariable(const std::string& name) = 0;
  36. virtual Method* getMethod(const std::string& name) = 0;
  37. virtual std::shared_ptr<Type> getType(const ast::Type& name) = 0;
  38. virtual std::shared_ptr<Type> getReturnableType(void) = 0;
  39. virtual std::string toString(void) = 0;
  40. };
  41. class qlow::sem::GlobalScope : public Scope
  42. {
  43. public:
  44. SymbolTable<Class> classes;
  45. SymbolTable<Method> functions;
  46. public:
  47. virtual Variable* getVariable(const std::string& name);
  48. virtual Method* getMethod(const std::string& name);
  49. virtual std::shared_ptr<Type> getType(const ast::Type& name);
  50. virtual std::shared_ptr<Type> getReturnableType(void);
  51. virtual std::string toString(void);
  52. };
  53. class qlow::sem::NativeScope : public GlobalScope
  54. {
  55. static NativeScope instance;
  56. public:
  57. SymbolTable<std::shared_ptr<NativeType>> types;
  58. public:
  59. virtual std::shared_ptr<Type> getType(const ast::Type& name);
  60. virtual std::string toString(void);
  61. static NativeScope& getInstance(void);
  62. };
  63. class qlow::sem::ClassScope : public Scope
  64. {
  65. Scope& parentScope;
  66. Class* class_ref;
  67. public:
  68. inline ClassScope(Scope& parentScope, Class* class_ref) :
  69. parentScope{ parentScope }, class_ref{ class_ref }
  70. {
  71. }
  72. virtual Variable* getVariable(const std::string& name);
  73. virtual Method* getMethod(const std::string& name);
  74. virtual std::shared_ptr<Type> getType(const ast::Type& name);
  75. virtual std::shared_ptr<Type> getReturnableType(void);
  76. virtual std::string toString(void);
  77. };
  78. class qlow::sem::LocalScope : public Scope
  79. {
  80. Scope& parentScope;
  81. SymbolTable<Variable> localVariables;
  82. std::shared_ptr<Type> returnType;
  83. public:
  84. inline LocalScope(Scope& parentScope, Type* returnType) :
  85. parentScope{ parentScope },
  86. returnType{ returnType }
  87. {
  88. }
  89. inline LocalScope(Scope& parentScope) :
  90. parentScope{ parentScope }
  91. {
  92. auto returnable = parentScope.getReturnableType();
  93. if (returnable) {
  94. returnType = std::move(returnable);
  95. }
  96. else {
  97. returnType = nullptr;
  98. }
  99. }
  100. void putVariable(const std::string& name, std::unique_ptr<Variable> v);
  101. SymbolTable<Variable>& getLocals(void);
  102. virtual Variable* getVariable(const std::string& name);
  103. virtual Method* getMethod(const std::string& name);
  104. virtual std::shared_ptr<Type> getType(const ast::Type& name);
  105. virtual std::shared_ptr<Type> getReturnableType(void);
  106. virtual std::string toString(void);
  107. };
  108. #endif // QLOW_SEM_SCOPE_H