21
21
22
22
namespace llvm {
23
23
class MemoryBuffer ;
24
+ namespace opt {
25
+ class ArgList ;
26
+ class Arg ;
27
+ } // namespace opt
24
28
}
25
29
26
30
namespace swift {
@@ -62,9 +66,11 @@ enum class InputFileKind {
62
66
IFK_LLVM_IR
63
67
};
64
68
65
- // / Options for controlling the behavior of the frontend.
66
- class FrontendOptions {
67
- public:
69
+ // / Information about all the inputs to the frontend.
70
+ class FrontendInputs {
71
+ private:
72
+ // FIXME: (dmu) create a class for the inputs
73
+
68
74
// / The names of input files to the frontend.
69
75
std::vector<std::string> InputFilenames;
70
76
@@ -75,13 +81,147 @@ class FrontendOptions {
75
81
// / be generated for the whole module.
76
82
Optional<SelectedInput> PrimaryInput;
77
83
84
+ public:
85
+ // Readers:
86
+
87
+ // Input filename readers
88
+ ArrayRef<std::string> getInputFilenames () const { return InputFilenames; }
89
+ bool hasInputFilenames () const { return !getInputFilenames ().empty (); }
90
+ unsigned inputFilenameCount () const { return getInputFilenames ().size (); }
91
+
92
+ bool hasUniqueInputFilename () const { return inputFilenameCount () == 1 ; }
93
+ const std::string &getFilenameOfFirstInput () const {
94
+ assert (hasInputFilenames ());
95
+ return getInputFilenames ()[0 ];
96
+ }
97
+
98
+ bool isReadingFromStdin () {
99
+ return hasUniqueInputFilename () && getFilenameOfFirstInput () == " -" ;
100
+ }
101
+
102
+ // If we have exactly one input filename, and its extension is "bc" or "ll",
103
+ // treat the input as LLVM_IR.
104
+ bool shouldTreatAsLLVM () const ;
105
+
106
+ // Input buffer readers
107
+
108
+ ArrayRef<llvm::MemoryBuffer *> getInputBuffers () const {
109
+ return InputBuffers;
110
+ }
111
+ unsigned inputBufferCount () const { return getInputBuffers ().size (); }
112
+
113
+ // Primary input readers
114
+
115
+ Optional<SelectedInput> getPrimaryInput () const { return PrimaryInput; }
116
+ bool hasPrimaryInput () const { return getPrimaryInput ().hasValue (); }
117
+
118
+ bool isWholeModule () { return !hasPrimaryInput (); }
119
+
120
+ bool isPrimaryInputAFileAt (unsigned i) {
121
+ return hasPrimaryInput () && getPrimaryInput ()->isFilename () &&
122
+ getPrimaryInput ()->Index == i;
123
+ }
124
+ bool haveAPrimaryInputFile () const {
125
+ return hasPrimaryInput () && getPrimaryInput ()->isFilename ();
126
+ }
127
+ Optional<unsigned > primaryInputFileIndex () const {
128
+ return haveAPrimaryInputFile ()
129
+ ? Optional<unsigned >(getPrimaryInput ()->Index )
130
+ : None;
131
+ }
132
+
133
+ StringRef primaryInputFilenameIfAny () const {
134
+ if (auto Index = primaryInputFileIndex ()) {
135
+ return getInputFilenames ()[*Index];
136
+ }
137
+ return StringRef ();
138
+ }
139
+
140
+ // Multi-facet readers
141
+ StringRef baseNameOfOutput (const llvm::opt::ArgList &Args,
142
+ StringRef ModuleName) const ;
143
+ bool shouldTreatAsSIL () const ;
144
+
145
+ // / Return true for error
146
+ bool verifyInputs (DiagnosticEngine &Diags, bool TreatAsSIL,
147
+ bool isREPLRequested, bool isNoneRequested) const ;
148
+
149
+ // Input filename writers
150
+
151
+ void addInputFilename (StringRef Filename) {
152
+ InputFilenames.push_back (Filename);
153
+ }
154
+ void transformInputFilenames (
155
+ const llvm::function_ref<std::string(std::string)> &fn);
156
+
157
+ // Input buffer writers
158
+
159
+ void addInputBuffer (llvm::MemoryBuffer *Buf) { InputBuffers.push_back (Buf); }
160
+
161
+ // Primary input writers
162
+
163
+ void setPrimaryInput (SelectedInput si) { PrimaryInput = si; }
164
+ void clearPrimaryInput () { PrimaryInput = 0 ; }
165
+ void setPrimaryInputForInputFilename (const std::string &inputFilename) {
166
+ setPrimaryInput (!inputFilename.empty () && inputFilename != " -"
167
+ ? SelectedInput (inputFilenameCount (),
168
+ SelectedInput::InputKind::Filename)
169
+ : SelectedInput (inputBufferCount (),
170
+ SelectedInput::InputKind::Buffer));
171
+ }
172
+
173
+ // Multi-faceted writers
174
+
175
+ void clearInputs () {
176
+ InputFilenames.clear ();
177
+ InputBuffers.clear ();
178
+ }
179
+
180
+ void setInputFilenamesAndPrimaryInput (DiagnosticEngine &Diags,
181
+ llvm::opt::ArgList &Args);
182
+
183
+ void readInputFileList (DiagnosticEngine &diags, llvm::opt::ArgList &Args,
184
+ const llvm::opt::Arg *filelistPath);
185
+ };
186
+
187
+ // / Options for controlling the behavior of the frontend.
188
+ class FrontendOptions {
189
+ public:
190
+ FrontendInputs Inputs;
191
+
78
192
// / The kind of input on which the frontend should operate.
79
193
InputFileKind InputKind = InputFileKind::IFK_Swift;
80
194
81
195
// / The specified output files. If only a single outputfile is generated,
82
196
// / the name of the last specified file is taken.
83
197
std::vector<std::string> OutputFilenames;
84
198
199
+ void forAllOutputPaths (std::function<void (const std::string &)> fn) const ;
200
+
201
+ // / Gets the name of the specified output filename.
202
+ // / If multiple files are specified, the last one is returned.
203
+ StringRef getSingleOutputFilename () const {
204
+ if (OutputFilenames.size () >= 1 )
205
+ return OutputFilenames.back ();
206
+ return StringRef ();
207
+ }
208
+ // / Sets a single filename as output filename.
209
+ void setSingleOutputFilename (const std::string &FileName) {
210
+ OutputFilenames.clear ();
211
+ OutputFilenames.push_back (FileName);
212
+ }
213
+ void setOutputFilenameToStdout () { setSingleOutputFilename (" -" ); }
214
+ bool isOutputFilenameStdout () const {
215
+ return getSingleOutputFilename () == " -" ;
216
+ }
217
+ bool isOutputFileDirectory () const ;
218
+ bool isOutputFilePlainFile () const ;
219
+ bool hasNamedOutputFile () const {
220
+ return !OutputFilenames.empty () && !isOutputFilenameStdout ();
221
+ }
222
+ void setOutputFileList (DiagnosticEngine &Diags,
223
+ const llvm::opt::ArgList &Args);
224
+
85
225
// / A list of arbitrary modules to import and make implicitly visible.
86
226
std::vector<std::string> ImplicitImportModuleNames;
87
227
@@ -325,27 +465,22 @@ class FrontendOptions {
325
465
// / Indicates whether the RequestedAction will immediately run code.
326
466
bool actionIsImmediate () const ;
327
467
328
- void forAllOutputPaths (std::function<void (const std::string &)> fn) const ;
329
-
330
- // / Gets the name of the specified output filename.
331
- // / If multiple files are specified, the last one is returned.
332
- StringRef getSingleOutputFilename () const {
333
- if (OutputFilenames.size () >= 1 )
334
- return OutputFilenames.back ();
335
- return StringRef ();
336
- }
337
-
338
- // / Sets a single filename as output filename.
339
- void setSingleOutputFilename (const std::string &FileName) {
340
- OutputFilenames.clear ();
341
- OutputFilenames.push_back (FileName);
342
- }
343
-
344
468
// / Return a hash code of any components from these options that should
345
469
// / contribute to a Swift Bridging PCH hash.
346
470
llvm::hash_code getPCHHashComponents () const {
347
471
return llvm::hash_value (0 );
348
472
}
473
+
474
+ StringRef originalPath () const ;
475
+
476
+ StringRef determineFallbackModuleName () const ;
477
+
478
+ bool isCompilingExactlyOneSwiftFile () const {
479
+ return InputKind == InputFileKind::IFK_Swift &&
480
+ Inputs.hasUniqueInputFilename ();
481
+ }
482
+
483
+ void setModuleName (DiagnosticEngine &Diags, const llvm::opt::ArgList &Args);
349
484
};
350
485
351
486
}
0 commit comments