Skip to content

Commit d74f235

Browse files
committed
Generalize @attached parsing and representation to also include @freestanding.
The attached and freestanding macro attributes use the same parsing logic and representation, so generalize the "attached" attribute into a more general "macro role" attribute.
1 parent f778406 commit d74f235

File tree

12 files changed

+112
-60
lines changed

12 files changed

+112
-60
lines changed

include/swift/AST/Attr.h

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,35 +2341,38 @@ class DeclarationAttr final
23412341
}
23422342
};
23432343

2344-
/// The @attached attribute, which declares that a given macro can be
2345-
/// "attached" as an attribute to declarations.
2346-
class AttachedAttr final
2344+
/// A macro role attribute, spelled with either @attached or @freestanding,
2345+
/// which declares one of the roles that a given macro can inhabit.
2346+
class MacroRoleAttr final
23472347
: public DeclAttribute,
2348-
private llvm::TrailingObjects<AttachedAttr, MacroIntroducedDeclName> {
2348+
private llvm::TrailingObjects<MacroRoleAttr, MacroIntroducedDeclName> {
23492349
friend TrailingObjects;
23502350

2351+
MacroSyntax syntax;
23512352
MacroRole role;
23522353
unsigned numNames;
23532354

2354-
AttachedAttr(SourceLoc atLoc, SourceRange range, MacroRole role,
2355-
ArrayRef<MacroIntroducedDeclName> names,
2356-
bool implicit);
2355+
MacroRoleAttr(SourceLoc atLoc, SourceRange range, MacroSyntax syntax,
2356+
MacroRole role, ArrayRef<MacroIntroducedDeclName> names,
2357+
bool implicit);
23572358

23582359
public:
2359-
static AttachedAttr *create(ASTContext &ctx, SourceLoc atLoc,
2360-
SourceRange range, MacroRole role,
2361-
ArrayRef<MacroIntroducedDeclName> names,
2362-
bool implicit);
2360+
static MacroRoleAttr *create(ASTContext &ctx, SourceLoc atLoc,
2361+
SourceRange range, MacroSyntax syntax,
2362+
MacroRole role,
2363+
ArrayRef<MacroIntroducedDeclName> names,
2364+
bool implicit);
23632365

23642366
size_t numTrailingObjects(OverloadToken<MacroIntroducedDeclName>) const {
23652367
return numNames;
23662368
}
23672369

2370+
MacroSyntax getMacroSyntax() const { return syntax; }
23682371
MacroRole getMacroRole() const { return role; }
23692372
ArrayRef<MacroIntroducedDeclName> getNames() const;
23702373

23712374
static bool classof(const DeclAttribute *DA) {
2372-
return DA->getKind() == DAK_Attached;
2375+
return DA->getKind() == DAK_MacroRole;
23732376
}
23742377
};
23752378

include/swift/AST/MacroDeclaration.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@
2121

2222
namespace swift {
2323

24+
/// Describes the syntax that is used for a macro, which affects its role.
25+
enum class MacroSyntax: uint8_t {
26+
/// Freestanding macro syntax starting with an explicit '#' followed by the
27+
/// macro name and optional arguments.
28+
Freestanding,
29+
30+
/// Attached macro syntax written as an attribute with a leading `@` followed
31+
/// by the macro name an optional arguments.
32+
Attached,
33+
};
34+
2435
/// The context in which a macro can be used, which determines the syntax it
2536
/// uses.
2637
enum class MacroRole: uint32_t {

include/swift/Parse/Parser.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,9 +1105,11 @@ class Parser {
11051105
ParserResult<DeclarationAttr> parseDeclarationAttribute(SourceLoc AtLoc,
11061106
SourceLoc Loc);
11071107

1108-
/// Parse the @attached attribute.
1109-
ParserResult<AttachedAttr> parseAttachedAttribute(SourceLoc AtLoc,
1110-
SourceLoc Loc);
1108+
/// Parse the @attached or @freestanding attribute that specifies a macro
1109+
/// role.
1110+
ParserResult<MacroRoleAttr> parseMacroRoleAttribute(
1111+
MacroSyntax syntax, SourceLoc AtLoc, SourceLoc Loc
1112+
);
11111113

11121114
/// Parse a specific attribute.
11131115
ParserStatus parseDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,

lib/AST/Attr.cpp

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,10 +1320,18 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
13201320
break;
13211321
}
13221322

1323-
case DAK_Attached: {
1324-
Printer.printAttrName("@attached");
1323+
case DAK_MacroRole: {
1324+
auto Attr = cast<MacroRoleAttr>(this);
1325+
switch (Attr->getMacroSyntax()) {
1326+
case MacroSyntax::Freestanding:
1327+
Printer.printAttrName("@freestanding");
1328+
break;
1329+
1330+
case MacroSyntax::Attached:
1331+
Printer.printAttrName("@attached");
1332+
break;
1333+
}
13251334
Printer << "(";
1326-
auto Attr = cast<AttachedAttr>(this);
13271335
Printer << getMacroRoleString(Attr->getMacroRole());
13281336
if (!Attr->getNames().empty()) {
13291337
Printer << ", names: ";
@@ -1520,8 +1528,14 @@ StringRef DeclAttribute::getAttrName() const {
15201528
return "_documentation";
15211529
case DAK_Declaration:
15221530
return "declaration";
1523-
case DAK_Attached:
1524-
return "attached";
1531+
case DAK_MacroRole:
1532+
switch (cast<MacroRoleAttr>(this)->getMacroSyntax()) {
1533+
case MacroSyntax::Freestanding:
1534+
return "freestanding";
1535+
1536+
case MacroSyntax::Attached:
1537+
return "attached";
1538+
}
15251539
}
15261540
llvm_unreachable("bad DeclAttrKind");
15271541
}
@@ -2405,27 +2419,27 @@ ArrayRef<MacroIntroducedDeclName> DeclarationAttr::getMemberNames() const {
24052419
};
24062420
}
24072421

2408-
AttachedAttr::AttachedAttr(SourceLoc atLoc, SourceRange range,
2409-
MacroRole role,
2410-
ArrayRef<MacroIntroducedDeclName> names,
2411-
bool implicit)
2412-
: DeclAttribute(DAK_Attached, atLoc, range, implicit),
2413-
role(role), numNames(names.size()) {
2422+
MacroRoleAttr::MacroRoleAttr(SourceLoc atLoc, SourceRange range,
2423+
MacroSyntax syntax, MacroRole role,
2424+
ArrayRef<MacroIntroducedDeclName> names,
2425+
bool implicit)
2426+
: DeclAttribute(DAK_MacroRole, atLoc, range, implicit),
2427+
syntax(syntax), role(role), numNames(names.size()) {
24142428
auto *trailingNamesBuffer = getTrailingObjects<MacroIntroducedDeclName>();
24152429
std::uninitialized_copy(names.begin(), names.end(), trailingNamesBuffer);
24162430
}
24172431

2418-
AttachedAttr *
2419-
AttachedAttr::create(ASTContext &ctx, SourceLoc atLoc, SourceRange range,
2420-
MacroRole role,
2421-
ArrayRef<MacroIntroducedDeclName> names,
2422-
bool implicit) {
2432+
MacroRoleAttr *
2433+
MacroRoleAttr::create(ASTContext &ctx, SourceLoc atLoc, SourceRange range,
2434+
MacroSyntax syntax, MacroRole role,
2435+
ArrayRef<MacroIntroducedDeclName> names,
2436+
bool implicit) {
24232437
unsigned size = totalSizeToAlloc<MacroIntroducedDeclName>(names.size());
2424-
auto *mem = ctx.Allocate(size, alignof(AttachedAttr));
2425-
return new (mem) AttachedAttr(atLoc, range, role, names, implicit);
2438+
auto *mem = ctx.Allocate(size, alignof(MacroRoleAttr));
2439+
return new (mem) MacroRoleAttr(atLoc, range, syntax, role, names, implicit);
24262440
}
24272441

2428-
ArrayRef<MacroIntroducedDeclName> AttachedAttr::getNames() const {
2442+
ArrayRef<MacroIntroducedDeclName> MacroRoleAttr::getNames() const {
24292443
return {
24302444
getTrailingObjects<MacroIntroducedDeclName>(),
24312445
numNames

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9829,7 +9829,7 @@ MacroRoles MacroDecl::getMacroRoles() const {
98299829
contexts |= MacroRole::Expression;
98309830
for (auto attr : getAttrs().getAttributes<DeclarationAttr>())
98319831
contexts |= attr->getMacroRole();
9832-
for (auto attr : getAttrs().getAttributes<AttachedAttr>())
9832+
for (auto attr : getAttrs().getAttributes<MacroRoleAttr>())
98339833
contexts |= attr->getMacroRole();
98349834
return contexts;
98359835
}

lib/Parse/ParseDecl.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2377,9 +2377,24 @@ static SmallVector<MacroIntroducedDeclName, 2> getMacroIntroducedNames(
23772377
return names;
23782378
}
23792379

2380-
ParserResult<AttachedAttr>
2381-
Parser::parseAttachedAttribute(SourceLoc AtLoc, SourceLoc Loc) {
2382-
StringRef attrName = "attached";
2380+
ParserResult<MacroRoleAttr>
2381+
Parser::parseMacroRoleAttribute(
2382+
MacroSyntax syntax, SourceLoc AtLoc, SourceLoc Loc)
2383+
{
2384+
StringRef attrName;
2385+
bool isAttached;
2386+
switch (syntax) {
2387+
case MacroSyntax::Freestanding:
2388+
attrName = "freestanding";
2389+
isAttached = false;
2390+
break;
2391+
2392+
case MacroSyntax::Attached:
2393+
attrName = "attached";
2394+
isAttached = true;
2395+
break;
2396+
}
2397+
23832398
if (!Tok.isFollowingLParen()) {
23842399
diagnose(Tok, diag::attr_expected_lparen, attrName, false);
23852400
return makeParserError();
@@ -2397,15 +2412,15 @@ Parser::parseAttachedAttribute(SourceLoc AtLoc, SourceLoc Loc) {
23972412
ArgumentList *argList = argListResult.get();
23982413

23992414
// Figure out the role.
2400-
auto role = getMacroRole(Diags, argList, /*attached=*/true);
2415+
auto role = getMacroRole(Diags, argList, isAttached);
24012416
if (!role)
24022417
return makeParserError();
24032418

2404-
auto names = getMacroIntroducedNames(Diags, argList, /*attached=*/true);
2419+
auto names = getMacroIntroducedNames(Diags, argList, isAttached);
24052420

24062421
SourceRange range(Loc, argList->getEndLoc());
2407-
return makeParserResult(AttachedAttr::create(
2408-
Context, AtLoc, range, *role, names, /*isImplicit*/ false));
2422+
return makeParserResult(MacroRoleAttr::create(
2423+
Context, AtLoc, range, syntax, *role, names, /*isImplicit*/ false));
24092424
}
24102425

24112426
/// Guts of \c parseSingleAttrOption and \c parseSingleAttrOptionIdentifier.
@@ -3406,8 +3421,10 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
34063421
return false;
34073422
break;
34083423
}
3409-
case DAK_Attached: {
3410-
auto Attr = parseAttachedAttribute(AtLoc, Loc);
3424+
case DAK_MacroRole: {
3425+
auto syntax = (AttrName == "freestanding" ? MacroSyntax::Freestanding
3426+
: MacroSyntax::Attached);
3427+
auto Attr = parseMacroRoleAttribute(syntax, AtLoc, Loc);
34113428
if (Attr.isNonNull())
34123429
Attributes.add(Attr.get());
34133430
else

lib/Sema/TypeCheckAttr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
162162
IGNORED_ATTR(Documentation)
163163
IGNORED_ATTR(Expression)
164164
IGNORED_ATTR(Declaration)
165-
IGNORED_ATTR(Attached)
165+
IGNORED_ATTR(MacroRole)
166166
#undef IGNORED_ATTR
167167

168168
void visitAlignmentAttr(AlignmentAttr *attr) {

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,7 @@ namespace {
16281628

16291629
UNINTERESTING_ATTR(Expression)
16301630
UNINTERESTING_ATTR(Declaration)
1631-
UNINTERESTING_ATTR(Attached)
1631+
UNINTERESTING_ATTR(MacroRole)
16321632
#undef UNINTERESTING_ATTR
16331633

16341634
void visitAvailableAttr(AvailableAttr *attr) {

lib/Serialization/Deserialization.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5449,14 +5449,15 @@ llvm::Error DeclDeserializer::deserializeDeclCommon() {
54495449
break;
54505450
}
54515451

5452-
case decls_block::Attached_DECL_ATTR: {
5452+
case decls_block::MacroRole_DECL_ATTR: {
54535453
bool isImplicit;
5454+
uint8_t rawMacroSyntax;
54545455
uint8_t rawMacroRole;
54555456
uint64_t numNames;
54565457
ArrayRef<uint64_t> introducedDeclNames;
5457-
serialization::decls_block::AttachedDeclAttrLayout::
5458-
readRecord(scratch, isImplicit, rawMacroRole, numNames,
5459-
introducedDeclNames);
5458+
serialization::decls_block::MacroRoleDeclAttrLayout::
5459+
readRecord(scratch, isImplicit, rawMacroSyntax, rawMacroRole,
5460+
numNames, introducedDeclNames);
54605461
auto role = *getActualMacroRole(rawMacroRole);
54615462
if (introducedDeclNames.size() != numNames * 2)
54625463
return MF.diagnoseFatal();
@@ -5468,8 +5469,9 @@ llvm::Error DeclDeserializer::deserializeDeclCommon() {
54685469
MF.getIdentifier(IdentifierID(introducedDeclNames[i + 1]));
54695470
names.push_back(MacroIntroducedDeclName(*kind, identifier));
54705471
}
5471-
Attr = AttachedAttr::create(
5472-
ctx, SourceLoc(), SourceRange(), role, names, isImplicit);
5472+
Attr = MacroRoleAttr::create(
5473+
ctx, SourceLoc(), SourceRange(),
5474+
static_cast<MacroSyntax>(rawMacroSyntax), role, names, isImplicit);
54735475
break;
54745476
}
54755477

lib/Serialization/ModuleFormat.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,10 +2226,11 @@ namespace decls_block {
22262226
BCArray<IdentifierIDField> // introduced decl name kind and identifier pairs
22272227
>;
22282228

2229-
using AttachedDeclAttrLayout = BCRecordLayout<
2230-
Attached_DECL_ATTR,
2229+
using MacroRoleDeclAttrLayout = BCRecordLayout<
2230+
MacroRole_DECL_ATTR,
22312231
BCFixed<1>, // implicit flag
2232-
MacroRoleField, // macro roles
2232+
BCFixed<1>, // macro syntax
2233+
MacroRoleField, // macro role
22332234
BCVBR<5>, // number of names
22342235
BCArray<IdentifierIDField> // introduced decl name kind and identifier pairs
22352236
>;

0 commit comments

Comments
 (0)