Skip to content

Commit 6293332

Browse files
committed
ASTGen: Translate default arguments
1 parent 808ab1b commit 6293332

File tree

8 files changed

+67
-32
lines changed

8 files changed

+67
-32
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ void *IntegerLiteralExpr_create(BridgedASTContext cContext, BridgedString cStr,
289289
void *BooleanLiteralExpr_create(BridgedASTContext cContext, _Bool value,
290290
BridgedSourceLoc cTokenLoc);
291291

292+
void *NilLiteralExpr_create(BridgedASTContext cContext,
293+
BridgedSourceLoc cNilKeywordLoc);
294+
292295
void *ArrayExpr_create(BridgedASTContext cContext, BridgedSourceLoc cLLoc,
293296
BridgedArrayRef elements, BridgedArrayRef commas,
294297
BridgedSourceLoc cRLoc);
@@ -315,7 +318,8 @@ void *
315318
ParamDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
316319
BridgedSourceLoc cSpecifierLoc, BridgedIdentifier cFirstName,
317320
BridgedSourceLoc cFirstNameLoc, BridgedIdentifier cSecondName,
318-
BridgedSourceLoc cSecondNameLoc, void *_Nullable opaqueType);
321+
BridgedSourceLoc cSecondNameLoc, void *_Nullable opaqueType,
322+
void *_Nullable opaqueDefaultValue);
319323

320324
struct BridgedFuncDecl
321325
FuncDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,

include/swift/AST/DefaultArgumentKind.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ enum class DefaultArgumentKind : uint8_t {
5454
};
5555
enum { NumDefaultArgumentKindBits = 4 };
5656

57+
/// Determine the kind of a default argument given a parsed expression that has
58+
/// not yet been type-checked.
59+
/// FIXME: Requestify/internalize the computation of the default arg expr and its kind (given a parsed expr) once the old parser no longer needs this.
60+
DefaultArgumentKind getDefaultArgKind(Expr *init);
61+
5762
struct ArgumentAttrs {
5863
DefaultArgumentKind argumentKind;
5964
bool isUnavailableInSwift = false;

lib/AST/CASTBridging.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "swift/AST/ParseRequests.h"
1212
#include "swift/AST/Pattern.h"
1313
#include "swift/AST/PluginRegistry.h"
14+
#include "swift/AST/SourceFile.h"
1415
#include "swift/AST/Stmt.h"
1516
#include "swift/AST/TypeRepr.h"
1617

@@ -319,6 +320,13 @@ void *BooleanLiteralExpr_create(BridgedASTContext cContext, bool value,
319320
return new (context) BooleanLiteralExpr(value, convertSourceLoc(cTokenLoc));
320321
}
321322

323+
void *NilLiteralExpr_create(BridgedASTContext cContext,
324+
BridgedSourceLoc cNilKeywordLoc) {
325+
Expr *e = new (convertASTContext(cContext))
326+
NilLiteralExpr(convertSourceLoc(cNilKeywordLoc));
327+
return e;
328+
}
329+
322330
void *VarDecl_create(BridgedASTContext cContext,
323331
BridgedDeclContext cDeclContext,
324332
BridgedSourceLoc cBindingKeywordLoc, void *opaqueNameExpr,
@@ -402,20 +410,34 @@ void *
402410
ParamDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
403411
BridgedSourceLoc cSpecifierLoc, BridgedIdentifier cFirstName,
404412
BridgedSourceLoc cFirstNameLoc, BridgedIdentifier cSecondName,
405-
BridgedSourceLoc cSecondNameLoc, void *_Nullable opaqueType) {
413+
BridgedSourceLoc cSecondNameLoc, void *_Nullable opaqueType,
414+
void *_Nullable opaqueDefaultValue) {
406415
assert((bool)cSecondNameLoc.raw == (bool)cSecondName.raw);
407416
if (!cSecondName.raw) {
408417
cSecondName = cFirstName;
409418
cSecondNameLoc = cFirstNameLoc;
410419
}
411420

412-
ASTContext &context = convertASTContext(cContext);
421+
auto *declContext = convertDeclContext(cDeclContext);
422+
423+
auto *defaultValue = static_cast<Expr *>(opaqueDefaultValue);
424+
DefaultArgumentKind defaultArgumentKind;
425+
426+
if (declContext->getParentSourceFile()->Kind == SourceFileKind::Interface &&
427+
isa<SuperRefExpr>(defaultValue)) {
428+
defaultValue = nullptr;
429+
defaultArgumentKind = DefaultArgumentKind::Inherited;
430+
} else {
431+
defaultArgumentKind = getDefaultArgKind(defaultValue);
432+
}
413433

414-
auto *paramDecl = new (context) ParamDecl(
434+
auto *paramDecl = new (convertASTContext(cContext)) ParamDecl(
415435
convertSourceLoc(cSpecifierLoc), convertSourceLoc(cFirstNameLoc),
416436
convertIdentifier(cFirstName), convertSourceLoc(cSecondNameLoc),
417-
convertIdentifier(cSecondName), convertDeclContext(cDeclContext));
437+
convertIdentifier(cSecondName), declContext);
418438
paramDecl->setTypeRepr(static_cast<TypeRepr *>(opaqueType));
439+
paramDecl->setDefaultExpr(defaultValue, /*isTypeChecked*/ false);
440+
paramDecl->setDefaultArgumentKind(defaultArgumentKind);
419441

420442
return paramDecl;
421443
}

lib/AST/Decl.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7897,6 +7897,30 @@ ParamDecl *ParamDecl::createImplicit(ASTContext &Context,
78977897
interfaceType, Parent, specifier);
78987898
}
78997899

