blob: 0325ba5734a0d1def5b9c114e59c85df7937cd10 [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"
15#include "clang/AST/Decl.h"
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "clang/Basic/TargetInfo.h"
19#include "llvm/ADT/SmallVector.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
30ASTContext::~ASTContext() {
31 // Deallocate all the types.
32 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000033 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000034 Types.pop_back();
35 }
Eli Friedman65489b72008-05-27 03:08:09 +000036
37 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000038}
39
40void ASTContext::PrintStats() const {
41 fprintf(stderr, "*** AST Context Stats:\n");
42 fprintf(stderr, " %d types total.\n", (int)Types.size());
43 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
44 unsigned NumVector = 0, NumComplex = 0;
45 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
46
47 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000048 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
49 unsigned NumObjCQualifiedIds = 0;
Steve Naroffe0430632008-05-21 15:59:22 +000050 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000051
52 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
53 Type *T = Types[i];
54 if (isa<BuiltinType>(T))
55 ++NumBuiltin;
56 else if (isa<PointerType>(T))
57 ++NumPointer;
58 else if (isa<ReferenceType>(T))
59 ++NumReference;
60 else if (isa<ComplexType>(T))
61 ++NumComplex;
62 else if (isa<ArrayType>(T))
63 ++NumArray;
64 else if (isa<VectorType>(T))
65 ++NumVector;
66 else if (isa<FunctionTypeNoProto>(T))
67 ++NumFunctionNP;
68 else if (isa<FunctionTypeProto>(T))
69 ++NumFunctionP;
70 else if (isa<TypedefType>(T))
71 ++NumTypeName;
72 else if (TagType *TT = dyn_cast<TagType>(T)) {
73 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +000074 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +000075 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +000076 case TagDecl::TK_struct: ++NumTagStruct; break;
77 case TagDecl::TK_union: ++NumTagUnion; break;
78 case TagDecl::TK_class: ++NumTagClass; break;
79 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +000080 }
Ted Kremenek42730c52008-01-07 19:49:32 +000081 } else if (isa<ObjCInterfaceType>(T))
82 ++NumObjCInterfaces;
83 else if (isa<ObjCQualifiedInterfaceType>(T))
84 ++NumObjCQualifiedInterfaces;
85 else if (isa<ObjCQualifiedIdType>(T))
86 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +000087 else if (isa<TypeOfType>(T))
88 ++NumTypeOfTypes;
89 else if (isa<TypeOfExpr>(T))
90 ++NumTypeOfExprs;
Steve Naroff948fd372007-09-17 14:16:13 +000091 else {
Chris Lattner8a35b462007-12-12 06:43:05 +000092 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +000093 assert(0 && "Unknown type!");
94 }
95 }
96
97 fprintf(stderr, " %d builtin types\n", NumBuiltin);
98 fprintf(stderr, " %d pointer types\n", NumPointer);
99 fprintf(stderr, " %d reference types\n", NumReference);
100 fprintf(stderr, " %d complex types\n", NumComplex);
101 fprintf(stderr, " %d array types\n", NumArray);
102 fprintf(stderr, " %d vector types\n", NumVector);
103 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
104 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
105 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
106 fprintf(stderr, " %d tagged types\n", NumTagged);
107 fprintf(stderr, " %d struct types\n", NumTagStruct);
108 fprintf(stderr, " %d union types\n", NumTagUnion);
109 fprintf(stderr, " %d class types\n", NumTagClass);
110 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000111 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000112 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000113 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000114 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000115 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000116 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
117 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
118
Chris Lattner4b009652007-07-25 00:24:17 +0000119 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
120 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
121 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
122 NumFunctionP*sizeof(FunctionTypeProto)+
123 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroffe0430632008-05-21 15:59:22 +0000124 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
125 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Chris Lattner4b009652007-07-25 00:24:17 +0000126}
127
128
129void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
130 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
131}
132
Chris Lattner4b009652007-07-25 00:24:17 +0000133void ASTContext::InitBuiltinTypes() {
134 assert(VoidTy.isNull() && "Context reinitialized?");
135
136 // C99 6.2.5p19.
137 InitBuiltinType(VoidTy, BuiltinType::Void);
138
139 // C99 6.2.5p2.
140 InitBuiltinType(BoolTy, BuiltinType::Bool);
141 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000142 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000143 InitBuiltinType(CharTy, BuiltinType::Char_S);
144 else
145 InitBuiltinType(CharTy, BuiltinType::Char_U);
146 // C99 6.2.5p4.
147 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
148 InitBuiltinType(ShortTy, BuiltinType::Short);
149 InitBuiltinType(IntTy, BuiltinType::Int);
150 InitBuiltinType(LongTy, BuiltinType::Long);
151 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
152
153 // C99 6.2.5p6.
154 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
155 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
156 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
157 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
158 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
159
160 // C99 6.2.5p10.
161 InitBuiltinType(FloatTy, BuiltinType::Float);
162 InitBuiltinType(DoubleTy, BuiltinType::Double);
163 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000164
165 // C++ 3.9.1p5
166 InitBuiltinType(WCharTy, BuiltinType::WChar);
167
Chris Lattner4b009652007-07-25 00:24:17 +0000168 // C99 6.2.5p11.
169 FloatComplexTy = getComplexType(FloatTy);
170 DoubleComplexTy = getComplexType(DoubleTy);
171 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff9d12c902007-10-15 14:41:52 +0000172
173 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000174 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000175 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000176 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000177 ClassStructType = 0;
178
Ted Kremenek42730c52008-01-07 19:49:32 +0000179 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000180
181 // void * type
182 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000183}
184
185//===----------------------------------------------------------------------===//
186// Type Sizing and Analysis
187//===----------------------------------------------------------------------===//
188
Chris Lattner2a674dc2008-06-30 18:32:54 +0000189/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
190/// scalar floating point type.
191const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
192 const BuiltinType *BT = T->getAsBuiltinType();
193 assert(BT && "Not a floating point type!");
194 switch (BT->getKind()) {
195 default: assert(0 && "Not a floating point type!");
196 case BuiltinType::Float: return Target.getFloatFormat();
197 case BuiltinType::Double: return Target.getDoubleFormat();
198 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
199 }
200}
201
202
Chris Lattner4b009652007-07-25 00:24:17 +0000203/// getTypeSize - Return the size of the specified type, in bits. This method
204/// does not work on incomplete types.
205std::pair<uint64_t, unsigned>
Chris Lattner8cd0e932008-03-05 18:54:05 +0000206ASTContext::getTypeInfo(QualType T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000207 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000208 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000209 unsigned Align;
210 switch (T->getTypeClass()) {
211 case Type::TypeName: assert(0 && "Not a canonical type!");
212 case Type::FunctionNoProto:
213 case Type::FunctionProto:
214 default:
215 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000216 case Type::VariableArray:
217 assert(0 && "VLAs not implemented yet!");
218 case Type::ConstantArray: {
219 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
220
Chris Lattner8cd0e932008-03-05 18:54:05 +0000221 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000222 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000223 Align = EltInfo.second;
224 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000225 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000226 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000227 case Type::Vector: {
228 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000229 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000230 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000231 // FIXME: This isn't right for unusual vectors
232 Align = Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000233 break;
234 }
235
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000236 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000237 switch (cast<BuiltinType>(T)->getKind()) {
238 default: assert(0 && "Unknown builtin type!");
239 case BuiltinType::Void:
240 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000241 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000242 Width = Target.getBoolWidth();
243 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000244 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000245 case BuiltinType::Char_S:
246 case BuiltinType::Char_U:
247 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000248 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000249 Width = Target.getCharWidth();
250 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000251 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000252 case BuiltinType::WChar:
253 Width = Target.getWCharWidth();
254 Align = Target.getWCharAlign();
255 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000256 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000257 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000258 Width = Target.getShortWidth();
259 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000260 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000261 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000262 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000263 Width = Target.getIntWidth();
264 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000265 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000266 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000267 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000268 Width = Target.getLongWidth();
269 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000270 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000271 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000272 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000273 Width = Target.getLongLongWidth();
274 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000275 break;
276 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000277 Width = Target.getFloatWidth();
278 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000279 break;
280 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000281 Width = Target.getDoubleWidth();
282 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000283 break;
284 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000285 Width = Target.getLongDoubleWidth();
286 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000287 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000288 }
289 break;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000290 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000291 // FIXME: Pointers into different addr spaces could have different sizes and
292 // alignment requirements: getPointerInfo should take an AddrSpace.
293 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000294 case Type::ObjCQualifiedId:
Chris Lattner1d78a862008-04-07 07:01:58 +0000295 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000296 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000297 break;
Chris Lattner461a6c52008-03-08 08:34:58 +0000298 case Type::Pointer: {
299 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000300 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000301 Align = Target.getPointerAlign(AS);
302 break;
303 }
Chris Lattner4b009652007-07-25 00:24:17 +0000304 case Type::Reference:
305 // "When applied to a reference or a reference type, the result is the size
306 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000307 // FIXME: This is wrong for struct layout: a reference in a struct has
308 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000309 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner4b009652007-07-25 00:24:17 +0000310
311 case Type::Complex: {
312 // Complex types have the same alignment as their elements, but twice the
313 // size.
314 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000315 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000316 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000317 Align = EltInfo.second;
318 break;
319 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000320 case Type::ObjCInterface: {
321 ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
322 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
323 Width = Layout.getSize();
324 Align = Layout.getAlignment();
325 break;
326 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000327 case Type::Tagged: {
Chris Lattnerfd799692008-08-09 21:35:13 +0000328 if (cast<TagType>(T)->getDecl()->isInvalidDecl()) {
329 Width = 1;
330 Align = 1;
331 break;
332 }
333
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000334 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
335 return getTypeInfo(ET->getDecl()->getIntegerType());
336
337 RecordType *RT = cast<RecordType>(T);
338 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
339 Width = Layout.getSize();
340 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000341 break;
342 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000343 }
Chris Lattner4b009652007-07-25 00:24:17 +0000344
345 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000346 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000347}
348
Devang Patelbfe323c2008-06-04 21:22:16 +0000349/// LayoutField - Field layout.
350void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
351 bool IsUnion, bool StructIsPacked,
352 ASTContext &Context) {
353 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
354 uint64_t FieldOffset = IsUnion ? 0 : Size;
355 uint64_t FieldSize;
356 unsigned FieldAlign;
357
358 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
359 // TODO: Need to check this algorithm on other targets!
360 // (tested on Linux-X86)
361 llvm::APSInt I(32);
362 bool BitWidthIsICE =
363 BitWidthExpr->isIntegerConstantExpr(I, Context);
364 assert (BitWidthIsICE && "Invalid BitField size expression");
365 FieldSize = I.getZExtValue();
366
367 std::pair<uint64_t, unsigned> FieldInfo =
368 Context.getTypeInfo(FD->getType());
369 uint64_t TypeSize = FieldInfo.first;
370
371 FieldAlign = FieldInfo.second;
372 if (FieldIsPacked)
373 FieldAlign = 1;
374 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
375 FieldAlign = std::max(FieldAlign, AA->getAlignment());
376
377 // Check if we need to add padding to give the field the correct
378 // alignment.
379 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
380 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
381
382 // Padding members don't affect overall alignment
383 if (!FD->getIdentifier())
384 FieldAlign = 1;
385 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000386 if (FD->getType()->isIncompleteArrayType()) {
387 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000388 // query getTypeInfo about these, so we figure it out here.
389 // Flexible array members don't have any size, but they
390 // have to be aligned appropriately for their element type.
391 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000392 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000393 FieldAlign = Context.getTypeAlign(ATy->getElementType());
394 } else {
395 std::pair<uint64_t, unsigned> FieldInfo =
396 Context.getTypeInfo(FD->getType());
397 FieldSize = FieldInfo.first;
398 FieldAlign = FieldInfo.second;
399 }
400
401 if (FieldIsPacked)
402 FieldAlign = 8;
403 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
404 FieldAlign = std::max(FieldAlign, AA->getAlignment());
405
406 // Round up the current record size to the field's alignment boundary.
407 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
408 }
409
410 // Place this field at the current location.
411 FieldOffsets[FieldNo] = FieldOffset;
412
413 // Reserve space for this field.
414 if (IsUnion) {
415 Size = std::max(Size, FieldSize);
416 } else {
417 Size = FieldOffset + FieldSize;
418 }
419
420 // Remember max struct/class alignment.
421 Alignment = std::max(Alignment, FieldAlign);
422}
423
Devang Patel4b6bf702008-06-04 21:54:36 +0000424
425/// getASTObjcInterfaceLayout - Get or compute information about the layout of the
426/// specified Objective C, which indicates its size and ivar
427/// position information.
428const ASTRecordLayout &
429ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
430 // Look up this layout, if already laid out, return what we have.
431 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
432 if (Entry) return *Entry;
433
434 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
435 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000436 ASTRecordLayout *NewEntry = NULL;
437 unsigned FieldCount = D->ivar_size();
438 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
439 FieldCount++;
440 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
441 unsigned Alignment = SL.getAlignment();
442 uint64_t Size = SL.getSize();
443 NewEntry = new ASTRecordLayout(Size, Alignment);
444 NewEntry->InitializeLayout(FieldCount);
445 NewEntry->SetFieldOffset(0, 0); // Super class is at the beginning of the layout.
446 } else {
447 NewEntry = new ASTRecordLayout();
448 NewEntry->InitializeLayout(FieldCount);
449 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000450 Entry = NewEntry;
451
Devang Patel4b6bf702008-06-04 21:54:36 +0000452 bool IsPacked = D->getAttr<PackedAttr>();
453
454 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
455 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
456 AA->getAlignment()));
457
458 // Layout each ivar sequentially.
459 unsigned i = 0;
460 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
461 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
462 const ObjCIvarDecl* Ivar = (*IVI);
463 NewEntry->LayoutField(Ivar, i++, false, IsPacked, *this);
464 }
465
466 // Finally, round the size of the total struct up to the alignment of the
467 // struct itself.
468 NewEntry->FinalizeLayout();
469 return *NewEntry;
470}
471
Devang Patel7a78e432007-11-01 19:11:01 +0000472/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000473/// specified record (struct/union/class), which indicates its size and field
474/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000475const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000476 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000477
Chris Lattner4b009652007-07-25 00:24:17 +0000478 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000479 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000480 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000481
Devang Patel7a78e432007-11-01 19:11:01 +0000482 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
483 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
484 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000485 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000486
Devang Patelbfe323c2008-06-04 21:22:16 +0000487 NewEntry->InitializeLayout(D->getNumMembers());
Eli Friedman5949a022008-05-30 09:31:38 +0000488 bool StructIsPacked = D->getAttr<PackedAttr>();
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000489 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000490
Eli Friedman5949a022008-05-30 09:31:38 +0000491 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000492 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
493 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000494
Eli Friedman5949a022008-05-30 09:31:38 +0000495 // Layout each field, for now, just sequentially, respecting alignment. In
496 // the future, this will need to be tweakable by targets.
497 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
498 const FieldDecl *FD = D->getMember(i);
Devang Patelbfe323c2008-06-04 21:22:16 +0000499 NewEntry->LayoutField(FD, i, IsUnion, StructIsPacked, *this);
Chris Lattner4b009652007-07-25 00:24:17 +0000500 }
Eli Friedman5949a022008-05-30 09:31:38 +0000501
502 // Finally, round the size of the total struct up to the alignment of the
503 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000504 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000505 return *NewEntry;
506}
507
Chris Lattner4b009652007-07-25 00:24:17 +0000508//===----------------------------------------------------------------------===//
509// Type creation/memoization methods
510//===----------------------------------------------------------------------===//
511
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000512QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000513 QualType CanT = getCanonicalType(T);
514 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000515 return T;
516
517 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
518 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000519 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000520 "Type is already address space qualified");
521
522 // Check if we've already instantiated an address space qual'd type of this
523 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000524 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000525 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000526 void *InsertPos = 0;
527 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
528 return QualType(ASQy, 0);
529
530 // If the base type isn't canonical, this won't be a canonical type either,
531 // so fill in the canonical type field.
532 QualType Canonical;
533 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000534 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000535
536 // Get the new insert position for the node we care about.
537 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
538 assert(NewIP == 0 && "Shouldn't be in the map!");
539 }
Chris Lattner35fef522008-02-20 20:55:12 +0000540 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000541 ASQualTypes.InsertNode(New, InsertPos);
542 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000543 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000544}
545
Chris Lattner4b009652007-07-25 00:24:17 +0000546
547/// getComplexType - Return the uniqued reference to the type for a complex
548/// number with the specified element type.
549QualType ASTContext::getComplexType(QualType T) {
550 // Unique pointers, to guarantee there is only one pointer of a particular
551 // structure.
552 llvm::FoldingSetNodeID ID;
553 ComplexType::Profile(ID, T);
554
555 void *InsertPos = 0;
556 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
557 return QualType(CT, 0);
558
559 // If the pointee type isn't canonical, this won't be a canonical type either,
560 // so fill in the canonical type field.
561 QualType Canonical;
562 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000563 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000564
565 // Get the new insert position for the node we care about.
566 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
567 assert(NewIP == 0 && "Shouldn't be in the map!");
568 }
569 ComplexType *New = new ComplexType(T, Canonical);
570 Types.push_back(New);
571 ComplexTypes.InsertNode(New, InsertPos);
572 return QualType(New, 0);
573}
574
575
576/// getPointerType - Return the uniqued reference to the type for a pointer to
577/// the specified type.
578QualType ASTContext::getPointerType(QualType T) {
579 // Unique pointers, to guarantee there is only one pointer of a particular
580 // structure.
581 llvm::FoldingSetNodeID ID;
582 PointerType::Profile(ID, T);
583
584 void *InsertPos = 0;
585 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
586 return QualType(PT, 0);
587
588 // If the pointee type isn't canonical, this won't be a canonical type either,
589 // so fill in the canonical type field.
590 QualType Canonical;
591 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000592 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000593
594 // Get the new insert position for the node we care about.
595 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
596 assert(NewIP == 0 && "Shouldn't be in the map!");
597 }
598 PointerType *New = new PointerType(T, Canonical);
599 Types.push_back(New);
600 PointerTypes.InsertNode(New, InsertPos);
601 return QualType(New, 0);
602}
603
604/// getReferenceType - Return the uniqued reference to the type for a reference
605/// to the specified type.
606QualType ASTContext::getReferenceType(QualType T) {
607 // Unique pointers, to guarantee there is only one pointer of a particular
608 // structure.
609 llvm::FoldingSetNodeID ID;
610 ReferenceType::Profile(ID, T);
611
612 void *InsertPos = 0;
613 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
614 return QualType(RT, 0);
615
616 // If the referencee type isn't canonical, this won't be a canonical type
617 // either, so fill in the canonical type field.
618 QualType Canonical;
619 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000620 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000621
622 // Get the new insert position for the node we care about.
623 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
624 assert(NewIP == 0 && "Shouldn't be in the map!");
625 }
626
627 ReferenceType *New = new ReferenceType(T, Canonical);
628 Types.push_back(New);
629 ReferenceTypes.InsertNode(New, InsertPos);
630 return QualType(New, 0);
631}
632
Steve Naroff83c13012007-08-30 01:06:46 +0000633/// getConstantArrayType - Return the unique reference to the type for an
634/// array of the specified element type.
635QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000636 const llvm::APInt &ArySize,
637 ArrayType::ArraySizeModifier ASM,
638 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000639 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000640 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000641
642 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000643 if (ConstantArrayType *ATP =
644 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000645 return QualType(ATP, 0);
646
647 // If the element type isn't canonical, this won't be a canonical type either,
648 // so fill in the canonical type field.
649 QualType Canonical;
650 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000651 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000652 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000653 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000654 ConstantArrayType *NewIP =
655 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
656
Chris Lattner4b009652007-07-25 00:24:17 +0000657 assert(NewIP == 0 && "Shouldn't be in the map!");
658 }
659
Steve Naroff24c9b982007-08-30 18:10:14 +0000660 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
661 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000662 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000663 Types.push_back(New);
664 return QualType(New, 0);
665}
666
Steve Naroffe2579e32007-08-30 18:14:25 +0000667/// getVariableArrayType - Returns a non-unique reference to the type for a
668/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000669QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
670 ArrayType::ArraySizeModifier ASM,
671 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000672 // Since we don't unique expressions, it isn't possible to unique VLA's
673 // that have an expression provided for their size.
674
675 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
676 ASM, EltTypeQuals);
677
678 VariableArrayTypes.push_back(New);
679 Types.push_back(New);
680 return QualType(New, 0);
681}
682
683QualType ASTContext::getIncompleteArrayType(QualType EltTy,
684 ArrayType::ArraySizeModifier ASM,
685 unsigned EltTypeQuals) {
686 llvm::FoldingSetNodeID ID;
687 IncompleteArrayType::Profile(ID, EltTy);
688
689 void *InsertPos = 0;
690 if (IncompleteArrayType *ATP =
691 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
692 return QualType(ATP, 0);
693
694 // If the element type isn't canonical, this won't be a canonical type
695 // either, so fill in the canonical type field.
696 QualType Canonical;
697
698 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000699 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000700 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000701
702 // Get the new insert position for the node we care about.
703 IncompleteArrayType *NewIP =
704 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
705
706 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000707 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000708
709 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
710 ASM, EltTypeQuals);
711
712 IncompleteArrayTypes.InsertNode(New, InsertPos);
713 Types.push_back(New);
714 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000715}
716
Chris Lattner4b009652007-07-25 00:24:17 +0000717/// getVectorType - Return the unique reference to a vector type of
718/// the specified element type and size. VectorType must be a built-in type.
719QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
720 BuiltinType *baseType;
721
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000722 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000723 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
724
725 // Check if we've already instantiated a vector of this type.
726 llvm::FoldingSetNodeID ID;
727 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
728 void *InsertPos = 0;
729 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
730 return QualType(VTP, 0);
731
732 // If the element type isn't canonical, this won't be a canonical type either,
733 // so fill in the canonical type field.
734 QualType Canonical;
735 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000736 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000737
738 // Get the new insert position for the node we care about.
739 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
740 assert(NewIP == 0 && "Shouldn't be in the map!");
741 }
742 VectorType *New = new VectorType(vecType, NumElts, Canonical);
743 VectorTypes.InsertNode(New, InsertPos);
744 Types.push_back(New);
745 return QualType(New, 0);
746}
747
Nate Begemanaf6ed502008-04-18 23:10:10 +0000748/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +0000749/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000750QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +0000751 BuiltinType *baseType;
752
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000753 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +0000754 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +0000755
756 // Check if we've already instantiated a vector of this type.
757 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000758 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +0000759 void *InsertPos = 0;
760 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
761 return QualType(VTP, 0);
762
763 // If the element type isn't canonical, this won't be a canonical type either,
764 // so fill in the canonical type field.
765 QualType Canonical;
766 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000767 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000768
769 // Get the new insert position for the node we care about.
770 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
771 assert(NewIP == 0 && "Shouldn't be in the map!");
772 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000773 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000774 VectorTypes.InsertNode(New, InsertPos);
775 Types.push_back(New);
776 return QualType(New, 0);
777}
778
779/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
780///
781QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
782 // Unique functions, to guarantee there is only one function of a particular
783 // structure.
784 llvm::FoldingSetNodeID ID;
785 FunctionTypeNoProto::Profile(ID, ResultTy);
786
787 void *InsertPos = 0;
788 if (FunctionTypeNoProto *FT =
789 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
790 return QualType(FT, 0);
791
792 QualType Canonical;
793 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000794 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000795
796 // Get the new insert position for the node we care about.
797 FunctionTypeNoProto *NewIP =
798 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
799 assert(NewIP == 0 && "Shouldn't be in the map!");
800 }
801
802 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
803 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000804 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000805 return QualType(New, 0);
806}
807
808/// getFunctionType - Return a normal function type with a typed argument
809/// list. isVariadic indicates whether the argument list includes '...'.
810QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
811 unsigned NumArgs, bool isVariadic) {
812 // Unique functions, to guarantee there is only one function of a particular
813 // structure.
814 llvm::FoldingSetNodeID ID;
815 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
816
817 void *InsertPos = 0;
818 if (FunctionTypeProto *FTP =
819 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
820 return QualType(FTP, 0);
821
822 // Determine whether the type being created is already canonical or not.
823 bool isCanonical = ResultTy->isCanonical();
824 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
825 if (!ArgArray[i]->isCanonical())
826 isCanonical = false;
827
828 // If this type isn't canonical, get the canonical version of it.
829 QualType Canonical;
830 if (!isCanonical) {
831 llvm::SmallVector<QualType, 16> CanonicalArgs;
832 CanonicalArgs.reserve(NumArgs);
833 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000834 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +0000835
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000836 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +0000837 &CanonicalArgs[0], NumArgs,
838 isVariadic);
839
840 // Get the new insert position for the node we care about.
841 FunctionTypeProto *NewIP =
842 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
843 assert(NewIP == 0 && "Shouldn't be in the map!");
844 }
845
846 // FunctionTypeProto objects are not allocated with new because they have a
847 // variable size array (for parameter types) at the end of them.
848 FunctionTypeProto *FTP =
849 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
850 NumArgs*sizeof(QualType));
851 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
852 Canonical);
853 Types.push_back(FTP);
854 FunctionTypeProtos.InsertNode(FTP, InsertPos);
855 return QualType(FTP, 0);
856}
857
Douglas Gregor1d661552008-04-13 21:07:44 +0000858/// getTypeDeclType - Return the unique reference to the type for the
859/// specified type declaration.
860QualType ASTContext::getTypeDeclType(TypeDecl *Decl) {
861 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
862
863 if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(Decl))
864 return getTypedefType(Typedef);
865 else if (ObjCInterfaceDecl *ObjCInterface
866 = dyn_cast_or_null<ObjCInterfaceDecl>(Decl))
867 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000868
869 if (CXXRecordDecl *CXXRecord = dyn_cast_or_null<CXXRecordDecl>(Decl))
870 Decl->TypeForDecl = new CXXRecordType(CXXRecord);
871 else if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +0000872 Decl->TypeForDecl = new RecordType(Record);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000873 else if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +0000874 Decl->TypeForDecl = new EnumType(Enum);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000875 else
Douglas Gregor1d661552008-04-13 21:07:44 +0000876 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000877
878 Types.push_back(Decl->TypeForDecl);
879 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +0000880}
881
Chris Lattner4b009652007-07-25 00:24:17 +0000882/// getTypedefType - Return the unique reference to the type for the
883/// specified typename decl.
884QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
885 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
886
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000887 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000888 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000889 Types.push_back(Decl->TypeForDecl);
890 return QualType(Decl->TypeForDecl, 0);
891}
892
Ted Kremenek42730c52008-01-07 19:49:32 +0000893/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000894/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000895QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000896 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
897
Ted Kremenek42730c52008-01-07 19:49:32 +0000898 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000899 Types.push_back(Decl->TypeForDecl);
900 return QualType(Decl->TypeForDecl, 0);
901}
902
Chris Lattnere1352302008-04-07 04:56:42 +0000903/// CmpProtocolNames - Comparison predicate for sorting protocols
904/// alphabetically.
905static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
906 const ObjCProtocolDecl *RHS) {
907 return strcmp(LHS->getName(), RHS->getName()) < 0;
908}
909
910static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
911 unsigned &NumProtocols) {
912 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
913
914 // Sort protocols, keyed by name.
915 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
916
917 // Remove duplicates.
918 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
919 NumProtocols = ProtocolsEnd-Protocols;
920}
921
922
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +0000923/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
924/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000925QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
926 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000927 // Sort the protocol list alphabetically to canonicalize it.
928 SortAndUniqueProtocols(Protocols, NumProtocols);
929
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000930 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +0000931 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000932
933 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000934 if (ObjCQualifiedInterfaceType *QT =
935 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000936 return QualType(QT, 0);
937
938 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +0000939 ObjCQualifiedInterfaceType *QType =
940 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000941 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000942 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000943 return QualType(QType, 0);
944}
945
Chris Lattnere1352302008-04-07 04:56:42 +0000946/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
947/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +0000948QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000949 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000950 // Sort the protocol list alphabetically to canonicalize it.
951 SortAndUniqueProtocols(Protocols, NumProtocols);
952
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000953 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000954 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000955
956 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000957 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +0000958 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000959 return QualType(QT, 0);
960
961 // No Match;
Chris Lattner4a68fe02008-07-26 00:46:50 +0000962 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000963 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000964 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000965 return QualType(QType, 0);
966}
967
Steve Naroff0604dd92007-08-01 18:02:17 +0000968/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
969/// TypeOfExpr AST's (since expression's are never shared). For example,
970/// multiple declarations that refer to "typeof(x)" all contain different
971/// DeclRefExpr's. This doesn't effect the type checker, since it operates
972/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000973QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000974 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff0604dd92007-08-01 18:02:17 +0000975 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
976 Types.push_back(toe);
977 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000978}
979
Steve Naroff0604dd92007-08-01 18:02:17 +0000980/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
981/// TypeOfType AST's. The only motivation to unique these nodes would be
982/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
983/// an issue. This doesn't effect the type checker, since it operates
984/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000985QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000986 QualType Canonical = getCanonicalType(tofType);
Steve Naroff0604dd92007-08-01 18:02:17 +0000987 TypeOfType *tot = new TypeOfType(tofType, Canonical);
988 Types.push_back(tot);
989 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000990}
991
Chris Lattner4b009652007-07-25 00:24:17 +0000992/// getTagDeclType - Return the unique reference to the type for the
993/// specified TagDecl (struct/union/class/enum) decl.
994QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +0000995 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +0000996 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +0000997}
998
999/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1000/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1001/// needs to agree with the definition in <stddef.h>.
1002QualType ASTContext::getSizeType() const {
1003 // On Darwin, size_t is defined as a "long unsigned int".
1004 // FIXME: should derive from "Target".
1005 return UnsignedLongTy;
1006}
1007
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001008/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001009/// width of characters in wide strings, The value is target dependent and
1010/// needs to agree with the definition in <stddef.h>.
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001011QualType ASTContext::getWCharType() const {
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001012 if (LangOpts.CPlusPlus)
1013 return WCharTy;
1014
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001015 // On Darwin, wchar_t is defined as a "int".
1016 // FIXME: should derive from "Target".
1017 return IntTy;
1018}
1019
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001020/// getSignedWCharType - Return the type of "signed wchar_t".
1021/// Used when in C++, as a GCC extension.
1022QualType ASTContext::getSignedWCharType() const {
1023 // FIXME: derive from "Target" ?
1024 return WCharTy;
1025}
1026
1027/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1028/// Used when in C++, as a GCC extension.
1029QualType ASTContext::getUnsignedWCharType() const {
1030 // FIXME: derive from "Target" ?
1031 return UnsignedIntTy;
1032}
1033
Chris Lattner4b009652007-07-25 00:24:17 +00001034/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1035/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1036QualType ASTContext::getPointerDiffType() const {
1037 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
1038 // FIXME: should derive from "Target".
1039 return IntTy;
1040}
1041
Chris Lattner19eb97e2008-04-02 05:18:44 +00001042//===----------------------------------------------------------------------===//
1043// Type Operators
1044//===----------------------------------------------------------------------===//
1045
Chris Lattner3dae6f42008-04-06 22:41:35 +00001046/// getCanonicalType - Return the canonical (structural) type corresponding to
1047/// the specified potentially non-canonical type. The non-canonical version
1048/// of a type may have many "decorated" versions of types. Decorators can
1049/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1050/// to be free of any of these, allowing two canonical types to be compared
1051/// for exact equality with a simple pointer comparison.
1052QualType ASTContext::getCanonicalType(QualType T) {
1053 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001054
1055 // If the result has type qualifiers, make sure to canonicalize them as well.
1056 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1057 if (TypeQuals == 0) return CanType;
1058
1059 // If the type qualifiers are on an array type, get the canonical type of the
1060 // array with the qualifiers applied to the element type.
1061 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1062 if (!AT)
1063 return CanType.getQualifiedType(TypeQuals);
1064
1065 // Get the canonical version of the element with the extra qualifiers on it.
1066 // This can recursively sink qualifiers through multiple levels of arrays.
1067 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1068 NewEltTy = getCanonicalType(NewEltTy);
1069
1070 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1071 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1072 CAT->getIndexTypeQualifier());
1073 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1074 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1075 IAT->getIndexTypeQualifier());
1076
1077 // FIXME: What is the ownership of size expressions in VLAs?
1078 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1079 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1080 VAT->getSizeModifier(),
1081 VAT->getIndexTypeQualifier());
1082}
1083
1084
1085const ArrayType *ASTContext::getAsArrayType(QualType T) {
1086 // Handle the non-qualified case efficiently.
1087 if (T.getCVRQualifiers() == 0) {
1088 // Handle the common positive case fast.
1089 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1090 return AT;
1091 }
1092
1093 // Handle the common negative case fast, ignoring CVR qualifiers.
1094 QualType CType = T->getCanonicalTypeInternal();
1095
1096 // Make sure to look through type qualifiers (like ASQuals) for the negative
1097 // test.
1098 if (!isa<ArrayType>(CType) &&
1099 !isa<ArrayType>(CType.getUnqualifiedType()))
1100 return 0;
1101
1102 // Apply any CVR qualifiers from the array type to the element type. This
1103 // implements C99 6.7.3p8: "If the specification of an array type includes
1104 // any type qualifiers, the element type is so qualified, not the array type."
1105
1106 // If we get here, we either have type qualifiers on the type, or we have
1107 // sugar such as a typedef in the way. If we have type qualifiers on the type
1108 // we must propagate them down into the elemeng type.
1109 unsigned CVRQuals = T.getCVRQualifiers();
1110 unsigned AddrSpace = 0;
1111 Type *Ty = T.getTypePtr();
1112
1113 // Rip through ASQualType's and typedefs to get to a concrete type.
1114 while (1) {
1115 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1116 AddrSpace = ASQT->getAddressSpace();
1117 Ty = ASQT->getBaseType();
1118 } else {
1119 T = Ty->getDesugaredType();
1120 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1121 break;
1122 CVRQuals |= T.getCVRQualifiers();
1123 Ty = T.getTypePtr();
1124 }
1125 }
1126
1127 // If we have a simple case, just return now.
1128 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1129 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1130 return ATy;
1131
1132 // Otherwise, we have an array and we have qualifiers on it. Push the
1133 // qualifiers into the array element type and return a new array type.
1134 // Get the canonical version of the element with the extra qualifiers on it.
1135 // This can recursively sink qualifiers through multiple levels of arrays.
1136 QualType NewEltTy = ATy->getElementType();
1137 if (AddrSpace)
1138 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1139 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1140
1141 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1142 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1143 CAT->getSizeModifier(),
1144 CAT->getIndexTypeQualifier()));
1145 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1146 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1147 IAT->getSizeModifier(),
1148 IAT->getIndexTypeQualifier()));
1149
1150 // FIXME: What is the ownership of size expressions in VLAs?
1151 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1152 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1153 VAT->getSizeModifier(),
1154 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001155}
1156
1157
Chris Lattner19eb97e2008-04-02 05:18:44 +00001158/// getArrayDecayedType - Return the properly qualified result of decaying the
1159/// specified array type to a pointer. This operation is non-trivial when
1160/// handling typedefs etc. The canonical type of "T" must be an array type,
1161/// this returns a pointer to a properly qualified element of the array.
1162///
1163/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1164QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001165 // Get the element type with 'getAsArrayType' so that we don't lose any
1166 // typedefs in the element type of the array. This also handles propagation
1167 // of type qualifiers from the array type into the element type if present
1168 // (C99 6.7.3p8).
1169 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1170 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001171
Chris Lattnera1923f62008-08-04 07:31:14 +00001172 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001173
1174 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001175 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001176}
1177
Chris Lattner4b009652007-07-25 00:24:17 +00001178/// getFloatingRank - Return a relative rank for floating point types.
1179/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001180static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001181 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001182 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001183
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001184 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001185 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001186 case BuiltinType::Float: return FloatRank;
1187 case BuiltinType::Double: return DoubleRank;
1188 case BuiltinType::LongDouble: return LongDoubleRank;
1189 }
1190}
1191
Steve Narofffa0c4532007-08-27 01:41:48 +00001192/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1193/// point or a complex type (based on typeDomain/typeSize).
1194/// 'typeDomain' is a real floating point or complex type.
1195/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001196QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1197 QualType Domain) const {
1198 FloatingRank EltRank = getFloatingRank(Size);
1199 if (Domain->isComplexType()) {
1200 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001201 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001202 case FloatRank: return FloatComplexTy;
1203 case DoubleRank: return DoubleComplexTy;
1204 case LongDoubleRank: return LongDoubleComplexTy;
1205 }
Chris Lattner4b009652007-07-25 00:24:17 +00001206 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001207
1208 assert(Domain->isRealFloatingType() && "Unknown domain!");
1209 switch (EltRank) {
1210 default: assert(0 && "getFloatingRank(): illegal value for rank");
1211 case FloatRank: return FloatTy;
1212 case DoubleRank: return DoubleTy;
1213 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001214 }
Chris Lattner4b009652007-07-25 00:24:17 +00001215}
1216
Chris Lattner51285d82008-04-06 23:55:33 +00001217/// getFloatingTypeOrder - Compare the rank of the two specified floating
1218/// point types, ignoring the domain of the type (i.e. 'double' ==
1219/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1220/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001221int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1222 FloatingRank LHSR = getFloatingRank(LHS);
1223 FloatingRank RHSR = getFloatingRank(RHS);
1224
1225 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001226 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001227 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001228 return 1;
1229 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001230}
1231
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001232/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1233/// routine will assert if passed a built-in type that isn't an integer or enum,
1234/// or if it is not canonicalized.
1235static unsigned getIntegerRank(Type *T) {
1236 assert(T->isCanonical() && "T should be canonicalized");
1237 if (isa<EnumType>(T))
1238 return 4;
1239
1240 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001241 default: assert(0 && "getIntegerRank(): not a built-in integer");
1242 case BuiltinType::Bool:
1243 return 1;
1244 case BuiltinType::Char_S:
1245 case BuiltinType::Char_U:
1246 case BuiltinType::SChar:
1247 case BuiltinType::UChar:
1248 return 2;
1249 case BuiltinType::Short:
1250 case BuiltinType::UShort:
1251 return 3;
1252 case BuiltinType::Int:
1253 case BuiltinType::UInt:
1254 return 4;
1255 case BuiltinType::Long:
1256 case BuiltinType::ULong:
1257 return 5;
1258 case BuiltinType::LongLong:
1259 case BuiltinType::ULongLong:
1260 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001261 }
1262}
1263
Chris Lattner51285d82008-04-06 23:55:33 +00001264/// getIntegerTypeOrder - Returns the highest ranked integer type:
1265/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1266/// LHS < RHS, return -1.
1267int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001268 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1269 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001270 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001271
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001272 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1273 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001274
Chris Lattner51285d82008-04-06 23:55:33 +00001275 unsigned LHSRank = getIntegerRank(LHSC);
1276 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001277
Chris Lattner51285d82008-04-06 23:55:33 +00001278 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1279 if (LHSRank == RHSRank) return 0;
1280 return LHSRank > RHSRank ? 1 : -1;
1281 }
Chris Lattner4b009652007-07-25 00:24:17 +00001282
Chris Lattner51285d82008-04-06 23:55:33 +00001283 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1284 if (LHSUnsigned) {
1285 // If the unsigned [LHS] type is larger, return it.
1286 if (LHSRank >= RHSRank)
1287 return 1;
1288
1289 // If the signed type can represent all values of the unsigned type, it
1290 // wins. Because we are dealing with 2's complement and types that are
1291 // powers of two larger than each other, this is always safe.
1292 return -1;
1293 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001294
Chris Lattner51285d82008-04-06 23:55:33 +00001295 // If the unsigned [RHS] type is larger, return it.
1296 if (RHSRank >= LHSRank)
1297 return -1;
1298
1299 // If the signed type can represent all values of the unsigned type, it
1300 // wins. Because we are dealing with 2's complement and types that are
1301 // powers of two larger than each other, this is always safe.
1302 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001303}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001304
1305// getCFConstantStringType - Return the type used for constant CFStrings.
1306QualType ASTContext::getCFConstantStringType() {
1307 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001308 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001309 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Chris Lattner58114f02008-03-15 21:32:50 +00001310 &Idents.get("NSConstantString"), 0);
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001311 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001312
1313 // const int *isa;
1314 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001315 // int flags;
1316 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001317 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001318 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001319 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001320 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001321 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001322 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001323
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001324 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerf3874bc2008-04-06 04:47:34 +00001325 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner81db64a2008-03-16 00:16:02 +00001326 FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001327
1328 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1329 }
1330
1331 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001332}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001333
Anders Carlssone3f02572007-10-29 06:33:42 +00001334// This returns true if a type has been typedefed to BOOL:
1335// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001336static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001337 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001338 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001339
1340 return false;
1341}
1342
Ted Kremenek42730c52008-01-07 19:49:32 +00001343/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001344/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001345int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001346 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001347
1348 // Make all integer and enum types at least as large as an int
1349 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001350 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001351 // Treat arrays as pointers, since that's how they're passed in.
1352 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001353 sz = getTypeSize(VoidPtrTy);
1354 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001355}
1356
Ted Kremenek42730c52008-01-07 19:49:32 +00001357/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001358/// declaration.
Ted Kremenek42730c52008-01-07 19:49:32 +00001359void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001360 std::string& S)
1361{
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001362 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001363 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001364 // Encode result type.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001365 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001366 // Compute size of all parameters.
1367 // Start with computing size of a pointer in number of bytes.
1368 // FIXME: There might(should) be a better way of doing this computation!
1369 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001370 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001371 // The first two arguments (self and _cmd) are pointers; account for
1372 // their size.
1373 int ParmOffset = 2 * PtrSize;
1374 int NumOfParams = Decl->getNumParams();
1375 for (int i = 0; i < NumOfParams; i++) {
1376 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001377 int sz = getObjCEncodingTypeSize (PType);
1378 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001379 ParmOffset += sz;
1380 }
1381 S += llvm::utostr(ParmOffset);
1382 S += "@0:";
1383 S += llvm::utostr(PtrSize);
1384
1385 // Argument types.
1386 ParmOffset = 2 * PtrSize;
1387 for (int i = 0; i < NumOfParams; i++) {
1388 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001389 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001390 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001391 getObjCEncodingForTypeQualifier(
1392 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian248db262008-01-22 22:44:46 +00001393 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001394 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001395 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001396 }
1397}
1398
Fariborz Jahanian248db262008-01-22 22:44:46 +00001399void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Chris Lattnera1923f62008-08-04 07:31:14 +00001400 llvm::SmallVector<const RecordType *, 8> &ERType) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00001401 // FIXME: This currently doesn't encode:
1402 // @ An object (whether statically typed or typed id)
1403 // # A class object (Class)
1404 // : A method selector (SEL)
1405 // {name=type...} A structure
1406 // (name=type...) A union
1407 // bnum A bit field of num bits
1408
1409 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001410 char encoding;
1411 switch (BT->getKind()) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001412 default: assert(0 && "Unhandled builtin type kind");
1413 case BuiltinType::Void: encoding = 'v'; break;
1414 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001415 case BuiltinType::Char_U:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001416 case BuiltinType::UChar: encoding = 'C'; break;
1417 case BuiltinType::UShort: encoding = 'S'; break;
1418 case BuiltinType::UInt: encoding = 'I'; break;
1419 case BuiltinType::ULong: encoding = 'L'; break;
1420 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001421 case BuiltinType::Char_S:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001422 case BuiltinType::SChar: encoding = 'c'; break;
1423 case BuiltinType::Short: encoding = 's'; break;
1424 case BuiltinType::Int: encoding = 'i'; break;
1425 case BuiltinType::Long: encoding = 'l'; break;
1426 case BuiltinType::LongLong: encoding = 'q'; break;
1427 case BuiltinType::Float: encoding = 'f'; break;
1428 case BuiltinType::Double: encoding = 'd'; break;
1429 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001430 }
1431
1432 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001433 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001434 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001435 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001436 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001437
1438 }
1439 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001440 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001441 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001442 S += '@';
1443 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001444 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001445 S += '#';
1446 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001447 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001448 S += ':';
1449 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001450 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001451
1452 if (PointeeTy->isCharType()) {
1453 // char pointer types should be encoded as '*' unless it is a
1454 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001455 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001456 S += '*';
1457 return;
1458 }
1459 }
1460
1461 S += '^';
Fariborz Jahanian248db262008-01-22 22:44:46 +00001462 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Chris Lattnera1923f62008-08-04 07:31:14 +00001463 } else if (const ArrayType *AT =
1464 // Ignore type qualifiers etc.
1465 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001466 S += '[';
1467
1468 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1469 S += llvm::utostr(CAT->getSize().getZExtValue());
1470 else
1471 assert(0 && "Unhandled array type!");
1472
Fariborz Jahanian248db262008-01-22 22:44:46 +00001473 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001474 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001475 } else if (T->getAsFunctionType()) {
1476 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001477 } else if (const RecordType *RTy = T->getAsRecordType()) {
1478 RecordDecl *RDecl= RTy->getDecl();
1479 S += '{';
1480 S += RDecl->getName();
Fariborz Jahanian248db262008-01-22 22:44:46 +00001481 bool found = false;
1482 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1483 if (ERType[i] == RTy) {
1484 found = true;
1485 break;
1486 }
1487 if (!found) {
1488 ERType.push_back(RTy);
1489 S += '=';
1490 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1491 FieldDecl *field = RDecl->getMember(i);
1492 getObjCEncodingForType(field->getType(), S, ERType);
1493 }
1494 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1495 ERType.pop_back();
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001496 }
1497 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001498 } else if (T->isEnumeralType()) {
1499 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001500 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001501 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001502}
1503
Ted Kremenek42730c52008-01-07 19:49:32 +00001504void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001505 std::string& S) const {
1506 if (QT & Decl::OBJC_TQ_In)
1507 S += 'n';
1508 if (QT & Decl::OBJC_TQ_Inout)
1509 S += 'N';
1510 if (QT & Decl::OBJC_TQ_Out)
1511 S += 'o';
1512 if (QT & Decl::OBJC_TQ_Bycopy)
1513 S += 'O';
1514 if (QT & Decl::OBJC_TQ_Byref)
1515 S += 'R';
1516 if (QT & Decl::OBJC_TQ_Oneway)
1517 S += 'V';
1518}
1519
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001520void ASTContext::setBuiltinVaListType(QualType T)
1521{
1522 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1523
1524 BuiltinVaListType = T;
1525}
1526
Ted Kremenek42730c52008-01-07 19:49:32 +00001527void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001528{
Ted Kremenek42730c52008-01-07 19:49:32 +00001529 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff9d12c902007-10-15 14:41:52 +00001530
Ted Kremenek42730c52008-01-07 19:49:32 +00001531 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001532
1533 // typedef struct objc_object *id;
1534 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1535 assert(ptr && "'id' incorrectly typed");
1536 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1537 assert(rec && "'id' incorrectly typed");
1538 IdStructType = rec;
1539}
1540
Ted Kremenek42730c52008-01-07 19:49:32 +00001541void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001542{
Ted Kremenek42730c52008-01-07 19:49:32 +00001543 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001544
Ted Kremenek42730c52008-01-07 19:49:32 +00001545 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001546
1547 // typedef struct objc_selector *SEL;
1548 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1549 assert(ptr && "'SEL' incorrectly typed");
1550 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1551 assert(rec && "'SEL' incorrectly typed");
1552 SelStructType = rec;
1553}
1554
Ted Kremenek42730c52008-01-07 19:49:32 +00001555void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001556{
Ted Kremenek42730c52008-01-07 19:49:32 +00001557 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1558 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001559}
1560
Ted Kremenek42730c52008-01-07 19:49:32 +00001561void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001562{
Ted Kremenek42730c52008-01-07 19:49:32 +00001563 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001564
Ted Kremenek42730c52008-01-07 19:49:32 +00001565 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001566
1567 // typedef struct objc_class *Class;
1568 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1569 assert(ptr && "'Class' incorrectly typed");
1570 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1571 assert(rec && "'Class' incorrectly typed");
1572 ClassStructType = rec;
1573}
1574
Ted Kremenek42730c52008-01-07 19:49:32 +00001575void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1576 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001577 "'NSConstantString' type already set!");
1578
Ted Kremenek42730c52008-01-07 19:49:32 +00001579 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001580}
1581
Ted Kremenek118930e2008-07-24 23:58:27 +00001582
1583//===----------------------------------------------------------------------===//
1584// Type Predicates.
1585//===----------------------------------------------------------------------===//
1586
1587/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1588/// to an object type. This includes "id" and "Class" (two 'special' pointers
1589/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1590/// ID type).
1591bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1592 if (Ty->isObjCQualifiedIdType())
1593 return true;
1594
1595 if (!Ty->isPointerType())
1596 return false;
1597
1598 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1599 // pointer types. This looks for the typedef specifically, not for the
1600 // underlying type.
1601 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1602 return true;
1603
1604 // If this a pointer to an interface (e.g. NSString*), it is ok.
1605 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1606}
1607
Chris Lattner6ff358b2008-04-07 06:51:04 +00001608//===----------------------------------------------------------------------===//
1609// Type Compatibility Testing
1610//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00001611
Chris Lattner390564e2008-04-07 06:49:41 +00001612/// C99 6.2.7p1: If both are complete types, then the following additional
1613/// requirements apply.
1614/// FIXME (handle compatibility across source files).
1615static bool areCompatTagTypes(TagType *LHS, TagType *RHS,
1616 const ASTContext &C) {
Steve Naroff4a5e2072007-11-07 06:03:51 +00001617 // "Class" and "id" are compatible built-in structure types.
Chris Lattner390564e2008-04-07 06:49:41 +00001618 if (C.isObjCIdType(QualType(LHS, 0)) && C.isObjCClassType(QualType(RHS, 0)) ||
1619 C.isObjCClassType(QualType(LHS, 0)) && C.isObjCIdType(QualType(RHS, 0)))
Steve Naroff4a5e2072007-11-07 06:03:51 +00001620 return true;
Eli Friedmane7fb03a2008-02-15 06:03:44 +00001621
Chris Lattner390564e2008-04-07 06:49:41 +00001622 // Within a translation unit a tag type is only compatible with itself. Self
1623 // equality is already handled by the time we get here.
1624 assert(LHS != RHS && "Self equality not handled!");
1625 return false;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001626}
1627
1628bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1629 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1630 // identically qualified and both shall be pointers to compatible types.
Chris Lattner35fef522008-02-20 20:55:12 +00001631 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1632 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001633 return false;
1634
Chris Lattner25168a52008-07-26 21:30:36 +00001635 QualType ltype = lhs->getAsPointerType()->getPointeeType();
1636 QualType rtype = rhs->getAsPointerType()->getPointeeType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001637
1638 return typesAreCompatible(ltype, rtype);
1639}
1640
Steve Naroff85f0dc52007-10-15 20:41:53 +00001641bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
Chris Lattner25168a52008-07-26 21:30:36 +00001642 const FunctionType *lbase = lhs->getAsFunctionType();
1643 const FunctionType *rbase = rhs->getAsFunctionType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001644 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1645 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1646
1647 // first check the return types (common between C99 and K&R).
1648 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1649 return false;
1650
1651 if (lproto && rproto) { // two C99 style function prototypes
1652 unsigned lproto_nargs = lproto->getNumArgs();
1653 unsigned rproto_nargs = rproto->getNumArgs();
1654
1655 if (lproto_nargs != rproto_nargs)
1656 return false;
1657
1658 // both prototypes have the same number of arguments.
1659 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1660 (rproto->isVariadic() && !lproto->isVariadic()))
1661 return false;
1662
1663 // The use of ellipsis agree...now check the argument types.
1664 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001665 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1666 // is taken as having the unqualified version of it's declared type.
Steve Naroffdec17fe2008-01-29 00:15:50 +00001667 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001668 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001669 return false;
1670 return true;
1671 }
Chris Lattner1d78a862008-04-07 07:01:58 +00001672
Steve Naroff85f0dc52007-10-15 20:41:53 +00001673 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1674 return true;
1675
1676 // we have a mixture of K&R style with C99 prototypes
1677 const FunctionTypeProto *proto = lproto ? lproto : rproto;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001678 if (proto->isVariadic())
1679 return false;
1680
1681 // FIXME: Each parameter type T in the prototype must be compatible with the
1682 // type resulting from applying the usual argument conversions to T.
1683 return true;
1684}
1685
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001686// C99 6.7.5.2p6
1687static bool areCompatArrayTypes(ArrayType *LHS, ArrayType *RHS, ASTContext &C) {
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001688 // Constant arrays must be the same size to be compatible.
1689 if (const ConstantArrayType* LCAT = dyn_cast<ConstantArrayType>(LHS))
1690 if (const ConstantArrayType* RCAT = dyn_cast<ConstantArrayType>(RHS))
1691 if (RCAT->getSize() != LCAT->getSize())
1692 return false;
Eli Friedman1e7537832008-02-06 04:53:22 +00001693
Chris Lattnerc8971d72008-04-07 06:58:21 +00001694 // Compatible arrays must have compatible element types
1695 return C.typesAreCompatible(LHS->getElementType(), RHS->getElementType());
Steve Naroff85f0dc52007-10-15 20:41:53 +00001696}
1697
Chris Lattner6ff358b2008-04-07 06:51:04 +00001698/// areCompatVectorTypes - Return true if the two specified vector types are
1699/// compatible.
1700static bool areCompatVectorTypes(const VectorType *LHS,
1701 const VectorType *RHS) {
1702 assert(LHS->isCanonical() && RHS->isCanonical());
1703 return LHS->getElementType() == RHS->getElementType() &&
1704 LHS->getNumElements() == RHS->getNumElements();
1705}
1706
1707/// areCompatObjCInterfaces - Return true if the two interface types are
1708/// compatible for assignment from RHS to LHS. This handles validation of any
1709/// protocol qualifiers on the LHS or RHS.
1710///
Chris Lattner1d78a862008-04-07 07:01:58 +00001711static bool areCompatObjCInterfaces(const ObjCInterfaceType *LHS,
1712 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00001713 // Verify that the base decls are compatible: the RHS must be a subclass of
1714 // the LHS.
1715 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1716 return false;
1717
1718 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1719 // protocol qualified at all, then we are good.
1720 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1721 return true;
1722
1723 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1724 // isn't a superset.
1725 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1726 return true; // FIXME: should return false!
1727
1728 // Finally, we must have two protocol-qualified interfaces.
1729 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1730 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1731 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1732 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1733 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1734 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1735
1736 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1737 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1738 // LHS in a single parallel scan until we run out of elements in LHS.
1739 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1740 ObjCProtocolDecl *LHSProto = *LHSPI;
1741
1742 while (RHSPI != RHSPE) {
1743 ObjCProtocolDecl *RHSProto = *RHSPI++;
1744 // If the RHS has a protocol that the LHS doesn't, ignore it.
1745 if (RHSProto != LHSProto)
1746 continue;
1747
1748 // Otherwise, the RHS does have this element.
1749 ++LHSPI;
1750 if (LHSPI == LHSPE)
1751 return true; // All protocols in LHS exist in RHS.
1752
1753 LHSProto = *LHSPI;
1754 }
1755
1756 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1757 return false;
1758}
1759
1760
Steve Naroff85f0dc52007-10-15 20:41:53 +00001761/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1762/// both shall have the identically qualified version of a compatible type.
1763/// C99 6.2.7p1: Two types have compatible types if their types are the
1764/// same. See 6.7.[2,3,5] for additional rules.
Chris Lattner855fed42008-04-07 04:07:56 +00001765bool ASTContext::typesAreCompatible(QualType LHS_NC, QualType RHS_NC) {
Chris Lattner25168a52008-07-26 21:30:36 +00001766 QualType LHS = getCanonicalType(LHS_NC);
1767 QualType RHS = getCanonicalType(RHS_NC);
Chris Lattner4d5670b2008-04-03 05:07:04 +00001768
Bill Wendling6a9d8542007-12-03 07:33:35 +00001769 // C++ [expr]: If an expression initially has the type "reference to T", the
1770 // type is adjusted to "T" prior to any further analysis, the expression
1771 // designates the object or function denoted by the reference, and the
1772 // expression is an lvalue.
Chris Lattner855fed42008-04-07 04:07:56 +00001773 if (ReferenceType *RT = dyn_cast<ReferenceType>(LHS))
1774 LHS = RT->getPointeeType();
1775 if (ReferenceType *RT = dyn_cast<ReferenceType>(RHS))
1776 RHS = RT->getPointeeType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001777
Chris Lattnerd47d6042008-04-07 05:37:56 +00001778 // If two types are identical, they are compatible.
1779 if (LHS == RHS)
1780 return true;
1781
1782 // If qualifiers differ, the types are different.
Chris Lattnerb5709e22008-04-07 05:43:21 +00001783 unsigned LHSAS = LHS.getAddressSpace(), RHSAS = RHS.getAddressSpace();
1784 if (LHS.getCVRQualifiers() != RHS.getCVRQualifiers() || LHSAS != RHSAS)
Chris Lattnerd47d6042008-04-07 05:37:56 +00001785 return false;
Chris Lattnerb5709e22008-04-07 05:43:21 +00001786
1787 // Strip off ASQual's if present.
1788 if (LHSAS) {
1789 LHS = LHS.getUnqualifiedType();
1790 RHS = RHS.getUnqualifiedType();
1791 }
Chris Lattnerd47d6042008-04-07 05:37:56 +00001792
Chris Lattner855fed42008-04-07 04:07:56 +00001793 Type::TypeClass LHSClass = LHS->getTypeClass();
1794 Type::TypeClass RHSClass = RHS->getTypeClass();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001795
1796 // We want to consider the two function types to be the same for these
1797 // comparisons, just force one to the other.
1798 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1799 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00001800
1801 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00001802 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
1803 LHSClass = Type::ConstantArray;
1804 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
1805 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001806
Nate Begemanaf6ed502008-04-18 23:10:10 +00001807 // Canonicalize ExtVector -> Vector.
1808 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
1809 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00001810
Chris Lattner7cdcb252008-04-07 06:38:24 +00001811 // Consider qualified interfaces and interfaces the same.
1812 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
1813 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
1814
Chris Lattnerb5709e22008-04-07 05:43:21 +00001815 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001816 if (LHSClass != RHSClass) {
Chris Lattner7cdcb252008-04-07 06:38:24 +00001817 // ID is compatible with all interface types.
1818 if (isa<ObjCInterfaceType>(LHS))
1819 return isObjCIdType(RHS);
1820 if (isa<ObjCInterfaceType>(RHS))
1821 return isObjCIdType(LHS);
Steve Naroff44549772008-06-04 15:07:33 +00001822
1823 // ID is compatible with all qualified id types.
1824 if (isa<ObjCQualifiedIdType>(LHS)) {
1825 if (const PointerType *PT = RHS->getAsPointerType())
1826 return isObjCIdType(PT->getPointeeType());
1827 }
1828 if (isa<ObjCQualifiedIdType>(RHS)) {
1829 if (const PointerType *PT = LHS->getAsPointerType())
1830 return isObjCIdType(PT->getPointeeType());
1831 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001832 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1833 // a signed integer type, or an unsigned integer type.
Chris Lattner855fed42008-04-07 04:07:56 +00001834 if (LHS->isEnumeralType() && RHS->isIntegralType()) {
1835 EnumDecl* EDecl = cast<EnumType>(LHS)->getDecl();
1836 return EDecl->getIntegerType() == RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001837 }
Chris Lattner855fed42008-04-07 04:07:56 +00001838 if (RHS->isEnumeralType() && LHS->isIntegralType()) {
1839 EnumDecl* EDecl = cast<EnumType>(RHS)->getDecl();
1840 return EDecl->getIntegerType() == LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001841 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001842
Steve Naroff85f0dc52007-10-15 20:41:53 +00001843 return false;
1844 }
Chris Lattnerb5709e22008-04-07 05:43:21 +00001845
Steve Naroffc88babe2008-01-09 22:43:08 +00001846 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001847 switch (LHSClass) {
Chris Lattnerb5709e22008-04-07 05:43:21 +00001848 case Type::ASQual:
1849 case Type::FunctionProto:
1850 case Type::VariableArray:
1851 case Type::IncompleteArray:
1852 case Type::Reference:
Chris Lattner7cdcb252008-04-07 06:38:24 +00001853 case Type::ObjCQualifiedInterface:
Chris Lattnerb5709e22008-04-07 05:43:21 +00001854 assert(0 && "Canonicalized away above");
Chris Lattnerc38d4522008-01-14 05:45:46 +00001855 case Type::Pointer:
Chris Lattner855fed42008-04-07 04:07:56 +00001856 return pointerTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001857 case Type::ConstantArray:
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001858 return areCompatArrayTypes(cast<ArrayType>(LHS), cast<ArrayType>(RHS),
1859 *this);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001860 case Type::FunctionNoProto:
Chris Lattner855fed42008-04-07 04:07:56 +00001861 return functionTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001862 case Type::Tagged: // handle structures, unions
Chris Lattner390564e2008-04-07 06:49:41 +00001863 return areCompatTagTypes(cast<TagType>(LHS), cast<TagType>(RHS), *this);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001864 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00001865 // Only exactly equal builtin types are compatible, which is tested above.
1866 return false;
1867 case Type::Vector:
1868 return areCompatVectorTypes(cast<VectorType>(LHS), cast<VectorType>(RHS));
Chris Lattnerc38d4522008-01-14 05:45:46 +00001869 case Type::ObjCInterface:
Chris Lattner7cdcb252008-04-07 06:38:24 +00001870 return areCompatObjCInterfaces(cast<ObjCInterfaceType>(LHS),
1871 cast<ObjCInterfaceType>(RHS));
Chris Lattnerc38d4522008-01-14 05:45:46 +00001872 default:
1873 assert(0 && "unexpected type");
Steve Naroff85f0dc52007-10-15 20:41:53 +00001874 }
1875 return true; // should never get here...
1876}
Ted Kremenek738e6c02007-10-31 17:10:13 +00001877
Chris Lattner1d78a862008-04-07 07:01:58 +00001878//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00001879// Integer Predicates
1880//===----------------------------------------------------------------------===//
1881unsigned ASTContext::getIntWidth(QualType T) {
1882 if (T == BoolTy)
1883 return 1;
1884 // At the moment, only bool has padding bits
1885 return (unsigned)getTypeSize(T);
1886}
1887
1888QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
1889 assert(T->isSignedIntegerType() && "Unexpected type");
1890 if (const EnumType* ETy = T->getAsEnumType())
1891 T = ETy->getDecl()->getIntegerType();
1892 const BuiltinType* BTy = T->getAsBuiltinType();
1893 assert (BTy && "Unexpected signed integer type");
1894 switch (BTy->getKind()) {
1895 case BuiltinType::Char_S:
1896 case BuiltinType::SChar:
1897 return UnsignedCharTy;
1898 case BuiltinType::Short:
1899 return UnsignedShortTy;
1900 case BuiltinType::Int:
1901 return UnsignedIntTy;
1902 case BuiltinType::Long:
1903 return UnsignedLongTy;
1904 case BuiltinType::LongLong:
1905 return UnsignedLongLongTy;
1906 default:
1907 assert(0 && "Unexpected signed integer type");
1908 return QualType();
1909 }
1910}
1911
1912
1913//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00001914// Serialization Support
1915//===----------------------------------------------------------------------===//
1916
Ted Kremenek738e6c02007-10-31 17:10:13 +00001917/// Emit - Serialize an ASTContext object to Bitcode.
1918void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00001919 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00001920 S.EmitRef(SourceMgr);
1921 S.EmitRef(Target);
1922 S.EmitRef(Idents);
1923 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001924
Ted Kremenek68228a92007-10-31 22:44:07 +00001925 // Emit the size of the type vector so that we can reserve that size
1926 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001927 S.EmitInt(Types.size());
1928
Ted Kremenek034a78c2007-11-13 22:02:55 +00001929 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1930 I!=E;++I)
1931 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001932
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001933 S.EmitOwnedPtr(TUDecl);
1934
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001935 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001936}
1937
Ted Kremenekacba3612007-11-13 00:25:37 +00001938ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00001939
1940 // Read the language options.
1941 LangOptions LOpts;
1942 LOpts.Read(D);
1943
Ted Kremenek68228a92007-10-31 22:44:07 +00001944 SourceManager &SM = D.ReadRef<SourceManager>();
1945 TargetInfo &t = D.ReadRef<TargetInfo>();
1946 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1947 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00001948
Ted Kremenek68228a92007-10-31 22:44:07 +00001949 unsigned size_reserve = D.ReadInt();
1950
Ted Kremenek842126e2008-06-04 15:55:15 +00001951 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00001952
Ted Kremenek034a78c2007-11-13 22:02:55 +00001953 for (unsigned i = 0; i < size_reserve; ++i)
1954 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00001955
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001956 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
1957
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001958 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00001959
1960 return A;
1961}