Skip to content

Commit 9229b07

Browse files
authored
Merge pull request swiftlang#79374 from rintaro/astgen-objectliteralexpr
[ASTGen] Generate ObjectLiteralExpr
2 parents 7af666e + 13380da commit 9229b07

File tree

4 files changed

+138
-10
lines changed

4 files changed

+138
-10
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,6 +1774,21 @@ BridgedNilLiteralExpr
17741774
BridgedNilLiteralExpr_createParsed(BridgedASTContext cContext,
17751775
BridgedSourceLoc cNilKeywordLoc);
17761776

1777+
enum ENUM_EXTENSIBILITY_ATTR(open) BridgedObjectLiteralKind : size_t {
1778+
#define POUND_OBJECT_LITERAL(Name, Desc, Proto) BridgedObjectLiteralKind_##Name,
1779+
#include "swift/AST/TokenKinds.def"
1780+
BridgedObjectLiteralKind_none,
1781+
};
1782+
1783+
SWIFT_NAME("BridgedObjectLiteralKind.init(from:)")
1784+
BridgedObjectLiteralKind
1785+
BridgedObjectLiteralKind_fromString(BridgedStringRef cStr);
1786+
1787+
SWIFT_NAME("BridgedObjectLiteralExpr.createParsed(_:poundLoc:kind:args:)")
1788+
BridgedObjectLiteralExpr BridgedObjectLiteralExpr_createParsed(
1789+
BridgedASTContext cContext, BridgedSourceLoc cPoundLoc,
1790+
BridgedObjectLiteralKind cKind, BridgedArgumentList args);
1791+
17771792
SWIFT_NAME("BridgedOptionalTryExpr.createParsed(_:tryLoc:subExpr:questionLoc:)")
17781793
BridgedOptionalTryExpr BridgedOptionalTryExpr_createParsed(
17791794
BridgedASTContext cContext, BridgedSourceLoc cTryLoc, BridgedExpr cSubExpr,

lib/AST/Bridging/ExprBridging.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ unbridge(BridgedMagicIdentifierLiteralKind cKind) {
403403
case BridgedMagicIdentifierLiteralKindNone:
404404
return std::nullopt;
405405
}
406+
llvm_unreachable("unhandled enum value");
406407
}
407408

408409
BridgedMagicIdentifierLiteralExpr
@@ -419,6 +420,38 @@ BridgedNilLiteralExpr_createParsed(BridgedASTContext cContext,
419420
return new (cContext.unbridged()) NilLiteralExpr(cNilKeywordLoc.unbridged());
420421
}
421422

