Skip to content

[AST] Remove ParenType #59141

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 3 commits into from
Nov 1, 2024
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
1 change: 0 additions & 1 deletion docs/ABI/Mangling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,6 @@ productions:
type ::= base-type "XSq" // sugared Optional type
type ::= base-type "XSa" // sugared Array type
type ::= key-type value-type "XSD" // sugared Dictionary type
type ::= base-type "XSp" // sugared Paren type

Generics
~~~~~~~~
Expand Down
2 changes: 0 additions & 2 deletions include/swift/AST/ASTDemangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,6 @@ class ASTBuilder {

Type createDictionaryType(Type key, Type value);

Type createParenType(Type base);

Type createIntegerType(intptr_t value);

Type createNegativeIntegerType(intptr_t value);
Expand Down
3 changes: 1 addition & 2 deletions include/swift/AST/TypeNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ UNCHECKED_TYPE(TypeVariable, Type)
UNCHECKED_TYPE(ErrorUnion, Type)
ALWAYS_CANONICAL_TYPE(Integer, Type)
ABSTRACT_SUGARED_TYPE(Sugar, Type)
SUGARED_TYPE(Paren, SugarType)
SUGARED_TYPE(TypeAlias, SugarType)
ABSTRACT_SUGARED_TYPE(SyntaxSugar, SugarType)
ABSTRACT_SUGARED_TYPE(UnarySyntaxSugar, SyntaxSugarType)
Expand All @@ -219,7 +218,7 @@ ABSTRACT_SUGARED_TYPE(Sugar, Type)
TYPE_RANGE(UnarySyntaxSugar, ArraySlice, VariadicSequence)
SUGARED_TYPE(Dictionary, SyntaxSugarType)
TYPE_RANGE(SyntaxSugar, ArraySlice, Dictionary)
TYPE_RANGE(Sugar, Paren, Dictionary)
TYPE_RANGE(Sugar, TypeAlias, Dictionary)
LAST_TYPE(Dictionary) // Sugared types are last to make isa<SugarType>() fast.

#endif
Expand Down
23 changes: 4 additions & 19 deletions include/swift/AST/TypeTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,10 @@ class TypeTransform {

public:
Type doIt(Type t, TypePosition pos) {
if (!isa<ParenType>(t.getPointer())) {
// Transform this type node.
if (std::optional<Type> transformed = asDerived().transform(t.getPointer(), pos))
return *transformed;

// Recur.
}
// Transform this type node.
if (std::optional<Type> transformed =
asDerived().transform(t.getPointer(), pos))
return *transformed;

// Recur into children of this type.
TypeBase *base = t.getPointer();
Expand Down Expand Up @@ -536,18 +533,6 @@ case TypeKind::Id:
newUnderlyingTy);
}

case TypeKind::Paren: {
auto paren = cast<ParenType>(base);
Type underlying = doIt(paren->getUnderlyingType(), pos);
if (!underlying)
return Type();

if (underlying.getPointer() == paren->getUnderlyingType().getPointer())
return t;

return ParenType::get(ctx, underlying);
}

case TypeKind::ErrorUnion: {
auto errorUnion = cast<ErrorUnionType>(base);
bool anyChanged = false;
Expand Down
23 changes: 0 additions & 23 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,6 @@ class alignas(1 << TypeAlignInBits) TypeBase
HasCachedType : 1
);

SWIFT_INLINE_BITFIELD_EMPTY(ParenType, SugarType);

SWIFT_INLINE_BITFIELD_FULL(AnyFunctionType, TypeBase, NumAFTExtInfoBits+1+1+1+1+16,
/// Extra information which affects how the function is called, like
/// regparm and the calling convention.
Expand Down Expand Up @@ -688,9 +686,6 @@ class alignas(1 << TypeAlignInBits) TypeBase
/// Returns true if this contextual type is (Escapable && !isNoEscape).
bool mayEscape() { return !isNoEscape() && isEscapable(); }

/// Does the type have outer parenthesis?
bool hasParenSugar() const { return getKind() == TypeKind::Paren; }

/// Are values of this type essentially just class references,
/// possibly with some sort of additional information?
///
Expand Down Expand Up @@ -1288,9 +1283,6 @@ class alignas(1 << TypeAlignInBits) TypeBase
/// argument labels removed.
Type removeArgumentLabels(unsigned numArgumentLabels);

/// Retrieve the type without any parentheses around it.
Type getWithoutParens();

/// Replace the base type of the result type of the given function
/// type with a new result type, as per a DynamicSelf or other
/// covariant return transformation. The optionality of the
Expand Down Expand Up @@ -2589,21 +2581,6 @@ class YieldTypeFlags {
uint8_t toRaw() const { return value.toRaw(); }
};

/// ParenType - A paren type is a type that's been written in parentheses.
class ParenType : public SugarType {
ParenType(Type UnderlyingType, RecursiveTypeProperties properties);

public:
static ParenType *get(const ASTContext &C, Type underlying);

Type getUnderlyingType() const { return getSinglyDesugaredType(); }

// Implement isa/cast/dyncast/etc.
static bool classof(const TypeBase *T) {
return T->getKind() == TypeKind::Paren;
}
};

/// TupleTypeElt - This represents a single element of a tuple.
class TupleTypeElt {
/// An optional name for the field.
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Demangling/DemangleNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ NODE(AssociatedTypeGenericParamRef)
NODE(SugaredOptional)
NODE(SugaredArray)
NODE(SugaredDictionary)
NODE(SugaredParen)
NODE(SugaredParen) // Removed in Swift 6.TBD

// Added in Swift 5.1
NODE(AccessorFunctionReference)
Expand Down
4 changes: 3 additions & 1 deletion include/swift/Demangling/TypeDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,9 @@ class TypeDecoder {
if (base.isError())
return base;

return Builder.createParenType(base.getType());
// ParenType has been removed, return the base type for backwards
// compatibility.
return base.getType();
}
case NodeKind::OpaqueType: {
if (Node->getNumChildren() < 3)
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Sema/CSBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ struct PotentialBinding {
public:
PotentialBinding(Type type, AllowedBindingKind kind, Constraint *source)
: PotentialBinding(
type->getWithoutParens(), kind,
type, kind,
PointerUnion<Constraint *, ConstraintLocator *>(source)) {}

bool isDefaultableBinding() const {
Expand Down
3 changes: 1 addition & 2 deletions lib/APIDigester/ModuleAnalyzerNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1065,13 +1065,12 @@ static StringRef getPrintedName(SDKContext &Ctx, Type Ty,
if (IsImplicitlyUnwrappedOptional)
PO.PrintOptionalAsImplicitlyUnwrapped = true;

Ty->getWithoutParens().print(OS, PO);
Ty.print(OS, PO);
return Ctx.buffer(OS.str());
}

static StringRef getTypeName(SDKContext &Ctx, Type Ty,
bool IsImplicitlyUnwrappedOptional) {
Ty = Ty->getWithoutParens();
if (Ty->isVoid()) {
return Ctx.buffer("Void");
}
Expand Down
17 changes: 1 addition & 16 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,6 @@ struct ASTContext::Implementation {
llvm::DenseMap<Type, VariadicSequenceType*> VariadicSequenceTypes;
llvm::DenseMap<std::pair<Type, Type>, DictionaryType *> DictionaryTypes;
llvm::DenseMap<Type, OptionalType*> OptionalTypes;
llvm::DenseMap<Type, ParenType*> ParenTypes;
llvm::DenseMap<uintptr_t, ReferenceStorageType*> ReferenceStorageTypes;
llvm::DenseMap<Type, LValueType*> LValueTypes;
llvm::DenseMap<Type, InOutType*> InOutTypes;
Expand Down Expand Up @@ -3240,7 +3239,6 @@ size_t ASTContext::Implementation::Arena::getTotalMemory() const {
llvm::capacity_in_bytes(DictionaryTypes) +
llvm::capacity_in_bytes(OptionalTypes) +
llvm::capacity_in_bytes(VariadicSequenceTypes) +
llvm::capacity_in_bytes(ParenTypes) +
llvm::capacity_in_bytes(ReferenceStorageTypes) +
llvm::capacity_in_bytes(LValueTypes) +
llvm::capacity_in_bytes(InOutTypes) +
Expand Down Expand Up @@ -3279,7 +3277,6 @@ void ASTContext::Implementation::Arena::dump(llvm::raw_ostream &os) const {
SIZE_AND_BYTES(VariadicSequenceTypes);
SIZE_AND_BYTES(DictionaryTypes);
SIZE_AND_BYTES(OptionalTypes);
SIZE_AND_BYTES(ParenTypes);
SIZE_AND_BYTES(ReferenceStorageTypes);
SIZE_AND_BYTES(LValueTypes);
SIZE_AND_BYTES(InOutTypes);
Expand Down Expand Up @@ -3629,18 +3626,6 @@ BuiltinVectorType *BuiltinVectorType::get(const ASTContext &context,
return vecTy;
}

ParenType *ParenType::get(const ASTContext &C, Type underlying) {
auto properties = underlying->getRecursiveProperties();
auto arena = getArena(properties);
ParenType *&Result = C.getImpl().getArena(arena).ParenTypes[underlying];
if (Result == nullptr) {
Result = new (C, arena) ParenType(underlying, properties);
assert((C.hadError() || !underlying->is<InOutType>()) &&
"Cannot wrap InOutType");
}
return Result;
}

CanTupleType TupleType::getEmpty(const ASTContext &C) {
return cast<TupleType>(CanType(C.TheEmptyTupleType));
}
Expand Down Expand Up @@ -4567,7 +4552,7 @@ Type AnyFunctionType::composeTuple(ASTContext &ctx, ArrayRef<Param> params,
elements.emplace_back(param.getParameterType(), param.getLabel());
}
if (elements.size() == 1 && !elements[0].hasName())
return ParenType::get(ctx, elements[0].getType());
return elements[0].getType();
return TupleType::get(elements, ctx);
}

Expand Down
4 changes: 0 additions & 4 deletions lib/AST/ASTDemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1053,10 +1053,6 @@ Type ASTBuilder::createDictionaryType(Type key, Type value) {
return DictionaryType::get(key, value);
}

Type ASTBuilder::createParenType(Type base) {
return ParenType::get(Ctx, base);
}

Type ASTBuilder::createIntegerType(intptr_t value) {
return IntegerType::get(std::to_string(value), /*isNegative*/ false, Ctx);
}
Expand Down
8 changes: 0 additions & 8 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4140,14 +4140,6 @@ namespace {
printFoot();
}

void visitParenType(ParenType *T, StringRef label) {
printCommon("paren_type", label);

printRec(T->getUnderlyingType());

printFoot();
}

void visitTupleType(TupleType *T, StringRef label) {
printCommon("tuple_type", label);

Expand Down
6 changes: 0 additions & 6 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1396,12 +1396,6 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
return;
}

case TypeKind::Paren:
assert(DWARFMangling && "sugared types are only legal for the debugger");
appendType(cast<ParenType>(tybase)->getUnderlyingType(), sig, forDecl);
appendOperator("XSp");
return;

case TypeKind::ArraySlice:
assert(DWARFMangling && "sugared types are only legal for the debugger");
appendType(cast<ArraySliceType>(tybase)->getBaseType(), sig, forDecl);
Expand Down
8 changes: 1 addition & 7 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6102,12 +6102,6 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
}
}

void visitParenType(ParenType *T) {
Printer << "(";
visit(T->getUnderlyingType()->getInOutObjectType());
Printer << ")";
}

void visitPackType(PackType *T) {
if (Options.PrintExplicitPackTypes || Options.PrintTypesForDebugging)
Printer << "Pack{";
Expand Down Expand Up @@ -6185,7 +6179,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
Printer << ": ";
} else if (e == 1 && !EltType->is<PackExpansionType>()) {
// Unlabeled one-element tuples always print the empty label to
// distinguish them from the older syntax for ParenType.
// distinguish them from the older syntax for parens.
Printer << "_: ";
}
visit(EltType);
Expand Down
10 changes: 0 additions & 10 deletions lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2135,16 +2135,6 @@ class Verifier : public ASTWalker {
verifyCheckedBase(E);
}

void verifyChecked(ParenExpr *E) {
PrettyStackTraceExpr debugStack(Ctx, "verifying ParenExpr", E);
auto ty = dyn_cast<ParenType>(E->getType().getPointer());
if (!ty) {
Out << "ParenExpr not of ParenType\n";
abort();
}
verifyCheckedBase(E);
}

void verifyChecked(AnyTryExpr *E) {
PrettyStackTraceExpr debugStack(Ctx, "verifying AnyTryExpr", E);

Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ArgumentList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ Expr *ArgumentList::packIntoImplicitTupleOrParen(
if (auto *unary = getUnlabeledUnaryExpr()) {
auto *paren = new (ctx) ParenExpr(getLParenLoc(), unary, getRParenLoc());
if (auto ty = getType(unary))
paren->setType(ParenType::get(ctx, ty));
paren->setType(ty);
paren->setImplicit();
return paren;
}
Expand Down
31 changes: 5 additions & 26 deletions lib/AST/DiagnosticEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,8 @@ static bool typeSpellingIsAmbiguous(Type type,
for (auto arg : Args) {
if (arg.getKind() == DiagnosticArgumentKind::Type) {
auto argType = arg.getAsType();
if (argType && argType->getWithoutParens().getPointer() != type.getPointer() &&
argType->getWithoutParens().getString(PO) == type.getString(PO)) {
if (argType && argType.getPointer() != type.getPointer() &&
argType.getString(PO) == type.getString(PO)) {
// Currently, existential types are spelled the same way
// as protocols and compositions. We can remove this once
// existenials are printed with 'any'.
Expand Down Expand Up @@ -900,41 +900,20 @@ static void formatDiagnosticArgument(StringRef Modifier,
// Compute the appropriate print options for this argument.
auto printOptions = PrintOptions::forDiagnosticArguments();
if (Arg.getKind() == DiagnosticArgumentKind::Type) {
type = Arg.getAsType()->getWithoutParens();
if (type.isNull()) {
// FIXME: We should never receive a nullptr here, but this is causing
// crashes (rdar://75740683). Remove once ParenType never contains
// nullptr as the underlying type.
Out << "<null>";
break;
}
type = Arg.getAsType();
if (type->getASTContext().TypeCheckerOpts.PrintFullConvention)
printOptions.PrintFunctionRepresentationAttrs =
PrintOptions::FunctionRepresentationMode::Full;
needsQualification = typeSpellingIsAmbiguous(type, Args, printOptions);
} else if (Arg.getKind() == DiagnosticArgumentKind::FullyQualifiedType) {
type = Arg.getAsFullyQualifiedType().getType()->getWithoutParens();
if (type.isNull()) {
// FIXME: We should never receive a nullptr here, but this is causing
// crashes (rdar://75740683). Remove once ParenType never contains
// nullptr as the underlying type.
Out << "<null>";
break;
}
type = Arg.getAsFullyQualifiedType().getType();
if (type->getASTContext().TypeCheckerOpts.PrintFullConvention)
printOptions.PrintFunctionRepresentationAttrs =
PrintOptions::FunctionRepresentationMode::Full;
needsQualification = true;
} else {
assert(Arg.getKind() == DiagnosticArgumentKind::WitnessType);
type = Arg.getAsWitnessType().getType()->getWithoutParens();
if (type.isNull()) {
// FIXME: We should never receive a nullptr here, but this is causing
// crashes (rdar://75740683). Remove once ParenType never contains
// nullptr as the underlying type.
Out << "<null>";
break;
}
type = Arg.getAsWitnessType().getType();
printOptions.PrintGenericRequirements = false;
printOptions.PrintInverseRequirements = false;
needsQualification = typeSpellingIsAmbiguous(type, Args, printOptions);
Expand Down
Loading