Skip to content

Commit a2dafc3

Browse files
committed
Remove _getBool
1 parent 2bccdad commit a2dafc3

File tree

14 files changed

+100
-118
lines changed

14 files changed

+100
-118
lines changed

include/swift/AST/ASTContext.h

-3
Original file line numberDiff line numberDiff line change
@@ -513,9 +513,6 @@ class ASTContext final {
513513
/// promises to return non-null.
514514
bool hasArrayLiteralIntrinsics(LazyResolver *resolver) const;
515515

516-
/// Retrieve the declaration of Swift._getBool.
517-
FuncDecl *getGetBoolDecl(LazyResolver *resolver) const;
518-
519516
/// Retrieve the declaration of Swift.==(Int, Int) -> Bool.
520517
FuncDecl *getEqualIntDecl() const;
521518

include/swift/AST/DiagnosticsSema.def

-2
Original file line numberDiff line numberDiff line change
@@ -3458,8 +3458,6 @@ ERROR(pointer_argument_intrinsics_not_found,none,
34583458
ERROR(array_literal_intrinsics_not_found,none,
34593459
"broken standard library: cannot find intrinsic operations on "
34603460
"Array<T>", ())
3461-
ERROR(bool_intrinsics_not_found,none,
3462-
"broken standard library: cannot find intrinsic operations on Bool", ())
34633461
ERROR(class_super_access,none,
34643462
"class %select{must be declared %select{"
34653463
"%select{private|fileprivate|internal|%error|%error}1|private or fileprivate}3"

lib/AST/ASTContext.cpp

-49
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,6 @@ FOR_KNOWN_FOUNDATION_TYPES(CACHE_FOUNDATION_DECL)
194194
#define FUNC_DECL(Name, Id) FuncDecl *Get##Name = nullptr;
195195
#include "swift/AST/KnownDecls.def"
196196

197-
/// func _getBool(Builtin.Int1) -> Bool
198-
FuncDecl *GetBoolDecl = nullptr;
199-
200197
/// func ==(Int, Int) -> Bool
201198
FuncDecl *EqualIntDecl = nullptr;
202199

@@ -996,36 +993,6 @@ lookupOperatorFunc(const ASTContext &ctx, StringRef oper, Type contextType,
996993
return nullptr;
997994
}
998995

999-
/// Looks up the implementation (assumed to be singular) of a globally-defined
1000-
/// standard library intrinsic function and passes the potential match to the
1001-
/// given callback if it was found. If the callback returns true, then the
1002-
/// match is returned; otherwise, nullptr is returned.
1003-
/// \p ctx The AST context.
1004-
/// \p name The name of the function.
1005-
/// \p resolver The lazy resolver.
1006-
/// \p callback A callback that takes as its two arguments the input type and
1007-
/// result type of the candidate function declaration and returns true if
1008-
/// the function matches the desired criteria.
1009-
/// \return The matching function declaration, or nullptr if there was no match.
1010-
static FuncDecl *
1011-
lookupLibraryIntrinsicFunc(const ASTContext &ctx, StringRef name,
1012-
LazyResolver *resolver,
1013-
llvm::function_ref<bool(FunctionType *)> pred) {
1014-
Type inputType, resultType;
1015-
auto decl = findLibraryIntrinsic(ctx, name, resolver);
1016-
if (!decl)
1017-
return nullptr;
1018-
1019-
auto *funcTy = getIntrinsicCandidateType(decl, /*allowTypeMembers=*/false);
1020-
if (!funcTy)
1021-
return nullptr;
1022-
1023-
if (pred(funcTy))
1024-
return decl;
1025-
1026-
return nullptr;
1027-
}
1028-
1029996
FuncDecl *ASTContext::getEqualIntDecl() const {
1030997
if (getImpl().EqualIntDecl)
1031998
return getImpl().EqualIntDecl;
@@ -1047,22 +1014,6 @@ FuncDecl *ASTContext::getEqualIntDecl() const {
10471014
return decl;
10481015
}
10491016

1050-
FuncDecl *ASTContext::getGetBoolDecl(LazyResolver *resolver) const {
1051-
if (getImpl().GetBoolDecl)
1052-
return getImpl().GetBoolDecl;
1053-
1054-
auto boolType = getBoolDecl()->getDeclaredType();
1055-
auto decl = lookupLibraryIntrinsicFunc(*this, "_getBool",
1056-
resolver, [=](FunctionType *type) {
1057-
// Look for the signature (Builtin.Int1) -> Bool
1058-
if (type->getParams().size() != 1) return false;
1059-
if (!isBuiltinInt1Type(type->getParams()[0].getOldType())) return false;
1060-
return type->getResult()->isEqual(boolType);
1061-
});
1062-
getImpl().GetBoolDecl = decl;
1063-
return decl;
1064-
}
1065-
10661017
FuncDecl *ASTContext::getHashValueForDecl() const {
10671018
if (getImpl().HashValueForDecl)
10681019
return getImpl().HashValueForDecl;

lib/SILGen/SILGenApply.cpp

+23-24
Original file line numberDiff line numberDiff line change
@@ -5385,31 +5385,30 @@ getMagicFunctionString(SILGenFunction &SGF) {
53855385
}
53865386

53875387
/// Emit an application of the given allocating initializer.
5388-
static RValue emitApplyAllocatingInitializer(SILGenFunction &SGF,
5389-
SILLocation loc,
5390-
ConcreteDeclRef init,
5391-
RValue &&args,
5392-
Type overriddenSelfType,
5393-
SGFContext C) {
5388+
RValue SILGenFunction::emitApplyAllocatingInitializer(SILLocation loc,
5389+
ConcreteDeclRef init,
5390+
RValue &&args,
5391+
Type overriddenSelfType,
5392+
SGFContext C) {
53945393
ConstructorDecl *ctor = cast<ConstructorDecl>(init.getDecl());
53955394

53965395
// Form the reference to the allocating initializer.
53975396
auto initRef = SILDeclRef(ctor, SILDeclRef::Kind::Allocator)
53985397
.asForeign(requiresForeignEntryPoint(ctor));
5399-
auto initConstant = SGF.getConstantInfo(initRef);
5398+
auto initConstant = getConstantInfo(initRef);
54005399
auto subs = init.getSubstitutions();
54015400

54025401
// Scope any further writeback just within this operation.
5403-
FormalEvaluationScope writebackScope(SGF);
5402+
FormalEvaluationScope writebackScope(*this);
54045403

54055404
// Form the metatype argument.
54065405
ManagedValue selfMetaVal;
54075406
SILType selfMetaTy;
54085407
{
54095408
// Determine the self metatype type.
54105409
CanSILFunctionType substFnType =
5411-
initConstant.SILFnType->substGenericArgs(SGF.SGM.M, subs);
5412-
SILType selfParamMetaTy = SGF.getSILType(substFnType->getSelfParameter());
5410+
initConstant.SILFnType->substGenericArgs(SGM.M, subs);
5411+
SILType selfParamMetaTy = getSILType(substFnType->getSelfParameter());
54135412

54145413
if (overriddenSelfType) {
54155414
// If the 'self' type has been overridden, form a metatype to the
@@ -5419,17 +5418,17 @@ static RValue emitApplyAllocatingInitializer(SILGenFunction &SGF,
54195418
selfParamMetaTy.castTo<MetatypeType>()
54205419
->getRepresentation());
54215420
selfMetaTy =
5422-
SGF.getLoweredType(overriddenSelfMetaType->getCanonicalType());
5421+
getLoweredType(overriddenSelfMetaType->getCanonicalType());
54235422
} else {
54245423
selfMetaTy = selfParamMetaTy;
54255424
}
54265425

54275426
// Form the metatype value.
5428-
SILValue selfMeta = SGF.B.createMetatype(loc, selfMetaTy);
5427+
SILValue selfMeta = B.createMetatype(loc, selfMetaTy);
54295428

54305429
// If the types differ, we need an upcast.
54315430
if (selfMetaTy != selfParamMetaTy)
5432-
selfMeta = SGF.B.createUpcast(loc, selfMeta, selfParamMetaTy);
5431+
selfMeta = B.createUpcast(loc, selfMeta, selfParamMetaTy);
54335432

54345433
selfMetaVal = ManagedValue::forUnmanaged(selfMeta);
54355434
}
@@ -5438,12 +5437,12 @@ static RValue emitApplyAllocatingInitializer(SILGenFunction &SGF,
54385437
Optional<Callee> callee;
54395438
if (isa<ProtocolDecl>(ctor->getDeclContext())) {
54405439
callee.emplace(Callee::forWitnessMethod(
5441-
SGF, selfMetaVal.getType().getASTType(),
5440+
*this, selfMetaVal.getType().getASTType(),
54425441
initRef, subs, loc));
54435442
} else if (getMethodDispatch(ctor) == MethodDispatch::Class) {
5444-
callee.emplace(Callee::forClassMethod(SGF, initRef, subs, loc));
5443+
callee.emplace(Callee::forClassMethod(*this, initRef, subs, loc));
54455444
} else {
5446-
callee.emplace(Callee::forDirect(SGF, initRef, subs, loc));
5445+
callee.emplace(Callee::forDirect(*this, initRef, subs, loc));
54475446
}
54485447

54495448
auto substFormalType = callee->getSubstFormalType();
@@ -5461,12 +5460,12 @@ static RValue emitApplyAllocatingInitializer(SILGenFunction &SGF,
54615460
}
54625461

54635462
// Form the call emission.
5464-
CallEmission emission(SGF, std::move(*callee), std::move(writebackScope));
5463+
CallEmission emission(*this, std::move(*callee), std::move(writebackScope));
54655464

54665465
// Self metatype.
54675466
emission.addCallSite(loc,
54685467
ArgumentSource(loc,
5469-
RValue(SGF, loc,
5468+
RValue(*this, loc,
54705469
selfMetaVal.getType()
54715470
.getASTType(),
54725471
std::move(selfMetaVal))),
@@ -5484,11 +5483,11 @@ static RValue emitApplyAllocatingInitializer(SILGenFunction &SGF,
54845483

54855484
// If we need a downcast, do it down.
54865485
if (requiresDowncast) {
5487-
ManagedValue v = std::move(result).getAsSingleValue(SGF, loc);
5486+
ManagedValue v = std::move(result).getAsSingleValue(*this, loc);
54885487
CanType canOverriddenSelfType = overriddenSelfType->getCanonicalType();
5489-
SILType loweredResultTy = SGF.getLoweredType(canOverriddenSelfType);
5490-
v = SGF.B.createUncheckedRefCast(loc, v, loweredResultTy);
5491-
result = RValue(SGF, loc, canOverriddenSelfType, v);
5488+
SILType loweredResultTy = getLoweredType(canOverriddenSelfType);
5489+
v = B.createUncheckedRefCast(loc, v, loweredResultTy);
5490+
result = RValue(*this, loc, canOverriddenSelfType, v);
54925491
}
54935492

54945493
return result;
@@ -5556,7 +5555,7 @@ RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) {
55565555
// Call the builtin initializer.
55575556
relabelArgument(builtinInit, builtinLiteralArgs);
55585557
RValue builtinLiteral =
5559-
emitApplyAllocatingInitializer(*this, literal, builtinInit,
5558+
emitApplyAllocatingInitializer(literal, builtinInit,
55605559
std::move(builtinLiteralArgs),
55615560
Type(),
55625561
init ? SGFContext() : C);
@@ -5566,7 +5565,7 @@ RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) {
55665565

55675566
// Otherwise, perform the second initialization step.
55685567
relabelArgument(init, builtinLiteral);
5569-
RValue result = emitApplyAllocatingInitializer(*this, literal, init,
5568+
RValue result = emitApplyAllocatingInitializer(literal, init,
55705569
std::move(builtinLiteral),
55715570
literal->getType(), C);
55725571
return result;

lib/SILGen/SILGenExpr.cpp

+37-10
Original file line numberDiff line numberDiff line change
@@ -1864,13 +1864,27 @@ RValue RValueEmitter::visitIsExpr(IsExpr *E, SGFContext C) {
18641864
SILValue isa = emitIsa(SGF, E, E->getSubExpr(),
18651865
E->getCastTypeLoc().getType(), E->getCastKind());
18661866

1867-
// Call the _getBool library intrinsic.
1867+
// Call the Bool(_builtinBooleanLiteral:) initializer
18681868
ASTContext &ctx = SGF.getASTContext();
1869+
DeclName name(ctx, DeclBaseName::createConstructor(),
1870+
{ ctx.Id_builtinBooleanLiteral });
1871+
auto members = ctx.getBoolDecl()->lookupDirect(name);
1872+
1873+
// Ensure we just have one initializer
1874+
assert(members.size() == 1);
1875+
1876+
// Ensure it is actually an initializer
1877+
assert(::isa<ConstructorDecl>(members[0]));
1878+
1879+
auto init = dyn_cast<ConstructorDecl>(members[0]);
1880+
Type builtinArgType = BuiltinIntegerType::get(1, ctx);
1881+
RValue builtinArg(SGF, ManagedValue::forUnmanaged(isa),
1882+
builtinArgType->getCanonicalType());
18691883
auto result =
1870-
SGF.emitApplyOfLibraryIntrinsic(E, ctx.getGetBoolDecl(nullptr),
1871-
SubstitutionMap(),
1872-
ManagedValue::forUnmanaged(isa),
1873-
C);
1884+
SGF.emitApplyAllocatingInitializer(E, ConcreteDeclRef(init),
1885+
std::move(builtinArg), Type(),
1886+
C);
1887+
18741888
return result;
18751889
}
18761890

@@ -1894,12 +1908,25 @@ RValue RValueEmitter::visitEnumIsCaseExpr(EnumIsCaseExpr *E,
18941908
{{E->getEnumElement(), t}});
18951909
}
18961910

1897-
// Call the _getBool library intrinsic.
1911+
// Call the Bool(_builtinBooleanLiteral:) initializer
1912+
DeclName name(ctx, DeclBaseName::createConstructor(),
1913+
{ ctx.Id_builtinBooleanLiteral });
1914+
auto members = ctx.getBoolDecl()->lookupDirect(name);
1915+
1916+
// Ensure we just have one initializer
1917+
assert(members.size() == 1);
1918+
1919+
// Ensure it is actually an initializer
1920+
assert(::isa<ConstructorDecl>(members[0]));
1921+
1922+
auto init = dyn_cast<ConstructorDecl>(members[0]);
1923+
Type builtinArgType = BuiltinIntegerType::get(1, ctx);
1924+
RValue builtinArg(SGF, ManagedValue::forUnmanaged(selected),
1925+
builtinArgType->getCanonicalType());
18981926
auto result =
1899-
SGF.emitApplyOfLibraryIntrinsic(E, ctx.getGetBoolDecl(nullptr),
1900-
SubstitutionMap(),
1901-
ManagedValue::forUnmanaged(selected),
1902-
C);
1927+
SGF.emitApplyAllocatingInitializer(E, ConcreteDeclRef(init),
1928+
std::move(builtinArg), Type(),
1929+
C);
19031930
return result;
19041931
}
19051932

lib/SILGen/SILGenFunction.h

+4
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,10 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
14711471
ArrayRef<ManagedValue> args,
14721472
SGFContext ctx);
14731473

1474+
RValue emitApplyAllocatingInitializer(SILLocation loc, ConcreteDeclRef init,
1475+
RValue &&args, Type overriddenSelfType,
1476+
SGFContext ctx);
1477+
14741478
CleanupHandle emitBeginApply(SILLocation loc, ManagedValue fn,
14751479
SubstitutionMap subs, ArrayRef<ManagedValue> args,
14761480
CanSILFunctionType substFnType,

lib/Sema/CSApply.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -3250,9 +3250,18 @@ namespace {
32503250
}
32513251

32523252
// SIL-generation magically turns this into a Bool; make sure it can.
3253-
if (!cs.getASTContext().getGetBoolDecl(&cs.getTypeChecker())) {
3254-
tc.diagnose(expr->getLoc(), diag::bool_intrinsics_not_found);
3253+
DeclName initName(tc.Context, DeclBaseName::createConstructor(),
3254+
{ tc.Context.Id_builtinBooleanLiteral });
3255+
auto members = cs.getASTContext().getBoolDecl()->lookupDirect(initName);
3256+
3257+
if (members.size() != 1) {
3258+
tc.diagnose(expr->getLoc(), diag::broken_bool);
32553259
// Continue anyway.
3260+
} else {
3261+
if (!isa<ConstructorDecl>(members[0])) {
3262+
tc.diagnose(expr->getLoc(), diag::broken_bool);
3263+
// Continue anyway.
3264+
}
32563265
}
32573266

32583267
// Dig through the optionals in the from/to types.

stdlib/public/core/Bool.swift

-5
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,6 @@ extension Bool : CustomStringConvertible {
187187
}
188188
}
189189

190-
// This is a magic entry point known to the compiler.
191-
@_transparent
192-
public // COMPILER_INTRINSIC
193-
func _getBool(_ v: Builtin.Int1) -> Bool { return Bool(v) }
194-
195190
extension Bool: Equatable {
196191
@_transparent
197192
public static func == (lhs: Bool, rhs: Bool) -> Bool {

test/Interpreter/builtin_bridge_object.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ if true {
7070
// CHECK-NEXT: true
7171

7272
var bo3 = Builtin.castToBridgeObject(C(), 0._builtinWordValue)
73-
print(_getBool(Builtin.isUnique(&bo3)))
73+
print(Bool(_builtinBooleanLiteral: Builtin.isUnique(&bo3)))
7474
// CHECK-NEXT: true
7575
let bo4 = bo3
76-
print(_getBool(Builtin.isUnique(&bo3)))
76+
print(Bool(_builtinBooleanLiteral: Builtin.isUnique(&bo3)))
7777
// CHECK-NEXT: false
7878
_fixLifetime(bo3)
7979
_fixLifetime(bo4)
@@ -98,10 +98,10 @@ if true {
9898
// CHECK-NEXT: true
9999

100100
var bo3 = Builtin.castToBridgeObject(C(), NATIVE_SPARE_BITS._builtinWordValue)
101-
print(_getBool(Builtin.isUnique(&bo3)))
101+
print(Bool(_builtinBooleanLiteral: Builtin.isUnique(&bo3)))
102102
// CHECK-NEXT: true
103103
let bo4 = bo3
104-
print(_getBool(Builtin.isUnique(&bo3)))
104+
print(Bool(_builtinBooleanLiteral: Builtin.isUnique(&bo3)))
105105
// CHECK-NEXT: false
106106
_fixLifetime(bo3)
107107
_fixLifetime(bo4)
@@ -132,7 +132,7 @@ if true {
132132
print(x === x2)
133133

134134
var bo3 = nonNativeBridgeObject(NSNumber(value: 22))
135-
print(_getBool(Builtin.isUnique(&bo3)))
135+
print(Bool(_builtinBooleanLiteral: Builtin.isUnique(&bo3)))
136136
// CHECK-NEXT: false
137137
_fixLifetime(bo3)
138138
}
@@ -154,7 +154,7 @@ if true {
154154
print(x === x2)
155155

156156
var bo3 = nonNativeBridgeObject(unTaggedString)
157-
print(_getBool(Builtin.isUnique(&bo3)))
157+
print(Bool(_builtinBooleanLiteral: Builtin.isUnique(&bo3)))
158158
// CHECK-NEXT: false
159159
_fixLifetime(bo3)
160160
}

0 commit comments

Comments
 (0)