Skip to content

Commit 5369cca

Browse files
committed
---
yaml --- r: 319451 b: refs/heads/master-rebranch c: b345524 h: refs/heads/master i: 319449: c4352f6 319447: 056440f
1 parent 00f77b3 commit 5369cca

File tree

9 files changed

+266
-62
lines changed

9 files changed

+266
-62
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1457,4 +1457,4 @@ refs/tags/swift-DEVELOPMENT-SNAPSHOT-2019-08-02-a: ddd2b2976aa9bfde5f20fe37f6bd2
14571457
refs/tags/swift-DEVELOPMENT-SNAPSHOT-2019-08-03-a: 171cc166f2abeb5ca2a4003700a8a78a108bd300
14581458
refs/heads/benlangmuir-patch-1: baaebaf39d52f3bf36710d4fe40cf212e996b212
14591459
refs/heads/i-do-redeclare: 8c4e6d5de5c1e3f0a2cedccf319df713ea22c48e
1460-
refs/heads/master-rebranch: 0c9bb5eb004c0832a02967444af2d776728d8bfa
1460+
refs/heads/master-rebranch: b345524b53ef01965658a52eb9e8bcaf92609d1d

branches/master-rebranch/include/swift/AST/ASTContext.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ namespace swift {
5353
class AvailabilityContext;
5454
class BoundGenericType;
5555
class ClangNode;
56+
class ConcreteDeclRef;
5657
class ConstructorDecl;
5758
class Decl;
5859
class DeclContext;
@@ -514,7 +515,20 @@ class ASTContext final {
514515
bool hasArrayLiteralIntrinsics() const;
515516

516517
/// Retrieve the declaration of Swift.Bool.init(_builtinBooleanLiteral:)
517-
ConstructorDecl *getBoolBuiltinInitDecl() const;
518+
ConcreteDeclRef getBoolBuiltinInitDecl() const;
519+
520+
/// Retrieve the witness for init(_builtinIntegerLiteral:).
521+
ConcreteDeclRef getIntBuiltinInitDecl(NominalTypeDecl *intDecl) const;
522+
523+
/// Retrieve the witness for init(_builtinFloatLiteral:).
524+
ConcreteDeclRef getFloatBuiltinInitDecl(NominalTypeDecl *floatDecl) const;
525+
526+
/// Retrieve the witness for (_builtinStringLiteral:utf8CodeUnitCount:isASCII:).
527+
ConcreteDeclRef getStringBuiltinInitDecl(NominalTypeDecl *stringDecl) const;
528+
529+
ConcreteDeclRef getBuiltinInitDecl(NominalTypeDecl *decl,
530+
KnownProtocolKind builtinProtocol,
531+
llvm::function_ref<DeclName (ASTContext &ctx)> initName) const;
518532

519533
/// Retrieve the declaration of Swift.==(Int, Int) -> Bool.
520534
FuncDecl *getEqualIntDecl() const;

branches/master-rebranch/include/swift/AST/KnownStdlibTypes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ KNOWN_STDLIB_TYPE_DECL(Float80, NominalTypeDecl, 0)
4848
KNOWN_STDLIB_TYPE_DECL(_MaxBuiltinFloatType, TypeAliasDecl, 0)
4949

5050
KNOWN_STDLIB_TYPE_DECL(String, NominalTypeDecl, 0)
51+
KNOWN_STDLIB_TYPE_DECL(StaticString, NominalTypeDecl, 0)
5152
KNOWN_STDLIB_TYPE_DECL(Substring, NominalTypeDecl, 0)
5253
KNOWN_STDLIB_TYPE_DECL(Array, NominalTypeDecl, 1)
5354
KNOWN_STDLIB_TYPE_DECL(Set, NominalTypeDecl, 1)

branches/master-rebranch/lib/AST/ASTContext.cpp

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,6 @@ FOR_KNOWN_FOUNDATION_TYPES(CACHE_FOUNDATION_DECL)
183183
#define FUNC_DECL(Name, Id) FuncDecl *Get##Name = nullptr;
184184
#include "swift/AST/KnownDecls.def"
185185

186-
/// Swift.Bool.init(_builtinBooleanLiteral:)
187-
ConstructorDecl *BoolBuiltinInitDecl = nullptr;
188-
189186
/// func ==(Int, Int) -> Bool
190187
FuncDecl *EqualIntDecl = nullptr;
191188

@@ -277,6 +274,10 @@ FOR_KNOWN_FOUNDATION_TYPES(CACHE_FOUNDATION_DECL)
277274
/// to the original property with the delegate.
278275
llvm::DenseMap<const VarDecl *, VarDecl *> OriginalDelegatedProperties;
279276

277+
/// The builtin initializer witness for a literal. Used when building
278+
/// LiteralExprs in fully-checked AST.
279+
llvm::DenseMap<const NominalTypeDecl *, ConcreteDeclRef> BuiltinInitWitness;
280+
280281
/// Structure that captures data that is segregated into different
281282
/// arenas.
282283
struct Arena {
@@ -963,27 +964,80 @@ lookupOperatorFunc(const ASTContext &ctx, StringRef oper, Type contextType,
963964
return nullptr;
964965
}
965966

966-
ConstructorDecl *ASTContext::getBoolBuiltinInitDecl() const {
967-
if (getImpl().BoolBuiltinInitDecl)
968-
return getImpl().BoolBuiltinInitDecl;
967+
ConcreteDeclRef ASTContext::getBoolBuiltinInitDecl() const {
968+
auto fn = [&](ASTContext &ctx) {
969+
return DeclName(ctx, DeclBaseName::createConstructor(),
970+
{ Id_builtinBooleanLiteral });
971+
};
972+
auto builtinProtocolKind =
973+
KnownProtocolKind::ExpressibleByBuiltinBooleanLiteral;
974+
return getBuiltinInitDecl(getBoolDecl(), builtinProtocolKind, fn);
975+
}
969976

970-
if (!getBoolDecl())
971-
return nullptr;
977+
ConcreteDeclRef
978+
ASTContext::getIntBuiltinInitDecl(NominalTypeDecl *intDecl) const {
979+
auto fn = [&](ASTContext &ctx) {
980+
return DeclName(ctx, DeclBaseName::createConstructor(),
981+
{ Id_builtinIntegerLiteral });
982+
};
983+
auto builtinProtocolKind =
984+
KnownProtocolKind::ExpressibleByBuiltinIntegerLiteral;
985+
return getBuiltinInitDecl(intDecl, builtinProtocolKind, fn);
986+
}
972987

973-
DeclName initName(*const_cast<ASTContext *>(this),
974-
DeclBaseName::createConstructor(),
975-
{ Id_builtinBooleanLiteral });
976-
auto members = getBoolDecl()->lookupDirect(initName);
988+
ConcreteDeclRef
989+
ASTContext::getFloatBuiltinInitDecl(NominalTypeDecl *floatDecl) const {
990+
auto fn = [&](ASTContext &ctx) {
991+
return DeclName(ctx, DeclBaseName::createConstructor(),
992+
{ Id_builtinFloatLiteral });
993+
};
977994

978-
if (members.size() != 1)
979-
return nullptr;
995+
auto builtinProtocolKind =
996+
KnownProtocolKind::ExpressibleByBuiltinFloatLiteral;
997+
return getBuiltinInitDecl(floatDecl, builtinProtocolKind, fn);
998+
}
999+
1000+
ConcreteDeclRef
1001+
ASTContext::getStringBuiltinInitDecl(NominalTypeDecl *stringDecl) const {
1002+
auto fn = [&](ASTContext &ctx) {
1003+
return DeclName(ctx, DeclBaseName::createConstructor(),
1004+
{ Id_builtinStringLiteral,
1005+
getIdentifier("utf8CodeUnitCount"),
1006+
getIdentifier("isASCII") });
1007+
};
1008+
1009+
auto builtinProtocolKind =
1010+
KnownProtocolKind::ExpressibleByBuiltinStringLiteral;
1011+
return getBuiltinInitDecl(stringDecl, builtinProtocolKind, fn);
1012+
}
1013+
1014+
ConcreteDeclRef
1015+
ASTContext::getBuiltinInitDecl(NominalTypeDecl *decl,
1016+
KnownProtocolKind builtinProtocolKind,
1017+
llvm::function_ref<DeclName (ASTContext &ctx)> initName) const {
1018+
auto &witness = getImpl().BuiltinInitWitness[decl];
1019+
if (witness)
1020+
return witness;
9801021

981-
if (auto init = dyn_cast<ConstructorDecl>(members[0])) {
982-
getImpl().BoolBuiltinInitDecl = init;
983-
return init;
1022+
auto type = decl->getDeclaredType();
1023+
auto builtinProtocol = getProtocol(builtinProtocolKind);
1024+
auto builtinConformance = getStdlibModule()->lookupConformance(
1025+
type, builtinProtocol);
1026+
if (!builtinConformance) {
1027+
assert(false && "Missing required conformance");
1028+
witness = ConcreteDeclRef();
1029+
return witness;
9841030
}
9851031

986-
return nullptr;
1032+
auto *ctx = const_cast<ASTContext *>(this);
1033+
witness = builtinConformance->getWitnessByName(type, initName(*ctx));
1034+
if (!witness) {
1035+
assert(false && "Missing required witness");
1036+
witness = ConcreteDeclRef();
1037+
return witness;
1038+
}
1039+
1040+
return witness;
9871041
}
9881042

9891043
FuncDecl *ASTContext::getEqualIntDecl() const {

branches/master-rebranch/lib/ClangImporter/ImportDecl.cpp

Lines changed: 110 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,7 @@ makeUnionFieldAccessors(ClangImporter::Implementation &Impl,
10011001
{ inoutSelf },
10021002
{ Identifier() });
10031003
selfPointer->setType(C.TheRawPointerType);
1004+
selfPointer->setThrows(false);
10041005

10051006
auto initializeFn = cast<FuncDecl>(getBuiltinValueDecl(
10061007
C, C.getIdentifier("initialize")));
@@ -1018,6 +1019,7 @@ makeUnionFieldAccessors(ClangImporter::Implementation &Impl,
10181019
{ newValueRef, selfPointer },
10191020
{ Identifier(), Identifier() });
10201021
initialize->setType(TupleType::getEmpty(C));
1022+
initialize->setThrows(false);
10211023

10221024
auto body = BraceStmt::create(C, SourceLoc(), { initialize }, SourceLoc(),
10231025
/*implicit*/ true);
@@ -1499,15 +1501,19 @@ static void makeStructRawValued(
14991501
createValueConstructor(Impl, structDecl, var,
15001502
/*wantCtorParamNames=*/false,
15011503
/*wantBody=*/!Impl.hasFinishedTypeChecking()));
1502-
structDecl->addMember(
1504+
1505+
auto *initRawValue =
15031506
createValueConstructor(Impl, structDecl, var,
15041507
/*wantCtorParamNames=*/true,
1505-
/*wantBody=*/!Impl.hasFinishedTypeChecking()));
1508+
/*wantBody=*/!Impl.hasFinishedTypeChecking());
1509+
structDecl->addMember(initRawValue);
15061510
structDecl->addMember(patternBinding);
15071511
structDecl->addMember(var);
15081512
structDecl->addMember(varGetter);
15091513

15101514
addSynthesizedTypealias(structDecl, ctx.Id_RawValue, underlyingType);
1515+
Impl.RawTypes[structDecl] = underlyingType;
1516+
Impl.RawInits[structDecl] = initRawValue;
15111517
}
15121518

15131519
/// Create a rawValue-ed constructor that bridges to its underlying storage.
@@ -1650,6 +1656,8 @@ static void makeStructRawValuedWithBridge(
16501656
structDecl->addMember(computedVarGetter);
16511657

16521658
addSynthesizedTypealias(structDecl, ctx.Id_RawValue, bridgedType);
1659+
Impl.RawTypes[structDecl] = bridgedType;
1660+
Impl.RawInits[structDecl] = init;
16531661
}
16541662

16551663
/// Build a declaration for an Objective-C subscript getter.
@@ -2852,6 +2860,8 @@ namespace {
28522860
enumDecl->addMember(rawValueBinding);
28532861

28542862
addSynthesizedTypealias(enumDecl, C.Id_RawValue, underlyingType);
2863+
Impl.RawTypes[enumDecl] = underlyingType;
2864+
Impl.RawInits[enumDecl] = rawValueConstructor;
28552865

28562866
// If we have an error wrapper, finish it up now that its
28572867
// nested enum has been constructed.
@@ -3381,7 +3391,7 @@ namespace {
33813391
// Create the global constant.
33823392
auto result = Impl.createConstant(name, dc, type,
33833393
clang::APValue(decl->getInitVal()),
3384-
ConstantConvertKind::Coerce,
3394+
ConstantConvertKind::None,
33853395
/*static*/dc->isTypeContext(), decl);
33863396
Impl.ImportedDecls[{decl->getCanonicalDecl(), getVersion()}] = result;
33873397

@@ -5546,11 +5556,15 @@ Decl *SwiftDeclConverter::importEnumCaseAlias(
55465556
auto constantRef =
55475557
new (Impl.SwiftContext) DeclRefExpr(original, DeclNameLoc(),
55485558
/*implicit*/ true);
5559+
constantRef->setType(original->getInterfaceType());
5560+
55495561
Type importedEnumTy = importedEnum->getDeclaredInterfaceType();
5562+
55505563
auto typeRef = TypeExpr::createImplicit(importedEnumTy, Impl.SwiftContext);
55515564
auto instantiate = new (Impl.SwiftContext)
55525565
DotSyntaxCallExpr(constantRef, SourceLoc(), typeRef);
55535566
instantiate->setType(importedEnumTy);
5567+
instantiate->setThrows(false);
55545568

55555569
Decl *CD = Impl.createConstant(name, importIntoDC, importedEnumTy,
55565570
instantiate, ConstantConvertKind::None,
@@ -8139,6 +8153,21 @@ ClangImporter::Implementation::importDeclContextOf(
81398153
return ext;
81408154
}
81418155

8156+
static Type getConstantLiteralType(ClangImporter::Implementation &Impl,
8157+
Type type, ConstantConvertKind convertKind) {
8158+
switch (convertKind) {
8159+
case ConstantConvertKind::Construction:
8160+
case ConstantConvertKind::ConstructionWithUnwrap: {
8161+
auto found = Impl.RawTypes.find(type->getAnyNominal());
8162+
assert(found != Impl.RawTypes.end());
8163+
return found->second;
8164+
}
8165+
8166+
default:
8167+
return type;
8168+
}
8169+
}
8170+
81428171
ValueDecl *
81438172
ClangImporter::Implementation::createConstant(Identifier name, DeclContext *dc,
81448173
Type type,
@@ -8181,20 +8210,49 @@ ClangImporter::Implementation::createConstant(Identifier name, DeclContext *dc,
81818210
if (isNegative)
81828211
printedValue = printedValue.drop_front();
81838212

8213+
auto literalType = getConstantLiteralType(*this, type, convertKind);
8214+
81848215
// Create the expression node.
81858216
StringRef printedValueCopy(context.AllocateCopy(printedValue));
81868217
if (value.getKind() == clang::APValue::Int) {
81878218
if (type->getCanonicalType()->isBool()) {
8188-
expr = new (context) BooleanLiteralExpr(value.getInt().getBoolValue(),
8189-
SourceLoc(),
8190-
/**Implicit=*/true);
8219+
auto *boolExpr =
8220+
new (context) BooleanLiteralExpr(value.getInt().getBoolValue(),
8221+
SourceLoc(),
8222+
/**Implicit=*/true);
8223+
8224+
boolExpr->setBuiltinInitializer(
8225+
context.getBoolBuiltinInitDecl());
8226+
boolExpr->setType(literalType);
8227+
8228+
expr = boolExpr;
81918229
} else {
8192-
expr = new (context) IntegerLiteralExpr(printedValueCopy, SourceLoc(),
8193-
/*Implicit=*/true);
8230+
auto *intExpr =
8231+
new (context) IntegerLiteralExpr(printedValueCopy, SourceLoc(),
8232+
/*Implicit=*/true);
8233+
8234+
auto *intDecl = literalType->getAnyNominal();
8235+
intExpr->setBuiltinInitializer(
8236+
context.getIntBuiltinInitDecl(intDecl));
8237+
intExpr->setType(literalType);
8238+
8239+
expr = intExpr;
81948240
}
81958241
} else {
8196-
expr = new (context) FloatLiteralExpr(printedValueCopy, SourceLoc(),
8197-
/*Implicit=*/true);
8242+
auto *floatExpr =
8243+
new (context) FloatLiteralExpr(printedValueCopy, SourceLoc(),
8244+
/*Implicit=*/true);
8245+
8246+
auto maxFloatTypeDecl = context.get_MaxBuiltinFloatTypeDecl();
8247+
floatExpr->setBuiltinType(
8248+
maxFloatTypeDecl->getUnderlyingTypeLoc().getType());
8249+
8250+
auto *floatDecl = literalType->getAnyNominal();
8251+
floatExpr->setBuiltinInitializer(
8252+
context.getFloatBuiltinInitDecl(floatDecl));
8253+
floatExpr->setType(literalType);
8254+
8255+
expr = floatExpr;
81988256
}
81998257

82008258
if (isNegative)
@@ -8216,6 +8274,13 @@ ClangImporter::Implementation::createConstant(Identifier name, DeclContext *dc,
82168274
bool isStatic,
82178275
ClangNode ClangN) {
82188276
auto expr = new (SwiftContext) StringLiteralExpr(value, SourceRange());
8277+
8278+
auto literalType = getConstantLiteralType(*this, type, convertKind);
8279+
auto *stringDecl = literalType->getAnyNominal();
8280+
expr->setBuiltinInitializer(
8281+
SwiftContext.getStringBuiltinInitDecl(stringDecl));
8282+
expr->setType(literalType);
8283+
82198284
return createConstant(name, dc, type, expr, convertKind, isStatic, ClangN);
82208285
}
82218286

@@ -8280,20 +8345,42 @@ ClangImporter::Implementation::createConstant(Identifier name, DeclContext *dc,
82808345
case ConstantConvertKind::Construction:
82818346
case ConstantConvertKind::ConstructionWithUnwrap: {
82828347
auto typeRef = TypeExpr::createImplicit(type, C);
8283-
8284-
expr = CallExpr::createImplicit(C, typeRef, { expr }, { C.Id_rawValue });
8285-
if (convertKind == ConstantConvertKind::ConstructionWithUnwrap)
8286-
expr = new (C) ForceValueExpr(expr, SourceLoc());
8287-
break;
8288-
}
82898348

8290-
case ConstantConvertKind::Coerce:
8291-
break;
8349+
// Reference init(rawValue: T)
8350+
auto found = RawInits.find(type->getAnyNominal());
8351+
assert(found != RawInits.end());
8352+
8353+
auto *init = found->second;
8354+
auto initTy = init->getInterfaceType()->removeArgumentLabels(1);
8355+
auto declRef =
8356+
new (C) DeclRefExpr(init, DeclNameLoc(), /*Implicit=*/true,
8357+
AccessSemantics::Ordinary, initTy);
8358+
8359+
// (Self) -> ...
8360+
initTy = initTy->castTo<FunctionType>()->getResult();
8361+
auto initRef = new (C) DotSyntaxCallExpr(declRef, SourceLoc(),
8362+
typeRef, initTy);
8363+
initRef->setThrows(false);
8364+
8365+
// (rawValue: T) -> ...
8366+
initTy = initTy->castTo<FunctionType>()->getResult();
8367+
8368+
auto initCall = CallExpr::createImplicit(C, initRef, { expr },
8369+
{ C.Id_rawValue });
8370+
initCall->setType(initTy);
8371+
initCall->setThrows(false);
8372+
8373+
expr = initCall;
8374+
8375+
// Force unwrap if our init(rawValue:) is failable, which is currently
8376+
// the case with enums.
8377+
if (convertKind == ConstantConvertKind::ConstructionWithUnwrap) {
8378+
initTy = initTy->getOptionalObjectType();
8379+
expr = new (C) ForceValueExpr(expr, SourceLoc());
8380+
expr->setType(initTy);
8381+
}
82928382

8293-
case ConstantConvertKind::Downcast: {
8294-
expr = new (C) ForcedCheckedCastExpr(expr, SourceLoc(), SourceLoc(),
8295-
TypeLoc::withoutLoc(type));
8296-
expr->setImplicit();
8383+
assert(initTy->isEqual(type));
82978384
break;
82988385
}
82998386
}
@@ -8305,6 +8392,7 @@ ClangImporter::Implementation::createConstant(Identifier name, DeclContext *dc,
83058392
func->setBody(BraceStmt::create(C, SourceLoc(),
83068393
ASTNode(ret),
83078394
SourceLoc()));
8395+
func->setBodyTypeCheckedIfPresent();
83088396
}
83098397

83108398
// Mark the function transparent so that we inline it away completely.

0 commit comments

Comments
 (0)