Browse Source

rewriting types still

Nicolas Winkler 6 years ago
parent
commit
3c27a0922e
6 changed files with 112 additions and 57 deletions
  1. 3 3
      src/Scope.cpp
  2. 12 12
      src/Scope.h
  3. 12 0
      src/sem/Context.cpp
  4. 65 34
      src/sem/Semantic.h
  5. 6 0
      src/sem/Type.cpp
  6. 14 8
      src/sem/Type.h

+ 3 - 3
src/Scope.cpp

@@ -12,7 +12,7 @@ sem::Scope::~Scope(void)
 
 
 sem::Method* sem::Scope::resolveMethod(const std::string& name,
-    const std::vector<std::shared_ptr<Type>> argumentTypes)
+    const std::vector<TypeId> argumentTypes)
 {
     sem::Method* m = getMethod(name);
     if (!m)
@@ -166,13 +166,13 @@ std::string sem::ClassScope::toString(void)
 }
 
 
-std::shared_ptr<sem::Type> sem::ClassScope::getType(const ast::Type& name)
+sem::TypeId sem::ClassScope::getType(const ast::Type& name)
 {
     return parentScope.getType(name);
 }
 
 
-std::shared_ptr<sem::Type> sem::ClassScope::getReturnableType(void)
+sem::TypeId sem::ClassScope::getReturnableType(void)
 {
     return nullptr;
 }

+ 12 - 12
src/Scope.h

@@ -55,7 +55,7 @@ public:
     virtual TypeId getType(const ast::Type& name) = 0;
     virtual TypeId getReturnableType(void) = 0;
     virtual Method* resolveMethod(const std::string& name,
-        const std::vector<std::shared_ptr<Type>> argumentTypes);
+        const std::vector<TypeId> argumentTypes);
 
     virtual std::string toString(void) = 0;
 };
@@ -72,8 +72,8 @@ public:
 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 TypeId getType(const ast::Type& name);
+    virtual TypeId getReturnableType(void);
 
     virtual std::string toString(void);
 };
@@ -85,7 +85,7 @@ class qlow::sem::NativeScope : public GlobalScope
 public:
     SymbolTable<std::shared_ptr<NativeType>> types;
 public:
-    virtual std::shared_ptr<Type> getType(const ast::Type& name);
+    virtual TypeId getType(const ast::Type& name);
 
     virtual std::string toString(void);
     
@@ -105,8 +105,8 @@ 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 TypeId getType(const ast::Type& name);
+    virtual TypeId getReturnableType(void);
     virtual std::string toString(void);
 };
 
@@ -115,7 +115,7 @@ class qlow::sem::LocalScope : public Scope
 {
     Scope& parentScope;
     SymbolTable<Variable> localVariables;
-    std::shared_ptr<Type> returnType;
+    TypeId returnType;
     Method* enclosingMethod;
 public:
     LocalScope(Scope& parentScope, Method* enclosingMethod);
@@ -126,8 +126,8 @@ 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 TypeId getType(const ast::Type& name);
+    virtual TypeId getReturnableType(void);
     virtual std::string toString(void);
 };
 
@@ -144,8 +144,8 @@ 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 TypeId getType(const ast::Type& name);
+    virtual TypeId getReturnableType(void);
     virtual std::string toString(void);
 };
 
@@ -162,7 +162,7 @@ public:
     
     
     virtual Method* getMethod(const std::string& name);
-    std::shared_ptr<Type> implementInlineOperation(const std::string&, llvm::Value* a);
+    TypeId implementInlineOperation(const std::string&, llvm::Value* a);
 };
 
 

+ 12 - 0
src/sem/Context.cpp

@@ -30,3 +30,15 @@ std::optional<std::reference_wrapper<qlow::sem::Type>> Context::getType(TypeId t
     }
 }
 
+
+qlow::sem::TypeId Context::getPointerTo(TypeId id)
+{
+    return addType(Type::createPointerType(*this, id));
+}
+
+
+qlow::sem::TypeId Context::getArrayOf(TypeId id)
+{
+    return addType(Type::createArrayType(*this, id));
+}
+

+ 65 - 34
src/sem/Semantic.h

@@ -9,6 +9,7 @@
 #include "Scope.h"
 
 #include "Type.h"
+#include "Context.h"
 
 #include <llvm/IR/Value.h>
 #include <llvm/IR/IRBuilder.h>
