clang  10.0.0svn
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;
28 };
29 
30 static const TypeInfo TypeInfos[] = {
31 #define TYPE(NAME, ID, PP_TYPE, TEMP_SUFFIX, ...) \
32  { NAME, TEMP_SUFFIX, TY_##PP_TYPE, { __VA_ARGS__ }, },
33 #include "clang/Driver/Types.def"
34 #undef TYPE
35 };
36 static const unsigned numTypes = llvm::array_lengthof(TypeInfos);
37 
38 static const TypeInfo &getInfo(unsigned id) {
39  assert(id > 0 && id - 1 < numTypes && "Invalid Type ID.");
40  return TypeInfos[id - 1];
41 }
42 
43 const char *types::getTypeName(ID Id) {
44  return getInfo(Id).Name;
45 }
46 
48  ID PPT = getInfo(Id).PreprocessedType;
49  assert((llvm::is_contained(getInfo(Id).Phases, phases::Preprocess) !=
50  (PPT == TY_INVALID)) &&
51  "Unexpected Preprocess Type.");
52  return PPT;
53 }
54 
56  return Id == TY_CXXModule || Id == TY_PP_CXXModule;
57 }
58 
61  return TY_ModuleFile;
62  if (onlyPrecompileType(Id))
63  return TY_PCH;
64  return TY_INVALID;
65 }
66 
67 const char *types::getTypeTempSuffix(ID Id, bool CLMode) {
68  if (CLMode) {
69  switch (Id) {
70  case TY_Object:
71  case TY_LTO_BC:
72  return "obj";
73  case TY_Image:
74  return "exe";
75  case TY_PP_Asm:
76  return "asm";
77  default:
78  break;
79  }
80  }
81  return getInfo(Id).TempSuffix;
82 }
83 
85  return llvm::is_contained(getInfo(Id).Phases, phases::Assemble) &&
86  !llvm::is_contained(getInfo(Id).Phases, phases::Compile) &&
87  !llvm::is_contained(getInfo(Id).Phases, phases::Backend);
88 }
89 
91  return llvm::is_contained(getInfo(Id).Phases, phases::Precompile) &&
93 }
94 
96  static const clang::driver::types::ID kStaticLangageTypes[] = {
97  TY_CUDA_DEVICE, TY_HIP_DEVICE, TY_PP_CHeader,
98  TY_PP_ObjCHeader, TY_PP_CXXHeader, TY_PP_ObjCXXHeader,
99  TY_PP_CXXModule, TY_LTO_IR, TY_LTO_BC,
100  TY_Plist, TY_RewrittenObjC, TY_RewrittenLegacyObjC,
101  TY_Remap, TY_PCH, TY_Object,
102  TY_Image, TY_dSYM, TY_Dependencies,
103  TY_CUDA_FATBIN, TY_HIP_FATBIN};
104  return !llvm::is_contained(kStaticLangageTypes, Id);
105 }
106 
108  return Id == TY_PCH || Id == TY_dSYM || Id == TY_CUDA_FATBIN ||
109  Id == TY_HIP_FATBIN;
110 }
111 
113  return (Id == TY_Nothing ||
114  Id == TY_Image ||
115  Id == TY_Object ||
116  Id == TY_LTO_BC);
117 }
118 
120  switch (Id) {
121  default:
122  return false;
123 
124  case TY_Asm:
125  case TY_C: case TY_PP_C:
126  case TY_CL:
127  case TY_CUDA: case TY_PP_CUDA:
128  case TY_CUDA_DEVICE:
129  case TY_HIP:
130  case TY_PP_HIP:
131  case TY_HIP_DEVICE:
132  case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
133  case TY_CXX: case TY_PP_CXX:
134  case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
135  case TY_CHeader: case TY_PP_CHeader:
136  case TY_CLHeader:
137  case TY_ObjCHeader: case TY_PP_ObjCHeader:
138  case TY_CXXHeader: case TY_PP_CXXHeader:
139  case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
140  case TY_CXXModule: case TY_PP_CXXModule:
141  case TY_AST: case TY_ModuleFile:
142  case TY_LLVM_IR: case TY_LLVM_BC:
143  return true;
144  }
145 }
146 
148  switch (Id) {
149  default:
150  return false;
151 
152  case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
153  case TY_ObjCXX: case TY_PP_ObjCXX:
154  case TY_ObjCHeader: case TY_PP_ObjCHeader:
155  case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader: case TY_PP_ObjCXX_Alias:
156  return true;
157  }
158 }
159 
161  switch (Id) {
162  default:
163  return false;
164 
165  case TY_CXX: case TY_PP_CXX:
166  case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
167  case TY_CXXHeader: case TY_PP_CXXHeader:
168  case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
169  case TY_CXXModule: case TY_PP_CXXModule:
170  case TY_CUDA: case TY_PP_CUDA: case TY_CUDA_DEVICE:
171  case TY_HIP:
172  case TY_PP_HIP:
173  case TY_HIP_DEVICE:
174  return true;
175  }
176 }
177 
179  switch (Id) {
180  default:
181  return false;
182 
183  case TY_LLVM_IR:
184  case TY_LLVM_BC:
185  case TY_LTO_IR:
186  case TY_LTO_BC:
187  return true;
188  }
189 }
190 
192  switch (Id) {
193  default:
194  return false;
195 
196  case TY_CUDA:
197  case TY_PP_CUDA:
198  case TY_CUDA_DEVICE:
199  return true;
200  }
201 }
202 
204  switch (Id) {
205  default:
206  return false;
207 
208  case TY_HIP:
209  case TY_PP_HIP:
210  case TY_HIP_DEVICE:
211  return true;
212  }
213 }
214 
216  return Id != TY_Object && getPreprocessedType(Id) != TY_INVALID;
217 }
218 
220  return llvm::StringSwitch<types::ID>(Ext)
221  .Case("c", TY_C)
222  .Case("C", TY_CXX)
223  .Case("F", TY_Fortran)
224  .Case("f", TY_PP_Fortran)
225  .Case("h", TY_CHeader)
226  .Case("H", TY_CXXHeader)
227  .Case("i", TY_PP_C)
228  .Case("m", TY_ObjC)
229  .Case("M", TY_ObjCXX)
230  .Case("o", TY_Object)
231  .Case("S", TY_Asm)
232  .Case("s", TY_PP_Asm)
233  .Case("bc", TY_LLVM_BC)
234  .Case("cc", TY_CXX)
235  .Case("CC", TY_CXX)
236  .Case("cl", TY_CL)
237  .Case("cp", TY_CXX)
238  .Case("cu", TY_CUDA)
239  .Case("hh", TY_CXXHeader)
240  .Case("ii", TY_PP_CXX)
241  .Case("ll", TY_LLVM_IR)
242  .Case("mi", TY_PP_ObjC)
243  .Case("mm", TY_ObjCXX)
244  .Case("rs", TY_RenderScript)
245  .Case("adb", TY_Ada)
246  .Case("ads", TY_Ada)
247  .Case("asm", TY_PP_Asm)
248  .Case("ast", TY_AST)
249  .Case("ccm", TY_CXXModule)
250  .Case("cpp", TY_CXX)
251  .Case("CPP", TY_CXX)
252  .Case("c++", TY_CXX)
253  .Case("C++", TY_CXX)
254  .Case("cui", TY_PP_CUDA)
255  .Case("cxx", TY_CXX)
256  .Case("CXX", TY_CXX)
257  .Case("F90", TY_Fortran)
258  .Case("f90", TY_PP_Fortran)
259  .Case("F95", TY_Fortran)
260  .Case("f95", TY_PP_Fortran)
261  .Case("for", TY_PP_Fortran)
262  .Case("FOR", TY_PP_Fortran)
263  .Case("fpp", TY_Fortran)
264  .Case("FPP", TY_Fortran)
265  .Case("gch", TY_PCH)
266  .Case("hip", TY_HIP)
267  .Case("hpp", TY_CXXHeader)
268  .Case("iim", TY_PP_CXXModule)
269  .Case("lib", TY_Object)
270  .Case("mii", TY_PP_ObjCXX)
271  .Case("obj", TY_Object)
272  .Case("ifs", TY_IFS)
273  .Case("pch", TY_PCH)
274  .Case("pcm", TY_ModuleFile)
275  .Case("c++m", TY_CXXModule)
276  .Case("cppm", TY_CXXModule)
277  .Case("cxxm", TY_CXXModule)
278  .Default(TY_INVALID);
279 }
280 
282  for (unsigned i=0; i<numTypes; ++i) {
283  types::ID Id = (types::ID) (i + 1);
284  if (canTypeBeUserSpecified(Id) &&
285  strcmp(Name, getInfo(Id).Name) == 0)
286  return Id;
287  }
288 
289  return TY_INVALID;
290 }
291 
292 // FIXME: Why don't we just put this list in the defs file, eh.
293 // FIXME: The list is now in Types.def but for now this function will verify
294 // the old behavior and a subsequent change will delete most of the body.
296  P = getInfo(Id).Phases;
297  assert(0 < P.size() && "Not enough phases in list");
298  assert(P.size() <= phases::MaxNumberOfPhases && "Too many phases in list");
299 }
300 
302  llvm::opt::DerivedArgList &DAL, ID Id,
305  types::getCompilationPhases(Id, PhaseList);
306 
307  // Filter to compiler mode. When the compiler is run as a preprocessor then
308  // compilation is not an option.
309  // -S runs the compiler in Assembly listing mode.
310  if (Driver.CCCIsCPP() || DAL.getLastArg(options::OPT_E) ||
311  DAL.getLastArg(options::OPT__SLASH_EP) ||
312  DAL.getLastArg(options::OPT_M, options::OPT_MM) ||
313  DAL.getLastArg(options::OPT__SLASH_P))
314  llvm::copy_if(PhaseList, std::back_inserter(P),
315  [](phases::ID Phase) { return Phase <= phases::Preprocess; });
316 
317  // --precompile only runs up to precompilation.
318  // This is a clang extension and is not compatible with GCC.
319  else if (DAL.getLastArg(options::OPT__precompile))
320  llvm::copy_if(PhaseList, std::back_inserter(P),
321  [](phases::ID Phase) { return Phase <= phases::Precompile; });
322 
323  // Treat Interface Stubs like its own compilation mode.
324  else if (DAL.getLastArg(options::OPT_emit_interface_stubs)) {
327  phases::ID LastPhase = phases::IfsMerge;
328  if (Id != types::TY_IFS) {
329  if (DAL.hasArg(options::OPT_c))
330  LastPhase = phases::Compile;
331  PL = IfsModePhaseList;
332  types::getCompilationPhases(types::TY_IFS_CPP, PL);
333  }
334  llvm::copy_if(PL, std::back_inserter(P), [&](phases::ID Phase) {
335  return Phase <= LastPhase;
336  });
337  }
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  llvm::copy_if(PhaseList, std::back_inserter(P),
350  [](phases::ID Phase) { return Phase <= phases::Compile; });
351 
352  else if (DAL.getLastArg(options::OPT_S) ||
353  DAL.getLastArg(options::OPT_emit_llvm))
354  llvm::copy_if(PhaseList, std::back_inserter(P),
355  [](phases::ID Phase) { return Phase <= phases::Backend; });
356 
357  else if (DAL.getLastArg(options::OPT_c))
358  llvm::copy_if(PhaseList, std::back_inserter(P),
359  [](phases::ID Phase) { return Phase <= phases::Assemble; });
360 
361  // Generally means, do every phase until Link.
362  else
363  P = PhaseList;
364 }
365 
367  switch (Id) {
368  default:
369  return Id;
370 
371  case types::TY_C:
372  return types::TY_CXX;
373  case types::TY_PP_C:
374  return types::TY_PP_CXX;
375  case types::TY_CHeader:
376  return types::TY_CXXHeader;
377  case types::TY_PP_CHeader:
378  return types::TY_PP_CXXHeader;
379  }
380 }
381 
383  switch (Id) {
384  default:
385  return Id;
386 
387  // FIXME: Handle preprocessed input types.
388  case types::TY_C:
389  return types::TY_CHeader;
390  case types::TY_CXX:
391  case types::TY_CXXModule:
392  return types::TY_CXXHeader;
393  case types::TY_ObjC:
394  return types::TY_ObjCHeader;
395  case types::TY_ObjCXX:
396  return types::TY_ObjCXXHeader;
397  case types::TY_CL:
398  return types::TY_CLHeader;
399  }
400 }
ID
ID - Ordered values for successive stages in the compilation process which interact with user options...
Definition: Phases.h:17
const llvm::SmallVector< phases::ID, phases::MaxNumberOfPhases > Phases
Definition: Types.cpp:27
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:112
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:67
StringRef P
bool onlyAssembleType(ID Id)
onlyAssembleType - Should this type only be assembled.
Definition: Types.cpp:84
bool canTypeBeUserSpecified(ID Id)
canTypeBeUserSpecified - Can this type be specified on the command line (by the type name); this is u...
Definition: Types.cpp:95
ID lookupCXXTypeForCType(ID Id)
lookupCXXTypeForCType - Lookup CXX input type that corresponds to given C type (used for clang++ emul...
Definition: Types.cpp:366
const char * Name
Definition: Types.cpp:24
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:36
ID PreprocessedType
Definition: Types.cpp:26
bool isHIP(ID Id)
isHIP - Is this a HIP input.
Definition: Types.cpp:203
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:47
static const TypeInfo TypeInfos[]
Definition: Types.cpp:30
ID lookupHeaderTypeForSourceType(ID Id)
Lookup header file input type that corresponds to given source file type (used for clang-cl emulation...
Definition: Types.cpp:382
const char * getTypeName(ID Id)
getTypeName - Return the name of the type for Id.
Definition: Types.cpp:43
int Id
Definition: ASTDiff.cpp:190
void getCompilationPhases(ID Id, llvm::SmallVectorImpl< phases::ID > &Phases)
getCompilationPhases - Get the list of compilation phases (&#39;Phases&#39;) to be done for type &#39;Id&#39;...
Definition: Types.cpp:295
const char * TempSuffix
Definition: Types.cpp:25
bool isCuda(ID Id)
isCuda - Is this a CUDA input.
Definition: Types.cpp:191
bool isAcceptedByClang(ID Id)
isAcceptedByClang - Can clang handle this input type.
Definition: Types.cpp:119
static bool isPrepeocessedModuleType(ID Id)
Definition: Types.cpp:55
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:59
static const TypeInfo & getInfo(unsigned id)
Definition: Types.cpp:38
bool isCXX(ID Id)
isCXX - Is this a "C++" input (C++ and Obj-C++ sources and headers).
Definition: Types.cpp:160
ID lookupTypeForExtension(llvm::StringRef Ext)
lookupTypeForExtension - Lookup the type to use for the file extension Ext.
Definition: Types.cpp:219
bool onlyPrecompileType(ID Id)
onlyPrecompileType - Should this type only be precompiled.
Definition: Types.cpp:90
bool CCCIsCPP() const
Whether the driver is just the preprocessor.
Definition: Driver.h:175
bool appendSuffixForType(ID Id)
appendSuffixForType - When generating outputs of this type, should the suffix be appended (instead of...
Definition: Types.cpp:107
ID lookupTypeForTypeSpecifier(const char *Name)
lookupTypeForTypSpecifier - Lookup the type to use for a user specified type name.
Definition: Types.cpp:281
bool isLLVMIR(ID Id)
Is this LLVM IR.
Definition: Types.cpp:178
bool isSrcFile(ID Id)
isSrcFile - Is this a source file, i.e.
Definition: Types.cpp:215
bool isObjC(ID Id)
isObjC - Is this an "ObjC" input (Obj-C and Obj-C++ sources and headers).
Definition: Types.cpp:147