123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- #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"
- 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 Cast;
- class Scope;
- class GlobalScope;
- class NativeScope;
- class ClassScope;
- class LocalScope;
- class TypeScope;
- class NativeTypeScope;
-
- class Type;
- class NativeType;
- }
- }
- class qlow::sem::Scope
- {
- public:
- virtual ~Scope(void);
- virtual Variable* getVariable(const std::string& name) = 0;
- virtual Method* getMethod(const std::string& name) = 0;
- virtual std::shared_ptr<Type> getType(const ast::Type& name) = 0;
- virtual std::shared_ptr<Type> getReturnableType(void) = 0;
- virtual Method* resolveMethod(const std::string& name,
- const std::vector<std::shared_ptr<Type>> argumentTypes);
- virtual std::string toString(void) = 0;
- };
- class qlow::sem::GlobalScope : public Scope
- {
- public:
- SymbolTable<Class> classes;
- SymbolTable<Method> functions;
- OwningList<Cast> casts;
- public:
- virtual Variable* getVariable(const std::string& name);
- virtual Method* getMethod(const std::string& name);
- virtual std::shared_ptr<Type> getType(const ast::Type& name);
- virtual std::shared_ptr<Type> getReturnableType(void);
- virtual std::string toString(void);
- };
- class qlow::sem::NativeScope : public GlobalScope
- {
- static NativeScope instance;
- public:
- SymbolTable<std::shared_ptr<NativeType>> types;
- public:
- virtual std::shared_ptr<Type> getType(const ast::Type& name);
- virtual std::string toString(void);
-
- static NativeScope& getInstance(void);
- };
- class qlow::sem::ClassScope : public Scope
- {
- Scope& parentScope;
- Class* classRef;
- public:
- inline ClassScope(Scope& parentScope, Class* classRef) :
- parentScope{ parentScope }, classRef{ classRef }
- {
- }
- virtual Variable* getVariable(const std::string& name);
- virtual Method* getMethod(const std::string& name);
- virtual std::shared_ptr<Type> getType(const ast::Type& name);
- virtual std::shared_ptr<Type> getReturnableType(void);
- virtual std::string toString(void);
- };
- class qlow::sem::LocalScope : public Scope
- {
- Scope& parentScope;
- SymbolTable<Variable> localVariables;
- std::shared_ptr<Type> 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 std::shared_ptr<Type> getType(const ast::Type& name);
- virtual std::shared_ptr<Type> getReturnableType(void);
- virtual std::string toString(void);
- };
- class qlow::sem::TypeScope : public Scope
- {
- protected:
- Type& type;
- public:
- inline TypeScope(Type& type) :
- type{ type }
- {
- }
-
-
- virtual Variable* getVariable(const std::string& name);
- virtual Method* getMethod(const std::string& name);
- virtual std::shared_ptr<Type> getType(const ast::Type& name);
- virtual std::shared_ptr<Type> getReturnableType(void);
- virtual std::string toString(void);
- };
- class qlow::sem::NativeTypeScope : public TypeScope
- {
- NativeType& nativeType;
- public:
- inline NativeTypeScope(NativeType& type) :
- TypeScope{ (Type&) type },
- nativeType{ type }
- {
- }
-
-
- virtual Method* getMethod(const std::string& name);
- std::shared_ptr<Type> implementInlineOperation(const std::string&, llvm::Value* a);
- };
- #endif // QLOW_SEM_SCOPE_H
|