Selaa lähdekoodia

rewrote type class

QlowB 6 vuotta sitten
vanhempi
commit
91641b5f24
4 muutettua tiedostoa jossa 136 lisäystä ja 16 poistoa
  1. 2 2
      src/sem/Context.h
  2. 1 1
      src/sem/Semantic.h
  3. 49 1
      src/sem/Type.cpp
  4. 84 12
      src/sem/Type.h

+ 2 - 2
src/sem/Context.h

@@ -6,12 +6,12 @@
 #include <vector>
 #include <optional>
 
+#include "Type.h"
+
 namespace qlow::sem
 {
     class Type;
     class Context;
-    
-    using TypeId = size_t;
 }
 
 namespace std

+ 1 - 1
src/sem/Semantic.h

@@ -101,7 +101,7 @@ struct qlow::sem::Class : public SemanticObject
 
 struct qlow::sem::Variable : public SemanticObject
 {
-    std::shared_ptr<Type> type;
+    TypeId type;
     std::string name;
     bool isParameter;
 

+ 49 - 1
src/sem/Type.cpp

@@ -1,5 +1,53 @@
 #include "Type.h"
 
+#include "Context.h"
+
+using qlow::sem::Type;
+
+Type::Kind Type::getKind(void) const
+{
+    return static_cast<Kind>(type.index());
+}
+
+
+std::string Type::asString(void) const
+{
+    return std::visit(
+        [&] (const auto& t) -> std::string {
+            using T = std::decay_t<decltype(t)>;
+            if constexpr (std::is_same<T, NativeType>) {
+                return this->name;
+            }
+            else if constexpr (std::is_same<T, ClassType>) {
+                return this->name;
+            }
+            else if constexpr (std::is_same<T, PointerType>) {
+                auto& context = t.targetType.getContext();
+                return context.getType(t.targetType) + "*";
+            }
+            else if constexpr (std::is_same<T, ArrayType>) {
+                auto& context = t.targetType.getContext();
+                return "[" + context.getType(t.targetType) + "]";
+            }
+        }
+        ,
+        type
+    );
+}
+
+bool Type::operator==(const Type& other) const
+{
+    return this->name == other.name &&
+           this->kind == other.kind &&
+           this->type == other.type;
+}
+
+
+
+
+
+
+#if 0
 #include "Semantic.h"
 #include "Builtin.h"
 
@@ -257,4 +305,4 @@ llvm::Value* sem::NativeType::generateImplicitCast(llvm::Value* value)
 {
     // TODO implement
 }
-
+#endif

+ 84 - 12
src/sem/Type.h

@@ -1,6 +1,89 @@
 #ifndef QLOW_SEM_TYPE_H
 #define QLOW_SEM_TYPE_H
 
+#include <variant>
+#include <string>
+
+namespace qlow::sem
+{
+    struct SemanticObject;
+    class Type;
+    
+    struct TypeId;
+
+    // forward declarations to other files
+    struct Class;
+    struct Context;
+}
+
+
+struct qlow::sem::SemanticObject
+{
+    virtual ~SemanticObject(void);
+    
+    /**
+     * \brief converts the object to a readable string for debugging purposes. 
+     */
+    virtual std::string toString(void) const;
+};
+
+
+class qlow::sem::TypeId
+{
+    Context& context;
+    size_t id;
+public:
+    inline TypeId(Context& context, size_t id) :
+        context{ context }, id{ id } {}
+
+    inline Context& getContext(void) const { return context; }
+    inline size_t getId(void) const { return id; }
+};
+
+
+class qlow::sem::Type
+{
+public:
+    enum class Kind
+    {
+        NATIVE,
+        CLASS,
+        POINTER,
+        ARRAY
+    };
+private:
+    std::string name;
+
+    struct NativeType
+    {
+    };
+
+    struct ClassType
+    {
+        Class* classType;
+    };
+
+    struct PointerType
+    {
+        TypeId targetType;
+    };
+
+    struct ArrayType 
+    {
+        TypeId targetType;
+    };
+
+    std::variant<NativeType, ClassType, PointerType, ArrayType> type;
+public:
+    Kind getKind(void) const;
+    std::string asString(void) const;
+    size_t hash(void) const;
+
+    bool operator == (const Type& other) const;
+};
+
+ 
+#if 0
 #include "Scope.h"
 
 #include <memory>
@@ -38,18 +121,6 @@ namespace qlow
     }
 }
 
-
-struct qlow::sem::SemanticObject
-{
-    virtual ~SemanticObject(void);
-    
-    /**
-     * \brief converts the object to a readable string for debugging purposes. 
-     */
-    virtual std::string toString(void) const;
-};
-
-
 class qlow::sem::Type : public SemanticObject
 {
 public:
@@ -190,5 +261,6 @@ public:
     llvm::Value* generateImplicitCast(llvm::Value* value);
 };
 
+#endif
 
 #endif // QLOW_SEM_TYPE_H