blob: 1647e757e3fe0aaea43def16c1d012912fa112b9 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000017#include "clang/AST/Expr.h"
18#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "clang/Basic/TargetInfo.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000020#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000021#include "llvm/Bitcode/Serialize.h"
22#include "llvm/Bitcode/Deserialize.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000023
Chris Lattner4b009652007-07-25 00:24:17 +000024using namespace clang;
25
26enum FloatingRank {
27 FloatRank, DoubleRank, LongDoubleRank
28};
29
Chris Lattner2fda0ed2008-10-05 17:34:18 +000030ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
31 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000032 IdentifierTable &idents, SelectorTable &sels,
33 unsigned size_reserve) :
Anders Carlssonf58cac72008-08-30 19:34:46 +000034 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
35 SourceMgr(SM), LangOpts(LOpts), Target(t),
Douglas Gregor24afd4a2008-11-17 14:58:09 +000036 Idents(idents), Selectors(sels)
Daniel Dunbarde300732008-08-11 04:54:23 +000037{
38 if (size_reserve > 0) Types.reserve(size_reserve);
39 InitBuiltinTypes();
40 BuiltinInfo.InitializeBuiltins(idents, Target);
41 TUDecl = TranslationUnitDecl::Create(*this);
42}
43
Chris Lattner4b009652007-07-25 00:24:17 +000044ASTContext::~ASTContext() {
45 // Deallocate all the types.
46 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000047 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000048 Types.pop_back();
49 }
Eli Friedman65489b72008-05-27 03:08:09 +000050
51 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000052}
53
54void ASTContext::PrintStats() const {
55 fprintf(stderr, "*** AST Context Stats:\n");
56 fprintf(stderr, " %d types total.\n", (int)Types.size());
57 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar47677342008-09-26 03:23:00 +000058 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000059 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
60
61 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000062 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
63 unsigned NumObjCQualifiedIds = 0;
Steve Naroffe0430632008-05-21 15:59:22 +000064 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000065
66 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
67 Type *T = Types[i];
68 if (isa<BuiltinType>(T))
69 ++NumBuiltin;
70 else if (isa<PointerType>(T))
71 ++NumPointer;
Daniel Dunbar47677342008-09-26 03:23:00 +000072 else if (isa<BlockPointerType>(T))
73 ++NumBlockPointer;
Chris Lattner4b009652007-07-25 00:24:17 +000074 else if (isa<ReferenceType>(T))
75 ++NumReference;
76 else if (isa<ComplexType>(T))
77 ++NumComplex;
78 else if (isa<ArrayType>(T))
79 ++NumArray;
80 else if (isa<VectorType>(T))
81 ++NumVector;
82 else if (isa<FunctionTypeNoProto>(T))
83 ++NumFunctionNP;
84 else if (isa<FunctionTypeProto>(T))
85 ++NumFunctionP;
86 else if (isa<TypedefType>(T))
87 ++NumTypeName;
88 else if (TagType *TT = dyn_cast<TagType>(T)) {
89 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +000090 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +000091 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +000092 case TagDecl::TK_struct: ++NumTagStruct; break;
93 case TagDecl::TK_union: ++NumTagUnion; break;
94 case TagDecl::TK_class: ++NumTagClass; break;
95 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +000096 }
Ted Kremenek42730c52008-01-07 19:49:32 +000097 } else if (isa<ObjCInterfaceType>(T))
98 ++NumObjCInterfaces;
99 else if (isa<ObjCQualifiedInterfaceType>(T))
100 ++NumObjCQualifiedInterfaces;
101 else if (isa<ObjCQualifiedIdType>(T))
102 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +0000103 else if (isa<TypeOfType>(T))
104 ++NumTypeOfTypes;
105 else if (isa<TypeOfExpr>(T))
106 ++NumTypeOfExprs;
Steve Naroff948fd372007-09-17 14:16:13 +0000107 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000108 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000109 assert(0 && "Unknown type!");
110 }
111 }
112
113 fprintf(stderr, " %d builtin types\n", NumBuiltin);
114 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar47677342008-09-26 03:23:00 +0000115 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000116 fprintf(stderr, " %d reference types\n", NumReference);
117 fprintf(stderr, " %d complex types\n", NumComplex);
118 fprintf(stderr, " %d array types\n", NumArray);
119 fprintf(stderr, " %d vector types\n", NumVector);
120 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
121 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
122 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
123 fprintf(stderr, " %d tagged types\n", NumTagged);
124 fprintf(stderr, " %d struct types\n", NumTagStruct);
125 fprintf(stderr, " %d union types\n", NumTagUnion);
126 fprintf(stderr, " %d class types\n", NumTagClass);
127 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000128 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000129 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000130 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000131 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000132 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000133 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
134 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
135
Chris Lattner4b009652007-07-25 00:24:17 +0000136 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
137 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
138 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
139 NumFunctionP*sizeof(FunctionTypeProto)+
140 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroffe0430632008-05-21 15:59:22 +0000141 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
142 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Chris Lattner4b009652007-07-25 00:24:17 +0000143}
144
145
146void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
147 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
148}
149
Chris Lattner4b009652007-07-25 00:24:17 +0000150void ASTContext::InitBuiltinTypes() {
151 assert(VoidTy.isNull() && "Context reinitialized?");
152
153 // C99 6.2.5p19.
154 InitBuiltinType(VoidTy, BuiltinType::Void);
155
156 // C99 6.2.5p2.
157 InitBuiltinType(BoolTy, BuiltinType::Bool);
158 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000159 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000160 InitBuiltinType(CharTy, BuiltinType::Char_S);
161 else
162 InitBuiltinType(CharTy, BuiltinType::Char_U);
163 // C99 6.2.5p4.
164 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
165 InitBuiltinType(ShortTy, BuiltinType::Short);
166 InitBuiltinType(IntTy, BuiltinType::Int);
167 InitBuiltinType(LongTy, BuiltinType::Long);
168 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
169
170 // C99 6.2.5p6.
171 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
172 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
173 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
174 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
175 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
176
177 // C99 6.2.5p10.
178 InitBuiltinType(FloatTy, BuiltinType::Float);
179 InitBuiltinType(DoubleTy, BuiltinType::Double);
180 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000181
182 // C++ 3.9.1p5
183 InitBuiltinType(WCharTy, BuiltinType::WChar);
184
Douglas Gregord2baafd2008-10-21 16:13:35 +0000185 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000186 InitBuiltinType(OverloadTy, BuiltinType::Overload);
187
188 // Placeholder type for type-dependent expressions whose type is
189 // completely unknown. No code should ever check a type against
190 // DependentTy and users should never see it; however, it is here to
191 // help diagnose failures to properly check for type-dependent
192 // expressions.
193 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000194
Chris Lattner4b009652007-07-25 00:24:17 +0000195 // C99 6.2.5p11.
196 FloatComplexTy = getComplexType(FloatTy);
197 DoubleComplexTy = getComplexType(DoubleTy);
198 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000199
Steve Naroff9d12c902007-10-15 14:41:52 +0000200 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000201 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000202 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000203 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000204 ClassStructType = 0;
205
Ted Kremenek42730c52008-01-07 19:49:32 +0000206 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000207
208 // void * type
209 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000210}
211
212//===----------------------------------------------------------------------===//
213// Type Sizing and Analysis
214//===----------------------------------------------------------------------===//
215
Chris Lattner2a674dc2008-06-30 18:32:54 +0000216/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
217/// scalar floating point type.
218const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
219 const BuiltinType *BT = T->getAsBuiltinType();
220 assert(BT && "Not a floating point type!");
221 switch (BT->getKind()) {
222 default: assert(0 && "Not a floating point type!");
223 case BuiltinType::Float: return Target.getFloatFormat();
224 case BuiltinType::Double: return Target.getDoubleFormat();
225 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
226 }
227}
228
229
Chris Lattner4b009652007-07-25 00:24:17 +0000230/// getTypeSize - Return the size of the specified type, in bits. This method
231/// does not work on incomplete types.
232std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000233ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000234 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000235 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000236 unsigned Align;
237 switch (T->getTypeClass()) {
238 case Type::TypeName: assert(0 && "Not a canonical type!");
239 case Type::FunctionNoProto:
240 case Type::FunctionProto:
241 default:
242 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000243 case Type::VariableArray:
244 assert(0 && "VLAs not implemented yet!");
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000245 case Type::DependentSizedArray:
246 assert(0 && "Dependently-sized arrays don't have a known size");
Steve Naroff83c13012007-08-30 01:06:46 +0000247 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000248 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000249
Chris Lattner8cd0e932008-03-05 18:54:05 +0000250 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000251 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000252 Align = EltInfo.second;
253 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000254 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000255 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000256 case Type::Vector: {
257 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000258 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000259 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000260 // FIXME: This isn't right for unusual vectors
261 Align = Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000262 break;
263 }
264
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000265 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000266 switch (cast<BuiltinType>(T)->getKind()) {
267 default: assert(0 && "Unknown builtin type!");
268 case BuiltinType::Void:
269 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000270 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000271 Width = Target.getBoolWidth();
272 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000273 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000274 case BuiltinType::Char_S:
275 case BuiltinType::Char_U:
276 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000277 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000278 Width = Target.getCharWidth();
279 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000280 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000281 case BuiltinType::WChar:
282 Width = Target.getWCharWidth();
283 Align = Target.getWCharAlign();
284 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000285 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000286 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000287 Width = Target.getShortWidth();
288 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000289 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000290 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000291 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000292 Width = Target.getIntWidth();
293 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000294 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000295 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000296 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000297 Width = Target.getLongWidth();
298 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000299 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000300 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000301 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000302 Width = Target.getLongLongWidth();
303 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000304 break;
305 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000306 Width = Target.getFloatWidth();
307 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000308 break;
309 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000310 Width = Target.getDoubleWidth();
311 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000312 break;
313 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000314 Width = Target.getLongDoubleWidth();
315 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000316 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000317 }
318 break;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000319 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000320 // FIXME: Pointers into different addr spaces could have different sizes and
321 // alignment requirements: getPointerInfo should take an AddrSpace.
322 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000323 case Type::ObjCQualifiedId:
Chris Lattner1d78a862008-04-07 07:01:58 +0000324 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000325 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000326 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000327 case Type::BlockPointer: {
328 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
329 Width = Target.getPointerWidth(AS);
330 Align = Target.getPointerAlign(AS);
331 break;
332 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000333 case Type::Pointer: {
334 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000335 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000336 Align = Target.getPointerAlign(AS);
337 break;
338 }
Chris Lattner4b009652007-07-25 00:24:17 +0000339 case Type::Reference:
340 // "When applied to a reference or a reference type, the result is the size
341 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000342 // FIXME: This is wrong for struct layout: a reference in a struct has
343 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000344 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner4b009652007-07-25 00:24:17 +0000345
346 case Type::Complex: {
347 // Complex types have the same alignment as their elements, but twice the
348 // size.
349 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000350 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000351 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000352 Align = EltInfo.second;
353 break;
354 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000355 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000356 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000357 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
358 Width = Layout.getSize();
359 Align = Layout.getAlignment();
360 break;
361 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000362 case Type::Tagged: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000363 const TagType *TT = cast<TagType>(T);
364
365 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000366 Width = 1;
367 Align = 1;
368 break;
369 }
370
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000371 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000372 return getTypeInfo(ET->getDecl()->getIntegerType());
373
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000374 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000375 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
376 Width = Layout.getSize();
377 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000378 break;
379 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000380 }
Chris Lattner4b009652007-07-25 00:24:17 +0000381
382 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000383 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000384}
385
Devang Patelbfe323c2008-06-04 21:22:16 +0000386/// LayoutField - Field layout.
387void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000388 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000389 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000390 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000391 uint64_t FieldOffset = IsUnion ? 0 : Size;
392 uint64_t FieldSize;
393 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000394
395 // FIXME: Should this override struct packing? Probably we want to
396 // take the minimum?
397 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
398 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000399
400 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
401 // TODO: Need to check this algorithm on other targets!
402 // (tested on Linux-X86)
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +0000403 FieldSize =
404 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000405
406 std::pair<uint64_t, unsigned> FieldInfo =
407 Context.getTypeInfo(FD->getType());
408 uint64_t TypeSize = FieldInfo.first;
409
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000410 // Determine the alignment of this bitfield. The packing
411 // attributes define a maximum and the alignment attribute defines
412 // a minimum.
413 // FIXME: What is the right behavior when the specified alignment
414 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000415 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000416 if (FieldPacking)
417 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000418 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
419 FieldAlign = std::max(FieldAlign, AA->getAlignment());
420
421 // Check if we need to add padding to give the field the correct
422 // alignment.
423 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
424 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
425
426 // Padding members don't affect overall alignment
427 if (!FD->getIdentifier())
428 FieldAlign = 1;
429 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000430 if (FD->getType()->isIncompleteArrayType()) {
431 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000432 // query getTypeInfo about these, so we figure it out here.
433 // Flexible array members don't have any size, but they
434 // have to be aligned appropriately for their element type.
435 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000436 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000437 FieldAlign = Context.getTypeAlign(ATy->getElementType());
438 } else {
439 std::pair<uint64_t, unsigned> FieldInfo =
440 Context.getTypeInfo(FD->getType());
441 FieldSize = FieldInfo.first;
442 FieldAlign = FieldInfo.second;
443 }
444
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000445 // Determine the alignment of this bitfield. The packing
446 // attributes define a maximum and the alignment attribute defines
447 // a minimum. Additionally, the packing alignment must be at least
448 // a byte for non-bitfields.
449 //
450 // FIXME: What is the right behavior when the specified alignment
451 // is smaller than the specified packing?
452 if (FieldPacking)
453 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000454 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
455 FieldAlign = std::max(FieldAlign, AA->getAlignment());
456
457 // Round up the current record size to the field's alignment boundary.
458 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
459 }
460
461 // Place this field at the current location.
462 FieldOffsets[FieldNo] = FieldOffset;
463
464 // Reserve space for this field.
465 if (IsUnion) {
466 Size = std::max(Size, FieldSize);
467 } else {
468 Size = FieldOffset + FieldSize;
469 }
470
471 // Remember max struct/class alignment.
472 Alignment = std::max(Alignment, FieldAlign);
473}
474
Devang Patel4b6bf702008-06-04 21:54:36 +0000475
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000476/// getASTObjcInterfaceLayout - Get or compute information about the layout of
477/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000478/// position information.
479const ASTRecordLayout &
480ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
481 // Look up this layout, if already laid out, return what we have.
482 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
483 if (Entry) return *Entry;
484
485 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
486 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000487 ASTRecordLayout *NewEntry = NULL;
488 unsigned FieldCount = D->ivar_size();
489 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
490 FieldCount++;
491 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
492 unsigned Alignment = SL.getAlignment();
493 uint64_t Size = SL.getSize();
494 NewEntry = new ASTRecordLayout(Size, Alignment);
495 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000496 // Super class is at the beginning of the layout.
497 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000498 } else {
499 NewEntry = new ASTRecordLayout();
500 NewEntry->InitializeLayout(FieldCount);
501 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000502 Entry = NewEntry;
503
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000504 unsigned StructPacking = 0;
505 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
506 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000507
508 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
509 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
510 AA->getAlignment()));
511
512 // Layout each ivar sequentially.
513 unsigned i = 0;
514 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
515 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
516 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000517 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000518 }
519
520 // Finally, round the size of the total struct up to the alignment of the
521 // struct itself.
522 NewEntry->FinalizeLayout();
523 return *NewEntry;
524}
525
Devang Patel7a78e432007-11-01 19:11:01 +0000526/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000527/// specified record (struct/union/class), which indicates its size and field
528/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000529const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000530 D = D->getDefinition(*this);
531 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000532
Chris Lattner4b009652007-07-25 00:24:17 +0000533 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000534 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000535 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000536
Devang Patel7a78e432007-11-01 19:11:01 +0000537 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
538 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
539 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000540 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000541
Douglas Gregor39677622008-12-11 20:41:00 +0000542 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000543 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000544 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000545
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000546 unsigned StructPacking = 0;
547 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
548 StructPacking = PA->getAlignment();
549
Eli Friedman5949a022008-05-30 09:31:38 +0000550 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000551 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
552 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000553
Eli Friedman5949a022008-05-30 09:31:38 +0000554 // Layout each field, for now, just sequentially, respecting alignment. In
555 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000556 unsigned FieldIdx = 0;
Douglas Gregor640a04b2008-12-11 17:59:21 +0000557 for (RecordDecl::field_const_iterator Field = D->field_begin(),
558 FieldEnd = D->field_end();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000559 Field != FieldEnd; (void)++Field, ++FieldIdx)
560 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000561
562 // Finally, round the size of the total struct up to the alignment of the
563 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000564 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000565 return *NewEntry;
566}
567
Chris Lattner4b009652007-07-25 00:24:17 +0000568//===----------------------------------------------------------------------===//
569// Type creation/memoization methods
570//===----------------------------------------------------------------------===//
571
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000572QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000573 QualType CanT = getCanonicalType(T);
574 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000575 return T;
576
577 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
578 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000579 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000580 "Type is already address space qualified");
581
582 // Check if we've already instantiated an address space qual'd type of this
583 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000584 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000585 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000586 void *InsertPos = 0;
587 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
588 return QualType(ASQy, 0);
589
590 // If the base type isn't canonical, this won't be a canonical type either,
591 // so fill in the canonical type field.
592 QualType Canonical;
593 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000594 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000595
596 // Get the new insert position for the node we care about.
597 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000598 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000599 }
Chris Lattner35fef522008-02-20 20:55:12 +0000600 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000601 ASQualTypes.InsertNode(New, InsertPos);
602 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000603 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000604}
605
Chris Lattner4b009652007-07-25 00:24:17 +0000606
607/// getComplexType - Return the uniqued reference to the type for a complex
608/// number with the specified element type.
609QualType ASTContext::getComplexType(QualType T) {
610 // Unique pointers, to guarantee there is only one pointer of a particular
611 // structure.
612 llvm::FoldingSetNodeID ID;
613 ComplexType::Profile(ID, T);
614
615 void *InsertPos = 0;
616 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
617 return QualType(CT, 0);
618
619 // If the pointee type isn't canonical, this won't be a canonical type either,
620 // so fill in the canonical type field.
621 QualType Canonical;
622 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000623 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000624
625 // Get the new insert position for the node we care about.
626 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000627 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000628 }
629 ComplexType *New = new ComplexType(T, Canonical);
630 Types.push_back(New);
631 ComplexTypes.InsertNode(New, InsertPos);
632 return QualType(New, 0);
633}
634
635
636/// getPointerType - Return the uniqued reference to the type for a pointer to
637/// the specified type.
638QualType ASTContext::getPointerType(QualType T) {
639 // Unique pointers, to guarantee there is only one pointer of a particular
640 // structure.
641 llvm::FoldingSetNodeID ID;
642 PointerType::Profile(ID, T);
643
644 void *InsertPos = 0;
645 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
646 return QualType(PT, 0);
647
648 // If the pointee type isn't canonical, this won't be a canonical type either,
649 // so fill in the canonical type field.
650 QualType Canonical;
651 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000652 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000653
654 // Get the new insert position for the node we care about.
655 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000656 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000657 }
658 PointerType *New = new PointerType(T, Canonical);
659 Types.push_back(New);
660 PointerTypes.InsertNode(New, InsertPos);
661 return QualType(New, 0);
662}
663
Steve Naroff7aa54752008-08-27 16:04:49 +0000664/// getBlockPointerType - Return the uniqued reference to the type for
665/// a pointer to the specified block.
666QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000667 assert(T->isFunctionType() && "block of function types only");
668 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000669 // structure.
670 llvm::FoldingSetNodeID ID;
671 BlockPointerType::Profile(ID, T);
672
673 void *InsertPos = 0;
674 if (BlockPointerType *PT =
675 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
676 return QualType(PT, 0);
677
Steve Narofffd5b19d2008-08-28 19:20:44 +0000678 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000679 // type either so fill in the canonical type field.
680 QualType Canonical;
681 if (!T->isCanonical()) {
682 Canonical = getBlockPointerType(getCanonicalType(T));
683
684 // Get the new insert position for the node we care about.
685 BlockPointerType *NewIP =
686 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000687 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000688 }
689 BlockPointerType *New = new BlockPointerType(T, Canonical);
690 Types.push_back(New);
691 BlockPointerTypes.InsertNode(New, InsertPos);
692 return QualType(New, 0);
693}
694
Chris Lattner4b009652007-07-25 00:24:17 +0000695/// getReferenceType - Return the uniqued reference to the type for a reference
696/// to the specified type.
697QualType ASTContext::getReferenceType(QualType T) {
698 // Unique pointers, to guarantee there is only one pointer of a particular
699 // structure.
700 llvm::FoldingSetNodeID ID;
701 ReferenceType::Profile(ID, T);
702
703 void *InsertPos = 0;
704 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
705 return QualType(RT, 0);
706
707 // If the referencee type isn't canonical, this won't be a canonical type
708 // either, so fill in the canonical type field.
709 QualType Canonical;
710 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000711 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000712
713 // Get the new insert position for the node we care about.
714 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000715 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000716 }
717
718 ReferenceType *New = new ReferenceType(T, Canonical);
719 Types.push_back(New);
720 ReferenceTypes.InsertNode(New, InsertPos);
721 return QualType(New, 0);
722}
723
Steve Naroff83c13012007-08-30 01:06:46 +0000724/// getConstantArrayType - Return the unique reference to the type for an
725/// array of the specified element type.
726QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000727 const llvm::APInt &ArySize,
728 ArrayType::ArraySizeModifier ASM,
729 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000730 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000731 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000732
733 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000734 if (ConstantArrayType *ATP =
735 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000736 return QualType(ATP, 0);
737
738 // If the element type isn't canonical, this won't be a canonical type either,
739 // so fill in the canonical type field.
740 QualType Canonical;
741 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000742 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000743 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000744 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000745 ConstantArrayType *NewIP =
746 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000747 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000748 }
749
Steve Naroff24c9b982007-08-30 18:10:14 +0000750 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
751 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000752 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000753 Types.push_back(New);
754 return QualType(New, 0);
755}
756
Steve Naroffe2579e32007-08-30 18:14:25 +0000757/// getVariableArrayType - Returns a non-unique reference to the type for a
758/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000759QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
760 ArrayType::ArraySizeModifier ASM,
761 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000762 // Since we don't unique expressions, it isn't possible to unique VLA's
763 // that have an expression provided for their size.
764
765 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
766 ASM, EltTypeQuals);
767
768 VariableArrayTypes.push_back(New);
769 Types.push_back(New);
770 return QualType(New, 0);
771}
772
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000773/// getDependentSizedArrayType - Returns a non-unique reference to
774/// the type for a dependently-sized array of the specified element
775/// type. FIXME: We will need these to be uniqued, or at least
776/// comparable, at some point.
777QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
778 ArrayType::ArraySizeModifier ASM,
779 unsigned EltTypeQuals) {
780 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
781 "Size must be type- or value-dependent!");
782
783 // Since we don't unique expressions, it isn't possible to unique
784 // dependently-sized array types.
785
786 DependentSizedArrayType *New
787 = new DependentSizedArrayType(EltTy, QualType(), NumElts,
788 ASM, EltTypeQuals);
789
790 DependentSizedArrayTypes.push_back(New);
791 Types.push_back(New);
792 return QualType(New, 0);
793}
794
Eli Friedman8ff07782008-02-15 18:16:39 +0000795QualType ASTContext::getIncompleteArrayType(QualType EltTy,
796 ArrayType::ArraySizeModifier ASM,
797 unsigned EltTypeQuals) {
798 llvm::FoldingSetNodeID ID;
799 IncompleteArrayType::Profile(ID, EltTy);
800
801 void *InsertPos = 0;
802 if (IncompleteArrayType *ATP =
803 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
804 return QualType(ATP, 0);
805
806 // If the element type isn't canonical, this won't be a canonical type
807 // either, so fill in the canonical type field.
808 QualType Canonical;
809
810 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000811 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000812 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000813
814 // Get the new insert position for the node we care about.
815 IncompleteArrayType *NewIP =
816 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000817 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000818 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000819
820 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
821 ASM, EltTypeQuals);
822
823 IncompleteArrayTypes.InsertNode(New, InsertPos);
824 Types.push_back(New);
825 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000826}
827
Chris Lattner4b009652007-07-25 00:24:17 +0000828/// getVectorType - Return the unique reference to a vector type of
829/// the specified element type and size. VectorType must be a built-in type.
830QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
831 BuiltinType *baseType;
832
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000833 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000834 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
835
836 // Check if we've already instantiated a vector of this type.
837 llvm::FoldingSetNodeID ID;
838 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
839 void *InsertPos = 0;
840 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
841 return QualType(VTP, 0);
842
843 // If the element type isn't canonical, this won't be a canonical type either,
844 // so fill in the canonical type field.
845 QualType Canonical;
846 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000847 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000848
849 // Get the new insert position for the node we care about.
850 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000851 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000852 }
853 VectorType *New = new VectorType(vecType, NumElts, Canonical);
854 VectorTypes.InsertNode(New, InsertPos);
855 Types.push_back(New);
856 return QualType(New, 0);
857}
858
Nate Begemanaf6ed502008-04-18 23:10:10 +0000859/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +0000860/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000861QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +0000862 BuiltinType *baseType;
863
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000864 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +0000865 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +0000866
867 // Check if we've already instantiated a vector of this type.
868 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000869 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +0000870 void *InsertPos = 0;
871 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
872 return QualType(VTP, 0);
873
874 // If the element type isn't canonical, this won't be a canonical type either,
875 // so fill in the canonical type field.
876 QualType Canonical;
877 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000878 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000879
880 // Get the new insert position for the node we care about.
881 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000882 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000883 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000884 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000885 VectorTypes.InsertNode(New, InsertPos);
886 Types.push_back(New);
887 return QualType(New, 0);
888}
889
890/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
891///
892QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
893 // Unique functions, to guarantee there is only one function of a particular
894 // structure.
895 llvm::FoldingSetNodeID ID;
896 FunctionTypeNoProto::Profile(ID, ResultTy);
897
898 void *InsertPos = 0;
899 if (FunctionTypeNoProto *FT =
900 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
901 return QualType(FT, 0);
902
903 QualType Canonical;
904 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000905 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000906
907 // Get the new insert position for the node we care about.
908 FunctionTypeNoProto *NewIP =
909 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000910 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000911 }
912
913 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
914 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000915 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000916 return QualType(New, 0);
917}
918
919/// getFunctionType - Return a normal function type with a typed argument
920/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000921QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000922 unsigned NumArgs, bool isVariadic,
923 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000924 // Unique functions, to guarantee there is only one function of a particular
925 // structure.
926 llvm::FoldingSetNodeID ID;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000927 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
928 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000929
930 void *InsertPos = 0;
931 if (FunctionTypeProto *FTP =
932 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
933 return QualType(FTP, 0);
934
935 // Determine whether the type being created is already canonical or not.
936 bool isCanonical = ResultTy->isCanonical();
937 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
938 if (!ArgArray[i]->isCanonical())
939 isCanonical = false;
940
941 // If this type isn't canonical, get the canonical version of it.
942 QualType Canonical;
943 if (!isCanonical) {
944 llvm::SmallVector<QualType, 16> CanonicalArgs;
945 CanonicalArgs.reserve(NumArgs);
946 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000947 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +0000948
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000949 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +0000950 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +0000951 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000952
953 // Get the new insert position for the node we care about.
954 FunctionTypeProto *NewIP =
955 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000956 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000957 }
958
959 // FunctionTypeProto objects are not allocated with new because they have a
960 // variable size array (for parameter types) at the end of them.
961 FunctionTypeProto *FTP =
962 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
963 NumArgs*sizeof(QualType));
964 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000965 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000966 Types.push_back(FTP);
967 FunctionTypeProtos.InsertNode(FTP, InsertPos);
968 return QualType(FTP, 0);
969}
970
Douglas Gregor1d661552008-04-13 21:07:44 +0000971/// getTypeDeclType - Return the unique reference to the type for the
972/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +0000973QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000974 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +0000975 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
976
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000977 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +0000978 return getTypedefType(Typedef);
Douglas Gregordd861062008-12-05 18:15:24 +0000979 else if (TemplateTypeParmDecl *TP = dyn_cast<TemplateTypeParmDecl>(Decl))
980 return getTemplateTypeParmType(TP);
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000981 else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +0000982 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000983
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000984 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000985 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
986 : new CXXRecordType(CXXRecord);
987 }
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000988 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000989 Decl->TypeForDecl = PrevDecl ? PrevDecl->TypeForDecl
990 : new RecordType(Record);
991 }
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +0000992 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +0000993 Decl->TypeForDecl = new EnumType(Enum);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000994 else
Douglas Gregor1d661552008-04-13 21:07:44 +0000995 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000996
Ted Kremenek46a837c2008-09-05 17:16:31 +0000997 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000998 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +0000999}
1000
Douglas Gregor8acb7272008-12-11 16:49:14 +00001001/// setTagDefinition - Used by RecordDecl::completeDefinition and
1002/// EnumDecl::completeDefinition to inform about which
1003/// RecordDecl/EnumDecl serves as the definition of a particular
1004/// struct/union/class/enum.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001005void ASTContext::setTagDefinition(TagDecl* D) {
1006 assert (D->isDefinition());
Douglas Gregor8acb7272008-12-11 16:49:14 +00001007 if (!D->TypeForDecl)
1008 getTypeDeclType(D);
1009 else
1010 cast<TagType>(D->TypeForDecl)->decl = D;
Ted Kremenek46a837c2008-09-05 17:16:31 +00001011}
1012
Chris Lattner4b009652007-07-25 00:24:17 +00001013/// getTypedefType - Return the unique reference to the type for the
1014/// specified typename decl.
1015QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1016 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1017
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001018 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001019 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001020 Types.push_back(Decl->TypeForDecl);
1021 return QualType(Decl->TypeForDecl, 0);
1022}
1023
Douglas Gregordd861062008-12-05 18:15:24 +00001024/// getTemplateTypeParmType - Return the unique reference to the type
1025/// for the specified template type parameter declaration.
1026QualType ASTContext::getTemplateTypeParmType(TemplateTypeParmDecl *Decl) {
1027 if (!Decl->TypeForDecl) {
1028 Decl->TypeForDecl = new TemplateTypeParmType(Decl);
1029 Types.push_back(Decl->TypeForDecl);
1030 }
1031 return QualType(Decl->TypeForDecl, 0);
1032}
1033
Ted Kremenek42730c52008-01-07 19:49:32 +00001034/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001035/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001036QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001037 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1038
Ted Kremenek42730c52008-01-07 19:49:32 +00001039 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001040 Types.push_back(Decl->TypeForDecl);
1041 return QualType(Decl->TypeForDecl, 0);
1042}
1043
Chris Lattnere1352302008-04-07 04:56:42 +00001044/// CmpProtocolNames - Comparison predicate for sorting protocols
1045/// alphabetically.
1046static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1047 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001048 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001049}
1050
1051static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1052 unsigned &NumProtocols) {
1053 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1054
1055 // Sort protocols, keyed by name.
1056 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1057
1058 // Remove duplicates.
1059 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1060 NumProtocols = ProtocolsEnd-Protocols;
1061}
1062
1063
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001064/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1065/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001066QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1067 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001068 // Sort the protocol list alphabetically to canonicalize it.
1069 SortAndUniqueProtocols(Protocols, NumProtocols);
1070
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001071 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001072 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001073
1074 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001075 if (ObjCQualifiedInterfaceType *QT =
1076 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001077 return QualType(QT, 0);
1078
1079 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001080 ObjCQualifiedInterfaceType *QType =
1081 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001082 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001083 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001084 return QualType(QType, 0);
1085}
1086
Chris Lattnere1352302008-04-07 04:56:42 +00001087/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1088/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001089QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001090 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001091 // Sort the protocol list alphabetically to canonicalize it.
1092 SortAndUniqueProtocols(Protocols, NumProtocols);
1093
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001094 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001095 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001096
1097 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001098 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001099 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001100 return QualType(QT, 0);
1101
1102 // No Match;
Chris Lattner4a68fe02008-07-26 00:46:50 +00001103 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001104 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001105 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001106 return QualType(QType, 0);
1107}
1108
Steve Naroff0604dd92007-08-01 18:02:17 +00001109/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1110/// TypeOfExpr AST's (since expression's are never shared). For example,
1111/// multiple declarations that refer to "typeof(x)" all contain different
1112/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1113/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +00001114QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001115 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff0604dd92007-08-01 18:02:17 +00001116 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
1117 Types.push_back(toe);
1118 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001119}
1120
Steve Naroff0604dd92007-08-01 18:02:17 +00001121/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1122/// TypeOfType AST's. The only motivation to unique these nodes would be
1123/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1124/// an issue. This doesn't effect the type checker, since it operates
1125/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001126QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001127 QualType Canonical = getCanonicalType(tofType);
Steve Naroff0604dd92007-08-01 18:02:17 +00001128 TypeOfType *tot = new TypeOfType(tofType, Canonical);
1129 Types.push_back(tot);
1130 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001131}
1132
Chris Lattner4b009652007-07-25 00:24:17 +00001133/// getTagDeclType - Return the unique reference to the type for the
1134/// specified TagDecl (struct/union/class/enum) decl.
1135QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001136 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001137 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001138}
1139
1140/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1141/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1142/// needs to agree with the definition in <stddef.h>.
1143QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001144 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001145}
1146
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001147/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001148/// width of characters in wide strings, The value is target dependent and
1149/// needs to agree with the definition in <stddef.h>.
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001150QualType ASTContext::getWCharType() const {
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001151 if (LangOpts.CPlusPlus)
1152 return WCharTy;
1153
Douglas Gregorc6507e42008-11-03 14:12:49 +00001154 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1155 // wide-character type?
1156 return getFromTargetType(Target.getWCharType());
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001157}
1158
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001159/// getSignedWCharType - Return the type of "signed wchar_t".
1160/// Used when in C++, as a GCC extension.
1161QualType ASTContext::getSignedWCharType() const {
1162 // FIXME: derive from "Target" ?
1163 return WCharTy;
1164}
1165
1166/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1167/// Used when in C++, as a GCC extension.
1168QualType ASTContext::getUnsignedWCharType() const {
1169 // FIXME: derive from "Target" ?
1170 return UnsignedIntTy;
1171}
1172
Chris Lattner4b009652007-07-25 00:24:17 +00001173/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1174/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1175QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001176 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001177}
1178
Chris Lattner19eb97e2008-04-02 05:18:44 +00001179//===----------------------------------------------------------------------===//
1180// Type Operators
1181//===----------------------------------------------------------------------===//
1182
Chris Lattner3dae6f42008-04-06 22:41:35 +00001183/// getCanonicalType - Return the canonical (structural) type corresponding to
1184/// the specified potentially non-canonical type. The non-canonical version
1185/// of a type may have many "decorated" versions of types. Decorators can
1186/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1187/// to be free of any of these, allowing two canonical types to be compared
1188/// for exact equality with a simple pointer comparison.
1189QualType ASTContext::getCanonicalType(QualType T) {
1190 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001191
1192 // If the result has type qualifiers, make sure to canonicalize them as well.
1193 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1194 if (TypeQuals == 0) return CanType;
1195
1196 // If the type qualifiers are on an array type, get the canonical type of the
1197 // array with the qualifiers applied to the element type.
1198 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1199 if (!AT)
1200 return CanType.getQualifiedType(TypeQuals);
1201
1202 // Get the canonical version of the element with the extra qualifiers on it.
1203 // This can recursively sink qualifiers through multiple levels of arrays.
1204 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1205 NewEltTy = getCanonicalType(NewEltTy);
1206
1207 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1208 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1209 CAT->getIndexTypeQualifier());
1210 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1211 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1212 IAT->getIndexTypeQualifier());
1213
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001214 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1215 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1216 DSAT->getSizeModifier(),
1217 DSAT->getIndexTypeQualifier());
1218
Chris Lattnera1923f62008-08-04 07:31:14 +00001219 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1220 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1221 VAT->getSizeModifier(),
1222 VAT->getIndexTypeQualifier());
1223}
1224
1225
1226const ArrayType *ASTContext::getAsArrayType(QualType T) {
1227 // Handle the non-qualified case efficiently.
1228 if (T.getCVRQualifiers() == 0) {
1229 // Handle the common positive case fast.
1230 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1231 return AT;
1232 }
1233
1234 // Handle the common negative case fast, ignoring CVR qualifiers.
1235 QualType CType = T->getCanonicalTypeInternal();
1236
1237 // Make sure to look through type qualifiers (like ASQuals) for the negative
1238 // test.
1239 if (!isa<ArrayType>(CType) &&
1240 !isa<ArrayType>(CType.getUnqualifiedType()))
1241 return 0;
1242
1243 // Apply any CVR qualifiers from the array type to the element type. This
1244 // implements C99 6.7.3p8: "If the specification of an array type includes
1245 // any type qualifiers, the element type is so qualified, not the array type."
1246
1247 // If we get here, we either have type qualifiers on the type, or we have
1248 // sugar such as a typedef in the way. If we have type qualifiers on the type
1249 // we must propagate them down into the elemeng type.
1250 unsigned CVRQuals = T.getCVRQualifiers();
1251 unsigned AddrSpace = 0;
1252 Type *Ty = T.getTypePtr();
1253
1254 // Rip through ASQualType's and typedefs to get to a concrete type.
1255 while (1) {
1256 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1257 AddrSpace = ASQT->getAddressSpace();
1258 Ty = ASQT->getBaseType();
1259 } else {
1260 T = Ty->getDesugaredType();
1261 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1262 break;
1263 CVRQuals |= T.getCVRQualifiers();
1264 Ty = T.getTypePtr();
1265 }
1266 }
1267
1268 // If we have a simple case, just return now.
1269 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1270 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1271 return ATy;
1272
1273 // Otherwise, we have an array and we have qualifiers on it. Push the
1274 // qualifiers into the array element type and return a new array type.
1275 // Get the canonical version of the element with the extra qualifiers on it.
1276 // This can recursively sink qualifiers through multiple levels of arrays.
1277 QualType NewEltTy = ATy->getElementType();
1278 if (AddrSpace)
1279 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1280 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1281
1282 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1283 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1284 CAT->getSizeModifier(),
1285 CAT->getIndexTypeQualifier()));
1286 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1287 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1288 IAT->getSizeModifier(),
1289 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001290
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001291 if (const DependentSizedArrayType *DSAT
1292 = dyn_cast<DependentSizedArrayType>(ATy))
1293 return cast<ArrayType>(
1294 getDependentSizedArrayType(NewEltTy,
1295 DSAT->getSizeExpr(),
1296 DSAT->getSizeModifier(),
1297 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001298
Chris Lattnera1923f62008-08-04 07:31:14 +00001299 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1300 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1301 VAT->getSizeModifier(),
1302 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001303}
1304
1305
Chris Lattner19eb97e2008-04-02 05:18:44 +00001306/// getArrayDecayedType - Return the properly qualified result of decaying the
1307/// specified array type to a pointer. This operation is non-trivial when
1308/// handling typedefs etc. The canonical type of "T" must be an array type,
1309/// this returns a pointer to a properly qualified element of the array.
1310///
1311/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1312QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001313 // Get the element type with 'getAsArrayType' so that we don't lose any
1314 // typedefs in the element type of the array. This also handles propagation
1315 // of type qualifiers from the array type into the element type if present
1316 // (C99 6.7.3p8).
1317 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1318 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001319
Chris Lattnera1923f62008-08-04 07:31:14 +00001320 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001321
1322 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001323 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001324}
1325
Chris Lattner4b009652007-07-25 00:24:17 +00001326/// getFloatingRank - Return a relative rank for floating point types.
1327/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001328static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001329 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001330 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001331
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001332 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001333 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001334 case BuiltinType::Float: return FloatRank;
1335 case BuiltinType::Double: return DoubleRank;
1336 case BuiltinType::LongDouble: return LongDoubleRank;
1337 }
1338}
1339
Steve Narofffa0c4532007-08-27 01:41:48 +00001340/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1341/// point or a complex type (based on typeDomain/typeSize).
1342/// 'typeDomain' is a real floating point or complex type.
1343/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001344QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1345 QualType Domain) const {
1346 FloatingRank EltRank = getFloatingRank(Size);
1347 if (Domain->isComplexType()) {
1348 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001349 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001350 case FloatRank: return FloatComplexTy;
1351 case DoubleRank: return DoubleComplexTy;
1352 case LongDoubleRank: return LongDoubleComplexTy;
1353 }
Chris Lattner4b009652007-07-25 00:24:17 +00001354 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001355
1356 assert(Domain->isRealFloatingType() && "Unknown domain!");
1357 switch (EltRank) {
1358 default: assert(0 && "getFloatingRank(): illegal value for rank");
1359 case FloatRank: return FloatTy;
1360 case DoubleRank: return DoubleTy;
1361 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001362 }
Chris Lattner4b009652007-07-25 00:24:17 +00001363}
1364
Chris Lattner51285d82008-04-06 23:55:33 +00001365/// getFloatingTypeOrder - Compare the rank of the two specified floating
1366/// point types, ignoring the domain of the type (i.e. 'double' ==
1367/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1368/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001369int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1370 FloatingRank LHSR = getFloatingRank(LHS);
1371 FloatingRank RHSR = getFloatingRank(RHS);
1372
1373 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001374 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001375 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001376 return 1;
1377 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001378}
1379
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001380/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1381/// routine will assert if passed a built-in type that isn't an integer or enum,
1382/// or if it is not canonicalized.
1383static unsigned getIntegerRank(Type *T) {
1384 assert(T->isCanonical() && "T should be canonicalized");
1385 if (isa<EnumType>(T))
1386 return 4;
1387
1388 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001389 default: assert(0 && "getIntegerRank(): not a built-in integer");
1390 case BuiltinType::Bool:
1391 return 1;
1392 case BuiltinType::Char_S:
1393 case BuiltinType::Char_U:
1394 case BuiltinType::SChar:
1395 case BuiltinType::UChar:
1396 return 2;
1397 case BuiltinType::Short:
1398 case BuiltinType::UShort:
1399 return 3;
1400 case BuiltinType::Int:
1401 case BuiltinType::UInt:
1402 return 4;
1403 case BuiltinType::Long:
1404 case BuiltinType::ULong:
1405 return 5;
1406 case BuiltinType::LongLong:
1407 case BuiltinType::ULongLong:
1408 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001409 }
1410}
1411
Chris Lattner51285d82008-04-06 23:55:33 +00001412/// getIntegerTypeOrder - Returns the highest ranked integer type:
1413/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1414/// LHS < RHS, return -1.
1415int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001416 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1417 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001418 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001419
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001420 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1421 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001422
Chris Lattner51285d82008-04-06 23:55:33 +00001423 unsigned LHSRank = getIntegerRank(LHSC);
1424 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001425
Chris Lattner51285d82008-04-06 23:55:33 +00001426 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1427 if (LHSRank == RHSRank) return 0;
1428 return LHSRank > RHSRank ? 1 : -1;
1429 }
Chris Lattner4b009652007-07-25 00:24:17 +00001430
Chris Lattner51285d82008-04-06 23:55:33 +00001431 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1432 if (LHSUnsigned) {
1433 // If the unsigned [LHS] type is larger, return it.
1434 if (LHSRank >= RHSRank)
1435 return 1;
1436
1437 // If the signed type can represent all values of the unsigned type, it
1438 // wins. Because we are dealing with 2's complement and types that are
1439 // powers of two larger than each other, this is always safe.
1440 return -1;
1441 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001442
Chris Lattner51285d82008-04-06 23:55:33 +00001443 // If the unsigned [RHS] type is larger, return it.
1444 if (RHSRank >= LHSRank)
1445 return -1;
1446
1447 // If the signed type can represent all values of the unsigned type, it
1448 // wins. Because we are dealing with 2's complement and types that are
1449 // powers of two larger than each other, this is always safe.
1450 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001451}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001452
1453// getCFConstantStringType - Return the type used for constant CFStrings.
1454QualType ASTContext::getCFConstantStringType() {
1455 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001456 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001457 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001458 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001459 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001460
1461 // const int *isa;
1462 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001463 // int flags;
1464 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001465 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001466 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001467 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001468 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001469
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001470 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001471 for (unsigned i = 0; i < 4; ++i) {
1472 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1473 SourceLocation(), 0,
1474 FieldTypes[i], /*BitWidth=*/0,
1475 /*Mutable=*/false, /*PrevDecl=*/0);
1476 CFConstantStringTypeDecl->addDecl(*this, Field, true);
1477 }
1478
1479 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001480 }
1481
1482 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001483}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001484
Anders Carlssonf58cac72008-08-30 19:34:46 +00001485QualType ASTContext::getObjCFastEnumerationStateType()
1486{
1487 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001488 ObjCFastEnumerationStateTypeDecl =
1489 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1490 &Idents.get("__objcFastEnumerationState"));
1491
Anders Carlssonf58cac72008-08-30 19:34:46 +00001492 QualType FieldTypes[] = {
1493 UnsignedLongTy,
1494 getPointerType(ObjCIdType),
1495 getPointerType(UnsignedLongTy),
1496 getConstantArrayType(UnsignedLongTy,
1497 llvm::APInt(32, 5), ArrayType::Normal, 0)
1498 };
1499
Douglas Gregor8acb7272008-12-11 16:49:14 +00001500 for (size_t i = 0; i < 4; ++i) {
1501 FieldDecl *Field = FieldDecl::Create(*this,
1502 ObjCFastEnumerationStateTypeDecl,
1503 SourceLocation(), 0,
1504 FieldTypes[i], /*BitWidth=*/0,
1505 /*Mutable=*/false, /*PrevDecl=*/0);
1506 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field, true);
1507 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00001508
Douglas Gregor8acb7272008-12-11 16:49:14 +00001509 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001510 }
1511
1512 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1513}
1514
Anders Carlssone3f02572007-10-29 06:33:42 +00001515// This returns true if a type has been typedefed to BOOL:
1516// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001517static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001518 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00001519 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1520 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001521
1522 return false;
1523}
1524
Ted Kremenek42730c52008-01-07 19:49:32 +00001525/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001526/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001527int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001528 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001529
1530 // Make all integer and enum types at least as large as an int
1531 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001532 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001533 // Treat arrays as pointers, since that's how they're passed in.
1534 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001535 sz = getTypeSize(VoidPtrTy);
1536 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001537}
1538
Ted Kremenek42730c52008-01-07 19:49:32 +00001539/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001540/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001541void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00001542 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001543 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001544 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001545 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001546 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001547 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001548 // Compute size of all parameters.
1549 // Start with computing size of a pointer in number of bytes.
1550 // FIXME: There might(should) be a better way of doing this computation!
1551 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001552 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001553 // The first two arguments (self and _cmd) are pointers; account for
1554 // their size.
1555 int ParmOffset = 2 * PtrSize;
1556 int NumOfParams = Decl->getNumParams();
1557 for (int i = 0; i < NumOfParams; i++) {
1558 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001559 int sz = getObjCEncodingTypeSize (PType);
1560 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001561 ParmOffset += sz;
1562 }
1563 S += llvm::utostr(ParmOffset);
1564 S += "@0:";
1565 S += llvm::utostr(PtrSize);
1566
1567 // Argument types.
1568 ParmOffset = 2 * PtrSize;
1569 for (int i = 0; i < NumOfParams; i++) {
1570 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001571 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001572 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001573 getObjCEncodingForTypeQualifier(
1574 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001575 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001576 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001577 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001578 }
1579}
1580
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001581/// getObjCEncodingForPropertyDecl - Return the encoded type for this
1582/// method declaration. If non-NULL, Container must be either an
1583/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1584/// NULL when getting encodings for protocol properties.
1585void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1586 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00001587 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001588 // Collect information from the property implementation decl(s).
1589 bool Dynamic = false;
1590 ObjCPropertyImplDecl *SynthesizePID = 0;
1591
1592 // FIXME: Duplicated code due to poor abstraction.
1593 if (Container) {
1594 if (const ObjCCategoryImplDecl *CID =
1595 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1596 for (ObjCCategoryImplDecl::propimpl_iterator
1597 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1598 ObjCPropertyImplDecl *PID = *i;
1599 if (PID->getPropertyDecl() == PD) {
1600 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1601 Dynamic = true;
1602 } else {
1603 SynthesizePID = PID;
1604 }
1605 }
1606 }
1607 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001608 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001609 for (ObjCCategoryImplDecl::propimpl_iterator
1610 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1611 ObjCPropertyImplDecl *PID = *i;
1612 if (PID->getPropertyDecl() == PD) {
1613 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1614 Dynamic = true;
1615 } else {
1616 SynthesizePID = PID;
1617 }
1618 }
1619 }
1620 }
1621 }
1622
1623 // FIXME: This is not very efficient.
1624 S = "T";
1625
1626 // Encode result type.
1627 // FIXME: GCC uses a generating_property_type_encoding mode during
1628 // this part. Investigate.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001629 getObjCEncodingForType(PD->getType(), S);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001630
1631 if (PD->isReadOnly()) {
1632 S += ",R";
1633 } else {
1634 switch (PD->getSetterKind()) {
1635 case ObjCPropertyDecl::Assign: break;
1636 case ObjCPropertyDecl::Copy: S += ",C"; break;
1637 case ObjCPropertyDecl::Retain: S += ",&"; break;
1638 }
1639 }
1640
1641 // It really isn't clear at all what this means, since properties
1642 // are "dynamic by default".
1643 if (Dynamic)
1644 S += ",D";
1645
1646 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1647 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001648 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001649 }
1650
1651 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1652 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001653 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001654 }
1655
1656 if (SynthesizePID) {
1657 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1658 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00001659 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001660 }
1661
1662 // FIXME: OBJCGC: weak & strong
1663}
1664
Fariborz Jahanian248db262008-01-22 22:44:46 +00001665void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00001666 bool NameFields) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001667 // We follow the behavior of gcc, expanding structures which are
1668 // directly pointed to, and expanding embedded structures. Note that
1669 // these rules are sufficient to prevent recursive encoding of the
1670 // same type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001671 getObjCEncodingForTypeImpl(T, S, true, true, NameFields);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001672}
1673
1674void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
1675 bool ExpandPointedToStructures,
1676 bool ExpandStructures,
Daniel Dunbaraa913102008-10-17 16:17:37 +00001677 bool NameFields) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00001678 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001679 char encoding;
1680 switch (BT->getKind()) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001681 default: assert(0 && "Unhandled builtin type kind");
1682 case BuiltinType::Void: encoding = 'v'; break;
1683 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001684 case BuiltinType::Char_U:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001685 case BuiltinType::UChar: encoding = 'C'; break;
1686 case BuiltinType::UShort: encoding = 'S'; break;
1687 case BuiltinType::UInt: encoding = 'I'; break;
1688 case BuiltinType::ULong: encoding = 'L'; break;
1689 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001690 case BuiltinType::Char_S:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001691 case BuiltinType::SChar: encoding = 'c'; break;
1692 case BuiltinType::Short: encoding = 's'; break;
1693 case BuiltinType::Int: encoding = 'i'; break;
1694 case BuiltinType::Long: encoding = 'l'; break;
1695 case BuiltinType::LongLong: encoding = 'q'; break;
1696 case BuiltinType::Float: encoding = 'f'; break;
1697 case BuiltinType::Double: encoding = 'd'; break;
1698 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001699 }
1700
1701 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001702 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001703 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001704 // Treat id<P...> same as 'id' for encoding purposes.
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001705 return getObjCEncodingForTypeImpl(getObjCIdType(), S,
1706 ExpandPointedToStructures,
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001707 ExpandStructures, NameFields);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001708 }
1709 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001710 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001711 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001712 S += '@';
1713 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001714 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001715 S += '#';
1716 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001717 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001718 S += ':';
1719 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001720 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001721
1722 if (PointeeTy->isCharType()) {
1723 // char pointer types should be encoded as '*' unless it is a
1724 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001725 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001726 S += '*';
1727 return;
1728 }
1729 }
1730
1731 S += '^';
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001732 getObjCEncodingForTypeImpl(PT->getPointeeType(), S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00001733 false, ExpandPointedToStructures,
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001734 NameFields);
Chris Lattnera1923f62008-08-04 07:31:14 +00001735 } else if (const ArrayType *AT =
1736 // Ignore type qualifiers etc.
1737 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001738 S += '[';
1739
1740 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1741 S += llvm::utostr(CAT->getSize().getZExtValue());
1742 else
1743 assert(0 && "Unhandled array type!");
1744
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001745 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001746 false, ExpandStructures, NameFields);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001747 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001748 } else if (T->getAsFunctionType()) {
1749 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001750 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001751 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00001752 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00001753 // Anonymous structures print as '?'
1754 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
1755 S += II->getName();
1756 } else {
1757 S += '?';
1758 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001759 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00001760 S += '=';
Douglas Gregor8acb7272008-12-11 16:49:14 +00001761 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
1762 FieldEnd = RDecl->field_end();
1763 Field != FieldEnd; ++Field) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00001764 if (NameFields) {
1765 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00001766 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00001767 S += '"';
1768 }
1769
1770 // Special case bit-fields.
Douglas Gregor8acb7272008-12-11 16:49:14 +00001771 if (const Expr *E = Field->getBitWidth()) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00001772 // FIXME: Fix constness.
1773 ASTContext *Ctx = const_cast<ASTContext*>(this);
1774 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
1775 // FIXME: Obj-C is losing information about the type size
1776 // here. Investigate if this is a problem.
1777 S += 'b';
1778 S += llvm::utostr(N);
1779 } else {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001780 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
1781 NameFields);
Daniel Dunbaraa913102008-10-17 16:17:37 +00001782 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00001783 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001784 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00001785 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001786 } else if (T->isEnumeralType()) {
1787 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00001788 } else if (T->isBlockPointerType()) {
1789 S += '^'; // This type string is the same as general pointers.
Anders Carlsson36f07d82007-10-29 05:01:08 +00001790 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001791 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001792}
1793
Ted Kremenek42730c52008-01-07 19:49:32 +00001794void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001795 std::string& S) const {
1796 if (QT & Decl::OBJC_TQ_In)
1797 S += 'n';
1798 if (QT & Decl::OBJC_TQ_Inout)
1799 S += 'N';
1800 if (QT & Decl::OBJC_TQ_Out)
1801 S += 'o';
1802 if (QT & Decl::OBJC_TQ_Bycopy)
1803 S += 'O';
1804 if (QT & Decl::OBJC_TQ_Byref)
1805 S += 'R';
1806 if (QT & Decl::OBJC_TQ_Oneway)
1807 S += 'V';
1808}
1809
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001810void ASTContext::setBuiltinVaListType(QualType T)
1811{
1812 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1813
1814 BuiltinVaListType = T;
1815}
1816
Ted Kremenek42730c52008-01-07 19:49:32 +00001817void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001818{
Ted Kremenek42730c52008-01-07 19:49:32 +00001819 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001820
1821 // typedef struct objc_object *id;
1822 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1823 assert(ptr && "'id' incorrectly typed");
1824 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1825 assert(rec && "'id' incorrectly typed");
1826 IdStructType = rec;
1827}
1828
Ted Kremenek42730c52008-01-07 19:49:32 +00001829void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001830{
Ted Kremenek42730c52008-01-07 19:49:32 +00001831 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001832
1833 // typedef struct objc_selector *SEL;
1834 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1835 assert(ptr && "'SEL' incorrectly typed");
1836 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1837 assert(rec && "'SEL' incorrectly typed");
1838 SelStructType = rec;
1839}
1840
Ted Kremenek42730c52008-01-07 19:49:32 +00001841void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001842{
Ted Kremenek42730c52008-01-07 19:49:32 +00001843 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001844}
1845
Ted Kremenek42730c52008-01-07 19:49:32 +00001846void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001847{
Ted Kremenek42730c52008-01-07 19:49:32 +00001848 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001849
1850 // typedef struct objc_class *Class;
1851 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1852 assert(ptr && "'Class' incorrectly typed");
1853 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1854 assert(rec && "'Class' incorrectly typed");
1855 ClassStructType = rec;
1856}
1857
Ted Kremenek42730c52008-01-07 19:49:32 +00001858void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1859 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001860 "'NSConstantString' type already set!");
1861
Ted Kremenek42730c52008-01-07 19:49:32 +00001862 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001863}
1864
Douglas Gregorc6507e42008-11-03 14:12:49 +00001865/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00001866/// TargetInfo, produce the corresponding type. The unsigned @p Type
1867/// is actually a value of type @c TargetInfo::IntType.
1868QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001869 switch (Type) {
1870 case TargetInfo::NoInt: return QualType();
1871 case TargetInfo::SignedShort: return ShortTy;
1872 case TargetInfo::UnsignedShort: return UnsignedShortTy;
1873 case TargetInfo::SignedInt: return IntTy;
1874 case TargetInfo::UnsignedInt: return UnsignedIntTy;
1875 case TargetInfo::SignedLong: return LongTy;
1876 case TargetInfo::UnsignedLong: return UnsignedLongTy;
1877 case TargetInfo::SignedLongLong: return LongLongTy;
1878 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
1879 }
1880
1881 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00001882 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00001883}
Ted Kremenek118930e2008-07-24 23:58:27 +00001884
1885//===----------------------------------------------------------------------===//
1886// Type Predicates.
1887//===----------------------------------------------------------------------===//
1888
1889/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1890/// to an object type. This includes "id" and "Class" (two 'special' pointers
1891/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1892/// ID type).
1893bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1894 if (Ty->isObjCQualifiedIdType())
1895 return true;
1896
Steve Naroffd9e00802008-10-21 18:24:04 +00001897 // Blocks are objects.
1898 if (Ty->isBlockPointerType())
1899 return true;
1900
1901 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00001902 if (!Ty->isPointerType())
1903 return false;
1904
1905 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1906 // pointer types. This looks for the typedef specifically, not for the
1907 // underlying type.
1908 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1909 return true;
1910
1911 // If this a pointer to an interface (e.g. NSString*), it is ok.
1912 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1913}
1914
Chris Lattner6ff358b2008-04-07 06:51:04 +00001915//===----------------------------------------------------------------------===//
1916// Type Compatibility Testing
1917//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00001918
Steve Naroff3454b6c2008-09-04 15:10:53 +00001919/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00001920/// block types. Types must be strictly compatible here. For example,
1921/// C unfortunately doesn't produce an error for the following:
1922///
1923/// int (*emptyArgFunc)();
1924/// int (*intArgList)(int) = emptyArgFunc;
1925///
1926/// For blocks, we will produce an error for the following (similar to C++):
1927///
1928/// int (^emptyArgBlock)();
1929/// int (^intArgBlock)(int) = emptyArgBlock;
1930///
1931/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
1932///
Steve Naroff3454b6c2008-09-04 15:10:53 +00001933bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00001934 const FunctionType *lbase = lhs->getAsFunctionType();
1935 const FunctionType *rbase = rhs->getAsFunctionType();
1936 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1937 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1938 if (lproto && rproto)
1939 return !mergeTypes(lhs, rhs).isNull();
1940 return false;
Steve Naroff3454b6c2008-09-04 15:10:53 +00001941}
1942
Chris Lattner6ff358b2008-04-07 06:51:04 +00001943/// areCompatVectorTypes - Return true if the two specified vector types are
1944/// compatible.
1945static bool areCompatVectorTypes(const VectorType *LHS,
1946 const VectorType *RHS) {
1947 assert(LHS->isCanonical() && RHS->isCanonical());
1948 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001949 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00001950}
1951
Eli Friedman0d9549b2008-08-22 00:56:42 +00001952/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00001953/// compatible for assignment from RHS to LHS. This handles validation of any
1954/// protocol qualifiers on the LHS or RHS.
1955///
Eli Friedman0d9549b2008-08-22 00:56:42 +00001956bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
1957 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00001958 // Verify that the base decls are compatible: the RHS must be a subclass of
1959 // the LHS.
1960 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1961 return false;
1962
1963 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1964 // protocol qualified at all, then we are good.
1965 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1966 return true;
1967
1968 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1969 // isn't a superset.
1970 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1971 return true; // FIXME: should return false!
1972
1973 // Finally, we must have two protocol-qualified interfaces.
1974 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1975 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1976 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1977 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1978 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1979 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1980
1981 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1982 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1983 // LHS in a single parallel scan until we run out of elements in LHS.
1984 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1985 ObjCProtocolDecl *LHSProto = *LHSPI;
1986
1987 while (RHSPI != RHSPE) {
1988 ObjCProtocolDecl *RHSProto = *RHSPI++;
1989 // If the RHS has a protocol that the LHS doesn't, ignore it.
1990 if (RHSProto != LHSProto)
1991 continue;
1992
1993 // Otherwise, the RHS does have this element.
1994 ++LHSPI;
1995 if (LHSPI == LHSPE)
1996 return true; // All protocols in LHS exist in RHS.
1997
1998 LHSProto = *LHSPI;
1999 }
2000
2001 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2002 return false;
2003}
2004
Steve Naroff85f0dc52007-10-15 20:41:53 +00002005/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2006/// both shall have the identically qualified version of a compatible type.
2007/// C99 6.2.7p1: Two types have compatible types if their types are the
2008/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002009bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2010 return !mergeTypes(LHS, RHS).isNull();
2011}
2012
2013QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2014 const FunctionType *lbase = lhs->getAsFunctionType();
2015 const FunctionType *rbase = rhs->getAsFunctionType();
2016 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2017 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2018 bool allLTypes = true;
2019 bool allRTypes = true;
2020
2021 // Check return type
2022 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2023 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002024 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2025 allLTypes = false;
2026 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2027 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002028
2029 if (lproto && rproto) { // two C99 style function prototypes
2030 unsigned lproto_nargs = lproto->getNumArgs();
2031 unsigned rproto_nargs = rproto->getNumArgs();
2032
2033 // Compatible functions must have the same number of arguments
2034 if (lproto_nargs != rproto_nargs)
2035 return QualType();
2036
2037 // Variadic and non-variadic functions aren't compatible
2038 if (lproto->isVariadic() != rproto->isVariadic())
2039 return QualType();
2040
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002041 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2042 return QualType();
2043
Eli Friedman0d9549b2008-08-22 00:56:42 +00002044 // Check argument compatibility
2045 llvm::SmallVector<QualType, 10> types;
2046 for (unsigned i = 0; i < lproto_nargs; i++) {
2047 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2048 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2049 QualType argtype = mergeTypes(largtype, rargtype);
2050 if (argtype.isNull()) return QualType();
2051 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002052 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2053 allLTypes = false;
2054 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2055 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002056 }
2057 if (allLTypes) return lhs;
2058 if (allRTypes) return rhs;
2059 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002060 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002061 }
2062
2063 if (lproto) allRTypes = false;
2064 if (rproto) allLTypes = false;
2065
2066 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2067 if (proto) {
2068 if (proto->isVariadic()) return QualType();
2069 // Check that the types are compatible with the types that
2070 // would result from default argument promotions (C99 6.7.5.3p15).
2071 // The only types actually affected are promotable integer
2072 // types and floats, which would be passed as a different
2073 // type depending on whether the prototype is visible.
2074 unsigned proto_nargs = proto->getNumArgs();
2075 for (unsigned i = 0; i < proto_nargs; ++i) {
2076 QualType argTy = proto->getArgType(i);
2077 if (argTy->isPromotableIntegerType() ||
2078 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2079 return QualType();
2080 }
2081
2082 if (allLTypes) return lhs;
2083 if (allRTypes) return rhs;
2084 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002085 proto->getNumArgs(), lproto->isVariadic(),
2086 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002087 }
2088
2089 if (allLTypes) return lhs;
2090 if (allRTypes) return rhs;
2091 return getFunctionTypeNoProto(retType);
2092}
2093
2094QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002095 // C++ [expr]: If an expression initially has the type "reference to T", the
2096 // type is adjusted to "T" prior to any further analysis, the expression
2097 // designates the object or function denoted by the reference, and the
2098 // expression is an lvalue.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002099 // FIXME: C++ shouldn't be going through here! The rules are different
2100 // enough that they should be handled separately.
2101 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002102 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002103 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002104 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002105
Eli Friedman0d9549b2008-08-22 00:56:42 +00002106 QualType LHSCan = getCanonicalType(LHS),
2107 RHSCan = getCanonicalType(RHS);
2108
2109 // If two types are identical, they are compatible.
2110 if (LHSCan == RHSCan)
2111 return LHS;
2112
2113 // If the qualifiers are different, the types aren't compatible
2114 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2115 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2116 return QualType();
2117
2118 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2119 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2120
Chris Lattnerc38d4522008-01-14 05:45:46 +00002121 // We want to consider the two function types to be the same for these
2122 // comparisons, just force one to the other.
2123 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2124 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002125
2126 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002127 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2128 LHSClass = Type::ConstantArray;
2129 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2130 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002131
Nate Begemanaf6ed502008-04-18 23:10:10 +00002132 // Canonicalize ExtVector -> Vector.
2133 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2134 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002135
Chris Lattner7cdcb252008-04-07 06:38:24 +00002136 // Consider qualified interfaces and interfaces the same.
2137 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2138 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002139
Chris Lattnerb5709e22008-04-07 05:43:21 +00002140 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002141 if (LHSClass != RHSClass) {
Steve Naroff28ceff72008-12-10 22:14:21 +00002142 // ID is compatible with all qualified id types.
2143 if (LHS->isObjCQualifiedIdType()) {
2144 if (const PointerType *PT = RHS->getAsPointerType()) {
2145 QualType pType = PT->getPointeeType();
2146 if (isObjCIdType(pType))
2147 return LHS;
2148 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2149 // Unfortunately, this API is part of Sema (which we don't have access
2150 // to. Need to refactor. The following check is insufficient, since we
2151 // need to make sure the class implements the protocol.
2152 if (pType->isObjCInterfaceType())
2153 return LHS;
2154 }
2155 }
2156 if (RHS->isObjCQualifiedIdType()) {
2157 if (const PointerType *PT = LHS->getAsPointerType()) {
2158 QualType pType = PT->getPointeeType();
2159 if (isObjCIdType(pType))
2160 return RHS;
2161 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2162 // Unfortunately, this API is part of Sema (which we don't have access
2163 // to. Need to refactor. The following check is insufficient, since we
2164 // need to make sure the class implements the protocol.
2165 if (pType->isObjCInterfaceType())
2166 return RHS;
2167 }
2168 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002169 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2170 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002171 if (const EnumType* ETy = LHS->getAsEnumType()) {
2172 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2173 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002174 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002175 if (const EnumType* ETy = RHS->getAsEnumType()) {
2176 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2177 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002178 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002179
Eli Friedman0d9549b2008-08-22 00:56:42 +00002180 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002181 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002182
Steve Naroffc88babe2008-01-09 22:43:08 +00002183 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002184 switch (LHSClass) {
Chris Lattnerc38d4522008-01-14 05:45:46 +00002185 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002186 {
2187 // Merge two pointer types, while trying to preserve typedef info
2188 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2189 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2190 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2191 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002192 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2193 return LHS;
2194 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2195 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002196 return getPointerType(ResultType);
2197 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002198 case Type::BlockPointer:
2199 {
2200 // Merge two block pointer types, while trying to preserve typedef info
2201 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2202 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2203 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2204 if (ResultType.isNull()) return QualType();
2205 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2206 return LHS;
2207 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2208 return RHS;
2209 return getBlockPointerType(ResultType);
2210 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002211 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002212 {
2213 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2214 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2215 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2216 return QualType();
2217
2218 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2219 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2220 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2221 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002222 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2223 return LHS;
2224 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2225 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002226 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2227 ArrayType::ArraySizeModifier(), 0);
2228 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2229 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002230 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2231 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002232 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2233 return LHS;
2234 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2235 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002236 if (LVAT) {
2237 // FIXME: This isn't correct! But tricky to implement because
2238 // the array's size has to be the size of LHS, but the type
2239 // has to be different.
2240 return LHS;
2241 }
2242 if (RVAT) {
2243 // FIXME: This isn't correct! But tricky to implement because
2244 // the array's size has to be the size of RHS, but the type
2245 // has to be different.
2246 return RHS;
2247 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002248 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2249 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002250 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002251 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002252 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002253 return mergeFunctionTypes(LHS, RHS);
2254 case Type::Tagged:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002255 // FIXME: Why are these compatible?
2256 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2257 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2258 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002259 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002260 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002261 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002262 case Type::Vector:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002263 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2264 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002265 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002266 case Type::ObjCInterface:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002267 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2268 // for checking assignment/comparison safety
2269 return QualType();
Steve Naroff28ceff72008-12-10 22:14:21 +00002270 case Type::ObjCQualifiedId:
2271 // Distinct qualified id's are not compatible.
2272 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002273 default:
2274 assert(0 && "unexpected type");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002275 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002276 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00002277}
Ted Kremenek738e6c02007-10-31 17:10:13 +00002278
Chris Lattner1d78a862008-04-07 07:01:58 +00002279//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00002280// Integer Predicates
2281//===----------------------------------------------------------------------===//
2282unsigned ASTContext::getIntWidth(QualType T) {
2283 if (T == BoolTy)
2284 return 1;
2285 // At the moment, only bool has padding bits
2286 return (unsigned)getTypeSize(T);
2287}
2288
2289QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2290 assert(T->isSignedIntegerType() && "Unexpected type");
2291 if (const EnumType* ETy = T->getAsEnumType())
2292 T = ETy->getDecl()->getIntegerType();
2293 const BuiltinType* BTy = T->getAsBuiltinType();
2294 assert (BTy && "Unexpected signed integer type");
2295 switch (BTy->getKind()) {
2296 case BuiltinType::Char_S:
2297 case BuiltinType::SChar:
2298 return UnsignedCharTy;
2299 case BuiltinType::Short:
2300 return UnsignedShortTy;
2301 case BuiltinType::Int:
2302 return UnsignedIntTy;
2303 case BuiltinType::Long:
2304 return UnsignedLongTy;
2305 case BuiltinType::LongLong:
2306 return UnsignedLongLongTy;
2307 default:
2308 assert(0 && "Unexpected signed integer type");
2309 return QualType();
2310 }
2311}
2312
2313
2314//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00002315// Serialization Support
2316//===----------------------------------------------------------------------===//
2317
Ted Kremenek738e6c02007-10-31 17:10:13 +00002318/// Emit - Serialize an ASTContext object to Bitcode.
2319void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00002320 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00002321 S.EmitRef(SourceMgr);
2322 S.EmitRef(Target);
2323 S.EmitRef(Idents);
2324 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002325
Ted Kremenek68228a92007-10-31 22:44:07 +00002326 // Emit the size of the type vector so that we can reserve that size
2327 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002328 S.EmitInt(Types.size());
2329
Ted Kremenek034a78c2007-11-13 22:02:55 +00002330 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2331 I!=E;++I)
2332 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002333
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002334 S.EmitOwnedPtr(TUDecl);
2335
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002336 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002337}
2338
Ted Kremenekacba3612007-11-13 00:25:37 +00002339ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00002340
2341 // Read the language options.
2342 LangOptions LOpts;
2343 LOpts.Read(D);
2344
Ted Kremenek68228a92007-10-31 22:44:07 +00002345 SourceManager &SM = D.ReadRef<SourceManager>();
2346 TargetInfo &t = D.ReadRef<TargetInfo>();
2347 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2348 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00002349
Ted Kremenek68228a92007-10-31 22:44:07 +00002350 unsigned size_reserve = D.ReadInt();
2351
Douglas Gregor24afd4a2008-11-17 14:58:09 +00002352 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2353 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00002354
Ted Kremenek034a78c2007-11-13 22:02:55 +00002355 for (unsigned i = 0; i < size_reserve; ++i)
2356 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00002357
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002358 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2359
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002360 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00002361
2362 return A;
2363}