-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba826ef
commit f5cd430
Showing
17 changed files
with
313 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright (c) 2021-2024 ChilliBits. All rights reserved. | ||
|
||
#include "QualType.h" | ||
|
||
namespace spice::compiler { | ||
|
||
/** | ||
* Check for the matching compatibility of two types. | ||
* Useful for struct and function matching as well as assignment type validation and function arg matching. | ||
* | ||
* @param otherType Type to compare against | ||
* @param ignoreArraySize Ignore array sizes | ||
* @param ignoreSpecifiers Ignore specifiers, except for pointer and reference types | ||
* @param allowConstify Match when the types are the same, but the lhs type is more const restrictive than the rhs type | ||
* @return Matching or not | ||
*/ | ||
bool QualType::matches(const QualType &otherType, bool ignoreArraySize, bool ignoreSpecifiers, bool allowConstify) const { | ||
// Compare type | ||
if (!type.matches(otherType.type, ignoreArraySize)) | ||
return false; | ||
|
||
// Ignore or compare specifiers | ||
return ignoreSpecifiers || specifiers.match(otherType.specifiers, allowConstify); | ||
} | ||
|
||
/** | ||
* Get the name of the symbol type as a string | ||
* | ||
* @param withSize Include the array size for sized types | ||
* @param ignorePublic Ignore any potential public specifier | ||
* @return Symbol type name | ||
*/ | ||
std::string QualType::getName(bool withSize, bool ignorePublic) const { // NOLINT(misc-no-recursion) | ||
std::stringstream name; | ||
|
||
// Append the specifiers | ||
const TypeSpecifiers defaultForSuperType = TypeSpecifiers::of(type.getSuperType()); | ||
if (!ignorePublic && specifiers.isPublic && !defaultForSuperType.isPublic) | ||
name << "public "; | ||
if (specifiers.isInline && !defaultForSuperType.isInline) | ||
name << "inline "; | ||
if (specifiers.isComposition && !defaultForSuperType.isComposition) | ||
name << "compose "; | ||
if (specifiers.isConst && !defaultForSuperType.isConst) | ||
name << "const "; | ||
if (specifiers.isHeap && !defaultForSuperType.isHeap) | ||
name << "heap "; | ||
if (specifiers.isSigned && !defaultForSuperType.isSigned) | ||
name << "signed "; | ||
if (!specifiers.isSigned && defaultForSuperType.isSigned) | ||
name << "unsigned "; | ||
|
||
// Loop through all chain elements | ||
type.getName(name, withSize); | ||
|
||
return name.str(); | ||
} | ||
|
||
bool QualType::isConst() const { return specifiers.isConst; } | ||
|
||
bool QualType::isSigned() const { | ||
assert(isOneOf({TY_INT, TY_SHORT, TY_LONG, TY_BYTE, TY_CHAR, TY_BOOL})); | ||
return specifiers.isSigned; | ||
} | ||
|
||
bool QualType::isInline() const { | ||
assert(isOneOf({TY_FUNCTION, TY_PROCEDURE})); | ||
return specifiers.isInline; | ||
} | ||
|
||
bool QualType::isPublic() const { | ||
assert(type.isPrimitive() /* Global variables */ || isOneOf({TY_FUNCTION, TY_PROCEDURE, TY_ENUM, TY_STRUCT, TY_INTERFACE})); | ||
return specifiers.isPublic; | ||
} | ||
|
||
bool QualType::isHeap() const { return specifiers.isHeap; } | ||
|
||
bool QualType::isConstRef() const { return specifiers.isConst && type.isRef(); } | ||
|
||
QualType QualType::toNonConst() const { | ||
QualType qualType = *this; | ||
qualType.specifiers.isConst = false; | ||
return qualType; | ||
} | ||
|
||
/** | ||
* Check if a certain input type can be bound (assigned) to the current type. | ||
* | ||
* @param inputType Qualified type, which should be bound to the current type | ||
* @param isTemporary Is the input type a temporary type | ||
* @return Can be bound or not | ||
*/ | ||
bool QualType::canBind(const QualType &inputType, bool isTemporary) const { | ||
return !isTemporary || inputType.type.isRef() || !type.isRef() || isConstRef(); | ||
} | ||
|
||
/** | ||
* Replace the base type with another one | ||
* | ||
* @param newBaseType New base type | ||
* @return The new type | ||
*/ | ||
QualType QualType::replaceBaseType(const QualType &newBaseType) const { | ||
// Create new type | ||
Type newType = type.replaceBaseType(newBaseType.getType()); | ||
// Create new specifiers | ||
TypeSpecifiers newSpecifiers = specifiers.merge(newBaseType.specifiers); | ||
// Return the new qualified type | ||
return {newType, newSpecifiers}; | ||
} | ||
|
||
} // namespace spice::compiler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) 2021-2024 ChilliBits. All rights reserved. | ||
|
||
#pragma once | ||
|
||
#include <utility> | ||
|
||
#include <symboltablebuilder/Type.h> | ||
#include <symboltablebuilder/TypeSpecifiers.h> | ||
|
||
namespace spice::compiler { | ||
|
||
class QualType { | ||
public: | ||
// Constructors | ||
explicit QualType(Type type) : type(std::move(type)) {} | ||
QualType(Type type, TypeSpecifiers specifiers) : type(std::move(type)), specifiers(specifiers) {} | ||
|
||
// Public methods | ||
[[nodiscard]] Type &getType() { return type; } | ||
[[nodiscard]] const Type &getType() const { return type; } | ||
[[nodiscard]] bool matches(const QualType &otherType, bool ignoreArraySize, bool ignoreSpecifiers, bool allowConstify) const; | ||
[[nodiscard]] std::string getName(bool withSize = false, bool ignorePublic = false) const; | ||
[[nodiscard]] ALWAYS_INLINE bool is(SuperType superType) const { return type.is(superType); } | ||
[[nodiscard]] ALWAYS_INLINE bool isOneOf(const std::initializer_list<SuperType> &superTypes) const { | ||
return type.isOneOf(superTypes); | ||
} | ||
[[nodiscard]] bool isConst() const; | ||
[[nodiscard]] bool isSigned() const; | ||
[[nodiscard]] bool isInline() const; | ||
[[nodiscard]] bool isPublic() const; | ||
[[nodiscard]] bool isHeap() const; | ||
[[nodiscard]] bool isConstRef() const; | ||
[[nodiscard]] QualType toNonConst() const; | ||
[[nodiscard]] bool canBind(const QualType &otherType, bool isTemporary) const; | ||
[[nodiscard]] QualType replaceBaseType(const QualType &newBaseType) const; | ||
|
||
private: | ||
// Private members | ||
Type type; | ||
TypeSpecifiers specifiers; | ||
}; | ||
|
||
// Make sure we have no unexpected increases in memory consumption | ||
static_assert(sizeof(QualType) == 32); | ||
|
||
} // namespace spice::compiler |
Oops, something went wrong.