Scope.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #ifndef QLOW_SEM_SCOPE_H
  2. #define QLOW_SEM_SCOPE_H
  3. #include <optional>
  4. #include <map>
  5. #include <memory>
  6. #include <llvm/IR/Type.h>
  7. #include <llvm/IR/Value.h>
  8. #include "Util.h"
  9. namespace qlow
  10. {
  11. namespace ast
  12. {
  13. struct Type;
  14. }
  15. namespace sem
  16. {
  17. /*!
  18. * \note contains owning pointers to elements
  19. */
  20. template<typename T>
  21. using SymbolTable = std::map<std::string, std::unique_ptr<T>>;
  22. struct Class;
  23. struct Method;
  24. struct Variable;
  25. class Cast;
  26. class Scope;
  27. class GlobalScope;
  28. class NativeScope;
  29. class ClassScope;
  30. class LocalScope;
  31. class TypeScope;
  32. class NativeTypeScope;
  33. class Type;
  34. class NativeType;
  35. }
  36. }
  37. class qlow::sem::Scope
  38. {
  39. public:
  40. virtual ~Scope(void);
  41. virtual Variable* getVariable(const std::string& name) = 0;
  42. virtual Method* getMethod(const std::string& name) = 0;
  43. virtual std::shared_ptr<Type> getType(const ast::Type& name) = 0;
  44. virtual std::shared_ptr<Type> getReturnableType(void) = 0;
  45. virtual Method* resolveMethod(const std::string& name,
  46. const std::vector<std::shared_ptr<Type>> argumentTypes);
  47. virtual std::string toString(void) = 0;
  48. };
  49. class qlow::sem::GlobalScope : public Scope
  50. {
  51. public:
  52. SymbolTable<Class> classes;
  53. SymbolTable<Method> functions;
  54. OwningList<Cast> casts;
  55. public:
  56. virtual Variable* getVariable(const std::string& name);
  57. virtual Method* getMethod(const std::string& name);
  58. virtual std::shared_ptr<Type> getType(const ast::Type& name);
  59. virtual std::shared_ptr<Type> getReturnableType(void);
  60. virtual std::string toString(void);
  61. };
  62. class qlow::sem::NativeScope : public GlobalScope
  63. {
  64. static NativeScope instance;
  65. public:
  66. SymbolTable<std::shared_ptr<NativeType>> types;
  67. public:
  68. virtual std::shared_ptr<Type> getType(const ast::Type& name);
  69. virtual std::string toString(void);
  70. static NativeScope& getInstance(void);
  71. };
  72. class qlow::sem::ClassScope : public Scope
  73. {
  74. Scope& parentScope;
  75. Class* classRef;
  76. public:
  77. inline ClassScope(Scope& parentScope, Class* classRef) :
  78. parentScope{ parentScope }, classRef{ classRef }
  79. {
  80. }
  81. virtual Variable* getVariable(const std::string& name);
  82. virtual Method* getMethod(const std::string& name);
  83. virtual std::shared_ptr<Type> getType(const ast::Type& name);
  84. virtual std::shared_ptr<Type> getReturnableType(void);
  85. virtual std::string toString(void);
  86. };
  87. class qlow::sem::LocalScope : public Scope
  88. {
  89. Scope& parentScope;
  90. SymbolTable<Variable> localVariables;
  91. std::shared_ptr<Type> returnType;
  92. Method* enclosingMethod;
  93. public:
  94. LocalScope(Scope& parentScope, Method* enclosingMethod);
  95. LocalScope(LocalScope& parentScope);
  96. void putVariable(const std::string& name, std::unique_ptr<Variable> v);
  97. SymbolTable<Variable>& getLocals(void);
  98. virtual Variable* getVariable(const std::string& name);
  99. virtual Method* getMethod(const std::string& name);
  100. virtual std::shared_ptr<Type> getType(const ast::Type& name);
  101. virtual std::shared_ptr<Type> getReturnableType(void);
  102. virtual std::string toString(void);
  103. };
  104. class qlow::sem::TypeScope : public Scope
  105. {
  106. protected:
  107. Type& type;
  108. public:
  109. inline TypeScope(Type& type) :
  110. type{ type }
  111. {
  112. }
  113. virtual Variable* getVariable(const std::string& name);
  114. virtual Method* getMethod(const std::string& name);
  115. virtual std::shared_ptr<Type> getType(const ast::Type& name);
  116. virtual std::shared_ptr<Type> getReturnableType(void);
  117. virtual std::string toString(void);
  118. };
  119. class qlow::sem::NativeTypeScope : public TypeScope
  120. {
  121. NativeType& nativeType;
  122. public:
  123. inline NativeTypeScope(NativeType& type) :
  124. TypeScope{ (Type&) type },
  125. nativeType{ type }
  126. {
  127. }
  128. virtual Method* getMethod(const std::string& name);
  129. std::shared_ptr<Type> implementInlineOperation(const std::string&, llvm::Value* a);
  130. };
  131. #endif // QLOW_SEM_SCOPE_H