123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- #ifndef QLOW_SEM_SCOPE_H
- #define QLOW_SEM_SCOPE_H
- #include <optional>
- #include <map>
- #include <memory>
- #include <llvm/IR/Type.h>
- #include <llvm/IR/Value.h>
- #include "Util.h"
- #include "Type.h"
- #include "Context.h"
- namespace qlow
- {
- namespace ast
- {
- struct Type;
- }
-
- namespace sem
- {
- /*!
- * \note contains owning pointers to elements
- */
- template<typename T>
- using SymbolTable = std::map<std::string, std::unique_ptr<T>>;
- struct Class;
- struct Method;
- struct Variable;
- class Scope;
- class GlobalScope;
- class NativeScope;
- class ClassScope;
- class LocalScope;
- class TypeScope;
- class NativeTypeScope;
-
- class Type;
- struct NativeMethod;
- }
- }
- class qlow::sem::Scope
- {
- protected:
- Context& context;
- public:
- inline Scope(Context& context) :
- context{ context } {}
- Scope(Scope&&) = default;
- Scope& operator=(Scope&&) = default;
- virtual ~Scope(void);
- virtual Variable* getVariable(const std::string& name) = 0;
- virtual Method* getMethod(const std::string& name) = 0;
- virtual TypeId getType(const ast::Type* name) = 0;
- virtual TypeId getReturnableType(void) = 0;
- virtual Method* resolveMethod(const std::string& name,
- const std::vector<TypeId> argumentTypes);
- virtual std::string toString(void) = 0;
- inline Context& getContext(void) { return context; }
- };
- class qlow::sem::GlobalScope : public Scope
- {
- public:
- SymbolTable<Class> classes;
- SymbolTable<Method> functions;
- //OwningList<Cast> casts;
- public:
- inline GlobalScope(Context& context) :
- Scope{ context } {}
- virtual Variable* getVariable(const std::string& name);
- virtual Method* getMethod(const std::string& name);
- virtual TypeId getType(const ast::Type* name);
- virtual TypeId getReturnableType(void);
- inline const SymbolTable<Class>& getClasses(void) const { return classes; }
- inline const SymbolTable<Method>& getMethods(void) const { return functions; }
- virtual std::string toString(void);
- };
- class qlow::sem::NativeScope : public GlobalScope
- {
- protected:
- std::unordered_map<std::string, TypeId> types;
- std::map<Type::Native, TypeId> typesByNative;
- public:
- inline NativeScope(Context& context) :
- GlobalScope{ context } {}
- virtual TypeId getType(const ast::Type* name);
- virtual TypeId getType(Type::Native nt);
- virtual void addNativeType(std::string name, Type::Native nt, TypeId id);
- virtual std::string toString(void);
- };
- class qlow::sem::ClassScope : public Scope
- {
- Scope& parentScope;
- Class* classRef;
- public:
- inline ClassScope(Scope& parentScope, Class* classRef) :
- Scope{ parentScope.getContext() },
- parentScope{ parentScope }, classRef{ classRef }
- {
- }
- virtual Variable* getVariable(const std::string& name);
- virtual Method* getMethod(const std::string& name);
- virtual TypeId getType(const ast::Type* name);
- virtual TypeId getReturnableType(void);
- virtual std::string toString(void);
- };
- class qlow::sem::LocalScope : public Scope
- {
- Scope& parentScope;
- SymbolTable<Variable> localVariables;
- TypeId returnType;
- Method* enclosingMethod;
- public:
- LocalScope(Scope& parentScope, Method* enclosingMethod);
- LocalScope(LocalScope& parentScope);
- void putVariable(const std::string& name, std::unique_ptr<Variable> v);
- SymbolTable<Variable>& getLocals(void);
- virtual Variable* getVariable(const std::string& name);
- virtual Method* getMethod(const std::string& name);
- virtual TypeId getType(const ast::Type* name);
- virtual TypeId getReturnableType(void);
- virtual std::string toString(void);
- };
- class qlow::sem::TypeScope : public Scope
- {
- protected:
- TypeId type;
- public:
- inline TypeScope(Context& context, TypeId type) :
- Scope{ context },
- type{ type }
- {
- }
- TypeScope(TypeScope&&) = default;
- TypeScope& operator=(TypeScope&&) = default;
-
- virtual Variable* getVariable(const std::string& name);
- virtual Method* getMethod(const std::string& name);
- virtual TypeId getType(const ast::Type* name);
- virtual TypeId getReturnableType(void);
- virtual std::string toString(void);
- virtual bool isNativeTypeScope(void) const;
- };
- class qlow::sem::NativeTypeScope : public TypeScope
- {
- public:
- SymbolTable<NativeMethod> nativeMethods;
- inline NativeTypeScope(Context& context, TypeId type) :
- TypeScope{ context, type }
- {
- }
- NativeTypeScope(NativeTypeScope&&);
- //NativeTypeScope& operator=(NativeTypeScope&&);
- ~NativeTypeScope(void);
-
- virtual Method* getMethod(const std::string& name);
- virtual bool isNativeTypeScope(void) const;
- TypeId implementInlineOperation(const std::string&, llvm::Value* a);
- };
- #endif // QLOW_SEM_SCOPE_H
|