clang 20.0.0git
Types.cpp
Go to the documentation of this file.
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
10#include "clang/Driver/Driver.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
20using namespace clang::driver;
21using namespace clang::driver::types;
22
23struct TypeInfo {
24 const char *Name;
25 const char *TempSuffix;
28 unsigned Bits = 0;
29
30 public:
31 constexpr PhasesBitSet(std::initializer_list<phases::ID> Phases) {
32 for (auto Id : Phases)
33 Bits |= 1 << Id;
34 }
35 bool contains(phases::ID Id) const { return Bits & (1 << Id); }
37};
38
39static 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};
45static const unsigned numTypes = std::size(TypeInfos);
46
47static const TypeInfo &getInfo(unsigned id) {
48 assert(id > 0 && id - 1 < numTypes && "Invalid Type ID.");
49 return TypeInfos[id - 1];
50}
51
52const char *types::getTypeName(ID Id) {
53 return getInfo(Id).Name;
54}
55
58 assert((getInfo(Id).Phases.contains(phases::Preprocess) !=
59 (PPT == TY_INVALID)) &&
60 "Unexpected Preprocess Type.");
61 return PPT;
62}
63
65 return Id == TY_CXXModule || Id == TY_PP_CXXModule;
66}
67
69 return Id == TY_CXXSHeader || Id == TY_CXXUHeader || Id == TY_CXXHUHeader ||
70 Id == TY_PP_CXXHeaderUnit;
71}
72
75 return TY_ModuleFile;
77 return TY_HeaderUnit;
79 return TY_PCH;
80 return TY_INVALID;
81}
82
83const char *types::getTypeTempSuffix(ID Id, bool CLStyle) {
84 if (CLStyle) {
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;
95 }
96 }
97 return getInfo(Id).TempSuffix;
98}
99
103}
104
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);
115}
116
118 return Id == TY_PCH || Id == TY_dSYM || Id == TY_CUDA_FATBIN ||
119 Id == TY_HIP_FATBIN;
120}
121
123 return (Id == TY_Nothing ||
124 Id == TY_Image ||
125 Id == TY_Object ||
126 Id == TY_LTO_BC);
127}
128
130 switch (Id) {
131 default:
132 return false;
133
134 case TY_Asm:
135 case TY_C: case TY_PP_C:
136 case TY_CL: case TY_PP_CL: case TY_CLCXX: case TY_PP_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;
159 }
160}
161
163 switch (Id) {
164 default:
165 return false;
166
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;
173 case TY_PP_CUDA:
174 case TY_CUDA:
175 return true;
176 }
177}
178
180 switch (Id) {
181 default:
182 return false;
183
184 case TY_PP_C:
185 case TY_C:
186 case TY_CL:
187 case TY_PP_CL:
188 case TY_CLCXX:
189 case TY_PP_CLCXX:
190 case TY_PP_CUDA:
191 case TY_CUDA:
192 case TY_CUDA_DEVICE:
193 case TY_PP_HIP:
194 case TY_HIP:
195 case TY_HIP_DEVICE:
196 case TY_PP_ObjC:
197 case TY_PP_ObjC_Alias:
198 case TY_ObjC:
199 case TY_PP_CXX:
200 case TY_CXX:
201 case TY_PP_ObjCXX:
202 case TY_PP_ObjCXX_Alias:
203 case TY_ObjCXX:
204 case TY_RenderScript:
205 case TY_PP_CHeader:
206 case TY_CHeader:
207 case TY_CLHeader:
208 case TY_PP_ObjCHeader:
209 case TY_ObjCHeader:
210 case TY_PP_CXXHeader:
211 case TY_CXXHeader:
212 case TY_PP_ObjCXXHeader:
213 case TY_ObjCXXHeader:
214 case TY_CXXModule:
215 case TY_PP_CXXModule:
216 return true;
217 }
218}
219
221 switch (Id) {
222 default:
223 return false;
224
225 case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
226 case TY_ObjCXX: case TY_PP_ObjCXX:
227 case TY_ObjCHeader: case TY_PP_ObjCHeader:
228 case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader: case TY_PP_ObjCXX_Alias:
229 return true;
230 }
231}
232
233bool types::isOpenCL(ID Id) { return Id == TY_CL || Id == TY_CLCXX; }
234
236 switch (Id) {
237 default:
238 return false;
239
240 case TY_CXX: case TY_PP_CXX:
241 case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
242 case TY_CXXHeader: case TY_PP_CXXHeader:
243 case TY_CXXSHeader:
244 case TY_CXXUHeader:
245 case TY_CXXHUHeader:
246 case TY_PP_CXXHeaderUnit:
247 case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
248 case TY_CXXModule:
249 case TY_PP_CXXModule:
250 case TY_ModuleFile:
251 case TY_PP_CLCXX:
252 case TY_CUDA: case TY_PP_CUDA: case TY_CUDA_DEVICE:
253 case TY_HIP:
254 case TY_PP_HIP:
255 case TY_HIP_DEVICE:
256 return true;
257 }
258}
259
261 switch (Id) {
262 default:
263 return false;
264
265 case TY_LLVM_IR:
266 case TY_LLVM_BC:
267 case TY_LTO_IR:
268 case TY_LTO_BC:
269 return true;
270 }
271}
272
274 switch (Id) {
275 default:
276 return false;
277
278 case TY_CUDA:
279 case TY_PP_CUDA:
280 case TY_CUDA_DEVICE:
281 return true;
282 }
283}
284
286 switch (Id) {
287 default:
288 return false;
289
290 case TY_HIP:
291 case TY_PP_HIP:
292 case TY_HIP_DEVICE:
293 return true;
294 }
295}
296
297bool types::isHLSL(ID Id) { return Id == TY_HLSL; }
298
300 return Id != TY_Object && getPreprocessedType(Id) != TY_INVALID;
301}
302
304 return llvm::StringSwitch<types::ID>(Ext)
305 .Case("c", TY_C)
306 .Case("C", TY_CXX)
307 .Case("F", TY_Fortran)
308 .Case("f", TY_PP_Fortran)
309 .Case("h", TY_CHeader)
310 .Case("H", TY_CXXHeader)
311 .Case("i", TY_PP_C)
312 .Case("m", TY_ObjC)
313 .Case("M", TY_ObjCXX)
314 .Case("o", TY_Object)
315 .Case("S", TY_Asm)
316 .Case("s", TY_PP_Asm)
317 .Case("bc", TY_LLVM_BC)
318 .Case("cc", TY_CXX)
319 .Case("CC", TY_CXX)
320 .Case("cl", TY_CL)
321 .Case("cli", TY_PP_CL)
322 .Case("clcpp", TY_CLCXX)
323 .Case("clii", TY_PP_CLCXX)
324 .Case("cp", TY_CXX)
325 .Case("cu", TY_CUDA)
326 .Case("hh", TY_CXXHeader)
327 .Case("ii", TY_PP_CXX)
328 .Case("ll", TY_LLVM_IR)
329 .Case("mi", TY_PP_ObjC)
330 .Case("mm", TY_ObjCXX)
331 .Case("rs", TY_RenderScript)
332 .Case("adb", TY_Ada)
333 .Case("ads", TY_Ada)
334 .Case("asm", TY_PP_Asm)
335 .Case("ast", TY_AST)
336 .Case("ccm", TY_CXXModule)
337 .Case("cpp", TY_CXX)
338 .Case("CPP", TY_CXX)
339 .Case("c++", TY_CXX)
340 .Case("C++", TY_CXX)
341 .Case("cui", TY_PP_CUDA)
342 .Case("cxx", TY_CXX)
343 .Case("CXX", TY_CXX)
344 .Case("F03", TY_Fortran)
345 .Case("f03", TY_PP_Fortran)
346 .Case("F08", TY_Fortran)
347 .Case("f08", TY_PP_Fortran)
348 .Case("F90", TY_Fortran)
349 .Case("f90", TY_PP_Fortran)
350 .Case("F95", TY_Fortran)
351 .Case("f95", TY_PP_Fortran)
352 .Case("for", TY_PP_Fortran)
353 .Case("FOR", TY_PP_Fortran)
354 .Case("fpp", TY_Fortran)
355 .Case("FPP", TY_Fortran)
356 .Case("gch", TY_PCH)
357 .Case("hip", TY_HIP)
358 .Case("hipi", TY_PP_HIP)
359 .Case("hpp", TY_CXXHeader)
360 .Case("hxx", TY_CXXHeader)
361 .Case("iim", TY_PP_CXXModule)
362 .Case("iih", TY_PP_CXXHeaderUnit)
363 .Case("lib", TY_Object)
364 .Case("mii", TY_PP_ObjCXX)
365 .Case("obj", TY_Object)
366 .Case("ifs", TY_IFS)
367 .Case("pch", TY_PCH)
368 .Case("pcm", TY_ModuleFile)
369 .Case("c++m", TY_CXXModule)
370 .Case("cppm", TY_CXXModule)
371 .Case("cxxm", TY_CXXModule)
372 .Case("hlsl", TY_HLSL)
373 .Default(TY_INVALID);
374}
375
377 for (unsigned i=0; i<numTypes; ++i) {
378 types::ID Id = (types::ID) (i + 1);
380 strcmp(Name, getInfo(Id).Name) == 0)
381 return Id;
382 }
383 // Accept "cu" as an alias for "cuda" for NVCC compatibility
384 if (strcmp(Name, "cu") == 0) {
385 return types::TY_CUDA;
386 }
387 return TY_INVALID;
388}
389
393 const auto &Info = getInfo(Id);
394 for (int I = 0; I <= LastPhase; ++I)
395 if (Info.Phases.contains(static_cast<phases::ID>(I)))
396 P.push_back(static_cast<phases::ID>(I));
397 assert(P.size() <= phases::MaxNumberOfPhases && "Too many phases in list");
398 return P;
399}
400
403 llvm::opt::DerivedArgList &DAL, ID Id) {
405}
406
408 switch (Id) {
409 default:
410 return Id;
411
412 case types::TY_C:
413 return types::TY_CXX;
414 case types::TY_PP_C:
415 return types::TY_PP_CXX;
416 case types::TY_CHeader:
417 return types::TY_CXXHeader;
418 case types::TY_PP_CHeader:
419 return types::TY_PP_CXXHeader;
420 }
421}
422
424 switch (Id) {
425 default:
426 return Id;
427
428 // FIXME: Handle preprocessed input types.
429 case types::TY_C:
430 return types::TY_CHeader;
431 case types::TY_CXX:
432 case types::TY_CXXModule:
433 return types::TY_CXXHeader;
434 case types::TY_ObjC:
435 return types::TY_ObjCHeader;
436 case types::TY_ObjCXX:
437 return types::TY_ObjCXXHeader;
438 case types::TY_CL:
439 case types::TY_CLCXX:
440 return types::TY_CLHeader;
441 }
442}
StringRef P
uint32_t Id
Definition: SemaARM.cpp:1144
static constexpr TypeInfo TypeInfos[]
Definition: Types.cpp:39
static const TypeInfo & getInfo(unsigned id)
Definition: Types.cpp:47
static bool isPreprocessedHeaderUnitType(ID Id)
Definition: Types.cpp:68
static const unsigned numTypes
Definition: Types.cpp:45
static bool isPreprocessedModuleType(ID Id)
Definition: Types.cpp:64
constexpr PhasesBitSet(std::initializer_list< phases::ID > Phases)
Definition: Types.cpp:31
bool contains(phases::ID Id) const
Definition: Types.cpp:35
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:77
phases::ID getFinalPhase(const llvm::opt::DerivedArgList &DAL, llvm::opt::Arg **FinalPhaseArg=nullptr) const
Definition: Driver.cpp:342
ID
ID - Ordered values for successive stages in the compilation process which interact with user options...
Definition: Phases.h:17
ID lookupTypeForTypeSpecifier(const char *Name)
lookupTypeForTypSpecifier - Lookup the type to use for a user specified type name.
Definition: Types.cpp:376
bool isHLSL(ID Id)
isHLSL - Is this an HLSL input.
Definition: Types.cpp:297
bool isObjC(ID Id)
isObjC - Is this an "ObjC" input (Obj-C and Obj-C++ sources and headers).
Definition: Types.cpp:220
ID getPreprocessedType(ID Id)
getPreprocessedType - Get the ID of the type for this input when it has been preprocessed,...
Definition: Types.cpp:56
bool isCuda(ID Id)
isCuda - Is this a CUDA input.
Definition: Types.cpp:273
bool onlyPrecompileType(ID Id)
onlyPrecompileType - Should this type only be precompiled.
Definition: Types.cpp:100
bool isLLVMIR(ID Id)
Is this LLVM IR.
Definition: Types.cpp:260
const char * getTypeName(ID Id)
getTypeName - Return the name of the type for Id.
Definition: Types.cpp:52
bool isOpenCL(ID Id)
isOpenCL - Is this an "OpenCL" input.
Definition: Types.cpp:233
bool canTypeBeUserSpecified(ID Id)
canTypeBeUserSpecified - Can this type be specified on the command line (by the type name); this is u...
Definition: Types.cpp:105
llvm::SmallVector< phases::ID, phases::MaxNumberOfPhases > getCompilationPhases(ID Id, phases::ID LastPhase=phases::IfsMerge)
getCompilationPhases - Get the list of compilation phases ('Phases') to be done for type 'Id' up unti...
Definition: Types.cpp:391
bool isSrcFile(ID Id)
isSrcFile - Is this a source file, i.e.
Definition: Types.cpp:299
bool isDerivedFromC(ID Id)
isDerivedFromC - Is the input derived from C.
Definition: Types.cpp:179
ID lookupCXXTypeForCType(ID Id)
lookupCXXTypeForCType - Lookup CXX input type that corresponds to given C type (used for clang++ emul...
Definition: Types.cpp:407
ID getPrecompiledType(ID Id)
getPrecompiledType - Get the ID of the type for this input when it has been precompiled,...
Definition: Types.cpp:73
bool isHIP(ID Id)
isHIP - Is this a HIP input.
Definition: Types.cpp:285
bool isAcceptedByClang(ID Id)
isAcceptedByClang - Can clang handle this input type.
Definition: Types.cpp:129
bool appendSuffixForType(ID Id)
appendSuffixForType - When generating outputs of this type, should the suffix be appended (instead of...
Definition: Types.cpp:117
bool canLipoType(ID Id)
canLipoType - Is this type acceptable as the output of a universal build (currently,...
Definition: Types.cpp:122
const char * getTypeTempSuffix(ID Id, bool CLStyle=false)
getTypeTempSuffix - Return the suffix to use when creating a temp file of this type,...
Definition: Types.cpp:83
ID lookupHeaderTypeForSourceType(ID Id)
Lookup header file input type that corresponds to given source file type (used for clang-cl emulation...
Definition: Types.cpp:423
ID lookupTypeForExtension(llvm::StringRef Ext)
lookupTypeForExtension - Lookup the type to use for the file extension Ext.
Definition: Types.cpp:303
bool isAcceptedByFlang(ID Id)
isAcceptedByFlang - Can flang handle this input type.
Definition: Types.cpp:162
bool isCXX(ID Id)
isCXX - Is this a "C++" input (C++ and Obj-C++ sources and headers).
Definition: Types.cpp:235
class TypeInfo::PhasesBitSet Phases
const char * TempSuffix
Definition: Types.cpp:25
ID PreprocessedType
Definition: Types.cpp:26
const char * Name
Definition: Types.cpp:24