blob: a68fea469d19e3364cc9de543228aa5cef973845 [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 Lattner4b009652007-07-25 00:24:17 +0000184 T = T.getCanonicalType();
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 }
203 case Type::OCUVector:
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 // FIXME: need to use TargetInfo to derive the target specific sizes. This
215 // implementation will suffice for play with vector support.
216 switch (cast<BuiltinType>(T)->getKind()) {
217 default: assert(0 && "Unknown builtin type!");
218 case BuiltinType::Void:
219 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000220 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000221 Width = Target.getBoolWidth();
222 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000223 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000224 case BuiltinType::Char_S:
225 case BuiltinType::Char_U:
226 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000227 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000228 Width = Target.getCharWidth();
229 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000230 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000231 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000232 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000233 Width = Target.getShortWidth();
234 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000235 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000236 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000237 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000238 Width = Target.getIntWidth();
239 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000240 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000241 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000242 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000243 Width = Target.getLongWidth();
244 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000245 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000246 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000247 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000248 Width = Target.getLongLongWidth();
249 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000250 break;
251 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000252 Width = Target.getFloatWidth();
253 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000254 break;
255 case BuiltinType::Double:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000256 Width = Target.getDoubleWidth();
257 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000258 break;
259 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000260 Width = Target.getLongDoubleWidth();
261 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000262 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000263 }
264 break;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000265 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000266 // FIXME: Pointers into different addr spaces could have different sizes and
267 // alignment requirements: getPointerInfo should take an AddrSpace.
268 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000269 case Type::ObjCQualifiedId:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000270 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000271 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000272 break;
Chris Lattner461a6c52008-03-08 08:34:58 +0000273 case Type::Pointer: {
274 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000275 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000276 Align = Target.getPointerAlign(AS);
277 break;
278 }
Chris Lattner4b009652007-07-25 00:24:17 +0000279 case Type::Reference:
280 // "When applied to a reference or a reference type, the result is the size
281 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000282 // FIXME: This is wrong for struct layout: a reference in a struct has
283 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000284 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner4b009652007-07-25 00:24:17 +0000285
286 case Type::Complex: {
287 // Complex types have the same alignment as their elements, but twice the
288 // size.
289 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000290 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000291 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000292 Align = EltInfo.second;
293 break;
294 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000295 case Type::Tagged: {
296 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
297 return getTypeInfo(ET->getDecl()->getIntegerType());
298
299 RecordType *RT = cast<RecordType>(T);
300 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
301 Width = Layout.getSize();
302 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000303 break;
304 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000305 }
Chris Lattner4b009652007-07-25 00:24:17 +0000306
307 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000308 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000309}
310
Devang Patel7a78e432007-11-01 19:11:01 +0000311/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000312/// specified record (struct/union/class), which indicates its size and field
313/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000314const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000315 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
316
317 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000318 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000319 if (Entry) return *Entry;
320
Devang Patel7a78e432007-11-01 19:11:01 +0000321 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
322 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
323 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000324 Entry = NewEntry;
325
326 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
327 uint64_t RecordSize = 0;
328 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
329
330 if (D->getKind() != Decl::Union) {
Anders Carlsson7dce0292008-02-16 19:51:27 +0000331 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
332 RecordAlign = std::max(RecordAlign, AA->getAlignment());
333
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000334 bool StructIsPacked = D->getAttr<PackedAttr>();
335
Chris Lattner4b009652007-07-25 00:24:17 +0000336 // Layout each field, for now, just sequentially, respecting alignment. In
337 // the future, this will need to be tweakable by targets.
338 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
339 const FieldDecl *FD = D->getMember(i);
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000340 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
Eli Friedman67571ac2008-02-06 05:33:51 +0000341 uint64_t FieldSize;
342 unsigned FieldAlign;
Anders Carlsson058237f2008-02-18 07:13:09 +0000343
344 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
345 llvm::APSInt I(32);
346 bool BitWidthIsICE =
347 BitWidthExpr->isIntegerConstantExpr(I, *this);
348 assert (BitWidthIsICE && "Invalid BitField size expression");
349 FieldSize = I.getZExtValue();
350
Chris Lattner8cd0e932008-03-05 18:54:05 +0000351 std::pair<uint64_t, unsigned> TypeInfo = getTypeInfo(FD->getType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000352 uint64_t TypeSize = TypeInfo.first;
Anders Carlsson7dce0292008-02-16 19:51:27 +0000353
354 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
355 FieldAlign = AA->getAlignment();
356 else if (FieldIsPacked)
357 FieldAlign = 8;
358 else {
Anders Carlsson058237f2008-02-18 07:13:09 +0000359 // FIXME: This is X86 specific, use 32-bit alignment for long long.
360 if (FD->getType()->isIntegerType() && TypeInfo.second > 32)
361 FieldAlign = 32;
362 else
363 FieldAlign = TypeInfo.second;
Anders Carlsson7dce0292008-02-16 19:51:27 +0000364 }
Eli Friedman67571ac2008-02-06 05:33:51 +0000365
Anders Carlsson058237f2008-02-18 07:13:09 +0000366 // Check if we need to add padding to give the field the correct
367 // alignment.
368 if (RecordSize % FieldAlign + FieldSize > TypeSize)
369 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
370
371 } else {
372 if (FD->getType()->isIncompleteType()) {
373 // This must be a flexible array member; we can't directly
374 // query getTypeInfo about these, so we figure it out here.
375 // Flexible array members don't have any size, but they
376 // have to be aligned appropriately for their element type.
377
378 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
379 FieldAlign = AA->getAlignment();
380 else if (FieldIsPacked)
381 FieldAlign = 8;
382 else {
383 const ArrayType* ATy = FD->getType()->getAsArrayType();
Chris Lattner8cd0e932008-03-05 18:54:05 +0000384 FieldAlign = getTypeAlign(ATy->getElementType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000385 }
386 FieldSize = 0;
387 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000388 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000389 FieldSize = FieldInfo.first;
390
391 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
392 FieldAlign = AA->getAlignment();
393 else if (FieldIsPacked)
394 FieldAlign = 8;
395 else
396 FieldAlign = FieldInfo.second;
397 }
398
399 // Round up the current record size to the field's alignment boundary.
400 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
401 }
Chris Lattner4b009652007-07-25 00:24:17 +0000402
403 // Place this field at the current location.
404 FieldOffsets[i] = RecordSize;
405
406 // Reserve space for this field.
407 RecordSize += FieldSize;
408
409 // Remember max struct/class alignment.
410 RecordAlign = std::max(RecordAlign, FieldAlign);
411 }
412
413 // Finally, round the size of the total struct up to the alignment of the
414 // struct itself.
415 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
416 } else {
417 // Union layout just puts each member at the start of the record.
418 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
419 const FieldDecl *FD = D->getMember(i);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000420 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Chris Lattner4b009652007-07-25 00:24:17 +0000421 uint64_t FieldSize = FieldInfo.first;
422 unsigned FieldAlign = FieldInfo.second;
423
Anders Carlsson058237f2008-02-18 07:13:09 +0000424 // FIXME: This is X86 specific, use 32-bit alignment for long long.
425 if (FD->getType()->isIntegerType() && FieldAlign > 32)
426 FieldAlign = 32;
427
Chris Lattner4b009652007-07-25 00:24:17 +0000428 // Round up the current record size to the field's alignment boundary.
429 RecordSize = std::max(RecordSize, FieldSize);
430
431 // Place this field at the start of the record.
432 FieldOffsets[i] = 0;
433
434 // Remember max struct/class alignment.
435 RecordAlign = std::max(RecordAlign, FieldAlign);
436 }
437 }
438
439 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
440 return *NewEntry;
441}
442
Chris Lattner4b009652007-07-25 00:24:17 +0000443//===----------------------------------------------------------------------===//
444// Type creation/memoization methods
445//===----------------------------------------------------------------------===//
446
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000447QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattner35fef522008-02-20 20:55:12 +0000448 if (T.getCanonicalType().getAddressSpace() == AddressSpace)
449 return T;
450
451 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
452 // with CVR qualifiers from here on out.
453 assert(T.getCanonicalType().getAddressSpace() == 0 &&
454 "Type is already address space qualified");
455
456 // Check if we've already instantiated an address space qual'd type of this
457 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000458 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000459 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000460 void *InsertPos = 0;
461 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
462 return QualType(ASQy, 0);
463
464 // If the base type isn't canonical, this won't be a canonical type either,
465 // so fill in the canonical type field.
466 QualType Canonical;
467 if (!T->isCanonical()) {
468 Canonical = getASQualType(T.getCanonicalType(), AddressSpace);
469
470 // Get the new insert position for the node we care about.
471 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
472 assert(NewIP == 0 && "Shouldn't be in the map!");
473 }
Chris Lattner35fef522008-02-20 20:55:12 +0000474 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000475 ASQualTypes.InsertNode(New, InsertPos);
476 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000477 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000478}
479
Chris Lattner4b009652007-07-25 00:24:17 +0000480
481/// getComplexType - Return the uniqued reference to the type for a complex
482/// number with the specified element type.
483QualType ASTContext::getComplexType(QualType T) {
484 // Unique pointers, to guarantee there is only one pointer of a particular
485 // structure.
486 llvm::FoldingSetNodeID ID;
487 ComplexType::Profile(ID, T);
488
489 void *InsertPos = 0;
490 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
491 return QualType(CT, 0);
492
493 // If the pointee type isn't canonical, this won't be a canonical type either,
494 // so fill in the canonical type field.
495 QualType Canonical;
496 if (!T->isCanonical()) {
497 Canonical = getComplexType(T.getCanonicalType());
498
499 // Get the new insert position for the node we care about.
500 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
501 assert(NewIP == 0 && "Shouldn't be in the map!");
502 }
503 ComplexType *New = new ComplexType(T, Canonical);
504 Types.push_back(New);
505 ComplexTypes.InsertNode(New, InsertPos);
506 return QualType(New, 0);
507}
508
509
510/// getPointerType - Return the uniqued reference to the type for a pointer to
511/// the specified type.
512QualType ASTContext::getPointerType(QualType T) {
513 // Unique pointers, to guarantee there is only one pointer of a particular
514 // structure.
515 llvm::FoldingSetNodeID ID;
516 PointerType::Profile(ID, T);
517
518 void *InsertPos = 0;
519 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
520 return QualType(PT, 0);
521
522 // If the pointee type isn't canonical, this won't be a canonical type either,
523 // so fill in the canonical type field.
524 QualType Canonical;
525 if (!T->isCanonical()) {
526 Canonical = getPointerType(T.getCanonicalType());
527
528 // Get the new insert position for the node we care about.
529 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
530 assert(NewIP == 0 && "Shouldn't be in the map!");
531 }
532 PointerType *New = new PointerType(T, Canonical);
533 Types.push_back(New);
534 PointerTypes.InsertNode(New, InsertPos);
535 return QualType(New, 0);
536}
537
538/// getReferenceType - Return the uniqued reference to the type for a reference
539/// to the specified type.
540QualType ASTContext::getReferenceType(QualType T) {
541 // Unique pointers, to guarantee there is only one pointer of a particular
542 // structure.
543 llvm::FoldingSetNodeID ID;
544 ReferenceType::Profile(ID, T);
545
546 void *InsertPos = 0;
547 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
548 return QualType(RT, 0);
549
550 // If the referencee type isn't canonical, this won't be a canonical type
551 // either, so fill in the canonical type field.
552 QualType Canonical;
553 if (!T->isCanonical()) {
554 Canonical = getReferenceType(T.getCanonicalType());
555
556 // Get the new insert position for the node we care about.
557 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
558 assert(NewIP == 0 && "Shouldn't be in the map!");
559 }
560
561 ReferenceType *New = new ReferenceType(T, Canonical);
562 Types.push_back(New);
563 ReferenceTypes.InsertNode(New, InsertPos);
564 return QualType(New, 0);
565}
566
Steve Naroff83c13012007-08-30 01:06:46 +0000567/// getConstantArrayType - Return the unique reference to the type for an
568/// array of the specified element type.
569QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000570 const llvm::APInt &ArySize,
571 ArrayType::ArraySizeModifier ASM,
572 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000573 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000574 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000575
576 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000577 if (ConstantArrayType *ATP =
578 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000579 return QualType(ATP, 0);
580
581 // If the element type isn't canonical, this won't be a canonical type either,
582 // so fill in the canonical type field.
583 QualType Canonical;
584 if (!EltTy->isCanonical()) {
Steve Naroff24c9b982007-08-30 18:10:14 +0000585 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
586 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000587 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000588 ConstantArrayType *NewIP =
589 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
590
Chris Lattner4b009652007-07-25 00:24:17 +0000591 assert(NewIP == 0 && "Shouldn't be in the map!");
592 }
593
Steve Naroff24c9b982007-08-30 18:10:14 +0000594 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
595 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000596 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000597 Types.push_back(New);
598 return QualType(New, 0);
599}
600
Steve Naroffe2579e32007-08-30 18:14:25 +0000601/// getVariableArrayType - Returns a non-unique reference to the type for a
602/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000603QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
604 ArrayType::ArraySizeModifier ASM,
605 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000606 // Since we don't unique expressions, it isn't possible to unique VLA's
607 // that have an expression provided for their size.
608
609 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
610 ASM, EltTypeQuals);
611
612 VariableArrayTypes.push_back(New);
613 Types.push_back(New);
614 return QualType(New, 0);
615}
616
617QualType ASTContext::getIncompleteArrayType(QualType EltTy,
618 ArrayType::ArraySizeModifier ASM,
619 unsigned EltTypeQuals) {
620 llvm::FoldingSetNodeID ID;
621 IncompleteArrayType::Profile(ID, EltTy);
622
623 void *InsertPos = 0;
624 if (IncompleteArrayType *ATP =
625 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
626 return QualType(ATP, 0);
627
628 // If the element type isn't canonical, this won't be a canonical type
629 // either, so fill in the canonical type field.
630 QualType Canonical;
631
632 if (!EltTy->isCanonical()) {
633 Canonical = getIncompleteArrayType(EltTy.getCanonicalType(),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000634 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000635
636 // Get the new insert position for the node we care about.
637 IncompleteArrayType *NewIP =
638 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
639
640 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000641 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000642
643 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
644 ASM, EltTypeQuals);
645
646 IncompleteArrayTypes.InsertNode(New, InsertPos);
647 Types.push_back(New);
648 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000649}
650
Chris Lattner4b009652007-07-25 00:24:17 +0000651/// getVectorType - Return the unique reference to a vector type of
652/// the specified element type and size. VectorType must be a built-in type.
653QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
654 BuiltinType *baseType;
655
656 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
657 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
658
659 // Check if we've already instantiated a vector of this type.
660 llvm::FoldingSetNodeID ID;
661 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
662 void *InsertPos = 0;
663 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
664 return QualType(VTP, 0);
665
666 // If the element type isn't canonical, this won't be a canonical type either,
667 // so fill in the canonical type field.
668 QualType Canonical;
669 if (!vecType->isCanonical()) {
670 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
671
672 // Get the new insert position for the node we care about.
673 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
674 assert(NewIP == 0 && "Shouldn't be in the map!");
675 }
676 VectorType *New = new VectorType(vecType, NumElts, Canonical);
677 VectorTypes.InsertNode(New, InsertPos);
678 Types.push_back(New);
679 return QualType(New, 0);
680}
681
682/// getOCUVectorType - Return the unique reference to an OCU vector type of
683/// the specified element type and size. VectorType must be a built-in type.
684QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
685 BuiltinType *baseType;
686
687 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
688 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
689
690 // Check if we've already instantiated a vector of this type.
691 llvm::FoldingSetNodeID ID;
692 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
693 void *InsertPos = 0;
694 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
695 return QualType(VTP, 0);
696
697 // If the element type isn't canonical, this won't be a canonical type either,
698 // so fill in the canonical type field.
699 QualType Canonical;
700 if (!vecType->isCanonical()) {
701 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
702
703 // Get the new insert position for the node we care about.
704 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
705 assert(NewIP == 0 && "Shouldn't be in the map!");
706 }
707 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
708 VectorTypes.InsertNode(New, InsertPos);
709 Types.push_back(New);
710 return QualType(New, 0);
711}
712
713/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
714///
715QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
716 // Unique functions, to guarantee there is only one function of a particular
717 // structure.
718 llvm::FoldingSetNodeID ID;
719 FunctionTypeNoProto::Profile(ID, ResultTy);
720
721 void *InsertPos = 0;
722 if (FunctionTypeNoProto *FT =
723 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
724 return QualType(FT, 0);
725
726 QualType Canonical;
727 if (!ResultTy->isCanonical()) {
728 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
729
730 // Get the new insert position for the node we care about.
731 FunctionTypeNoProto *NewIP =
732 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
733 assert(NewIP == 0 && "Shouldn't be in the map!");
734 }
735
736 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
737 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000738 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000739 return QualType(New, 0);
740}
741
742/// getFunctionType - Return a normal function type with a typed argument
743/// list. isVariadic indicates whether the argument list includes '...'.
744QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
745 unsigned NumArgs, bool isVariadic) {
746 // Unique functions, to guarantee there is only one function of a particular
747 // structure.
748 llvm::FoldingSetNodeID ID;
749 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
750
751 void *InsertPos = 0;
752 if (FunctionTypeProto *FTP =
753 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
754 return QualType(FTP, 0);
755
756 // Determine whether the type being created is already canonical or not.
757 bool isCanonical = ResultTy->isCanonical();
758 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
759 if (!ArgArray[i]->isCanonical())
760 isCanonical = false;
761
762 // If this type isn't canonical, get the canonical version of it.
763 QualType Canonical;
764 if (!isCanonical) {
765 llvm::SmallVector<QualType, 16> CanonicalArgs;
766 CanonicalArgs.reserve(NumArgs);
767 for (unsigned i = 0; i != NumArgs; ++i)
768 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
769
770 Canonical = getFunctionType(ResultTy.getCanonicalType(),
771 &CanonicalArgs[0], NumArgs,
772 isVariadic);
773
774 // Get the new insert position for the node we care about.
775 FunctionTypeProto *NewIP =
776 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
777 assert(NewIP == 0 && "Shouldn't be in the map!");
778 }
779
780 // FunctionTypeProto objects are not allocated with new because they have a
781 // variable size array (for parameter types) at the end of them.
782 FunctionTypeProto *FTP =
783 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
784 NumArgs*sizeof(QualType));
785 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
786 Canonical);
787 Types.push_back(FTP);
788 FunctionTypeProtos.InsertNode(FTP, InsertPos);
789 return QualType(FTP, 0);
790}
791
792/// getTypedefType - Return the unique reference to the type for the
793/// specified typename decl.
794QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
795 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
796
797 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000798 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000799 Types.push_back(Decl->TypeForDecl);
800 return QualType(Decl->TypeForDecl, 0);
801}
802
Ted Kremenek42730c52008-01-07 19:49:32 +0000803/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000804/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000805QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000806 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
807
Ted Kremenek42730c52008-01-07 19:49:32 +0000808 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000809 Types.push_back(Decl->TypeForDecl);
810 return QualType(Decl->TypeForDecl, 0);
811}
812
Ted Kremenek42730c52008-01-07 19:49:32 +0000813/// getObjCQualifiedInterfaceType - Return a
814/// ObjCQualifiedInterfaceType type for the given interface decl and
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000815/// the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000816QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
817 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000818 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000819 ObjCQualifiedInterfaceType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000820
821 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000822 if (ObjCQualifiedInterfaceType *QT =
823 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000824 return QualType(QT, 0);
825
826 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +0000827 ObjCQualifiedInterfaceType *QType =
828 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000829 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000830 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000831 return QualType(QType, 0);
832}
833
Ted Kremenek42730c52008-01-07 19:49:32 +0000834/// getObjCQualifiedIdType - Return a
835/// getObjCQualifiedIdType type for the 'id' decl and
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000836/// the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000837QualType ASTContext::getObjCQualifiedIdType(QualType idType,
838 ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000839 unsigned NumProtocols) {
840 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000841 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000842
843 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000844 if (ObjCQualifiedIdType *QT =
845 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000846 return QualType(QT, 0);
847
848 // No Match;
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000849 QualType Canonical;
850 if (!idType->isCanonical()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000851 Canonical = getObjCQualifiedIdType(idType.getCanonicalType(),
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000852 Protocols, NumProtocols);
Ted Kremenek42730c52008-01-07 19:49:32 +0000853 ObjCQualifiedIdType *NewQT =
854 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000855 assert(NewQT == 0 && "Shouldn't be in the map!");
856 }
857
Ted Kremenek42730c52008-01-07 19:49:32 +0000858 ObjCQualifiedIdType *QType =
859 new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000860 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000861 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000862 return QualType(QType, 0);
863}
864
Steve Naroff0604dd92007-08-01 18:02:17 +0000865/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
866/// TypeOfExpr AST's (since expression's are never shared). For example,
867/// multiple declarations that refer to "typeof(x)" all contain different
868/// DeclRefExpr's. This doesn't effect the type checker, since it operates
869/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000870QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroff7cbb1462007-07-31 12:34:36 +0000871 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000872 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
873 Types.push_back(toe);
874 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000875}
876
Steve Naroff0604dd92007-08-01 18:02:17 +0000877/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
878/// TypeOfType AST's. The only motivation to unique these nodes would be
879/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
880/// an issue. This doesn't effect the type checker, since it operates
881/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000882QualType ASTContext::getTypeOfType(QualType tofType) {
883 QualType Canonical = tofType.getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000884 TypeOfType *tot = new TypeOfType(tofType, Canonical);
885 Types.push_back(tot);
886 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000887}
888
Chris Lattner4b009652007-07-25 00:24:17 +0000889/// getTagDeclType - Return the unique reference to the type for the
890/// specified TagDecl (struct/union/class/enum) decl.
891QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +0000892 assert (Decl);
893
Ted Kremenekf05026d2007-11-14 00:03:20 +0000894 // The decl stores the type cache.
Ted Kremenekae8fa032007-11-26 21:16:01 +0000895 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Ted Kremenekf05026d2007-11-14 00:03:20 +0000896
897 TagType* T = new TagType(Decl, QualType());
Ted Kremenekae8fa032007-11-26 21:16:01 +0000898 Types.push_back(T);
899 Decl->TypeForDecl = T;
Ted Kremenekf05026d2007-11-14 00:03:20 +0000900
901 return QualType(T, 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000902}
903
904/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
905/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
906/// needs to agree with the definition in <stddef.h>.
907QualType ASTContext::getSizeType() const {
908 // On Darwin, size_t is defined as a "long unsigned int".
909 // FIXME: should derive from "Target".
910 return UnsignedLongTy;
911}
912
Eli Friedmanfdd35d72008-02-12 08:29:21 +0000913/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
914/// width of characters in wide strings, The value is target dependent and
915/// needs to agree with the definition in <stddef.h>.
916QualType ASTContext::getWcharType() const {
917 // On Darwin, wchar_t is defined as a "int".
918 // FIXME: should derive from "Target".
919 return IntTy;
920}
921
Chris Lattner4b009652007-07-25 00:24:17 +0000922/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
923/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
924QualType ASTContext::getPointerDiffType() const {
925 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
926 // FIXME: should derive from "Target".
927 return IntTy;
928}
929
Chris Lattner19eb97e2008-04-02 05:18:44 +0000930//===----------------------------------------------------------------------===//
931// Type Operators
932//===----------------------------------------------------------------------===//
933
934/// getArrayDecayedType - Return the properly qualified result of decaying the
935/// specified array type to a pointer. This operation is non-trivial when
936/// handling typedefs etc. The canonical type of "T" must be an array type,
937/// this returns a pointer to a properly qualified element of the array.
938///
939/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
940QualType ASTContext::getArrayDecayedType(QualType Ty) {
941 // Handle the common case where typedefs are not involved directly.
942 QualType EltTy;
943 unsigned ArrayQuals = 0;
944 unsigned PointerQuals = 0;
945 if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
946 // Since T "isa" an array type, it could not have had an address space
947 // qualifier, just CVR qualifiers. The properly qualified element pointer
948 // gets the union of the CVR qualifiers from the element and the array, and
949 // keeps any address space qualifier on the element type if present.
950 EltTy = AT->getElementType();
951 ArrayQuals = Ty.getCVRQualifiers();
952 PointerQuals = AT->getIndexTypeQualifier();
953 } else {
954 // Otherwise, we have an ASQualType or a typedef, etc. Make sure we don't
955 // lose qualifiers when dealing with typedefs. Example:
956 // typedef int arr[10];
957 // void test2() {
958 // const arr b;
959 // b[4] = 1;
960 // }
961 //
962 // The decayed type of b is "const int*" even though the element type of the
963 // array is "int".
964 QualType CanTy = Ty.getCanonicalType();
965 const ArrayType *PrettyArrayType = Ty->getAsArrayType();
966 assert(PrettyArrayType && "Not an array type!");
967
968 // Get the element type with 'getAsArrayType' so that we don't lose any
969 // typedefs in the element type of the array.
970 EltTy = PrettyArrayType->getElementType();
971
972 // If the array was address-space qualifier, make sure to ASQual the element
973 // type. We can just grab the address space from the canonical type.
974 if (unsigned AS = CanTy.getAddressSpace())
975 EltTy = getASQualType(EltTy, AS);
976
977 // To properly handle [multiple levels of] typedefs, typeof's etc, we take
978 // the CVR qualifiers directly from the canonical type, which is guaranteed
979 // to have the full set unioned together.
980 ArrayQuals = CanTy.getCVRQualifiers();
981 PointerQuals = PrettyArrayType->getIndexTypeQualifier();
982 }
983
Chris Lattnerda79b3f2008-04-02 06:06:35 +0000984 // Apply any CVR qualifiers from the array type to the element type. This
985 // implements C99 6.7.3p8: "If the specification of an array type includes
986 // any type qualifiers, the element type is so qualified, not the array type."
Chris Lattner19eb97e2008-04-02 05:18:44 +0000987 EltTy = EltTy.getQualifiedType(ArrayQuals | EltTy.getCVRQualifiers());
988
989 QualType PtrTy = getPointerType(EltTy);
990
991 // int x[restrict 4] -> int *restrict
992 PtrTy = PtrTy.getQualifiedType(PointerQuals);
993
994 return PtrTy;
995}
996
Chris Lattner4b009652007-07-25 00:24:17 +0000997/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
998/// routine will assert if passed a built-in type that isn't an integer or enum.
999static int getIntegerRank(QualType t) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001000 if (isa<EnumType>(t.getCanonicalType()))
Chris Lattner4b009652007-07-25 00:24:17 +00001001 return 4;
Chris Lattner4b009652007-07-25 00:24:17 +00001002
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001003 const BuiltinType *BT = t.getCanonicalType()->getAsBuiltinType();
Chris Lattner4b009652007-07-25 00:24:17 +00001004 switch (BT->getKind()) {
1005 default:
1006 assert(0 && "getIntegerRank(): not a built-in integer");
1007 case BuiltinType::Bool:
1008 return 1;
1009 case BuiltinType::Char_S:
1010 case BuiltinType::Char_U:
1011 case BuiltinType::SChar:
1012 case BuiltinType::UChar:
1013 return 2;
1014 case BuiltinType::Short:
1015 case BuiltinType::UShort:
1016 return 3;
1017 case BuiltinType::Int:
1018 case BuiltinType::UInt:
1019 return 4;
1020 case BuiltinType::Long:
1021 case BuiltinType::ULong:
1022 return 5;
1023 case BuiltinType::LongLong:
1024 case BuiltinType::ULongLong:
1025 return 6;
1026 }
1027}
1028
1029/// getFloatingRank - Return a relative rank for floating point types.
1030/// This routine will assert if passed a built-in type that isn't a float.
1031static int getFloatingRank(QualType T) {
1032 T = T.getCanonicalType();
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001033 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001034 return getFloatingRank(CT->getElementType());
1035
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001036 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattner5003e8b2007-11-01 05:03:41 +00001037 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001038 case BuiltinType::Float: return FloatRank;
1039 case BuiltinType::Double: return DoubleRank;
1040 case BuiltinType::LongDouble: return LongDoubleRank;
1041 }
1042}
1043
Steve Narofffa0c4532007-08-27 01:41:48 +00001044/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1045/// point or a complex type (based on typeDomain/typeSize).
1046/// 'typeDomain' is a real floating point or complex type.
1047/// 'typeSize' is a real floating point or complex type.
Steve Naroff3cf497f2007-08-27 01:27:54 +00001048QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
1049 QualType typeSize, QualType typeDomain) const {
1050 if (typeDomain->isComplexType()) {
1051 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001052 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001053 case FloatRank: return FloatComplexTy;
1054 case DoubleRank: return DoubleComplexTy;
1055 case LongDoubleRank: return LongDoubleComplexTy;
1056 }
Chris Lattner4b009652007-07-25 00:24:17 +00001057 }
Steve Naroff3cf497f2007-08-27 01:27:54 +00001058 if (typeDomain->isRealFloatingType()) {
1059 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001060 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001061 case FloatRank: return FloatTy;
1062 case DoubleRank: return DoubleTy;
1063 case LongDoubleRank: return LongDoubleTy;
1064 }
1065 }
1066 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattner1d2b4612007-09-16 19:23:47 +00001067 //an invalid return value, but the assert
1068 //will ensure that this code is never reached.
1069 return VoidTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001070}
1071
Steve Naroff45fc9822007-08-27 15:30:22 +00001072/// compareFloatingType - Handles 3 different combos:
1073/// float/float, float/complex, complex/complex.
1074/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
1075int ASTContext::compareFloatingType(QualType lt, QualType rt) {
1076 if (getFloatingRank(lt) == getFloatingRank(rt))
1077 return 0;
1078 if (getFloatingRank(lt) > getFloatingRank(rt))
1079 return 1;
1080 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001081}
1082
1083// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
1084// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
1085QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
1086 if (lhs == rhs) return lhs;
1087
1088 bool t1Unsigned = lhs->isUnsignedIntegerType();
1089 bool t2Unsigned = rhs->isUnsignedIntegerType();
1090
1091 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
1092 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
1093
1094 // We have two integer types with differing signs
1095 QualType unsignedType = t1Unsigned ? lhs : rhs;
1096 QualType signedType = t1Unsigned ? rhs : lhs;
1097
1098 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
1099 return unsignedType;
1100 else {
1101 // FIXME: Need to check if the signed type can represent all values of the
1102 // unsigned type. If it can, then the result is the signed type.
1103 // If it can't, then the result is the unsigned version of the signed type.
1104 // Should probably add a helper that returns a signed integer type from
1105 // an unsigned (and vice versa). C99 6.3.1.8.
1106 return signedType;
1107 }
1108}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001109
1110// getCFConstantStringType - Return the type used for constant CFStrings.
1111QualType ASTContext::getCFConstantStringType() {
1112 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001113 CFConstantStringTypeDecl =
Chris Lattnereee57c02008-04-04 06:12:32 +00001114 RecordDecl::Create(*this, Decl::Struct, NULL, SourceLocation(),
Chris Lattner58114f02008-03-15 21:32:50 +00001115 &Idents.get("NSConstantString"), 0);
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001116 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001117
1118 // const int *isa;
1119 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001120 // int flags;
1121 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001122 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001123 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001124 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001125 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001126 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001127 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001128
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001129 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerf3874bc2008-04-06 04:47:34 +00001130 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner81db64a2008-03-16 00:16:02 +00001131 FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001132
1133 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1134 }
1135
1136 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001137}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001138
Anders Carlssone3f02572007-10-29 06:33:42 +00001139// This returns true if a type has been typedefed to BOOL:
1140// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001141static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001142 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001143 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001144
1145 return false;
1146}
1147
Ted Kremenek42730c52008-01-07 19:49:32 +00001148/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001149/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001150int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001151 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001152
1153 // Make all integer and enum types at least as large as an int
1154 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001155 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001156 // Treat arrays as pointers, since that's how they're passed in.
1157 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001158 sz = getTypeSize(VoidPtrTy);
1159 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001160}
1161
Ted Kremenek42730c52008-01-07 19:49:32 +00001162/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001163/// declaration.
Ted Kremenek42730c52008-01-07 19:49:32 +00001164void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001165 std::string& S)
1166{
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001167 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001168 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001169 // Encode result type.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001170 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001171 // Compute size of all parameters.
1172 // Start with computing size of a pointer in number of bytes.
1173 // FIXME: There might(should) be a better way of doing this computation!
1174 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001175 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001176 // The first two arguments (self and _cmd) are pointers; account for
1177 // their size.
1178 int ParmOffset = 2 * PtrSize;
1179 int NumOfParams = Decl->getNumParams();
1180 for (int i = 0; i < NumOfParams; i++) {
1181 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001182 int sz = getObjCEncodingTypeSize (PType);
1183 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001184 ParmOffset += sz;
1185 }
1186 S += llvm::utostr(ParmOffset);
1187 S += "@0:";
1188 S += llvm::utostr(PtrSize);
1189
1190 // Argument types.
1191 ParmOffset = 2 * PtrSize;
1192 for (int i = 0; i < NumOfParams; i++) {
1193 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001194 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001195 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001196 getObjCEncodingForTypeQualifier(
1197 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian248db262008-01-22 22:44:46 +00001198 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001199 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001200 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001201 }
1202}
1203
Fariborz Jahanian248db262008-01-22 22:44:46 +00001204void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1205 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson36f07d82007-10-29 05:01:08 +00001206{
Anders Carlssone3f02572007-10-29 06:33:42 +00001207 // FIXME: This currently doesn't encode:
1208 // @ An object (whether statically typed or typed id)
1209 // # A class object (Class)
1210 // : A method selector (SEL)
1211 // {name=type...} A structure
1212 // (name=type...) A union
1213 // bnum A bit field of num bits
1214
1215 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001216 char encoding;
1217 switch (BT->getKind()) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001218 default: assert(0 && "Unhandled builtin type kind");
1219 case BuiltinType::Void: encoding = 'v'; break;
1220 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001221 case BuiltinType::Char_U:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001222 case BuiltinType::UChar: encoding = 'C'; break;
1223 case BuiltinType::UShort: encoding = 'S'; break;
1224 case BuiltinType::UInt: encoding = 'I'; break;
1225 case BuiltinType::ULong: encoding = 'L'; break;
1226 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001227 case BuiltinType::Char_S:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001228 case BuiltinType::SChar: encoding = 'c'; break;
1229 case BuiltinType::Short: encoding = 's'; break;
1230 case BuiltinType::Int: encoding = 'i'; break;
1231 case BuiltinType::Long: encoding = 'l'; break;
1232 case BuiltinType::LongLong: encoding = 'q'; break;
1233 case BuiltinType::Float: encoding = 'f'; break;
1234 case BuiltinType::Double: encoding = 'd'; break;
1235 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001236 }
1237
1238 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001239 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001240 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001241 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001242 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001243
1244 }
1245 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001246 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001247 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001248 S += '@';
1249 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001250 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001251 S += '#';
1252 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001253 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001254 S += ':';
1255 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001256 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001257
1258 if (PointeeTy->isCharType()) {
1259 // char pointer types should be encoded as '*' unless it is a
1260 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001261 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001262 S += '*';
1263 return;
1264 }
1265 }
1266
1267 S += '^';
Fariborz Jahanian248db262008-01-22 22:44:46 +00001268 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone3f02572007-10-29 06:33:42 +00001269 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001270 S += '[';
1271
1272 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1273 S += llvm::utostr(CAT->getSize().getZExtValue());
1274 else
1275 assert(0 && "Unhandled array type!");
1276
Fariborz Jahanian248db262008-01-22 22:44:46 +00001277 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001278 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001279 } else if (T->getAsFunctionType()) {
1280 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001281 } else if (const RecordType *RTy = T->getAsRecordType()) {
1282 RecordDecl *RDecl= RTy->getDecl();
1283 S += '{';
1284 S += RDecl->getName();
Fariborz Jahanian248db262008-01-22 22:44:46 +00001285 bool found = false;
1286 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1287 if (ERType[i] == RTy) {
1288 found = true;
1289 break;
1290 }
1291 if (!found) {
1292 ERType.push_back(RTy);
1293 S += '=';
1294 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1295 FieldDecl *field = RDecl->getMember(i);
1296 getObjCEncodingForType(field->getType(), S, ERType);
1297 }
1298 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1299 ERType.pop_back();
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001300 }
1301 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001302 } else if (T->isEnumeralType()) {
1303 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001304 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001305 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001306}
1307
Ted Kremenek42730c52008-01-07 19:49:32 +00001308void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001309 std::string& S) const {
1310 if (QT & Decl::OBJC_TQ_In)
1311 S += 'n';
1312 if (QT & Decl::OBJC_TQ_Inout)
1313 S += 'N';
1314 if (QT & Decl::OBJC_TQ_Out)
1315 S += 'o';
1316 if (QT & Decl::OBJC_TQ_Bycopy)
1317 S += 'O';
1318 if (QT & Decl::OBJC_TQ_Byref)
1319 S += 'R';
1320 if (QT & Decl::OBJC_TQ_Oneway)
1321 S += 'V';
1322}
1323
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001324void ASTContext::setBuiltinVaListType(QualType T)
1325{
1326 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1327
1328 BuiltinVaListType = T;
1329}
1330
Ted Kremenek42730c52008-01-07 19:49:32 +00001331void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001332{
Ted Kremenek42730c52008-01-07 19:49:32 +00001333 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff9d12c902007-10-15 14:41:52 +00001334
Ted Kremenek42730c52008-01-07 19:49:32 +00001335 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001336
1337 // typedef struct objc_object *id;
1338 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1339 assert(ptr && "'id' incorrectly typed");
1340 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1341 assert(rec && "'id' incorrectly typed");
1342 IdStructType = rec;
1343}
1344
Ted Kremenek42730c52008-01-07 19:49:32 +00001345void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001346{
Ted Kremenek42730c52008-01-07 19:49:32 +00001347 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001348
Ted Kremenek42730c52008-01-07 19:49:32 +00001349 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001350
1351 // typedef struct objc_selector *SEL;
1352 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1353 assert(ptr && "'SEL' incorrectly typed");
1354 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1355 assert(rec && "'SEL' incorrectly typed");
1356 SelStructType = rec;
1357}
1358
Ted Kremenek42730c52008-01-07 19:49:32 +00001359void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001360{
Ted Kremenek42730c52008-01-07 19:49:32 +00001361 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1362 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001363}
1364
Ted Kremenek42730c52008-01-07 19:49:32 +00001365void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001366{
Ted Kremenek42730c52008-01-07 19:49:32 +00001367 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001368
Ted Kremenek42730c52008-01-07 19:49:32 +00001369 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001370
1371 // typedef struct objc_class *Class;
1372 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1373 assert(ptr && "'Class' incorrectly typed");
1374 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1375 assert(rec && "'Class' incorrectly typed");
1376 ClassStructType = rec;
1377}
1378
Ted Kremenek42730c52008-01-07 19:49:32 +00001379void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1380 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001381 "'NSConstantString' type already set!");
1382
Ted Kremenek42730c52008-01-07 19:49:32 +00001383 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001384}
1385
Steve Naroff85f0dc52007-10-15 20:41:53 +00001386bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1387 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1388 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1389
1390 return lBuiltin->getKind() == rBuiltin->getKind();
1391}
1392
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001393/// objcTypesAreCompatible - This routine is called when two types
1394/// are of different class; one is interface type or is
1395/// a qualified interface type and the other type is of a different class.
1396/// Example, II or II<P>.
Steve Naroff85f0dc52007-10-15 20:41:53 +00001397bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001398 if (lhs->isObjCInterfaceType() && isObjCIdType(rhs))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001399 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001400 else if (isObjCIdType(lhs) && rhs->isObjCInterfaceType())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001401 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001402 if (ObjCInterfaceType *lhsIT =
1403 dyn_cast<ObjCInterfaceType>(lhs.getCanonicalType().getTypePtr())) {
1404 ObjCQualifiedInterfaceType *rhsQI =
1405 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001406 return rhsQI && (lhsIT->getDecl() == rhsQI->getDecl());
1407 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001408 else if (ObjCInterfaceType *rhsIT =
1409 dyn_cast<ObjCInterfaceType>(rhs.getCanonicalType().getTypePtr())) {
1410 ObjCQualifiedInterfaceType *lhsQI =
1411 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001412 return lhsQI && (rhsIT->getDecl() == lhsQI->getDecl());
1413 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00001414 return false;
1415}
1416
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001417/// Check that 'lhs' and 'rhs' are compatible interface types. Both types
1418/// must be canonical types.
Steve Naroff85f0dc52007-10-15 20:41:53 +00001419bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001420 assert (lhs->isCanonical() &&
1421 "interfaceTypesAreCompatible strip typedefs of lhs");
1422 assert (rhs->isCanonical() &&
1423 "interfaceTypesAreCompatible strip typedefs of rhs");
Fariborz Jahaniance2de812007-12-20 22:37:58 +00001424 if (lhs == rhs)
1425 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001426 ObjCInterfaceType *lhsIT = cast<ObjCInterfaceType>(lhs.getTypePtr());
1427 ObjCInterfaceType *rhsIT = cast<ObjCInterfaceType>(rhs.getTypePtr());
1428 ObjCInterfaceDecl *rhsIDecl = rhsIT->getDecl();
1429 ObjCInterfaceDecl *lhsIDecl = lhsIT->getDecl();
Fariborz Jahaniance2de812007-12-20 22:37:58 +00001430 // rhs is derived from lhs it is OK; else it is not OK.
1431 while (rhsIDecl != NULL) {
1432 if (rhsIDecl == lhsIDecl)
1433 return true;
1434 rhsIDecl = rhsIDecl->getSuperClass();
1435 }
1436 return false;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001437}
1438
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001439bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
1440 QualType rhs) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001441 ObjCQualifiedInterfaceType *lhsQI =
1442 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001443 assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
Ted Kremenek42730c52008-01-07 19:49:32 +00001444 ObjCQualifiedInterfaceType *rhsQI =
1445 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001446 assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001447 if (!interfaceTypesAreCompatible(
1448 getObjCInterfaceType(lhsQI->getDecl()).getCanonicalType(),
1449 getObjCInterfaceType(rhsQI->getDecl()).getCanonicalType()))
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001450 return false;
1451 /* All protocols in lhs must have a presense in rhs. */
1452 for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
1453 bool match = false;
Ted Kremenek42730c52008-01-07 19:49:32 +00001454 ObjCProtocolDecl *lhsProto = lhsQI->getProtocols(i);
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001455 for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001456 ObjCProtocolDecl *rhsProto = rhsQI->getProtocols(j);
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001457 if (lhsProto == rhsProto) {
1458 match = true;
1459 break;
1460 }
1461 }
1462 if (!match)
1463 return false;
1464 }
1465 return true;
1466}
1467
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001468/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
1469/// inheritance hierarchy of 'rProto'.
Ted Kremenek42730c52008-01-07 19:49:32 +00001470static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
1471 ObjCProtocolDecl *rProto) {
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001472 if (lProto == rProto)
1473 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001474 ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001475 for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
1476 if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
1477 return true;
1478 return false;
1479}
1480
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001481/// ClassImplementsProtocol - Checks that 'lProto' protocol
1482/// has been implemented in IDecl class, its super class or categories (if
1483/// lookupCategory is true).
Ted Kremenek42730c52008-01-07 19:49:32 +00001484static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1485 ObjCInterfaceDecl *IDecl,
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001486 bool lookupCategory) {
1487
1488 // 1st, look up the class.
Ted Kremenek42730c52008-01-07 19:49:32 +00001489 ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols();
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001490 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
1491 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1492 return true;
1493 }
1494
1495 // 2nd, look up the category.
1496 if (lookupCategory)
Ted Kremenek42730c52008-01-07 19:49:32 +00001497 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001498 CDecl = CDecl->getNextClassCategory()) {
1499 protoList = CDecl->getReferencedProtocols();
1500 for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
1501 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1502 return true;
1503 }
1504 }
1505
1506 // 3rd, look up the super class(s)
1507 if (IDecl->getSuperClass())
1508 return
1509 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory);
1510
1511 return false;
1512}
1513
Ted Kremenek42730c52008-01-07 19:49:32 +00001514/// ObjCQualifiedIdTypesAreCompatible - Compares two types, at least
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001515/// one of which is a protocol qualified 'id' type. When 'compare'
1516/// is true it is for comparison; when false, for assignment/initialization.
Ted Kremenek42730c52008-01-07 19:49:32 +00001517bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs,
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001518 QualType rhs,
1519 bool compare) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001520 // match id<P..> with an 'id' type in all cases.
1521 if (const PointerType *PT = lhs->getAsPointerType()) {
1522 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001523 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001524 return true;
1525
1526 }
1527 else if (const PointerType *PT = rhs->getAsPointerType()) {
1528 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001529 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001530 return true;
1531
1532 }
1533
Ted Kremenek42730c52008-01-07 19:49:32 +00001534 ObjCQualifiedInterfaceType *lhsQI = 0;
1535 ObjCQualifiedInterfaceType *rhsQI = 0;
1536 ObjCInterfaceDecl *lhsID = 0;
1537 ObjCInterfaceDecl *rhsID = 0;
1538 ObjCQualifiedIdType *lhsQID = dyn_cast<ObjCQualifiedIdType>(lhs);
1539 ObjCQualifiedIdType *rhsQID = dyn_cast<ObjCQualifiedIdType>(rhs);
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001540
1541 if (lhsQID) {
1542 if (!rhsQID && rhs->getTypeClass() == Type::Pointer) {
1543 QualType rtype =
1544 cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1545 rhsQI =
Ted Kremenek42730c52008-01-07 19:49:32 +00001546 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001547 rtype.getCanonicalType().getTypePtr());
Fariborz Jahanian87829072007-12-20 19:24:10 +00001548 if (!rhsQI) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001549 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanian87829072007-12-20 19:24:10 +00001550 rtype.getCanonicalType().getTypePtr());
1551 if (IT)
1552 rhsID = IT->getDecl();
1553 }
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001554 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001555 if (!rhsQI && !rhsQID && !rhsID)
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001556 return false;
1557
Fariborz Jahaniance5528d2008-01-03 20:01:35 +00001558 unsigned numRhsProtocols = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001559 ObjCProtocolDecl **rhsProtoList = 0;
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001560 if (rhsQI) {
1561 numRhsProtocols = rhsQI->getNumProtocols();
1562 rhsProtoList = rhsQI->getReferencedProtocols();
1563 }
1564 else if (rhsQID) {
1565 numRhsProtocols = rhsQID->getNumProtocols();
1566 rhsProtoList = rhsQID->getReferencedProtocols();
1567 }
1568
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001569 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001570 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001571 bool match = false;
1572
1573 // when comparing an id<P> on lhs with a static type on rhs,
1574 // see if static class implements all of id's protocols, directly or
1575 // through its super class and categories.
1576 if (rhsID) {
1577 if (ClassImplementsProtocol(lhsProto, rhsID, true))
1578 match = true;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001579 }
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001580 else for (unsigned j = 0; j < numRhsProtocols; j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001581 ObjCProtocolDecl *rhsProto = rhsProtoList[j];
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001582 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1583 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001584 match = true;
1585 break;
1586 }
1587 }
1588 if (!match)
1589 return false;
1590 }
1591 }
1592 else if (rhsQID) {
1593 if (!lhsQID && lhs->getTypeClass() == Type::Pointer) {
1594 QualType ltype =
1595 cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1596 lhsQI =
Ted Kremenek42730c52008-01-07 19:49:32 +00001597 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001598 ltype.getCanonicalType().getTypePtr());
Fariborz Jahanian87829072007-12-20 19:24:10 +00001599 if (!lhsQI) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001600 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanian87829072007-12-20 19:24:10 +00001601 ltype.getCanonicalType().getTypePtr());
1602 if (IT)
1603 lhsID = IT->getDecl();
1604 }
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001605 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001606 if (!lhsQI && !lhsQID && !lhsID)
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001607 return false;
Fariborz Jahanian87829072007-12-20 19:24:10 +00001608
Fariborz Jahaniance5528d2008-01-03 20:01:35 +00001609 unsigned numLhsProtocols = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001610 ObjCProtocolDecl **lhsProtoList = 0;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001611 if (lhsQI) {
1612 numLhsProtocols = lhsQI->getNumProtocols();
1613 lhsProtoList = lhsQI->getReferencedProtocols();
1614 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001615 else if (lhsQID) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001616 numLhsProtocols = lhsQID->getNumProtocols();
1617 lhsProtoList = lhsQID->getReferencedProtocols();
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001618 }
1619 bool match = false;
1620 // for static type vs. qualified 'id' type, check that class implements
1621 // one of 'id's protocols.
1622 if (lhsID) {
1623 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001624 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001625 if (ClassImplementsProtocol(rhsProto, lhsID, compare)) {
1626 match = true;
1627 break;
1628 }
1629 }
1630 }
1631 else for (unsigned i =0; i < numLhsProtocols; i++) {
1632 match = false;
Ted Kremenek42730c52008-01-07 19:49:32 +00001633 ObjCProtocolDecl *lhsProto = lhsProtoList[i];
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001634 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001635 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001636 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1637 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001638 match = true;
1639 break;
1640 }
1641 }
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001642 }
1643 if (!match)
1644 return false;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001645 }
1646 return true;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001647}
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001648
Chris Lattner5003e8b2007-11-01 05:03:41 +00001649bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1650 const VectorType *lVector = lhs->getAsVectorType();
1651 const VectorType *rVector = rhs->getAsVectorType();
1652
1653 if ((lVector->getElementType().getCanonicalType() ==
1654 rVector->getElementType().getCanonicalType()) &&
1655 (lVector->getNumElements() == rVector->getNumElements()))
1656 return true;
1657 return false;
1658}
1659
Steve Naroff85f0dc52007-10-15 20:41:53 +00001660// C99 6.2.7p1: If both are complete types, then the following additional
1661// requirements apply...FIXME (handle compatibility across source files).
1662bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
Steve Naroff4a5e2072007-11-07 06:03:51 +00001663 // "Class" and "id" are compatible built-in structure types.
Ted Kremenek42730c52008-01-07 19:49:32 +00001664 if (isObjCIdType(lhs) && isObjCClassType(rhs) ||
1665 isObjCClassType(lhs) && isObjCIdType(rhs))
Steve Naroff4a5e2072007-11-07 06:03:51 +00001666 return true;
Eli Friedmane7fb03a2008-02-15 06:03:44 +00001667
1668 // Within a translation unit a tag type is
1669 // only compatible with itself.
1670 return lhs.getCanonicalType() == rhs.getCanonicalType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001671}
1672
1673bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1674 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1675 // identically qualified and both shall be pointers to compatible types.
Chris Lattner35fef522008-02-20 20:55:12 +00001676 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1677 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001678 return false;
1679
1680 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1681 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1682
1683 return typesAreCompatible(ltype, rtype);
1684}
1685
Bill Wendling6a9d8542007-12-03 07:33:35 +00001686// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroff85f0dc52007-10-15 20:41:53 +00001687// reference to T, the operation assigns to the object of type T denoted by the
1688// reference.
1689bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1690 QualType ltype = lhs;
1691
1692 if (lhs->isReferenceType())
Chris Lattnercfac88d2008-04-02 17:35:06 +00001693 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getPointeeType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001694
1695 QualType rtype = rhs;
1696
1697 if (rhs->isReferenceType())
Chris Lattnercfac88d2008-04-02 17:35:06 +00001698 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getPointeeType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001699
1700 return typesAreCompatible(ltype, rtype);
1701}
1702
1703bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1704 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1705 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1706 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1707 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1708
1709 // first check the return types (common between C99 and K&R).
1710 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1711 return false;
1712
1713 if (lproto && rproto) { // two C99 style function prototypes
1714 unsigned lproto_nargs = lproto->getNumArgs();
1715 unsigned rproto_nargs = rproto->getNumArgs();
1716
1717 if (lproto_nargs != rproto_nargs)
1718 return false;
1719
1720 // both prototypes have the same number of arguments.
1721 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1722 (rproto->isVariadic() && !lproto->isVariadic()))
1723 return false;
1724
1725 // The use of ellipsis agree...now check the argument types.
1726 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001727 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1728 // is taken as having the unqualified version of it's declared type.
Steve Naroffdec17fe2008-01-29 00:15:50 +00001729 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001730 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001731 return false;
1732 return true;
1733 }
1734 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1735 return true;
1736
1737 // we have a mixture of K&R style with C99 prototypes
1738 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1739
1740 if (proto->isVariadic())
1741 return false;
1742
1743 // FIXME: Each parameter type T in the prototype must be compatible with the
1744 // type resulting from applying the usual argument conversions to T.
1745 return true;
1746}
1747
1748bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
Eli Friedman1e7537832008-02-06 04:53:22 +00001749 // Compatible arrays must have compatible element types
1750 QualType ltype = lhs->getAsArrayType()->getElementType();
1751 QualType rtype = rhs->getAsArrayType()->getElementType();
1752
Steve Naroff85f0dc52007-10-15 20:41:53 +00001753 if (!typesAreCompatible(ltype, rtype))
1754 return false;
Eli Friedman1e7537832008-02-06 04:53:22 +00001755
1756 // Compatible arrays must be the same size
1757 if (const ConstantArrayType* LCAT = lhs->getAsConstantArrayType())
1758 if (const ConstantArrayType* RCAT = rhs->getAsConstantArrayType())
1759 return RCAT->getSize() == LCAT->getSize();
1760
Steve Naroff85f0dc52007-10-15 20:41:53 +00001761 return true;
1762}
1763
1764/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1765/// both shall have the identically qualified version of a compatible type.
1766/// C99 6.2.7p1: Two types have compatible types if their types are the
1767/// same. See 6.7.[2,3,5] for additional rules.
1768bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
1769 QualType lcanon = lhs.getCanonicalType();
1770 QualType rcanon = rhs.getCanonicalType();
Chris Lattner4d5670b2008-04-03 05:07:04 +00001771
Steve Naroff85f0dc52007-10-15 20:41:53 +00001772 // If two types are identical, they are are compatible
1773 if (lcanon == rcanon)
1774 return true;
Chris Lattner4d5670b2008-04-03 05:07:04 +00001775
1776 if (lcanon.getCVRQualifiers() != rcanon.getCVRQualifiers() ||
1777 lcanon.getAddressSpace() != rcanon.getAddressSpace())
1778 return false;
Bill Wendling6a9d8542007-12-03 07:33:35 +00001779
1780 // C++ [expr]: If an expression initially has the type "reference to T", the
1781 // type is adjusted to "T" prior to any further analysis, the expression
1782 // designates the object or function denoted by the reference, and the
1783 // expression is an lvalue.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001784 if (ReferenceType *RT = dyn_cast<ReferenceType>(lcanon))
Chris Lattnercfac88d2008-04-02 17:35:06 +00001785 lcanon = RT->getPointeeType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001786 if (ReferenceType *RT = dyn_cast<ReferenceType>(rcanon))
Chris Lattnercfac88d2008-04-02 17:35:06 +00001787 rcanon = RT->getPointeeType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001788
1789 Type::TypeClass LHSClass = lcanon->getTypeClass();
1790 Type::TypeClass RHSClass = rcanon->getTypeClass();
1791
1792 // We want to consider the two function types to be the same for these
1793 // comparisons, just force one to the other.
1794 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1795 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00001796
1797 // Same as above for arrays
1798 if (LHSClass == Type::VariableArray) LHSClass = Type::ConstantArray;
1799 if (RHSClass == Type::VariableArray) RHSClass = Type::ConstantArray;
Eli Friedman8ff07782008-02-15 18:16:39 +00001800 if (LHSClass == Type::IncompleteArray) LHSClass = Type::ConstantArray;
1801 if (RHSClass == Type::IncompleteArray) RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001802
Steve Naroffc88babe2008-01-09 22:43:08 +00001803 // If the canonical type classes don't match...
Chris Lattnerc38d4522008-01-14 05:45:46 +00001804 if (LHSClass != RHSClass) {
Steve Naroff85f0dc52007-10-15 20:41:53 +00001805 // For Objective-C, it is possible for two types to be compatible
1806 // when their classes don't match (when dealing with "id"). If either type
1807 // is an interface, we defer to objcTypesAreCompatible().
Ted Kremenek42730c52008-01-07 19:49:32 +00001808 if (lcanon->isObjCInterfaceType() || rcanon->isObjCInterfaceType())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001809 return objcTypesAreCompatible(lcanon, rcanon);
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001810
Chris Lattnerc38d4522008-01-14 05:45:46 +00001811 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1812 // a signed integer type, or an unsigned integer type.
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001813 if (lcanon->isEnumeralType() && rcanon->isIntegralType()) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001814 EnumDecl* EDecl = cast<EnumType>(lcanon)->getDecl();
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001815 return EDecl->getIntegerType() == rcanon;
1816 }
1817 if (rcanon->isEnumeralType() && lcanon->isIntegralType()) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001818 EnumDecl* EDecl = cast<EnumType>(rcanon)->getDecl();
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001819 return EDecl->getIntegerType() == lcanon;
1820 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001821
Steve Naroff85f0dc52007-10-15 20:41:53 +00001822 return false;
1823 }
Steve Naroffc88babe2008-01-09 22:43:08 +00001824 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001825 switch (LHSClass) {
1826 case Type::FunctionProto: assert(0 && "Canonicalized away above");
1827 case Type::Pointer:
1828 return pointerTypesAreCompatible(lcanon, rcanon);
1829 case Type::ConstantArray:
1830 case Type::VariableArray:
Eli Friedman8ff07782008-02-15 18:16:39 +00001831 case Type::IncompleteArray:
Chris Lattnerc38d4522008-01-14 05:45:46 +00001832 return arrayTypesAreCompatible(lcanon, rcanon);
1833 case Type::FunctionNoProto:
1834 return functionTypesAreCompatible(lcanon, rcanon);
1835 case Type::Tagged: // handle structures, unions
1836 return tagTypesAreCompatible(lcanon, rcanon);
1837 case Type::Builtin:
1838 return builtinTypesAreCompatible(lcanon, rcanon);
1839 case Type::ObjCInterface:
1840 return interfaceTypesAreCompatible(lcanon, rcanon);
1841 case Type::Vector:
1842 case Type::OCUVector:
1843 return vectorTypesAreCompatible(lcanon, rcanon);
1844 case Type::ObjCQualifiedInterface:
1845 return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
1846 default:
1847 assert(0 && "unexpected type");
Steve Naroff85f0dc52007-10-15 20:41:53 +00001848 }
1849 return true; // should never get here...
1850}
Ted Kremenek738e6c02007-10-31 17:10:13 +00001851
Ted Kremenek738e6c02007-10-31 17:10:13 +00001852/// Emit - Serialize an ASTContext object to Bitcode.
1853void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00001854 S.EmitRef(SourceMgr);
1855 S.EmitRef(Target);
1856 S.EmitRef(Idents);
1857 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001858
Ted Kremenek68228a92007-10-31 22:44:07 +00001859 // Emit the size of the type vector so that we can reserve that size
1860 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001861 S.EmitInt(Types.size());
1862
Ted Kremenek034a78c2007-11-13 22:02:55 +00001863 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1864 I!=E;++I)
1865 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001866
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001867 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001868}
1869
Ted Kremenekacba3612007-11-13 00:25:37 +00001870ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek68228a92007-10-31 22:44:07 +00001871 SourceManager &SM = D.ReadRef<SourceManager>();
1872 TargetInfo &t = D.ReadRef<TargetInfo>();
1873 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1874 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00001875
Ted Kremenek68228a92007-10-31 22:44:07 +00001876 unsigned size_reserve = D.ReadInt();
1877
1878 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1879
Ted Kremenek034a78c2007-11-13 22:02:55 +00001880 for (unsigned i = 0; i < size_reserve; ++i)
1881 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00001882
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001883 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00001884
1885 return A;
1886}