Skip to content

[6.1] Introduce a new Initializer subclass for the arguments of custom attributes #78031

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
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
10 changes: 9 additions & 1 deletion include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,14 @@ SWIFT_NAME("getter:BridgedPatternBindingInitializer.asDeclContext(self:)")
BridgedDeclContext BridgedPatternBindingInitializer_asDeclContext(
BridgedPatternBindingInitializer cInit);

SWIFT_NAME("BridgedCustomAttributeInitializer.create(declContext:)")
BridgedCustomAttributeInitializer
BridgedCustomAttributeInitializer_create(BridgedDeclContext cDeclContext);

SWIFT_NAME("getter:BridgedCustomAttributeInitializer.asDeclContext(self:)")
BridgedDeclContext BridgedCustomAttributeInitializer_asDeclContext(
BridgedCustomAttributeInitializer cInit);

SWIFT_NAME("getter:BridgedClosureExpr.asDeclContext(self:)")
BridgedDeclContext
BridgedClosureExpr_asDeclContext(BridgedClosureExpr cClosure);
Expand Down Expand Up @@ -609,7 +617,7 @@ SWIFT_NAME(
"BridgedCustomAttr.createParsed(_:atLoc:type:initContext:argumentList:)")
BridgedCustomAttr BridgedCustomAttr_createParsed(
BridgedASTContext cContext, BridgedSourceLoc cAtLoc, BridgedTypeRepr cType,
BridgedNullablePatternBindingInitializer cInitContext,
BridgedNullableCustomAttributeInitializer cInitContext,
BridgedNullableArgumentList cArgumentList);

