Skip to content

Commit b09a22a

Browse files
committed
Somewhat working
Test shadowed variable of same type Fully type check caller side macro expansion Skip macro default arg caller side expr at decl primary Test macro expand more complex expressions Set synthesized expression as implicit Add test case for with argument, not compiling currently Test with swiftinterface Always use the string representation of the default argument Now works across module boundary Check works for multiple files Make default argument expression work in single file Use expected-error Disallow expression macro as default argument Using as a sub expression in default argument still allowed as expression macros behave the same as built-in magic literals
1 parent d93d742 commit b09a22a

40 files changed

+496
-21
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/MacroRoles.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ ATTACHED_MACRO_ROLE(Conformance, "conformance", "c")
7070
/// declarations in a code block.
7171
EXPERIMENTAL_FREESTANDING_MACRO_ROLE(CodeItem, "codeItem", CodeItemMacros)
7272

73+
/// A freestanding macro that is used as the default argument
74+
EXPERIMENTAL_FREESTANDING_MACRO_ROLE(DefaultArgument, "defaultArgument", ExpressionMacroDefaultArguments)
75+
7376
/// An attached macro that adds extensions to the declaration the
7477
/// macro is attached to.
7578
ATTACHED_MACRO_ROLE(Extension, "extension", "e")

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/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

@@ -2970,7 +2970,7 @@ class ConstraintSystem {
29702970

29712971
friend Type swift::TypeChecker::typeCheckParameterDefault(Expr *&,
29722972
DeclContext *, Type,
2973-
bool);
2973+
bool, bool);
29742974

29752975
/// Emit the fixes computed as part of the solution, returning true if we were
29762976
/// 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
@@ -3877,6 +3877,8 @@ void ASTMangler::appendMacroExpansionContext(
38773877

38783878
case GeneratedSourceInfo::PrettyPrinted:
38793879
case GeneratedSourceInfo::ReplacedFunctionBody:
3880+
// TODO: ApolloZhu check if this correct?
3881+
case GeneratedSourceInfo::DefaultArgument:
38803882
return appendContext(origDC, StringRef());
38813883
}
38823884

lib/AST/ASTPrinter.cpp

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

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

0 commit comments

Comments
 (0)