[docs] Fix build-docs.sh
[llvm-project.git] / clang / lib / Driver / Types.cpp
blob6e30bb744b4fb5ddcbb71d8ca85e1c6b991ac560
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 //===----------------------------------------------------------------------===//
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>
20 using namespace clang::driver;
21 using namespace clang::driver::types;
23 struct TypeInfo {
24 const char *Name;
25 const char *TempSuffix;
26 ID PreprocessedType;
27 class PhasesBitSet {
28 unsigned Bits = 0;
30 public:
31 constexpr PhasesBitSet(std::initializer_list<phases::ID> Phases) {
32 for (auto Id : Phases)
33 Bits |= 1 << Id;
35 bool contains(phases::ID Id) const { return Bits & (1 << Id); }
36 } Phases;
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
45 static const unsigned numTypes = std::size(TypeInfos);
47 static const TypeInfo &getInfo(unsigned id) {
48 assert(id > 0 && id - 1 < numTypes && "Invalid Type ID.");
49 return TypeInfos[id - 1];
52 const char *types::getTypeName(ID Id) {
53 return getInfo(Id).Name;
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;
64 static bool isPreprocessedModuleType(ID Id) {
65 return Id == TY_CXXModule || Id == TY_PP_CXXModule;
68 static bool isPreprocessedHeaderUnitType(ID Id) {
69 return Id == TY_CXXSHeader || Id == TY_CXXUHeader || Id == TY_CXXHUHeader ||
70 Id == TY_PP_CXXHeaderUnit;
73 types::ID types::getPrecompiledType(ID Id) {
74 if (isPreprocessedModuleType(Id))
75 return TY_ModuleFile;
76 if (isPreprocessedHeaderUnitType(Id))
77 return TY_HeaderUnit;
78 if (onlyPrecompileType(Id))
79 return TY_PCH;
80 return TY_INVALID;
83 const char *types::getTypeTempSuffix(ID Id, bool CLMode) {
84 if (CLMode) {
85 switch (Id) {
86 case TY_Object:
87 case TY_LTO_BC:
88 return "obj";
89 case TY_Image:
90 return "exe";
91 case TY_PP_Asm:
92 return "asm";
93 default:
94 break;
97 return getInfo(Id).TempSuffix;
100 bool types::onlyPrecompileType(ID Id) {
101 return getInfo(Id).Phases.contains(phases::Precompile) &&
102 !isPreprocessedModuleType(Id);
105 bool types::canTypeBeUserSpecified(ID Id) {
106 static const clang::driver::types::ID kStaticLangageTypes[] = {
107 TY_CUDA_DEVICE, TY_HIP_DEVICE, TY_PP_CHeader,
108 TY_PP_ObjCHeader, TY_PP_CXXHeader, TY_PP_ObjCXXHeader,
109 TY_PP_CXXModule, TY_LTO_IR, TY_LTO_BC,
110 TY_Plist, TY_RewrittenObjC, TY_RewrittenLegacyObjC,
111 TY_Remap, TY_PCH, TY_Object,
112 TY_Image, TY_dSYM, TY_Dependencies,
113 TY_CUDA_FATBIN, TY_HIP_FATBIN};
114 return !llvm::is_contained(kStaticLangageTypes, Id);
117 bool types::appendSuffixForType(ID Id) {
118 return Id == TY_PCH || Id == TY_dSYM || Id == TY_CUDA_FATBIN ||
119 Id == TY_HIP_FATBIN;
122 bool types::canLipoType(ID Id) {
123 return (Id == TY_Nothing ||
124 Id == TY_Image ||
125 Id == TY_Object ||
126 Id == TY_LTO_BC);
129 bool types::isAcceptedByClang(ID Id) {
130 switch (Id) {
131 default:
132 return false;
134 case TY_Asm:
135 case TY_C: case TY_PP_C:
136 case TY_CL: case TY_CLCXX:
137 case TY_CUDA: case TY_PP_CUDA:
138 case TY_CUDA_DEVICE:
139 case TY_HIP:
140 case TY_PP_HIP:
141 case TY_HIP_DEVICE:
142 case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
143 case TY_CXX: case TY_PP_CXX:
144 case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
145 case TY_CHeader: case TY_PP_CHeader:
146 case TY_CLHeader:
147 case TY_ObjCHeader: case TY_PP_ObjCHeader:
148 case TY_CXXHeader: case TY_PP_CXXHeader:
149 case TY_CXXSHeader:
150 case TY_CXXUHeader:
151 case TY_CXXHUHeader:
152 case TY_PP_CXXHeaderUnit:
153 case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
154 case TY_CXXModule: case TY_PP_CXXModule:
155 case TY_AST: case TY_ModuleFile: case TY_PCH:
156 case TY_LLVM_IR: case TY_LLVM_BC:
157 case TY_API_INFO:
158 return true;
162 bool types::isAcceptedByFlang(ID Id) {
163 switch (Id) {
164 default:
165 return false;
167 case TY_Fortran:
168 case TY_PP_Fortran:
169 return true;
170 case TY_LLVM_IR:
171 case TY_LLVM_BC:
172 return true;
176 bool types::isDerivedFromC(ID Id) {
177 switch (Id) {
178 default:
179 return false;
181 case TY_PP_C:
182 case TY_C:
183 case TY_CL:
184 case TY_CLCXX:
185 case TY_PP_CUDA:
186 case TY_CUDA:
187 case TY_CUDA_DEVICE:
188 case TY_PP_HIP:
189 case TY_HIP:
190 case TY_HIP_DEVICE:
191 case TY_PP_ObjC:
192 case TY_PP_ObjC_Alias:
193 case TY_ObjC:
194 case TY_PP_CXX:
195 case TY_CXX:
196 case TY_PP_ObjCXX:
197 case TY_PP_ObjCXX_Alias:
198 case TY_ObjCXX:
199 case TY_RenderScript:
200 case TY_PP_CHeader:
201 case TY_CHeader:
202 case TY_CLHeader:
203 case TY_PP_ObjCHeader:
204 case TY_ObjCHeader:
205 case TY_PP_CXXHeader:
206 case TY_CXXHeader:
207 case TY_PP_ObjCXXHeader:
208 case TY_ObjCXXHeader:
209 case TY_CXXModule:
210 case TY_PP_CXXModule:
211 return true;
215 bool types::isObjC(ID Id) {
216 switch (Id) {
217 default:
218 return false;
220 case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
221 case TY_ObjCXX: case TY_PP_ObjCXX:
222 case TY_ObjCHeader: case TY_PP_ObjCHeader:
223 case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader: case TY_PP_ObjCXX_Alias:
224 return true;
228 bool types::isOpenCL(ID Id) { return Id == TY_CL || Id == TY_CLCXX; }
230 bool types::isCXX(ID Id) {
231 switch (Id) {
232 default:
233 return false;
235 case TY_CXX: case TY_PP_CXX:
236 case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
237 case TY_CXXHeader: case TY_PP_CXXHeader:
238 case TY_CXXSHeader:
239 case TY_CXXUHeader:
240 case TY_CXXHUHeader:
241 case TY_PP_CXXHeaderUnit:
242 case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
243 case TY_CXXModule: case TY_PP_CXXModule:
244 case TY_CUDA: case TY_PP_CUDA: case TY_CUDA_DEVICE:
245 case TY_HIP:
246 case TY_PP_HIP:
247 case TY_HIP_DEVICE:
248 return true;
252 bool types::isLLVMIR(ID Id) {
253 switch (Id) {
254 default:
255 return false;
257 case TY_LLVM_IR:
258 case TY_LLVM_BC:
259 case TY_LTO_IR:
260 case TY_LTO_BC:
261 return true;
265 bool types::isCuda(ID Id) {
266 switch (Id) {
267 default:
268 return false;
270 case TY_CUDA:
271 case TY_PP_CUDA:
272 case TY_CUDA_DEVICE:
273 return true;
277 bool types::isHIP(ID Id) {
278 switch (Id) {
279 default:
280 return false;
282 case TY_HIP:
283 case TY_PP_HIP:
284 case TY_HIP_DEVICE:
285 return true;
289 bool types::isSrcFile(ID Id) {
290 return Id != TY_Object && getPreprocessedType(Id) != TY_INVALID;
293 types::ID types::lookupTypeForExtension(llvm::StringRef Ext) {
294 return llvm::StringSwitch<types::ID>(Ext)
295 .Case("c", TY_C)
296 .Case("C", TY_CXX)
297 .Case("F", TY_Fortran)
298 .Case("f", TY_PP_Fortran)
299 .Case("h", TY_CHeader)
300 .Case("H", TY_CXXHeader)
301 .Case("i", TY_PP_C)
302 .Case("m", TY_ObjC)
303 .Case("M", TY_ObjCXX)
304 .Case("o", TY_Object)
305 .Case("S", TY_Asm)
306 .Case("s", TY_PP_Asm)
307 .Case("bc", TY_LLVM_BC)
308 .Case("cc", TY_CXX)
309 .Case("CC", TY_CXX)
310 .Case("cl", TY_CL)
311 .Case("clcpp", TY_CLCXX)
312 .Case("cp", TY_CXX)
313 .Case("cu", TY_CUDA)
314 .Case("hh", TY_CXXHeader)
315 .Case("ii", TY_PP_CXX)
316 .Case("ll", TY_LLVM_IR)
317 .Case("mi", TY_PP_ObjC)
318 .Case("mm", TY_ObjCXX)
319 .Case("rs", TY_RenderScript)
320 .Case("adb", TY_Ada)
321 .Case("ads", TY_Ada)
322 .Case("asm", TY_PP_Asm)
323 .Case("ast", TY_AST)
324 .Case("ccm", TY_CXXModule)
325 .Case("cpp", TY_CXX)
326 .Case("CPP", TY_CXX)
327 .Case("c++", TY_CXX)
328 .Case("C++", TY_CXX)
329 .Case("cui", TY_PP_CUDA)
330 .Case("cxx", TY_CXX)
331 .Case("CXX", TY_CXX)
332 .Case("F90", TY_Fortran)
333 .Case("f90", TY_PP_Fortran)
334 .Case("F95", TY_Fortran)
335 .Case("f95", TY_PP_Fortran)
336 .Case("for", TY_PP_Fortran)
337 .Case("FOR", TY_PP_Fortran)
338 .Case("fpp", TY_Fortran)
339 .Case("FPP", TY_Fortran)
340 .Case("gch", TY_PCH)
341 .Case("hip", TY_HIP)
342 .Case("hpp", TY_CXXHeader)
343 .Case("hxx", TY_CXXHeader)
344 .Case("iim", TY_PP_CXXModule)
345 .Case("iih", TY_PP_CXXHeaderUnit)
346 .Case("lib", TY_Object)
347 .Case("mii", TY_PP_ObjCXX)
348 .Case("obj", TY_Object)
349 .Case("ifs", TY_IFS)
350 .Case("pch", TY_PCH)
351 .Case("pcm", TY_ModuleFile)
352 .Case("c++m", TY_CXXModule)
353 .Case("cppm", TY_CXXModule)
354 .Case("cxxm", TY_CXXModule)
355 .Case("hlsl", TY_HLSL)
356 .Default(TY_INVALID);
359 types::ID types::lookupTypeForTypeSpecifier(const char *Name) {
360 for (unsigned i=0; i<numTypes; ++i) {
361 types::ID Id = (types::ID) (i + 1);
362 if (canTypeBeUserSpecified(Id) &&
363 strcmp(Name, getInfo(Id).Name) == 0)
364 return Id;
366 // Accept "cu" as an alias for "cuda" for NVCC compatibility
367 if (strcmp(Name, "cu") == 0) {
368 return types::TY_CUDA;
370 return TY_INVALID;
373 llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases>
374 types::getCompilationPhases(ID Id, phases::ID LastPhase) {
375 llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> P;
376 const auto &Info = getInfo(Id);
377 for (int I = 0; I <= LastPhase; ++I)
378 if (Info.Phases.contains(static_cast<phases::ID>(I)))
379 P.push_back(static_cast<phases::ID>(I));
380 assert(P.size() <= phases::MaxNumberOfPhases && "Too many phases in list");
381 return P;
384 llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases>
385 types::getCompilationPhases(const clang::driver::Driver &Driver,
386 llvm::opt::DerivedArgList &DAL, ID Id) {
387 return types::getCompilationPhases(Id, Driver.getFinalPhase(DAL));
390 ID types::lookupCXXTypeForCType(ID Id) {
391 switch (Id) {
392 default:
393 return Id;
395 case types::TY_C:
396 return types::TY_CXX;
397 case types::TY_PP_C:
398 return types::TY_PP_CXX;
399 case types::TY_CHeader:
400 return types::TY_CXXHeader;
401 case types::TY_PP_CHeader:
402 return types::TY_PP_CXXHeader;
406 ID types::lookupHeaderTypeForSourceType(ID Id) {
407 switch (Id) {
408 default:
409 return Id;
411 // FIXME: Handle preprocessed input types.
412 case types::TY_C:
413 return types::TY_CHeader;
414 case types::TY_CXX:
415 case types::TY_CXXModule:
416 return types::TY_CXXHeader;
417 case types::TY_ObjC:
418 return types::TY_ObjCHeader;
419 case types::TY_ObjCXX:
420 return types::TY_ObjCXXHeader;
421 case types::TY_CL:
422 case types::TY_CLCXX:
423 return types::TY_CLHeader;