Skip to content

Commit 02a20de

Browse files
committed
[Parsers] Stop sharing CustomAttributeInitializer instances across properties
We don't need to share them, and it's far simpler if we don't.
1 parent ee9c066 commit 02a20de

File tree

6 files changed

+31
-70
lines changed

6 files changed

+31
-70
lines changed

include/swift/Parse/Parser.h

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -998,8 +998,7 @@ class Parser {
998998

999999
/// Parse an #if ... #endif containing only attributes.
10001000
ParserStatus parseIfConfigAttributes(
1001-
DeclAttributes &attributes, bool ifConfigsAreDeclAttrs,
1002-
CustomAttributeInitializer *&initContext);
1001+
DeclAttributes &attributes, bool ifConfigsAreDeclAttrs);
10031002

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

1025-
/// Parse the optional attributes before a declaration.
1026-
///
1027-
/// This is the inner loop, which can be called recursively.
1028-
ParserStatus parseDeclAttributeList(DeclAttributes &Attributes,
1029-
bool IfConfigsAreDeclAttrs,
1030-
CustomAttributeInitializer *&initContext);
1031-
10321024
/// Parse the optional attributes before a closure declaration.
10331025
ParserStatus parseClosureDeclAttributeList(DeclAttributes &Attributes);
10341026

@@ -1162,7 +1154,6 @@ class Parser {
11621154
/// Parse a specific attribute.
11631155
ParserStatus parseDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
11641156
SourceLoc AtEndLoc,
1165-
CustomAttributeInitializer *&initContext,
11661157
bool isFromClangAttribute = false);
11671158

