Skip to content

Commit

Permalink
Remove implicit conversion between Type and QualType (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer committed May 5, 2024
1 parent 532968f commit 78c58d3
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/irgenerator/GenValues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/symboltablebuilder/QualType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

namespace spice::compiler {

QualType::QualType(Type t) : type(std::make_unique<Type>(std::move(t))), specifiers(TypeSpecifiers::of(t.getSuperType())) {}
QualType::QualType(SuperType superType) : type(std::make_unique<Type>(superType)), specifiers(TypeSpecifiers::of(superType)) {}
QualType::QualType(SuperType superType, const std::string &subType)
: type(std::make_unique<Type>(superType, subType)), specifiers(TypeSpecifiers::of(superType)) {}
QualType::QualType(Type t, TypeSpecifiers specifiers) : type(std::make_unique<Type>(std::move(t))), specifiers(specifiers) {}
QualType::QualType(const Type &t, TypeSpecifiers specifiers) : type(std::make_unique<Type>(t)), specifiers(specifiers) {}

// ToDo: Delete those two later on
QualType::QualType(const QualType &other) {
Expand Down
3 changes: 1 addition & 2 deletions src/symboltablebuilder/QualType.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 9 additions & 10 deletions src/symboltablebuilder/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
});
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -501,8 +499,9 @@ bool Type::isCoveredByGenericTypeList(std::vector<GenericType> &genericTypeList)
// If function/procedure, check param and return types
if (baseType.isOneOf({TY_FUNCTION, TY_PROCEDURE})) {
const std::vector<QualType> &paramAndReturnTypes = baseType.getFunctionParamAndReturnTypes();
covered &= std::ranges::all_of(paramAndReturnTypes,
[&](const Type &paramType) { return paramType.isCoveredByGenericTypeList(genericTypeList); });
covered &= std::ranges::all_of(paramAndReturnTypes, [&](const QualType &paramType) {
return paramType.getType().isCoveredByGenericTypeList(genericTypeList);
});
}

return covered;
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 1 addition & 3 deletions src/symboltablebuilder/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,11 @@ class Type {
using TypeChain = std::vector<TypeChainElement>;

// 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<QualType> &templateTypes);
explicit Type(TypeChain types);

// Public methods
[[nodiscard]] [[deprecated]] Type toPointer(const ASTNode *node) const;
Expand Down
2 changes: 1 addition & 1 deletion src/typechecker/OpRuleManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/typechecker/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
}

Expand Down

0 comments on commit 78c58d3

Please sign in to comment.