Skip to content

Commit a0f043d

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 c3fbc6e commit a0f043d

File tree

5 files changed

+30
-68
lines changed

5 files changed

+30
-68
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

@@ -1163,7 +1155,6 @@ class Parser {
11631155
/// Parse a specific attribute.
11641156
ParserStatus parseDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
11651157
SourceLoc AtEndLoc,
1166-
CustomAttributeInitializer *&initContext,
11671158
bool isFromClangAttribute = false);
11681159

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

11831168
ParserStatus parseNewDeclAttribute(DeclAttributes &Attributes,
11841169
SourceLoc AtLoc, DeclAttrKind DK,
@@ -1480,7 +1465,6 @@ class Parser {
14801465

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

14861470
ParserResult<TypeRepr> parseOldStyleProtocolComposition();

lib/ASTGen/Sources/ASTGen/DeclAttrs.swift

Lines changed: 10 additions & 8 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 {
@@ -64,7 +63,7 @@ extension ASTGenVisitor {
6463
return .underlying(attribute)
6564
}
6665
} body: { attribute in
67-
addAttribute(self.generateDeclAttribute(attribute: attribute, initContext: &initContext))
66+
addAttribute(self.generateDeclAttribute(attribute: attribute))
6867
}
6968

7069
func genStatic(node: DeclModifierSyntax, spelling: BridgedStaticSpelling) {
@@ -91,15 +90,14 @@ extension ASTGenVisitor {
9190
return DeclAttributesResult(
9291
attributes: attrs,
9392
staticSpelling: staticSpelling,
94-
staticLoc: staticLoc,
95-
initContext: initContext
93+
staticLoc: staticLoc
9694
)
9795
}
9896
}
9997

10098
// MARK: - Decl attributes
10199
extension ASTGenVisitor {
102-
func generateDeclAttribute(attribute node: AttributeSyntax, initContext: inout BridgedCustomAttributeInitializer?) -> BridgedDeclAttribute? {
100+
func generateDeclAttribute(attribute node: AttributeSyntax) -> BridgedDeclAttribute? {
103101
if let identTy = node.attributeName.as(IdentifierTypeSyntax.self) {
104102
let attrName = identTy.name.rawText
105103
let attrKind = BridgedDeclAttrKind(from: attrName.bridged)
@@ -321,7 +319,7 @@ extension ASTGenVisitor {
321319
}
322320
}
323321

324-
return self.generateCustomAttr(attribute: node, initContext: &initContext)?.asDeclAttribute
322+
return self.generateCustomAttr(attribute: node)?.asDeclAttribute
325323
}
326324

327325
func generateAlignmentAttr(attribute node: AttributeSyntax) -> BridgedAlignmentAttr? {
@@ -1119,18 +1117,21 @@ extension ASTGenVisitor {
11191117
)
11201118
}
11211119

1122-
func generateCustomAttr(attribute node: AttributeSyntax, initContext: inout BridgedCustomAttributeInitializer?) -> BridgedCustomAttr? {
1120+
func generateCustomAttr(attribute node: AttributeSyntax) -> BridgedCustomAttr? {
11231121
let type = self.generate(type: node.attributeName)
11241122

11251123
let argList: BridgedArgumentList?
1124+
let initContext: BridgedCustomAttributeInitializer?
11261125
if let args = node.arguments {
11271126
guard let args = args.as(LabeledExprListSyntax.self) else {
11281127
// TODO: Diagnose?
11291128
return nil
11301129
}
11311130

1132-
if !self.declContext.isLocalContext && initContext == nil {
1131+
if !self.declContext.isLocalContext {
11331132
initContext = BridgedCustomAttributeInitializer.create(declContext: self.declContext)
1133+
} else {
1134+
initContext = nil
11341135
}
11351136
argList = withDeclContext(initContext?.asDeclContext ?? self.declContext) {
11361137
self.generateArgumentList(
@@ -1143,6 +1144,7 @@ extension ASTGenVisitor {
11431144
}
11441145
} else {
11451146
argList = nil
1147+
initContext = nil
11461148
}
11471149

11481150
return .createParsed(

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
@@ -4115,8 +4115,7 @@ bool Parser::canParseCustomAttribute() {
41154115
return true;
41164116
}
41174117

4118-
ParserResult<CustomAttr> Parser::parseCustomAttribute(
4119-
SourceLoc atLoc, CustomAttributeInitializer *&initContext) {
4118+
ParserResult<CustomAttr> Parser::parseCustomAttribute(SourceLoc atLoc) {
41204119
assert(Tok.is(tok::identifier));
41214120

41224121
// Parse a custom attribute.
@@ -4133,14 +4132,14 @@ ParserResult<CustomAttr> Parser::parseCustomAttribute(
41334132
// and global variables in libraries.
41344133
ParserStatus status;
41354134
ArgumentList *argList = nullptr;
4135+
CustomAttributeInitializer *initContext = nullptr;
41364136
if (Tok.isFollowingLParen() && isCustomAttributeArgument()) {
41374137
// If we have no local context to parse the initial value into, create
41384138
// one for the attribute.
41394139
std::optional<ParseFunctionBody> initParser;
41404140
if (!CurDeclContext->isLocalContext()) {
4141-
if (!initContext)
4142-
initContext = CustomAttributeInitializer::create(CurDeclContext);
4143-
4141+
assert(!initContext);
4142+
initContext = CustomAttributeInitializer::create(CurDeclContext);
41444143
initParser.emplace(*this, initContext);
41454144
}
41464145
if (getEndOfPreviousLoc() != Tok.getLoc()) {
@@ -4191,7 +4190,6 @@ ParserResult<CustomAttr> Parser::parseCustomAttribute(
41914190
///
41924191
ParserStatus Parser::parseDeclAttribute(DeclAttributes &Attributes,
41934192
SourceLoc AtLoc, SourceLoc AtEndLoc,
4194-
CustomAttributeInitializer *&initContext,
41954193
bool isFromClangAttribute) {
41964194
if (AtEndLoc != Tok.getLoc()) {
41974195
diagnose(AtEndLoc, diag::attr_extra_whitespace_after_at)
@@ -4389,7 +4387,7 @@ ParserStatus Parser::parseDeclAttribute(DeclAttributes &Attributes,
43894387
diagnose(Tok, diag::unknown_attribute, "unknown");
43904388
} else {
43914389
// Change the context to create a custom attribute syntax.
4392-
auto customAttr = parseCustomAttribute(AtLoc, initContext);
4390+
auto customAttr = parseCustomAttribute(AtLoc);
43934391
if (auto attr = customAttr.getPtrOrNull())
43944392
Attributes.add(attr);
43954393

@@ -4406,10 +4404,9 @@ ParserStatus Parser::parseDeclAttribute(DeclAttributes &Attributes,
44064404

44074405
bool Parser::canParseTypeAttribute() {
44084406
TypeOrCustomAttr result; // ignored
4409-
CustomAttributeInitializer *initContext = nullptr;
44104407
return !parseTypeAttribute(result, /*atLoc=*/SourceLoc(),
44114408
/*atEndLoc=*/SourceLoc(),
4412-
ParseTypeReason::Unspecified, initContext,
4409+
ParseTypeReason::Unspecified,
44134410
/*justChecking*/ true)
44144411
.isError();
44154412
}
@@ -4611,7 +4608,6 @@ bool Parser::parseUUIDString(UUID &uuid, Diag<> diagnostic, bool justChecking) {
46114608
ParserStatus Parser::parseTypeAttribute(TypeOrCustomAttr &result,
46124609
SourceLoc AtLoc, SourceLoc AtEndLoc,
46134610
ParseTypeReason reason,
4614-
CustomAttributeInitializer *&initContext,
46154611
bool justChecking) {
46164612
if (AtEndLoc != Tok.getLoc()) {
46174613
diagnose(AtEndLoc, diag::attr_extra_whitespace_after_at)
@@ -4719,7 +4715,7 @@ ParserStatus Parser::parseTypeAttribute(TypeOrCustomAttr &result,
47194715
: makeParserError();
47204716

47214717
// Parse as a custom attribute.
4722-
auto customAttrResult = parseCustomAttribute(AtLoc, initContext);
4718+
auto customAttrResult = parseCustomAttribute(AtLoc);
47234719
if (customAttrResult.isParseErrorOrHasCompletion())
47244720
return customAttrResult;
47254721

@@ -5086,34 +5082,23 @@ ParserResult<LifetimeEntry> Parser::parseLifetimeEntry(SourceLoc loc) {
50865082
/// '@' attribute
50875083
/// \endverbatim
50885084
ParserStatus Parser::parseDeclAttributeList(
5089-
DeclAttributes &Attributes, bool ifConfigsAreDeclAttrs,
5090-
CustomAttributeInitializer *&initContext) {
5085+
DeclAttributes &Attributes, bool ifConfigsAreDeclAttrs) {
50915086
ParserStatus Status;
50925087
while (Tok.isAny(tok::at_sign, tok::pound_if)) {
50935088
if (Tok.is(tok::at_sign)) {
50945089
SourceLoc AtEndLoc = Tok.getRange().getEnd();
50955090
SourceLoc AtLoc = consumeToken();
5096-
Status |= parseDeclAttribute(Attributes, AtLoc, AtEndLoc, initContext);
5091+
Status |= parseDeclAttribute(Attributes, AtLoc, AtEndLoc);
50975092
} else {
50985093
if (!ifConfigsAreDeclAttrs && !ifConfigContainsOnlyAttributes())
50995094
break;
51005095

5101-
Status |= parseIfConfigAttributes(
5102-
Attributes, ifConfigsAreDeclAttrs, initContext);
5096+
Status |= parseIfConfigAttributes(Attributes, ifConfigsAreDeclAttrs);
51035097
}
51045098
}
51055099
return Status;
51065100
}
51075101

5108-
ParserStatus Parser::parseDeclAttributeList(
5109-
DeclAttributes &Attributes, bool IfConfigsAreDeclAttrs) {
5110-
if (Tok.isNot(tok::at_sign, tok::pound_if))
5111-
return makeParserSuccess();
5112-
5113-
CustomAttributeInitializer *initContext = nullptr;
5114-
return parseDeclAttributeList(Attributes, IfConfigsAreDeclAttrs, initContext);
5115-
}
5116-
51175102
// effectively parseDeclAttributeList but with selective modifier handling
51185103
ParserStatus Parser::parseClosureDeclAttributeList(DeclAttributes &Attributes) {
51195104
auto parsingNonisolated = [this] {
@@ -5124,23 +5109,21 @@ ParserStatus Parser::parseClosureDeclAttributeList(DeclAttributes &Attributes) {
51245109
if (Tok.isNot(tok::at_sign, tok::pound_if) && !parsingNonisolated())
51255110
return makeParserSuccess();
51265111

5127-
CustomAttributeInitializer *initContext = nullptr;
51285112
constexpr bool ifConfigsAreDeclAttrs = false;
51295113
ParserStatus Status;
51305114
while (Tok.isAny(tok::at_sign, tok::pound_if) || parsingNonisolated()) {
51315115
if (Tok.is(tok::at_sign)) {
51325116
SourceLoc AtEndLoc = Tok.getRange().getEnd();
51335117
SourceLoc AtLoc = consumeToken();
5134-
Status |= parseDeclAttribute(Attributes, AtLoc, AtEndLoc, initContext);
5118+
Status |= parseDeclAttribute(Attributes, AtLoc, AtEndLoc);
51355119
} else if (parsingNonisolated()) {
51365120
Status |=
51375121
parseNewDeclAttribute(Attributes, {}, DeclAttrKind::Nonisolated);
51385122
} else {
51395123
if (!ifConfigsAreDeclAttrs && !ifConfigContainsOnlyAttributes()) {
51405124
break;
51415125
}
5142-
Status |= parseIfConfigAttributes(Attributes, ifConfigsAreDeclAttrs,
5143-
initContext);
5126+
Status |= parseIfConfigAttributes(Attributes, ifConfigsAreDeclAttrs);
51445127
}
51455128
}
51465129
return Status;
@@ -5331,7 +5314,6 @@ ParserStatus Parser::parseDeclModifierList(DeclAttributes &Attributes,
53315314
/// \endverbatim
53325315
ParserStatus Parser::ParsedTypeAttributeList::slowParse(Parser &P) {
53335316
ParserStatus status;
5334-
CustomAttributeInitializer *initContext = nullptr;
53355317
auto &Tok = P.Tok;
53365318
while (P.isParameterSpecifier()) {
53375319
if (Tok.isContextualKeyword("isolated")) {
@@ -5431,7 +5413,7 @@ ParserStatus Parser::ParsedTypeAttributeList::slowParse(Parser &P) {
54315413
SourceLoc AtEndLoc = Tok.getRange().getEnd();
54325414
SourceLoc AtLoc = P.consumeToken();
54335415
status |=
5434-
P.parseTypeAttribute(result, AtLoc, AtEndLoc, ParseReason, initContext);
5416+
P.parseTypeAttribute(result, AtLoc, AtEndLoc, ParseReason);
54355417
if (status.isError())
54365418
return status;
54375419
if (result)
@@ -6116,9 +6098,8 @@ ParserStatus Parser::parseDecl(bool IsAtStartOfLineOrPreviousHadSemi,
61166098
DeclAttributes Attributes;
61176099
if (Tok.hasComment())
61186100
Attributes.add(new (Context) RawDocCommentAttr(Tok.getCommentRange()));
6119-
CustomAttributeInitializer *attrInitContext = nullptr;
61206101
ParserStatus AttrStatus = parseDeclAttributeList(
6121-
Attributes, IfConfigsAreDeclAttrs, attrInitContext);
6102+
Attributes, IfConfigsAreDeclAttrs);
61226103

61236104
// Parse modifiers.
61246105
// Keep track of where and whether we see a contextual keyword on the decl.
@@ -8642,7 +8623,6 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
86428623
topLevelParser.emplace(*this, topLevelDecl);
86438624
if (initContext)
86448625
initParser.emplace(*this, initContext);
8645-
86468626

86478627
SourceLoc EqualLoc = consumeToken(tok::equal);
86488628
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)