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