Skip to content

Commit cd70284

Browse files
committed
---
yaml --- r: 341707 b: refs/heads/rxwei-patch-1 c: c62cd12 h: refs/heads/master i: 341705: 6e2f4e4 341703: 89a87f1
1 parent 3aa9fdd commit cd70284

File tree

2 files changed

+113
-103
lines changed

2 files changed

+113
-103
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ refs/tags/swift-DEVELOPMENT-SNAPSHOT-2018-08-18-a: b10b1fce14385faa6d44f6b933e95
10151015
refs/heads/rdar-43033749-fix-batch-mode-no-diags-swift-5.0-branch: a14e64eaad30de89f0f5f0b2a782eed7ecdcb255
10161016
refs/heads/revert-19006-error-bridging-integer-type: 8a9065a3696535305ea53fe9b71f91cbe6702019
10171017
refs/heads/revert-19050-revert-19006-error-bridging-integer-type: ecf752d54b05dd0a20f510f0bfa54a3fec3bcaca
1018-
refs/heads/rxwei-patch-1: 8efe352bfddf1794ec893aee9af70d2af76e2427
1018+
refs/heads/rxwei-patch-1: c62cd12da3652235b2729e99ea8b956acb456057
10191019
refs/heads/shahmishal-patch-1: e58ec0f7488258d42bef51bc3e6d7b3dc74d7b2a
10201020
refs/heads/typelist-existential: 4046359efd541fb5c72d69a92eefc0a784df8f5e
10211021
refs/tags/swift-4.2-DEVELOPMENT-SNAPSHOT-2018-08-20-a: 4319ba09e4fb8650ee86061075c74a016b6baab9

branches/rxwei-patch-1/lib/ClangImporter/ImportDecl.cpp

Lines changed: 112 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,112 @@ makeIndirectFieldAccessors(ClangImporter::Implementation &Impl,
920920
return { getterDecl, setterDecl };
921921
}
922922

