blob: 12bbee2d9fc54b7200f84d4e7099e21abdf3ca5d [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"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Basic/TargetInfo.h"
18#include "llvm/ADT/SmallVector.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000019#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000020#include "llvm/Bitcode/Serialize.h"
21#include "llvm/Bitcode/Deserialize.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000022
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24
25enum FloatingRank {
26 FloatRank, DoubleRank, LongDoubleRank
27};
28
29ASTContext::~ASTContext() {
30 // Deallocate all the types.
31 while (!Types.empty()) {
32 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) {
33 // Destroy the object, but don't call delete. These are malloc'd.
34 FT->~FunctionTypeProto();
35 free(FT);
36 } else {
37 delete Types.back();
38 }
39 Types.pop_back();
40 }
41}
42
43void ASTContext::PrintStats() const {
44 fprintf(stderr, "*** AST Context Stats:\n");
45 fprintf(stderr, " %d types total.\n", (int)Types.size());
46 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
47 unsigned NumVector = 0, NumComplex = 0;
48 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
49
50 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000051 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
52 unsigned NumObjCQualifiedIds = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000053
54 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
55 Type *T = Types[i];
56 if (isa<BuiltinType>(T))
57 ++NumBuiltin;
58 else if (isa<PointerType>(T))
59 ++NumPointer;
60 else if (isa<ReferenceType>(T))
61 ++NumReference;
62 else if (isa<ComplexType>(T))
63 ++NumComplex;
64 else if (isa<ArrayType>(T))
65 ++NumArray;
66 else if (isa<VectorType>(T))
67 ++NumVector;
68 else if (isa<FunctionTypeNoProto>(T))
69 ++NumFunctionNP;
70 else if (isa<FunctionTypeProto>(T))
71 ++NumFunctionP;
72 else if (isa<TypedefType>(T))
73 ++NumTypeName;
74 else if (TagType *TT = dyn_cast<TagType>(T)) {
75 ++NumTagged;
76 switch (TT->getDecl()->getKind()) {
77 default: assert(0 && "Unknown tagged type!");
78 case Decl::Struct: ++NumTagStruct; break;
79 case Decl::Union: ++NumTagUnion; break;
80 case Decl::Class: ++NumTagClass; break;
81 case Decl::Enum: ++NumTagEnum; break;
82 }
Ted Kremenek42730c52008-01-07 19:49:32 +000083 } else if (isa<ObjCInterfaceType>(T))
84 ++NumObjCInterfaces;
85 else if (isa<ObjCQualifiedInterfaceType>(T))
86 ++NumObjCQualifiedInterfaces;
87 else if (isa<ObjCQualifiedIdType>(T))
88 ++NumObjCQualifiedIds;
Steve Naroff948fd372007-09-17 14:16:13 +000089 else {
Chris Lattner8a35b462007-12-12 06:43:05 +000090 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +000091 assert(0 && "Unknown type!");
92 }
93 }
94
95 fprintf(stderr, " %d builtin types\n", NumBuiltin);
96 fprintf(stderr, " %d pointer types\n", NumPointer);
97 fprintf(stderr, " %d reference types\n", NumReference);
98 fprintf(stderr, " %d complex types\n", NumComplex);
99 fprintf(stderr, " %d array types\n", NumArray);
100 fprintf(stderr, " %d vector types\n", NumVector);
101 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
102 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
103 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
104 fprintf(stderr, " %d tagged types\n", NumTagged);
105 fprintf(stderr, " %d struct types\n", NumTagStruct);
106 fprintf(stderr, " %d union types\n", NumTagUnion);
107 fprintf(stderr, " %d class types\n", NumTagClass);
108 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000109 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000110 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000111 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000112 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000113 NumObjCQualifiedIds);
Chris Lattner4b009652007-07-25 00:24:17 +0000114 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
115 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
116 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
117 NumFunctionP*sizeof(FunctionTypeProto)+
118 NumFunctionNP*sizeof(FunctionTypeNoProto)+
119 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
120}
121
122
123void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
124 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
125}
126
Chris Lattner4b009652007-07-25 00:24:17 +0000127void ASTContext::InitBuiltinTypes() {
128 assert(VoidTy.isNull() && "Context reinitialized?");
129
130 // C99 6.2.5p19.
131 InitBuiltinType(VoidTy, BuiltinType::Void);
132
133 // C99 6.2.5p2.
134 InitBuiltinType(BoolTy, BuiltinType::Bool);
135 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000136 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000137 InitBuiltinType(CharTy, BuiltinType::Char_S);
138 else
139 InitBuiltinType(CharTy, BuiltinType::Char_U);
140 // C99 6.2.5p4.
141 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
142 InitBuiltinType(ShortTy, BuiltinType::Short);
143 InitBuiltinType(IntTy, BuiltinType::Int);
144 InitBuiltinType(LongTy, BuiltinType::Long);
145 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
146
147 // C99 6.2.5p6.
148 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
149 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
150 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
151 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
152 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
153
154 // C99 6.2.5p10.
155 InitBuiltinType(FloatTy, BuiltinType::Float);
156 InitBuiltinType(DoubleTy, BuiltinType::Double);
157 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
158
159 // C99 6.2.5p11.
160 FloatComplexTy = getComplexType(FloatTy);
161 DoubleComplexTy = getComplexType(DoubleTy);
162 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff9d12c902007-10-15 14:41:52 +0000163
164 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000165 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000166 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000167 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000168 ClassStructType = 0;
169
Ted Kremenek42730c52008-01-07 19:49:32 +0000170 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000171
172 // void * type
173 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000174}
175
176//===----------------------------------------------------------------------===//
177// Type Sizing and Analysis
178//===----------------------------------------------------------------------===//
179
180/// getTypeSize - Return the size of the specified type, in bits. This method
181/// does not work on incomplete types.
182std::pair<uint64_t, unsigned>
Chris Lattner8cd0e932008-03-05 18:54:05 +0000183ASTContext::getTypeInfo(QualType T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000184 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000185 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000186 unsigned Align;
187 switch (T->getTypeClass()) {
188 case Type::TypeName: assert(0 && "Not a canonical type!");
189 case Type::FunctionNoProto:
190 case Type::FunctionProto:
191 default:
192 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000193 case Type::VariableArray:
194 assert(0 && "VLAs not implemented yet!");
195 case Type::ConstantArray: {
196 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
197
Chris Lattner8cd0e932008-03-05 18:54:05 +0000198 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000199 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000200 Align = EltInfo.second;
201 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000202 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000203 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000204 case Type::Vector: {
205 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000206 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000207 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Chris Lattner4b009652007-07-25 00:24:17 +0000208 // FIXME: Vector alignment is not the alignment of its elements.
209 Align = EltInfo.second;
210 break;
211 }
212
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000213 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000214 switch (cast<BuiltinType>(T)->getKind()) {
215 default: assert(0 && "Unknown builtin type!");
216 case BuiltinType::Void:
217 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000218 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000219 Width = Target.getBoolWidth();
220 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000221 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000222 case BuiltinType::Char_S:
223 case BuiltinType::Char_U:
224 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000225 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000226 Width = Target.getCharWidth();
227 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000228 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000229 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000230 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000231 Width = Target.getShortWidth();
232 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000233 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000234 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000235 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000236 Width = Target.getIntWidth();
237 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000238 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000239 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000240 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000241 Width = Target.getLongWidth();
242 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000243 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000244 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000245 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000246 Width = Target.getLongLongWidth();
247 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000248 break;
249 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000250 Width = Target.getFloatWidth();
251 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000252 break;
253 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000254 Width = Target.getDoubleWidth();
255 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000256 break;
257 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000258 Width = Target.getLongDoubleWidth();
259 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000260 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000261 }
262 break;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000263 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000264 // FIXME: Pointers into different addr spaces could have different sizes and
265 // alignment requirements: getPointerInfo should take an AddrSpace.
266 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000267 case Type::ObjCQualifiedId:
Chris Lattner1d78a862008-04-07 07:01:58 +0000268 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000269 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000270 break;
Chris Lattner461a6c52008-03-08 08:34:58 +0000271 case Type::Pointer: {
272 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000273 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000274 Align = Target.getPointerAlign(AS);
275 break;
276 }
Chris Lattner4b009652007-07-25 00:24:17 +0000277 case Type::Reference:
278 // "When applied to a reference or a reference type, the result is the size
279 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000280 // FIXME: This is wrong for struct layout: a reference in a struct has
281 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000282 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner4b009652007-07-25 00:24:17 +0000283
284 case Type::Complex: {
285 // Complex types have the same alignment as their elements, but twice the
286 // size.
287 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000288 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000289 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000290 Align = EltInfo.second;
291 break;
292 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000293 case Type::Tagged: {
294 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
295 return getTypeInfo(ET->getDecl()->getIntegerType());
296
297 RecordType *RT = cast<RecordType>(T);
298 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
299 Width = Layout.getSize();
300 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000301 break;
302 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000303 }
Chris Lattner4b009652007-07-25 00:24:17 +0000304
305 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000306 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000307}
308
Devang Patel7a78e432007-11-01 19:11:01 +0000309/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000310/// specified record (struct/union/class), which indicates its size and field
311/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000312const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000313 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
314
315 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000316 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000317 if (Entry) return *Entry;
318
Devang Patel7a78e432007-11-01 19:11:01 +0000319 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
320 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
321 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000322 Entry = NewEntry;
323
324 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
325 uint64_t RecordSize = 0;
326 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
327
328 if (D->getKind() != Decl::Union) {
Anders Carlsson7dce0292008-02-16 19:51:27 +0000329 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
330 RecordAlign = std::max(RecordAlign, AA->getAlignment());
331
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000332 bool StructIsPacked = D->getAttr<PackedAttr>();
333
Chris Lattner4b009652007-07-25 00:24:17 +0000334 // Layout each field, for now, just sequentially, respecting alignment. In
335 // the future, this will need to be tweakable by targets.
336 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
337 const FieldDecl *FD = D->getMember(i);
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000338 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
Eli Friedman67571ac2008-02-06 05:33:51 +0000339 uint64_t FieldSize;
340 unsigned FieldAlign;
Anders Carlsson058237f2008-02-18 07:13:09 +0000341
342 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
343 llvm::APSInt I(32);
344 bool BitWidthIsICE =
345 BitWidthExpr->isIntegerConstantExpr(I, *this);
346 assert (BitWidthIsICE && "Invalid BitField size expression");
347 FieldSize = I.getZExtValue();
348
Chris Lattner8cd0e932008-03-05 18:54:05 +0000349 std::pair<uint64_t, unsigned> TypeInfo = getTypeInfo(FD->getType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000350 uint64_t TypeSize = TypeInfo.first;
Anders Carlsson7dce0292008-02-16 19:51:27 +0000351
352 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
353 FieldAlign = AA->getAlignment();
354 else if (FieldIsPacked)
355 FieldAlign = 8;
356 else {
Eli Friedmanf8382542008-05-20 15:17:39 +0000357 FieldAlign = TypeInfo.second;
Anders Carlsson7dce0292008-02-16 19:51:27 +0000358 }
Eli Friedman67571ac2008-02-06 05:33:51 +0000359
Anders Carlsson058237f2008-02-18 07:13:09 +0000360 // Check if we need to add padding to give the field the correct
361 // alignment.
362 if (RecordSize % FieldAlign + FieldSize > TypeSize)
363 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
364
365 } else {
366 if (FD->getType()->isIncompleteType()) {
367 // This must be a flexible array member; we can't directly
368 // query getTypeInfo about these, so we figure it out here.
369 // Flexible array members don't have any size, but they
370 // have to be aligned appropriately for their element type.
371
372 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
373 FieldAlign = AA->getAlignment();
374 else if (FieldIsPacked)
375 FieldAlign = 8;
376 else {
377 const ArrayType* ATy = FD->getType()->getAsArrayType();
Chris Lattner8cd0e932008-03-05 18:54:05 +0000378 FieldAlign = getTypeAlign(ATy->getElementType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000379 }
380 FieldSize = 0;
381 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000382 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000383 FieldSize = FieldInfo.first;
384
385 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
386 FieldAlign = AA->getAlignment();
387 else if (FieldIsPacked)
388 FieldAlign = 8;
389 else
390 FieldAlign = FieldInfo.second;
391 }
392
393 // Round up the current record size to the field's alignment boundary.
394 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
395 }
Chris Lattner4b009652007-07-25 00:24:17 +0000396
397 // Place this field at the current location.
398 FieldOffsets[i] = RecordSize;
399
400 // Reserve space for this field.
401 RecordSize += FieldSize;
402
403 // Remember max struct/class alignment.
404 RecordAlign = std::max(RecordAlign, FieldAlign);
405 }
406
407 // Finally, round the size of the total struct up to the alignment of the
408 // struct itself.
409 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
410 } else {
411 // Union layout just puts each member at the start of the record.
412 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
413 const FieldDecl *FD = D->getMember(i);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000414 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Chris Lattner4b009652007-07-25 00:24:17 +0000415 uint64_t FieldSize = FieldInfo.first;
416 unsigned FieldAlign = FieldInfo.second;
Anders Carlsson058237f2008-02-18 07:13:09 +0000417
Chris Lattner4b009652007-07-25 00:24:17 +0000418 // Round up the current record size to the field's alignment boundary.
419 RecordSize = std::max(RecordSize, FieldSize);
Eli Friedmanf8382542008-05-20 15:17:39 +0000420
Chris Lattner4b009652007-07-25 00:24:17 +0000421 // Place this field at the start of the record.
422 FieldOffsets[i] = 0;
Eli Friedmanf8382542008-05-20 15:17:39 +0000423
Chris Lattner4b009652007-07-25 00:24:17 +0000424 // Remember max struct/class alignment.
425 RecordAlign = std::max(RecordAlign, FieldAlign);
426 }
427 }
428
429 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
430 return *NewEntry;
431}
432
Chris Lattner4b009652007-07-25 00:24:17 +0000433//===----------------------------------------------------------------------===//
434// Type creation/memoization methods
435//===----------------------------------------------------------------------===//
436
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000437QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000438 QualType CanT = getCanonicalType(T);
439 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000440 return T;
441
442 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
443 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000444 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000445 "Type is already address space qualified");
446
447 // Check if we've already instantiated an address space qual'd type of this
448 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000449 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000450 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000451 void *InsertPos = 0;
452 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
453 return QualType(ASQy, 0);
454
455 // If the base type isn't canonical, this won't be a canonical type either,
456 // so fill in the canonical type field.
457 QualType Canonical;
458 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000459 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000460
461 // Get the new insert position for the node we care about.
462 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
463 assert(NewIP == 0 && "Shouldn't be in the map!");
464 }
Chris Lattner35fef522008-02-20 20:55:12 +0000465 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000466 ASQualTypes.InsertNode(New, InsertPos);
467 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000468 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000469}
470
Chris Lattner4b009652007-07-25 00:24:17 +0000471
472/// getComplexType - Return the uniqued reference to the type for a complex
473/// number with the specified element type.
474QualType ASTContext::getComplexType(QualType T) {
475 // Unique pointers, to guarantee there is only one pointer of a particular
476 // structure.
477 llvm::FoldingSetNodeID ID;
478 ComplexType::Profile(ID, T);
479
480 void *InsertPos = 0;
481 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
482 return QualType(CT, 0);
483
484 // If the pointee type isn't canonical, this won't be a canonical type either,
485 // so fill in the canonical type field.
486 QualType Canonical;
487 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000488 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000489
490 // Get the new insert position for the node we care about.
491 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
492 assert(NewIP == 0 && "Shouldn't be in the map!");
493 }
494 ComplexType *New = new ComplexType(T, Canonical);
495 Types.push_back(New);
496 ComplexTypes.InsertNode(New, InsertPos);
497 return QualType(New, 0);
498}
499
500
501/// getPointerType - Return the uniqued reference to the type for a pointer to
502/// the specified type.
503QualType ASTContext::getPointerType(QualType T) {
504 // Unique pointers, to guarantee there is only one pointer of a particular
505 // structure.
506 llvm::FoldingSetNodeID ID;
507 PointerType::Profile(ID, T);
508
509 void *InsertPos = 0;
510 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
511 return QualType(PT, 0);
512
513 // If the pointee type isn't canonical, this won't be a canonical type either,
514 // so fill in the canonical type field.
515 QualType Canonical;
516 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000517 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000518
519 // Get the new insert position for the node we care about.
520 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
521 assert(NewIP == 0 && "Shouldn't be in the map!");
522 }
523 PointerType *New = new PointerType(T, Canonical);
524 Types.push_back(New);
525 PointerTypes.InsertNode(New, InsertPos);
526 return QualType(New, 0);
527}
528
529/// getReferenceType - Return the uniqued reference to the type for a reference
530/// to the specified type.
531QualType ASTContext::getReferenceType(QualType T) {
532 // Unique pointers, to guarantee there is only one pointer of a particular
533 // structure.
534 llvm::FoldingSetNodeID ID;
535 ReferenceType::Profile(ID, T);
536
537 void *InsertPos = 0;
538 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
539 return QualType(RT, 0);
540
541 // If the referencee type isn't canonical, this won't be a canonical type
542 // either, so fill in the canonical type field.
543 QualType Canonical;
544 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000545 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000546
547 // Get the new insert position for the node we care about.
548 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
549 assert(NewIP == 0 && "Shouldn't be in the map!");
550 }
551
552 ReferenceType *New = new ReferenceType(T, Canonical);
553 Types.push_back(New);
554 ReferenceTypes.InsertNode(New, InsertPos);
555 return QualType(New, 0);
556}
557
Steve Naroff83c13012007-08-30 01:06:46 +0000558/// getConstantArrayType - Return the unique reference to the type for an
559/// array of the specified element type.
560QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000561 const llvm::APInt &ArySize,
562 ArrayType::ArraySizeModifier ASM,
563 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000564 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000565 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000566
567 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000568 if (ConstantArrayType *ATP =
569 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000570 return QualType(ATP, 0);
571
572 // If the element type isn't canonical, this won't be a canonical type either,
573 // so fill in the canonical type field.
574 QualType Canonical;
575 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000576 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000577 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000578 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000579 ConstantArrayType *NewIP =
580 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
581
Chris Lattner4b009652007-07-25 00:24:17 +0000582 assert(NewIP == 0 && "Shouldn't be in the map!");
583 }
584
Steve Naroff24c9b982007-08-30 18:10:14 +0000585 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
586 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000587 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000588 Types.push_back(New);
589 return QualType(New, 0);
590}
591
Steve Naroffe2579e32007-08-30 18:14:25 +0000592/// getVariableArrayType - Returns a non-unique reference to the type for a
593/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000594QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
595 ArrayType::ArraySizeModifier ASM,
596 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000597 // Since we don't unique expressions, it isn't possible to unique VLA's
598 // that have an expression provided for their size.
599
600 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
601 ASM, EltTypeQuals);
602
603 VariableArrayTypes.push_back(New);
604 Types.push_back(New);
605 return QualType(New, 0);
606}
607
608QualType ASTContext::getIncompleteArrayType(QualType EltTy,
609 ArrayType::ArraySizeModifier ASM,
610 unsigned EltTypeQuals) {
611 llvm::FoldingSetNodeID ID;
612 IncompleteArrayType::Profile(ID, EltTy);
613
614 void *InsertPos = 0;
615 if (IncompleteArrayType *ATP =
616 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
617 return QualType(ATP, 0);
618
619 // If the element type isn't canonical, this won't be a canonical type
620 // either, so fill in the canonical type field.
621 QualType Canonical;
622
623 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000624 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000625 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000626
627 // Get the new insert position for the node we care about.
628 IncompleteArrayType *NewIP =
629 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
630
631 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000632 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000633
634 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
635 ASM, EltTypeQuals);
636
637 IncompleteArrayTypes.InsertNode(New, InsertPos);
638 Types.push_back(New);
639 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000640}
641
Chris Lattner4b009652007-07-25 00:24:17 +0000642/// getVectorType - Return the unique reference to a vector type of
643/// the specified element type and size. VectorType must be a built-in type.
644QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
645 BuiltinType *baseType;
646
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000647 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000648 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
649
650 // Check if we've already instantiated a vector of this type.
651 llvm::FoldingSetNodeID ID;
652 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
653 void *InsertPos = 0;
654 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
655 return QualType(VTP, 0);
656
657 // If the element type isn't canonical, this won't be a canonical type either,
658 // so fill in the canonical type field.
659 QualType Canonical;
660 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000661 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000662
663 // Get the new insert position for the node we care about.
664 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
665 assert(NewIP == 0 && "Shouldn't be in the map!");
666 }
667 VectorType *New = new VectorType(vecType, NumElts, Canonical);
668 VectorTypes.InsertNode(New, InsertPos);
669 Types.push_back(New);
670 return QualType(New, 0);
671}
672
Nate Begemanaf6ed502008-04-18 23:10:10 +0000673/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +0000674/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000675QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +0000676 BuiltinType *baseType;
677
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000678 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +0000679 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +0000680
681 // Check if we've already instantiated a vector of this type.
682 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000683 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +0000684 void *InsertPos = 0;
685 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
686 return QualType(VTP, 0);
687
688 // If the element type isn't canonical, this won't be a canonical type either,
689 // so fill in the canonical type field.
690 QualType Canonical;
691 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000692 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000693
694 // Get the new insert position for the node we care about.
695 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
696 assert(NewIP == 0 && "Shouldn't be in the map!");
697 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000698 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000699 VectorTypes.InsertNode(New, InsertPos);
700 Types.push_back(New);
701 return QualType(New, 0);
702}
703
704/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
705///
706QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
707 // Unique functions, to guarantee there is only one function of a particular
708 // structure.
709 llvm::FoldingSetNodeID ID;
710 FunctionTypeNoProto::Profile(ID, ResultTy);
711
712 void *InsertPos = 0;
713 if (FunctionTypeNoProto *FT =
714 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
715 return QualType(FT, 0);
716
717 QualType Canonical;
718 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000719 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000720
721 // Get the new insert position for the node we care about.
722 FunctionTypeNoProto *NewIP =
723 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
724 assert(NewIP == 0 && "Shouldn't be in the map!");
725 }
726
727 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
728 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000729 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000730 return QualType(New, 0);
731}
732
733/// getFunctionType - Return a normal function type with a typed argument
734/// list. isVariadic indicates whether the argument list includes '...'.
735QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
736 unsigned NumArgs, bool isVariadic) {
737 // Unique functions, to guarantee there is only one function of a particular
738 // structure.
739 llvm::FoldingSetNodeID ID;
740 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
741
742 void *InsertPos = 0;
743 if (FunctionTypeProto *FTP =
744 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
745 return QualType(FTP, 0);
746
747 // Determine whether the type being created is already canonical or not.
748 bool isCanonical = ResultTy->isCanonical();
749 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
750 if (!ArgArray[i]->isCanonical())
751 isCanonical = false;
752
753 // If this type isn't canonical, get the canonical version of it.
754 QualType Canonical;
755 if (!isCanonical) {
756 llvm::SmallVector<QualType, 16> CanonicalArgs;
757 CanonicalArgs.reserve(NumArgs);
758 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000759 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +0000760
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000761 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +0000762 &CanonicalArgs[0], NumArgs,
763 isVariadic);
764
765 // Get the new insert position for the node we care about.
766 FunctionTypeProto *NewIP =
767 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
768 assert(NewIP == 0 && "Shouldn't be in the map!");
769 }
770
771 // FunctionTypeProto objects are not allocated with new because they have a
772 // variable size array (for parameter types) at the end of them.
773 FunctionTypeProto *FTP =
774 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
775 NumArgs*sizeof(QualType));
776 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
777 Canonical);
778 Types.push_back(FTP);
779 FunctionTypeProtos.InsertNode(FTP, InsertPos);
780 return QualType(FTP, 0);
781}
782
Douglas Gregor1d661552008-04-13 21:07:44 +0000783/// getTypeDeclType - Return the unique reference to the type for the
784/// specified type declaration.
785QualType ASTContext::getTypeDeclType(TypeDecl *Decl) {
786 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
787
788 if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(Decl))
789 return getTypedefType(Typedef);
790 else if (ObjCInterfaceDecl *ObjCInterface
791 = dyn_cast_or_null<ObjCInterfaceDecl>(Decl))
792 return getObjCInterfaceType(ObjCInterface);
793 else if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Decl)) {
794 Decl->TypeForDecl = new RecordType(Record);
795 Types.push_back(Decl->TypeForDecl);
796 return QualType(Decl->TypeForDecl, 0);
797 } else if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Decl)) {
798 Decl->TypeForDecl = new EnumType(Enum);
799 Types.push_back(Decl->TypeForDecl);
800 return QualType(Decl->TypeForDecl, 0);
801 } else
802 assert(false && "TypeDecl without a type?");
803}
804
Chris Lattner4b009652007-07-25 00:24:17 +0000805/// getTypedefType - Return the unique reference to the type for the
806/// specified typename decl.
807QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
808 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
809
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000810 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000811 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000812 Types.push_back(Decl->TypeForDecl);
813 return QualType(Decl->TypeForDecl, 0);
814}
815
Ted Kremenek42730c52008-01-07 19:49:32 +0000816/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000817/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000818QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000819 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
820
Ted Kremenek42730c52008-01-07 19:49:32 +0000821 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000822 Types.push_back(Decl->TypeForDecl);
823 return QualType(Decl->TypeForDecl, 0);
824}
825
Chris Lattnere1352302008-04-07 04:56:42 +0000826/// CmpProtocolNames - Comparison predicate for sorting protocols
827/// alphabetically.
828static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
829 const ObjCProtocolDecl *RHS) {
830 return strcmp(LHS->getName(), RHS->getName()) < 0;
831}
832
833static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
834 unsigned &NumProtocols) {
835 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
836
837 // Sort protocols, keyed by name.
838 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
839
840 // Remove duplicates.
841 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
842 NumProtocols = ProtocolsEnd-Protocols;
843}
844
845
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +0000846/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
847/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000848QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
849 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000850 // Sort the protocol list alphabetically to canonicalize it.
851 SortAndUniqueProtocols(Protocols, NumProtocols);
852
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000853 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +0000854 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000855
856 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000857 if (ObjCQualifiedInterfaceType *QT =
858 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000859 return QualType(QT, 0);
860
861 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +0000862 ObjCQualifiedInterfaceType *QType =
863 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000864 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000865 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000866 return QualType(QType, 0);
867}
868
Chris Lattnere1352302008-04-07 04:56:42 +0000869/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
870/// and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000871QualType ASTContext::getObjCQualifiedIdType(QualType idType,
872 ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000873 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000874 // Sort the protocol list alphabetically to canonicalize it.
875 SortAndUniqueProtocols(Protocols, NumProtocols);
876
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000877 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000878 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000879
880 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000881 if (ObjCQualifiedIdType *QT =
882 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000883 return QualType(QT, 0);
884
885 // No Match;
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000886 QualType Canonical;
887 if (!idType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000888 Canonical = getObjCQualifiedIdType(getCanonicalType(idType),
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000889 Protocols, NumProtocols);
Ted Kremenek42730c52008-01-07 19:49:32 +0000890 ObjCQualifiedIdType *NewQT =
891 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000892 assert(NewQT == 0 && "Shouldn't be in the map!");
893 }
894
Ted Kremenek42730c52008-01-07 19:49:32 +0000895 ObjCQualifiedIdType *QType =
896 new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000897 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000898 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000899 return QualType(QType, 0);
900}
901
Steve Naroff0604dd92007-08-01 18:02:17 +0000902/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
903/// TypeOfExpr AST's (since expression's are never shared). For example,
904/// multiple declarations that refer to "typeof(x)" all contain different
905/// DeclRefExpr's. This doesn't effect the type checker, since it operates
906/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000907QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000908 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff0604dd92007-08-01 18:02:17 +0000909 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
910 Types.push_back(toe);
911 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000912}
913
Steve Naroff0604dd92007-08-01 18:02:17 +0000914/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
915/// TypeOfType AST's. The only motivation to unique these nodes would be
916/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
917/// an issue. This doesn't effect the type checker, since it operates
918/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000919QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000920 QualType Canonical = getCanonicalType(tofType);
Steve Naroff0604dd92007-08-01 18:02:17 +0000921 TypeOfType *tot = new TypeOfType(tofType, Canonical);
922 Types.push_back(tot);
923 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000924}
925
Chris Lattner4b009652007-07-25 00:24:17 +0000926/// getTagDeclType - Return the unique reference to the type for the
927/// specified TagDecl (struct/union/class/enum) decl.
928QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +0000929 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +0000930 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +0000931}
932
933/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
934/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
935/// needs to agree with the definition in <stddef.h>.
936QualType ASTContext::getSizeType() const {
937 // On Darwin, size_t is defined as a "long unsigned int".
938 // FIXME: should derive from "Target".
939 return UnsignedLongTy;
940}
941
Eli Friedmanfdd35d72008-02-12 08:29:21 +0000942/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
943/// width of characters in wide strings, The value is target dependent and
944/// needs to agree with the definition in <stddef.h>.
945QualType ASTContext::getWcharType() const {
946 // On Darwin, wchar_t is defined as a "int".
947 // FIXME: should derive from "Target".
948 return IntTy;
949}
950
Chris Lattner4b009652007-07-25 00:24:17 +0000951/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
952/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
953QualType ASTContext::getPointerDiffType() const {
954 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
955 // FIXME: should derive from "Target".
956 return IntTy;
957}
958
Chris Lattner19eb97e2008-04-02 05:18:44 +0000959//===----------------------------------------------------------------------===//
960// Type Operators
961//===----------------------------------------------------------------------===//
962
Chris Lattner3dae6f42008-04-06 22:41:35 +0000963/// getCanonicalType - Return the canonical (structural) type corresponding to
964/// the specified potentially non-canonical type. The non-canonical version
965/// of a type may have many "decorated" versions of types. Decorators can
966/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
967/// to be free of any of these, allowing two canonical types to be compared
968/// for exact equality with a simple pointer comparison.
969QualType ASTContext::getCanonicalType(QualType T) {
970 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
971 return QualType(CanType.getTypePtr(),
972 T.getCVRQualifiers() | CanType.getCVRQualifiers());
973}
974
975
Chris Lattner19eb97e2008-04-02 05:18:44 +0000976/// getArrayDecayedType - Return the properly qualified result of decaying the
977/// specified array type to a pointer. This operation is non-trivial when
978/// handling typedefs etc. The canonical type of "T" must be an array type,
979/// this returns a pointer to a properly qualified element of the array.
980///
981/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
982QualType ASTContext::getArrayDecayedType(QualType Ty) {
983 // Handle the common case where typedefs are not involved directly.
984 QualType EltTy;
985 unsigned ArrayQuals = 0;
986 unsigned PointerQuals = 0;
987 if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
988 // Since T "isa" an array type, it could not have had an address space
989 // qualifier, just CVR qualifiers. The properly qualified element pointer
990 // gets the union of the CVR qualifiers from the element and the array, and
991 // keeps any address space qualifier on the element type if present.
992 EltTy = AT->getElementType();
993 ArrayQuals = Ty.getCVRQualifiers();
994 PointerQuals = AT->getIndexTypeQualifier();
995 } else {
996 // Otherwise, we have an ASQualType or a typedef, etc. Make sure we don't
997 // lose qualifiers when dealing with typedefs. Example:
998 // typedef int arr[10];
999 // void test2() {
1000 // const arr b;
1001 // b[4] = 1;
1002 // }
1003 //
1004 // The decayed type of b is "const int*" even though the element type of the
1005 // array is "int".
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001006 QualType CanTy = getCanonicalType(Ty);
Chris Lattner19eb97e2008-04-02 05:18:44 +00001007 const ArrayType *PrettyArrayType = Ty->getAsArrayType();
1008 assert(PrettyArrayType && "Not an array type!");
1009
1010 // Get the element type with 'getAsArrayType' so that we don't lose any
1011 // typedefs in the element type of the array.
1012 EltTy = PrettyArrayType->getElementType();
1013
1014 // If the array was address-space qualifier, make sure to ASQual the element
1015 // type. We can just grab the address space from the canonical type.
1016 if (unsigned AS = CanTy.getAddressSpace())
1017 EltTy = getASQualType(EltTy, AS);
1018
1019 // To properly handle [multiple levels of] typedefs, typeof's etc, we take
1020 // the CVR qualifiers directly from the canonical type, which is guaranteed
1021 // to have the full set unioned together.
1022 ArrayQuals = CanTy.getCVRQualifiers();
1023 PointerQuals = PrettyArrayType->getIndexTypeQualifier();
1024 }
1025
Chris Lattnerda79b3f2008-04-02 06:06:35 +00001026 // Apply any CVR qualifiers from the array type to the element type. This
1027 // implements C99 6.7.3p8: "If the specification of an array type includes
1028 // any type qualifiers, the element type is so qualified, not the array type."
Chris Lattner19eb97e2008-04-02 05:18:44 +00001029 EltTy = EltTy.getQualifiedType(ArrayQuals | EltTy.getCVRQualifiers());
1030
1031 QualType PtrTy = getPointerType(EltTy);
1032
1033 // int x[restrict 4] -> int *restrict
1034 PtrTy = PtrTy.getQualifiedType(PointerQuals);
1035
1036 return PtrTy;
1037}
1038
Chris Lattner4b009652007-07-25 00:24:17 +00001039/// getFloatingRank - Return a relative rank for floating point types.
1040/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001041static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001042 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001043 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001044
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001045 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001046 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001047 case BuiltinType::Float: return FloatRank;
1048 case BuiltinType::Double: return DoubleRank;
1049 case BuiltinType::LongDouble: return LongDoubleRank;
1050 }
1051}
1052
Steve Narofffa0c4532007-08-27 01:41:48 +00001053/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1054/// point or a complex type (based on typeDomain/typeSize).
1055/// 'typeDomain' is a real floating point or complex type.
1056/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001057QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1058 QualType Domain) const {
1059 FloatingRank EltRank = getFloatingRank(Size);
1060 if (Domain->isComplexType()) {
1061 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001062 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001063 case FloatRank: return FloatComplexTy;
1064 case DoubleRank: return DoubleComplexTy;
1065 case LongDoubleRank: return LongDoubleComplexTy;
1066 }
Chris Lattner4b009652007-07-25 00:24:17 +00001067 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001068
1069 assert(Domain->isRealFloatingType() && "Unknown domain!");
1070 switch (EltRank) {
1071 default: assert(0 && "getFloatingRank(): illegal value for rank");
1072 case FloatRank: return FloatTy;
1073 case DoubleRank: return DoubleTy;
1074 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001075 }
Chris Lattner4b009652007-07-25 00:24:17 +00001076}
1077
Chris Lattner51285d82008-04-06 23:55:33 +00001078/// getFloatingTypeOrder - Compare the rank of the two specified floating
1079/// point types, ignoring the domain of the type (i.e. 'double' ==
1080/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1081/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001082int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1083 FloatingRank LHSR = getFloatingRank(LHS);
1084 FloatingRank RHSR = getFloatingRank(RHS);
1085
1086 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001087 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001088 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001089 return 1;
1090 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001091}
1092
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001093/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1094/// routine will assert if passed a built-in type that isn't an integer or enum,
1095/// or if it is not canonicalized.
1096static unsigned getIntegerRank(Type *T) {
1097 assert(T->isCanonical() && "T should be canonicalized");
1098 if (isa<EnumType>(T))
1099 return 4;
1100
1101 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001102 default: assert(0 && "getIntegerRank(): not a built-in integer");
1103 case BuiltinType::Bool:
1104 return 1;
1105 case BuiltinType::Char_S:
1106 case BuiltinType::Char_U:
1107 case BuiltinType::SChar:
1108 case BuiltinType::UChar:
1109 return 2;
1110 case BuiltinType::Short:
1111 case BuiltinType::UShort:
1112 return 3;
1113 case BuiltinType::Int:
1114 case BuiltinType::UInt:
1115 return 4;
1116 case BuiltinType::Long:
1117 case BuiltinType::ULong:
1118 return 5;
1119 case BuiltinType::LongLong:
1120 case BuiltinType::ULongLong:
1121 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001122 }
1123}
1124
Chris Lattner51285d82008-04-06 23:55:33 +00001125/// getIntegerTypeOrder - Returns the highest ranked integer type:
1126/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1127/// LHS < RHS, return -1.
1128int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001129 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1130 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001131 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001132
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001133 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1134 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001135
Chris Lattner51285d82008-04-06 23:55:33 +00001136 unsigned LHSRank = getIntegerRank(LHSC);
1137 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001138
Chris Lattner51285d82008-04-06 23:55:33 +00001139 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1140 if (LHSRank == RHSRank) return 0;
1141 return LHSRank > RHSRank ? 1 : -1;
1142 }
Chris Lattner4b009652007-07-25 00:24:17 +00001143
Chris Lattner51285d82008-04-06 23:55:33 +00001144 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1145 if (LHSUnsigned) {
1146 // If the unsigned [LHS] type is larger, return it.
1147 if (LHSRank >= RHSRank)
1148 return 1;
1149
1150 // If the signed type can represent all values of the unsigned type, it
1151 // wins. Because we are dealing with 2's complement and types that are
1152 // powers of two larger than each other, this is always safe.
1153 return -1;
1154 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001155
Chris Lattner51285d82008-04-06 23:55:33 +00001156 // If the unsigned [RHS] type is larger, return it.
1157 if (RHSRank >= LHSRank)
1158 return -1;
1159
1160 // If the signed type can represent all values of the unsigned type, it
1161 // wins. Because we are dealing with 2's complement and types that are
1162 // powers of two larger than each other, this is always safe.
1163 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001164}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001165
1166// getCFConstantStringType - Return the type used for constant CFStrings.
1167QualType ASTContext::getCFConstantStringType() {
1168 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001169 CFConstantStringTypeDecl =
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001170 RecordDecl::Create(*this, Decl::Struct, TUDecl, SourceLocation(),
Chris Lattner58114f02008-03-15 21:32:50 +00001171 &Idents.get("NSConstantString"), 0);
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001172 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001173
1174 // const int *isa;
1175 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001176 // int flags;
1177 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001178 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001179 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001180 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001181 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001182 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001183 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001184
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001185 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerf3874bc2008-04-06 04:47:34 +00001186 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner81db64a2008-03-16 00:16:02 +00001187 FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001188
1189 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1190 }
1191
1192 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001193}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001194
Anders Carlssone3f02572007-10-29 06:33:42 +00001195// This returns true if a type has been typedefed to BOOL:
1196// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001197static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001198 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001199 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001200
1201 return false;
1202}
1203
Ted Kremenek42730c52008-01-07 19:49:32 +00001204/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001205/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001206int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001207 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001208
1209 // Make all integer and enum types at least as large as an int
1210 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001211 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001212 // Treat arrays as pointers, since that's how they're passed in.
1213 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001214 sz = getTypeSize(VoidPtrTy);
1215 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001216}
1217
Ted Kremenek42730c52008-01-07 19:49:32 +00001218/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001219/// declaration.
Ted Kremenek42730c52008-01-07 19:49:32 +00001220void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001221 std::string& S)
1222{
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001223 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001224 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001225 // Encode result type.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001226 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001227 // Compute size of all parameters.
1228 // Start with computing size of a pointer in number of bytes.
1229 // FIXME: There might(should) be a better way of doing this computation!
1230 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001231 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001232 // The first two arguments (self and _cmd) are pointers; account for
1233 // their size.
1234 int ParmOffset = 2 * PtrSize;
1235 int NumOfParams = Decl->getNumParams();
1236 for (int i = 0; i < NumOfParams; i++) {
1237 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001238 int sz = getObjCEncodingTypeSize (PType);
1239 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001240 ParmOffset += sz;
1241 }
1242 S += llvm::utostr(ParmOffset);
1243 S += "@0:";
1244 S += llvm::utostr(PtrSize);
1245
1246 // Argument types.
1247 ParmOffset = 2 * PtrSize;
1248 for (int i = 0; i < NumOfParams; i++) {
1249 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001250 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001251 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001252 getObjCEncodingForTypeQualifier(
1253 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian248db262008-01-22 22:44:46 +00001254 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001255 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001256 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001257 }
1258}
1259
Fariborz Jahanian248db262008-01-22 22:44:46 +00001260void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1261 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson36f07d82007-10-29 05:01:08 +00001262{
Anders Carlssone3f02572007-10-29 06:33:42 +00001263 // FIXME: This currently doesn't encode:
1264 // @ An object (whether statically typed or typed id)
1265 // # A class object (Class)
1266 // : A method selector (SEL)
1267 // {name=type...} A structure
1268 // (name=type...) A union
1269 // bnum A bit field of num bits
1270
1271 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001272 char encoding;
1273 switch (BT->getKind()) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001274 default: assert(0 && "Unhandled builtin type kind");
1275 case BuiltinType::Void: encoding = 'v'; break;
1276 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001277 case BuiltinType::Char_U:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001278 case BuiltinType::UChar: encoding = 'C'; break;
1279 case BuiltinType::UShort: encoding = 'S'; break;
1280 case BuiltinType::UInt: encoding = 'I'; break;
1281 case BuiltinType::ULong: encoding = 'L'; break;
1282 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001283 case BuiltinType::Char_S:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001284 case BuiltinType::SChar: encoding = 'c'; break;
1285 case BuiltinType::Short: encoding = 's'; break;
1286 case BuiltinType::Int: encoding = 'i'; break;
1287 case BuiltinType::Long: encoding = 'l'; break;
1288 case BuiltinType::LongLong: encoding = 'q'; break;
1289 case BuiltinType::Float: encoding = 'f'; break;
1290 case BuiltinType::Double: encoding = 'd'; break;
1291 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001292 }
1293
1294 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001295 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001296 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001297 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001298 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001299
1300 }
1301 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001302 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001303 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001304 S += '@';
1305 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001306 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001307 S += '#';
1308 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001309 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001310 S += ':';
1311 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001312 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001313
1314 if (PointeeTy->isCharType()) {
1315 // char pointer types should be encoded as '*' unless it is a
1316 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001317 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001318 S += '*';
1319 return;
1320 }
1321 }
1322
1323 S += '^';
Fariborz Jahanian248db262008-01-22 22:44:46 +00001324 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone3f02572007-10-29 06:33:42 +00001325 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001326 S += '[';
1327
1328 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1329 S += llvm::utostr(CAT->getSize().getZExtValue());
1330 else
1331 assert(0 && "Unhandled array type!");
1332
Fariborz Jahanian248db262008-01-22 22:44:46 +00001333 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001334 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001335 } else if (T->getAsFunctionType()) {
1336 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001337 } else if (const RecordType *RTy = T->getAsRecordType()) {
1338 RecordDecl *RDecl= RTy->getDecl();
1339 S += '{';
1340 S += RDecl->getName();
Fariborz Jahanian248db262008-01-22 22:44:46 +00001341 bool found = false;
1342 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1343 if (ERType[i] == RTy) {
1344 found = true;
1345 break;
1346 }
1347 if (!found) {
1348 ERType.push_back(RTy);
1349 S += '=';
1350 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1351 FieldDecl *field = RDecl->getMember(i);
1352 getObjCEncodingForType(field->getType(), S, ERType);
1353 }
1354 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1355 ERType.pop_back();
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001356 }
1357 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001358 } else if (T->isEnumeralType()) {
1359 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001360 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001361 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001362}
1363
Ted Kremenek42730c52008-01-07 19:49:32 +00001364void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001365 std::string& S) const {
1366 if (QT & Decl::OBJC_TQ_In)
1367 S += 'n';
1368 if (QT & Decl::OBJC_TQ_Inout)
1369 S += 'N';
1370 if (QT & Decl::OBJC_TQ_Out)
1371 S += 'o';
1372 if (QT & Decl::OBJC_TQ_Bycopy)
1373 S += 'O';
1374 if (QT & Decl::OBJC_TQ_Byref)
1375 S += 'R';
1376 if (QT & Decl::OBJC_TQ_Oneway)
1377 S += 'V';
1378}
1379
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001380void ASTContext::setBuiltinVaListType(QualType T)
1381{
1382 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1383
1384 BuiltinVaListType = T;
1385}
1386
Ted Kremenek42730c52008-01-07 19:49:32 +00001387void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001388{
Ted Kremenek42730c52008-01-07 19:49:32 +00001389 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff9d12c902007-10-15 14:41:52 +00001390
Ted Kremenek42730c52008-01-07 19:49:32 +00001391 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001392
1393 // typedef struct objc_object *id;
1394 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1395 assert(ptr && "'id' incorrectly typed");
1396 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1397 assert(rec && "'id' incorrectly typed");
1398 IdStructType = rec;
1399}
1400
Ted Kremenek42730c52008-01-07 19:49:32 +00001401void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001402{
Ted Kremenek42730c52008-01-07 19:49:32 +00001403 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001404
Ted Kremenek42730c52008-01-07 19:49:32 +00001405 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001406
1407 // typedef struct objc_selector *SEL;
1408 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1409 assert(ptr && "'SEL' incorrectly typed");
1410 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1411 assert(rec && "'SEL' incorrectly typed");
1412 SelStructType = rec;
1413}
1414
Ted Kremenek42730c52008-01-07 19:49:32 +00001415void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001416{
Ted Kremenek42730c52008-01-07 19:49:32 +00001417 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1418 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001419}
1420
Ted Kremenek42730c52008-01-07 19:49:32 +00001421void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001422{
Ted Kremenek42730c52008-01-07 19:49:32 +00001423 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001424
Ted Kremenek42730c52008-01-07 19:49:32 +00001425 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001426
1427 // typedef struct objc_class *Class;
1428 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1429 assert(ptr && "'Class' incorrectly typed");
1430 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1431 assert(rec && "'Class' incorrectly typed");
1432 ClassStructType = rec;
1433}
1434
Ted Kremenek42730c52008-01-07 19:49:32 +00001435void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1436 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001437 "'NSConstantString' type already set!");
1438
Ted Kremenek42730c52008-01-07 19:49:32 +00001439 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001440}
1441
Chris Lattner6ff358b2008-04-07 06:51:04 +00001442//===----------------------------------------------------------------------===//
1443// Type Compatibility Testing
1444//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00001445
Chris Lattner390564e2008-04-07 06:49:41 +00001446/// C99 6.2.7p1: If both are complete types, then the following additional
1447/// requirements apply.
1448/// FIXME (handle compatibility across source files).
1449static bool areCompatTagTypes(TagType *LHS, TagType *RHS,
1450 const ASTContext &C) {
Steve Naroff4a5e2072007-11-07 06:03:51 +00001451 // "Class" and "id" are compatible built-in structure types.
Chris Lattner390564e2008-04-07 06:49:41 +00001452 if (C.isObjCIdType(QualType(LHS, 0)) && C.isObjCClassType(QualType(RHS, 0)) ||
1453 C.isObjCClassType(QualType(LHS, 0)) && C.isObjCIdType(QualType(RHS, 0)))
Steve Naroff4a5e2072007-11-07 06:03:51 +00001454 return true;
Eli Friedmane7fb03a2008-02-15 06:03:44 +00001455
Chris Lattner390564e2008-04-07 06:49:41 +00001456 // Within a translation unit a tag type is only compatible with itself. Self
1457 // equality is already handled by the time we get here.
1458 assert(LHS != RHS && "Self equality not handled!");
1459 return false;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001460}
1461
1462bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1463 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1464 // identically qualified and both shall be pointers to compatible types.
Chris Lattner35fef522008-02-20 20:55:12 +00001465 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1466 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001467 return false;
1468
1469 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1470 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1471
1472 return typesAreCompatible(ltype, rtype);
1473}
1474
Steve Naroff85f0dc52007-10-15 20:41:53 +00001475bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1476 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1477 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1478 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1479 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1480
1481 // first check the return types (common between C99 and K&R).
1482 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1483 return false;
1484
1485 if (lproto && rproto) { // two C99 style function prototypes
1486 unsigned lproto_nargs = lproto->getNumArgs();
1487 unsigned rproto_nargs = rproto->getNumArgs();
1488
1489 if (lproto_nargs != rproto_nargs)
1490 return false;
1491
1492 // both prototypes have the same number of arguments.
1493 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1494 (rproto->isVariadic() && !lproto->isVariadic()))
1495 return false;
1496
1497 // The use of ellipsis agree...now check the argument types.
1498 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001499 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1500 // is taken as having the unqualified version of it's declared type.
Steve Naroffdec17fe2008-01-29 00:15:50 +00001501 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001502 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001503 return false;
1504 return true;
1505 }
Chris Lattner1d78a862008-04-07 07:01:58 +00001506
Steve Naroff85f0dc52007-10-15 20:41:53 +00001507 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1508 return true;
1509
1510 // we have a mixture of K&R style with C99 prototypes
1511 const FunctionTypeProto *proto = lproto ? lproto : rproto;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001512 if (proto->isVariadic())
1513 return false;
1514
1515 // FIXME: Each parameter type T in the prototype must be compatible with the
1516 // type resulting from applying the usual argument conversions to T.
1517 return true;
1518}
1519
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001520// C99 6.7.5.2p6
1521static bool areCompatArrayTypes(ArrayType *LHS, ArrayType *RHS, ASTContext &C) {
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001522 // Constant arrays must be the same size to be compatible.
1523 if (const ConstantArrayType* LCAT = dyn_cast<ConstantArrayType>(LHS))
1524 if (const ConstantArrayType* RCAT = dyn_cast<ConstantArrayType>(RHS))
1525 if (RCAT->getSize() != LCAT->getSize())
1526 return false;
Eli Friedman1e7537832008-02-06 04:53:22 +00001527
Chris Lattnerc8971d72008-04-07 06:58:21 +00001528 // Compatible arrays must have compatible element types
1529 return C.typesAreCompatible(LHS->getElementType(), RHS->getElementType());
Steve Naroff85f0dc52007-10-15 20:41:53 +00001530}
1531
Chris Lattner6ff358b2008-04-07 06:51:04 +00001532/// areCompatVectorTypes - Return true if the two specified vector types are
1533/// compatible.
1534static bool areCompatVectorTypes(const VectorType *LHS,
1535 const VectorType *RHS) {
1536 assert(LHS->isCanonical() && RHS->isCanonical());
1537 return LHS->getElementType() == RHS->getElementType() &&
1538 LHS->getNumElements() == RHS->getNumElements();
1539}
1540
1541/// areCompatObjCInterfaces - Return true if the two interface types are
1542/// compatible for assignment from RHS to LHS. This handles validation of any
1543/// protocol qualifiers on the LHS or RHS.
1544///
Chris Lattner1d78a862008-04-07 07:01:58 +00001545static bool areCompatObjCInterfaces(const ObjCInterfaceType *LHS,
1546 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00001547 // Verify that the base decls are compatible: the RHS must be a subclass of
1548 // the LHS.
1549 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1550 return false;
1551
1552 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1553 // protocol qualified at all, then we are good.
1554 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1555 return true;
1556
1557 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1558 // isn't a superset.
1559 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1560 return true; // FIXME: should return false!
1561
1562 // Finally, we must have two protocol-qualified interfaces.
1563 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1564 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1565 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1566 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1567 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1568 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1569
1570 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1571 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1572 // LHS in a single parallel scan until we run out of elements in LHS.
1573 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1574 ObjCProtocolDecl *LHSProto = *LHSPI;
1575
1576 while (RHSPI != RHSPE) {
1577 ObjCProtocolDecl *RHSProto = *RHSPI++;
1578 // If the RHS has a protocol that the LHS doesn't, ignore it.
1579 if (RHSProto != LHSProto)
1580 continue;
1581
1582 // Otherwise, the RHS does have this element.
1583 ++LHSPI;
1584 if (LHSPI == LHSPE)
1585 return true; // All protocols in LHS exist in RHS.
1586
1587 LHSProto = *LHSPI;
1588 }
1589
1590 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1591 return false;
1592}
1593
1594
Steve Naroff85f0dc52007-10-15 20:41:53 +00001595/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1596/// both shall have the identically qualified version of a compatible type.
1597/// C99 6.2.7p1: Two types have compatible types if their types are the
1598/// same. See 6.7.[2,3,5] for additional rules.
Chris Lattner855fed42008-04-07 04:07:56 +00001599bool ASTContext::typesAreCompatible(QualType LHS_NC, QualType RHS_NC) {
1600 QualType LHS = LHS_NC.getCanonicalType();
1601 QualType RHS = RHS_NC.getCanonicalType();
Chris Lattner4d5670b2008-04-03 05:07:04 +00001602
Bill Wendling6a9d8542007-12-03 07:33:35 +00001603 // C++ [expr]: If an expression initially has the type "reference to T", the
1604 // type is adjusted to "T" prior to any further analysis, the expression
1605 // designates the object or function denoted by the reference, and the
1606 // expression is an lvalue.
Chris Lattner855fed42008-04-07 04:07:56 +00001607 if (ReferenceType *RT = dyn_cast<ReferenceType>(LHS))
1608 LHS = RT->getPointeeType();
1609 if (ReferenceType *RT = dyn_cast<ReferenceType>(RHS))
1610 RHS = RT->getPointeeType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001611
Chris Lattnerd47d6042008-04-07 05:37:56 +00001612 // If two types are identical, they are compatible.
1613 if (LHS == RHS)
1614 return true;
1615
1616 // If qualifiers differ, the types are different.
Chris Lattnerb5709e22008-04-07 05:43:21 +00001617 unsigned LHSAS = LHS.getAddressSpace(), RHSAS = RHS.getAddressSpace();
1618 if (LHS.getCVRQualifiers() != RHS.getCVRQualifiers() || LHSAS != RHSAS)
Chris Lattnerd47d6042008-04-07 05:37:56 +00001619 return false;
Chris Lattnerb5709e22008-04-07 05:43:21 +00001620
1621 // Strip off ASQual's if present.
1622 if (LHSAS) {
1623 LHS = LHS.getUnqualifiedType();
1624 RHS = RHS.getUnqualifiedType();
1625 }
Chris Lattnerd47d6042008-04-07 05:37:56 +00001626
Chris Lattner855fed42008-04-07 04:07:56 +00001627 Type::TypeClass LHSClass = LHS->getTypeClass();
1628 Type::TypeClass RHSClass = RHS->getTypeClass();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001629
1630 // We want to consider the two function types to be the same for these
1631 // comparisons, just force one to the other.
1632 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1633 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00001634
1635 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00001636 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
1637 LHSClass = Type::ConstantArray;
1638 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
1639 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001640
Nate Begemanaf6ed502008-04-18 23:10:10 +00001641 // Canonicalize ExtVector -> Vector.
1642 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
1643 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00001644
Chris Lattner7cdcb252008-04-07 06:38:24 +00001645 // Consider qualified interfaces and interfaces the same.
1646 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
1647 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
1648
Chris Lattnerb5709e22008-04-07 05:43:21 +00001649 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001650 if (LHSClass != RHSClass) {
Chris Lattner7cdcb252008-04-07 06:38:24 +00001651 // ID is compatible with all interface types.
1652 if (isa<ObjCInterfaceType>(LHS))
1653 return isObjCIdType(RHS);
1654 if (isa<ObjCInterfaceType>(RHS))
1655 return isObjCIdType(LHS);
Chris Lattner0d3e6452008-04-07 05:53:18 +00001656
Chris Lattnerc38d4522008-01-14 05:45:46 +00001657 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1658 // a signed integer type, or an unsigned integer type.
Chris Lattner855fed42008-04-07 04:07:56 +00001659 if (LHS->isEnumeralType() && RHS->isIntegralType()) {
1660 EnumDecl* EDecl = cast<EnumType>(LHS)->getDecl();
1661 return EDecl->getIntegerType() == RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001662 }
Chris Lattner855fed42008-04-07 04:07:56 +00001663 if (RHS->isEnumeralType() && LHS->isIntegralType()) {
1664 EnumDecl* EDecl = cast<EnumType>(RHS)->getDecl();
1665 return EDecl->getIntegerType() == LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001666 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001667
Steve Naroff85f0dc52007-10-15 20:41:53 +00001668 return false;
1669 }
Chris Lattnerb5709e22008-04-07 05:43:21 +00001670
Steve Naroffc88babe2008-01-09 22:43:08 +00001671 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001672 switch (LHSClass) {
Chris Lattnerb5709e22008-04-07 05:43:21 +00001673 case Type::ASQual:
1674 case Type::FunctionProto:
1675 case Type::VariableArray:
1676 case Type::IncompleteArray:
1677 case Type::Reference:
Chris Lattner7cdcb252008-04-07 06:38:24 +00001678 case Type::ObjCQualifiedInterface:
Chris Lattnerb5709e22008-04-07 05:43:21 +00001679 assert(0 && "Canonicalized away above");
Chris Lattnerc38d4522008-01-14 05:45:46 +00001680 case Type::Pointer:
Chris Lattner855fed42008-04-07 04:07:56 +00001681 return pointerTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001682 case Type::ConstantArray:
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001683 return areCompatArrayTypes(cast<ArrayType>(LHS), cast<ArrayType>(RHS),
1684 *this);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001685 case Type::FunctionNoProto:
Chris Lattner855fed42008-04-07 04:07:56 +00001686 return functionTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001687 case Type::Tagged: // handle structures, unions
Chris Lattner390564e2008-04-07 06:49:41 +00001688 return areCompatTagTypes(cast<TagType>(LHS), cast<TagType>(RHS), *this);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001689 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00001690 // Only exactly equal builtin types are compatible, which is tested above.
1691 return false;
1692 case Type::Vector:
1693 return areCompatVectorTypes(cast<VectorType>(LHS), cast<VectorType>(RHS));
Chris Lattnerc38d4522008-01-14 05:45:46 +00001694 case Type::ObjCInterface:
Chris Lattner7cdcb252008-04-07 06:38:24 +00001695 return areCompatObjCInterfaces(cast<ObjCInterfaceType>(LHS),
1696 cast<ObjCInterfaceType>(RHS));
Chris Lattnerc38d4522008-01-14 05:45:46 +00001697 default:
1698 assert(0 && "unexpected type");
Steve Naroff85f0dc52007-10-15 20:41:53 +00001699 }
1700 return true; // should never get here...
1701}
Ted Kremenek738e6c02007-10-31 17:10:13 +00001702
Chris Lattner1d78a862008-04-07 07:01:58 +00001703//===----------------------------------------------------------------------===//
1704// Serialization Support
1705//===----------------------------------------------------------------------===//
1706
Ted Kremenek738e6c02007-10-31 17:10:13 +00001707/// Emit - Serialize an ASTContext object to Bitcode.
1708void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00001709 S.EmitRef(SourceMgr);
1710 S.EmitRef(Target);
1711 S.EmitRef(Idents);
1712 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001713
Ted Kremenek68228a92007-10-31 22:44:07 +00001714 // Emit the size of the type vector so that we can reserve that size
1715 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001716 S.EmitInt(Types.size());
1717
Ted Kremenek034a78c2007-11-13 22:02:55 +00001718 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1719 I!=E;++I)
1720 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001721
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001722 S.EmitOwnedPtr(TUDecl);
1723
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001724 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001725}
1726
Ted Kremenekacba3612007-11-13 00:25:37 +00001727ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek68228a92007-10-31 22:44:07 +00001728 SourceManager &SM = D.ReadRef<SourceManager>();
1729 TargetInfo &t = D.ReadRef<TargetInfo>();
1730 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1731 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00001732
Ted Kremenek68228a92007-10-31 22:44:07 +00001733 unsigned size_reserve = D.ReadInt();
1734
1735 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1736
Ted Kremenek034a78c2007-11-13 22:02:55 +00001737 for (unsigned i = 0; i < size_reserve; ++i)
1738 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00001739
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001740 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
1741
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001742 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00001743
1744 return A;
1745}