clang  6.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_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
106  case TY_CXX: case TY_PP_CXX:
107  case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
108  case TY_CHeader: case TY_PP_CHeader:
109  case TY_CLHeader:
110  case TY_ObjCHeader: case TY_PP_ObjCHeader:
111  case TY_CXXHeader: case TY_PP_CXXHeader:
112  case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
113  case TY_CXXModule: case TY_PP_CXXModule:
114  case TY_AST: case TY_ModuleFile:
115  case TY_LLVM_IR: case TY_LLVM_BC:
116  return true;
117  }
118 }
119 
121  switch (Id) {
122  default:
123  return false;
124 
125  case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
126  case TY_ObjCXX: case TY_PP_ObjCXX:
127  case TY_ObjCHeader: case TY_PP_ObjCHeader:
128  case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader: case TY_PP_ObjCXX_Alias:
129  return true;
130  }
131 }
132 
134  switch (Id) {
135  default:
136  return false;
137 
138  case TY_CXX: case TY_PP_CXX:
139  case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
140  case TY_CXXHeader: case TY_PP_CXXHeader:
141  case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
142  case TY_CXXModule: case TY_PP_CXXModule:
143  case TY_CUDA: case TY_PP_CUDA: case TY_CUDA_DEVICE:
144  return true;
145  }
146 }
147 
149  switch (Id) {
150  default:
151  return false;
152 
153  case TY_LLVM_IR:
154  case TY_LLVM_BC:
155  case TY_LTO_IR:
156  case TY_LTO_BC:
157  return true;
158  }
159 }
160 
162  switch (Id) {
163  default:
164  return false;
165 
166  case TY_CUDA:
167  case TY_PP_CUDA:
168  case TY_CUDA_DEVICE:
169  return true;
170  }
171 }
172 
174  return Id != TY_Object && getPreprocessedType(Id) != TY_INVALID;
175 }
176 
178  return llvm::StringSwitch<types::ID>(Ext)
179  .Case("c", TY_C)
180  .Case("C", TY_CXX)
181  .Case("F", TY_Fortran)
182  .Case("f", TY_PP_Fortran)
183  .Case("h", TY_CHeader)
184  .Case("H", TY_CXXHeader)
185  .Case("i", TY_PP_C)
186  .Case("m", TY_ObjC)
187  .Case("M", TY_ObjCXX)
188  .Case("o", TY_Object)
189  .Case("S", TY_Asm)
190  .Case("s", TY_PP_Asm)
191  .Case("bc", TY_LLVM_BC)
192  .Case("cc", TY_CXX)
193  .Case("CC", TY_CXX)
194  .Case("cl", TY_CL)
195  .Case("cp", TY_CXX)
196  .Case("cu", TY_CUDA)
197  .Case("hh", TY_CXXHeader)
198  .Case("ii", TY_PP_CXX)
199  .Case("ll", TY_LLVM_IR)
200  .Case("mi", TY_PP_ObjC)
201  .Case("mm", TY_ObjCXX)
202  .Case("rs", TY_RenderScript)
203  .Case("adb", TY_Ada)
204  .Case("ads", TY_Ada)
205  .Case("asm", TY_PP_Asm)
206  .Case("ast", TY_AST)
207  .Case("ccm", TY_CXXModule)
208  .Case("cpp", TY_CXX)
209  .Case("CPP", TY_CXX)
210  .Case("c++", TY_CXX)
211  .Case("C++", TY_CXX)
212  .Case("cui", TY_PP_CUDA)
213  .Case("cxx", TY_CXX)
214  .Case("CXX", TY_CXX)
215  .Case("F90", TY_Fortran)
216  .Case("f90", TY_PP_Fortran)
217  .Case("F95", TY_Fortran)
218  .Case("f95", TY_PP_Fortran)
219  .Case("for", TY_PP_Fortran)
220  .Case("FOR", TY_PP_Fortran)
221  .Case("fpp", TY_Fortran)
222  .Case("FPP", TY_Fortran)
223  .Case("gch", TY_PCH)
224  .Case("hpp", TY_CXXHeader)
225  .Case("iim", TY_PP_CXXModule)
226  .Case("lib", TY_Object)
227  .Case("mii", TY_PP_ObjCXX)
228  .Case("obj", TY_Object)
229  .Case("pch", TY_PCH)
230  .Case("pcm", TY_ModuleFile)
231  .Case("c++m", TY_CXXModule)
232  .Case("cppm", TY_CXXModule)
233  .Case("cxxm", TY_CXXModule)
234  .Default(TY_INVALID);
235 }
236 
238  for (unsigned i=0; i<numTypes; ++i) {
239  types::ID Id = (types::ID) (i + 1);
240  if (canTypeBeUserSpecified(Id) &&
241  strcmp(Name, getInfo(Id).Name) == 0)
242  return Id;
243  }
244 
245  return TY_INVALID;
246 }
247 
248 // FIXME: Why don't we just put this list in the defs file, eh.
250  if (Id != TY_Object) {
251  if (getPreprocessedType(Id) != TY_INVALID) {
252  P.push_back(phases::Preprocess);
253  }
254 
255  if (getPrecompiledType(Id) != TY_INVALID) {
256  P.push_back(phases::Precompile);
257  }
258 
259  if (!onlyPrecompileType(Id)) {
260  if (!onlyAssembleType(Id)) {
261  P.push_back(phases::Compile);
262  P.push_back(phases::Backend);
263  }
264  P.push_back(phases::Assemble);
265  }
266  }
267 
268  if (!onlyPrecompileType(Id)) {
269  P.push_back(phases::Link);
270  }
271  assert(0 < P.size() && "Not enough phases in list");
272  assert(P.size() <= phases::MaxNumberOfPhases && "Too many phases in list");
273 }
274 
276  switch (Id) {
277  default:
278  return Id;
279 
280  case types::TY_C:
281  return types::TY_CXX;
282  case types::TY_PP_C:
283  return types::TY_PP_CXX;
284  case types::TY_CHeader:
285  return types::TY_CXXHeader;
286  case types::TY_PP_CHeader:
287  return types::TY_PP_CXXHeader;
288  }
289 }
290 
292  switch (Id) {
293  default:
294  return Id;
295 
296  case types::TY_C:
297  return types::TY_CHeader;
298  case types::TY_CXX:
299  return types::TY_CXXHeader;
300  case types::TY_ObjC:
301  return types::TY_ObjCHeader;
302  case types::TY_ObjCXX:
303  return types::TY_ObjCXXHeader;
304  case types::TY_CL:
305  return types::TY_CLHeader;
306  }
307 }
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:275
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
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:291
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:249
const char * TempSuffix
Definition: Types.cpp:22
bool isCuda(ID Id)
isCuda - Is this a CUDA input.
Definition: Types.cpp:161
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:133
ID lookupTypeForExtension(llvm::StringRef Ext)
lookupTypeForExtension - Lookup the type to use for the file extension Ext.
Definition: Types.cpp:177
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:237
bool isLLVMIR(ID Id)
Is this LLVM IR.
Definition: Types.cpp:148
bool isSrcFile(ID Id)
isSrcFile - Is this a source file, i.e.
Definition: Types.cpp:173
bool isObjC(ID Id)
isObjC - Is this an "ObjC" input (Obj-C and Obj-C++ sources and headers).
Definition: Types.cpp:120