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;
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 = llvm::array_lengthof(TypeInfos);
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,bool CLMode)47 const char *types::getTypeTempSuffix(ID Id, bool CLMode) {
48 if (Id == TY_Object && CLMode)
49 return "obj";
50 return getInfo(Id).TempSuffix;
51 }
52
onlyAssembleType(ID Id)53 bool types::onlyAssembleType(ID Id) {
54 return strchr(getInfo(Id).Flags, 'a');
55 }
56
onlyPrecompileType(ID Id)57 bool types::onlyPrecompileType(ID Id) {
58 return strchr(getInfo(Id).Flags, 'p');
59 }
60
canTypeBeUserSpecified(ID Id)61 bool types::canTypeBeUserSpecified(ID Id) {
62 return strchr(getInfo(Id).Flags, 'u');
63 }
64
appendSuffixForType(ID Id)65 bool types::appendSuffixForType(ID Id) {
66 return strchr(getInfo(Id).Flags, 'A');
67 }
68
canLipoType(ID Id)69 bool types::canLipoType(ID Id) {
70 return (Id == TY_Nothing ||
71 Id == TY_Image ||
72 Id == TY_Object ||
73 Id == TY_LTO_BC);
74 }
75
isAcceptedByClang(ID Id)76 bool types::isAcceptedByClang(ID Id) {
77 switch (Id) {
78 default:
79 return false;
80
81 case TY_Asm:
82 case TY_C: case TY_PP_C:
83 case TY_CL:
84 case TY_CUDA:
85 case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
86 case TY_CXX: case TY_PP_CXX:
87 case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
88 case TY_CHeader: case TY_PP_CHeader:
89 case TY_CLHeader:
90 case TY_ObjCHeader: case TY_PP_ObjCHeader:
91 case TY_CXXHeader: case TY_PP_CXXHeader:
92 case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
93 case TY_AST: case TY_ModuleFile:
94 case TY_LLVM_IR: case TY_LLVM_BC:
95 return true;
96 }
97 }
98
isObjC(ID Id)99 bool types::isObjC(ID Id) {
100 switch (Id) {
101 default:
102 return false;
103
104 case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
105 case TY_ObjCXX: case TY_PP_ObjCXX:
106 case TY_ObjCHeader: case TY_PP_ObjCHeader:
107 case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader: case TY_PP_ObjCXX_Alias:
108 return true;
109 }
110 }
111
isCXX(ID Id)112 bool types::isCXX(ID Id) {
113 switch (Id) {
114 default:
115 return false;
116
117 case TY_CXX: case TY_PP_CXX:
118 case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
119 case TY_CXXHeader: case TY_PP_CXXHeader:
120 case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
121 case TY_CUDA:
122 return true;
123 }
124 }
125
lookupTypeForExtension(const char * Ext)126 types::ID types::lookupTypeForExtension(const char *Ext) {
127 return llvm::StringSwitch<types::ID>(Ext)
128 .Case("c", TY_C)
129 .Case("i", TY_PP_C)
130 .Case("m", TY_ObjC)
131 .Case("M", TY_ObjCXX)
132 .Case("h", TY_CHeader)
133 .Case("C", TY_CXX)
134 .Case("H", TY_CXXHeader)
135 .Case("f", TY_PP_Fortran)
136 .Case("F", TY_Fortran)
137 .Case("s", TY_PP_Asm)
138 .Case("S", TY_Asm)
139 .Case("o", TY_Object)
140 .Case("obj", TY_Object)
141 .Case("ii", TY_PP_CXX)
142 .Case("mi", TY_PP_ObjC)
143 .Case("mm", TY_ObjCXX)
144 .Case("bc", TY_LLVM_BC)
145 .Case("cc", TY_CXX)
146 .Case("CC", TY_CXX)
147 .Case("cl", TY_CL)
148 .Case("cp", TY_CXX)
149 .Case("cu", TY_CUDA)
150 .Case("hh", TY_CXXHeader)
151 .Case("ll", TY_LLVM_IR)
152 .Case("hpp", TY_CXXHeader)
153 .Case("ads", TY_Ada)
154 .Case("adb", TY_Ada)
155 .Case("ast", TY_AST)
156 .Case("c++", TY_CXX)
157 .Case("C++", TY_CXX)
158 .Case("cxx", TY_CXX)
159 .Case("cpp", TY_CXX)
160 .Case("CPP", TY_CXX)
161 .Case("CXX", TY_CXX)
162 .Case("for", TY_PP_Fortran)
163 .Case("FOR", TY_PP_Fortran)
164 .Case("fpp", TY_Fortran)
165 .Case("FPP", TY_Fortran)
166 .Case("f90", TY_PP_Fortran)
167 .Case("f95", TY_PP_Fortran)
168 .Case("F90", TY_Fortran)
169 .Case("F95", TY_Fortran)
170 .Case("mii", TY_PP_ObjCXX)
171 .Case("pcm", TY_ModuleFile)
172 .Default(TY_INVALID);
173 }
174
lookupTypeForTypeSpecifier(const char * Name)175 types::ID types::lookupTypeForTypeSpecifier(const char *Name) {
176 for (unsigned i=0; i<numTypes; ++i) {
177 types::ID Id = (types::ID) (i + 1);
178 if (canTypeBeUserSpecified(Id) &&
179 strcmp(Name, getInfo(Id).Name) == 0)
180 return Id;
181 }
182
183 return TY_INVALID;
184 }
185
186 // FIXME: Why don't we just put this list in the defs file, eh.
getCompilationPhases(ID Id,llvm::SmallVectorImpl<phases::ID> & P)187 void types::getCompilationPhases(ID Id, llvm::SmallVectorImpl<phases::ID> &P) {
188 if (Id != TY_Object) {
189 if (getPreprocessedType(Id) != TY_INVALID) {
190 P.push_back(phases::Preprocess);
191 }
192
193 if (onlyPrecompileType(Id)) {
194 P.push_back(phases::Precompile);
195 } else {
196 if (!onlyAssembleType(Id)) {
197 P.push_back(phases::Compile);
198 }
199 P.push_back(phases::Assemble);
200 }
201 }
202 if (!onlyPrecompileType(Id)) {
203 P.push_back(phases::Link);
204 }
205 assert(0 < P.size() && "Not enough phases in list");
206 assert(P.size() <= phases::MaxNumberOfPhases && "Too many phases in list");
207 return;
208 }
209
lookupCXXTypeForCType(ID Id)210 ID types::lookupCXXTypeForCType(ID Id) {
211 switch (Id) {
212 default:
213 return Id;
214
215 case types::TY_C:
216 return types::TY_CXX;
217 case types::TY_PP_C:
218 return types::TY_PP_CXX;
219 case types::TY_CHeader:
220 return types::TY_CXXHeader;
221 case types::TY_PP_CHeader:
222 return types::TY_PP_CXXHeader;
223 }
224 }
225