Skip to content

NFC: repack misc typebase bits #13366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,15 @@ class alignas(1 << TypeAlignInBits) TypeBase {
};
NUMBITS(ErrorType, NumTypeBaseBits + 1);

struct ParenTypeBitfields {
unsigned : NumTypeBaseBits;

/// Whether there is an original type.
enum { NumFlagBits = 5 };
unsigned Flags : NumFlagBits;
};
NUMBITS(ParenType, NumTypeBaseBits + ParenTypeBitfields::NumFlagBits);

struct AnyFunctionTypeBitfields {
unsigned : NumTypeBaseBits;

Expand All @@ -309,7 +318,14 @@ class alignas(1 << TypeAlignInBits) TypeBase {
unsigned : NumTypeBaseBits;

/// \brief The unique number assigned to this type variable.
uint64_t ID : 64 - NumTypeBaseBits;
uint64_t ID : 32 - NumTypeBaseBits;

/// Type variable options.
unsigned Options : 3;

/// Index into the list of type variables, as used by the
/// constraint graph.
unsigned GraphIndex : 29;
};
NUMBITS(TypeVariableType, 64);

Expand Down Expand Up @@ -353,6 +369,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {
union {
TypeBaseBitfields TypeBaseBits;
ErrorTypeBitfields ErrorTypeBits;
ParenTypeBitfields ParenTypeBits;
AnyFunctionTypeBitfields AnyFunctionTypeBits;
TypeVariableTypeBitfields TypeVariableTypeBits;
ArchetypeTypeBitfields ArchetypeTypeBits;
Expand Down Expand Up @@ -1415,6 +1432,9 @@ class ParameterTypeFlags {

public:
ParameterTypeFlags() = default;
static ParameterTypeFlags fromRaw(uint8_t raw) {
return ParameterTypeFlags(OptionSet<ParameterFlags>(raw));
}

ParameterTypeFlags(bool variadic, bool autoclosure, bool escaping, bool inOut, bool shared)
: value((variadic ? Variadic : 0) |
Expand Down Expand Up @@ -1465,7 +1485,6 @@ class ParameterTypeFlags {
/// ParenType - A paren type is a type that's been written in parentheses.
class ParenType : public TypeBase {
Type UnderlyingType;
ParameterTypeFlags parameterFlags;

friend class ASTContext;

Expand All @@ -1482,7 +1501,9 @@ class ParenType : public TypeBase {
TypeBase *getSinglyDesugaredType();

/// Get the parameter flags
ParameterTypeFlags getParameterFlags() const { return parameterFlags; }
ParameterTypeFlags getParameterFlags() const {
return ParameterTypeFlags::fromRaw(ParenTypeBits.Flags);
}

// Implement isa/cast/dyncast/etc.
static bool classof(const TypeBase *T) {
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1314,8 +1314,8 @@ ParenType::ParenType(Type baseType, RecursiveTypeProperties properties,
: TypeBase(TypeKind::Paren, nullptr, properties),
UnderlyingType(flags.isInOut()
? InOutType::get(baseType)
: baseType),
parameterFlags(flags) {
: baseType) {
ParenTypeBits.Flags = flags.toRaw();
if (flags.isInOut())
assert(!baseType->is<InOutType>() && "caller did not pass a base type");
if (baseType->is<InOutType>())
Expand Down
67 changes: 38 additions & 29 deletions lib/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,6 @@ enum TypeVariableOptions {
/// to, what specific types it might be and, eventually, the fixed type to
/// which it is assigned.
class TypeVariableType::Implementation {
/// Type variable options.
unsigned Options : 3;

/// \brief The locator that describes where this type variable was generated.
constraints::ConstraintLocator *locator;

Expand All @@ -191,45 +188,53 @@ class TypeVariableType::Implementation {
/// The corresponding node in the constraint graph.
constraints::ConstraintGraphNode *GraphNode = nullptr;

/// Index into the list of type variables, as used by the
/// constraint graph.
unsigned GraphIndex;

friend class constraints::SavedTypeVariableBinding;

public:
/// \brief Retrieve the type variable associated with this implementation.
TypeVariableType *getTypeVariable() {
return reinterpret_cast<TypeVariableType *>(this) - 1;
}

/// \brief Retrieve the type variable associated with this implementation.
const TypeVariableType *getTypeVariable() const {
return reinterpret_cast<const TypeVariableType *>(this) - 1;
}

explicit Implementation(constraints::ConstraintLocator *locator,
unsigned options)
: Options(options), locator(locator),
ParentOrFixed(getTypeVariable()) { }
: locator(locator), ParentOrFixed(getTypeVariable()) {
getTypeVariable()->TypeVariableTypeBits.Options = options;
}

/// \brief Retrieve the unique ID corresponding to this type variable.
unsigned getID() const { return getTypeVariable()->getID(); }

unsigned getRawOptions() const {
return getTypeVariable()->TypeVariableTypeBits.Options;
}

void setRawOptions(unsigned bits) {
getTypeVariable()->TypeVariableTypeBits.Options = bits;
assert(getTypeVariable()->TypeVariableTypeBits.Options == bits
&& "Trucation");
}

/// Whether this type variable can bind to an lvalue type.
bool canBindToLValue() const { return Options & TVO_CanBindToLValue; }
bool canBindToLValue() const { return getRawOptions() & TVO_CanBindToLValue; }

/// Whether this type variable can bind to an inout type.
bool canBindToInOut() const { return Options & TVO_CanBindToInOut; }
bool canBindToInOut() const { return getRawOptions() & TVO_CanBindToInOut; }

/// Whether this type variable prefers a subtype binding over a supertype
/// binding.
bool prefersSubtypeBinding() const {
return Options & TVO_PrefersSubtypeBinding;
return getRawOptions() & TVO_PrefersSubtypeBinding;
}

bool mustBeMaterializable() const {
return !(Options & TVO_CanBindToInOut) && !(Options & TVO_CanBindToLValue);
}

/// \brief Retrieve the type variable associated with this implementation.
TypeVariableType *getTypeVariable() {
return reinterpret_cast<TypeVariableType *>(this) - 1;
}

/// \brief Retrieve the type variable associated with this implementation.
const TypeVariableType *getTypeVariable() const {
return reinterpret_cast<const TypeVariableType *>(this) - 1;
return !(getRawOptions() & TVO_CanBindToInOut) &&
!(getRawOptions() & TVO_CanBindToLValue);
}

/// Retrieve the corresponding node in the constraint graph.
Expand All @@ -243,11 +248,13 @@ class TypeVariableType::Implementation {
/// Retrieve the index into the constraint graph's list of type variables.
unsigned getGraphIndex() const {
assert(GraphNode && "Graph node isn't set");
return GraphIndex;
return getTypeVariable()->TypeVariableTypeBits.GraphIndex;
}

/// Set the index into the constraint graph's list of type variables.
void setGraphIndex(unsigned newIndex) { GraphIndex = newIndex; }
void setGraphIndex(unsigned newIndex) {
getTypeVariable()->TypeVariableTypeBits.GraphIndex = newIndex;
}

/// \brief Check whether this type variable either has a representative that
/// is not itself or has a fixed type binding.
Expand Down Expand Up @@ -339,8 +346,8 @@ class TypeVariableType::Implementation {
if (!mustBeMaterializable() && otherRep->getImpl().mustBeMaterializable()) {
if (record)
recordBinding(*record);
Options &= ~TVO_CanBindToLValue;
Options &= ~TVO_CanBindToInOut;
getTypeVariable()->TypeVariableTypeBits.Options &= ~TVO_CanBindToLValue;
getTypeVariable()->TypeVariableTypeBits.Options &= ~TVO_CanBindToInOut;
}
}

Expand Down Expand Up @@ -381,8 +388,10 @@ class TypeVariableType::Implementation {
if (!rep->getImpl().mustBeMaterializable()) {
if (record)
rep->getImpl().recordBinding(*record);
rep->getImpl().Options &= ~TVO_CanBindToLValue;
rep->getImpl().Options &= ~TVO_CanBindToInOut;
rep->getImpl().getTypeVariable()->TypeVariableTypeBits.Options
&= ~TVO_CanBindToLValue;
rep->getImpl().getTypeVariable()->TypeVariableTypeBits.Options
&= ~TVO_CanBindToInOut;
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ void TypeVariableType::Implementation::print(llvm::raw_ostream &OS) {
}

SavedTypeVariableBinding::SavedTypeVariableBinding(TypeVariableType *typeVar)
: TypeVarAndOptions(typeVar, typeVar->getImpl().Options),
: TypeVarAndOptions(typeVar, typeVar->getImpl().getRawOptions()),
ParentOrFixed(typeVar->getImpl().ParentOrFixed) { }

void SavedTypeVariableBinding::restore() {
auto *typeVar = getTypeVariable();
typeVar->getImpl().Options = getOptions();
typeVar->getImpl().setRawOptions(getOptions());
typeVar->getImpl().ParentOrFixed = ParentOrFixed;
}

Expand Down