clang  7.0.0svn
Types.cpp
Go to the documentation of this file.
1 //===--- Types.cpp - Driver input & temporary type information ------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "clang/Driver/Types.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/StringSwitch.h"
13 #include <cassert>
14 #include <string.h>
15 
16 using namespace clang::driver;
17 using namespace clang::driver::types;
18 
19 struct TypeInfo {
20  const char *Name;
21  const char *Flags;
22  const char *TempSuffix;
24 };
25 
26 static const TypeInfo TypeInfos[] = {
27 #define TYPE(NAME, ID, PP_TYPE, TEMP_SUFFIX, FLAGS) \
28  { NAME, FLAGS, TEMP_SUFFIX, TY_##PP_TYPE, },
29 #include "clang/Driver/Types.def"
30 #undef TYPE
31 };
32 static const unsigned numTypes = llvm::array_lengthof(TypeInfos);
33 
34 static const TypeInfo &getInfo(unsigned id) {
35  assert(id > 0 && id - 1 < numTypes && "Invalid Type ID.");
36  return TypeInfos[id - 1];
37 }
38 
39 const char *types::getTypeName(ID Id) {
40  return getInfo(Id).Name;
41 }
42 
44  return getInfo(Id).PreprocessedType;
45 }
46 
48  if (strchr(getInfo(Id).Flags, 'm'))
49  return TY_ModuleFile;
50  if (onlyPrecompileType(Id))
51  return TY_PCH;
52  return TY_INVALID;
53 }
54 
55 const char *types::getTypeTempSuffix(ID Id, bool CLMode) {
56  if (CLMode) {
57  switch (Id) {
58  case TY_Object:
59  case TY_LTO_BC:
60  return "obj";
61  case TY_Image:
62  return "exe";
63  case TY_PP_Asm:
64  return "asm";
65  default:
66  break;
67  }
68  }
69  return getInfo(Id).TempSuffix;
70 }
71 
73  return strchr(getInfo(Id).Flags, 'a');
74 }
75 
77  return strchr(getInfo(Id).Flags, 'p');
78 }
79 
81  return strchr(getInfo(Id).Flags, 'u');
82 }
83 
85  return strchr(getInfo(Id).Flags, 'A');
86 }
87 
89  return (Id == TY_Nothing ||
90  Id == TY_Image ||
91  Id == TY_Object ||
92  Id == TY_LTO_BC);
93 }
94 
96  switch (Id) {
97  default:
98  return false;
99 
100  case TY_Asm:
101  case TY_C: case TY_PP_C:
102  case TY_CL:
103  case TY_CUDA: case TY_PP_CUDA:
104  case TY_CUDA_DEVICE:
105  case TY_HIP:
106  case TY_PP_HIP:
107  case TY_HIP_DEVICE:
108  case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
109  case TY_CXX: case TY_PP_CXX:
110  case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
111  case TY_CHeader: case TY_PP_CHeader:
112  case TY_CLHeader:
113  case TY_ObjCHeader: case TY_PP_ObjCHeader:
114  case TY_CXXHeader: case TY_PP_CXXHeader:
115  case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
116  case TY_CXXModule: case TY_PP_CXXModule:
117  case TY_AST: case TY_ModuleFile:
118  case TY_LLVM_IR: case TY_LLVM_BC:
119  return true;
120  }
121 }
122 
124  switch (Id) {
125  default:
126  return false;
127 
128  case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
129  case TY_ObjCXX: case TY_PP_ObjCXX:
130  case TY_ObjCHeader: case TY_PP_ObjCHeader:
131  case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader: case TY_PP_ObjCXX_Alias:
132  return true;
133  }
134 }
135 
137  switch (Id) {
138  default:
139  return false;
140 
141  case TY_CXX: case TY_PP_CXX:
142  case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
143  case TY_CXXHeader: case TY_PP_CXXHeader:
144  case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
145  case TY_CXXModule: case TY_PP_CXXModule:
146  case TY_CUDA: case TY_PP_CUDA: case TY_CUDA_DEVICE:
147  case TY_HIP:
148  case TY_PP_HIP:
149  case TY_HIP_DEVICE:
150  return true;
151  }
152 }
153 
155  switch (Id) {
156  default:
157  return false;
158 
159  case TY_LLVM_IR:
160  case TY_LLVM_BC:
161  case TY_LTO_IR:
162  case TY_LTO_BC:
163  return true;
164  }
165 }
166 
168  switch (Id) {
169  default:
170  return false;
171 
172  case TY_CUDA:
173  case TY_PP_CUDA:
174  case TY_CUDA_DEVICE:
175  return true;
176  }
177 }
178 
180  switch (Id) {
181  default:
182  return false;
183 
184  case TY_HIP:
185  case TY_PP_HIP:
186  case TY_HIP_DEVICE:
187  return true;
188  }
189 }
190 
192  return Id != TY_Object && getPreprocessedType(Id) != TY_INVALID;
193 }
194 
196  return llvm::StringSwitch<types::ID>(Ext)
197  .Case("c", TY_C)
198  .Case("C", TY_CXX)
199  .Case("F", TY_Fortran)
200  .Case("f", TY_PP_Fortran)
201  .Case("h", TY_CHeader)
202  .Case("H", TY_CXXHeader)
203  .Case("i", TY_PP_C)
204  .Case("m", TY_ObjC)
205  .Case("M", TY_ObjCXX)
206  .Case("o", TY_Object)
207  .Case("S", TY_Asm)
208  .Case("s", TY_PP_Asm)
209  .Case("bc", TY_LLVM_BC)
210  .Case("cc", TY_CXX)
211  .Case("CC", TY_CXX)
212  .Case("cl", TY_CL)
213  .Case("cp", TY_CXX)
214  .Case("cu", TY_CUDA)
215  .Case("hh", TY_CXXHeader)
216  .Case("ii", TY_PP_CXX)
217  .Case("ll", TY_LLVM_IR)
218  .Case("mi", TY_PP_ObjC)
219  .Case("mm", TY_ObjCXX)
220  .Case("rs", TY_RenderScript)
221  .Case("adb", TY_Ada)
222  .Case("ads", TY_Ada)
223  .Case("asm", TY_PP_Asm)
224  .Case("ast", TY_AST)
225  .Case("ccm", TY_CXXModule)
226  .Case("cpp", TY_CXX)
227  .Case("CPP", TY_CXX)
228  .Case("c++", TY_CXX)
229  .Case("C++", TY_CXX)
230  .Case("cui", TY_PP_CUDA)
231  .Case("cxx", TY_CXX)
232  .Case("CXX", TY_CXX)
233  .Case("F90", TY_Fortran)
234  .Case("f90", TY_PP_Fortran)
235  .Case("F95", TY_Fortran)
236  .Case("f95", TY_PP_Fortran)
237  .Case("for", TY_PP_Fortran)
238  .Case("FOR", TY_PP_Fortran)
239  .Case("fpp", TY_Fortran)
240  .Case("FPP", TY_Fortran)
241  .Case("gch", TY_PCH)
242  .Case("hip", TY_HIP)
243  .Case("hpp", TY_CXXHeader)
244  .Case("iim", TY_PP_CXXModule)
245  .Case("lib", TY_Object)
246  .Case("mii", TY_PP_ObjCXX)
247  .Case("obj", TY_Object)
248  .Case("pch", TY_PCH)
249  .Case("pcm", TY_ModuleFile)
250  .Case("c++m", TY_CXXModule)
251  .Case("cppm", TY_CXXModule)
252  .Case("cxxm", TY_CXXModule)
253  .Default(TY_INVALID);
254 }
255 
257  for (unsigned i=0; i<numTypes; ++i) {
258  types::ID Id = (types::ID) (i + 1);
259  if (canTypeBeUserSpecified(Id) &&
260  strcmp(Name, getInfo(Id).Name) == 0)
261  return Id;
262  }
263 
264  return TY_INVALID;
265 }
266 
267 // FIXME: Why don't we just put this list in the defs file, eh.
269  if (Id != TY_Object) {
270  if (getPreprocessedType(Id) != TY_INVALID) {
271  P.push_back(phases::Preprocess);
272  }
273 
274  if (getPrecompiledType(Id) != TY_INVALID) {
275  P.push_back(phases::Precompile);
276  }
277 
278  if (!onlyPrecompileType(Id)) {
279  if (!onlyAssembleType(Id)) {
280  P.push_back(phases::Compile);
281  P.push_back(phases::Backend);
282  }
283  P.push_back(phases::Assemble);
284  }
285  }
286 
287  if (!onlyPrecompileType(Id)) {
288  P.push_back(phases::Link);
289  }
290  assert(0 < P.size() && "Not enough phases in list");
291  assert(P.size() <= phases::MaxNumberOfPhases && "Too many phases in list");
292 }
293 
295  switch (Id) {
296  default:
297  return Id;
298 
299  case types::TY_C:
300  return types::TY_CXX;
301  case types::TY_PP_C:
302  return types::TY_PP_CXX;
303  case types::TY_CHeader:
304  return types::TY_CXXHeader;
305  case types::TY_PP_CHeader:
306  return types::TY_PP_CXXHeader;
307  }
308 }
309 
311  switch (Id) {
312  default:
313  return Id;
314 
315  case types::TY_C:
316  return types::TY_CHeader;
317  case types::TY_CXX:
318  return types::TY_CXXHeader;
319  case types::TY_ObjC:
320  return types::TY_ObjCHeader;
321  case types::TY_ObjCXX:
322  return types::TY_ObjCXXHeader;
323  case types::TY_CL:
324  return types::TY_CLHeader;
325  }
326 }
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:88
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:55
StringRef P
bool onlyAssembleType(ID Id)
onlyAssembleType - Should this type only be assembled.
Definition: Types.cpp:72
bool canTypeBeUserSpecified(ID Id)
canTypeBeUserSpecified - Can this type be specified on the command line (by the type name); this is u...
Definition: Types.cpp:80
ID lookupCXXTypeForCType(ID Id)
lookupCXXTypeForCType - Lookup CXX input type that corresponds to given C type (used for clang++ emul...
Definition: Types.cpp:294
const char * Flags
Definition: Types.cpp:21
const char * Name
Definition: Types.cpp:20
static const unsigned numTypes
Definition: Types.cpp:32
ID PreprocessedType
Definition: Types.cpp:23
bool isHIP(ID Id)
isHIP - Is this a HIP input.
Definition: Types.cpp:179
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:43
static const TypeInfo TypeInfos[]
Definition: Types.cpp:26
ID lookupHeaderTypeForSourceType(ID Id)
Lookup header file input type that corresponds to given source file type (used for clang-cl emulation...
Definition: Types.cpp:310
const char * getTypeName(ID Id)
getTypeName - Return the name of the type for Id.
Definition: Types.cpp:39
int Id
Definition: ASTDiff.cpp:191
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:268
const char * TempSuffix
Definition: Types.cpp:22
bool isCuda(ID Id)
isCuda - Is this a CUDA input.
Definition: Types.cpp:167
bool isAcceptedByClang(ID Id)
isAcceptedByClang - Can clang handle this input type.
Definition: Types.cpp:95
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:47
static const TypeInfo & getInfo(unsigned id)
Definition: Types.cpp:34
bool isCXX(ID Id)
isCXX - Is this a "C++" input (C++ and Obj-C++ sources and headers).
Definition: Types.cpp:136
ID lookupTypeForExtension(llvm::StringRef Ext)
lookupTypeForExtension - Lookup the type to use for the file extension Ext.
Definition: Types.cpp:195
bool onlyPrecompileType(ID Id)
onlyPrecompileType - Should this type only be precompiled.
Definition: Types.cpp:76
bool appendSuffixForType(ID Id)
appendSuffixForType - When generating outputs of this type, should the suffix be appended (instead of...
Definition: Types.cpp:84
ID lookupTypeForTypeSpecifier(const char *Name)
lookupTypeForTypSpecifier - Lookup the type to use for a user specified type name.
Definition: Types.cpp:256
bool isLLVMIR(ID Id)
Is this LLVM IR.
Definition: Types.cpp:154
bool isSrcFile(ID Id)
isSrcFile - Is this a source file, i.e.
Definition: Types.cpp:191
bool isObjC(ID Id)
isObjC - Is this an "ObjC" input (Obj-C and Obj-C++ sources and headers).
Definition: Types.cpp:123