@@ -1060,65 +1060,6 @@ namespace {
1060
1060
// / Describes either a type or the name of a type to be resolved.
1061
1061
using TypeOrName = llvm::PointerUnion<Identifier, Type>;
1062
1062
1063
- // / Convert the given literal expression via a protocol pair.
1064
- // /
1065
- // / This routine handles the two-step literal conversion process used
1066
- // / by integer, float, character, extended grapheme cluster, and string
1067
- // / literals. The first step uses \c builtinProtocol while the second
1068
- // / step uses \c protocol.
1069
- // /
1070
- // / \param literal The literal expression.
1071
- // /
1072
- // / \param type The literal type. This type conforms to \c protocol,
1073
- // / and may also conform to \c builtinProtocol.
1074
- // /
1075
- // / \param openedType The literal type as it was opened in the type system.
1076
- // /
1077
- // / \param protocol The protocol that describes the literal requirement.
1078
- // /
1079
- // / \param literalType Either the name of the associated type in
1080
- // / \c protocol that describes the argument type of the conversion function
1081
- // / (\c literalFuncName) or the argument type itself.
1082
- // /
1083
- // / \param literalFuncName The name of the conversion function requirement
1084
- // / in \c protocol.
1085
- // /
1086
- // / \param builtinProtocol The "builtin" form of the protocol, which
1087
- // / always takes builtin types and can only be properly implemented
1088
- // / by standard library types. If \c type does not conform to this
1089
- // / protocol, it's literal type will.
1090
- // /
1091
- // / \param builtinLiteralType Either the name of the associated type in
1092
- // / \c builtinProtocol that describes the argument type of the builtin
1093
- // / conversion function (\c builtinLiteralFuncName) or the argument type
1094
- // / itself.
1095
- // /
1096
- // / \param builtinLiteralFuncName The name of the conversion function
1097
- // / requirement in \c builtinProtocol.
1098
- // /
1099
- // / \param isBuiltinArgType Function that determines whether the given
1100
- // / type is acceptable as the argument type for the builtin conversion.
1101
- // /
1102
- // / \param brokenProtocolDiag The diagnostic to emit if the protocol
1103
- // / is broken.
1104
- // /
1105
- // / \param brokenBuiltinProtocolDiag The diagnostic to emit if the builtin
1106
- // / protocol is broken.
1107
- // /
1108
- // / \returns the converted literal expression.
1109
- Expr *convertLiteral (Expr *literal,
1110
- Type type,
1111
- Type openedType,
1112
- ProtocolDecl *protocol,
1113
- TypeOrName literalType,
1114
- DeclName literalFuncName,
1115
- ProtocolDecl *builtinProtocol,
1116
- TypeOrName builtinLiteralType,
1117
- DeclName builtinLiteralFuncName,
1118
- bool (*isBuiltinArgType)(Type),
1119
- Diag<> brokenProtocolDiag,
1120
- Diag<> brokenBuiltinProtocolDiag);
1121
-
1122
1063
// / Convert the given literal expression via a protocol pair.
1123
1064
// /
1124
1065
// / This routine handles the two-step literal conversion process used
@@ -1903,19 +1844,11 @@ namespace {
1903
1844
DeclName builtinInitName (tc.Context , DeclBaseName::createConstructor (),
1904
1845
{ tc.Context .Id_builtinFloatLiteral });
1905
1846
1906
- return convertLiteral (
1907
- expr,
1908
- type,
1909
- cs.getType (expr),
1910
- protocol,
1911
- tc.Context .Id_FloatLiteralType ,
1912
- initName,
1913
- builtinProtocol,
1914
- maxType,
1915
- builtinInitName,
1916
- nullptr ,
1917
- diag::float_literal_broken_proto,
1918
- diag::builtin_float_literal_broken_proto);
1847
+ expr->setBuiltinType (maxType);
1848
+ return convertLiteralInPlace (
1849
+ expr, type, protocol, tc.Context .Id_FloatLiteralType , initName,
1850
+ builtinProtocol, builtinInitName, diag::float_literal_broken_proto,
1851
+ diag::builtin_float_literal_broken_proto);
1919
1852
}
1920
1853
1921
1854
Expr *visitBooleanLiteralExpr (BooleanLiteralExpr *expr) {
@@ -6865,157 +6798,6 @@ ExprRewriter::coerceObjectArgumentToType(Expr *expr,
6865
6798
/* isImplicit*/ true ));
6866
6799
}
6867
6800
6868
- Expr *ExprRewriter::convertLiteral (Expr *literal,
6869
- Type type,
6870
- Type openedType,
6871
- ProtocolDecl *protocol,
6872
- TypeOrName literalType,
6873
- DeclName literalFuncName,
6874
- ProtocolDecl *builtinProtocol,
6875
- TypeOrName builtinLiteralType,
6876
- DeclName builtinLiteralFuncName,
6877
- bool (*isBuiltinArgType)(Type),
6878
- Diag<> brokenProtocolDiag,
6879
- Diag<> brokenBuiltinProtocolDiag) {
6880
- auto &tc = cs.getTypeChecker ();
6881
-
6882
- auto getType = [&](const Expr *E) -> Type {
6883
- return cs.getType (E);
6884
- };
6885
-
6886
- auto setType = [&](Expr *E, Type Ty) {
6887
- cs.setType (E, Ty);
6888
- };
6889
-
6890
- // If coercing a literal to an unresolved type, we don't try to look up the
6891
- // witness members, just do it.
6892
- if (type->is <UnresolvedType>()) {
6893
- // Instead of updating the literal expr in place, allocate a new node. This
6894
- // avoids issues where Builtin types end up on expr nodes and pollute
6895
- // diagnostics.
6896
- literal = cast<LiteralExpr>(literal)->shallowClone (tc.Context , setType,
6897
- getType);
6898
-
6899
- // The literal expression has this type.
6900
- cs.setType (literal, type);
6901
- return literal;
6902
- }
6903
-
6904
- // Check whether this literal type conforms to the builtin protocol.
6905
- Optional<ProtocolConformanceRef> builtinConformance;
6906
- if (builtinProtocol &&
6907
- (builtinConformance =
6908
- tc.conformsToProtocol (
6909
- type, builtinProtocol, cs.DC ,
6910
- (ConformanceCheckFlags::InExpression)))) {
6911
-
6912
- // Find the builtin argument type we'll use.
6913
- Type argType;
6914
- if (builtinLiteralType.is <Type>())
6915
- argType = builtinLiteralType.get <Type>();
6916
- else
6917
- argType = tc.getWitnessType (type, builtinProtocol,
6918
- *builtinConformance,
6919
- builtinLiteralType.get <Identifier>(),
6920
- brokenBuiltinProtocolDiag);
6921
-
6922
- if (!argType)
6923
- return nullptr ;
6924
-
6925
- // Make sure it's of an appropriate builtin type.
6926
- if (isBuiltinArgType && !isBuiltinArgType (argType)) {
6927
- tc.diagnose (builtinProtocol->getLoc (), brokenBuiltinProtocolDiag);
6928
- return nullptr ;
6929
- }
6930
-
6931
- // Instead of updating the literal expr in place, allocate a new node. This
6932
- // avoids issues where Builtin types end up on expr nodes and pollute
6933
- // diagnostics.
6934
- literal = cast<LiteralExpr>(literal)->shallowClone (tc.Context , setType,
6935
- getType);
6936
-
6937
- // The literal expression has this type.
6938
- cs.setType (literal, argType);
6939
-
6940
- // Call the builtin conversion operation.
6941
- // FIXME: Bogus location info.
6942
- Expr *base =
6943
- TypeExpr::createImplicitHack (literal->getLoc (), type, tc.Context );
6944
-
6945
- cs.cacheExprTypes (base);
6946
- cs.setExprTypes (base);
6947
- cs.setExprTypes (literal);
6948
- SmallVector<Expr *, 1 > arguments = { literal };
6949
-
6950
- Expr *result = tc.callWitness (base, dc,
6951
- builtinProtocol, *builtinConformance,
6952
- builtinLiteralFuncName,
6953
- arguments,
6954
- brokenBuiltinProtocolDiag);
6955
- if (result)
6956
- cs.cacheExprTypes (result);
6957
-
6958
- return result;
6959
- }
6960
-
6961
- // This literal type must conform to the (non-builtin) protocol.
6962
- assert (protocol && " requirements should have stopped recursion" );
6963
- auto conformance = tc.conformsToProtocol (type, protocol, cs.DC ,
6964
- ConformanceCheckFlags::InExpression);
6965
- assert (conformance && " must conform to literal protocol" );
6966
-
6967
- // Figure out the (non-builtin) argument type if there is one.
6968
- Type argType;
6969
- if (literalType.is <Identifier>() &&
6970
- literalType.get <Identifier>().empty ()) {
6971
- // If there is no argument to the constructor function, then just pass in
6972
- // the empty tuple.
6973
- literal =
6974
- cs.cacheType (
6975
- TupleExpr::createEmpty (tc.Context , literal->getLoc (),
6976
- literal->getLoc (),
6977
- /* implicit*/ !literal->getLoc ().isValid ()));
6978
- } else {
6979
- // Otherwise, figure out the type of the constructor function and coerce to
6980
- // it.
6981
- if (literalType.is <Type>())
6982
- argType = literalType.get <Type>();
6983
- else
6984
- argType = tc.getWitnessType (type, protocol, *conformance,
6985
- literalType.get <Identifier>(),
6986
- brokenProtocolDiag);
6987
- if (!argType)
6988
- return nullptr ;
6989
-
6990
- // Convert the literal to the non-builtin argument type via the
6991
- // builtin protocol, first.
6992
- // FIXME: Do we need an opened type here?
6993
- literal = convertLiteral (literal, argType, argType, nullptr , Identifier (),
6994
- Identifier (), builtinProtocol,
6995
- builtinLiteralType, builtinLiteralFuncName,
6996
- isBuiltinArgType, brokenProtocolDiag,
6997
- brokenBuiltinProtocolDiag);
6998
- if (!literal)
6999
- return nullptr ;
7000
- }
7001
-
7002
- // Convert the resulting expression to the final literal type.
7003
- // FIXME: Bogus location info.
7004
- Expr *base = TypeExpr::createImplicitHack (literal->getLoc (), type,
7005
- tc.Context );
7006
- cs.cacheExprTypes (base);
7007
- cs.setExprTypes (base);
7008
- cs.setExprTypes (literal);
7009
-
7010
- literal = tc.callWitness (base, dc,
7011
- protocol, *conformance, literalFuncName,
7012
- literal, brokenProtocolDiag);
7013
- if (literal)
7014
- cs.cacheExprTypes (literal);
7015
-
7016
- return literal;
7017
- }
7018
-
7019
6801
Expr *ExprRewriter::convertLiteralInPlace (Expr *literal,
7020
6802
Type type,
7021
6803
ProtocolDecl *protocol,
0 commit comments