clang  13.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_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 
163 bool types::isOpenCL(ID Id) { return Id == TY_CL || Id == TY_CLCXX; }
164 
166  switch (Id) {
167  default:
168  return false;
169 
170  case TY_CXX: case TY_PP_CXX:
171  case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
172  case TY_CXXHeader: case TY_PP_CXXHeader:
173  case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
174  case TY_CXXModule: case TY_PP_CXXModule:
175  case TY_CUDA: case TY_PP_CUDA: case TY_CUDA_DEVICE:
176  case TY_HIP:
177  case TY_PP_HIP:
178  case TY_HIP_DEVICE:
179  return true;
180  }
181 }
182 
184  switch (Id) {
185  default:
186  return false;
187 
188  case TY_LLVM_IR:
189  case TY_LLVM_BC:
190  case TY_LTO_IR:
191  case TY_LTO_BC:
192  return true;
193  }
194 }
195 
197  switch (Id) {
198  default:
199  return false;
200 
201  case TY_CUDA:
202  case TY_PP_CUDA:
203  case TY_CUDA_DEVICE:
204  return true;
205  }
206 }
207 
209  switch (Id) {
210  default:
211  return false;
212 
213  case TY_HIP:
214  case TY_PP_HIP:
215  case TY_HIP_DEVICE:
216  return true;
217  }
218 }
219 
221  switch (Id) {
222  default:
223  return false;
224 
225  case TY_Fortran: case TY_PP_Fortran:
226  return true;
227  }
228 }
229 
231  return Id != TY_Object && getPreprocessedType(Id) != TY_INVALID;
232 }
233 
235  return llvm::StringSwitch<types::ID>(Ext)
236  .Case("c", TY_C)
237  .Case("C", TY_CXX)
238  .Case("F", TY_Fortran)
239  .Case("f", TY_PP_Fortran)
240  .Case("h", TY_CHeader)
241  .Case("H", TY_CXXHeader)
242  .Case("i", TY_PP_C)
243  .Case("m", TY_ObjC)
244  .Case("M", TY_ObjCXX)
245  .Case("o", TY_Object)
246  .Case("S", TY_Asm)
247  .Case("s", TY_PP_Asm)
248  .Case("bc", TY_LLVM_BC)
249  .Case("cc", TY_CXX)
250  .Case("CC", TY_CXX)
251  .Case("cl", TY_CL)
252  .Case("clcpp", TY_CLCXX)
253  .Case("cp", TY_CXX)
254  .Case("cu", TY_CUDA)
255  .Case("hh", TY_CXXHeader)
256  .Case("ii", TY_PP_CXX)
257  .Case("ll", TY_LLVM_IR)
258  .Case("mi", TY_PP_ObjC)
259  .Case("mm", TY_ObjCXX)
260  .Case("rs", TY_RenderScript)
261  .Case("adb", TY_Ada)
262  .Case("ads", TY_Ada)
263  .Case("asm", TY_PP_Asm)
264  .Case("ast", TY_AST)
265  .Case("ccm", TY_CXXModule)
266  .Case("cpp", TY_CXX)
267  .Case("CPP", TY_CXX)
268  .Case("c++", TY_CXX)
269  .Case("C++", TY_CXX)
270  .Case("cui", TY_PP_CUDA)
271  .Case("cxx", TY_CXX)
272  .Case("CXX", TY_CXX)
273  .Case("F90", TY_Fortran)
274  .Case("f90", TY_PP_Fortran)
275  .Case("F95", TY_Fortran)
276  .Case("f95", TY_PP_Fortran)
277  .Case("for", TY_PP_Fortran)
278  .Case("FOR", TY_PP_Fortran)
279  .Case("fpp", TY_Fortran)
280  .Case("FPP", TY_Fortran)
281  .Case("gch", TY_PCH)
282  .Case("hip", TY_HIP)
283  .Case("hpp", TY_CXXHeader)
284  .Case("hxx", TY_CXXHeader)
285  .Case("iim", TY_PP_CXXModule)
286  .Case("lib", TY_Object)
287  .Case("mii", TY_PP_ObjCXX)
288  .Case("obj", TY_Object)
289  .Case("ifs", TY_IFS)
290  .Case("pch", TY_PCH)
291  .Case("pcm", TY_ModuleFile)
292  .Case("c++m", TY_CXXModule)
293  .Case("cppm", TY_CXXModule)
294  .Case("cxxm", TY_CXXModule)
295  .Default(TY_INVALID);
296 }
297 
299  for (unsigned i=0; i<numTypes; ++i) {
300  types::ID Id = (types::ID) (i + 1);
301  if (canTypeBeUserSpecified(Id) &&
302  strcmp(Name, getInfo(Id).Name) == 0)
303  return Id;
304  }
305  // Accept "cu" as an alias for "cuda" for NVCC compatibility
306  if (strcmp(Name, "cu") == 0) {
307  return types::TY_CUDA;
308  }
309  return TY_INVALID;
310 }
311 
315  const auto &Info = getInfo(Id);
316  for (int I = 0; I <= LastPhase; ++I)
317  if (Info.Phases.contains(static_cast<phases::ID>(I)))
318  P.push_back(static_cast<phases::ID>(I));
319  assert(P.size() <= phases::MaxNumberOfPhases && "Too many phases in list");
320  return P;
321 }
322 
325  llvm::opt::DerivedArgList &DAL, ID Id) {
327 
328  // Filter to compiler mode. When the compiler is run as a preprocessor then
329  // compilation is not an option.
330  // -S runs the compiler in Assembly listing mode.
331  if (Driver.CCCIsCPP() || DAL.getLastArg(options::OPT_E) ||
332  DAL.getLastArg(options::OPT__SLASH_EP) ||
333  DAL.getLastArg(options::OPT_M, options::OPT_MM) ||
334  DAL.getLastArg(options::OPT__SLASH_P))
336 
337  // --precompile only runs up to precompilation.
338  // This is a clang extension and is not compatible with GCC.
339  else if (DAL.getLastArg(options::OPT__precompile))
341 
342  // -{fsyntax-only,-analyze,emit-ast} only run up to the compiler.
343  else if (DAL.getLastArg(options::OPT_fsyntax_only) ||
344  DAL.getLastArg(options::OPT_print_supported_cpus) ||
345  DAL.getLastArg(options::OPT_module_file_info) ||
346  DAL.getLastArg(options::OPT_verify_pch) ||
347  DAL.getLastArg(options::OPT_rewrite_objc) ||
348  DAL.getLastArg(options::OPT_rewrite_legacy_objc) ||
349  DAL.getLastArg(options::OPT__migrate) ||
350  DAL.getLastArg(options::OPT__analyze) ||
351  DAL.getLastArg(options::OPT_emit_ast))
353 
354  else if (DAL.getLastArg(options::OPT_S) ||
355  DAL.getLastArg(options::OPT_emit_llvm))
357 
358  else if (DAL.getLastArg(options::OPT_c))
360 
361  // Generally means, do every phase until Link.
362  else
364 
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:196
Driver.h
llvm::SmallVector
Definition: LLVM.h:38
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:165
clang::driver::types::lookupTypeForExtension
ID lookupTypeForExtension(llvm::StringRef Ext)
lookupTypeForExtension - Lookup the type to use for the file extension Ext.
Definition: Types.cpp:234
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
clang::driver::Driver::CCCIsCPP
bool CCCIsCPP() const
Whether the driver is just the preprocessor.
Definition: Driver.h:182
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:298
clang::driver::phases::Assemble
@ Assemble
Definition: Phases.h:22
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:230
clang::driver::types::isFortran
bool isFortran(ID Id)
isFortran - Is this a Fortran input.
Definition: Types.cpp:220
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:150
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:183
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::phases::LastPhase
@ LastPhase
Definition: Phases.h:25
clang::driver::types::isOpenCL
bool isOpenCL(ID Id)
isOpenCL - Is this an "OpenCL" input.
Definition: Types.cpp:163
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::getCompilationPhases
llvm::SmallVector< phases::ID, phases::MaxNumberOfPhases > getCompilationPhases(ID Id, phases::ID LastPhase=phases::LastPhase)
getCompilationPhases - Get the list of compilation phases ('Phases') to be done for type 'Id' up unti...
Definition: Types.cpp:313
clang::driver::types::ID
ID
Definition: Types.h:23
clang::driver::types
Definition: Types.h:22
clang::driver::phases::Precompile
@ Precompile
Definition: Phases.h:19
clang::driver::phases::Backend
@ Backend
Definition: Phases.h:21
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:208
clang::driver::phases::MaxNumberOfPhases
@ MaxNumberOfPhases
Definition: Phases.h:29
Types.h
clang::driver::phases::Compile
@ Compile
Definition: Phases.h:20
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