@@ -75,19 +76,25 @@ struct qlow::sem::Class : public SemanticObject
     SymbolTable<Field> fields;
     SymbolTable<Method> methods;
     ClassScope scope;
+    TypeId classType;
 
     /// \brief generated during llvm code generation, not availab
     llvm::Type* llvmType;
 
-    inline Class(qlow::ast::Class* astNode, GlobalScope& globalScope) :
+    inline Class(Context& context, qlow::ast::Class* astNode,
+            GlobalScope& globalScope) :
+        SemanticObject{ context },
         astNode{ astNode },
         name{ astNode->name },
         scope{ globalScope, this },
+        classType{ context.addType(Type::createClassType(context, this)) },
         llvmType{ nullptr }
     {
     }
 
-    inline Class(const std::string& nativeName, GlobalScope& globalScope) :
+    inline Class(Context& context, const std::string& nativeName,
+            GlobalScope& globalScope) :
+        SemanticObject{ context },
         astNode{ nullptr },
         name{ nativeName },
         scope{ globalScope, this },
@@ -110,7 +117,8 @@ struct qlow::sem::Variable : public SemanticObject
     llvm::Value* allocaInst;
     
     Variable(void) = default;
-    inline Variable(TypeId type, const std::string& name) :
+    inline Variable(Context& context, TypeId type, const std::string& name) :
+        SemanticObject{ context },
         type{ type },
         name{ name },
         allocaInst { nullptr }
@@ -130,7 +138,7 @@ struct qlow::sem::Field : public Variable
 
 struct qlow::sem::Method : public SemanticObject
 {
-    Class* containingType;
+    Class* containingClass;
     TypeId returnType;
     std::vector<Variable*> arguments;
     std::string name;
@@ -142,25 +150,28 @@ struct qlow::sem::Method : public SemanticObject
 
     llvm::Function* llvmNode;
 
-    inline Method(Scope& parentScope, TypeId returnType) :
-        containingType{ nullptr },
+    inline Method(Context& context, Scope& parentScope,
+            TypeId returnType) :
+        SemanticObject{ context },
+        containingClass{ nullptr },
         returnType{ returnType },
-        scope{ parentScope, this },
         thisExpression{ nullptr },
-        body{ nullptr }
+        body{ nullptr },
+        scope{ parentScope, this }
     {
     }
     
+    /*
     inline Method(ast::MethodDefinition* astNode, Scope& parentScope) :
         containingType{ nullptr },
-        returnType{ Type{ parentScope.returnType.getContext() }},
+        returnType{ parentScope.getReturnableType()},
         astNode{ astNode },
         name{ astNode->name },
         scope{ parentScope, this },
         thisExpression{ nullptr },
         body{ nullptr }
     {
-    }
+    }*/
     
     void generateThisExpression(void);
     
@@ -172,7 +183,13 @@ struct qlow::sem::ThisExpression : public Variable
 {
     Method* method;
     inline ThisExpression(Method* method) :
-        Variable{ method->containingType.toPointer(), "this" },
+        Variable{
+            method->context,
+            method->context.addType(Type::createPointerType(method->context,
+                        method->context.addType(Type::createClassType(method->context,
+                            method->containingClass)))),
+            "this"
+        },
         method{ method }
     {
     }
@@ -183,6 +200,8 @@ struct qlow::sem::ThisExpression : public Variable
 
 struct qlow::sem::Statement : public SemanticObject, public Visitable<llvm::Value*, gen::FunctionGenerator, qlow::StatementVisitor>
 {
+    inline Statement(Context& context) :
+        SemanticObject{ context } {}
     virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) = 0;
 };
 
@@ -192,7 +211,8 @@ struct qlow::sem::DoEndBlock : public Statement
     LocalScope scope;
     OwningList<Statement> statements;
 
-    inline DoEndBlock(LocalScope& parentScope) :
+    inline DoEndBlock(Context& context, LocalScope& parentScope) :
+        Statement{ context },
         scope{ parentScope } {}
     
     virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) override;
@@ -207,6 +227,7 @@ struct qlow::sem::IfElseBlock : public Statement
     inline IfElseBlock(std::unique_ptr<Expression> condition,
                        std::unique_ptr<DoEndBlock> ifBlock,
                        std::unique_ptr<DoEndBlock> elseBlock) :
+        Statement{ ifBlock->context },
         condition{ std::move(condition) },
         ifBlock{ std::move(ifBlock) },
         elseBlock{ std::move(elseBlock) }
