blob: bb03479a012483ba2a6ecde7df94f01e99239ed1 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Basic/TargetInfo.h"
18#include "llvm/ADT/SmallVector.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000019#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000020#include "llvm/Bitcode/Serialize.h"
21#include "llvm/Bitcode/Deserialize.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000022
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24
25enum FloatingRank {
26 FloatRank, DoubleRank, LongDoubleRank
27};
28
29ASTContext::~ASTContext() {
30 // Deallocate all the types.
31 while (!Types.empty()) {
32 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) {
33 // Destroy the object, but don't call delete. These are malloc'd.
34 FT->~FunctionTypeProto();
35 free(FT);
36 } else {
37 delete Types.back();
38 }
39 Types.pop_back();
40 }
41}
42
43void ASTContext::PrintStats() const {
44 fprintf(stderr, "*** AST Context Stats:\n");
45 fprintf(stderr, " %d types total.\n", (int)Types.size());
46 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
47 unsigned NumVector = 0, NumComplex = 0;
48 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
49
50 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000051 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
52 unsigned NumObjCQualifiedIds = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000053
54 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
55 Type *T = Types[i];
56 if (isa<BuiltinType>(T))
57 ++NumBuiltin;
58 else if (isa<PointerType>(T))
59 ++NumPointer;
60 else if (isa<ReferenceType>(T))
61 ++NumReference;
62 else if (isa<ComplexType>(T))
63 ++NumComplex;
64 else if (isa<ArrayType>(T))
65 ++NumArray;
66 else if (isa<VectorType>(T))
67 ++NumVector;
68 else if (isa<FunctionTypeNoProto>(T))
69 ++NumFunctionNP;
70 else if (isa<FunctionTypeProto>(T))
71 ++NumFunctionP;
72 else if (isa<TypedefType>(T))
73 ++NumTypeName;
74 else if (TagType *TT = dyn_cast<TagType>(T)) {
75 ++NumTagged;
76 switch (TT->getDecl()->getKind()) {
77 default: assert(0 && "Unknown tagged type!");
78 case Decl::Struct: ++NumTagStruct; break;
79 case Decl::Union: ++NumTagUnion; break;
80 case Decl::Class: ++NumTagClass; break;
81 case Decl::Enum: ++NumTagEnum; break;
82 }
Ted Kremenek42730c52008-01-07 19:49:32 +000083 } else if (isa<ObjCInterfaceType>(T))
84 ++NumObjCInterfaces;
85 else if (isa<ObjCQualifiedInterfaceType>(T))
86 ++NumObjCQualifiedInterfaces;
87 else if (isa<ObjCQualifiedIdType>(T))
88 ++NumObjCQualifiedIds;
Steve Naroff948fd372007-09-17 14:16:13 +000089 else {
Chris Lattner8a35b462007-12-12 06:43:05 +000090 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +000091 assert(0 && "Unknown type!");
92 }
93 }
94
95 fprintf(stderr, " %d builtin types\n", NumBuiltin);
96 fprintf(stderr, " %d pointer types\n", NumPointer);
97 fprintf(stderr, " %d reference types\n", NumReference);
98 fprintf(stderr, " %d complex types\n", NumComplex);
99 fprintf(stderr, " %d array types\n", NumArray);
100 fprintf(stderr, " %d vector types\n", NumVector);
101 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
102 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
103 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
104 fprintf(stderr, " %d tagged types\n", NumTagged);
105 fprintf(stderr, " %d struct types\n", NumTagStruct);
106 fprintf(stderr, " %d union types\n", NumTagUnion);
107 fprintf(stderr, " %d class types\n", NumTagClass);
108 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000109 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000110 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000111 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000112 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000113 NumObjCQualifiedIds);
Chris Lattner4b009652007-07-25 00:24:17 +0000114 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
115 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
116 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
117 NumFunctionP*sizeof(FunctionTypeProto)+
118 NumFunctionNP*sizeof(FunctionTypeNoProto)+
119 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
120}
121
122
123void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
124 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
125}
126
Chris Lattner4b009652007-07-25 00:24:17 +0000127void ASTContext::InitBuiltinTypes() {
128 assert(VoidTy.isNull() && "Context reinitialized?");
129
130 // C99 6.2.5p19.
131 InitBuiltinType(VoidTy, BuiltinType::Void);
132
133 // C99 6.2.5p2.
134 InitBuiltinType(BoolTy, BuiltinType::Bool);
135 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000136 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000137 InitBuiltinType(CharTy, BuiltinType::Char_S);
138 else
139 InitBuiltinType(CharTy, BuiltinType::Char_U);
140 // C99 6.2.5p4.
141 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
142 InitBuiltinType(ShortTy, BuiltinType::Short);
143 InitBuiltinType(IntTy, BuiltinType::Int);
144 InitBuiltinType(LongTy, BuiltinType::Long);
145 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
146
147 // C99 6.2.5p6.
148 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
149 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
150 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
151 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
152 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
153
154 // C99 6.2.5p10.
155 InitBuiltinType(FloatTy, BuiltinType::Float);
156 InitBuiltinType(DoubleTy, BuiltinType::Double);
157 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
158
159 // C99 6.2.5p11.
160 FloatComplexTy = getComplexType(FloatTy);
161 DoubleComplexTy = getComplexType(DoubleTy);
162 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff9d12c902007-10-15 14:41:52 +0000163
164 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000165 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000166 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000167 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000168 ClassStructType = 0;
169
Ted Kremenek42730c52008-01-07 19:49:32 +0000170 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000171
172 // void * type
173 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000174}
175
176//===----------------------------------------------------------------------===//
177// Type Sizing and Analysis
178//===----------------------------------------------------------------------===//
179
180/// getTypeSize - Return the size of the specified type, in bits. This method
181/// does not work on incomplete types.
182std::pair<uint64_t, unsigned>
Chris Lattner8cd0e932008-03-05 18:54:05 +0000183ASTContext::getTypeInfo(QualType T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000184 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000185 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000186 unsigned Align;
187 switch (T->getTypeClass()) {
188 case Type::TypeName: assert(0 && "Not a canonical type!");
189 case Type::FunctionNoProto:
190 case Type::FunctionProto:
191 default:
192 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000193 case Type::VariableArray:
194 assert(0 && "VLAs not implemented yet!");
195 case Type::ConstantArray: {
196 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
197
Chris Lattner8cd0e932008-03-05 18:54:05 +0000198 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000199 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000200 Align = EltInfo.second;
201 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000202 }
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 Lattnerc1b68db2008-04-06 22:59:24 +0000448 QualType CanT = getCanonicalType(T);
449 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000450 return T;
451
452 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
453 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000454 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000455 "Type is already address space qualified");
456
457 // Check if we've already instantiated an address space qual'd type of this
458 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000459 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000460 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000461 void *InsertPos = 0;
462 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
463 return QualType(ASQy, 0);
464
465 // If the base type isn't canonical, this won't be a canonical type either,
466 // so fill in the canonical type field.
467 QualType Canonical;
468 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000469 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000470
471 // Get the new insert position for the node we care about.
472 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
473 assert(NewIP == 0 && "Shouldn't be in the map!");
474 }
Chris Lattner35fef522008-02-20 20:55:12 +0000475 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000476 ASQualTypes.InsertNode(New, InsertPos);
477 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000478 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000479}
480
Chris Lattner4b009652007-07-25 00:24:17 +0000481
482/// getComplexType - Return the uniqued reference to the type for a complex
483/// number with the specified element type.
484QualType ASTContext::getComplexType(QualType T) {
485 // Unique pointers, to guarantee there is only one pointer of a particular
486 // structure.
487 llvm::FoldingSetNodeID ID;
488 ComplexType::Profile(ID, T);
489
490 void *InsertPos = 0;
491 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
492 return QualType(CT, 0);
493
494 // If the pointee type isn't canonical, this won't be a canonical type either,
495 // so fill in the canonical type field.
496 QualType Canonical;
497 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000498 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000499
500 // Get the new insert position for the node we care about.
501 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
502 assert(NewIP == 0 && "Shouldn't be in the map!");
503 }
504 ComplexType *New = new ComplexType(T, Canonical);
505 Types.push_back(New);
506 ComplexTypes.InsertNode(New, InsertPos);
507 return QualType(New, 0);
508}
509
510
511/// getPointerType - Return the uniqued reference to the type for a pointer to
512/// the specified type.
513QualType ASTContext::getPointerType(QualType T) {
514 // Unique pointers, to guarantee there is only one pointer of a particular
515 // structure.
516 llvm::FoldingSetNodeID ID;
517 PointerType::Profile(ID, T);
518
519 void *InsertPos = 0;
520 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
521 return QualType(PT, 0);
522
523 // If the pointee type isn't canonical, this won't be a canonical type either,
524 // so fill in the canonical type field.
525 QualType Canonical;
526 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000527 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000528
529 // Get the new insert position for the node we care about.
530 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
531 assert(NewIP == 0 && "Shouldn't be in the map!");
532 }
533 PointerType *New = new PointerType(T, Canonical);
534 Types.push_back(New);
535 PointerTypes.InsertNode(New, InsertPos);
536 return QualType(New, 0);
537}
538
539/// getReferenceType - Return the uniqued reference to the type for a reference
540/// to the specified type.
541QualType ASTContext::getReferenceType(QualType T) {
542 // Unique pointers, to guarantee there is only one pointer of a particular
543 // structure.
544 llvm::FoldingSetNodeID ID;
545 ReferenceType::Profile(ID, T);
546
547 void *InsertPos = 0;
548 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
549 return QualType(RT, 0);
550
551 // If the referencee type isn't canonical, this won't be a canonical type
552 // either, so fill in the canonical type field.
553 QualType Canonical;
554 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000555 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000556
557 // Get the new insert position for the node we care about.
558 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
559 assert(NewIP == 0 && "Shouldn't be in the map!");
560 }
561
562 ReferenceType *New = new ReferenceType(T, Canonical);
563 Types.push_back(New);
564 ReferenceTypes.InsertNode(New, InsertPos);
565 return QualType(New, 0);
566}
567
Steve Naroff83c13012007-08-30 01:06:46 +0000568/// getConstantArrayType - Return the unique reference to the type for an
569/// array of the specified element type.
570QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000571 const llvm::APInt &ArySize,
572 ArrayType::ArraySizeModifier ASM,
573 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000574 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000575 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000576
577 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000578 if (ConstantArrayType *ATP =
579 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000580 return QualType(ATP, 0);
581
582 // If the element type isn't canonical, this won't be a canonical type either,
583 // so fill in the canonical type field.
584 QualType Canonical;
585 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000586 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000587 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000588 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000589 ConstantArrayType *NewIP =
590 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
591
Chris Lattner4b009652007-07-25 00:24:17 +0000592 assert(NewIP == 0 && "Shouldn't be in the map!");
593 }
594
Steve Naroff24c9b982007-08-30 18:10:14 +0000595 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
596 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000597 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000598 Types.push_back(New);
599 return QualType(New, 0);
600}
601
Steve Naroffe2579e32007-08-30 18:14:25 +0000602/// getVariableArrayType - Returns a non-unique reference to the type for a
603/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000604QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
605 ArrayType::ArraySizeModifier ASM,
606 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000607 // Since we don't unique expressions, it isn't possible to unique VLA's
608 // that have an expression provided for their size.
609
610 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
611 ASM, EltTypeQuals);
612
613 VariableArrayTypes.push_back(New);
614 Types.push_back(New);
615 return QualType(New, 0);
616}
617
618QualType ASTContext::getIncompleteArrayType(QualType EltTy,
619 ArrayType::ArraySizeModifier ASM,
620 unsigned EltTypeQuals) {
621 llvm::FoldingSetNodeID ID;
622 IncompleteArrayType::Profile(ID, EltTy);
623
624 void *InsertPos = 0;
625 if (IncompleteArrayType *ATP =
626 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
627 return QualType(ATP, 0);
628
629 // If the element type isn't canonical, this won't be a canonical type
630 // either, so fill in the canonical type field.
631 QualType Canonical;
632
633 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000634 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000635 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000636
637 // Get the new insert position for the node we care about.
638 IncompleteArrayType *NewIP =
639 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
640
641 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000642 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000643
644 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
645 ASM, EltTypeQuals);
646
647 IncompleteArrayTypes.InsertNode(New, InsertPos);
648 Types.push_back(New);
649 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000650}
651
Chris Lattner4b009652007-07-25 00:24:17 +0000652/// getVectorType - Return the unique reference to a vector type of
653/// the specified element type and size. VectorType must be a built-in type.
654QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
655 BuiltinType *baseType;
656
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000657 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000658 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
659
660 // Check if we've already instantiated a vector of this type.
661 llvm::FoldingSetNodeID ID;
662 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
663 void *InsertPos = 0;
664 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
665 return QualType(VTP, 0);
666
667 // If the element type isn't canonical, this won't be a canonical type either,
668 // so fill in the canonical type field.
669 QualType Canonical;
670 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000671 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000672
673 // Get the new insert position for the node we care about.
674 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
675 assert(NewIP == 0 && "Shouldn't be in the map!");
676 }
677 VectorType *New = new VectorType(vecType, NumElts, Canonical);
678 VectorTypes.InsertNode(New, InsertPos);
679 Types.push_back(New);
680 return QualType(New, 0);
681}
682
683/// getOCUVectorType - Return the unique reference to an OCU vector type of
684/// the specified element type and size. VectorType must be a built-in type.
685QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
686 BuiltinType *baseType;
687
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000688 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000689 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
690
691 // Check if we've already instantiated a vector of this type.
692 llvm::FoldingSetNodeID ID;
693 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
694 void *InsertPos = 0;
695 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
696 return QualType(VTP, 0);
697
698 // If the element type isn't canonical, this won't be a canonical type either,
699 // so fill in the canonical type field.
700 QualType Canonical;
701 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000702 Canonical = getOCUVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000703
704 // Get the new insert position for the node we care about.
705 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
706 assert(NewIP == 0 && "Shouldn't be in the map!");
707 }
708 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
709 VectorTypes.InsertNode(New, InsertPos);
710 Types.push_back(New);
711 return QualType(New, 0);
712}
713
714/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
715///
716QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
717 // Unique functions, to guarantee there is only one function of a particular
718 // structure.
719 llvm::FoldingSetNodeID ID;
720 FunctionTypeNoProto::Profile(ID, ResultTy);
721
722 void *InsertPos = 0;
723 if (FunctionTypeNoProto *FT =
724 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
725 return QualType(FT, 0);
726
727 QualType Canonical;
728 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000729 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000730
731 // Get the new insert position for the node we care about.
732 FunctionTypeNoProto *NewIP =
733 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
734 assert(NewIP == 0 && "Shouldn't be in the map!");
735 }
736
737 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
738 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000739 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000740 return QualType(New, 0);
741}
742
743/// getFunctionType - Return a normal function type with a typed argument
744/// list. isVariadic indicates whether the argument list includes '...'.
745QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
746 unsigned NumArgs, bool isVariadic) {
747 // Unique functions, to guarantee there is only one function of a particular
748 // structure.
749 llvm::FoldingSetNodeID ID;
750 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
751
752 void *InsertPos = 0;
753 if (FunctionTypeProto *FTP =
754 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
755 return QualType(FTP, 0);
756
757 // Determine whether the type being created is already canonical or not.
758 bool isCanonical = ResultTy->isCanonical();
759 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
760 if (!ArgArray[i]->isCanonical())
761 isCanonical = false;
762
763 // If this type isn't canonical, get the canonical version of it.
764 QualType Canonical;
765 if (!isCanonical) {
766 llvm::SmallVector<QualType, 16> CanonicalArgs;
767 CanonicalArgs.reserve(NumArgs);
768 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000769 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +0000770
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000771 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +0000772 &CanonicalArgs[0], NumArgs,
773 isVariadic);
774
775 // Get the new insert position for the node we care about.
776 FunctionTypeProto *NewIP =
777 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
778 assert(NewIP == 0 && "Shouldn't be in the map!");
779 }
780
781 // FunctionTypeProto objects are not allocated with new because they have a
782 // variable size array (for parameter types) at the end of them.
783 FunctionTypeProto *FTP =
784 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
785 NumArgs*sizeof(QualType));
786 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
787 Canonical);
788 Types.push_back(FTP);
789 FunctionTypeProtos.InsertNode(FTP, InsertPos);
790 return QualType(FTP, 0);
791}
792
793/// getTypedefType - Return the unique reference to the type for the
794/// specified typename decl.
795QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
796 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
797
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000798 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000799 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000800 Types.push_back(Decl->TypeForDecl);
801 return QualType(Decl->TypeForDecl, 0);
802}
803
Ted Kremenek42730c52008-01-07 19:49:32 +0000804/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000805/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000806QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000807 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
808
Ted Kremenek42730c52008-01-07 19:49:32 +0000809 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000810 Types.push_back(Decl->TypeForDecl);
811 return QualType(Decl->TypeForDecl, 0);
812}
813
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +0000814/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
815/// the given interface decl and 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()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000851 Canonical = getObjCQualifiedIdType(getCanonicalType(idType),
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) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000871 QualType Canonical = getCanonicalType(tofExpr->getType());
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) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000883 QualType Canonical = getCanonicalType(tofType);
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
Chris Lattner3dae6f42008-04-06 22:41:35 +0000934/// getCanonicalType - Return the canonical (structural) type corresponding to
935/// the specified potentially non-canonical type. The non-canonical version
936/// of a type may have many "decorated" versions of types. Decorators can
937/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
938/// to be free of any of these, allowing two canonical types to be compared
939/// for exact equality with a simple pointer comparison.
940QualType ASTContext::getCanonicalType(QualType T) {
941 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
942 return QualType(CanType.getTypePtr(),
943 T.getCVRQualifiers() | CanType.getCVRQualifiers());
944}
945
946
Chris Lattner19eb97e2008-04-02 05:18:44 +0000947/// getArrayDecayedType - Return the properly qualified result of decaying the
948/// specified array type to a pointer. This operation is non-trivial when
949/// handling typedefs etc. The canonical type of "T" must be an array type,
950/// this returns a pointer to a properly qualified element of the array.
951///
952/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
953QualType ASTContext::getArrayDecayedType(QualType Ty) {
954 // Handle the common case where typedefs are not involved directly.
955 QualType EltTy;
956 unsigned ArrayQuals = 0;
957 unsigned PointerQuals = 0;
958 if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
959 // Since T "isa" an array type, it could not have had an address space
960 // qualifier, just CVR qualifiers. The properly qualified element pointer
961 // gets the union of the CVR qualifiers from the element and the array, and
962 // keeps any address space qualifier on the element type if present.
963 EltTy = AT->getElementType();
964 ArrayQuals = Ty.getCVRQualifiers();
965 PointerQuals = AT->getIndexTypeQualifier();
966 } else {
967 // Otherwise, we have an ASQualType or a typedef, etc. Make sure we don't
968 // lose qualifiers when dealing with typedefs. Example:
969 // typedef int arr[10];
970 // void test2() {
971 // const arr b;
972 // b[4] = 1;
973 // }
974 //
975 // The decayed type of b is "const int*" even though the element type of the
976 // array is "int".
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000977 QualType CanTy = getCanonicalType(Ty);
Chris Lattner19eb97e2008-04-02 05:18:44 +0000978 const ArrayType *PrettyArrayType = Ty->getAsArrayType();
979 assert(PrettyArrayType && "Not an array type!");
980
981 // Get the element type with 'getAsArrayType' so that we don't lose any
982 // typedefs in the element type of the array.
983 EltTy = PrettyArrayType->getElementType();
984
985 // If the array was address-space qualifier, make sure to ASQual the element
986 // type. We can just grab the address space from the canonical type.
987 if (unsigned AS = CanTy.getAddressSpace())
988 EltTy = getASQualType(EltTy, AS);
989
990 // To properly handle [multiple levels of] typedefs, typeof's etc, we take
991 // the CVR qualifiers directly from the canonical type, which is guaranteed
992 // to have the full set unioned together.
993 ArrayQuals = CanTy.getCVRQualifiers();
994 PointerQuals = PrettyArrayType->getIndexTypeQualifier();
995 }
996
Chris Lattnerda79b3f2008-04-02 06:06:35 +0000997 // Apply any CVR qualifiers from the array type to the element type. This
998 // implements C99 6.7.3p8: "If the specification of an array type includes
999 // any type qualifiers, the element type is so qualified, not the array type."
Chris Lattner19eb97e2008-04-02 05:18:44 +00001000 EltTy = EltTy.getQualifiedType(ArrayQuals | EltTy.getCVRQualifiers());
1001
1002 QualType PtrTy = getPointerType(EltTy);
1003
1004 // int x[restrict 4] -> int *restrict
1005 PtrTy = PtrTy.getQualifiedType(PointerQuals);
1006
1007 return PtrTy;
1008}
1009
Chris Lattner4b009652007-07-25 00:24:17 +00001010/// getFloatingRank - Return a relative rank for floating point types.
1011/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001012static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001013 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001014 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001015
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001016 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001017 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001018 case BuiltinType::Float: return FloatRank;
1019 case BuiltinType::Double: return DoubleRank;
1020 case BuiltinType::LongDouble: return LongDoubleRank;
1021 }
1022}
1023
Steve Narofffa0c4532007-08-27 01:41:48 +00001024/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1025/// point or a complex type (based on typeDomain/typeSize).
1026/// 'typeDomain' is a real floating point or complex type.
1027/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001028QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1029 QualType Domain) const {
1030 FloatingRank EltRank = getFloatingRank(Size);
1031 if (Domain->isComplexType()) {
1032 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001033 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001034 case FloatRank: return FloatComplexTy;
1035 case DoubleRank: return DoubleComplexTy;
1036 case LongDoubleRank: return LongDoubleComplexTy;
1037 }
Chris Lattner4b009652007-07-25 00:24:17 +00001038 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001039
1040 assert(Domain->isRealFloatingType() && "Unknown domain!");
1041 switch (EltRank) {
1042 default: assert(0 && "getFloatingRank(): illegal value for rank");
1043 case FloatRank: return FloatTy;
1044 case DoubleRank: return DoubleTy;
1045 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001046 }
Chris Lattner4b009652007-07-25 00:24:17 +00001047}
1048
Chris Lattner51285d82008-04-06 23:55:33 +00001049/// getFloatingTypeOrder - Compare the rank of the two specified floating
1050/// point types, ignoring the domain of the type (i.e. 'double' ==
1051/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1052/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001053int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1054 FloatingRank LHSR = getFloatingRank(LHS);
1055 FloatingRank RHSR = getFloatingRank(RHS);
1056
1057 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001058 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001059 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001060 return 1;
1061 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001062}
1063
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001064/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1065/// routine will assert if passed a built-in type that isn't an integer or enum,
1066/// or if it is not canonicalized.
1067static unsigned getIntegerRank(Type *T) {
1068 assert(T->isCanonical() && "T should be canonicalized");
1069 if (isa<EnumType>(T))
1070 return 4;
1071
1072 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001073 default: assert(0 && "getIntegerRank(): not a built-in integer");
1074 case BuiltinType::Bool:
1075 return 1;
1076 case BuiltinType::Char_S:
1077 case BuiltinType::Char_U:
1078 case BuiltinType::SChar:
1079 case BuiltinType::UChar:
1080 return 2;
1081 case BuiltinType::Short:
1082 case BuiltinType::UShort:
1083 return 3;
1084 case BuiltinType::Int:
1085 case BuiltinType::UInt:
1086 return 4;
1087 case BuiltinType::Long:
1088 case BuiltinType::ULong:
1089 return 5;
1090 case BuiltinType::LongLong:
1091 case BuiltinType::ULongLong:
1092 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001093 }
1094}
1095
Chris Lattner51285d82008-04-06 23:55:33 +00001096/// getIntegerTypeOrder - Returns the highest ranked integer type:
1097/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1098/// LHS < RHS, return -1.
1099int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001100 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1101 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001102 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001103
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001104 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1105 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001106
Chris Lattner51285d82008-04-06 23:55:33 +00001107 unsigned LHSRank = getIntegerRank(LHSC);
1108 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001109
Chris Lattner51285d82008-04-06 23:55:33 +00001110 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1111 if (LHSRank == RHSRank) return 0;
1112 return LHSRank > RHSRank ? 1 : -1;
1113 }
Chris Lattner4b009652007-07-25 00:24:17 +00001114
Chris Lattner51285d82008-04-06 23:55:33 +00001115 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1116 if (LHSUnsigned) {
1117 // If the unsigned [LHS] type is larger, return it.
1118 if (LHSRank >= RHSRank)
1119 return 1;
1120
1121 // If the signed type can represent all values of the unsigned type, it
1122 // wins. Because we are dealing with 2's complement and types that are
1123 // powers of two larger than each other, this is always safe.
1124 return -1;
1125 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001126
Chris Lattner51285d82008-04-06 23:55:33 +00001127 // If the unsigned [RHS] type is larger, return it.
1128 if (RHSRank >= LHSRank)
1129 return -1;
1130
1131 // If the signed type can represent all values of the unsigned type, it
1132 // wins. Because we are dealing with 2's complement and types that are
1133 // powers of two larger than each other, this is always safe.
1134 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001135}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001136
1137// getCFConstantStringType - Return the type used for constant CFStrings.
1138QualType ASTContext::getCFConstantStringType() {
1139 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001140 CFConstantStringTypeDecl =
Chris Lattnereee57c02008-04-04 06:12:32 +00001141 RecordDecl::Create(*this, Decl::Struct, NULL, SourceLocation(),
Chris Lattner58114f02008-03-15 21:32:50 +00001142 &Idents.get("NSConstantString"), 0);
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001143 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001144
1145 // const int *isa;
1146 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001147 // int flags;
1148 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001149 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001150 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001151 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001152 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001153 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001154 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001155
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001156 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerf3874bc2008-04-06 04:47:34 +00001157 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner81db64a2008-03-16 00:16:02 +00001158 FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001159
1160 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1161 }
1162
1163 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001164}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001165
Anders Carlssone3f02572007-10-29 06:33:42 +00001166// This returns true if a type has been typedefed to BOOL:
1167// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001168static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001169 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001170 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001171
1172 return false;
1173}
1174
Ted Kremenek42730c52008-01-07 19:49:32 +00001175/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001176/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001177int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001178 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001179
1180 // Make all integer and enum types at least as large as an int
1181 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001182 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001183 // Treat arrays as pointers, since that's how they're passed in.
1184 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001185 sz = getTypeSize(VoidPtrTy);
1186 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001187}
1188
Ted Kremenek42730c52008-01-07 19:49:32 +00001189/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001190/// declaration.
Ted Kremenek42730c52008-01-07 19:49:32 +00001191void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001192 std::string& S)
1193{
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001194 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001195 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001196 // Encode result type.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001197 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001198 // Compute size of all parameters.
1199 // Start with computing size of a pointer in number of bytes.
1200 // FIXME: There might(should) be a better way of doing this computation!
1201 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001202 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001203 // The first two arguments (self and _cmd) are pointers; account for
1204 // their size.
1205 int ParmOffset = 2 * PtrSize;
1206 int NumOfParams = Decl->getNumParams();
1207 for (int i = 0; i < NumOfParams; i++) {
1208 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001209 int sz = getObjCEncodingTypeSize (PType);
1210 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001211 ParmOffset += sz;
1212 }
1213 S += llvm::utostr(ParmOffset);
1214 S += "@0:";
1215 S += llvm::utostr(PtrSize);
1216
1217 // Argument types.
1218 ParmOffset = 2 * PtrSize;
1219 for (int i = 0; i < NumOfParams; i++) {
1220 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001221 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001222 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001223 getObjCEncodingForTypeQualifier(
1224 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian248db262008-01-22 22:44:46 +00001225 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001226 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001227 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001228 }
1229}
1230
Fariborz Jahanian248db262008-01-22 22:44:46 +00001231void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1232 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson36f07d82007-10-29 05:01:08 +00001233{
Anders Carlssone3f02572007-10-29 06:33:42 +00001234 // FIXME: This currently doesn't encode:
1235 // @ An object (whether statically typed or typed id)
1236 // # A class object (Class)
1237 // : A method selector (SEL)
1238 // {name=type...} A structure
1239 // (name=type...) A union
1240 // bnum A bit field of num bits
1241
1242 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001243 char encoding;
1244 switch (BT->getKind()) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001245 default: assert(0 && "Unhandled builtin type kind");
1246 case BuiltinType::Void: encoding = 'v'; break;
1247 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001248 case BuiltinType::Char_U:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001249 case BuiltinType::UChar: encoding = 'C'; break;
1250 case BuiltinType::UShort: encoding = 'S'; break;
1251 case BuiltinType::UInt: encoding = 'I'; break;
1252 case BuiltinType::ULong: encoding = 'L'; break;
1253 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001254 case BuiltinType::Char_S:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001255 case BuiltinType::SChar: encoding = 'c'; break;
1256 case BuiltinType::Short: encoding = 's'; break;
1257 case BuiltinType::Int: encoding = 'i'; break;
1258 case BuiltinType::Long: encoding = 'l'; break;
1259 case BuiltinType::LongLong: encoding = 'q'; break;
1260 case BuiltinType::Float: encoding = 'f'; break;
1261 case BuiltinType::Double: encoding = 'd'; break;
1262 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001263 }
1264
1265 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001266 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001267 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001268 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001269 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001270
1271 }
1272 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001273 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001274 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001275 S += '@';
1276 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001277 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001278 S += '#';
1279 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001280 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001281 S += ':';
1282 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001283 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001284
1285 if (PointeeTy->isCharType()) {
1286 // char pointer types should be encoded as '*' unless it is a
1287 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001288 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001289 S += '*';
1290 return;
1291 }
1292 }
1293
1294 S += '^';
Fariborz Jahanian248db262008-01-22 22:44:46 +00001295 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone3f02572007-10-29 06:33:42 +00001296 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001297 S += '[';
1298
1299 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1300 S += llvm::utostr(CAT->getSize().getZExtValue());
1301 else
1302 assert(0 && "Unhandled array type!");
1303
Fariborz Jahanian248db262008-01-22 22:44:46 +00001304 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001305 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001306 } else if (T->getAsFunctionType()) {
1307 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001308 } else if (const RecordType *RTy = T->getAsRecordType()) {
1309 RecordDecl *RDecl= RTy->getDecl();
1310 S += '{';
1311 S += RDecl->getName();
Fariborz Jahanian248db262008-01-22 22:44:46 +00001312 bool found = false;
1313 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1314 if (ERType[i] == RTy) {
1315 found = true;
1316 break;
1317 }
1318 if (!found) {
1319 ERType.push_back(RTy);
1320 S += '=';
1321 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1322 FieldDecl *field = RDecl->getMember(i);
1323 getObjCEncodingForType(field->getType(), S, ERType);
1324 }
1325 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1326 ERType.pop_back();
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001327 }
1328 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001329 } else if (T->isEnumeralType()) {
1330 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001331 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001332 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001333}
1334
Ted Kremenek42730c52008-01-07 19:49:32 +00001335void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001336 std::string& S) const {
1337 if (QT & Decl::OBJC_TQ_In)
1338 S += 'n';
1339 if (QT & Decl::OBJC_TQ_Inout)
1340 S += 'N';
1341 if (QT & Decl::OBJC_TQ_Out)
1342 S += 'o';
1343 if (QT & Decl::OBJC_TQ_Bycopy)
1344 S += 'O';
1345 if (QT & Decl::OBJC_TQ_Byref)
1346 S += 'R';
1347 if (QT & Decl::OBJC_TQ_Oneway)
1348 S += 'V';
1349}
1350
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001351void ASTContext::setBuiltinVaListType(QualType T)
1352{
1353 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1354
1355 BuiltinVaListType = T;
1356}
1357
Ted Kremenek42730c52008-01-07 19:49:32 +00001358void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001359{
Ted Kremenek42730c52008-01-07 19:49:32 +00001360 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff9d12c902007-10-15 14:41:52 +00001361
Ted Kremenek42730c52008-01-07 19:49:32 +00001362 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001363
1364 // typedef struct objc_object *id;
1365 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1366 assert(ptr && "'id' incorrectly typed");
1367 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1368 assert(rec && "'id' incorrectly typed");
1369 IdStructType = rec;
1370}
1371
Ted Kremenek42730c52008-01-07 19:49:32 +00001372void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001373{
Ted Kremenek42730c52008-01-07 19:49:32 +00001374 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001375
Ted Kremenek42730c52008-01-07 19:49:32 +00001376 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001377
1378 // typedef struct objc_selector *SEL;
1379 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1380 assert(ptr && "'SEL' incorrectly typed");
1381 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1382 assert(rec && "'SEL' incorrectly typed");
1383 SelStructType = rec;
1384}
1385
Ted Kremenek42730c52008-01-07 19:49:32 +00001386void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001387{
Ted Kremenek42730c52008-01-07 19:49:32 +00001388 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1389 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001390}
1391
Ted Kremenek42730c52008-01-07 19:49:32 +00001392void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001393{
Ted Kremenek42730c52008-01-07 19:49:32 +00001394 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001395
Ted Kremenek42730c52008-01-07 19:49:32 +00001396 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001397
1398 // typedef struct objc_class *Class;
1399 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1400 assert(ptr && "'Class' incorrectly typed");
1401 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1402 assert(rec && "'Class' incorrectly typed");
1403 ClassStructType = rec;
1404}
1405
Ted Kremenek42730c52008-01-07 19:49:32 +00001406void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1407 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001408 "'NSConstantString' type already set!");
1409
Ted Kremenek42730c52008-01-07 19:49:32 +00001410 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001411}
1412
Steve Naroff85f0dc52007-10-15 20:41:53 +00001413bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1414 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1415 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1416
1417 return lBuiltin->getKind() == rBuiltin->getKind();
1418}
1419
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001420/// objcTypesAreCompatible - This routine is called when two types
1421/// are of different class; one is interface type or is
1422/// a qualified interface type and the other type is of a different class.
1423/// Example, II or II<P>.
Chris Lattner972e3942008-04-07 01:30:37 +00001424bool ASTContext::objcTypesAreCompatible(QualType LHS, QualType RHS) {
1425 // ID is compatible with all interface types.
1426 if (LHS->isObjCInterfaceType() && isObjCIdType(RHS))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001427 return true;
Chris Lattner972e3942008-04-07 01:30:37 +00001428 else if (isObjCIdType(LHS) && RHS->isObjCInterfaceType())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001429 return true;
Chris Lattnerb06cf30b2008-04-07 00:27:04 +00001430
Chris Lattner972e3942008-04-07 01:30:37 +00001431 // II is compatible with II<P> if the base is the same. Otherwise, no two
1432 // qualified interface types are the same.
1433 if (const ObjCInterfaceType *LHSIT = LHS->getAsObjCInterfaceType()) {
1434 if (const ObjCInterfaceType *RHSIT = RHS->getAsObjCInterfaceType()) {
1435 // If the base decls match and one is a qualified interface and one isn't,
1436 // then they are compatible.
1437 return LHSIT->getDecl() == RHSIT->getDecl() &&
1438 isa<ObjCQualifiedInterfaceType>(LHSIT) !=
1439 isa<ObjCQualifiedInterfaceType>(RHSIT);
1440 }
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001441 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00001442 return false;
1443}
1444
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001445/// areCompatObjCQualInterfaces - Return true if the two qualified interface
1446/// types are compatible for assignment from RHS to LHS.
1447///
Chris Lattnere7bf8df2008-04-07 04:17:40 +00001448static bool
1449areCompatObjCQualInterfaces(const ObjCQualifiedInterfaceType *LHS,
1450 const ObjCQualifiedInterfaceType *RHS) {
1451 // Verify that the base decls are compatible: the RHS must be a subclass of
Chris Lattner40d63172008-04-07 04:13:03 +00001452 // the LHS.
Chris Lattnere7bf8df2008-04-07 04:17:40 +00001453 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001454 return false;
Chris Lattner855fed42008-04-07 04:07:56 +00001455
Chris Lattnere7bf8df2008-04-07 04:17:40 +00001456 // All protocols in LHS must have a presence in RHS.
1457 for (unsigned i = 0; i != LHS->getNumProtocols(); ++i) {
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001458 bool match = false;
Chris Lattnere7bf8df2008-04-07 04:17:40 +00001459 ObjCProtocolDecl *lhsProto = LHS->getProtocols(i);
1460 for (unsigned j = 0; j != RHS->getNumProtocols(); ++j) {
1461 ObjCProtocolDecl *rhsProto = RHS->getProtocols(j);
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001462 if (lhsProto == rhsProto) {
1463 match = true;
1464 break;
1465 }
1466 }
1467 if (!match)
1468 return false;
1469 }
1470 return true;
1471}
1472
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001473/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
1474/// inheritance hierarchy of 'rProto'.
Ted Kremenek42730c52008-01-07 19:49:32 +00001475static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
1476 ObjCProtocolDecl *rProto) {
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001477 if (lProto == rProto)
1478 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001479 ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001480 for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
1481 if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
1482 return true;
1483 return false;
1484}
1485
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001486/// ClassImplementsProtocol - Checks that 'lProto' protocol
1487/// has been implemented in IDecl class, its super class or categories (if
1488/// lookupCategory is true).
Ted Kremenek42730c52008-01-07 19:49:32 +00001489static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1490 ObjCInterfaceDecl *IDecl,
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001491 bool lookupCategory) {
1492
1493 // 1st, look up the class.
Ted Kremenek42730c52008-01-07 19:49:32 +00001494 ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols();
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001495 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
1496 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1497 return true;
1498 }
1499
1500 // 2nd, look up the category.
1501 if (lookupCategory)
Ted Kremenek42730c52008-01-07 19:49:32 +00001502 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001503 CDecl = CDecl->getNextClassCategory()) {
1504 protoList = CDecl->getReferencedProtocols();
1505 for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
1506 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1507 return true;
1508 }
1509 }
1510
1511 // 3rd, look up the super class(s)
1512 if (IDecl->getSuperClass())
1513 return
1514 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory);
1515
1516 return false;
1517}
1518
Ted Kremenek42730c52008-01-07 19:49:32 +00001519/// ObjCQualifiedIdTypesAreCompatible - Compares two types, at least
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001520/// one of which is a protocol qualified 'id' type. When 'compare'
1521/// is true it is for comparison; when false, for assignment/initialization.
Ted Kremenek42730c52008-01-07 19:49:32 +00001522bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs,
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001523 QualType rhs,
1524 bool compare) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001525 // match id<P..> with an 'id' type in all cases.
1526 if (const PointerType *PT = lhs->getAsPointerType()) {
1527 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001528 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001529 return true;
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001530 } else if (const PointerType *PT = rhs->getAsPointerType()) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001531 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001532 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001533 return true;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001534 }
1535
Ted Kremenek42730c52008-01-07 19:49:32 +00001536 ObjCQualifiedInterfaceType *lhsQI = 0;
1537 ObjCQualifiedInterfaceType *rhsQI = 0;
1538 ObjCInterfaceDecl *lhsID = 0;
1539 ObjCInterfaceDecl *rhsID = 0;
1540 ObjCQualifiedIdType *lhsQID = dyn_cast<ObjCQualifiedIdType>(lhs);
1541 ObjCQualifiedIdType *rhsQID = dyn_cast<ObjCQualifiedIdType>(rhs);
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001542
1543 if (lhsQID) {
1544 if (!rhsQID && rhs->getTypeClass() == Type::Pointer) {
1545 QualType rtype =
1546 cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1547 rhsQI =
Ted Kremenek42730c52008-01-07 19:49:32 +00001548 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001549 rtype.getCanonicalType().getTypePtr());
Fariborz Jahanian87829072007-12-20 19:24:10 +00001550 if (!rhsQI) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001551 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanian87829072007-12-20 19:24:10 +00001552 rtype.getCanonicalType().getTypePtr());
1553 if (IT)
1554 rhsID = IT->getDecl();
1555 }
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001556 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001557 if (!rhsQI && !rhsQID && !rhsID)
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001558 return false;
1559
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001560 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001561 if (rhsQI) {
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001562 RHSProtoI = rhsQI->qual_begin();
1563 RHSProtoE = rhsQI->qual_end();
1564 } else if (rhsQID) {
1565 RHSProtoI = rhsQID->qual_begin();
1566 RHSProtoE = rhsQID->qual_end();
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001567 }
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;
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001579 } else {
1580 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
1581 ObjCProtocolDecl *rhsProto = *RHSProtoI;
1582 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1583 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
1584 match = true;
1585 break;
1586 }
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001587 }
1588 }
1589 if (!match)
1590 return false;
1591 }
1592 }
1593 else if (rhsQID) {
1594 if (!lhsQID && lhs->getTypeClass() == Type::Pointer) {
1595 QualType ltype =
1596 cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1597 lhsQI =
Ted Kremenek42730c52008-01-07 19:49:32 +00001598 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001599 ltype.getCanonicalType().getTypePtr());
Fariborz Jahanian87829072007-12-20 19:24:10 +00001600 if (!lhsQI) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001601 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanian87829072007-12-20 19:24:10 +00001602 ltype.getCanonicalType().getTypePtr());
1603 if (IT)
1604 lhsID = IT->getDecl();
1605 }
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001606 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001607 if (!lhsQI && !lhsQID && !lhsID)
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001608 return false;
Fariborz Jahanian87829072007-12-20 19:24:10 +00001609
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001610 ObjCQualifiedIdType::qual_iterator LHSProtoI, LHSProtoE;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001611 if (lhsQI) {
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001612 LHSProtoI = lhsQI->qual_begin();
1613 LHSProtoE = lhsQI->qual_end();
1614 } else if (lhsQID) {
1615 LHSProtoI = lhsQID->qual_begin();
1616 LHSProtoE = lhsQID->qual_end();
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001617 }
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001618
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001619 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 }
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001630 } else {
1631 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
1632 match = false;
1633 ObjCProtocolDecl *lhsProto = *LHSProtoI;
1634 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
1635 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
1636 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1637 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
1638 match = true;
1639 break;
1640 }
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001641 }
1642 }
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001643 }
1644 if (!match)
1645 return false;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001646 }
1647 return true;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001648}
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001649
Chris Lattner5003e8b2007-11-01 05:03:41 +00001650bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1651 const VectorType *lVector = lhs->getAsVectorType();
1652 const VectorType *rVector = rhs->getAsVectorType();
1653
1654 if ((lVector->getElementType().getCanonicalType() ==
1655 rVector->getElementType().getCanonicalType()) &&
1656 (lVector->getNumElements() == rVector->getNumElements()))
1657 return true;
1658 return false;
1659}
1660
Steve Naroff85f0dc52007-10-15 20:41:53 +00001661// C99 6.2.7p1: If both are complete types, then the following additional
1662// requirements apply...FIXME (handle compatibility across source files).
1663bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
Steve Naroff4a5e2072007-11-07 06:03:51 +00001664 // "Class" and "id" are compatible built-in structure types.
Ted Kremenek42730c52008-01-07 19:49:32 +00001665 if (isObjCIdType(lhs) && isObjCClassType(rhs) ||
1666 isObjCClassType(lhs) && isObjCIdType(rhs))
Steve Naroff4a5e2072007-11-07 06:03:51 +00001667 return true;
Eli Friedmane7fb03a2008-02-15 06:03:44 +00001668
1669 // Within a translation unit a tag type is
1670 // only compatible with itself.
1671 return lhs.getCanonicalType() == rhs.getCanonicalType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001672}
1673
1674bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1675 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1676 // identically qualified and both shall be pointers to compatible types.
Chris Lattner35fef522008-02-20 20:55:12 +00001677 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1678 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001679 return false;
1680
1681 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1682 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1683
1684 return typesAreCompatible(ltype, rtype);
1685}
1686
Bill Wendling6a9d8542007-12-03 07:33:35 +00001687// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroff85f0dc52007-10-15 20:41:53 +00001688// reference to T, the operation assigns to the object of type T denoted by the
1689// reference.
1690bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1691 QualType ltype = lhs;
1692
1693 if (lhs->isReferenceType())
Chris Lattnercfac88d2008-04-02 17:35:06 +00001694 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getPointeeType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001695
1696 QualType rtype = rhs;
1697
1698 if (rhs->isReferenceType())
Chris Lattnercfac88d2008-04-02 17:35:06 +00001699 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getPointeeType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001700
1701 return typesAreCompatible(ltype, rtype);
1702}
1703
1704bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1705 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1706 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1707 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1708 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1709
1710 // first check the return types (common between C99 and K&R).
1711 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1712 return false;
1713
1714 if (lproto && rproto) { // two C99 style function prototypes
1715 unsigned lproto_nargs = lproto->getNumArgs();
1716 unsigned rproto_nargs = rproto->getNumArgs();
1717
1718 if (lproto_nargs != rproto_nargs)
1719 return false;
1720
1721 // both prototypes have the same number of arguments.
1722 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1723 (rproto->isVariadic() && !lproto->isVariadic()))
1724 return false;
1725
1726 // The use of ellipsis agree...now check the argument types.
1727 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001728 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1729 // is taken as having the unqualified version of it's declared type.
Steve Naroffdec17fe2008-01-29 00:15:50 +00001730 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001731 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001732 return false;
1733 return true;
1734 }
1735 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1736 return true;
1737
1738 // we have a mixture of K&R style with C99 prototypes
1739 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1740
1741 if (proto->isVariadic())
1742 return false;
1743
1744 // FIXME: Each parameter type T in the prototype must be compatible with the
1745 // type resulting from applying the usual argument conversions to T.
1746 return true;
1747}
1748
1749bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
Eli Friedman1e7537832008-02-06 04:53:22 +00001750 // Compatible arrays must have compatible element types
1751 QualType ltype = lhs->getAsArrayType()->getElementType();
1752 QualType rtype = rhs->getAsArrayType()->getElementType();
1753
Steve Naroff85f0dc52007-10-15 20:41:53 +00001754 if (!typesAreCompatible(ltype, rtype))
1755 return false;
Eli Friedman1e7537832008-02-06 04:53:22 +00001756
1757 // Compatible arrays must be the same size
1758 if (const ConstantArrayType* LCAT = lhs->getAsConstantArrayType())
1759 if (const ConstantArrayType* RCAT = rhs->getAsConstantArrayType())
1760 return RCAT->getSize() == LCAT->getSize();
1761
Steve Naroff85f0dc52007-10-15 20:41:53 +00001762 return true;
1763}
1764
1765/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1766/// both shall have the identically qualified version of a compatible type.
1767/// C99 6.2.7p1: Two types have compatible types if their types are the
1768/// same. See 6.7.[2,3,5] for additional rules.
Chris Lattner855fed42008-04-07 04:07:56 +00001769bool ASTContext::typesAreCompatible(QualType LHS_NC, QualType RHS_NC) {
1770 QualType LHS = LHS_NC.getCanonicalType();
1771 QualType RHS = RHS_NC.getCanonicalType();
Chris Lattner4d5670b2008-04-03 05:07:04 +00001772
Steve Naroff85f0dc52007-10-15 20:41:53 +00001773 // If two types are identical, they are are compatible
Chris Lattner855fed42008-04-07 04:07:56 +00001774 if (LHS == RHS)
Steve Naroff85f0dc52007-10-15 20:41:53 +00001775 return true;
Chris Lattner4d5670b2008-04-03 05:07:04 +00001776
Chris Lattner855fed42008-04-07 04:07:56 +00001777 if (LHS.getCVRQualifiers() != RHS.getCVRQualifiers() ||
1778 LHS.getAddressSpace() != RHS.getAddressSpace())
Chris Lattner4d5670b2008-04-03 05:07:04 +00001779 return false;
Bill Wendling6a9d8542007-12-03 07:33:35 +00001780
1781 // C++ [expr]: If an expression initially has the type "reference to T", the
1782 // type is adjusted to "T" prior to any further analysis, the expression
1783 // designates the object or function denoted by the reference, and the
1784 // expression is an lvalue.
Chris Lattner855fed42008-04-07 04:07:56 +00001785 if (ReferenceType *RT = dyn_cast<ReferenceType>(LHS))
1786 LHS = RT->getPointeeType();
1787 if (ReferenceType *RT = dyn_cast<ReferenceType>(RHS))
1788 RHS = RT->getPointeeType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001789
Chris Lattner855fed42008-04-07 04:07:56 +00001790 Type::TypeClass LHSClass = LHS->getTypeClass();
1791 Type::TypeClass RHSClass = RHS->getTypeClass();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001792
1793 // We want to consider the two function types to be the same for these
1794 // comparisons, just force one to the other.
1795 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1796 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00001797
1798 // Same as above for arrays
1799 if (LHSClass == Type::VariableArray) LHSClass = Type::ConstantArray;
1800 if (RHSClass == Type::VariableArray) RHSClass = Type::ConstantArray;
Eli Friedman8ff07782008-02-15 18:16:39 +00001801 if (LHSClass == Type::IncompleteArray) LHSClass = Type::ConstantArray;
1802 if (RHSClass == Type::IncompleteArray) RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001803
Steve Naroffc88babe2008-01-09 22:43:08 +00001804 // If the canonical type classes don't match...
Chris Lattnerc38d4522008-01-14 05:45:46 +00001805 if (LHSClass != RHSClass) {
Steve Naroff85f0dc52007-10-15 20:41:53 +00001806 // For Objective-C, it is possible for two types to be compatible
1807 // when their classes don't match (when dealing with "id"). If either type
1808 // is an interface, we defer to objcTypesAreCompatible().
Chris Lattner855fed42008-04-07 04:07:56 +00001809 if (LHS->isObjCInterfaceType() || RHS->isObjCInterfaceType())
1810 return objcTypesAreCompatible(LHS, RHS);
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001811
Chris Lattnerc38d4522008-01-14 05:45:46 +00001812 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1813 // a signed integer type, or an unsigned integer type.
Chris Lattner855fed42008-04-07 04:07:56 +00001814 if (LHS->isEnumeralType() && RHS->isIntegralType()) {
1815 EnumDecl* EDecl = cast<EnumType>(LHS)->getDecl();
1816 return EDecl->getIntegerType() == RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001817 }
Chris Lattner855fed42008-04-07 04:07:56 +00001818 if (RHS->isEnumeralType() && LHS->isIntegralType()) {
1819 EnumDecl* EDecl = cast<EnumType>(RHS)->getDecl();
1820 return EDecl->getIntegerType() == LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001821 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001822
Steve Naroff85f0dc52007-10-15 20:41:53 +00001823 return false;
1824 }
Steve Naroffc88babe2008-01-09 22:43:08 +00001825 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001826 switch (LHSClass) {
1827 case Type::FunctionProto: assert(0 && "Canonicalized away above");
1828 case Type::Pointer:
Chris Lattner855fed42008-04-07 04:07:56 +00001829 return pointerTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001830 case Type::ConstantArray:
1831 case Type::VariableArray:
Eli Friedman8ff07782008-02-15 18:16:39 +00001832 case Type::IncompleteArray:
Chris Lattner855fed42008-04-07 04:07:56 +00001833 return arrayTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001834 case Type::FunctionNoProto:
Chris Lattner855fed42008-04-07 04:07:56 +00001835 return functionTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001836 case Type::Tagged: // handle structures, unions
Chris Lattner855fed42008-04-07 04:07:56 +00001837 return tagTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001838 case Type::Builtin:
Chris Lattner855fed42008-04-07 04:07:56 +00001839 return builtinTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001840 case Type::ObjCInterface:
Chris Lattner40d63172008-04-07 04:13:03 +00001841 // The LHS must be a superclass of the RHS.
1842 return cast<ObjCInterfaceType>(LHS)->getDecl()->isSuperClassOf(
1843 cast<ObjCInterfaceType>(RHS)->getDecl());
Chris Lattnerc38d4522008-01-14 05:45:46 +00001844 case Type::Vector:
1845 case Type::OCUVector:
Chris Lattner855fed42008-04-07 04:07:56 +00001846 return vectorTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001847 case Type::ObjCQualifiedInterface:
Chris Lattnere7bf8df2008-04-07 04:17:40 +00001848 return areCompatObjCQualInterfaces(cast<ObjCQualifiedInterfaceType>(LHS),
1849 cast<ObjCQualifiedInterfaceType>(RHS));
Chris Lattnerc38d4522008-01-14 05:45:46 +00001850 default:
1851 assert(0 && "unexpected type");
Steve Naroff85f0dc52007-10-15 20:41:53 +00001852 }
1853 return true; // should never get here...
1854}
Ted Kremenek738e6c02007-10-31 17:10:13 +00001855
Ted Kremenek738e6c02007-10-31 17:10:13 +00001856/// Emit - Serialize an ASTContext object to Bitcode.
1857void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00001858 S.EmitRef(SourceMgr);
1859 S.EmitRef(Target);
1860 S.EmitRef(Idents);
1861 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001862
Ted Kremenek68228a92007-10-31 22:44:07 +00001863 // Emit the size of the type vector so that we can reserve that size
1864 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001865 S.EmitInt(Types.size());
1866
Ted Kremenek034a78c2007-11-13 22:02:55 +00001867 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1868 I!=E;++I)
1869 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001870
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001871 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001872}
1873
Ted Kremenekacba3612007-11-13 00:25:37 +00001874ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek68228a92007-10-31 22:44:07 +00001875 SourceManager &SM = D.ReadRef<SourceManager>();
1876 TargetInfo &t = D.ReadRef<TargetInfo>();
1877 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1878 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00001879
Ted Kremenek68228a92007-10-31 22:44:07 +00001880 unsigned size_reserve = D.ReadInt();
1881
1882 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1883
Ted Kremenek034a78c2007-11-13 22:02:55 +00001884 for (unsigned i = 0; i < size_reserve; ++i)
1885 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00001886
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001887 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00001888
1889 return A;
1890}