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
12 #include "llvm/ADT/StringSwitch.h"
13 #include <string.h>
14 #include <cassert>
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;
23 ID PreprocessedType;
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 = sizeof(TypeInfos) / sizeof(TypeInfos[0]);
33
getInfo(unsigned id)34 static const TypeInfo &getInfo(unsigned id) {
35 assert(id > 0 && id - 1 < numTypes && "Invalid Type ID.");
36 return TypeInfos[id - 1];
37 }
38
getTypeName(ID Id)39 const char *types::getTypeName(ID Id) {
40 return getInfo(Id).Name;
41 }
42
getPreprocessedType(ID Id)43 types::ID types::getPreprocessedType(ID Id) {
44 return getInfo(Id).PreprocessedType;
45 }
46
getTypeTempSuffix(ID Id)47 const char *types::getTypeTempSuffix(ID Id) {
48 return getInfo(Id).TempSuffix;
49 }
50
onlyAssembleType(ID Id)51 bool types::onlyAssembleType(ID Id) {
52 return strchr(getInfo(Id).Flags, 'a');
53 }
54
onlyPrecompileType(ID Id)55 bool types::onlyPrecompileType(ID Id) {
56 return strchr(getInfo(Id).Flags, 'p');
57 }
58
canTypeBeUserSpecified(ID Id)59 bool types::canTypeBeUserSpecified(ID Id) {
60 return strchr(getInfo(Id).Flags, 'u');
61 }
62
appendSuffixForType(ID Id)63 bool types::appendSuffixForType(ID Id) {
64 return strchr(getInfo(Id).Flags, 'A');
65 }
66
canLipoType(ID Id)67 bool types::canLipoType(ID Id) {
68 return (Id == TY_Nothing ||
69 Id == TY_Image ||
70 Id == TY_Object ||
71 Id == TY_LTO_BC);
72 }
73
isAcceptedByClang(ID Id)74 bool types::isAcceptedByClang(ID Id) {
75 switch (Id) {
76 default:
77 return false;
78
79 case TY_Asm:
80 case TY_C: case TY_PP_C:
81 case TY_CL:
82 case TY_CUDA:
83 case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
84 case TY_CXX: case TY_PP_CXX:
85 case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
86 case TY_CHeader: case TY_PP_CHeader:
87 case TY_CLHeader:
88 case TY_ObjCHeader: case TY_PP_ObjCHeader:
89 case TY_CXXHeader: case TY_PP_CXXHeader:
90 case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
91 case TY_AST:
92 case TY_LLVM_IR: case TY_LLVM_BC:
93 return true;
94 }
95 }
96
isOnlyAcceptedByClang(ID Id)97 bool types::isOnlyAcceptedByClang(ID Id) {
98 switch (Id) {
99 default:
100 return false;
101
102 case TY_AST:
103 case TY_LLVM_IR:
104 case TY_LLVM_BC:
105 case TY_RewrittenObjC:
106 case TY_RewrittenLegacyObjC:
107 return true;
108 }
109 }
110
isObjC(ID Id)111 bool types::isObjC(ID Id) {
112 switch (Id) {
113 default:
114 return false;
115
116 case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
117 case TY_ObjCXX: case TY_PP_ObjCXX:
118 case TY_ObjCHeader: case TY_PP_ObjCHeader:
119 case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader: case TY_PP_ObjCXX_Alias:
120 return true;
121 }
122 }
123
isCXX(ID Id)124 bool types::isCXX(ID Id) {
125 switch (Id) {
126 default:
127 return false;
128
129 case TY_CXX: case TY_PP_CXX:
130 case TY_ObjCXX: case TY_PP_ObjCXX:
131 case TY_CXXHeader: case TY_PP_CXXHeader:
132 case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
133 case TY_CUDA:
134 return true;
135 }
136 }
137
lookupTypeForExtension(const char * Ext)138 types::ID types::lookupTypeForExtension(const char *Ext) {
139 return llvm::StringSwitch<types::ID>(Ext)
140 .Case("c", TY_C)
141 .Case("i", TY_PP_C)
142 .Case("m", TY_ObjC)
143 .Case("M", TY_ObjCXX)
144 .Case("h", TY_CHeader)
145 .Case("C", TY_CXX)
146 .Case("H", TY_CXXHeader)
147 .Case("f", TY_PP_Fortran)
148 .Case("F", TY_Fortran)
149 .Case("s", TY_PP_Asm)
150 .Case("S", TY_Asm)
151 .Case("o", TY_Object)
152 .Case("ii", TY_PP_CXX)
153 .Case("mi", TY_PP_ObjC)
154 .Case("mm", TY_ObjCXX)
155 .Case("bc", TY_LLVM_BC)
156 .Case("cc", TY_CXX)
157 .Case("CC", TY_CXX)
158 .Case("cl", TY_CL)
159 .Case("cp", TY_CXX)
160 .Case("cu", TY_CUDA)
161 .Case("hh", TY_CXXHeader)
162 .Case("ll", TY_LLVM_IR)
163 .Case("hpp", TY_CXXHeader)
164 .Case("ads", TY_Ada)
165 .Case("adb", TY_Ada)
166 .Case("ast", TY_AST)
167 .Case("c++", TY_CXX)
168 .Case("C++", TY_CXX)
169 .Case("cxx", TY_CXX)
170 .Case("cpp", TY_CXX)
171 .Case("CPP", TY_CXX)
172 .Case("CXX", TY_CXX)
173 .Case("for", TY_PP_Fortran)
174 .Case("FOR", TY_PP_Fortran)
175 .Case("fpp", TY_Fortran)
176 .Case("FPP", TY_Fortran)
177 .Case("f90", TY_PP_Fortran)
178 .Case("f95", TY_PP_Fortran)
179 .Case("F90", TY_Fortran)
180 .Case("F95", TY_Fortran)
181 .Case("mii", TY_PP_ObjCXX)
182 .Default(TY_INVALID);
183 }
184
lookupTypeForTypeSpecifier(const char * Name)185 types::ID types::lookupTypeForTypeSpecifier(const char *Name) {
186 unsigned N = strlen(Name);
187
188 for (unsigned i=0; i<numTypes; ++i) {
189 types::ID Id = (types::ID) (i + 1);
190 if (canTypeBeUserSpecified(Id) &&
191 memcmp(Name, getInfo(Id).Name, N + 1) == 0)
192 return Id;
193 }
194
195 return TY_INVALID;
196 }
197
198 // FIXME: Why don't we just put this list in the defs file, eh.
199
getNumCompilationPhases(ID Id)200 unsigned types::getNumCompilationPhases(ID Id) {
201 if (Id == TY_Object)
202 return 1;
203
204 unsigned N = 0;
205 if (getPreprocessedType(Id) != TY_INVALID)
206 N += 1;
207
208 if (onlyAssembleType(Id))
209 return N + 2; // assemble, link
210 if (onlyPrecompileType(Id))
211 return N + 1; // precompile
212
213 return N + 3; // compile, assemble, link
214 }
215
getCompilationPhase(ID Id,unsigned N)216 phases::ID types::getCompilationPhase(ID Id, unsigned N) {
217 assert(N < getNumCompilationPhases(Id) && "Invalid index.");
218
219 if (Id == TY_Object)
220 return phases::Link;
221
222 if (getPreprocessedType(Id) != TY_INVALID) {
223 if (N == 0)
224 return phases::Preprocess;
225 --N;
226 }
227
228 if (onlyAssembleType(Id))
229 return N == 0 ? phases::Assemble : phases::Link;
230
231 if (onlyPrecompileType(Id))
232 return phases::Precompile;
233
234 if (N == 0)
235 return phases::Compile;
236 if (N == 1)
237 return phases::Assemble;
238
239 return phases::Link;
240 }
241
lookupCXXTypeForCType(ID Id)242 ID types::lookupCXXTypeForCType(ID Id) {
243 switch (Id) {
244 default:
245 return Id;
246
247 case types::TY_C:
248 return types::TY_CXX;
249 case types::TY_PP_C:
250 return types::TY_PP_CXX;
251 case types::TY_CHeader:
252 return types::TY_CXXHeader;
253 case types::TY_PP_CHeader:
254 return types::TY_PP_CXXHeader;
255 }
256 }
257