@@ -222,7 +243,8 @@ struct qlow::sem::WhileBlock : public Statement
     std::unique_ptr<Expression> condition;
     std::unique_ptr<DoEndBlock> body;
     inline WhileBlock(std::unique_ptr<Expression> condition,
-                       std::unique_ptr<DoEndBlock> body) :
+                      std::unique_ptr<DoEndBlock> body) :
+        Statement{ body->context },
         condition{ std::move(condition) },
         body{ std::move(body) }
     {
@@ -237,6 +259,9 @@ struct qlow::sem::AssignmentStatement : public Statement
     std::unique_ptr<Expression> target;
     std::unique_ptr<Expression> value;
 
+    inline AssignmentStatement(Context& context) :
+        Statement{ context } {}
+
     virtual std::string toString(void) const override;
     virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) override;
 };
@@ -246,6 +271,9 @@ struct qlow::sem::ReturnStatement : public Statement
 {
     std::unique_ptr<Expression> value;
 
+    inline ReturnStatement(Context& context) :
+        Statement{ context } {}
+
     virtual std::string toString(void) const override;
     virtual llvm::Value* accept(qlow::StatementVisitor&, gen::FunctionGenerator&) override;
 };
@@ -262,7 +290,8 @@ struct qlow::sem::Expression :
 {
     TypeId type;
     
-    inline Expression(TypeId type) :
+    inline Expression(Context& context, TypeId type) :
+        SemanticObject{ context },
         type{ type }
     {
     }
@@ -278,8 +307,8 @@ struct qlow::sem::Operation : public Expression
 {
     std::string opString;
     
-    inline Operation(TypeId type) :
-        Expression{ type }
+    inline Operation(Context& context, TypeId type) :
+        Expression{ context, type }
     {
     }
 };
@@ -290,7 +319,7 @@ struct qlow::sem::LocalVariableExpression : public Expression
     Variable* var;
     
     inline LocalVariableExpression(Variable* var) :
-        Expression{ var->type },
+        Expression{ var->context, var->type },
         var{ var }
     {
     }
@@ -308,7 +337,7 @@ struct qlow::sem::AddressExpression : public Expression
     std::unique_ptr<sem::Expression> target;
     
     inline AddressExpression(std::unique_ptr<sem::Expression> target) :
-        Expression{ target->type.toPointer() },
+        Expression{ target->context, context.getPointerTo(target->type) },
         target{ std::move(target) }
     {
     }
@@ -326,8 +355,9 @@ struct qlow::sem::BinaryOperation : public Operation
     /// method that is called to execute the operator
     sem::Method* operationMethod;
     
-    inline BinaryOperation(TypeId type, ast::BinaryOperation* astNode) :
-        Operation{ type },
+    inline BinaryOperation(Context& context,
+            TypeId type, ast::BinaryOperation* astNode) :
+        Operation{ context, type },
         astNode{ astNode }
     {
     }
@@ -348,7 +378,7 @@ struct qlow::sem::CastExpression : public Expression
     inline CastExpression(std::unique_ptr<Expression> expression,
                           TypeId type,
                           ast::CastExpression* astNode) :
-        Expression{ type },
+        Expression{ expression->context, type },
         expression{ std::move(expression) },
         targetType{ type },
         astNode{ astNode }
@@ -366,8 +396,8 @@ struct qlow::sem::NewArrayExpression : public Expression
     TypeId arrayType;
     std::unique_ptr<Expression> length;
     
-    inline NewArrayExpression(TypeId arrayType) :
-        Expression{ arrayType.toArray() },
+    inline NewArrayExpression(Context& context, TypeId arrayType) :
+        Expression{ context, context.getArrayOf(arrayType) },
         arrayType{ arrayType }
     {
     }
@@ -382,8 +412,8 @@ struct qlow::sem::UnaryOperation : public Operation
     qlow::ast::UnaryOperation::Side side;
     std::unique_ptr<Expression> arg;
     
-    inline UnaryOperation(TypeId type) :
-        Operation{ type }
+    inline UnaryOperation(Context& context, TypeId type) :
+        Operation{ context, type }
     {
     }
     
@@ -399,10 +429,10 @@ struct qlow::sem::MethodCallExpression : public Expression
     OwningList<Expression> arguments;
     
     inline MethodCallExpression(std::unique_ptr<Expression> target,
-                                 Method* callee) :
-        Expression{ callee->returnType },
-        target{ std::move(target) },
-        callee{ callee }
+                                Method* callee) :
+        Expression{ target->context, callee->returnType },
+        callee{ callee },
+        target{ std::move(target) }
     {
     }
     
