Skip to content

Commit 81ffafd

Browse files
authored
Merge pull request #70602 from ApolloZhu/macro/expression-as-default-argument
[Macros] Expression macro as caller-side default argument
2 parents 4d8c842 + 8da4b1e commit 81ffafd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+730
-25
lines changed

include/swift/AST/DefaultArgumentKind.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ enum class DefaultArgumentKind : uint8_t {
5151
// Magic identifier literals expanded at the call site:
5252
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) NAME,
5353
#include "swift/AST/MagicIdentifierKinds.def"
54+
/// An expression macro.
55+
ExpressionMacro
5456
};
5557
enum { NumDefaultArgumentKindBits = 4 };
5658

include/swift/AST/Module.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ enum class SourceFileKind {
106106
SIL, ///< Came from a .sil file.
107107
Interface, ///< Came from a .swiftinterface file, representing another module.
108108
MacroExpansion, ///< Came from a macro expansion.
109+
DefaultArgument, ///< Came from default argument at caller side
109110
};
110111

111112
/// Contains information about where a particular path is used in

include/swift/AST/SourceFile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ class SourceFile final : public FileUnit {
674674
case SourceFileKind::Interface:
675675
case SourceFileKind::SIL:
676676
case SourceFileKind::MacroExpansion:
677+
case SourceFileKind::DefaultArgument:
677678
return false;
678679
}
679680
llvm_unreachable("bad SourceFileKind");

include/swift/Basic/Features.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ EXPERIMENTAL_FEATURE(NamedOpaqueTypes, false)
135135
EXPERIMENTAL_FEATURE(FlowSensitiveConcurrencyCaptures, false)
136136
EXPERIMENTAL_FEATURE(CodeItemMacros, false)
137137
EXPERIMENTAL_FEATURE(BodyMacros, true)
138+
EXPERIMENTAL_FEATURE(ExpressionMacroDefaultArguments, false)
138139
EXPERIMENTAL_FEATURE(TupleConformances, false)
139140

140141
// Whether to enable @_used and @_section attributes

include/swift/Basic/SourceManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class GeneratedSourceInfo {
4343

4444
/// Pretty-printed declarations that have no source location.
4545
PrettyPrinted,
46+
47+
/// The expansion of default argument at caller side
48+
DefaultArgument,
4649
} kind;
4750

4851
/// The source range in the enclosing buffer where this source was generated.

include/swift/Bridging/ASTGen.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ void *_Nonnull swift_ASTGen_resolveExecutableMacro(
6868
void *_Nonnull opaquePluginHandle);
6969
void swift_ASTGen_destroyExecutableMacro(void *_Nonnull macro);
7070

71+
bool swift_ASTGen_checkDefaultArgumentMacroExpression(
72+
void *_Nonnull diagEngine, void *_Nonnull sourceFile,
73+
const void *_Nonnull macroSourceLocation);
74+
7175
ptrdiff_t swift_ASTGen_checkMacroDefinition(
7276
void *_Nonnull diagEngine, void *_Nonnull sourceFile,
7377
const void *_Nonnull macroSourceLocation,

include/swift/Sema/ConstraintSystem.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ llvm::Optional<constraints::SyntacticElementTarget>
8585
typeCheckTarget(constraints::SyntacticElementTarget &target,
8686
OptionSet<TypeCheckExprFlags> options);
8787

88-
Type typeCheckParameterDefault(Expr *&, DeclContext *, Type, bool);
88+
Type typeCheckParameterDefault(Expr *&, DeclContext *, Type, bool, bool);
8989

9090
} // end namespace TypeChecker
9191

@@ -3019,7 +3019,7 @@ class ConstraintSystem {
30193019

30203020
friend Type swift::TypeChecker::typeCheckParameterDefault(Expr *&,
30213021
DeclContext *, Type,
3022-
bool);
3022+
bool, bool);
30233023

30243024
/// Emit the fixes computed as part of the solution, returning true if we were
30253025
/// able to emit an error message, or false if none of the fixits worked out.

lib/AST/ASTDumper.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ static StringRef getDumpString(DefaultArgumentKind value) {
309309
case DefaultArgumentKind::EmptyDictionary: return "[:]";
310310
case DefaultArgumentKind::Normal: return "normal";
311311
case DefaultArgumentKind::StoredProperty: return "stored property";
312+
case DefaultArgumentKind::ExpressionMacro:
313+
return "expression macro";
312314
}
313315

314316
llvm_unreachable("Unhandled DefaultArgumentKind in switch.");

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3885,6 +3885,7 @@ void ASTMangler::appendMacroExpansionContext(
38853885

38863886
case GeneratedSourceInfo::PrettyPrinted:
38873887
case GeneratedSourceInfo::ReplacedFunctionBody:
3888+
case GeneratedSourceInfo::DefaultArgument:
38883889
return appendContext(origDC, StringRef());
38893890
}
38903891

@@ -3935,6 +3936,7 @@ void ASTMangler::appendMacroExpansionContext(
39353936

39363937
case GeneratedSourceInfo::PrettyPrinted:
39373938
case GeneratedSourceInfo::ReplacedFunctionBody:
3939+
case GeneratedSourceInfo::DefaultArgument:
39383940
llvm_unreachable("Exited above");
39393941
}
39403942

lib/AST/ASTPrinter.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3043,6 +3043,18 @@ static bool usesFeatureFreestandingMacros(Decl *decl) {
30433043
return macro->getMacroRoles().contains(MacroRole::Declaration);
30443044
}
30453045

3046+
static bool usesFeatureExpressionMacroDefaultArguments(Decl *decl) {
3047+
if (auto func = dyn_cast<AbstractFunctionDecl>(decl)) {
3048+
for (auto param : *func->getParameters()) {
3049+
if (param->getDefaultArgumentKind() ==
3050+
DefaultArgumentKind::ExpressionMacro)
3051+
return true;
3052+
}
3053+
}
3054+
3055+
return false;
3056+
}
3057+
30463058
static bool usesFeatureCodeItemMacros(Decl *decl) {
30473059
auto macro = dyn_cast<MacroDecl>(decl);
30483060
if (!macro)

0 commit comments

Comments
 (0)