Scope.h 2.8 KB

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