@@ -420,9 +450,9 @@ struct qlow::sem::FieldAccessExpression : public Expression
     
     inline FieldAccessExpression(std::unique_ptr<Expression> target,
                                  Field* accessed ) :
-        Expression{ accessed->type },
-        target{ std::move(target) },
-        accessed{ accessed }
+        Expression{ target->context, accessed->type },
+        accessed{ accessed },
+        target{ std::move(target) }
     {
     }
     
@@ -439,8 +469,8 @@ struct qlow::sem::IntConst : public Expression
 {
     unsigned long long value;
 
-    inline IntConst(unsigned long long value) :
-        Expression{ std::make_shared<NativeType>(NativeType::INTEGER) },
+    inline IntConst(Context& context, unsigned long long value) :
+        Expression{ context.addType(/* integer */) },
         value{ value }
     {
     }
@@ -453,6 +483,7 @@ struct qlow::sem::FeatureCallStatement : public Statement
 {
     std::unique_ptr<MethodCallExpression> expr;
     inline FeatureCallStatement(std::unique_ptr<MethodCallExpression> expr) :
+        Statement{ expr->context },
         expr{ std::move(expr) } {}
 
     virtual std::string toString(void) const override;

+ 6 - 0
src/sem/Type.cpp

@@ -6,6 +6,7 @@ using qlow::sem::TypeId;
 using qlow::sem::Type;
 
 
+/*
 TypeId TypeId::toPointer(void) const
 {
     return context.addType(Type::createPointerType(context, *this));
@@ -17,6 +18,7 @@ TypeId TypeId::toArray(void) const
     return context.addType(Type::createArrayType(context, *this));
 }
 
+*/
 
 Type::Kind Type::getKind(void) const
 {
@@ -60,6 +62,10 @@ bool Type::operator==(const Type& other) const
            this->type == other.type;
 }
 
+Type Type::createClassType(Context& c, Class* classType)
+{
+    return Type{ Union{ ClassType{ classType }}};
+}
 
 Type Type::createPointerType(Context& c, TypeId pointsTo)
 {

+ 14 - 8
src/sem/Type.h

@@ -10,16 +10,19 @@ namespace qlow::sem
     struct SemanticObject;
     class Type;
     
-    struct TypeId;
+    using TypeId = size_t;
 
     // forward declarations to other files
     struct Class;
-    struct Context;
+    class Context;
 }
 
 
 struct qlow::sem::SemanticObject
 {
+    Context& context;
+    inline SemanticObject(Context& context) :
+        context{ context } {}
     virtual ~SemanticObject(void);
     
     /**
@@ -29,6 +32,7 @@ struct qlow::sem::SemanticObject
 };
 
 
+/*
 class qlow::sem::TypeId
 {
     Context& context;
@@ -46,6 +50,7 @@ public:
     TypeId toPointer(void) const;
     TypeId toArray(void) const;
 };
+*/
 
 
 class qlow::sem::Type
@@ -93,6 +98,7 @@ public:
 
     bool operator == (const Type& other) const;
 
+    static Type createClassType(Context& c, Class* classType);
     static Type createPointerType(Context& c, TypeId pointsTo);
     static Type createArrayType(Context& c, TypeId pointsTo);
 };
@@ -162,24 +168,24 @@ public:
 
     virtual size_t hash(void) const;
     
-//    static std::shared_ptr<Type> VOID;
-//    static std::shared_ptr<Type> INTEGER;
-//    static std::shared_ptr<Type> BOOLEAN;
+//    static TypeId VOID;
+//    static TypeId INTEGER;
+//    static TypeId BOOLEAN;
 };
 
 
 class qlow::sem::PointerType : public Type
 {
-    std::shared_ptr<Type> derefType;
+    TypeId derefType;
     sem::TypeScope scope;
 public:
-    inline PointerType(std::shared_ptr<Type> derefType) :
+    inline PointerType(TypeId derefType) :
         derefType{ derefType },
         scope{ *this }
     {
     }
     
-    const std::shared_ptr<Type>& getDerefType(void) const { return derefType; }
+    const TypeId& getDerefType(void) const { return derefType; }
     
     inline bool isPointerType(void) const override { return true; }