923+
/// Synthesizer for the body of a union field getter.
924+
static std::pair<BraceStmt *, bool>
925+
synthesizeUnionFieldGetterBody(AbstractFunctionDecl *afd, void *context) {
926+
auto getterDecl = cast<AccessorDecl>(afd);
927+
ASTContext &ctx = getterDecl->getASTContext();
928+
auto importedFieldDecl = static_cast<VarDecl *>(context);
929+
930+
auto selfDecl = getterDecl->getImplicitSelfDecl();
931+
932+
auto selfRef = new (ctx) DeclRefExpr(selfDecl, DeclNameLoc(),
933+
/*implicit*/ true);
934+
selfRef->setType(selfDecl->getInterfaceType());
935+
936+
auto reinterpretCast = cast<FuncDecl>(getBuiltinValueDecl(
937+
ctx, ctx.getIdentifier("reinterpretCast")));
938+
939+
ConcreteDeclRef reinterpretCastRef(
940+
reinterpretCast,
941+
SubstitutionMap::get(reinterpretCast->getGenericSignature(),
942+
{selfDecl->getInterfaceType(),
943+
importedFieldDecl->getInterfaceType()},
944+
ArrayRef<ProtocolConformanceRef>()));
945+
auto reinterpretCastRefExpr
946+
= new (ctx) DeclRefExpr(reinterpretCastRef, DeclNameLoc(),
947+
/*implicit*/ true);
948+
reinterpretCastRefExpr->setType(
949+
FunctionType::get(
950+
AnyFunctionType::Param(selfDecl->getInterfaceType()),
951+
importedFieldDecl->getInterfaceType()));
952+
953+
auto reinterpreted = CallExpr::createImplicit(ctx, reinterpretCastRefExpr,
954+
{ selfRef },
955+
{ Identifier() });
956+
reinterpreted->setType(importedFieldDecl->getInterfaceType());
957+
reinterpreted->setThrows(false);
958+
auto ret = new (ctx) ReturnStmt(SourceLoc(), reinterpreted);
959+
auto body = BraceStmt::create(ctx, SourceLoc(), ASTNode(ret), SourceLoc(),
960+
/*implicit*/ true);
961+
return { body, /*isTypeChecked=*/true };
962+
}
963+
964+
/// Synthesizer for the body of a union field setter.
965+
static std::pair<BraceStmt *, bool>
966+
synthesizeUnionFieldSetterBody(AbstractFunctionDecl *afd, void *context) {
967+
auto setterDecl = cast<AccessorDecl>(afd);
968+
ASTContext &ctx = setterDecl->getASTContext();
969+
970+
auto inoutSelfDecl = setterDecl->getImplicitSelfDecl();
971+
972+
auto inoutSelfRef = new (ctx) DeclRefExpr(inoutSelfDecl, DeclNameLoc(),
973+
/*implicit*/ true);
974+
inoutSelfRef->setType(LValueType::get(inoutSelfDecl->getInterfaceType()));
975+
auto inoutSelf = new (ctx) InOutExpr(SourceLoc(), inoutSelfRef,
976+
setterDecl->mapTypeIntoContext(inoutSelfDecl->getValueInterfaceType()),
977+
/*implicit*/ true);
978+
inoutSelf->setType(InOutType::get(inoutSelfDecl->getInterfaceType()));
979+
980+
auto newValueDecl = setterDecl->getParameters()->get(0);
981+
982+
auto newValueRef = new (ctx) DeclRefExpr(newValueDecl, DeclNameLoc(),
983+
/*implicit*/ true);
984+
newValueRef->setType(newValueDecl->getInterfaceType());
985+
986+
auto addressofFn = cast<FuncDecl>(getBuiltinValueDecl(
987+
ctx, ctx.getIdentifier("addressof")));
988+
ConcreteDeclRef addressofFnRef(addressofFn,
989+
SubstitutionMap::get(addressofFn->getGenericSignature(),
990+
{inoutSelfDecl->getInterfaceType()},
991+
ArrayRef<ProtocolConformanceRef>()));
992+
auto addressofFnRefExpr
993+
= new (ctx) DeclRefExpr(addressofFnRef, DeclNameLoc(), /*implicit*/ true);
994+
addressofFnRefExpr->setType(
995+
FunctionType::get(
996+
AnyFunctionType::Param(inoutSelfDecl->getInterfaceType(),
997+
Identifier(),
998+
ParameterTypeFlags().withInOut(true)),
999+
ctx.TheRawPointerType));
1000+
auto selfPointer = CallExpr::createImplicit(ctx, addressofFnRefExpr,
1001+
{ inoutSelf },
1002+
{ Identifier() });
1003+
selfPointer->setType(ctx.TheRawPointerType);
1004+
selfPointer->setThrows(false);
1005+
1006+
auto initializeFn = cast<FuncDecl>(getBuiltinValueDecl(
1007+
ctx, ctx.getIdentifier("initialize")));
1008+
ConcreteDeclRef initializeFnRef(initializeFn,
1009+
SubstitutionMap::get(initializeFn->getGenericSignature(),
1010+
{newValueDecl->getInterfaceType()},
1011+
ArrayRef<ProtocolConformanceRef>()));
1012+
auto initializeFnRefExpr
1013+
= new (ctx) DeclRefExpr(initializeFnRef, DeclNameLoc(), /*implicit*/ true);
1014+
initializeFnRefExpr->setType(
1015+
FunctionType::get({AnyFunctionType::Param(newValueDecl->getInterfaceType()),
1016+
AnyFunctionType::Param(ctx.TheRawPointerType)},
1017+
TupleType::getEmpty(ctx)));
1018+
auto initialize = CallExpr::createImplicit(ctx, initializeFnRefExpr,
1019+
{ newValueRef, selfPointer },
1020+
{ Identifier(), Identifier() });
1021+
initialize->setType(TupleType::getEmpty(ctx));
1022+
initialize->setThrows(false);
1023+
1024+
auto body = BraceStmt::create(ctx, SourceLoc(), { initialize }, SourceLoc(),
1025+
/*implicit*/ true);
1026+
return { body, /*isTypeChecked=*/true };
1027+
}
1028+
9231029
/// Build the union field getter and setter.
9241030
///
9251031
/// \code
@@ -945,114 +1051,18 @@ makeUnionFieldAccessors(ClangImporter::Implementation &Impl,
9451051
auto getterDecl = makeFieldGetterDecl(Impl,
9461052
importedUnionDecl,
9471053
importedFieldDecl);
1054+
getterDecl->setBodySynthesizer(synthesizeUnionFieldGetterBody,
1055+
importedFieldDecl);
1056+
getterDecl->getAttrs().add(new (C) TransparentAttr(/*implicit*/ true));
9481057

9491058
auto setterDecl = makeFieldSetterDecl(Impl,
9501059
importedUnionDecl,
9511060
importedFieldDecl);
1061+
setterDecl->setBodySynthesizer(synthesizeUnionFieldSetterBody,
1062+
importedFieldDecl);
1063+
setterDecl->getAttrs().add(new (C) TransparentAttr(/*implicit*/ true));
9521064

9531065
makeComputed(importedFieldDecl, getterDecl, setterDecl);
954-
955-
// Don't bother synthesizing the body if we've already finished type-checking.
956-
if (Impl.hasFinishedTypeChecking())
957-
return { getterDecl, setterDecl };
958-
959-
// Synthesize the getter body
960-
{
961-
auto selfDecl = getterDecl->getImplicitSelfDecl();
962-
963-
auto selfRef = new (C) DeclRefExpr(selfDecl, DeclNameLoc(),
964-
/*implicit*/ true);
965-
selfRef->setType(selfDecl->getInterfaceType());
966-
967-
auto reinterpretCast = cast<FuncDecl>(getBuiltinValueDecl(
968-
C, C.getIdentifier("reinterpretCast")));
969-
970-
ConcreteDeclRef reinterpretCastRef(
971-
reinterpretCast,
972-
SubstitutionMap::get(reinterpretCast->getGenericSignature(),
973-
{selfDecl->getInterfaceType(),
974-
importedFieldDecl->getInterfaceType()},
975-
ArrayRef<ProtocolConformanceRef>()));
976-
auto reinterpretCastRefExpr
977-
= new (C) DeclRefExpr(reinterpretCastRef, DeclNameLoc(),
978-
/*implicit*/ true);
979-
reinterpretCastRefExpr->setType(
980-
FunctionType::get(
981-
AnyFunctionType::Param(selfDecl->getInterfaceType()),
982-
importedFieldDecl->getInterfaceType()));
983-
984-
auto reinterpreted = CallExpr::createImplicit(C, reinterpretCastRefExpr,
985-
{ selfRef },
986-
{ Identifier() });
987-
reinterpreted->setType(importedFieldDecl->getInterfaceType());
988-
reinterpreted->setThrows(false);
989-
auto ret = new (C) ReturnStmt(SourceLoc(), reinterpreted);
990-
auto body = BraceStmt::create(C, SourceLoc(), ASTNode(ret), SourceLoc(),
991-
/*implicit*/ true);
992-
getterDecl->setBody(body, AbstractFunctionDecl::BodyKind::TypeChecked);
993-
getterDecl->getAttrs().add(new (C) TransparentAttr(/*implicit*/ true));
994-
}
995-
996-
// Synthesize the setter body
997-
{
998-
auto inoutSelfDecl = setterDecl->getImplicitSelfDecl();
999-
1000-
auto inoutSelfRef = new (C) DeclRefExpr(inoutSelfDecl, DeclNameLoc(),
1001-
/*implicit*/ true);
1002-
inoutSelfRef->setType(LValueType::get(inoutSelfDecl->getInterfaceType()));
1003-
auto inoutSelf = new (C) InOutExpr(SourceLoc(), inoutSelfRef,
1004-
importedUnionDecl->getDeclaredType(), /*implicit*/ true);
1005-
inoutSelf->setType(InOutType::get(inoutSelfDecl->getInterfaceType()));
1006-
1007-
auto newValueDecl = setterDecl->getParameters()->get(0);
1008-
1009-
auto newValueRef = new (C) DeclRefExpr(newValueDecl, DeclNameLoc(),
1010-
/*implicit*/ true);
1011-
newValueRef->setType(newValueDecl->getInterfaceType());
1012-
1013-
auto addressofFn = cast<FuncDecl>(getBuiltinValueDecl(
1014-
C, C.getIdentifier("addressof")));
1015-
ConcreteDeclRef addressofFnRef(addressofFn,
1016-
SubstitutionMap::get(addressofFn->getGenericSignature(),
1017-
{inoutSelfDecl->getInterfaceType()},
1018-
ArrayRef<ProtocolConformanceRef>()));
1019-
auto addressofFnRefExpr
1020-
= new (C) DeclRefExpr(addressofFnRef, DeclNameLoc(), /*implicit*/ true);
1021-
addressofFnRefExpr->setType(
1022-
FunctionType::get(AnyFunctionType::Param(inoutSelfDecl->getInterfaceType(),
1023-
Identifier(),
1024-
ParameterTypeFlags().withInOut(true)),
1025-
C.TheRawPointerType));
1026-
auto selfPointer = CallExpr::createImplicit(C, addressofFnRefExpr,
1027-
{ inoutSelf },
1028-
{ Identifier() });
1029-
selfPointer->setType(C.TheRawPointerType);
1030-
selfPointer->setThrows(false);
1031-
1032-
auto initializeFn = cast<FuncDecl>(getBuiltinValueDecl(
1033-
C, C.getIdentifier("initialize")));
1034-
ConcreteDeclRef initializeFnRef(initializeFn,
1035-
SubstitutionMap::get(initializeFn->getGenericSignature(),
1036-
{newValueDecl->getInterfaceType()},
1037-
ArrayRef<ProtocolConformanceRef>()));
1038-
auto initializeFnRefExpr
1039-
= new (C) DeclRefExpr(initializeFnRef, DeclNameLoc(), /*implicit*/ true);
1040-
initializeFnRefExpr->setType(
1041-
FunctionType::get({AnyFunctionType::Param(newValueDecl->getInterfaceType()),
1042-
AnyFunctionType::Param(C.TheRawPointerType)},
1043-
TupleType::getEmpty(C)));
1044-
auto initialize = CallExpr::createImplicit(C, initializeFnRefExpr,
1045-
{ newValueRef, selfPointer },
1046-
{ Identifier(), Identifier() });
1047-
initialize->setType(TupleType::getEmpty(C));
1048-
initialize->setThrows(false);
1049-
1050-
auto body = BraceStmt::create(C, SourceLoc(), { initialize }, SourceLoc(),
1051-
/*implicit*/ true);
1052-
setterDecl->setBody(body, AbstractFunctionDecl::BodyKind::TypeChecked);
1053-
setterDecl->getAttrs().add(new (C) TransparentAttr(/*implicit*/ true));
1054-
}
1055-
10561066
return { getterDecl, setterDecl };
10571067
}
10581068

0 commit comments

Comments
 (0)