clang  14.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 
9 #include "clang/Driver/Types.h"
10 #include "clang/Driver/Driver.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;
27  class PhasesBitSet {
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); }
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 
47 static const TypeInfo &getInfo(unsigned id) {
48  assert(id > 0 && id - 1 < numTypes && "Invalid Type ID.");
49  return TypeInfos[id - 1];
50 }
51 
52 const 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 
70  return TY_ModuleFile;
72  return TY_PCH;
73  return TY_INVALID;
74 }
75 
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 
96 }
97 
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 
111  return Id == TY_PCH || Id == TY_dSYM || Id == TY_CUDA_FATBIN ||
112  Id == TY_HIP_FATBIN;
113 }
114 
116  return (Id == TY_Nothing ||
117  Id == TY_Image ||
118  Id == TY_Object ||
119  Id == TY_LTO_BC);
120 }
121 
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: case TY_CLCXX:
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 
151  switch (Id) {
152  default:
153  return false;
154 
155  case TY_PP_C:
156  case TY_C:
157  case TY_CL:
158  case TY_CLCXX:
159  case TY_PP_CUDA:
160  case TY_CUDA:
161  case TY_CUDA_DEVICE:
162  case TY_PP_HIP:
163  case TY_HIP:
164  case TY_HIP_DEVICE:
165  case TY_PP_ObjC:
166  case TY_PP_ObjC_Alias:
167  case TY_ObjC:
168  case TY_PP_CXX:
169  case TY_CXX:
170  case TY_PP_ObjCXX:
171  case TY_PP_ObjCXX_Alias:
172  case TY_ObjCXX:
173  case TY_RenderScript:
174  case TY_PP_CHeader:
175  case TY_CHeader:
176  case TY_CLHeader:
177  case TY_PP_ObjCHeader:
178  case TY_ObjCHeader:
179  case TY_PP_CXXHeader:
180  case TY_CXXHeader:
181  case TY_PP_ObjCXXHeader:
182  case TY_ObjCXXHeader:
183  case TY_CXXModule:
184  case TY_PP_CXXModule:
185  return true;
186  }
187 }
188 
190  switch (Id) {
191  default:
192  return false;
193 
194  case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
195  case TY_ObjCXX: case TY_PP_ObjCXX:
196  case TY_ObjCHeader: case TY_PP_ObjCHeader:
197  case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader: case TY_PP_ObjCXX_Alias:
198  return true;
199  }
200 }
201 
202 bool types::isOpenCL(ID Id) { return Id == TY_CL || Id == TY_CLCXX; }
203 
205  switch (Id) {
206  default:
207  return false;
208 
209  case TY_CXX: case TY_PP_CXX:
210  case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
211  case TY_CXXHeader: case TY_PP_CXXHeader:
212  case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
213  case TY_CXXModule: case TY_PP_CXXModule:
214  case TY_CUDA: case TY_PP_CUDA: case TY_CUDA_DEVICE:
215  case TY_HIP:
216  case TY_PP_HIP:
217  case TY_HIP_DEVICE:
218  return true;
219  }
220 }
221 
223  switch (Id) {
224  default:
225  return false;
226 
227  case TY_LLVM_IR:
228  case TY_LLVM_BC:
229  case TY_LTO_IR:
230  case TY_LTO_BC:
231  return true;
232  }
233 }
234 
236  switch (Id) {
237  default:
238  return false;
239 
240  case TY_CUDA:
241  case TY_PP_CUDA:
242  case TY_CUDA_DEVICE:
243  return true;
244  }
245 }
246 
248  switch (Id) {
249  default:
250  return false;
251 
252  case TY_HIP:
253  case TY_PP_HIP:
254  case TY_HIP_DEVICE:
255  return true;
256  }
257 }
258 
260  switch (Id) {
261  default:
262  return false;
263 
264  case TY_Fortran: case TY_PP_Fortran:
265  return true;
266  }
267 }
268 
270  return Id != TY_Object && getPreprocessedType(Id) != TY_INVALID;
271 }
272 
274  return llvm::StringSwitch<types::ID>(Ext)
275  .Case("c", TY_C)
276  .Case("C", TY_CXX)
277  .Case("F", TY_Fortran)
278  .Case("f", TY_PP_Fortran)
279  .Case("h", TY_CHeader)
280  .Case("H", TY_CXXHeader)
281  .Case("i", TY_PP_C)
282  .Case("m", TY_ObjC)
283  .Case("M", TY_ObjCXX)
284  .Case("o", TY_Object)
285  .Case("S", TY_Asm)
286  .Case("s", TY_PP_Asm)
287  .Case("bc", TY_LLVM_BC)
288  .Case("cc", TY_CXX)
289  .Case("CC", TY_CXX)
290  .Case("cl", TY_CL)
291  .Case("clcpp", TY_CLCXX)
292  .Case("cp", TY_CXX)
293  .Case("cu", TY_CUDA)
294  .Case("hh", TY_CXXHeader)
295  .Case("ii", TY_PP_CXX)
296  .Case("ll", TY_LLVM_IR)
297  .Case("mi", TY_PP_ObjC)
298  .Case("mm", TY_ObjCXX)
299  .Case("rs", TY_RenderScript)
300  .Case("adb", TY_Ada)
301  .Case("ads", TY_Ada)
302  .Case("asm", TY_PP_Asm)
303  .Case("ast", TY_AST)
304  .Case("ccm", TY_CXXModule)
305  .Case("cpp", TY_CXX)
306  .Case("CPP", TY_CXX)
307  .Case("c++", TY_CXX)
308  .Case("C++", TY_CXX)
309  .Case("cui", TY_PP_CUDA)
310  .Case("cxx", TY_CXX)
311  .Case("CXX", TY_CXX)
312  .Case("F90", TY_Fortran)
313  .Case("f90", TY_PP_Fortran)
314  .Case("F95", TY_Fortran)
315  .Case("f95", TY_PP_Fortran)
316  .Case("for", TY_PP_Fortran)
317  .Case("FOR", TY_PP_Fortran)
318  .Case("fpp", TY_Fortran)
319  .Case("FPP", TY_Fortran)
320  .Case("gch", TY_PCH)
321  .Case("hip", TY_HIP)
322  .Case("hpp", TY_CXXHeader)
323  .Case("hxx", TY_CXXHeader)
324  .Case("iim", TY_PP_CXXModule)
325  .Case("lib", TY_Object)
326  .Case("mii", TY_PP_ObjCXX)
327  .Case("obj", TY_Object)
328  .Case("ifs", TY_IFS)
329  .Case("pch", TY_PCH)
330  .Case("pcm", TY_ModuleFile)
331  .Case("c++m", TY_CXXModule)
332  .Case("cppm", TY_CXXModule)
333  .Case("cxxm", TY_CXXModule)
334  .Default(TY_INVALID);
335 }
336 
338  for (unsigned i=0; i<numTypes; ++i) {
339  types::ID Id = (types::ID) (i + 1);
340  if (canTypeBeUserSpecified(Id) &&
341  strcmp(Name, getInfo(Id).Name) == 0)
342  return Id;
343  }
344  // Accept "cu" as an alias for "cuda" for NVCC compatibility
345  if (strcmp(Name, "cu") == 0) {
346  return types::TY_CUDA;
347  }
348  return TY_INVALID;
349 }
350 
354  const auto &Info = getInfo(Id);
355  for (int I = 0; I <= LastPhase; ++I)
356  if (Info.Phases.contains(static_cast<phases::ID>(I)))
357  P.push_back(static_cast<phases::ID>(I));
358  assert(P.size() <= phases::MaxNumberOfPhases && "Too many phases in list");
359  return P;
360 }
361 
364  llvm::opt::DerivedArgList &DAL, ID Id) {
366 }
367 
369  switch (Id) {
370  default:
371  return Id;
372 
373  case types::TY_C:
374  return types::TY_CXX;
375  case types::TY_PP_C:
376  return types::TY_PP_CXX;
377  case types::TY_CHeader:
378  return types::TY_CXXHeader;
379  case types::TY_PP_CHeader:
380  return types::TY_PP_CXXHeader;
381  }
382 }
383 
385  switch (Id) {
386  default:
387  return Id;
388 
389  // FIXME: Handle preprocessed input types.
390  case types::TY_C:
391  return types::TY_CHeader;
392  case types::TY_CXX:
393  case types::TY_CXXModule:
394  return types::TY_CXXHeader;
395  case types::TY_ObjC:
396  return types::TY_ObjCHeader;
397  case types::TY_ObjCXX:
398  return types::TY_ObjCXXHeader;
399  case types::TY_CL:
400  case types::TY_CLCXX:
401  return types::TY_CLHeader;
402  }
403 }
isPreprocessedModuleType
static bool isPreprocessedModuleType(ID Id)
Definition: Types.cpp:64
TypeInfo::Name
const char * Name
Definition: Types.cpp:24
TypeInfo::PhasesBitSet
Definition: Types.cpp:27
clang::driver::types::isCuda
bool isCuda(ID Id)
isCuda - Is this a CUDA input.
Definition: Types.cpp:235
Driver.h
clang::driver::types::getCompilationPhases
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:352
llvm::SmallVector
Definition: LLVM.h:38
clang::driver::phases::MaxNumberOfPhases
@ MaxNumberOfPhases
Definition: Phases.h:28
clang::driver::phases::Preprocess
@ Preprocess
Definition: Phases.h:18
TypeInfo::TempSuffix
const char * TempSuffix
Definition: Types.cpp:25
TypeInfos
static constexpr TypeInfo TypeInfos[]
Definition: Types.cpp:39
clang::driver::phases::ID
ID
ID - Ordered values for successive stages in the compilation process which interact with user options...
Definition: Phases.h:17
TypeInfo::PhasesBitSet::contains
bool contains(phases::ID Id) const
Definition: Types.cpp:35
TypeInfo
Definition: Types.cpp:23
clang::driver::types::isAcceptedByClang
bool isAcceptedByClang(ID Id)
isAcceptedByClang - Can clang handle this input type.
Definition: Types.cpp:122
clang::driver::types::getPrecompiledType
ID getPrecompiledType(ID Id)
getPrecompiledType - Get the ID of the type for this input when it has been precompiled,...
Definition: Types.cpp:68
clang::driver::types::isCXX
bool isCXX(ID Id)
isCXX - Is this a "C++" input (C++ and Obj-C++ sources and headers).
Definition: Types.cpp:204
clang::driver::types::lookupTypeForExtension
ID lookupTypeForExtension(llvm::StringRef Ext)
lookupTypeForExtension - Lookup the type to use for the file extension Ext.
Definition: Types.cpp:273
clang::driver::types::onlyPrecompileType
bool onlyPrecompileType(ID Id)
onlyPrecompileType - Should this type only be precompiled.
Definition: Types.cpp:93
TypeInfo::PreprocessedType
ID PreprocessedType
Definition: Types.cpp:26
numTypes
static const unsigned numTypes
Definition: Types.cpp:45
Options.h
Id
int Id
Definition: ASTDiff.cpp:191
clang::driver::types::lookupTypeForTypeSpecifier
ID lookupTypeForTypeSpecifier(const char *Name)
lookupTypeForTypSpecifier - Lookup the type to use for a user specified type name.
Definition: Types.cpp:337
DriverDiagnostic.h
clang::driver::types::TY_INVALID
@ TY_INVALID
Definition: Types.h:24
clang::driver::types::isSrcFile
bool isSrcFile(ID Id)
isSrcFile - Is this a source file, i.e.
Definition: Types.cpp:269
clang::driver::types::isFortran
bool isFortran(ID Id)
isFortran - Is this a Fortran input.
Definition: Types.cpp:259
clang::driver::types::isObjC
bool isObjC(ID Id)
isObjC - Is this an "ObjC" input (Obj-C and Obj-C++ sources and headers).
Definition: Types.cpp:189
clang::driver::types::appendSuffixForType
bool appendSuffixForType(ID Id)
appendSuffixForType - When generating outputs of this type, should the suffix be appended (instead of...
Definition: Types.cpp:110
clang::driver::types::isLLVMIR
bool isLLVMIR(ID Id)
Is this LLVM IR.
Definition: Types.cpp:222
TypeInfo::PhasesBitSet::PhasesBitSet
constexpr PhasesBitSet(std::initializer_list< phases::ID > Phases)
Definition: Types.cpp:31
clang::driver::types::canTypeBeUserSpecified
bool canTypeBeUserSpecified(ID Id)
canTypeBeUserSpecified - Can this type be specified on the command line (by the type name); this is u...
Definition: Types.cpp:98
clang::driver::Driver::getFinalPhase
phases::ID getFinalPhase(const llvm::opt::DerivedArgList &DAL, llvm::opt::Arg **FinalPhaseArg=nullptr) const
Definition: Driver.cpp:271
clang::driver::types::isOpenCL
bool isOpenCL(ID Id)
isOpenCL - Is this an "OpenCL" input.
Definition: Types.cpp:202
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
clang::driver::types::canLipoType
bool canLipoType(ID Id)
canLipoType - Is this type acceptable as the output of a universal build (currently,...
Definition: Types.cpp:115
clang::driver::types::getTypeTempSuffix
const char * getTypeTempSuffix(ID Id, bool CLMode=false)
getTypeTempSuffix - Return the suffix to use when creating a temp file of this type,...
Definition: Types.cpp:76
getInfo
static const TypeInfo & getInfo(unsigned id)
Definition: Types.cpp:47
clang::driver::types::ID
ID
Definition: Types.h:23
clang::driver::types::isDerivedFromC
bool isDerivedFromC(ID Id)
isDerivedFromC - Is the input derived from C.
Definition: Types.cpp:150
clang::driver::types
Definition: Types.h:22
clang::driver::phases::Precompile
@ Precompile
Definition: Phases.h:19
clang::driver::Driver
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
clang::driver::types::lookupCXXTypeForCType
ID lookupCXXTypeForCType(ID Id)
lookupCXXTypeForCType - Lookup CXX input type that corresponds to given C type (used for clang++ emul...
Definition: Types.cpp:368
clang::driver::types::isHIP
bool isHIP(ID Id)
isHIP - Is this a HIP input.
Definition: Types.cpp:247
Types.h
clang::driver::types::getPreprocessedType
ID getPreprocessedType(ID Id)
getPreprocessedType - Get the ID of the type for this input when it has been preprocessed,...
Definition: Types.cpp:56
TypeInfo::Phases
class TypeInfo::PhasesBitSet Phases
clang::driver
Definition: Action.h:31
clang::driver::types::getTypeName
const char * getTypeName(ID Id)
getTypeName - Return the name of the type for Id.
Definition: Types.cpp:52
clang::driver::types::lookupHeaderTypeForSourceType
ID lookupHeaderTypeForSourceType(ID Id)
Lookup header file input type that corresponds to given source file type (used for clang-cl emulation...
Definition: Types.cpp:384