Scope.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #include "Type.h"
  10. #include "Context.h"
  11. namespace qlow
  12. {
  13. namespace ast
  14. {
  15. struct Type;
  16. }
  17. namespace sem
  18. {
  19. /*!
  20. * \note contains owning pointers to elements
  21. */
  22. template<typename T>
  23. using SymbolTable = std::map<std::string, std::unique_ptr<T>>;
  24. struct Class;
  25. struct Method;
  26. struct Variable;
  27. class Scope;
  28. class GlobalScope;
  29. class NativeScope;
  30. class ClassScope;
  31. class LocalScope;
  32. class TypeScope;
  33. class NativeTypeScope;
  34. class Type;
  35. struct NativeMethod;
  36. }
  37. }
  38. class qlow::sem::Scope
  39. {
  40. protected:
  41. Context& context;
  42. public:
  43. inline Scope(Context& context) :
  44. context{ context } {}
  45. Scope(Scope&&) = default;
  46. Scope& operator=(Scope&&) = default;
  47. virtual ~Scope(void);
  48. virtual Variable* getVariable(const std::string& name) = 0;
  49. virtual Method* getMethod(const std::string& name) = 0;
  50. virtual TypeId getType(const ast::Type* name) = 0;
  51. virtual TypeId getReturnableType(void) = 0;
  52. virtual Method* resolveMethod(const std::string& name,
  53. const std::vector<TypeId> argumentTypes);
  54. virtual std::string toString(void) = 0;
  55. inline Context& getContext(void) { return context; }
  56. };
  57. class qlow::sem::GlobalScope : public Scope
  58. {
  59. public:
  60. SymbolTable<Class> classes;
  61. SymbolTable<Method> functions;
  62. //OwningList<Cast> casts;
  63. public:
  64. inline GlobalScope(Context& context) :
  65. Scope{ context } {}
  66. virtual Variable* getVariable(const std::string& name);
  67. virtual Method* getMethod(const std::string& name);
  68. virtual TypeId getType(const ast::Type* name);
  69. virtual TypeId getReturnableType(void);
  70. inline const SymbolTable<Class>& getClasses(void) const { return classes; }
  71. inline const SymbolTable<Method>& getMethods(void) const { return functions; }
  72. virtual std::string toString(void);
  73. };
  74. class qlow::sem::NativeScope : public GlobalScope
  75. {
  76. protected:
  77. std::unordered_map<std::string, TypeId> types;
  78. std::map<Type::Native, TypeId> typesByNative;
  79. public:
  80. inline NativeScope(Context& context) :
  81. GlobalScope{ context } {}
  82. virtual TypeId getType(const ast::Type* name);
  83. virtual TypeId getType(Type::Native nt);
  84. virtual void addNativeType(std::string name, Type::Native nt, TypeId id);
  85. virtual std::string toString(void);
  86. };
  87. class qlow::sem::ClassScope : public Scope
  88. {
  89. Scope& parentScope;
  90. Class* classRef;
  91. public:
  92. inline ClassScope(Scope& parentScope, Class* classRef) :
  93. Scope{ parentScope.getContext() },
  94. parentScope{ parentScope }, classRef{ classRef }
  95. {
  96. }
  97. virtual Variable* getVariable(const std::string& name);
  98. virtual Method* getMethod(const std::string& name);
  99. virtual TypeId getType(const ast::Type* name);
  100. virtual TypeId getReturnableType(void);
  101. virtual std::string toString(void);
  102. };
  103. class qlow::sem::LocalScope : public Scope
  104. {
  105. Scope& parentScope;
  106. SymbolTable<Variable> localVariables;
  107. TypeId returnType;
  108. Method* enclosingMethod;
  109. public:
  110. LocalScope(Scope& parentScope, Method* enclosingMethod);
  111. LocalScope(LocalScope& parentScope);
  112. void putVariable(const std::string& name, std::unique_ptr<Variable> v);
  113. SymbolTable<Variable>& getLocals(void);
  114. virtual Variable* getVariable(const std::string& name);
  115. virtual Method* getMethod(const std::string& name);
  116. virtual TypeId getType(const ast::Type* name);
  117. virtual TypeId getReturnableType(void);
  118. virtual std::string toString(void);
  119. };
  120. class qlow::sem::TypeScope : public Scope
  121. {
  122. protected:
  123. TypeId type;
  124. public:
  125. inline TypeScope(Context& context, TypeId type) :
  126. Scope{ context },
  127. type{ type }
  128. {
  129. }
  130. TypeScope(TypeScope&&) = default;
  131. TypeScope& operator=(TypeScope&&) = default;
  132. virtual Variable* getVariable(const std::string& name);
  133. virtual Method* getMethod(const std::string& name);
  134. virtual TypeId getType(const ast::Type* name);
  135. virtual TypeId getReturnableType(void);
  136. virtual std::string toString(void);
  137. virtual bool isNativeTypeScope(void) const;
  138. };
  139. class qlow::sem::NativeTypeScope : public TypeScope
  140. {
  141. public:
  142. SymbolTable<NativeMethod> nativeMethods;
  143. inline NativeTypeScope(Context& context, TypeId type) :
  144. TypeScope{ context, type }
  145. {
  146. }
  147. NativeTypeScope(NativeTypeScope&&);
  148. //NativeTypeScope& operator=(NativeTypeScope&&);
  149. ~NativeTypeScope(void);
  150. virtual Method* getMethod(const std::string& name);
  151. virtual bool isNativeTypeScope(void) const;
  152. TypeId implementInlineOperation(const std::string&, llvm::Value* a);
  153. };
  154. #endif // QLOW_SEM_SCOPE_H