SWIFT_NAME(
Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/ASTBridgingWrappers.def
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ AST_BRIDGING_WRAPPER_NULLABLE(GenericParamList)
AST_BRIDGING_WRAPPER_NULLABLE(TrailingWhereClause)
AST_BRIDGING_WRAPPER_NULLABLE(ParameterList)
AST_BRIDGING_WRAPPER_NULLABLE(PatternBindingInitializer)
AST_BRIDGING_WRAPPER_NULLABLE(CustomAttributeInitializer)
AST_BRIDGING_WRAPPER_NONNULL(TypeAttributes)
AST_BRIDGING_WRAPPER_NONNULL(CustomAttribute)
AST_BRIDGING_WRAPPER_NULLABLE(ArgumentList)
Expand Down
9 changes: 5 additions & 4 deletions include/swift/AST/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class AbstractFunctionDecl;
class FuncDecl;
class ClassDecl;
class AccessorDecl;
class CustomAttributeInitializer;
class GenericFunctionType;
class LazyConformanceLoader;
class LazyMemberLoader;
Expand Down Expand Up @@ -1846,13 +1847,13 @@ class ClangImporterSynthesizedTypeAttr : public DeclAttribute {
class CustomAttr final : public DeclAttribute {
TypeExpr *typeExpr;
ArgumentList *argList;
PatternBindingInitializer *initContext;
CustomAttributeInitializer *initContext;
Expr *semanticInit = nullptr;

mutable unsigned isArgUnsafeBit : 1;

CustomAttr(SourceLoc atLoc, SourceRange range, TypeExpr *type,
PatternBindingInitializer *initContext, ArgumentList *argList,
CustomAttributeInitializer *initContext, ArgumentList *argList,
bool implicit);

public:
Expand All @@ -1863,7 +1864,7 @@ class CustomAttr final : public DeclAttribute {
}

static CustomAttr *create(ASTContext &ctx, SourceLoc atLoc, TypeExpr *type,
PatternBindingInitializer *initContext,
CustomAttributeInitializer *initContext,
ArgumentList *argList, bool implicit = false);

TypeExpr *getTypeExpr() const { return typeExpr; }
Expand Down Expand Up @@ -1896,7 +1897,7 @@ class CustomAttr final : public DeclAttribute {
Expr *getSemanticInit() const { return semanticInit; }
void setSemanticInit(Expr *expr) { semanticInit = expr; }

PatternBindingInitializer *getInitContext() const { return initContext; }
CustomAttributeInitializer *getInitContext() const { return initContext; }

static bool classof(const DeclAttribute *DA) {
return DA->getKind() == DeclAttrKind::Custom;
Expand Down
35 changes: 35 additions & 0 deletions include/swift/AST/Initializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

#include "swift/AST/DeclContext.h"

namespace llvm {
class raw_ostream;
}

namespace swift {
class ParamDecl;
class PatternBindingDecl;
Expand All @@ -36,6 +40,9 @@ enum class InitializerKind : uint8_t {

/// A property wrapper initialization expression.
PropertyWrapper,

/// An expression within a custom attribute.
CustomAttribute,
};

/// An Initializer is a kind of DeclContext used for expressions that
Expand Down Expand Up @@ -176,6 +183,34 @@ class PropertyWrapperInitializer : public Initializer {
}
};

/// An expression within a custom attribute. The parent context is the
/// context in which the attributed declaration occurs.
class CustomAttributeInitializer : public Initializer {
public:
explicit CustomAttributeInitializer(DeclContext *parent)
: Initializer(InitializerKind::CustomAttribute, parent) {}

static CustomAttributeInitializer *create(DeclContext *parent) {
return new (parent->getASTContext()) CustomAttributeInitializer(parent);
}

void setEnclosingInitializer(Initializer *newParent) {
setParent(newParent);
}

static bool classof(const DeclContext *DC) {
if (auto init = dyn_cast<Initializer>(DC))
return classof(init);
return false;
}

static bool classof(const Initializer *I) {
return I->getInitializerKind() == InitializerKind::CustomAttribute;
}
};

void simple_display(llvm::raw_ostream &out, Initializer *init);

} // end namespace swift

#endif
20 changes: 2 additions & 18 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -998,8 +998,7 @@ class Parser {

/// Parse an #if ... #endif containing only attributes.
ParserStatus parseIfConfigAttributes(
DeclAttributes &attributes, bool ifConfigsAreDeclAttrs,
PatternBindingInitializer *initContext);
DeclAttributes &attributes, bool ifConfigsAreDeclAttrs);

/// Parse a #error or #warning diagnostic.
ParserResult<PoundDiagnosticDecl> parseDeclPoundDiagnostic();
Expand All @@ -1022,13 +1021,6 @@ class Parser {
ParserStatus parseDeclAttributeList(DeclAttributes &Attributes,
bool IfConfigsAreDeclAttrs = false);

/// Parse the optional attributes before a declaration.
///
/// This is the inner loop, which can be called recursively.
ParserStatus parseDeclAttributeList(DeclAttributes &Attributes,
bool IfConfigsAreDeclAttrs,
PatternBindingInitializer *initContext);

/// Parse the optional attributes before a closure declaration.
ParserStatus parseClosureDeclAttributeList(DeclAttributes &Attributes);

Expand Down Expand Up @@ -1163,7 +1155,6 @@ class Parser {
/// Parse a specific attribute.
ParserStatus parseDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
SourceLoc AtEndLoc,
PatternBindingInitializer *&initContext,
bool isFromClangAttribute = false);

bool isCustomAttributeArgument();
Expand All @@ -1172,13 +1163,7 @@ class Parser {
/// Parse a custom attribute after the initial '@'.
///
/// \param atLoc The location of the already-parsed '@'.
///
/// \param initContext A reference to the initializer context used
/// for the set of custom attributes. This should start as nullptr, and
/// will get filled in by this function. The same variable should be provided
/// for every custom attribute within the same attribute list.
ParserResult<CustomAttr> parseCustomAttribute(
SourceLoc atLoc, PatternBindingInitializer *&initContext);
ParserResult<CustomAttr> parseCustomAttribute(SourceLoc atLoc);

ParserStatus parseNewDeclAttribute(DeclAttributes &Attributes,
SourceLoc AtLoc, DeclAttrKind DK,
Expand Down Expand Up @@ -1480,7 +1465,6 @@ class Parser {

ParserStatus parseTypeAttribute(TypeOrCustomAttr &result, SourceLoc AtLoc,
SourceLoc AtEndLoc, ParseTypeReason reason,
PatternBindingInitializer *&initContext,
bool justChecking = false);

ParserResult<TypeRepr> parseOldStyleProtocolComposition();
Expand Down
12 changes: 1 addition & 11 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1809,17 +1809,7 @@ void swift::printContext(raw_ostream &os, DeclContext *dc) {
break;

case DeclContextKind::Initializer:
switch (cast<Initializer>(dc)->getInitializerKind()) {
case InitializerKind::PatternBinding:
os << "pattern binding initializer";
break;
case InitializerKind::DefaultArgument:
os << "default argument initializer";
break;
case InitializerKind::PropertyWrapper:
os << "property wrapper initializer";
break;
}
simple_display(os, cast<Initializer>(dc));
break;

case DeclContextKind::TopLevelCodeDecl:
Expand Down
6 changes: 6 additions & 0 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2540,6 +2540,12 @@ void ASTMangler::appendContext(const DeclContext *ctx,
}
return;
}

case InitializerKind::CustomAttribute: {
BaseEntitySignature nullBase(nullptr);
appendContext(ctx->getParent(), nullBase, useModuleName);
return;
}
}
llvm_unreachable("bad initializer kind");
}
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/Attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2899,7 +2899,7 @@ ProtocolDecl *ImplementsAttr::getProtocol(DeclContext *dc) const {
}

CustomAttr::CustomAttr(SourceLoc atLoc, SourceRange range, TypeExpr *type,
PatternBindingInitializer *initContext,
CustomAttributeInitializer *initContext,
ArgumentList *argList, bool implicit)
: DeclAttribute(DeclAttrKind::Custom, atLoc, range, implicit),
typeExpr(type), argList(argList), initContext(initContext) {
Expand All @@ -2908,7 +2908,7 @@ CustomAttr::CustomAttr(SourceLoc atLoc, SourceRange range, TypeExpr *type,
}

CustomAttr *CustomAttr::create(ASTContext &ctx, SourceLoc atLoc, TypeExpr *type,
PatternBindingInitializer *initContext,
CustomAttributeInitializer *initContext,
ArgumentList *argList, bool implicit) {
assert(type);
SourceRange range(atLoc, type->getSourceRange().End);
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/Bridging/DeclAttributeBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ BridgedCDeclAttr BridgedCDeclAttr_createParsed(BridgedASTContext cContext,

BridgedCustomAttr BridgedCustomAttr_createParsed(
BridgedASTContext cContext, BridgedSourceLoc cAtLoc, BridgedTypeRepr cType,
BridgedNullablePatternBindingInitializer cInitContext,
BridgedNullableCustomAttributeInitializer cInitContext,
BridgedNullableArgumentList cArgumentList) {
ASTContext &context = cContext.unbridged();
return CustomAttr::create(
Expand Down
10 changes: 10 additions & 0 deletions lib/AST/Bridging/DeclContextBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ BridgedDeclContext BridgedPatternBindingInitializer_asDeclContext(
return cInit.unbridged();
}

BridgedCustomAttributeInitializer
BridgedCustomAttributeInitializer_create(BridgedDeclContext cDeclContext) {
return CustomAttributeInitializer::create(cDeclContext.unbridged());
}

BridgedDeclContext BridgedCustomAttributeInitializer_asDeclContext(
BridgedCustomAttributeInitializer cInit) {
return cInit.unbridged();
}

BridgedDeclContext
BridgedClosureExpr_asDeclContext(BridgedClosureExpr cClosure) {
return cClosure.unbridged();
Expand Down
11 changes: 10 additions & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2061,8 +2061,17 @@ PatternBindingDecl::create(ASTContext &Ctx, SourceLoc StaticLoc,
// FIXME: We ought to reconsider this since it won't recontextualize any
// closures/decls present in the initialization expr. This currently should
// only affect implicit code though.
if (!initContext && !Parent->isLocalContext())
if (!initContext && !Parent->isLocalContext()) {
initContext = PatternBindingInitializer::create(Parent);
}

// Set up the custom attribute contexts for each variable in this pattern.
PBD->getPattern(idx)->forEachVariable([&](VarDecl *var) {
for (auto custom : var->getAttrs().getAttributes<CustomAttr>()) {
if (auto attributeInit = custom->getInitContext())
attributeInit->setEnclosingInitializer(initContext);
}
});

// We need to call setPattern to ensure the VarDecls in the pattern have
// the PatternBindingDecl set as their parent. We also need to call
Expand Down
3 changes: 3 additions & 0 deletions lib/AST/DeclContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,9 @@ unsigned DeclContext::printContext(raw_ostream &OS, const unsigned indent,
}
break;
}
case InitializerKind::CustomAttribute:
OS << " CustomAttribute";
break;
}
break;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/AST/TypeCheckRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,9 @@ void swift::simple_display(llvm::raw_ostream &out, Initializer *init) {
case InitializerKind::PropertyWrapper:
out << "property wrapper initializer";
break;
case InitializerKind::CustomAttribute:
out << "custom attribute initializer";
break;
}
}

Expand Down
4 changes: 4 additions & 0 deletions lib/ASTGen/Sources/ASTGen/Bridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extension BridgedNullableGenericParamList: /*@retroactive*/ swiftASTGen.BridgedN
extension BridgedNullableTrailingWhereClause: /*@retroactive*/ swiftASTGen.BridgedNullable {}
extension BridgedNullableParameterList: /*@retroactive*/ swiftASTGen.BridgedNullable {}
extension BridgedNullablePatternBindingInitializer: /*@retroactive*/ swiftASTGen.BridgedNullable {}
extension BridgedNullableCustomAttributeInitializer: /*@retroactive*/ swiftASTGen.BridgedNullable {}
extension BridgedNullableArgumentList: /*@retroactive*/ swiftASTGen.BridgedNullable {}

extension BridgedIdentifier: /*@retroactive*/ Swift.Equatable {
Expand Down Expand Up @@ -80,6 +81,9 @@ extension BridgedParameterList: BridgedHasNullable {
extension BridgedPatternBindingInitializer: BridgedHasNullable {
typealias Nullable = BridgedNullablePatternBindingInitializer
}
extension BridgedCustomAttributeInitializer: BridgedHasNullable {
typealias Nullable = BridgedNullableCustomAttributeInitializer
}
extension BridgedArgumentList: BridgedHasNullable {
typealias Nullable = BridgedNullableArgumentList
}
Expand Down
Loading