11681159
bool isCustomAttributeArgument();
@@ -1171,13 +1162,7 @@ class Parser {
11711162
/// Parse a custom attribute after the initial '@'.
11721163
///
11731164
/// \param atLoc The location of the already-parsed '@'.
1174-
///
1175-
/// \param initContext A reference to the initializer context used
1176-
/// for the set of custom attributes. This should start as nullptr, and
1177-
/// will get filled in by this function. The same variable should be provided
1178-
/// for every custom attribute within the same attribute list.
1179-
ParserResult<CustomAttr> parseCustomAttribute(
1180-
SourceLoc atLoc, CustomAttributeInitializer *&initContext);
1165+
ParserResult<CustomAttr> parseCustomAttribute(SourceLoc atLoc);
11811166

11821167
ParserStatus parseNewDeclAttribute(DeclAttributes &Attributes,
11831168
SourceLoc AtLoc, DeclAttrKind DK,
@@ -1479,7 +1464,6 @@ class Parser {
14791464

14801465
ParserStatus parseTypeAttribute(TypeOrCustomAttr &result, SourceLoc AtLoc,
14811466
SourceLoc AtEndLoc, ParseTypeReason reason,
1482-
CustomAttributeInitializer *&initContext,
14831467
bool justChecking = false);
14841468

14851469
ParserResult<TypeRepr> parseOldStyleProtocolComposition();

lib/ASTGen/Sources/ASTGen/DeclAttrs.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ extension ASTGenVisitor {
2222
var attributes: BridgedDeclAttributes
2323
var staticSpelling: BridgedStaticSpelling
2424
var staticLoc: BridgedSourceLoc
25-
var initContext: BridgedCustomAttributeInitializer?
2625
}
2726

2827
func generateDeclAttributes(_ node: some WithAttributesSyntax & WithModifiersSyntax, allowStatic: Bool) -> DeclAttributesResult {
@@ -56,7 +55,6 @@ extension ASTGenVisitor {
5655

5756
// '@' attributes.
5857
// FIXME: Factor out this and share it with 'generate(accessorDecl:)'.
59-
var initContext: BridgedCustomAttributeInitializer? = nil
6058
visitIfConfigElements(node.attributes, of: AttributeSyntax.self) { element in
6159
switch element {
6260
case .ifConfigDecl(let ifConfigDecl):
@@ -65,7 +63,7 @@ extension ASTGenVisitor {
6563
return .underlying(attribute)
6664
}
6765
} body: { attribute in
68-
addAttribute(self.generateDeclAttribute(attribute: attribute, initContext: &initContext))
66+
addAttribute(self.generateDeclAttribute(attribute: attribute))
6967
}
7068

7169
func genStatic(node: DeclModifierSyntax, spelling: BridgedStaticSpelling) {
@@ -92,15 +90,14 @@ extension ASTGenVisitor {
9290
return DeclAttributesResult(
9391
attributes: attrs,
9492
staticSpelling: staticSpelling,
95-
staticLoc: staticLoc,
96-
initContext: initContext
93+
staticLoc: staticLoc
9794
)
9895
}
9996
}
10097

10198
// MARK: - Decl attributes
10299
extension ASTGenVisitor {
103-
func generateDeclAttribute(attribute node: AttributeSyntax, initContext: inout BridgedCustomAttributeInitializer?) -> BridgedDeclAttribute? {
100+
func generateDeclAttribute(attribute node: AttributeSyntax) -> BridgedDeclAttribute? {
104101
if let identTy = node.attributeName.as(IdentifierTypeSyntax.self) {
105102
let attrName = identTy.name.rawText
106103
let attrKind = BridgedDeclAttrKind(from: attrName.bridged)
@@ -316,7 +313,7 @@ extension ASTGenVisitor {
316313
}
317314
}
318315

319-
return self.generateCustomAttr(attribute: node, initContext: &initContext)?.asDeclAttribute
316+
return self.generateCustomAttr(attribute: node)?.asDeclAttribute
320317
}
321318

322319
/// E.g.:
@@ -1513,18 +1510,21 @@ extension ASTGenVisitor {
15131510
)
15141511
}
15151512

1516-
func generateCustomAttr(attribute node: AttributeSyntax, initContext: inout BridgedCustomAttributeInitializer?) -> BridgedCustomAttr? {
1513+
func generateCustomAttr(attribute node: AttributeSyntax) -> BridgedCustomAttr? {
15171514
let type = self.generate(type: node.attributeName)
15181515

15191516
let argList: BridgedArgumentList?
1517+
let initContext: BridgedCustomAttributeInitializer?
15201518
if let args = node.arguments {
15211519
guard let args = args.as(LabeledExprListSyntax.self) else {
15221520
// TODO: Diagnose?
15231521
return nil
15241522
}
15251523

1526-
if !self.declContext.isLocalContext && initContext == nil {
1524+
if !self.declContext.isLocalContext {
15271525
initContext = BridgedCustomAttributeInitializer.create(declContext: self.declContext)
1526+
} else {
1527+
initContext = nil
15281528
}
15291529
argList = withDeclContext(initContext?.asDeclContext ?? self.declContext) {
15301530
self.generateArgumentList(
@@ -1537,6 +1537,7 @@ extension ASTGenVisitor {
15371537
}
15381538
} else {
15391539
argList = nil
1540+
initContext = nil
15401541
}
15411542

15421543
return .createParsed(

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ extension ASTGenVisitor {
390390
return .underlying(attribute)
391391
}
392392
} body: { node in
393-
if let attr = self.generateDeclAttribute(attribute: node, initContext: &initContext) {
393+
if let attr = self.generateDeclAttribute(attribute: node) {
394394
attrs.add(attr)
395395
}
396396
}

lib/ASTGen/Sources/ASTGen/Exprs.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,7 @@ extension ASTGenVisitor {
232232
return .underlying(attribute)
233233
}
234234
} body: { node in
235-
var initCtx: BridgedCustomAttributeInitializer?
236-
if let attr = self.generateDeclAttribute(attribute: node, initContext: &initCtx) {
235+
if let attr = self.generateDeclAttribute(attribute: node) {
237236
result.attributes.add(attr)
238237
}
239238
}

lib/Parse/ParseDecl.cpp

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4097,8 +4097,7 @@ bool Parser::canParseCustomAttribute() {
40974097
return true;
40984098
}
40994099

4100-
ParserResult<CustomAttr> Parser::parseCustomAttribute(
4101-
SourceLoc atLoc, CustomAttributeInitializer *&initContext) {
4100+
ParserResult<CustomAttr> Parser::parseCustomAttribute(SourceLoc atLoc) {
41024101
assert(Tok.is(tok::identifier));
41034102

41044103
// Parse a custom attribute.
@@ -4115,14 +4114,14 @@ ParserResult<CustomAttr> Parser::parseCustomAttribute(
41154114
// and global variables in libraries.
41164115
ParserStatus status;
41174116
ArgumentList *argList = nullptr;
4117+
CustomAttributeInitializer *initContext = nullptr;
41184118
if (Tok.isFollowingLParen() && isCustomAttributeArgument()) {
41194119
// If we have no local context to parse the initial value into, create
41204120
// one for the attribute.
41214121
std::optional<ParseFunctionBody> initParser;
41224122
if (!CurDeclContext->isLocalContext()) {
4123-
if (!initContext)
4124-
initContext = CustomAttributeInitializer::create(CurDeclContext);
4125-
4123+
assert(!initContext);
4124+
initContext = CustomAttributeInitializer::create(CurDeclContext);
41264125
initParser.emplace(*this, initContext);
41274126
}
41284127
if (getEndOfPreviousLoc() != Tok.getLoc()) {
@@ -4173,7 +4172,6 @@ ParserResult<CustomAttr> Parser::parseCustomAttribute(
41734172
///
41744173
ParserStatus Parser::parseDeclAttribute(DeclAttributes &Attributes,
41754174
SourceLoc AtLoc, SourceLoc AtEndLoc,
4176-
CustomAttributeInitializer *&initContext,
41774175
bool isFromClangAttribute) {
41784176
if (AtEndLoc != Tok.getLoc()) {
41794177
diagnose(AtEndLoc, diag::attr_extra_whitespace_after_at)
@@ -4371,7 +4369,7 @@ ParserStatus Parser::parseDeclAttribute(DeclAttributes &Attributes,
43714369
diagnose(Tok, diag::unknown_attribute, "unknown");
43724370
} else {
43734371
// Change the context to create a custom attribute syntax.
4374-
auto customAttr = parseCustomAttribute(AtLoc, initContext);
4372+
auto customAttr = parseCustomAttribute(AtLoc);
43754373
if (auto attr = customAttr.getPtrOrNull())
43764374
Attributes.add(attr);
43774375

@@ -4388,10 +4386,9 @@ ParserStatus Parser::parseDeclAttribute(DeclAttributes &Attributes,
43884386

43894387
bool Parser::canParseTypeAttribute() {
43904388
TypeOrCustomAttr result; // ignored
4391-
CustomAttributeInitializer *initContext = nullptr;
43924389
return !parseTypeAttribute(result, /*atLoc=*/SourceLoc(),
43934390
/*atEndLoc=*/SourceLoc(),
4394-
ParseTypeReason::Unspecified, initContext,
4391+
ParseTypeReason::Unspecified,
43954392
/*justChecking*/ true)
43964393
.isError();
43974394
}
@@ -4593,7 +4590,6 @@ bool Parser::parseUUIDString(UUID &uuid, Diag<> diagnostic, bool justChecking) {
45934590
ParserStatus Parser::parseTypeAttribute(TypeOrCustomAttr &result,
45944591
SourceLoc AtLoc, SourceLoc AtEndLoc,
45954592
ParseTypeReason reason,
4596-
CustomAttributeInitializer *&initContext,
45974593
bool justChecking) {
45984594
if (AtEndLoc != Tok.getLoc()) {
45994595
diagnose(AtEndLoc, diag::attr_extra_whitespace_after_at)
@@ -4701,7 +4697,7 @@ ParserStatus Parser::parseTypeAttribute(TypeOrCustomAttr &result,
47014697
: makeParserError();
47024698

47034699
// Parse as a custom attribute.
4704-
auto customAttrResult = parseCustomAttribute(AtLoc, initContext);
4700+
auto customAttrResult = parseCustomAttribute(AtLoc);
47054701
if (customAttrResult.isParseErrorOrHasCompletion())
47064702
return customAttrResult;
47074703

@@ -5068,34 +5064,23 @@ ParserResult<LifetimeEntry> Parser::parseLifetimeEntry(SourceLoc loc) {
50685064
/// '@' attribute
50695065
/// \endverbatim
50705066
ParserStatus Parser::parseDeclAttributeList(
5071-
DeclAttributes &Attributes, bool ifConfigsAreDeclAttrs,
5072-
CustomAttributeInitializer *&initContext) {
5067+
DeclAttributes &Attributes, bool ifConfigsAreDeclAttrs) {
50735068
ParserStatus Status;
50745069
while (Tok.isAny(tok::at_sign, tok::pound_if)) {
50755070
if (Tok.is(tok::at_sign)) {
50765071
SourceLoc AtEndLoc = Tok.getRange().getEnd();
50775072
SourceLoc AtLoc = consumeToken();
5078-
Status |= parseDeclAttribute(Attributes, AtLoc, AtEndLoc, initContext);
5073+
Status |= parseDeclAttribute(Attributes, AtLoc, AtEndLoc);
50795074
} else {
50805075
if (!ifConfigsAreDeclAttrs && !ifConfigContainsOnlyAttributes())
50815076
break;
50825077

5083-
Status |= parseIfConfigAttributes(
5084-
Attributes, ifConfigsAreDeclAttrs, initContext);
5078+
Status |= parseIfConfigAttributes(Attributes, ifConfigsAreDeclAttrs);
50855079
}
50865080
}
50875081
return Status;
50885082
}
50895083

5090-
ParserStatus Parser::parseDeclAttributeList(
5091-
DeclAttributes &Attributes, bool IfConfigsAreDeclAttrs) {
5092-
if (Tok.isNot(tok::at_sign, tok::pound_if))
5093-
return makeParserSuccess();
5094-
5095-
CustomAttributeInitializer *initContext = nullptr;
5096-
return parseDeclAttributeList(Attributes, IfConfigsAreDeclAttrs, initContext);
5097-
}
5098-
50995084
// effectively parseDeclAttributeList but with selective modifier handling
51005085
ParserStatus Parser::parseClosureDeclAttributeList(DeclAttributes &Attributes) {
51015086
auto parsingNonisolated = [this] {
@@ -5106,23 +5091,21 @@ ParserStatus Parser::parseClosureDeclAttributeList(DeclAttributes &Attributes) {
51065091
if (Tok.isNot(tok::at_sign, tok::pound_if) && !parsingNonisolated())
51075092
return makeParserSuccess();
51085093

5109-
CustomAttributeInitializer *initContext = nullptr;
51105094
constexpr bool ifConfigsAreDeclAttrs = false;
51115095
ParserStatus Status;
51125096
while (Tok.isAny(tok::at_sign, tok::pound_if) || parsingNonisolated()) {
51135097
if (Tok.is(tok::at_sign)) {
51145098
SourceLoc AtEndLoc = Tok.getRange().getEnd();
51155099
SourceLoc AtLoc = consumeToken();
5116-
Status |= parseDeclAttribute(Attributes, AtLoc, AtEndLoc, initContext);
5100+
Status |= parseDeclAttribute(Attributes, AtLoc, AtEndLoc);
51175101
} else if (parsingNonisolated()) {
51185102
Status |=
51195103
parseNewDeclAttribute(Attributes, {}, DeclAttrKind::Nonisolated);
51205104
} else {
51215105
if (!ifConfigsAreDeclAttrs && !ifConfigContainsOnlyAttributes()) {
51225106
break;
51235107
}
5124-
Status |= parseIfConfigAttributes(Attributes, ifConfigsAreDeclAttrs,
5125-
initContext);
5108+
Status |= parseIfConfigAttributes(Attributes, ifConfigsAreDeclAttrs);
51265109
}
51275110
}
51285111
return Status;
@@ -5313,7 +5296,6 @@ ParserStatus Parser::parseDeclModifierList(DeclAttributes &Attributes,
53135296
/// \endverbatim
53145297
ParserStatus Parser::ParsedTypeAttributeList::slowParse(Parser &P) {
53155298
ParserStatus status;
5316-
CustomAttributeInitializer *initContext = nullptr;
53175299
auto &Tok = P.Tok;
53185300
while (P.isParameterSpecifier()) {
53195301
if (Tok.isContextualKeyword("isolated")) {
@@ -5413,7 +5395,7 @@ ParserStatus Parser::ParsedTypeAttributeList::slowParse(Parser &P) {
54135395
SourceLoc AtEndLoc = Tok.getRange().getEnd();
54145396
SourceLoc AtLoc = P.consumeToken();
54155397
status |=
5416-
P.parseTypeAttribute(result, AtLoc, AtEndLoc, ParseReason, initContext);
5398+
P.parseTypeAttribute(result, AtLoc, AtEndLoc, ParseReason);
54175399
if (status.isError())
54185400
return status;
54195401
if (result)
@@ -6098,9 +6080,8 @@ ParserStatus Parser::parseDecl(bool IsAtStartOfLineOrPreviousHadSemi,
60986080
DeclAttributes Attributes;
60996081
if (Tok.hasComment())
61006082
Attributes.add(new (Context) RawDocCommentAttr(Tok.getCommentRange()));
6101-
CustomAttributeInitializer *attrInitContext = nullptr;
61026083
ParserStatus AttrStatus = parseDeclAttributeList(
6103-
Attributes, IfConfigsAreDeclAttrs, attrInitContext);
6084+
Attributes, IfConfigsAreDeclAttrs);
61046085

61056086
// Parse modifiers.
61066087
// Keep track of where and whether we see a contextual keyword on the decl.
@@ -8624,7 +8605,6 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
86248605
topLevelParser.emplace(*this, topLevelDecl);
86258606
if (initContext)
86268607
initParser.emplace(*this, initContext);
8627-
86288608

86298609
SourceLoc EqualLoc = consumeToken(tok::equal);
86308610
PBDEntries.back().setEqualLoc(EqualLoc);

lib/Parse/ParseIfConfig.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -991,21 +991,18 @@ ParserStatus Parser::parseIfConfig(
991991
}
992992

993993
ParserStatus Parser::parseIfConfigAttributes(
994-
DeclAttributes &attributes, bool ifConfigsAreDeclAttrs,
995-
CustomAttributeInitializer *&initContext) {
994+
DeclAttributes &attributes, bool ifConfigsAreDeclAttrs) {
996995
ParserStatus status = makeParserSuccess();
997996
return parseIfConfigRaw<ParserStatus>(
998997
IfConfigContext::DeclAttrs,
999998
[&](SourceLoc clauseLoc, Expr *condition, bool isActive,
1000999
IfConfigElementsRole role) {
10011000
if (isActive) {
1002-
status |= parseDeclAttributeList(
1003-
attributes, ifConfigsAreDeclAttrs, initContext);
1001+
status |= parseDeclAttributeList(attributes, ifConfigsAreDeclAttrs);
10041002
} else if (role != IfConfigElementsRole::Skipped) {
10051003
DeclAttributes skippedAttributes;
1006-
CustomAttributeInitializer *skippedInitContext = nullptr;
10071004
status |= parseDeclAttributeList(
1008-
skippedAttributes, ifConfigsAreDeclAttrs, skippedInitContext);
1005+
skippedAttributes, ifConfigsAreDeclAttrs);
10091006
}
10101007
},
10111008
[&](SourceLoc endLoc, bool hadMissingEnd) {

0 commit comments

Comments
 (0)