7900+
DefaultArgumentKind swift::getDefaultArgKind(Expr *init) {
7901+
if (!init)
7902+
return DefaultArgumentKind::None;
7903+
7904+
// Parse an as-written 'nil' expression as the special NilLiteral kind,
7905+
// which is emitted by the caller and can participate in rethrows
7906+
// checking.
7907+
if (isa<NilLiteralExpr>(init))
7908+
return DefaultArgumentKind::NilLiteral;
7909+
7910+
auto magic = dyn_cast<MagicIdentifierLiteralExpr>(init);
7911+
if (!magic)
7912+
return DefaultArgumentKind::Normal;
7913+
7914+
switch (magic->getKind()) {
7915+
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
7916+
case MagicIdentifierLiteralExpr::NAME: \
7917+
return DefaultArgumentKind::NAME;
7918+
#include "swift/AST/MagicIdentifierKinds.def"
7919+
}
7920+
7921+
llvm_unreachable("Unhandled MagicIdentifierLiteralExpr in switch.");
7922+
}
7923+
79007924
/// Retrieve the type of 'self' for the given context.
79017925
Type DeclContext::getSelfTypeInContext() const {
79027926
return mapTypeIntoContext(getSelfInterfaceType());

lib/ASTGen/Sources/ASTGen/Literals.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,8 @@ extension ASTGenVisitor {
4747
)
4848
)
4949
}
50+
51+
func visit(_ node: NilLiteralExprSyntax) -> ASTNode {
52+
.expr(NilLiteralExpr_create(self.ctx, self.bridgedSourceLoc(for: node.nilKeyword)))
53+
}
5054
}

lib/ASTGen/Sources/ASTGen/ParameterClause.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ extension ASTGenVisitor {
4141
self.bridgedSourceLoc(for: node.firstName),
4242
secondName,
4343
secondNameLoc,
44-
self.visit(node.type).rawValue
44+
self.visit(node.type).rawValue,
45+
self.visit(node.defaultValue?.value)?.rawValue
4546
)
4647
)
4748
}

lib/Parse/ParsePattern.cpp

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,6 @@
3030

3131
using namespace swift;
3232

33-
/// Determine the kind of a default argument given a parsed
34-
/// expression that has not yet been type-checked.
35-
static DefaultArgumentKind getDefaultArgKind(Expr *init) {
36-
if (!init)
37-
return DefaultArgumentKind::None;
38-
39-
// Parse an as-written 'nil' expression as the special NilLiteral kind,
40-
// which is emitted by the caller and can participate in rethrows
41-
// checking.
42-
if (isa<NilLiteralExpr>(init))
43-
return DefaultArgumentKind::NilLiteral;
44-
45-
auto magic = dyn_cast<MagicIdentifierLiteralExpr>(init);
46-
if (!magic)
47-
return DefaultArgumentKind::Normal;
48-
49-
switch (magic->getKind()) {
50-
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
51-
case MagicIdentifierLiteralExpr::NAME: return DefaultArgumentKind::NAME;
52-
#include "swift/AST/MagicIdentifierKinds.def"
53-
}
54-
55-
llvm_unreachable("Unhandled MagicIdentifierLiteralExpr in switch.");
56-
}
57-
5833
void Parser::DefaultArgumentInfo::setFunctionContext(
5934
DeclContext *DC, ParameterList *paramList){
6035
for (auto context : ParsedContexts) {

test/ASTGen/verify-parse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func test2(e b: Bool) {
4949
true
5050
}
5151

52-
func test3(y: Int) -> Int {
52+
func test3(y: Int = 0, oi: Int? = nil) -> Int {
5353
let x =
5454
y
5555
return x

0 commit comments

Comments
 (0)