@@ -504,10 +504,17 @@ createOptRecordFile(StringRef Filename, DiagnosticEngine &DE) {
504
504
return File;
505
505
}
506
506
507
+ struct PostSILGenInputs {
508
+ std::unique_ptr<SILModule> TheSILModule;
509
+ bool astGuaranteedToCorrespondToSIL;
510
+ ModuleOrSourceFile ModuleOrPrimarySourceFile;
511
+ };
512
+
507
513
static bool performCompileStepsPostSILGen (CompilerInstance &Instance,
508
514
CompilerInvocation &Invocation,
509
515
std::unique_ptr<SILModule> SM,
510
516
bool astGuaranteedToCorrespondToSIL,
517
+ ModuleOrSourceFile MSF,
511
518
bool moduleIsPublic,
512
519
int &ReturnValue,
513
520
FrontendObserver *observer,
@@ -783,16 +790,13 @@ static bool performCompile(CompilerInstance &Instance,
783
790
assert (Action >= FrontendOptions::ActionType::EmitSILGen &&
784
791
" All actions not requiring SILGen must have been handled!" );
785
792
786
- // The second boolean in each std::pair<> in this std::deque<> indicates
787
- // whether the SIL is guaranteed to correspond to the the AST. This might be
788
- // false if we loaded SIL from an SIB.
789
- std::deque<std::pair<std::unique_ptr<SILModule>, bool >> SMs;
793
+ auto mod = Instance.getMainModule ();
794
+ std::deque<PostSILGenInputs> PSGIs;
790
795
if (auto SM = Instance.takeSILModule ()) {
791
- SMs .push_back (std::make_pair ( std:: move (SM), false ) );
796
+ PSGIs .push_back (PostSILGenInputs{ std::move (SM), false , mod} );
792
797
}
793
798
794
- if (SMs.empty ()) {
795
- auto mod = Instance.getMainModule ();
799
+ if (PSGIs.empty ()) {
796
800
auto fileIsSIB = [](const FileUnit *File) -> bool {
797
801
auto SASTF = dyn_cast<SerializedASTFile>(File);
798
802
return SASTF && SASTF->isSIB ();
@@ -807,9 +811,10 @@ static bool performCompile(CompilerInstance &Instance,
807
811
InputFile::
808
812
convertBufferNameFromLLVM_getFileOrSTDIN_toSwiftConventions (
809
813
SASTF->getFilename ()))) {
810
- assert (SMs .empty () && " Can only handle one primary AST input" );
814
+ assert (PSGIs .empty () && " Can only handle one primary AST input" );
811
815
auto SM = performSILGeneration (*SASTF, SILOpts, None);
812
- SMs.push_back (std::make_pair (std::move (SM), !fileIsSIB (SASTF)));
816
+ PSGIs.push_back (
817
+ PostSILGenInputs{std::move (SM), !fileIsSIB (SASTF), mod});
813
818
}
814
819
}
815
820
}
@@ -819,25 +824,26 @@ static bool performCompile(CompilerInstance &Instance,
819
824
// once for each such input.
820
825
for (auto *PrimaryFile : Instance.getPrimarySourceFiles ()) {
821
826
auto SM = performSILGeneration (*PrimaryFile, SILOpts, None);
822
- SMs.push_back (std::make_pair (std::move (SM), !fileIsSIB (PrimaryFile)));
827
+ PSGIs.push_back (PostSILGenInputs{
828
+ std::move (SM), !fileIsSIB (PrimaryFile), PrimaryFile});
823
829
}
824
830
}
825
831
} else {
826
832
// If we have no primary inputs we are in WMO mode and need to build a
827
833
// SILModule for the entire module.
828
834
auto SM = performSILGeneration (mod, SILOpts, true );
829
- SMs.push_back (std::make_pair (std::move (SM),
830
- llvm::none_of (mod->getFiles (),
831
- fileIsSIB)));
835
+ PSGIs.push_back (PostSILGenInputs{
836
+ std::move (SM), llvm::none_of (mod->getFiles (), fileIsSIB), mod});
832
837
}
833
838
}
834
839
835
- while (!SMs .empty ()) {
836
- auto pair = std::move (SMs .front ());
837
- SMs .pop_front ();
840
+ while (!PSGIs .empty ()) {
841
+ auto PSGI = std::move (PSGIs .front ());
842
+ PSGIs .pop_front ();
838
843
if (performCompileStepsPostSILGen (Instance, Invocation,
839
- std::move (pair.first ),
840
- pair.second ,
844
+ std::move (PSGI.TheSILModule ),
845
+ PSGI.astGuaranteedToCorrespondToSIL ,
846
+ PSGI.ModuleOrPrimarySourceFile ,
841
847
moduleIsPublic,
842
848
ReturnValue, observer, Stats))
843
849
return true ;
@@ -850,6 +856,7 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
850
856
CompilerInvocation &Invocation,
851
857
std::unique_ptr<SILModule> SM,
852
858
bool astGuaranteedToCorrespondToSIL,
859
+ ModuleOrSourceFile MSF,
853
860
bool moduleIsPublic,
854
861
int &ReturnValue,
855
862
FrontendObserver *observer,
@@ -883,14 +890,13 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
883
890
if (Invocation.getSILOptions ().LinkMode == SILOptions::LinkAll)
884
891
performSILLinking (SM.get (), true );
885
892
886
- auto DC = Instance.getPrimarySourceFileOrMainModule ();
887
893
if (!opts.ModuleOutputPath .empty ()) {
888
894
SerializationOptions serializationOpts;
889
895
serializationOpts.OutputPath = opts.ModuleOutputPath .c_str ();
890
896
serializationOpts.SerializeAllSIL = true ;
891
897
serializationOpts.IsSIB = true ;
892
898
893
- serialize (DC , serializationOpts, SM.get ());
899
+ serialize (MSF , serializationOpts, SM.get ());
894
900
}
895
901
return Context.hadError ();
896
902
}
@@ -939,7 +945,6 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
939
945
940
946
auto SerializeSILModuleAction = [&]() {
941
947
if (!opts.ModuleOutputPath .empty () || !opts.ModuleDocOutputPath .empty ()) {
942
- auto DC = Instance.getPrimarySourceFileOrMainModule ();
943
948
if (!opts.ModuleOutputPath .empty ()) {
944
949
SerializationOptions serializationOpts;
945
950
serializationOpts.OutputPath = opts.ModuleOutputPath .c_str ();
@@ -961,7 +966,7 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
961
966
serializationOpts.SerializeOptionsForDebugging =
962
967
!moduleIsPublic || opts.AlwaysSerializeDebuggingOptions ;
963
968
964
- serialize (DC , serializationOpts, SM.get ());
969
+ serialize (MSF , serializationOpts, SM.get ());
965
970
}
966
971
}
967
972
};
@@ -1011,8 +1016,8 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
1011
1016
// Get the main source file's private discriminator and attach it to
1012
1017
// the compile unit's flags.
1013
1018
if (IRGenOpts.DebugInfoKind != IRGenDebugInfoKind::None &&
1014
- Instance. getPrimarySourceFile ()) {
1015
- Identifier PD = Instance. getPrimarySourceFile ()->getPrivateDiscriminator ();
1019
+ MSF. is <SourceFile*> ()) {
1020
+ Identifier PD = MSF. get <SourceFile*> ()->getPrivateDiscriminator ();
1016
1021
if (!PD.empty ())
1017
1022
IRGenOpts.DWARFDebugFlags += (" -private-discriminator " +PD.str ()).str ();
1018
1023
}
@@ -1023,14 +1028,13 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
1023
1028
}
1024
1029
1025
1030
if (Action == FrontendOptions::ActionType::EmitSIB) {
1026
- auto DC = Instance.getPrimarySourceFileOrMainModule ();
1027
1031
if (!opts.ModuleOutputPath .empty ()) {
1028
1032
SerializationOptions serializationOpts;
1029
1033
serializationOpts.OutputPath = opts.ModuleOutputPath .c_str ();
1030
1034
serializationOpts.SerializeAllSIL = true ;
1031
1035
serializationOpts.IsSIB = true ;
1032
1036
1033
- serialize (DC , serializationOpts, SM.get ());
1037
+ serialize (MSF , serializationOpts, SM.get ());
1034
1038
}
1035
1039
return Context.hadError ();
1036
1040
}
@@ -1042,7 +1046,7 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
1042
1046
if (Action == FrontendOptions::ActionType::MergeModules ||
1043
1047
Action == FrontendOptions::ActionType::EmitModuleOnly) {
1044
1048
if (shouldIndex) {
1045
- if (emitIndexData (Instance. getPrimarySourceFile (),
1049
+ if (emitIndexData (MSF. dyn_cast <SourceFile*> (),
1046
1050
Invocation, Instance))
1047
1051
return true ;
1048
1052
}
@@ -1074,7 +1078,7 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
1074
1078
// TODO: remove once the frontend understands what action it should perform
1075
1079
IRGenOpts.OutputKind = getOutputKind (Action);
1076
1080
if (Action == FrontendOptions::ActionType::Immediate) {
1077
- assert (Instance. getPrimarySourceFiles (). empty () &&
1081
+ assert (!MSF. is <SourceFile*> () &&
1078
1082
" -i doesn't work in -primary-file mode" );
1079
1083
IRGenOpts.UseJIT = true ;
1080
1084
IRGenOpts.DebugInfoKind = IRGenDebugInfoKind::Normal;
@@ -1096,14 +1100,14 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
1096
1100
auto &LLVMContext = getGlobalLLVMContext ();
1097
1101
std::unique_ptr<llvm::Module> IRModule;
1098
1102
llvm::GlobalVariable *HashGlobal;
1099
- if (!Instance. getPrimarySourceFiles (). empty ()) {
1103
+ if (MSF. is <SourceFile*> ()) {
1100
1104
IRModule = performIRGeneration (IRGenOpts,
1101
- *Instance. getPrimarySourceFile (),
1105
+ *MSF. get <SourceFile*> (),
1102
1106
std::move (SM),
1103
1107
opts.getSingleOutputFilename (), LLVMContext,
1104
1108
0 , &HashGlobal);
1105
1109
} else {
1106
- IRModule = performIRGeneration (IRGenOpts, Instance. getMainModule (),
1110
+ IRModule = performIRGeneration (IRGenOpts, MSF. get <ModuleDecl*> (),
1107
1111
std::move (SM),
1108
1112
opts.getSingleOutputFilename (), LLVMContext,
1109
1113
&HashGlobal);
@@ -1112,7 +1116,7 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
1112
1116
// Walk the AST for indexing after IR generation. Walking it before seems
1113
1117
// to cause miscompilation issues.
1114
1118
if (shouldIndex) {
1115
- if (emitIndexData (Instance. getPrimarySourceFile (), Invocation, Instance))
1119
+ if (emitIndexData (MSF. dyn_cast <SourceFile*> (), Invocation, Instance))
1116
1120
return true ;
1117
1121
}
1118
1122
@@ -1141,12 +1145,13 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
1141
1145
const auto &SILOpts = Invocation.getSILOptions ();
1142
1146
const auto hasMultipleIGMs = SILOpts.hasMultipleIGMs ();
1143
1147
bool error;
1144
- if (!Instance. getPrimarySourceFiles (). empty ())
1145
- error = validateTBD (Instance. getPrimarySourceFile (),
1148
+ if (MSF. is <SourceFile*> ())
1149
+ error = validateTBD (MSF. get <SourceFile*> (),
1146
1150
*IRModule, hasMultipleIGMs,
1147
1151
allSymbols);
1148
1152
else
1149
- error = validateTBD (Instance.getMainModule (), *IRModule, hasMultipleIGMs,
1153
+ error = validateTBD (MSF.get <ModuleDecl*>(),
1154
+ *IRModule, hasMultipleIGMs,
1150
1155
allSymbols);
1151
1156
if (error)
1152
1157
return true ;
0 commit comments