Skip to content

[5.9][Macro] Attributes for freestanding macro expansion #65990

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
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
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -2056,6 +2056,8 @@ ERROR(macro_expansion_expr_expected_macro_identifier,PointsToFirstBadToken,
"expected a macro identifier for a pound literal expression", ())
ERROR(macro_expansion_decl_expected_macro_identifier,PointsToFirstBadToken,
"expected a macro identifier for a pound literal declaration", ())
ERROR(extra_whitespace_macro_expansion_identifier,PointsToFirstBadToken,
"extraneous whitespace between '#' and macro name is not permitted", ())

ERROR(macro_role_attr_expected_kind,PointsToFirstBadToken,
"expected %select{a freestanding|an attached}0 macro role such as "
Expand Down
16 changes: 15 additions & 1 deletion include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -900,11 +900,15 @@ class Parser {
// Decl Parsing

/// Returns true if parser is at the start of a Swift decl or decl-import.
bool isStartOfSwiftDecl(bool allowPoundIfAttributes = true);
bool isStartOfSwiftDecl(bool allowPoundIfAttributes = true,
bool hadAttrsOrModifiers = false);

/// Returns true if the parser is at the start of a SIL decl.
bool isStartOfSILDecl();

/// Returns true if the parser is at a freestanding macro expansion.
bool isStartOfFreestandingMacroExpansion();

/// Parse the top-level Swift items into the provided vector.
///
/// Each item will be a declaration, statement, or expression.
Expand Down Expand Up @@ -1747,6 +1751,16 @@ class Parser {
DeclNameRef parseDeclNameRef(DeclNameLoc &loc, const Diagnostic &diag,
DeclNameOptions flags);

/// Parse macro expansion.
///
/// macro-expansion:
/// '#' identifier generic-arguments? expr-paren? trailing-closure?
ParserStatus parseFreestandingMacroExpansion(
SourceLoc &poundLoc, DeclNameLoc &macroNameLoc, DeclNameRef &macroNameRef,
SourceLoc &leftAngleLoc, SmallVectorImpl<TypeRepr *> &genericArgs,
SourceLoc &rightAngleLoc, ArgumentList *&argList, bool isExprBasic,
const Diagnostic &diag);

ParserResult<Expr> parseExprIdentifier();
Expr *parseExprEditorPlaceholder(Token PlaceholderTok,
Identifier PlaceholderId);
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1482,7 +1482,7 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,

bool shouldSkip(Decl *D) {
if (!Walker.shouldWalkMacroArgumentsAndExpansion().second &&
D->isInMacroExpansionInContext())
D->isInMacroExpansionInContext() && !Walker.Parent.isNull())
return true;

if (auto *VD = dyn_cast<VarDecl>(D)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/Attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ OrigDeclAttrFilter::operator()(const DeclAttribute *Attr) const {
auto declLoc = decl->getStartLoc();
auto *mod = decl->getModuleContext();
auto *declFile = mod->getSourceFileContainingLocation(declLoc);
auto *attrFile = mod->getSourceFileContainingLocation(Attr->AtLoc);
auto *attrFile = mod->getSourceFileContainingLocation(Attr->getLocation());
if (!declFile || !attrFile)
return Attr;

Expand Down
8 changes: 8 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "swift/AST/Decl.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTMangler.h"
#include "swift/AST/ASTPrinter.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/AccessRequests.h"
#include "swift/AST/AccessScope.h"
Expand Down Expand Up @@ -10065,6 +10066,13 @@ void swift::simple_display(llvm::raw_ostream &out, const Decl *decl) {
typeRepr->print(out);
else
ext->getSelfNominalTypeDecl()->dumpRef(out);
} else if (auto med = dyn_cast<MacroExpansionDecl>(decl)) {
out << '#' << med->getMacroName() << " in ";
printContext(out, med->getDeclContext());
if (med->getLoc().isValid()) {
out << '@';
med->getLoc().print(out, med->getASTContext().SourceMgr);
}
} else {
out << "(unknown decl)";
}
Expand Down
1 change: 1 addition & 0 deletions lib/ASTGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ add_pure_swift_host_library(swiftASTGen STATIC
SwiftSyntax::SwiftParserDiagnostics
SwiftSyntax::SwiftSyntax
SwiftSyntax::SwiftSyntaxMacros
SwiftSyntax::SwiftSyntaxMacroExpansion
swiftLLVMJSON
)
2 changes: 1 addition & 1 deletion lib/ASTGen/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ let package = Package(
.product(name: "SwiftOperators", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacroExpansion", package: "swift-syntax"),
"swiftLLVMJSON"
],
path: "Sources/ASTGen",
exclude: ["CMakeLists.txt"],
swiftSettings: swiftSetttings
),
.target(
Expand Down
Loading