Skip to content

[5.9] [Macros] Improve parsing, representation, and serialization of role attributes #65363

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
15 changes: 7 additions & 8 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -2028,8 +2028,6 @@ ERROR(foreign_diagnostic,none,
//------------------------------------------------------------------------------
// MARK: macros
//------------------------------------------------------------------------------
ERROR(expected_macro_value_type,PointsToFirstBadToken,
"expected macro value type following ':'", ())
ERROR(expected_lparen_macro,PointsToFirstBadToken,
"expected '(' for macro parameters or ':' for a value-like macro", ())
ERROR(expected_type_macro_result,PointsToFirstBadToken,
Expand All @@ -2042,12 +2040,13 @@ ERROR(macro_expansion_expr_expected_macro_identifier,PointsToFirstBadToken,
ERROR(macro_expansion_decl_expected_macro_identifier,PointsToFirstBadToken,
"expected a macro identifier for a pound literal declaration", ())

ERROR(declaration_attr_expected_kind,PointsToFirstBadToken,
"expected a declaration macro kind ('freestanding' or 'attached')", ())

ERROR(macro_role_attr_expected_kind,PointsToFirstBadToken,
"expected %select{a freestanding|an attached}0 macro role such as "
"%select{'expression'|'accessor'}0", (bool))
ERROR(macro_role_attr_expected_attached_kind,PointsToFirstBadToken,
"expected an attached macro role such as 'peer'", ())
ERROR(macro_role_attr_expected_freestanding_kind,PointsToFirstBadToken,
"expected a freestanding macro role such as 'expression'", ())
ERROR(macro_role_syntax_mismatch,PointsToFirstBadToken,
"%select{a freestanding|an attached}0 macro cannot have the %1 role",
(bool, Identifier))
Expand All @@ -2056,14 +2055,14 @@ ERROR(macro_attribute_unknown_label,PointsToFirstBadToken,
(bool, Identifier))
ERROR(macro_attribute_duplicate_label,PointsToFirstBadToken,
"@%select{freestanding|attached}0 already has an argument with "
"label %1", (bool, Identifier))
ERROR(macro_attribute_missing_label,PointsToFirstBadToken,
"label %1", (bool, StringRef))
ERROR(macro_attribute_missing_label,none,
"@%select{freestanding|attached}0 argument is missing label '%1'",
(bool, StringRef))
ERROR(macro_attribute_unknown_name_kind,PointsToFirstBadToken,
"unknown introduced name kind %0", (Identifier))
ERROR(macro_attribute_unknown_argument_form,PointsToFirstBadToken,
"introduced name argument should be an identifier", ())
"introduced name argument should be a name", ())
ERROR(macro_attribute_introduced_name_requires_argument,PointsToFirstBadToken,
"introduced name kind %0 requires a single argument '(name)'", (Identifier))
ERROR(macro_attribute_introduced_name_requires_no_argument,PointsToFirstBadToken,
Expand Down
10 changes: 5 additions & 5 deletions include/swift/AST/MacroDeclaration.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ class MacroIntroducedDeclName {

private:
Kind kind;
Identifier identifier;
DeclName name;

public:
MacroIntroducedDeclName(Kind kind, Identifier identifier = Identifier())
: kind(kind), identifier(identifier) {};
MacroIntroducedDeclName(Kind kind, DeclName name = DeclName())
: kind(kind), name(name) {};

static MacroIntroducedDeclName getNamed(Identifier name) {
static MacroIntroducedDeclName getNamed(DeclName name) {
return MacroIntroducedDeclName(Kind::Named, name);
}

Expand All @@ -138,7 +138,7 @@ class MacroIntroducedDeclName {
}

Kind getKind() const { return kind; }
Identifier getIdentifier() const { return identifier; }
DeclName getName() const { return name; }
};

}
Expand Down
6 changes: 3 additions & 3 deletions lib/AST/Attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1382,9 +1382,9 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
[&](MacroIntroducedDeclName name) {
Printer << getMacroIntroducedDeclNameString(name.getKind());
if (macroIntroducedNameRequiresArgument(name.getKind())) {
StringRef nameText = name.getIdentifier().str();
bool shouldEscape = escapeKeywordInContext(
nameText, PrintNameContext::Normal) || nameText == "$";
SmallString<32> buffer;
StringRef nameText = name.getName().getString(buffer);
bool shouldEscape = nameText == "$";
Printer << "(";
if (shouldEscape)
Printer << "`";
Expand Down
6 changes: 3 additions & 3 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10256,7 +10256,7 @@ void MacroDecl::getIntroducedNames(MacroRole role, ValueDecl *attachedTo,
for (auto expandedName : attr->getNames()) {
switch (expandedName.getKind()) {
case MacroIntroducedDeclNameKind::Named: {
names.push_back(DeclName(expandedName.getIdentifier()));
names.push_back(DeclName(expandedName.getName()));
break;
}

Expand All @@ -10276,7 +10276,7 @@ void MacroDecl::getIntroducedNames(MacroRole role, ValueDecl *attachedTo,
std::string prefixedName;
{
llvm::raw_string_ostream out(prefixedName);
out << expandedName.getIdentifier();
out << expandedName.getName();
out << baseName.getIdentifier();
}

Expand All @@ -10294,7 +10294,7 @@ void MacroDecl::getIntroducedNames(MacroRole role, ValueDecl *attachedTo,
{
llvm::raw_string_ostream out(suffixedName);
out << baseName.getIdentifier();
out << expandedName.getIdentifier();
out << expandedName.getName();
}

Identifier nameId = ctx.getIdentifier(suffixedName);
Expand Down
Loading