blob: 8fd088a305f13407298116369d1a4840b42bb79a [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 {
Anders Carlsson058237f2008-02-18 07:13:09 +0000357 // FIXME: This is X86 specific, use 32-bit alignment for long long.
358 if (FD->getType()->isIntegerType() && TypeInfo.second > 32)
359 FieldAlign = 32;
360 else
361 FieldAlign = TypeInfo.second;
Anders Carlsson7dce0292008-02-16 19:51:27 +0000362 }
Eli Friedman67571ac2008-02-06 05:33:51 +0000363
Anders Carlsson058237f2008-02-18 07:13:09 +0000364 // Check if we need to add padding to give the field the correct
365 // alignment.
366 if (RecordSize % FieldAlign + FieldSize > TypeSize)
367 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
368
369 } else {
370 if (FD->getType()->isIncompleteType()) {
371 // This must be a flexible array member; we can't directly
372 // query getTypeInfo about these, so we figure it out here.
373 // Flexible array members don't have any size, but they
374 // have to be aligned appropriately for their element type.
375
376 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
377 FieldAlign = AA->getAlignment();
378 else if (FieldIsPacked)
379 FieldAlign = 8;
380 else {
381 const ArrayType* ATy = FD->getType()->getAsArrayType();
Chris Lattner8cd0e932008-03-05 18:54:05 +0000382 FieldAlign = getTypeAlign(ATy->getElementType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000383 }
384 FieldSize = 0;
385 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000386 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000387 FieldSize = FieldInfo.first;
388
389 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
390 FieldAlign = AA->getAlignment();
391 else if (FieldIsPacked)
392 FieldAlign = 8;
393 else
394 FieldAlign = FieldInfo.second;
395 }
396
397 // Round up the current record size to the field's alignment boundary.
398 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
399 }
Chris Lattner4b009652007-07-25 00:24:17 +0000400
401 // Place this field at the current location.
402 FieldOffsets[i] = RecordSize;
403
404 // Reserve space for this field.
405 RecordSize += FieldSize;
406
407 // Remember max struct/class alignment.
408 RecordAlign = std::max(RecordAlign, FieldAlign);
409 }
410
411 // Finally, round the size of the total struct up to the alignment of the
412 // struct itself.
413 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
414 } else {
415 // Union layout just puts each member at the start of the record.
416 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
417 const FieldDecl *FD = D->getMember(i);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000418 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Chris Lattner4b009652007-07-25 00:24:17 +0000419 uint64_t FieldSize = FieldInfo.first;
420 unsigned FieldAlign = FieldInfo.second;
421
Anders Carlsson058237f2008-02-18 07:13:09 +0000422 // FIXME: This is X86 specific, use 32-bit alignment for long long.
423 if (FD->getType()->isIntegerType() && FieldAlign > 32)
424 FieldAlign = 32;
425
Chris Lattner4b009652007-07-25 00:24:17 +0000426 // Round up the current record size to the field's alignment boundary.
427 RecordSize = std::max(RecordSize, FieldSize);
428
429 // Place this field at the start of the record.
430 FieldOffsets[i] = 0;
431
432 // Remember max struct/class alignment.
433 RecordAlign = std::max(RecordAlign, FieldAlign);
434 }
435 }
436
437 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
438 return *NewEntry;
439}
440
Chris Lattner4b009652007-07-25 00:24:17 +0000441//===----------------------------------------------------------------------===//
442// Type creation/memoization methods
443//===----------------------------------------------------------------------===//
444
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000445QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000446 QualType CanT = getCanonicalType(T);
447 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000448 return T;
449
450 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
451 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000452 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000453 "Type is already address space qualified");
454
455 // Check if we've already instantiated an address space qual'd type of this
456 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000457 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000458 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000459 void *InsertPos = 0;
460 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
461 return QualType(ASQy, 0);
462
463 // If the base type isn't canonical, this won't be a canonical type either,
464 // so fill in the canonical type field.
465 QualType Canonical;
466 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000467 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000468
469 // Get the new insert position for the node we care about.
470 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
471 assert(NewIP == 0 && "Shouldn't be in the map!");
472 }
Chris Lattner35fef522008-02-20 20:55:12 +0000473 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000474 ASQualTypes.InsertNode(New, InsertPos);
475 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000476 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000477}
478
Chris Lattner4b009652007-07-25 00:24:17 +0000479
480/// getComplexType - Return the uniqued reference to the type for a complex
481/// number with the specified element type.
482QualType ASTContext::getComplexType(QualType T) {
483 // Unique pointers, to guarantee there is only one pointer of a particular
484 // structure.
485 llvm::FoldingSetNodeID ID;
486 ComplexType::Profile(ID, T);
487
488 void *InsertPos = 0;
489 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
490 return QualType(CT, 0);
491
492 // If the pointee type isn't canonical, this won't be a canonical type either,
493 // so fill in the canonical type field.
494 QualType Canonical;
495 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000496 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000497
498 // Get the new insert position for the node we care about.
499 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
500 assert(NewIP == 0 && "Shouldn't be in the map!");
501 }
502 ComplexType *New = new ComplexType(T, Canonical);
503 Types.push_back(New);
504 ComplexTypes.InsertNode(New, InsertPos);
505 return QualType(New, 0);
506}
507
508
509/// getPointerType - Return the uniqued reference to the type for a pointer to
510/// the specified type.
511QualType ASTContext::getPointerType(QualType T) {
512 // Unique pointers, to guarantee there is only one pointer of a particular
513 // structure.
514 llvm::FoldingSetNodeID ID;
515 PointerType::Profile(ID, T);
516
517 void *InsertPos = 0;
518 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
519 return QualType(PT, 0);
520
521 // If the pointee type isn't canonical, this won't be a canonical type either,
522 // so fill in the canonical type field.
523 QualType Canonical;
524 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000525 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000526
527 // Get the new insert position for the node we care about.
528 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
529 assert(NewIP == 0 && "Shouldn't be in the map!");
530 }
531 PointerType *New = new PointerType(T, Canonical);
532 Types.push_back(New);
533 PointerTypes.InsertNode(New, InsertPos);
534 return QualType(New, 0);
535}
536
537/// getReferenceType - Return the uniqued reference to the type for a reference
538/// to the specified type.
539QualType ASTContext::getReferenceType(QualType T) {
540 // Unique pointers, to guarantee there is only one pointer of a particular
541 // structure.
542 llvm::FoldingSetNodeID ID;
543 ReferenceType::Profile(ID, T);
544
545 void *InsertPos = 0;
546 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
547 return QualType(RT, 0);
548
549 // If the referencee type isn't canonical, this won't be a canonical type
550 // either, so fill in the canonical type field.
551 QualType Canonical;
552 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000553 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000554
555 // Get the new insert position for the node we care about.
556 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
557 assert(NewIP == 0 && "Shouldn't be in the map!");
558 }
559
560 ReferenceType *New = new ReferenceType(T, Canonical);
561 Types.push_back(New);
562 ReferenceTypes.InsertNode(New, InsertPos);
563 return QualType(New, 0);
564}
565
Steve Naroff83c13012007-08-30 01:06:46 +0000566/// getConstantArrayType - Return the unique reference to the type for an
567/// array of the specified element type.
568QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000569 const llvm::APInt &ArySize,
570 ArrayType::ArraySizeModifier ASM,
571 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000572 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000573 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000574
575 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000576 if (ConstantArrayType *ATP =
577 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000578 return QualType(ATP, 0);
579
580 // If the element type isn't canonical, this won't be a canonical type either,
581 // so fill in the canonical type field.
582 QualType Canonical;
583 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000584 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000585 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000586 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000587 ConstantArrayType *NewIP =
588 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
589
Chris Lattner4b009652007-07-25 00:24:17 +0000590 assert(NewIP == 0 && "Shouldn't be in the map!");
591 }
592
Steve Naroff24c9b982007-08-30 18:10:14 +0000593 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
594 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000595 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000596 Types.push_back(New);
597 return QualType(New, 0);
598}
599
Steve Naroffe2579e32007-08-30 18:14:25 +0000600/// getVariableArrayType - Returns a non-unique reference to the type for a
601/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000602QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
603 ArrayType::ArraySizeModifier ASM,
604 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000605 // Since we don't unique expressions, it isn't possible to unique VLA's
606 // that have an expression provided for their size.
607
608 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
609 ASM, EltTypeQuals);
610
611 VariableArrayTypes.push_back(New);
612 Types.push_back(New);
613 return QualType(New, 0);
614}
615
616QualType ASTContext::getIncompleteArrayType(QualType EltTy,
617 ArrayType::ArraySizeModifier ASM,
618 unsigned EltTypeQuals) {
619 llvm::FoldingSetNodeID ID;
620 IncompleteArrayType::Profile(ID, EltTy);
621
622 void *InsertPos = 0;
623 if (IncompleteArrayType *ATP =
624 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
625 return QualType(ATP, 0);
626
627 // If the element type isn't canonical, this won't be a canonical type
628 // either, so fill in the canonical type field.
629 QualType Canonical;
630
631 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000632 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000633 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000634
635 // Get the new insert position for the node we care about.
636 IncompleteArrayType *NewIP =
637 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
638
639 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000640 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000641
642 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
643 ASM, EltTypeQuals);
644
645 IncompleteArrayTypes.InsertNode(New, InsertPos);
646 Types.push_back(New);
647 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000648}
649
Chris Lattner4b009652007-07-25 00:24:17 +0000650/// getVectorType - Return the unique reference to a vector type of
651/// the specified element type and size. VectorType must be a built-in type.
652QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
653 BuiltinType *baseType;
654
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000655 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000656 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
657
658 // Check if we've already instantiated a vector of this type.
659 llvm::FoldingSetNodeID ID;
660 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
661 void *InsertPos = 0;
662 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
663 return QualType(VTP, 0);
664
665 // If the element type isn't canonical, this won't be a canonical type either,
666 // so fill in the canonical type field.
667 QualType Canonical;
668 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000669 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000670
671 // Get the new insert position for the node we care about.
672 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
673 assert(NewIP == 0 && "Shouldn't be in the map!");
674 }
675 VectorType *New = new VectorType(vecType, NumElts, Canonical);
676 VectorTypes.InsertNode(New, InsertPos);
677 Types.push_back(New);
678 return QualType(New, 0);
679}
680
Nate Begemanaf6ed502008-04-18 23:10:10 +0000681/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +0000682/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000683QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +0000684 BuiltinType *baseType;
685
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000686 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +0000687 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +0000688
689 // Check if we've already instantiated a vector of this type.
690 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000691 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +0000692 void *InsertPos = 0;
693 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
694 return QualType(VTP, 0);
695
696 // If the element type isn't canonical, this won't be a canonical type either,
697 // so fill in the canonical type field.
698 QualType Canonical;
699 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000700 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000701
702 // Get the new insert position for the node we care about.
703 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
704 assert(NewIP == 0 && "Shouldn't be in the map!");
705 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000706 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000707 VectorTypes.InsertNode(New, InsertPos);
708 Types.push_back(New);
709 return QualType(New, 0);
710}
711
712/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
713///
714QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
715 // Unique functions, to guarantee there is only one function of a particular
716 // structure.
717 llvm::FoldingSetNodeID ID;
718 FunctionTypeNoProto::Profile(ID, ResultTy);
719
720 void *InsertPos = 0;
721 if (FunctionTypeNoProto *FT =
722 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
723 return QualType(FT, 0);
724
725 QualType Canonical;
726 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000727 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000728
729 // Get the new insert position for the node we care about.
730 FunctionTypeNoProto *NewIP =
731 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
732 assert(NewIP == 0 && "Shouldn't be in the map!");
733 }
734
735 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
736 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000737 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000738 return QualType(New, 0);
739}
740
741/// getFunctionType - Return a normal function type with a typed argument
742/// list. isVariadic indicates whether the argument list includes '...'.
743QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
744 unsigned NumArgs, bool isVariadic) {
745 // Unique functions, to guarantee there is only one function of a particular
746 // structure.
747 llvm::FoldingSetNodeID ID;
748 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
749
750 void *InsertPos = 0;
751 if (FunctionTypeProto *FTP =
752 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
753 return QualType(FTP, 0);
754
755 // Determine whether the type being created is already canonical or not.
756 bool isCanonical = ResultTy->isCanonical();
757 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
758 if (!ArgArray[i]->isCanonical())
759 isCanonical = false;
760
761 // If this type isn't canonical, get the canonical version of it.
762 QualType Canonical;
763 if (!isCanonical) {
764 llvm::SmallVector<QualType, 16> CanonicalArgs;
765 CanonicalArgs.reserve(NumArgs);
766 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000767 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +0000768
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000769 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +0000770 &CanonicalArgs[0], NumArgs,
771 isVariadic);
772
773 // Get the new insert position for the node we care about.
774 FunctionTypeProto *NewIP =
775 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
776 assert(NewIP == 0 && "Shouldn't be in the map!");
777 }
778
779 // FunctionTypeProto objects are not allocated with new because they have a
780 // variable size array (for parameter types) at the end of them.
781 FunctionTypeProto *FTP =
782 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
783 NumArgs*sizeof(QualType));
784 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
785 Canonical);
786 Types.push_back(FTP);
787 FunctionTypeProtos.InsertNode(FTP, InsertPos);
788 return QualType(FTP, 0);
789}
790
Douglas Gregor1d661552008-04-13 21:07:44 +0000791/// getTypeDeclType - Return the unique reference to the type for the
792/// specified type declaration.
793QualType ASTContext::getTypeDeclType(TypeDecl *Decl) {
794 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
795
796 if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(Decl))
797 return getTypedefType(Typedef);
798 else if (ObjCInterfaceDecl *ObjCInterface
799 = dyn_cast_or_null<ObjCInterfaceDecl>(Decl))
800 return getObjCInterfaceType(ObjCInterface);
801 else if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Decl)) {
802 Decl->TypeForDecl = new RecordType(Record);
803 Types.push_back(Decl->TypeForDecl);
804 return QualType(Decl->TypeForDecl, 0);
805 } else if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Decl)) {
806 Decl->TypeForDecl = new EnumType(Enum);
807 Types.push_back(Decl->TypeForDecl);
808 return QualType(Decl->TypeForDecl, 0);
809 } else
810 assert(false && "TypeDecl without a type?");
811}
812
Chris Lattner4b009652007-07-25 00:24:17 +0000813/// getTypedefType - Return the unique reference to the type for the
814/// specified typename decl.
815QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
816 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
817
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000818 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000819 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000820 Types.push_back(Decl->TypeForDecl);
821 return QualType(Decl->TypeForDecl, 0);
822}
823
Ted Kremenek42730c52008-01-07 19:49:32 +0000824/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000825/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000826QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000827 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
828
Ted Kremenek42730c52008-01-07 19:49:32 +0000829 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000830 Types.push_back(Decl->TypeForDecl);
831 return QualType(Decl->TypeForDecl, 0);
832}
833
Chris Lattnere1352302008-04-07 04:56:42 +0000834/// CmpProtocolNames - Comparison predicate for sorting protocols
835/// alphabetically.
836static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
837 const ObjCProtocolDecl *RHS) {
838 return strcmp(LHS->getName(), RHS->getName()) < 0;
839}
840
841static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
842 unsigned &NumProtocols) {
843 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
844
845 // Sort protocols, keyed by name.
846 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
847
848 // Remove duplicates.
849 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
850 NumProtocols = ProtocolsEnd-Protocols;
851}
852
853
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +0000854/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
855/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000856QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
857 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000858 // Sort the protocol list alphabetically to canonicalize it.
859 SortAndUniqueProtocols(Protocols, NumProtocols);
860
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000861 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +0000862 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000863
864 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000865 if (ObjCQualifiedInterfaceType *QT =
866 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000867 return QualType(QT, 0);
868
869 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +0000870 ObjCQualifiedInterfaceType *QType =
871 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000872 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000873 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000874 return QualType(QType, 0);
875}
876
Chris Lattnere1352302008-04-07 04:56:42 +0000877/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
878/// and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000879QualType ASTContext::getObjCQualifiedIdType(QualType idType,
880 ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000881 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000882 // Sort the protocol list alphabetically to canonicalize it.
883 SortAndUniqueProtocols(Protocols, NumProtocols);
884
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000885 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000886 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000887
888 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000889 if (ObjCQualifiedIdType *QT =
890 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000891 return QualType(QT, 0);
892
893 // No Match;
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000894 QualType Canonical;
895 if (!idType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000896 Canonical = getObjCQualifiedIdType(getCanonicalType(idType),
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000897 Protocols, NumProtocols);
Ted Kremenek42730c52008-01-07 19:49:32 +0000898 ObjCQualifiedIdType *NewQT =
899 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000900 assert(NewQT == 0 && "Shouldn't be in the map!");
901 }
902
Ted Kremenek42730c52008-01-07 19:49:32 +0000903 ObjCQualifiedIdType *QType =
904 new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000905 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000906 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000907 return QualType(QType, 0);
908}
909
Steve Naroff0604dd92007-08-01 18:02:17 +0000910/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
911/// TypeOfExpr AST's (since expression's are never shared). For example,
912/// multiple declarations that refer to "typeof(x)" all contain different
913/// DeclRefExpr's. This doesn't effect the type checker, since it operates
914/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000915QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000916 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff0604dd92007-08-01 18:02:17 +0000917 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
918 Types.push_back(toe);
919 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000920}
921
Steve Naroff0604dd92007-08-01 18:02:17 +0000922/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
923/// TypeOfType AST's. The only motivation to unique these nodes would be
924/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
925/// an issue. This doesn't effect the type checker, since it operates
926/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000927QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000928 QualType Canonical = getCanonicalType(tofType);
Steve Naroff0604dd92007-08-01 18:02:17 +0000929 TypeOfType *tot = new TypeOfType(tofType, Canonical);
930 Types.push_back(tot);
931 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000932}
933
Chris Lattner4b009652007-07-25 00:24:17 +0000934/// getTagDeclType - Return the unique reference to the type for the
935/// specified TagDecl (struct/union/class/enum) decl.
936QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +0000937 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +0000938 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +0000939}
940
941/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
942/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
943/// needs to agree with the definition in <stddef.h>.
944QualType ASTContext::getSizeType() const {
945 // On Darwin, size_t is defined as a "long unsigned int".
946 // FIXME: should derive from "Target".
947 return UnsignedLongTy;
948}
949
Eli Friedmanfdd35d72008-02-12 08:29:21 +0000950/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
951/// width of characters in wide strings, The value is target dependent and
952/// needs to agree with the definition in <stddef.h>.
953QualType ASTContext::getWcharType() const {
954 // On Darwin, wchar_t is defined as a "int".
955 // FIXME: should derive from "Target".
956 return IntTy;
957}
958
Chris Lattner4b009652007-07-25 00:24:17 +0000959/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
960/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
961QualType ASTContext::getPointerDiffType() const {
962 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
963 // FIXME: should derive from "Target".
964 return IntTy;
965}
966
Chris Lattner19eb97e2008-04-02 05:18:44 +0000967//===----------------------------------------------------------------------===//
968// Type Operators
969//===----------------------------------------------------------------------===//
970
Chris Lattner3dae6f42008-04-06 22:41:35 +0000971/// getCanonicalType - Return the canonical (structural) type corresponding to
972/// the specified potentially non-canonical type. The non-canonical version
973/// of a type may have many "decorated" versions of types. Decorators can
974/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
975/// to be free of any of these, allowing two canonical types to be compared
976/// for exact equality with a simple pointer comparison.
977QualType ASTContext::getCanonicalType(QualType T) {
978 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
979 return QualType(CanType.getTypePtr(),
980 T.getCVRQualifiers() | CanType.getCVRQualifiers());
981}
982
983
Chris Lattner19eb97e2008-04-02 05:18:44 +0000984/// getArrayDecayedType - Return the properly qualified result of decaying the
985/// specified array type to a pointer. This operation is non-trivial when
986/// handling typedefs etc. The canonical type of "T" must be an array type,
987/// this returns a pointer to a properly qualified element of the array.
988///
989/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
990QualType ASTContext::getArrayDecayedType(QualType Ty) {
991 // Handle the common case where typedefs are not involved directly.
992 QualType EltTy;
993 unsigned ArrayQuals = 0;
994 unsigned PointerQuals = 0;
995 if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
996 // Since T "isa" an array type, it could not have had an address space
997 // qualifier, just CVR qualifiers. The properly qualified element pointer
998 // gets the union of the CVR qualifiers from the element and the array, and
999 // keeps any address space qualifier on the element type if present.
1000 EltTy = AT->getElementType();
1001 ArrayQuals = Ty.getCVRQualifiers();
1002 PointerQuals = AT->getIndexTypeQualifier();
1003 } else {
1004 // Otherwise, we have an ASQualType or a typedef, etc. Make sure we don't
1005 // lose qualifiers when dealing with typedefs. Example:
1006 // typedef int arr[10];
1007 // void test2() {
1008 // const arr b;
1009 // b[4] = 1;
1010 // }
1011 //
1012 // The decayed type of b is "const int*" even though the element type of the
1013 // array is "int".
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001014 QualType CanTy = getCanonicalType(Ty);
Chris Lattner19eb97e2008-04-02 05:18:44 +00001015 const ArrayType *PrettyArrayType = Ty->getAsArrayType();
1016 assert(PrettyArrayType && "Not an array type!");
1017
1018 // Get the element type with 'getAsArrayType' so that we don't lose any
1019 // typedefs in the element type of the array.
1020 EltTy = PrettyArrayType->getElementType();
1021
1022 // If the array was address-space qualifier, make sure to ASQual the element
1023 // type. We can just grab the address space from the canonical type.
1024 if (unsigned AS = CanTy.getAddressSpace())
1025 EltTy = getASQualType(EltTy, AS);
1026
1027 // To properly handle [multiple levels of] typedefs, typeof's etc, we take
1028 // the CVR qualifiers directly from the canonical type, which is guaranteed
1029 // to have the full set unioned together.
1030 ArrayQuals = CanTy.getCVRQualifiers();
1031 PointerQuals = PrettyArrayType->getIndexTypeQualifier();
1032 }
1033
Chris Lattnerda79b3f2008-04-02 06:06:35 +00001034 // Apply any CVR qualifiers from the array type to the element type. This
1035 // implements C99 6.7.3p8: "If the specification of an array type includes
1036 // any type qualifiers, the element type is so qualified, not the array type."
Chris Lattner19eb97e2008-04-02 05:18:44 +00001037 EltTy = EltTy.getQualifiedType(ArrayQuals | EltTy.getCVRQualifiers());
1038
1039 QualType PtrTy = getPointerType(EltTy);
1040
1041 // int x[restrict 4] -> int *restrict
1042 PtrTy = PtrTy.getQualifiedType(PointerQuals);
1043
1044 return PtrTy;
1045}
1046
Chris Lattner4b009652007-07-25 00:24:17 +00001047/// getFloatingRank - Return a relative rank for floating point types.
1048/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001049static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001050 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001051 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001052
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001053 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001054 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001055 case BuiltinType::Float: return FloatRank;
1056 case BuiltinType::Double: return DoubleRank;
1057 case BuiltinType::LongDouble: return LongDoubleRank;
1058 }
1059}
1060
Steve Narofffa0c4532007-08-27 01:41:48 +00001061/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1062/// point or a complex type (based on typeDomain/typeSize).
1063/// 'typeDomain' is a real floating point or complex type.
1064/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001065QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1066 QualType Domain) const {
1067 FloatingRank EltRank = getFloatingRank(Size);
1068 if (Domain->isComplexType()) {
1069 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001070 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001071 case FloatRank: return FloatComplexTy;
1072 case DoubleRank: return DoubleComplexTy;
1073 case LongDoubleRank: return LongDoubleComplexTy;
1074 }
Chris Lattner4b009652007-07-25 00:24:17 +00001075 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001076
1077 assert(Domain->isRealFloatingType() && "Unknown domain!");
1078 switch (EltRank) {
1079 default: assert(0 && "getFloatingRank(): illegal value for rank");
1080 case FloatRank: return FloatTy;
1081 case DoubleRank: return DoubleTy;
1082 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001083 }
Chris Lattner4b009652007-07-25 00:24:17 +00001084}
1085
Chris Lattner51285d82008-04-06 23:55:33 +00001086/// getFloatingTypeOrder - Compare the rank of the two specified floating
1087/// point types, ignoring the domain of the type (i.e. 'double' ==
1088/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1089/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001090int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1091 FloatingRank LHSR = getFloatingRank(LHS);
1092 FloatingRank RHSR = getFloatingRank(RHS);
1093
1094 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001095 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001096 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001097 return 1;
1098 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001099}
1100
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001101/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1102/// routine will assert if passed a built-in type that isn't an integer or enum,
1103/// or if it is not canonicalized.
1104static unsigned getIntegerRank(Type *T) {
1105 assert(T->isCanonical() && "T should be canonicalized");
1106 if (isa<EnumType>(T))
1107 return 4;
1108
1109 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001110 default: assert(0 && "getIntegerRank(): not a built-in integer");
1111 case BuiltinType::Bool:
1112 return 1;
1113 case BuiltinType::Char_S:
1114 case BuiltinType::Char_U:
1115 case BuiltinType::SChar:
1116 case BuiltinType::UChar:
1117 return 2;
1118 case BuiltinType::Short:
1119 case BuiltinType::UShort:
1120 return 3;
1121 case BuiltinType::Int:
1122 case BuiltinType::UInt:
1123 return 4;
1124 case BuiltinType::Long:
1125 case BuiltinType::ULong:
1126 return 5;
1127 case BuiltinType::LongLong:
1128 case BuiltinType::ULongLong:
1129 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001130 }
1131}
1132
Chris Lattner51285d82008-04-06 23:55:33 +00001133/// getIntegerTypeOrder - Returns the highest ranked integer type:
1134/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1135/// LHS < RHS, return -1.
1136int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001137 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1138 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001139 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001140
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001141 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1142 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001143
Chris Lattner51285d82008-04-06 23:55:33 +00001144 unsigned LHSRank = getIntegerRank(LHSC);
1145 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001146
Chris Lattner51285d82008-04-06 23:55:33 +00001147 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1148 if (LHSRank == RHSRank) return 0;
1149 return LHSRank > RHSRank ? 1 : -1;
1150 }
Chris Lattner4b009652007-07-25 00:24:17 +00001151
Chris Lattner51285d82008-04-06 23:55:33 +00001152 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1153 if (LHSUnsigned) {
1154 // If the unsigned [LHS] type is larger, return it.
1155 if (LHSRank >= RHSRank)
1156 return 1;
1157
1158 // If the signed type can represent all values of the unsigned type, it
1159 // wins. Because we are dealing with 2's complement and types that are
1160 // powers of two larger than each other, this is always safe.
1161 return -1;
1162 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001163
Chris Lattner51285d82008-04-06 23:55:33 +00001164 // If the unsigned [RHS] type is larger, return it.
1165 if (RHSRank >= LHSRank)
1166 return -1;
1167
1168 // If the signed type can represent all values of the unsigned type, it
1169 // wins. Because we are dealing with 2's complement and types that are
1170 // powers of two larger than each other, this is always safe.
1171 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001172}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001173
1174// getCFConstantStringType - Return the type used for constant CFStrings.
1175QualType ASTContext::getCFConstantStringType() {
1176 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001177 CFConstantStringTypeDecl =
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001178 RecordDecl::Create(*this, Decl::Struct, TUDecl, SourceLocation(),
Chris Lattner58114f02008-03-15 21:32:50 +00001179 &Idents.get("NSConstantString"), 0);
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001180 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001181
1182 // const int *isa;
1183 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001184 // int flags;
1185 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001186 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001187 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001188 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001189 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001190 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001191 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001192
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001193 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerf3874bc2008-04-06 04:47:34 +00001194 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner81db64a2008-03-16 00:16:02 +00001195 FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001196
1197 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1198 }
1199
1200 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001201}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001202
Anders Carlssone3f02572007-10-29 06:33:42 +00001203// This returns true if a type has been typedefed to BOOL:
1204// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001205static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001206 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001207 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001208
1209 return false;
1210}
1211
Ted Kremenek42730c52008-01-07 19:49:32 +00001212/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001213/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001214int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001215 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001216
1217 // Make all integer and enum types at least as large as an int
1218 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001219 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001220 // Treat arrays as pointers, since that's how they're passed in.
1221 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001222 sz = getTypeSize(VoidPtrTy);
1223 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001224}
1225
Ted Kremenek42730c52008-01-07 19:49:32 +00001226/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001227/// declaration.
Ted Kremenek42730c52008-01-07 19:49:32 +00001228void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001229 std::string& S)
1230{
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001231 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001232 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001233 // Encode result type.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001234 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001235 // Compute size of all parameters.
1236 // Start with computing size of a pointer in number of bytes.
1237 // FIXME: There might(should) be a better way of doing this computation!
1238 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001239 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001240 // The first two arguments (self and _cmd) are pointers; account for
1241 // their size.
1242 int ParmOffset = 2 * PtrSize;
1243 int NumOfParams = Decl->getNumParams();
1244 for (int i = 0; i < NumOfParams; i++) {
1245 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001246 int sz = getObjCEncodingTypeSize (PType);
1247 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001248 ParmOffset += sz;
1249 }
1250 S += llvm::utostr(ParmOffset);
1251 S += "@0:";
1252 S += llvm::utostr(PtrSize);
1253
1254 // Argument types.
1255 ParmOffset = 2 * PtrSize;
1256 for (int i = 0; i < NumOfParams; i++) {
1257 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001258 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001259 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001260 getObjCEncodingForTypeQualifier(
1261 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian248db262008-01-22 22:44:46 +00001262 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001263 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001264 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001265 }
1266}
1267
Fariborz Jahanian248db262008-01-22 22:44:46 +00001268void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1269 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson36f07d82007-10-29 05:01:08 +00001270{
Anders Carlssone3f02572007-10-29 06:33:42 +00001271 // FIXME: This currently doesn't encode:
1272 // @ An object (whether statically typed or typed id)
1273 // # A class object (Class)
1274 // : A method selector (SEL)
1275 // {name=type...} A structure
1276 // (name=type...) A union
1277 // bnum A bit field of num bits
1278
1279 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001280 char encoding;
1281 switch (BT->getKind()) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001282 default: assert(0 && "Unhandled builtin type kind");
1283 case BuiltinType::Void: encoding = 'v'; break;
1284 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001285 case BuiltinType::Char_U:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001286 case BuiltinType::UChar: encoding = 'C'; break;
1287 case BuiltinType::UShort: encoding = 'S'; break;
1288 case BuiltinType::UInt: encoding = 'I'; break;
1289 case BuiltinType::ULong: encoding = 'L'; break;
1290 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001291 case BuiltinType::Char_S:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001292 case BuiltinType::SChar: encoding = 'c'; break;
1293 case BuiltinType::Short: encoding = 's'; break;
1294 case BuiltinType::Int: encoding = 'i'; break;
1295 case BuiltinType::Long: encoding = 'l'; break;
1296 case BuiltinType::LongLong: encoding = 'q'; break;
1297 case BuiltinType::Float: encoding = 'f'; break;
1298 case BuiltinType::Double: encoding = 'd'; break;
1299 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001300 }
1301
1302 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001303 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001304 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001305 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001306 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001307
1308 }
1309 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001310 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001311 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001312 S += '@';
1313 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001314 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001315 S += '#';
1316 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001317 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001318 S += ':';
1319 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001320 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001321
1322 if (PointeeTy->isCharType()) {
1323 // char pointer types should be encoded as '*' unless it is a
1324 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001325 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001326 S += '*';
1327 return;
1328 }
1329 }
1330
1331 S += '^';
Fariborz Jahanian248db262008-01-22 22:44:46 +00001332 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone3f02572007-10-29 06:33:42 +00001333 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001334 S += '[';
1335
1336 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1337 S += llvm::utostr(CAT->getSize().getZExtValue());
1338 else
1339 assert(0 && "Unhandled array type!");
1340
Fariborz Jahanian248db262008-01-22 22:44:46 +00001341 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001342 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001343 } else if (T->getAsFunctionType()) {
1344 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001345 } else if (const RecordType *RTy = T->getAsRecordType()) {
1346 RecordDecl *RDecl= RTy->getDecl();
1347 S += '{';
1348 S += RDecl->getName();
Fariborz Jahanian248db262008-01-22 22:44:46 +00001349 bool found = false;
1350 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1351 if (ERType[i] == RTy) {
1352 found = true;
1353 break;
1354 }
1355 if (!found) {
1356 ERType.push_back(RTy);
1357 S += '=';
1358 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1359 FieldDecl *field = RDecl->getMember(i);
1360 getObjCEncodingForType(field->getType(), S, ERType);
1361 }
1362 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1363 ERType.pop_back();
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001364 }
1365 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001366 } else if (T->isEnumeralType()) {
1367 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001368 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001369 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001370}
1371
Ted Kremenek42730c52008-01-07 19:49:32 +00001372void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001373 std::string& S) const {
1374 if (QT & Decl::OBJC_TQ_In)
1375 S += 'n';
1376 if (QT & Decl::OBJC_TQ_Inout)
1377 S += 'N';
1378 if (QT & Decl::OBJC_TQ_Out)
1379 S += 'o';
1380 if (QT & Decl::OBJC_TQ_Bycopy)
1381 S += 'O';
1382 if (QT & Decl::OBJC_TQ_Byref)
1383 S += 'R';
1384 if (QT & Decl::OBJC_TQ_Oneway)
1385 S += 'V';
1386}
1387
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001388void ASTContext::setBuiltinVaListType(QualType T)
1389{
1390 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1391
1392 BuiltinVaListType = T;
1393}
1394
Ted Kremenek42730c52008-01-07 19:49:32 +00001395void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001396{
Ted Kremenek42730c52008-01-07 19:49:32 +00001397 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff9d12c902007-10-15 14:41:52 +00001398
Ted Kremenek42730c52008-01-07 19:49:32 +00001399 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001400
1401 // typedef struct objc_object *id;
1402 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1403 assert(ptr && "'id' incorrectly typed");
1404 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1405 assert(rec && "'id' incorrectly typed");
1406 IdStructType = rec;
1407}
1408
Ted Kremenek42730c52008-01-07 19:49:32 +00001409void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001410{
Ted Kremenek42730c52008-01-07 19:49:32 +00001411 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001412
Ted Kremenek42730c52008-01-07 19:49:32 +00001413 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001414
1415 // typedef struct objc_selector *SEL;
1416 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1417 assert(ptr && "'SEL' incorrectly typed");
1418 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1419 assert(rec && "'SEL' incorrectly typed");
1420 SelStructType = rec;
1421}
1422
Ted Kremenek42730c52008-01-07 19:49:32 +00001423void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001424{
Ted Kremenek42730c52008-01-07 19:49:32 +00001425 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1426 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001427}
1428
Ted Kremenek42730c52008-01-07 19:49:32 +00001429void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001430{
Ted Kremenek42730c52008-01-07 19:49:32 +00001431 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001432
Ted Kremenek42730c52008-01-07 19:49:32 +00001433 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001434
1435 // typedef struct objc_class *Class;
1436 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1437 assert(ptr && "'Class' incorrectly typed");
1438 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1439 assert(rec && "'Class' incorrectly typed");
1440 ClassStructType = rec;
1441}
1442
Ted Kremenek42730c52008-01-07 19:49:32 +00001443void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1444 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001445 "'NSConstantString' type already set!");
1446
Ted Kremenek42730c52008-01-07 19:49:32 +00001447 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001448}
1449
Chris Lattner6ff358b2008-04-07 06:51:04 +00001450//===----------------------------------------------------------------------===//
1451// Type Compatibility Testing
1452//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00001453
Chris Lattner390564e2008-04-07 06:49:41 +00001454/// C99 6.2.7p1: If both are complete types, then the following additional
1455/// requirements apply.
1456/// FIXME (handle compatibility across source files).
1457static bool areCompatTagTypes(TagType *LHS, TagType *RHS,
1458 const ASTContext &C) {
Steve Naroff4a5e2072007-11-07 06:03:51 +00001459 // "Class" and "id" are compatible built-in structure types.
Chris Lattner390564e2008-04-07 06:49:41 +00001460 if (C.isObjCIdType(QualType(LHS, 0)) && C.isObjCClassType(QualType(RHS, 0)) ||
1461 C.isObjCClassType(QualType(LHS, 0)) && C.isObjCIdType(QualType(RHS, 0)))
Steve Naroff4a5e2072007-11-07 06:03:51 +00001462 return true;
Eli Friedmane7fb03a2008-02-15 06:03:44 +00001463
Chris Lattner390564e2008-04-07 06:49:41 +00001464 // Within a translation unit a tag type is only compatible with itself. Self
1465 // equality is already handled by the time we get here.
1466 assert(LHS != RHS && "Self equality not handled!");
1467 return false;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001468}
1469
1470bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1471 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1472 // identically qualified and both shall be pointers to compatible types.
Chris Lattner35fef522008-02-20 20:55:12 +00001473 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1474 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001475 return false;
1476
1477 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1478 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1479
1480 return typesAreCompatible(ltype, rtype);
1481}
1482
Steve Naroff85f0dc52007-10-15 20:41:53 +00001483bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1484 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1485 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1486 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1487 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1488
1489 // first check the return types (common between C99 and K&R).
1490 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1491 return false;
1492
1493 if (lproto && rproto) { // two C99 style function prototypes
1494 unsigned lproto_nargs = lproto->getNumArgs();
1495 unsigned rproto_nargs = rproto->getNumArgs();
1496
1497 if (lproto_nargs != rproto_nargs)
1498 return false;
1499
1500 // both prototypes have the same number of arguments.
1501 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1502 (rproto->isVariadic() && !lproto->isVariadic()))
1503 return false;
1504
1505 // The use of ellipsis agree...now check the argument types.
1506 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001507 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1508 // is taken as having the unqualified version of it's declared type.
Steve Naroffdec17fe2008-01-29 00:15:50 +00001509 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001510 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001511 return false;
1512 return true;
1513 }
Chris Lattner1d78a862008-04-07 07:01:58 +00001514
Steve Naroff85f0dc52007-10-15 20:41:53 +00001515 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1516 return true;
1517
1518 // we have a mixture of K&R style with C99 prototypes
1519 const FunctionTypeProto *proto = lproto ? lproto : rproto;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001520 if (proto->isVariadic())
1521 return false;
1522
1523 // FIXME: Each parameter type T in the prototype must be compatible with the
1524 // type resulting from applying the usual argument conversions to T.
1525 return true;
1526}
1527
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001528// C99 6.7.5.2p6
1529static bool areCompatArrayTypes(ArrayType *LHS, ArrayType *RHS, ASTContext &C) {
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001530 // Constant arrays must be the same size to be compatible.
1531 if (const ConstantArrayType* LCAT = dyn_cast<ConstantArrayType>(LHS))
1532 if (const ConstantArrayType* RCAT = dyn_cast<ConstantArrayType>(RHS))
1533 if (RCAT->getSize() != LCAT->getSize())
1534 return false;
Eli Friedman1e7537832008-02-06 04:53:22 +00001535
Chris Lattnerc8971d72008-04-07 06:58:21 +00001536 // Compatible arrays must have compatible element types
1537 return C.typesAreCompatible(LHS->getElementType(), RHS->getElementType());
Steve Naroff85f0dc52007-10-15 20:41:53 +00001538}
1539
Chris Lattner6ff358b2008-04-07 06:51:04 +00001540/// areCompatVectorTypes - Return true if the two specified vector types are
1541/// compatible.
1542static bool areCompatVectorTypes(const VectorType *LHS,
1543 const VectorType *RHS) {
1544 assert(LHS->isCanonical() && RHS->isCanonical());
1545 return LHS->getElementType() == RHS->getElementType() &&
1546 LHS->getNumElements() == RHS->getNumElements();
1547}
1548
1549/// areCompatObjCInterfaces - Return true if the two interface types are
1550/// compatible for assignment from RHS to LHS. This handles validation of any
1551/// protocol qualifiers on the LHS or RHS.
1552///
Chris Lattner1d78a862008-04-07 07:01:58 +00001553static bool areCompatObjCInterfaces(const ObjCInterfaceType *LHS,
1554 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00001555 // Verify that the base decls are compatible: the RHS must be a subclass of
1556 // the LHS.
1557 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1558 return false;
1559
1560 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1561 // protocol qualified at all, then we are good.
1562 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1563 return true;
1564
1565 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1566 // isn't a superset.
1567 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1568 return true; // FIXME: should return false!
1569
1570 // Finally, we must have two protocol-qualified interfaces.
1571 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1572 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1573 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1574 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1575 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1576 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1577
1578 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1579 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1580 // LHS in a single parallel scan until we run out of elements in LHS.
1581 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1582 ObjCProtocolDecl *LHSProto = *LHSPI;
1583
1584 while (RHSPI != RHSPE) {
1585 ObjCProtocolDecl *RHSProto = *RHSPI++;
1586 // If the RHS has a protocol that the LHS doesn't, ignore it.
1587 if (RHSProto != LHSProto)
1588 continue;
1589
1590 // Otherwise, the RHS does have this element.
1591 ++LHSPI;
1592 if (LHSPI == LHSPE)
1593 return true; // All protocols in LHS exist in RHS.
1594
1595 LHSProto = *LHSPI;
1596 }
1597
1598 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1599 return false;
1600}
1601
1602
Steve Naroff85f0dc52007-10-15 20:41:53 +00001603/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1604/// both shall have the identically qualified version of a compatible type.
1605/// C99 6.2.7p1: Two types have compatible types if their types are the
1606/// same. See 6.7.[2,3,5] for additional rules.
Chris Lattner855fed42008-04-07 04:07:56 +00001607bool ASTContext::typesAreCompatible(QualType LHS_NC, QualType RHS_NC) {
1608 QualType LHS = LHS_NC.getCanonicalType();
1609 QualType RHS = RHS_NC.getCanonicalType();
Chris Lattner4d5670b2008-04-03 05:07:04 +00001610
Bill Wendling6a9d8542007-12-03 07:33:35 +00001611 // C++ [expr]: If an expression initially has the type "reference to T", the
1612 // type is adjusted to "T" prior to any further analysis, the expression
1613 // designates the object or function denoted by the reference, and the
1614 // expression is an lvalue.
Chris Lattner855fed42008-04-07 04:07:56 +00001615 if (ReferenceType *RT = dyn_cast<ReferenceType>(LHS))
1616 LHS = RT->getPointeeType();
1617 if (ReferenceType *RT = dyn_cast<ReferenceType>(RHS))
1618 RHS = RT->getPointeeType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001619
Chris Lattnerd47d6042008-04-07 05:37:56 +00001620 // If two types are identical, they are compatible.
1621 if (LHS == RHS)
1622 return true;
1623
1624 // If qualifiers differ, the types are different.
Chris Lattnerb5709e22008-04-07 05:43:21 +00001625 unsigned LHSAS = LHS.getAddressSpace(), RHSAS = RHS.getAddressSpace();
1626 if (LHS.getCVRQualifiers() != RHS.getCVRQualifiers() || LHSAS != RHSAS)
Chris Lattnerd47d6042008-04-07 05:37:56 +00001627 return false;
Chris Lattnerb5709e22008-04-07 05:43:21 +00001628
1629 // Strip off ASQual's if present.
1630 if (LHSAS) {
1631 LHS = LHS.getUnqualifiedType();
1632 RHS = RHS.getUnqualifiedType();
1633 }
Chris Lattnerd47d6042008-04-07 05:37:56 +00001634
Chris Lattner855fed42008-04-07 04:07:56 +00001635 Type::TypeClass LHSClass = LHS->getTypeClass();
1636 Type::TypeClass RHSClass = RHS->getTypeClass();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001637
1638 // We want to consider the two function types to be the same for these
1639 // comparisons, just force one to the other.
1640 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1641 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00001642
1643 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00001644 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
1645 LHSClass = Type::ConstantArray;
1646 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
1647 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001648
Nate Begemanaf6ed502008-04-18 23:10:10 +00001649 // Canonicalize ExtVector -> Vector.
1650 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
1651 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00001652
Chris Lattner7cdcb252008-04-07 06:38:24 +00001653 // Consider qualified interfaces and interfaces the same.
1654 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
1655 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
1656
Chris Lattnerb5709e22008-04-07 05:43:21 +00001657 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001658 if (LHSClass != RHSClass) {
Chris Lattner7cdcb252008-04-07 06:38:24 +00001659 // ID is compatible with all interface types.
1660 if (isa<ObjCInterfaceType>(LHS))
1661 return isObjCIdType(RHS);
1662 if (isa<ObjCInterfaceType>(RHS))
1663 return isObjCIdType(LHS);
Chris Lattner0d3e6452008-04-07 05:53:18 +00001664
Chris Lattnerc38d4522008-01-14 05:45:46 +00001665 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1666 // a signed integer type, or an unsigned integer type.
Chris Lattner855fed42008-04-07 04:07:56 +00001667 if (LHS->isEnumeralType() && RHS->isIntegralType()) {
1668 EnumDecl* EDecl = cast<EnumType>(LHS)->getDecl();
1669 return EDecl->getIntegerType() == RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001670 }
Chris Lattner855fed42008-04-07 04:07:56 +00001671 if (RHS->isEnumeralType() && LHS->isIntegralType()) {
1672 EnumDecl* EDecl = cast<EnumType>(RHS)->getDecl();
1673 return EDecl->getIntegerType() == LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001674 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001675
Steve Naroff85f0dc52007-10-15 20:41:53 +00001676 return false;
1677 }
Chris Lattnerb5709e22008-04-07 05:43:21 +00001678
Steve Naroffc88babe2008-01-09 22:43:08 +00001679 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001680 switch (LHSClass) {
Chris Lattnerb5709e22008-04-07 05:43:21 +00001681 case Type::ASQual:
1682 case Type::FunctionProto:
1683 case Type::VariableArray:
1684 case Type::IncompleteArray:
1685 case Type::Reference:
Chris Lattner7cdcb252008-04-07 06:38:24 +00001686 case Type::ObjCQualifiedInterface:
Chris Lattnerb5709e22008-04-07 05:43:21 +00001687 assert(0 && "Canonicalized away above");
Chris Lattnerc38d4522008-01-14 05:45:46 +00001688 case Type::Pointer:
Chris Lattner855fed42008-04-07 04:07:56 +00001689 return pointerTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001690 case Type::ConstantArray:
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001691 return areCompatArrayTypes(cast<ArrayType>(LHS), cast<ArrayType>(RHS),
1692 *this);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001693 case Type::FunctionNoProto:
Chris Lattner855fed42008-04-07 04:07:56 +00001694 return functionTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001695 case Type::Tagged: // handle structures, unions
Chris Lattner390564e2008-04-07 06:49:41 +00001696 return areCompatTagTypes(cast<TagType>(LHS), cast<TagType>(RHS), *this);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001697 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00001698 // Only exactly equal builtin types are compatible, which is tested above.
1699 return false;
1700 case Type::Vector:
1701 return areCompatVectorTypes(cast<VectorType>(LHS), cast<VectorType>(RHS));
Chris Lattnerc38d4522008-01-14 05:45:46 +00001702 case Type::ObjCInterface:
Chris Lattner7cdcb252008-04-07 06:38:24 +00001703 return areCompatObjCInterfaces(cast<ObjCInterfaceType>(LHS),
1704 cast<ObjCInterfaceType>(RHS));
Chris Lattnerc38d4522008-01-14 05:45:46 +00001705 default:
1706 assert(0 && "unexpected type");
Steve Naroff85f0dc52007-10-15 20:41:53 +00001707 }
1708 return true; // should never get here...
1709}
Ted Kremenek738e6c02007-10-31 17:10:13 +00001710
Chris Lattner1d78a862008-04-07 07:01:58 +00001711//===----------------------------------------------------------------------===//
1712// Serialization Support
1713//===----------------------------------------------------------------------===//
1714
Ted Kremenek738e6c02007-10-31 17:10:13 +00001715/// Emit - Serialize an ASTContext object to Bitcode.
1716void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00001717 S.EmitRef(SourceMgr);
1718 S.EmitRef(Target);
1719 S.EmitRef(Idents);
1720 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001721
Ted Kremenek68228a92007-10-31 22:44:07 +00001722 // Emit the size of the type vector so that we can reserve that size
1723 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001724 S.EmitInt(Types.size());
1725
Ted Kremenek034a78c2007-11-13 22:02:55 +00001726 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1727 I!=E;++I)
1728 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001729
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001730 S.EmitOwnedPtr(TUDecl);
1731
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001732 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001733}
1734
Ted Kremenekacba3612007-11-13 00:25:37 +00001735ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek68228a92007-10-31 22:44:07 +00001736 SourceManager &SM = D.ReadRef<SourceManager>();
1737 TargetInfo &t = D.ReadRef<TargetInfo>();
1738 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1739 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00001740
Ted Kremenek68228a92007-10-31 22:44:07 +00001741 unsigned size_reserve = D.ReadInt();
1742
1743 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1744
Ted Kremenek034a78c2007-11-13 22:02:55 +00001745 for (unsigned i = 0; i < size_reserve; ++i)
1746 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00001747
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001748 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
1749
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001750 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00001751
1752 return A;
1753}