@@ -361,6 +361,26 @@ namespace {
361
361
return base.getOldType ();
362
362
}
363
363
364
+ // / Check whether it is possible to have an ObjC key path string for the keypath expression
365
+ // / and set the key path string, if yes
366
+ void checkAndSetObjCKeyPathString (KeyPathExpr *keyPath) {
367
+ if (cs.getASTContext ().LangOpts .EnableObjCInterop ) {
368
+ SmallString<64 > compatStringBuf;
369
+ if (buildObjCKeyPathString (keyPath, compatStringBuf)) {
370
+ auto stringCopy = cs.getASTContext ().AllocateCopy <char >(compatStringBuf.begin (),
371
+ compatStringBuf.end ());
372
+ auto stringExpr = new (cs.getASTContext ()) StringLiteralExpr (
373
+ StringRef (stringCopy, compatStringBuf.size ()),
374
+ SourceRange (),
375
+ /* implicit*/ true );
376
+ cs.setType (
377
+ stringExpr,
378
+ cs.getASTContext ().getStringDecl ()->getDeclaredInterfaceType ());
379
+ keyPath->setObjCStringLiteralExpr (stringExpr);
380
+ }
381
+ }
382
+ }
383
+
364
384
// Returns None if the AST does not contain enough information to recover
365
385
// substitutions; this is different from an Optional(SubstitutionMap()),
366
386
// indicating a valid call to a non-generic operator.
@@ -2007,6 +2027,11 @@ namespace {
2007
2027
keyPath->setParsedPath (componentExpr);
2008
2028
keyPath->resolveComponents (ctx, components);
2009
2029
cs.cacheExprTypes (keyPath);
2030
+
2031
+ // See whether there's an equivalent ObjC key path string we can produce
2032
+ // for interop purposes.
2033
+ checkAndSetObjCKeyPathString (keyPath);
2034
+
2010
2035
return keyPath;
2011
2036
}
2012
2037
@@ -4711,22 +4736,7 @@ namespace {
4711
4736
4712
4737
// See whether there's an equivalent ObjC key path string we can produce
4713
4738
// for interop purposes.
4714
- if (cs.getASTContext ().LangOpts .EnableObjCInterop ) {
4715
- SmallString<64 > compatStringBuf;
4716
- if (buildObjCKeyPathString (E, compatStringBuf)) {
4717
- auto stringCopy =
4718
- cs.getASTContext ().AllocateCopy <char >(compatStringBuf.begin (),
4719
- compatStringBuf.end ());
4720
- auto stringExpr = new (cs.getASTContext ()) StringLiteralExpr (
4721
- StringRef (stringCopy, compatStringBuf.size ()),
4722
- SourceRange (),
4723
- /* implicit*/ true );
4724
- cs.setType (
4725
- stringExpr,
4726
- cs.getASTContext ().getStringDecl ()->getDeclaredInterfaceType ());
4727
- E->setObjCStringLiteralExpr (stringExpr);
4728
- }
4729
- }
4739
+ checkAndSetObjCKeyPathString (E);
4730
4740
4731
4741
// The final component type ought to line up with the leaf type of the
4732
4742
// key path.
0 commit comments