@@ -457,12 +457,34 @@ void SILGenFunction::emitArtificialTopLevel(ClassDecl *mainClass) {
457
457
CanType IUOptNSStringTy
458
458
= ImplicitlyUnwrappedOptionalType::get (NSStringTy)->getCanonicalType ();
459
459
460
+ // Look up UIApplicationMain.
461
+ // FIXME: Doing an AST lookup here is gross and not entirely sound;
462
+ // we're getting away with it because the types are guaranteed to already
463
+ // be imported.
464
+ ASTContext &ctx = getASTContext ();
465
+ Module *UIKit = ctx.getLoadedModule (ctx.getIdentifier (" UIKit" ));
466
+ SmallVector<ValueDecl *, 1 > results;
467
+ UIKit->lookupQualified (UIKit->getDeclaredType (),
468
+ ctx.getIdentifier (" UIApplicationMain" ),
469
+ NL_QualifiedDefault,
470
+ /* resolver*/ nullptr ,
471
+ results);
472
+ assert (!results.empty () && " couldn't find UIApplicationMain in UIKit" );
473
+ assert (results.size () == 1 && " more than one UIApplicationMain?" );
474
+
475
+ SILDeclRef mainRef{results.front (), ResilienceExpansion::Minimal,
476
+ SILDeclRef::ConstructAtNaturalUncurryLevel,
477
+ /* isForeign*/ true };
478
+ auto UIApplicationMainFn = SGM.M .getOrCreateFunction (mainClass, mainRef,
479
+ NotForDefinition);
480
+ auto fnTy = UIApplicationMainFn->getLoweredFunctionType ();
481
+
460
482
// Get the class name as a string using NSStringFromClass.
461
483
CanType mainClassTy = mainClass->getDeclaredTypeInContext ()->getCanonicalType ();
462
484
CanType mainClassMetaty = CanMetatypeType::get (mainClassTy,
463
485
MetatypeRepresentation::ObjC);
464
486
ProtocolDecl *anyObjectProtocol =
465
- getASTContext () .getProtocol (KnownProtocolKind::AnyObject);
487
+ ctx .getProtocol (KnownProtocolKind::AnyObject);
466
488
auto mainClassAnyObjectConformance = ProtocolConformanceRef (
467
489
*SGM.M .getSwiftModule ()->lookupConformance (mainClassTy, anyObjectProtocol,
468
490
nullptr ));
@@ -482,7 +504,7 @@ void SILGenFunction::emitArtificialTopLevel(ClassDecl *mainClass) {
482
504
SILResultInfo (OptNSStringTy,
483
505
ResultConvention::Autoreleased),
484
506
/* error result*/ None,
485
- getASTContext () );
507
+ ctx );
486
508
auto NSStringFromClassFn
487
509
= SGM.M .getOrCreateFunction (mainClass, " NSStringFromClass" ,
488
510
SILLinkage::PublicExternal,
@@ -493,22 +515,35 @@ void SILGenFunction::emitArtificialTopLevel(ClassDecl *mainClass) {
493
515
SILType::getPrimitiveObjectType (mainClassMetaty));
494
516
metaTy = B.createInitExistentialMetatype (mainClass, metaTy,
495
517
SILType::getPrimitiveObjectType (anyObjectMetaTy),
496
- getASTContext () .AllocateCopy (
518
+ ctx .AllocateCopy (
497
519
llvm::makeArrayRef (mainClassAnyObjectConformance)));
498
520
SILValue optName = B.createApply (mainClass,
499
521
NSStringFromClass,
500
522
NSStringFromClass->getType (),
501
523
SILType::getPrimitiveObjectType (OptNSStringTy),
502
524
{}, metaTy);
503
- SILValue iuoptName = B.createUncheckedBitCast (mainClass, optName,
504
- SILType::getPrimitiveObjectType (IUOptNSStringTy));
505
525
506
- // Call UIApplicationMain.
507
- auto UIApplicationMainFn = SGM.M .lookUpFunction (" UIApplicationMain" );
508
- assert (UIApplicationMainFn && " UIKit not imported?" );
526
+ // Fix up the string parameters to have the right type.
527
+ SILType nameArgTy = fnTy->getSILArgumentType (3 );
528
+ assert (nameArgTy == fnTy->getSILArgumentType (2 ));
529
+ auto managedName = ManagedValue::forUnmanaged (optName);
530
+ SILValue nilValue;
531
+ if (optName->getType () == nameArgTy) {
532
+ nilValue = getOptionalNoneValue (mainClass,
533
+ getTypeLowering (OptNSStringTy));
534
+ } else {
535
+ assert (nameArgTy.getSwiftRValueType () == IUOptNSStringTy);
536
+ nilValue = getOptionalNoneValue (mainClass,
537
+ getTypeLowering (IUOptNSStringTy));
538
+ managedName = emitOptionalToOptional (
539
+ mainClass, managedName,
540
+ SILType::getPrimitiveObjectType (IUOptNSStringTy),
541
+ [](SILGenFunction &, SILLocation, ManagedValue input, SILType) {
542
+ return input;
543
+ });
544
+ }
509
545
510
546
// Fix up argv to have the right type.
511
- auto fnTy = UIApplicationMainFn->getLoweredFunctionType ();
512
547
auto argvTy = fnTy->getSILArgumentType (1 );
513
548
514
549
SILType unwrappedTy = argvTy;
@@ -517,35 +552,36 @@ void SILGenFunction::emitArtificialTopLevel(ClassDecl *mainClass) {
517
552
unwrappedTy = SILType::getPrimitiveObjectType (canInnerTy);
518
553
}
519
554
555
+ auto managedArgv = ManagedValue::forUnmanaged (argv);
556
+
520
557
if (unwrappedTy != argv->getType ()) {
521
558
auto converted =
522
- emitPointerToPointer (mainClass, ManagedValue::forUnmanaged (argv) ,
559
+ emitPointerToPointer (mainClass, managedArgv ,
523
560
argv->getType ().getSwiftRValueType (),
524
561
unwrappedTy.getSwiftRValueType ());
525
- argv = converted. getUnmanagedSingleValue (*this , mainClass);
562
+ managedArgv = std::move ( converted). getAsSingleValue (*this , mainClass);
526
563
}
527
564
528
565
if (unwrappedTy != argvTy) {
529
- argv = getOptionalSomeValue (mainClass, ManagedValue::forUnmanaged (argv) ,
530
- getTypeLowering (argvTy)). getUnmanagedValue ( );
566
+ managedArgv = getOptionalSomeValue (mainClass, managedArgv ,
567
+ getTypeLowering (argvTy));
531
568
}
532
569
533
570
auto UIApplicationMain = B.createFunctionRef (mainClass, UIApplicationMainFn);
534
- auto nil = B.createEnum (mainClass, SILValue (),
535
- getASTContext ().getImplicitlyUnwrappedOptionalNoneDecl (),
536
- SILType::getPrimitiveObjectType (IUOptNSStringTy));
537
571
538
- SILValue args[] = { argc, argv, nil, iuoptName };
572
+ SILValue args[] = {argc, managedArgv.getValue (), nilValue,
573
+ managedName.getValue ()};
539
574
540
575
B.createApply (mainClass, UIApplicationMain,
541
576
UIApplicationMain->getType (),
542
577
argc->getType (), {}, args);
543
578
SILValue r = B.createIntegerLiteral (mainClass,
544
- SILType::getBuiltinIntegerType (32 , getASTContext () ), 0 );
579
+ SILType::getBuiltinIntegerType (32 , ctx ), 0 );
545
580
auto rType = F.getLoweredFunctionType ()->getSingleResult ().getSILType ();
546
581
if (r->getType () != rType)
547
582
r = B.createStruct (mainClass, rType, r);
548
583
584
+ Cleanups.emitCleanupsForReturn (mainClass);
549
585
B.createReturn (mainClass, r);
550
586
return ;
551
587
}
0 commit comments