From 78c58d3ce9f74e203dca8792ace7f9b3c420c313 Mon Sep 17 00:00:00 2001 From: Marc Auberer Date: Mon, 6 May 2024 01:09:36 +0200 Subject: [PATCH] Remove implicit conversion between Type and QualType (#536) --- src/irgenerator/GenValues.cpp | 4 ++-- src/symboltablebuilder/QualType.cpp | 3 +-- src/symboltablebuilder/QualType.h | 3 +-- src/symboltablebuilder/Type.cpp | 19 +++++++++---------- src/symboltablebuilder/Type.h | 4 +--- src/typechecker/OpRuleManager.cpp | 2 +- src/typechecker/TypeChecker.cpp | 2 +- 7 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/irgenerator/GenValues.cpp b/src/irgenerator/GenValues.cpp index fd6ea546d..0053fab34 100644 --- a/src/irgenerator/GenValues.cpp +++ b/src/irgenerator/GenValues.cpp @@ -146,8 +146,8 @@ std::any IRGenerator::visitFctCall(const FctCallNode *node) { const QualType &expectedSTy = paramSTypes.at(i); const QualType &actualSTy = argNode->getEvaluatedSymbolType(manIdx); - const auto matchFct = [](const Type &lhsTy, const Type &rhsTy) { - return lhsTy.matches(rhsTy, false, true, true) || lhsTy.matchesInterfaceImplementedByStruct(rhsTy); + const auto matchFct = [](const QualType &lhsTy, const QualType &rhsTy) { + return lhsTy.matches(rhsTy, false, true, true) || lhsTy.getType().matchesInterfaceImplementedByStruct(rhsTy.getType()); }; // If the arrays are both of size -1 or 0, they are both pointers and do not need to be cast implicitly diff --git a/src/symboltablebuilder/QualType.cpp b/src/symboltablebuilder/QualType.cpp index 0149268d4..3c47ec45d 100644 --- a/src/symboltablebuilder/QualType.cpp +++ b/src/symboltablebuilder/QualType.cpp @@ -8,11 +8,10 @@ namespace spice::compiler { -QualType::QualType(Type t) : type(std::make_unique(std::move(t))), specifiers(TypeSpecifiers::of(t.getSuperType())) {} QualType::QualType(SuperType superType) : type(std::make_unique(superType)), specifiers(TypeSpecifiers::of(superType)) {} QualType::QualType(SuperType superType, const std::string &subType) : type(std::make_unique(superType, subType)), specifiers(TypeSpecifiers::of(superType)) {} -QualType::QualType(Type t, TypeSpecifiers specifiers) : type(std::make_unique(std::move(t))), specifiers(specifiers) {} +QualType::QualType(const Type &t, TypeSpecifiers specifiers) : type(std::make_unique(t)), specifiers(specifiers) {} // ToDo: Delete those two later on QualType::QualType(const QualType &other) { diff --git a/src/symboltablebuilder/QualType.h b/src/symboltablebuilder/QualType.h index 7a081914b..c2c007142 100644 --- a/src/symboltablebuilder/QualType.h +++ b/src/symboltablebuilder/QualType.h @@ -23,10 +23,9 @@ class QualType { public: // Constructors QualType() = default; - [[deprecated]] /*ToDo: explicit*/ QualType(Type type); explicit QualType(SuperType superType); QualType(SuperType superType, const std::string &subType); - QualType(Type type, TypeSpecifiers specifiers); + QualType(const Type &type, TypeSpecifiers specifiers); // ToDo: Remove those later on QualType(const QualType &other); diff --git a/src/symboltablebuilder/Type.cpp b/src/symboltablebuilder/Type.cpp index 66e3fe549..b949a085f 100644 --- a/src/symboltablebuilder/Type.cpp +++ b/src/symboltablebuilder/Type.cpp @@ -20,8 +20,6 @@ namespace spice::compiler { Type::Type(SuperType superType) : typeChain({TypeChainElement{superType}}) {} -Type::Type(const QualType &qualType) : typeChain(qualType.getType().typeChain) {} - Type::Type(SuperType superType, const std::string &subType) : typeChain({TypeChainElement{superType, subType}}) {} Type::Type(SuperType superType, const std::string &subType, uint64_t typeId, const Type::TypeChainElementData &data, @@ -209,7 +207,7 @@ const Type *Type::replaceBase(const Type &newBaseType) const { // Create new type Type newType = *this; - TypeChain &newTypeChain = newType.typeChain; + TypeChain newTypeChain = newBaseType.typeChain; const bool doubleRef = newTypeChain.back().superType == TY_REF && typeChain.back().superType == TY_REF; for (size_t i = 1; i < typeChain.size(); i++) if (!doubleRef || i > 1) @@ -381,9 +379,9 @@ bool Type::implements(const Type &symbolType, const ASTNode *node) const { assert(is(TY_STRUCT) && symbolType.is(TY_INTERFACE)); Struct *spiceStruct = getStruct(node); assert(spiceStruct != nullptr); - return std::ranges::any_of(spiceStruct->interfaceTypes, [&](const Type &interfaceType) { + return std::ranges::any_of(spiceStruct->interfaceTypes, [&](const QualType &interfaceType) { assert(interfaceType.is(TY_INTERFACE)); - return symbolType.matches(interfaceType, false, false, true); + return symbolType.matches(interfaceType.getType(), false, false, true); }); } @@ -436,13 +434,13 @@ bool Type::hasAnyGenericParts() const { // NOLINT(misc-no-recursion) // Check if the type has generic template types const auto templateTypes = baseType.getTemplateTypes(); - if (std::ranges::any_of(templateTypes, [](const Type &t) { return t.hasAnyGenericParts(); })) + if (std::ranges::any_of(templateTypes, [](const QualType &t) { return t.hasAnyGenericParts(); })) return true; // Check param and return types or functions/procedures if (baseType.isOneOf({TY_FUNCTION, TY_PROCEDURE})) { const auto paramTypes = baseType.getFunctionParamAndReturnTypes(); - if (std::ranges::any_of(paramTypes, [](const Type &t) { return t.hasAnyGenericParts(); })) + if (std::ranges::any_of(paramTypes, [](const QualType &t) { return t.hasAnyGenericParts(); })) return true; } @@ -501,8 +499,9 @@ bool Type::isCoveredByGenericTypeList(std::vector &genericTypeList) // If function/procedure, check param and return types if (baseType.isOneOf({TY_FUNCTION, TY_PROCEDURE})) { const std::vector ¶mAndReturnTypes = baseType.getFunctionParamAndReturnTypes(); - covered &= std::ranges::all_of(paramAndReturnTypes, - [&](const Type ¶mType) { return paramType.isCoveredByGenericTypeList(genericTypeList); }); + covered &= std::ranges::all_of(paramAndReturnTypes, [&](const QualType ¶mType) { + return paramType.getType().isCoveredByGenericTypeList(genericTypeList); + }); } return covered; @@ -704,7 +703,7 @@ bool Type::matchesInterfaceImplementedByStruct(const Type &otherType) const { // Check if the rhs is a struct type that implements the lhs interface type const Struct *spiceStruct = otherType.getStruct(nullptr); assert(spiceStruct != nullptr); - const auto pred = [&](const Type &interfaceType) { return matches(interfaceType, false, false, true); }; + const auto pred = [&](const QualType &interfaceType) { return matches(interfaceType.getType(), false, false, true); }; return std::ranges::any_of(spiceStruct->interfaceTypes, pred); } diff --git a/src/symboltablebuilder/Type.h b/src/symboltablebuilder/Type.h index abda1cf68..ea2e84f5d 100644 --- a/src/symboltablebuilder/Type.h +++ b/src/symboltablebuilder/Type.h @@ -101,13 +101,11 @@ class Type { using TypeChain = std::vector; // Constructors - Type() = default; explicit Type(SuperType superType); - [[deprecated]] Type(const QualType &qualType); // ToDo: Remove + explicit Type(TypeChain types); Type(SuperType superType, const std::string &subType); Type(SuperType superType, const std::string &subType, uint64_t typeId, const TypeChainElementData &data, const std::vector &templateTypes); - explicit Type(TypeChain types); // Public methods [[nodiscard]] [[deprecated]] Type toPointer(const ASTNode *node) const; diff --git a/src/typechecker/OpRuleManager.cpp b/src/typechecker/OpRuleManager.cpp index 3646df851..9108e1abc 100644 --- a/src/typechecker/OpRuleManager.cpp +++ b/src/typechecker/OpRuleManager.cpp @@ -116,7 +116,7 @@ QualType OpRuleManager::getAssignResultTypeCommon(const ASTNode *node, const Exp QualType lhsTypeCopy = lhsType; QualType rhsTypeCopy = rhsType; QualType::unwrapBoth(lhsTypeCopy, rhsTypeCopy); - if (lhsTypeCopy.getType().matchesInterfaceImplementedByStruct(rhsTypeCopy)) + if (lhsTypeCopy.getType().matchesInterfaceImplementedByStruct(rhsTypeCopy.getType())) return lhsType; } diff --git a/src/typechecker/TypeChecker.cpp b/src/typechecker/TypeChecker.cpp index 1e05caddb..4ccc64ccd 100644 --- a/src/typechecker/TypeChecker.cpp +++ b/src/typechecker/TypeChecker.cpp @@ -1228,7 +1228,7 @@ std::any TypeChecker::visitCastExpr(CastExprNode *node) { // Get result type QualType resultType = opRuleManager.getCastResultType(node, dstType, src); - SymbolTableEntry *entry = src.type.getType().isSameContainerTypeAs(dstType) ? src.entry : nullptr; + SymbolTableEntry *entry = src.type.isSameContainerTypeAs(dstType) ? src.entry : nullptr; return ExprResult{node->setEvaluatedSymbolType(resultType, manIdx), entry}; }