423+
SWIFT_NAME("BridgedObjectLiteralKind.init(from:)")
424+
BridgedObjectLiteralKind
425+
BridgedObjectLiteralKind_fromString(BridgedStringRef cStr) {
426+
return llvm::StringSwitch<BridgedObjectLiteralKind>(cStr.unbridged())
427+
#define POUND_OBJECT_LITERAL(Name, Desc, Proto) \
428+
.Case(#Name, BridgedObjectLiteralKind_##Name)
429+
#include "swift/AST/TokenKinds.def"
430+
.Default(BridgedObjectLiteralKind_none);
431+
}
432+
433+
static std::optional<ObjectLiteralExpr::LiteralKind>
434+
unbridge(BridgedObjectLiteralKind kind) {
435+
switch (kind) {
436+
#define POUND_OBJECT_LITERAL(Name, Desc, Proto) \
437+
case BridgedObjectLiteralKind_##Name: \
438+
return ObjectLiteralExpr::LiteralKind::Name;
439+
#include "swift/AST/TokenKinds.def"
440+
case BridgedObjectLiteralKind_none:
441+
return std::nullopt;
442+
}
443+
llvm_unreachable("unhandled enum value");
444+
}
445+
446+
SWIFT_NAME("BridgedObjectLiteralExpr.createParsed(_:poundLoc:kind:args:)")
447+
BridgedObjectLiteralExpr BridgedObjectLiteralExpr_createParsed(
448+
BridgedASTContext cContext, BridgedSourceLoc cPoundLoc,
449+
BridgedObjectLiteralKind cKind, BridgedArgumentList cArgs) {
450+
return ObjectLiteralExpr::create(cContext.unbridged(), cPoundLoc.unbridged(),
451+
*unbridge(cKind), cArgs.unbridged(),
452+
/*implicit=*/false);
453+
}
454+
422455
BridgedOptionalTryExpr BridgedOptionalTryExpr_createParsed(
423456
BridgedASTContext cContext, BridgedSourceLoc cTryLoc, BridgedExpr cSubExpr,
424457
BridgedSourceLoc cQuestionLoc) {

lib/ASTGen/Sources/ASTGen/Exprs.swift

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,44 @@ extension ASTGenVisitor {
797797
)
798798
}
799799

800+
func generateMagicIdentifierExpr(macroExpansionExpr node: MacroExpansionExprSyntax, kind: BridgedMagicIdentifierLiteralKind) -> BridgedMagicIdentifierLiteralExpr {
801+
guard node.lastToken(viewMode: .sourceAccurate) == node.macroName else {
802+
// TODO: Diagnose.
803+
fatalError("magic identifier token with arguments")
804+
}
805+
806+
return BridgedMagicIdentifierLiteralExpr.createParsed(
807+
self.ctx,
808+
kind: kind,
809+
loc: self.generateSourceLoc(node.macroName)
810+
)
811+
812+
}
813+
814+
func generateObjectLiteralExpr(macroExpansionExpr node: MacroExpansionExprSyntax, kind: BridgedObjectLiteralKind) -> BridgedObjectLiteralExpr {
815+
guard
816+
node.genericArgumentClause == nil,
817+
node.trailingClosure == nil,
818+
node.additionalTrailingClosures.isEmpty
819+
else {
820+
// TODO: Diagnose.
821+
fatalError("object identifier with generic specialization")
822+
}
823+
824+
return BridgedObjectLiteralExpr.createParsed(
825+
self.ctx,
826+
poundLoc: self.generateSourceLoc(node.pound),
827+
kind: kind,
828+
args: self.generateArgumentList(
829+
leftParen: node.leftParen,
830+
labeledExprList: node.arguments,
831+
rightParen: node.rightParen,
832+
trailingClosure: nil,
833+
additionalTrailingClosures: nil
834+
)
835+
)
836+
}
837+
800838
func generateObjCSelectorExpr(macroExpansionExpr node: MacroExpansionExprSyntax) -> BridgedObjCSelectorExpr {
801839
fatalError("unimplemented")
802840
}
@@ -806,27 +844,35 @@ extension ASTGenVisitor {
806844
}
807845

808846
func generate(macroExpansionExpr node: MacroExpansionExprSyntax) -> BridgedExpr {
847+
let macroNameText = node.macroName.rawText;
848+
809849
// '#file', '#line' etc.
810-
let magicIdentifierKind = BridgedMagicIdentifierLiteralKind(from: node.macroName.rawText.bridged)
850+
let magicIdentifierKind = BridgedMagicIdentifierLiteralKind(from: macroNameText.bridged)
811851
if magicIdentifierKind != .none {
812-
guard node.lastToken(viewMode: .sourceAccurate) == node.macroName else {
813-
// TODO: Diagnose
814-
fatalError("magic identifier token with arguments")
815-
}
852+
return self.generateMagicIdentifierExpr(
853+
macroExpansionExpr: node,
854+
kind: magicIdentifierKind
855+
).asExpr
856+
}
816857

817-
return BridgedMagicIdentifierLiteralExpr.createParsed(
818-
self.ctx,
819-
kind: magicIdentifierKind,
820-
loc: self.generateSourceLoc(node.macroName)
858+
// '#colorLiteral' et al.
859+
let objectLiteralKind = BridgedObjectLiteralKind(from: macroNameText.bridged)
860+
if objectLiteralKind != .none {
861+
return self.generateObjectLiteralExpr(
862+
macroExpansionExpr: node,
863+
kind: objectLiteralKind
821864
).asExpr
822865
}
823866

824867
// Other built-in pound expressions.
825-
switch node.macroName.rawText {
868+
switch macroNameText {
826869
case "selector":
827870
return self.generateObjCSelectorExpr(macroExpansionExpr: node).asExpr
828871
case "keyPath":
829872
return self.generateObjCKeyPathExpr(macroExpansionExpr: node).asExpr
873+
case "assert" where ctx.langOptsHasFeature(.StaticAssert), "error", "warning":
874+
// TODO: Diagnose.
875+
fatalError("Directives in expression position");
830876
default:
831877
// Fallback to MacroExpansionExpr.
832878
break

test/ASTGen/object_literals.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-feature ParserASTGen
2+
// REQUIRES: OS=macosx
3+
// REQUIRES: swift_swift_parser
4+
// REQUIRES: swift_feature_ParserASTGen
5+
6+
struct S: _ExpressibleByColorLiteral {
7+
init(_colorLiteralRed: Float, green: Float, blue: Float, alpha: Float) {}
8+
}
9+
10+
let y: S = #colorLiteral(red: 1, green: 0, blue: 0, alpha: 1)
11+
let y2 = #colorLiteral(red: 1, green: 0, blue: 0, alpha: 1) // expected-error{{could not infer type of color literal}}
12+
// expected-note@-1{{import AppKit to use 'NSColor' as the default color literal type}}
13+
let y3 = #colorLiteral(red: 1, bleen: 0, grue: 0, alpha: 1)
14+
// expected-error@-1 {{could not infer type of color literal}}
15+
// expected-note@-2 {{import AppKit to use 'NSColor' as the default color literal type}}
16+
17+
let _: S = #colorLiteral(red: 1, bleen: 0, grue: 0, alpha: 1)
18+
// expected-error@-1{{incorrect argument labels in call (have 'red:bleen:grue:alpha:', expected 'red:green:blue:alpha:')}} {{34-39=green}} {{44-48=blue}}
19+
20+
struct I: _ExpressibleByImageLiteral {
21+
init(imageLiteralResourceName: String) {}
22+
}
23+
24+
let z: I = #imageLiteral(resourceName: "hello.png")
25+
let z2 = #imageLiteral(resourceName: "hello.png") // expected-error{{could not infer type of image literal}}
26+
// expected-note@-1{{import AppKit to use 'NSImage' as the default image literal type}}
27+
28+
struct Path: _ExpressibleByFileReferenceLiteral {
29+
init(fileReferenceLiteralResourceName: String) {}
30+
}
31+
32+
let p1: Path = #fileLiteral(resourceName: "what.txt")
33+
let p2 = #fileLiteral(resourceName: "what.txt") // expected-error{{could not infer type of file reference literal}}
34+
// expected-note@-1{{import Foundation to use 'URL' as the default file reference literal type}}

0 commit comments

Comments
 (0)