1 //===--- Types.cpp - Driver input & temporary type information ------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "clang/Driver/Types.h"
10 #include "clang/Driver/Driver.h"
11 #include "clang/Driver/DriverDiagnostic.h"
12 #include "clang/Driver/Options.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/Option/Arg.h"
17 #include <cassert>
18 #include <cstring>
19
20 using namespace clang::driver;
21 using namespace clang::driver::types;
22
23 struct TypeInfo {
24 const char *Name;
25 const char *TempSuffix;
26 ID PreprocessedType;
27 class PhasesBitSet {
28 unsigned Bits = 0;
29
30 public:
PhasesBitSet(std::initializer_list<phases::ID> Phases)31 constexpr PhasesBitSet(std::initializer_list<phases::ID> Phases) {
32 for (auto Id : Phases)
33 Bits |= 1 << Id;
34 }
contains(phases::ID Id) const35 bool contains(phases::ID Id) const { return Bits & (1 << Id); }
36 } Phases;
37 };
38
39 static constexpr TypeInfo TypeInfos[] = {
40 #define TYPE(NAME, ID, PP_TYPE, TEMP_SUFFIX, ...) \
41 { NAME, TEMP_SUFFIX, TY_##PP_TYPE, { __VA_ARGS__ }, },
42 #include "clang/Driver/Types.def"
43 #undef TYPE
44 };
45 static const unsigned numTypes = llvm::array_lengthof(TypeInfos);
46
getInfo(unsigned id)47 static const TypeInfo &getInfo(unsigned id) {
48 assert(id > 0 && id - 1 < numTypes && "Invalid Type ID.");
49 return TypeInfos[id - 1];
50 }
51
getTypeName(ID Id)52 const char *types::getTypeName(ID Id) {
53 return getInfo(Id).Name;
54 }
55
getPreprocessedType(ID Id)56 types::ID types::getPreprocessedType(ID Id) {
57 ID PPT = getInfo(Id).PreprocessedType;
58 assert((getInfo(Id).Phases.contains(phases::Preprocess) !=
59 (PPT == TY_INVALID)) &&
60 "Unexpected Preprocess Type.");
61 return PPT;
62 }
63
isPreprocessedModuleType(ID Id)64 static bool isPreprocessedModuleType(ID Id) {
65 return Id == TY_CXXModule || Id == TY_PP_CXXModule;
66 }
67
getPrecompiledType(ID Id)68 types::ID types::getPrecompiledType(ID Id) {
69 if (isPreprocessedModuleType(Id))
70 return TY_ModuleFile;
71 if (onlyPrecompileType(Id))
72 return TY_PCH;
73 return TY_INVALID;
74 }
75
getTypeTempSuffix(ID Id,bool CLMode)76 const char *types::getTypeTempSuffix(ID Id, bool CLMode) {
77 if (CLMode) {
78 switch (Id) {
79 case TY_Object:
80 case TY_LTO_BC:
81 return "obj";
82 case TY_Image:
83 return "exe";
84 case TY_PP_Asm:
85 return "asm";
86 default:
87 break;
88 }
89 }
90 return getInfo(Id).TempSuffix;
91 }
92
onlyPrecompileType(ID Id)93 bool types::onlyPrecompileType(ID Id) {
94 return getInfo(Id).Phases.contains(phases::Precompile) &&
95 !isPreprocessedModuleType(Id);
96 }
97
canTypeBeUserSpecified(ID Id)98 bool types::canTypeBeUserSpecified(ID Id) {
99 static const clang::driver::types::ID kStaticLangageTypes[] = {
100 TY_CUDA_DEVICE, TY_HIP_DEVICE, TY_PP_CHeader,
101 TY_PP_ObjCHeader, TY_PP_CXXHeader, TY_PP_ObjCXXHeader,
102 TY_PP_CXXModule, TY_LTO_IR, TY_LTO_BC,
103 TY_Plist, TY_RewrittenObjC, TY_RewrittenLegacyObjC,
104 TY_Remap, TY_PCH, TY_Object,
105 TY_Image, TY_dSYM, TY_Dependencies,
106 TY_CUDA_FATBIN, TY_HIP_FATBIN};
107 return !llvm::is_contained(kStaticLangageTypes, Id);
108 }
109
appendSuffixForType(ID Id)110 bool types::appendSuffixForType(ID Id) {
111 return Id == TY_PCH || Id == TY_dSYM || Id == TY_CUDA_FATBIN ||
112 Id == TY_HIP_FATBIN;
113 }
114
canLipoType(ID Id)115 bool types::canLipoType(ID Id) {
116 return (Id == TY_Nothing ||
117 Id == TY_Image ||
118 Id == TY_Object ||
119 Id == TY_LTO_BC);
120 }
121
isAcceptedByClang(ID Id)122 bool types::isAcceptedByClang(ID Id) {
123 switch (Id) {
124 default:
125 return false;
126
127 case TY_Asm:
128 case TY_C: case TY_PP_C:
129 case TY_CL:
130 case TY_CUDA: case TY_PP_CUDA:
131 case TY_CUDA_DEVICE:
132 case TY_HIP:
133 case TY_PP_HIP:
134 case TY_HIP_DEVICE:
135 case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
136 case TY_CXX: case TY_PP_CXX:
137 case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
138 case TY_CHeader: case TY_PP_CHeader:
139 case TY_CLHeader:
140 case TY_ObjCHeader: case TY_PP_ObjCHeader:
141 case TY_CXXHeader: case TY_PP_CXXHeader:
142 case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
143 case TY_CXXModule: case TY_PP_CXXModule:
144 case TY_AST: case TY_ModuleFile: case TY_PCH:
145 case TY_LLVM_IR: case TY_LLVM_BC:
146 return true;
147 }
148 }
149
isObjC(ID Id)150 bool types::isObjC(ID Id) {
151 switch (Id) {
152 default:
153 return false;
154
155 case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
156 case TY_ObjCXX: case TY_PP_ObjCXX:
157 case TY_ObjCHeader: case TY_PP_ObjCHeader:
158 case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader: case TY_PP_ObjCXX_Alias:
159 return true;
160 }
161 }
162
isCXX(ID Id)163 bool types::isCXX(ID Id) {
164 switch (Id) {
165 default:
166 return false;
167
168 case TY_CXX: case TY_PP_CXX:
169 case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
170 case TY_CXXHeader: case TY_PP_CXXHeader:
171 case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
172 case TY_CXXModule: case TY_PP_CXXModule:
173 case TY_CUDA: case TY_PP_CUDA: case TY_CUDA_DEVICE:
174 case TY_HIP:
175 case TY_PP_HIP:
176 case TY_HIP_DEVICE:
177 return true;
178 }
179 }
180
isLLVMIR(ID Id)181 bool types::isLLVMIR(ID Id) {
182 switch (Id) {
183 default:
184 return false;
185
186 case TY_LLVM_IR:
187 case TY_LLVM_BC:
188 case TY_LTO_IR:
189 case TY_LTO_BC:
190 return true;
191 }
192 }
193
isCuda(ID Id)194 bool types::isCuda(ID Id) {
195 switch (Id) {
196 default:
197 return false;
198
199 case TY_CUDA:
200 case TY_PP_CUDA:
201 case TY_CUDA_DEVICE:
202 return true;
203 }
204 }
205
isHIP(ID Id)206 bool types::isHIP(ID Id) {
207 switch (Id) {
208 default:
209 return false;
210
211 case TY_HIP:
212 case TY_PP_HIP:
213 case TY_HIP_DEVICE:
214 return true;
215 }
216 }
217
isFortran(ID Id)218 bool types::isFortran(ID Id) {
219 switch (Id) {
220 default:
221 return false;
222
223 case TY_Fortran: case TY_PP_Fortran:
224 return true;
225 }
226 }
227
isSrcFile(ID Id)228 bool types::isSrcFile(ID Id) {
229 return Id != TY_Object && getPreprocessedType(Id) != TY_INVALID;
230 }
231
lookupTypeForExtension(llvm::StringRef Ext)232 types::ID types::lookupTypeForExtension(llvm::StringRef Ext) {
233 return llvm::StringSwitch<types::ID>(Ext)
234 .Case("c", TY_C)
235 .Case("C", TY_CXX)
236 .Case("F", TY_Fortran)
237 .Case("f", TY_PP_Fortran)
238 .Case("h", TY_CHeader)
239 .Case("H", TY_CXXHeader)
240 .Case("i", TY_PP_C)
241 .Case("m", TY_ObjC)
242 .Case("M", TY_ObjCXX)
243 .Case("o", TY_Object)
244 .Case("S", TY_Asm)
245 .Case("s", TY_PP_Asm)
246 .Case("bc", TY_LLVM_BC)
247 .Case("cc", TY_CXX)
248 .Case("CC", TY_CXX)
249 .Case("cl", TY_CL)
250 .Case("cp", TY_CXX)
251 .Case("cu", TY_CUDA)
252 .Case("hh", TY_CXXHeader)
253 .Case("ii", TY_PP_CXX)
254 .Case("ll", TY_LLVM_IR)
255 .Case("mi", TY_PP_ObjC)
256 .Case("mm", TY_ObjCXX)
257 .Case("rs", TY_RenderScript)
258 .Case("adb", TY_Ada)
259 .Case("ads", TY_Ada)
260 .Case("asm", TY_PP_Asm)
261 .Case("ast", TY_AST)
262 .Case("ccm", TY_CXXModule)
263 .Case("cpp", TY_CXX)
264 .Case("CPP", TY_CXX)
265 .Case("c++", TY_CXX)
266 .Case("C++", TY_CXX)
267 .Case("cui", TY_PP_CUDA)
268 .Case("cxx", TY_CXX)
269 .Case("CXX", TY_CXX)
270 .Case("F90", TY_Fortran)
271 .Case("f90", TY_PP_Fortran)
272 .Case("F95", TY_Fortran)
273 .Case("f95", TY_PP_Fortran)
274 .Case("for", TY_PP_Fortran)
275 .Case("FOR", TY_PP_Fortran)
276 .Case("fpp", TY_Fortran)
277 .Case("FPP", TY_Fortran)
278 .Case("gch", TY_PCH)
279 .Case("hip", TY_HIP)
280 .Case("hpp", TY_CXXHeader)
281 .Case("hxx", TY_CXXHeader)
282 .Case("iim", TY_PP_CXXModule)
283 .Case("lib", TY_Object)
284 .Case("mii", TY_PP_ObjCXX)
285 .Case("obj", TY_Object)
286 .Case("ifs", TY_IFS)
287 .Case("pch", TY_PCH)
288 .Case("pcm", TY_ModuleFile)
289 .Case("c++m", TY_CXXModule)
290 .Case("cppm", TY_CXXModule)
291 .Case("cxxm", TY_CXXModule)
292 .Default(TY_INVALID);
293 }
294
lookupTypeForTypeSpecifier(const char * Name)295 types::ID types::lookupTypeForTypeSpecifier(const char *Name) {
296 for (unsigned i=0; i<numTypes; ++i) {
297 types::ID Id = (types::ID) (i + 1);
298 if (canTypeBeUserSpecified(Id) &&
299 strcmp(Name, getInfo(Id).Name) == 0)
300 return Id;
301 }
302 // Accept "cu" as an alias for "cuda" for NVCC compatibility
303 if (strcmp(Name, "cu") == 0) {
304 return types::TY_CUDA;
305 }
306 return TY_INVALID;
307 }
308
309 llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases>
getCompilationPhases(ID Id,phases::ID LastPhase)310 types::getCompilationPhases(ID Id, phases::ID LastPhase) {
311 llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> P;
312 const auto &Info = getInfo(Id);
313 for (int I = 0; I <= LastPhase; ++I)
314 if (Info.Phases.contains(static_cast<phases::ID>(I)))
315 P.push_back(static_cast<phases::ID>(I));
316 assert(P.size() <= phases::MaxNumberOfPhases && "Too many phases in list");
317 return P;
318 }
319
320 llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases>
getCompilationPhases(const clang::driver::Driver & Driver,llvm::opt::DerivedArgList & DAL,ID Id)321 types::getCompilationPhases(const clang::driver::Driver &Driver,
322 llvm::opt::DerivedArgList &DAL, ID Id) {
323 phases::ID LastPhase;
324
325 // Filter to compiler mode. When the compiler is run as a preprocessor then
326 // compilation is not an option.
327 // -S runs the compiler in Assembly listing mode.
328 // -test-io is used by Flang to run InputOutputTest action
329 if (Driver.CCCIsCPP() || DAL.getLastArg(options::OPT_E) ||
330 DAL.getLastArg(options::OPT__SLASH_EP) ||
331 DAL.getLastArg(options::OPT_M, options::OPT_MM) ||
332 DAL.getLastArg(options::OPT__SLASH_P) ||
333 DAL.getLastArg(options::OPT_test_io))
334 LastPhase = phases::Preprocess;
335
336 // --precompile only runs up to precompilation.
337 // This is a clang extension and is not compatible with GCC.
338 else if (DAL.getLastArg(options::OPT__precompile))
339 LastPhase = phases::Precompile;
340
341 // -{fsyntax-only,-analyze,emit-ast} only run up to the compiler.
342 else if (DAL.getLastArg(options::OPT_fsyntax_only) ||
343 DAL.getLastArg(options::OPT_print_supported_cpus) ||
344 DAL.getLastArg(options::OPT_module_file_info) ||
345 DAL.getLastArg(options::OPT_verify_pch) ||
346 DAL.getLastArg(options::OPT_rewrite_objc) ||
347 DAL.getLastArg(options::OPT_rewrite_legacy_objc) ||
348 DAL.getLastArg(options::OPT__migrate) ||
349 DAL.getLastArg(options::OPT__analyze) ||
350 DAL.getLastArg(options::OPT_emit_ast))
351 LastPhase = phases::Compile;
352
353 else if (DAL.getLastArg(options::OPT_S) ||
354 DAL.getLastArg(options::OPT_emit_llvm))
355 LastPhase = phases::Backend;
356
357 else if (DAL.getLastArg(options::OPT_c))
358 LastPhase = phases::Assemble;
359
360 // Generally means, do every phase until Link.
361 else
362 LastPhase = phases::LastPhase;
363
364 return types::getCompilationPhases(Id, LastPhase);
365 }
366
lookupCXXTypeForCType(ID Id)367 ID types::lookupCXXTypeForCType(ID Id) {
368 switch (Id) {
369 default:
370 return Id;
371
372 case types::TY_C:
373 return types::TY_CXX;
374 case types::TY_PP_C:
375 return types::TY_PP_CXX;
376 case types::TY_CHeader:
377 return types::TY_CXXHeader;
378 case types::TY_PP_CHeader:
379 return types::TY_PP_CXXHeader;
380 }
381 }
382
lookupHeaderTypeForSourceType(ID Id)383 ID types::lookupHeaderTypeForSourceType(ID Id) {
384 switch (Id) {
385 default:
386 return Id;
387
388 // FIXME: Handle preprocessed input types.
389 case types::TY_C:
390 return types::TY_CHeader;
391 case types::TY_CXX:
392 case types::TY_CXXModule:
393 return types::TY_CXXHeader;
394 case types::TY_ObjC:
395 return types::TY_ObjCHeader;
396 case types::TY_ObjCXX:
397 return types::TY_ObjCXXHeader;
398 case types::TY_CL:
399 return types::TY_CLHeader;
400 }
401 }
402