Skip to content

Commit 1a124e7

Browse files
committed
[Macros] Implement AST printing and module interface generation for macros
1 parent 4ce0834 commit 1a124e7

File tree

6 files changed

+87
-12
lines changed

6 files changed

+87
-12
lines changed

include/swift/AST/Decl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8316,6 +8316,9 @@ class MacroDecl : public GenericContext, public ValueDecl {
83168316

83178317
SourceRange getSourceRange() const;
83188318

8319+
/// Retrieve the interface type produced when expanding this macro.
8320+
Type getResultInterfaceType() const;
8321+
83198322
static bool classof(const DeclContext *C) {
83208323
if (auto D = C->getAsDecl())
83218324
return classof(D);

lib/AST/ASTPrinter.cpp

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,10 +1899,6 @@ bool ShouldPrintChecker::shouldPrint(const Decl *D,
18991899
return true;
19001900
}
19011901

1902-
// Skip macros, which don't have an in-source representation.
1903-
if (isa<MacroDecl>(D))
1904-
return false;
1905-
19061902
// Skip declarations that are not accessible.
19071903
if (auto *VD = dyn_cast<ValueDecl>(D)) {
19081904
if (Options.AccessFilter > AccessLevel::Private &&
@@ -2738,7 +2734,7 @@ static bool usesFeatureActors(Decl *decl) {
27382734
}
27392735

27402736
static bool usesFeatureMacros(Decl *decl) {
2741-
return isa<MacroExpansionDecl>(decl);
2737+
return isa<MacroExpansionDecl>(decl) || isa<MacroDecl>(decl);
27422738
}
27432739

27442740
static bool usesFeatureConcurrentFunctions(Decl *decl) {
@@ -4446,7 +4442,59 @@ void PrintAST::visitMissingMemberDecl(MissingMemberDecl *decl) {
44464442
}
44474443

44484444
void PrintAST::visitMacroDecl(MacroDecl *decl) {
4449-
// No in-source representation of macros.
4445+
printDocumentationComment(decl);
4446+
printAttributes(decl);
4447+
printAccess(decl);
4448+
4449+
Printer.printIntroducerKeyword("macro", Options, " ");
4450+
printContextIfNeeded(decl);
4451+
4452+
recordDeclLoc(
4453+
decl,
4454+
[&]{
4455+
Printer.printName(
4456+
decl->getBaseIdentifier(),
4457+
getTypeMemberPrintNameContext(decl));
4458+
},
4459+
[&] {
4460+
printGenericDeclGenericParams(decl);
4461+
if (decl->parameterList) {
4462+
auto params = ArrayRef<AnyFunctionType::Param>();
4463+
if (!decl->isInvalid()) {
4464+
// Walk to the params of the subscript's indices.
4465+
auto type = decl->getInterfaceType();
4466+
params = type->castTo<AnyFunctionType>()->getParams();
4467+
}
4468+
printParameterList(
4469+
decl->parameterList, params, /*isAPINameByDefault*/true);
4470+
}
4471+
}
4472+
);
4473+
4474+
{
4475+
Printer.printStructurePre(PrintStructureKind::DeclResultTypeClause);
4476+
SWIFT_DEFER {
4477+
Printer.printStructurePost(PrintStructureKind::DeclResultTypeClause);
4478+
};
4479+
4480+
if (decl->parameterList)
4481+
Printer << " -> ";
4482+
else
4483+
Printer << ": ";
4484+
4485+
TypeLoc resultTypeLoc(
4486+
decl->resultType.getTypeRepr(), decl->getResultInterfaceType());
4487+
4488+
Printer.printDeclResultTypePre(decl, resultTypeLoc);
4489+
Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType);
4490+
printTypeLocWithOptions(resultTypeLoc, Options);
4491+
Printer.printStructurePost(PrintStructureKind::FunctionReturnType);
4492+
}
4493+
4494+
Printer << " = ";
4495+
Printer << decl->externalModuleName << "." << decl->externalMacroTypeName;
4496+
4497+
printDeclGenericRequirements(decl);
44504498
}
44514499

44524500
void PrintAST::visitMacroExpansionDecl(MacroExpansionDecl *decl) {

lib/AST/Decl.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9654,6 +9654,16 @@ MacroDecl::MacroDecl(
96549654
parameterList->setDeclContextOfParamDecls(this);
96559655
}
96569656

9657+
Type MacroDecl::getResultInterfaceType() const {
9658+
auto &ctx = getASTContext();
9659+
auto mutableThis = const_cast<MacroDecl *>(this);
9660+
if (auto type = evaluateOrDefault(ctx.evaluator,
9661+
ResultTypeRequest{mutableThis},
9662+
Type()))
9663+
return type;
9664+
return ErrorType::get(ctx);
9665+
}
9666+
96579667
SourceRange MacroDecl::getSourceRange() const {
96589668
return SourceRange(macroLoc, externalMacroTypeNameLoc);
96599669
}

lib/Sema/TypeCheckDecl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,8 +2550,7 @@ InterfaceTypeRequest::evaluate(Evaluator &eval, ValueDecl *D) const {
25502550

25512551
case DeclKind::Macro: {
25522552
auto macro = cast<MacroDecl>(D);
2553-
Type resultType = evaluateOrDefault(
2554-
Context.evaluator, ResultTypeRequest{macro}, ErrorType::get(Context));
2553+
Type resultType = macro->getResultInterfaceType();
25552554
if (!macro->parameterList)
25562555
return resultType;
25572556

lib/Serialization/Serialization.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4314,10 +4314,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
43144314
uint8_t rawAccessLevel =
43154315
getRawStableAccessLevel(macro->getFormalAccess());
43164316

4317-
Type resultType = evaluateOrDefault(
4318-
S.getASTContext().evaluator,
4319-
ResultTypeRequest{const_cast<MacroDecl *>(macro)},
4320-
Type());
4317+
Type resultType = macro->getResultInterfaceType();
43214318

43224319
unsigned abbrCode = S.DeclTypeAbbrCodes[MacroLayout::Code];
43234320
MacroLayout::emitRecord(S.Out, S.ScratchRecord, abbrCode,

test/ModuleInterface/macros.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend -typecheck -module-name Macros -emit-module-interface-path %t/Macros.swiftinterface -enable-experimental-feature Macros %s
4+
// RUN: %FileCheck %s < %t/Macros.swiftinterface --check-prefix CHECK
5+
// RUN: %target-swift-frontend -compile-module-from-interface %t/Macros.swiftinterface -o %t/Macros.swiftmodule
6+
7+
// CHECK: #if compiler(>=5.3) && $Macros
8+
// CHECK-NEXT: public macro publicStringify<T>(_ value: T) -> (T, Swift.String) = _SwiftSyntaxMacros.StringifyMacro
9+
// CHECK-NEXT: #endif
10+
public macro publicStringify<T>(_ value: T) -> (T, String) = _SwiftSyntaxMacros.StringifyMacro
11+
12+
// CHECK: #if compiler(>=5.3) && $Macros
13+
// CHECK: public macro publicLine<T>: T = _SwiftSyntaxMacros.Line where T : Swift.ExpressibleByIntegerLiteral
14+
// CHECK-NEXT: #endif
15+
public macro publicLine<T: ExpressibleByIntegerLiteral>: T = _SwiftSyntaxMacros.Line
16+
17+
// CHECK-NOT: internalStringify
18+
macro internalStringify<T>(_ value: T) -> (T, String) = _SwiftSyntaxMacros.StringifyMacro

0 commit comments

Comments
 (0)