clang  11.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 
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;
62 }
63 
65  return Id == TY_CXXModule || Id == TY_PP_CXXModule;
66 }
67 
70  return TY_ModuleFile;
71  if (onlyPrecompileType(Id))
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:
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:
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 
164  switch (Id) {
165  default:
166  return false;
167 
168  case TY_CXX: case TY_PP_CXX:
169  case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
170  case TY_CXXHeader: case TY_PP_CXXHeader:
171  case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
172  case TY_CXXModule: case TY_PP_CXXModule:
173  case TY_CUDA: case TY_PP_CUDA: case TY_CUDA_DEVICE:
174  case TY_HIP:
175  case TY_PP_HIP:
176  case TY_HIP_DEVICE:
177  return true;
178  }
179 }
180 
182  switch (Id) {
183  default:
184  return false;
185 
186  case TY_LLVM_IR:
187  case TY_LLVM_BC:
188  case TY_LTO_IR:
189  case TY_LTO_BC:
190  return true;
191  }
192 }
193 
195  switch (Id) {
196  default:
197  return false;
198 
199  case TY_CUDA:
200  case TY_PP_CUDA:
201  case TY_CUDA_DEVICE:
202  return true;
203  }
204 }
205 
207  switch (Id) {
208  default:
209  return false;
210 
211  case TY_HIP:
212  case TY_PP_HIP:
213  case TY_HIP_DEVICE:
214  return true;
215  }
216 }
217 
219  switch (Id) {
220  default:
221  return false;
222 
223  case TY_Fortran: case TY_PP_Fortran:
224  return true;
225  }
226 }
227 
229  return Id != TY_Object && getPreprocessedType(Id) != TY_INVALID;
230 }
231 
233  return llvm::StringSwitch<types::ID>(Ext)
234  .Case("c", TY_C)
235  .Case("C", TY_CXX)
236  .Case("F", TY_Fortran)
237  .Case("f", TY_PP_Fortran)
238  .Case("h", TY_CHeader)
239  .Case("H", TY_CXXHeader)
240  .Case("i", TY_PP_C)
241  .Case("m", TY_ObjC)
242  .Case("M", TY_ObjCXX)
243  .Case("o", TY_Object)
244  .Case("S", TY_Asm)
245  .Case("s", TY_PP_Asm)
246  .Case("bc", TY_LLVM_BC)
247  .Case("cc", TY_CXX)
248  .Case("CC", TY_CXX)
249  .Case("cl", TY_CL)
250  .Case("cp", TY_CXX)
251  .Case("cu", TY_CUDA)
252  .Case("hh", TY_CXXHeader)
253  .Case("ii", TY_PP_CXX)
254  .Case("ll", TY_LLVM_IR)
255  .Case("mi", TY_PP_ObjC)
256  .Case("mm", TY_ObjCXX)
257  .Case("rs", TY_RenderScript)
258  .Case("adb", TY_Ada)
259  .Case("ads", TY_Ada)
260  .Case("asm", TY_PP_Asm)
261  .Case("ast", TY_AST)
262  .Case("ccm", TY_CXXModule)
263  .Case("cpp", TY_CXX)
264  .Case("CPP", TY_CXX)
265  .Case("c++", TY_CXX)
266  .Case("C++", TY_CXX)
267  .Case("cui", TY_PP_CUDA)
268  .Case("cxx", TY_CXX)
269  .Case("CXX", TY_CXX)
270  .Case("F90", TY_Fortran)
271  .Case("f90", TY_PP_Fortran)
272  .Case("F95", TY_Fortran)
273  .Case("f95", TY_PP_Fortran)
274  .Case("for", TY_PP_Fortran)
275  .Case("FOR", TY_PP_Fortran)
276  .Case("fpp", TY_Fortran)
277  .Case("FPP", TY_Fortran)
278  .Case("gch", TY_PCH)
279  .Case("hip", TY_HIP)
280  .Case("hpp", TY_CXXHeader)
281  .Case("hxx", TY_CXXHeader)
282  .Case("iim", TY_PP_CXXModule)
283  .Case("lib", TY_Object)
284  .Case("mii", TY_PP_ObjCXX)
285  .Case("obj", TY_Object)
286  .Case("ifs", TY_IFS)
287  .Case("pch", TY_PCH)
288  .Case("pcm", TY_ModuleFile)
289  .Case("c++m", TY_CXXModule)
290  .Case("cppm", TY_CXXModule)
291  .Case("cxxm", TY_CXXModule)
292  .Default(TY_INVALID);
293 }
294 
296  for (unsigned i=0; i<numTypes; ++i) {
297  types::ID Id = (types::ID) (i + 1);
298  if (canTypeBeUserSpecified(Id) &&
299  strcmp(Name, getInfo(Id).Name) == 0)
300  return Id;
301  }
302  // Accept "cu" as an alias for "cuda" for NVCC compatibility
303  if (strcmp(Name, "cu") == 0) {
304  return types::TY_CUDA;
305  }
306  return TY_INVALID;
307 }
308 
312  const auto &Info = getInfo(Id);
313  for (int I = 0; I <= LastPhase; ++I)
314  if (Info.Phases.contains(static_cast<phases::ID>(I)))
315  P.push_back(static_cast<phases::ID>(I));
316  assert(P.size() <= phases::MaxNumberOfPhases && "Too many phases in list");
317  return P;
318 }
319 
322  llvm::opt::DerivedArgList &DAL, ID Id) {
324 
325  // Filter to compiler mode. When the compiler is run as a preprocessor then
326  // compilation is not an option.
327  // -S runs the compiler in Assembly listing mode.
328  if (Driver.CCCIsCPP() || DAL.getLastArg(options::OPT_E) ||
329  DAL.getLastArg(options::OPT__SLASH_EP) ||
330  DAL.getLastArg(options::OPT_M, options::OPT_MM) ||
331  DAL.getLastArg(options::OPT__SLASH_P))
332  LastPhase = phases::Preprocess;
333 
334  // --precompile only runs up to precompilation.
335  // This is a clang extension and is not compatible with GCC.
336  else if (DAL.getLastArg(options::OPT__precompile))
337  LastPhase = phases::Precompile;
338 
339  // -{fsyntax-only,-analyze,emit-ast} only run up to the compiler.
340  else if (DAL.getLastArg(options::OPT_fsyntax_only) ||
341  DAL.getLastArg(options::OPT_print_supported_cpus) ||
342  DAL.getLastArg(options::OPT_module_file_info) ||
343  DAL.getLastArg(options::OPT_verify_pch) ||
344  DAL.getLastArg(options::OPT_rewrite_objc) ||
345  DAL.getLastArg(options::OPT_rewrite_legacy_objc) ||
346  DAL.getLastArg(options::OPT__migrate) ||
347  DAL.getLastArg(options::OPT__analyze) ||
348  DAL.getLastArg(options::OPT_emit_ast))
349  LastPhase = phases::Compile;
350 
351  else if (DAL.getLastArg(options::OPT_S) ||
352  DAL.getLastArg(options::OPT_emit_llvm))
353  LastPhase = phases::Backend;
354 
355  else if (DAL.getLastArg(options::OPT_c))
356  LastPhase = phases::Assemble;
357 
358  // Generally means, do every phase until Link.
359  else
360  LastPhase = phases::LastPhase;
361 
362  return types::getCompilationPhases(Id, LastPhase);
363 }
364 
366  switch (Id) {
367  default:
368  return Id;
369 
370  case types::TY_C:
371  return types::TY_CXX;
372  case types::TY_PP_C:
373  return types::TY_PP_CXX;
374  case types::TY_CHeader:
375  return types::TY_CXXHeader;
376  case types::TY_PP_CHeader:
377  return types::TY_PP_CXXHeader;
378  }
379 }
380 
382  switch (Id) {
383  default:
384  return Id;
385 
386  // FIXME: Handle preprocessed input types.
387  case types::TY_C:
388  return types::TY_CHeader;
389  case types::TY_CXX:
390  case types::TY_CXXModule:
391  return types::TY_CXXHeader;
392  case types::TY_ObjC:
393  return types::TY_ObjCHeader;
394  case types::TY_ObjCXX:
395  return types::TY_ObjCXXHeader;
396  case types::TY_CL:
397  return types::TY_CLHeader;
398  }
399 }
ID
ID - Ordered values for successive stages in the compilation process which interact with user options...
Definition: Phases.h:17
static bool isPreprocessedModuleType(ID Id)
Definition: Types.cpp:64
bool canLipoType(ID Id)
canLipoType - Is this type acceptable as the output of a universal build (currently, just the Nothing, Image, and Object types).
Definition: Types.cpp:115
const char * getTypeTempSuffix(ID Id, bool CLMode=false)
getTypeTempSuffix - Return the suffix to use when creating a temp file of this type, or null if unspecified.
Definition: Types.cpp:76
StringRef P
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
llvm::SmallVector< phases::ID, phases::MaxNumberOfPhases > getCompilationPhases(ID Id, phases::ID LastPhase=phases::LastPhase)
getCompilationPhases - Get the list of compilation phases (&#39;Phases&#39;) to be done for type &#39;Id&#39; up unti...
Definition: Types.cpp:310
ID lookupCXXTypeForCType(ID Id)
lookupCXXTypeForCType - Lookup CXX input type that corresponds to given C type (used for clang++ emul...
Definition: Types.cpp:365
bool contains(phases::ID Id) const
Definition: Types.cpp:35
const char * Name
Definition: Types.cpp:24
static constexpr TypeInfo TypeInfos[]
Definition: Types.cpp:39
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
static const unsigned numTypes
Definition: Types.cpp:45
ID PreprocessedType
Definition: Types.cpp:26
bool isHIP(ID Id)
isHIP - Is this a HIP input.
Definition: Types.cpp:206
ID getPreprocessedType(ID Id)
getPreprocessedType - Get the ID of the type for this input when it has been preprocessed, or INVALID if this input is not preprocessed.
Definition: Types.cpp:56
ID lookupHeaderTypeForSourceType(ID Id)
Lookup header file input type that corresponds to given source file type (used for clang-cl emulation...
Definition: Types.cpp:381
const char * getTypeName(ID Id)
getTypeName - Return the name of the type for Id.
Definition: Types.cpp:52
int Id
Definition: ASTDiff.cpp:191
const char * TempSuffix
Definition: Types.cpp:25
class TypeInfo::PhasesBitSet Phases
bool isCuda(ID Id)
isCuda - Is this a CUDA input.
Definition: Types.cpp:194
bool isAcceptedByClang(ID Id)
isAcceptedByClang - Can clang handle this input type.
Definition: Types.cpp:122
constexpr PhasesBitSet(std::initializer_list< phases::ID > Phases)
Definition: Types.cpp:31
ID getPrecompiledType(ID Id)
getPrecompiledType - Get the ID of the type for this input when it has been precompiled, or INVALID if this input is not precompiled.
Definition: Types.cpp:68
static const TypeInfo & getInfo(unsigned id)
Definition: Types.cpp:47
bool isCXX(ID Id)
isCXX - Is this a "C++" input (C++ and Obj-C++ sources and headers).
Definition: Types.cpp:163
ID lookupTypeForExtension(llvm::StringRef Ext)
lookupTypeForExtension - Lookup the type to use for the file extension Ext.
Definition: Types.cpp:232
bool onlyPrecompileType(ID Id)
onlyPrecompileType - Should this type only be precompiled.
Definition: Types.cpp:93
bool CCCIsCPP() const
Whether the driver is just the preprocessor.
Definition: Driver.h:176
bool appendSuffixForType(ID Id)
appendSuffixForType - When generating outputs of this type, should the suffix be appended (instead of...
Definition: Types.cpp:110
ID lookupTypeForTypeSpecifier(const char *Name)
lookupTypeForTypSpecifier - Lookup the type to use for a user specified type name.
Definition: Types.cpp:295
bool isLLVMIR(ID Id)
Is this LLVM IR.
Definition: Types.cpp:181
bool isSrcFile(ID Id)
isSrcFile - Is this a source file, i.e.
Definition: Types.cpp:228
bool isFortran(ID Id)
isFortran - Is this a Fortran input.
Definition: Types.cpp:218
bool isObjC(ID Id)
isObjC - Is this an "ObjC" input (Obj-C and Obj-C++ sources and headers).
Definition: Types.cpp:150