ソースを参照

variant problem

Nicolas Winkler 6 年 前
コミット
399a5b4e69
3 ファイル変更36 行追加10 行削除
  1. 8 1
      src/sem/Context.cpp
  2. 12 7
      src/sem/Type.cpp
  3. 16 2
      src/sem/Type.h

+ 8 - 1
src/sem/Context.cpp

@@ -65,7 +65,14 @@ qlow::sem::TypeId Context::getNativeTypeId(Type::Native n)
 
 std::unique_ptr<qlow::sem::TypeScope> Context::getTypeScope(TypeId id)
 {
-    return std::make_unique<TypeScope>(*this, id);
+    if (auto type = getType(id)) {
+        if (type.getKind() == Type::Kind::NATIVE) {
+            return std::make_unique<NativeTypeScope>();
+        }
+        else {
+            return std::make_unique<TypeScope>(*this, id);
+        }
+    }
 }
 
 

+ 12 - 7
src/sem/Type.cpp

@@ -1,5 +1,5 @@
 #include "Type.h"
-
+#include "Scope.h"
 #include "Context.h"
 #include "ErrorReporting.h"
 
@@ -21,6 +21,10 @@ TypeId TypeId::toArray(void) const
 
 */
 
+Type::NativeType::~NativeType(void)
+{
+}
+
 Type::Kind Type::getKind(void) const
 {
     switch(type.index()) {
@@ -77,12 +81,13 @@ bool Type::operator==(const Type& other) const
 
 qlow::sem::Class* Type::getClass(void) const
 {
-    try {
-        return std::get<ClassType>(type).classType;
-    }
-    catch(std::bad_variant_access& bva) {
-        return nullptr;
-    }
+    return std::get_if<ClassType>(&type).classType;
+}
+
+
+qlow::sem::NativeTypeScope* Type::getNativeTypeScope(void) const
+{
+    return std::get_if<NativeType>(&type).typeScope;
 }
 
 

+ 16 - 2
src/sem/Type.h

@@ -21,6 +21,8 @@ namespace qlow::sem
 
     // forward declarations to other files
     struct Class;
+    class NativeTypeScope;
+
     class Context;
 }
 
@@ -85,6 +87,8 @@ private:
     struct NativeType
     {
         Native type;
+        std::unique_ptr<NativeTypeScope> typeScope;
+        ~NativeType(void);
         inline bool operator==(const NativeType& other) const { return type == other.type; }
     };
 
@@ -110,10 +114,10 @@ private:
     Union type;
 
     inline Type(Union type) :
-        type{ type } {}
+        type{ std::move(type) } {}
 
     inline Type(Union type, std::string name) :
-        name{ std::move(name) }, type{ type } {}
+        name{ std::move(name) }, type{ std::move(type) } {}
 
 public:
     Kind getKind(void) const;
@@ -122,7 +126,17 @@ public:
 
     bool operator == (const Type& other) const;
 
+    /**
+     * @brief return the class of this type if it is a class type,
+     *        <code>nullptr</code> otherwise.
+     */
     Class* getClass(void) const;
+    
+    /**
+     * @brief returns the type scope of this type if the type
+     *        is native, <code>nullptr</code> otherwise.
+     */
+    NativeTypeScope* getNativeTypeScope(void) const;
 
     llvm::Type* getLLVMType(llvm::LLVMContext* context);