@@ -920,6 +920,112 @@ makeIndirectFieldAccessors(ClangImporter::Implementation &Impl,
920
920
return { getterDecl, setterDecl };
921
921
}
922
922
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
+
923
1029
// / Build the union field getter and setter.
924
1030
// /
925
1031
// / \code
@@ -945,114 +1051,18 @@ makeUnionFieldAccessors(ClangImporter::Implementation &Impl,
945
1051
auto getterDecl = makeFieldGetterDecl (Impl,
946
1052
importedUnionDecl,
947
1053
importedFieldDecl);
1054
+ getterDecl->setBodySynthesizer (synthesizeUnionFieldGetterBody,
1055
+ importedFieldDecl);
1056
+ getterDecl->getAttrs ().add (new (C) TransparentAttr (/* implicit*/ true ));
948
1057
949
1058
auto setterDecl = makeFieldSetterDecl (Impl,
950
1059
importedUnionDecl,
951
1060
importedFieldDecl);
1061
+ setterDecl->setBodySynthesizer (synthesizeUnionFieldSetterBody,
1062
+ importedFieldDecl);
1063
+ setterDecl->getAttrs ().add (new (C) TransparentAttr (/* implicit*/ true ));
952
1064
953
1065
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
-
1056
1066
return { getterDecl, setterDecl };
1057
1067
}
1058
1068
0 commit comments