blob: b5b28bc390bee8c34afbc06c203d35fa36f0f6b1 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Basic/TargetInfo.h"
18#include "llvm/ADT/SmallVector.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000019#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000020#include "llvm/Bitcode/Serialize.h"
21#include "llvm/Bitcode/Deserialize.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000022
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24
25enum FloatingRank {
26 FloatRank, DoubleRank, LongDoubleRank
27};
28
29ASTContext::~ASTContext() {
30 // Deallocate all the types.
31 while (!Types.empty()) {
32 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) {
33 // Destroy the object, but don't call delete. These are malloc'd.
34 FT->~FunctionTypeProto();
35 free(FT);
36 } else {
37 delete Types.back();
38 }
39 Types.pop_back();
40 }
41}
42
43void ASTContext::PrintStats() const {
44 fprintf(stderr, "*** AST Context Stats:\n");
45 fprintf(stderr, " %d types total.\n", (int)Types.size());
46 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
47 unsigned NumVector = 0, NumComplex = 0;
48 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
49
50 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000051 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
52 unsigned NumObjCQualifiedIds = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000053
54 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
55 Type *T = Types[i];
56 if (isa<BuiltinType>(T))
57 ++NumBuiltin;
58 else if (isa<PointerType>(T))
59 ++NumPointer;
60 else if (isa<ReferenceType>(T))
61 ++NumReference;
62 else if (isa<ComplexType>(T))
63 ++NumComplex;
64 else if (isa<ArrayType>(T))
65 ++NumArray;
66 else if (isa<VectorType>(T))
67 ++NumVector;
68 else if (isa<FunctionTypeNoProto>(T))
69 ++NumFunctionNP;
70 else if (isa<FunctionTypeProto>(T))
71 ++NumFunctionP;
72 else if (isa<TypedefType>(T))
73 ++NumTypeName;
74 else if (TagType *TT = dyn_cast<TagType>(T)) {
75 ++NumTagged;
76 switch (TT->getDecl()->getKind()) {
77 default: assert(0 && "Unknown tagged type!");
78 case Decl::Struct: ++NumTagStruct; break;
79 case Decl::Union: ++NumTagUnion; break;
80 case Decl::Class: ++NumTagClass; break;
81 case Decl::Enum: ++NumTagEnum; break;
82 }
Ted Kremenek42730c52008-01-07 19:49:32 +000083 } else if (isa<ObjCInterfaceType>(T))
84 ++NumObjCInterfaces;
85 else if (isa<ObjCQualifiedInterfaceType>(T))
86 ++NumObjCQualifiedInterfaces;
87 else if (isa<ObjCQualifiedIdType>(T))
88 ++NumObjCQualifiedIds;
Steve Naroff948fd372007-09-17 14:16:13 +000089 else {
Chris Lattner8a35b462007-12-12 06:43:05 +000090 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +000091 assert(0 && "Unknown type!");
92 }
93 }
94
95 fprintf(stderr, " %d builtin types\n", NumBuiltin);
96 fprintf(stderr, " %d pointer types\n", NumPointer);
97 fprintf(stderr, " %d reference types\n", NumReference);
98 fprintf(stderr, " %d complex types\n", NumComplex);
99 fprintf(stderr, " %d array types\n", NumArray);
100 fprintf(stderr, " %d vector types\n", NumVector);
101 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
102 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
103 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
104 fprintf(stderr, " %d tagged types\n", NumTagged);
105 fprintf(stderr, " %d struct types\n", NumTagStruct);
106 fprintf(stderr, " %d union types\n", NumTagUnion);
107 fprintf(stderr, " %d class types\n", NumTagClass);
108 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000109 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000110 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000111 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000112 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000113 NumObjCQualifiedIds);
Chris Lattner4b009652007-07-25 00:24:17 +0000114 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
115 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
116 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
117 NumFunctionP*sizeof(FunctionTypeProto)+
118 NumFunctionNP*sizeof(FunctionTypeNoProto)+
119 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
120}
121
122
123void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
124 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
125}
126
Chris Lattner4b009652007-07-25 00:24:17 +0000127void ASTContext::InitBuiltinTypes() {
128 assert(VoidTy.isNull() && "Context reinitialized?");
129
130 // C99 6.2.5p19.
131 InitBuiltinType(VoidTy, BuiltinType::Void);
132
133 // C99 6.2.5p2.
134 InitBuiltinType(BoolTy, BuiltinType::Bool);
135 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000136 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000137 InitBuiltinType(CharTy, BuiltinType::Char_S);
138 else
139 InitBuiltinType(CharTy, BuiltinType::Char_U);
140 // C99 6.2.5p4.
141 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
142 InitBuiltinType(ShortTy, BuiltinType::Short);
143 InitBuiltinType(IntTy, BuiltinType::Int);
144 InitBuiltinType(LongTy, BuiltinType::Long);
145 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
146
147 // C99 6.2.5p6.
148 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
149 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
150 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
151 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
152 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
153
154 // C99 6.2.5p10.
155 InitBuiltinType(FloatTy, BuiltinType::Float);
156 InitBuiltinType(DoubleTy, BuiltinType::Double);
157 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
158
159 // C99 6.2.5p11.
160 FloatComplexTy = getComplexType(FloatTy);
161 DoubleComplexTy = getComplexType(DoubleTy);
162 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff9d12c902007-10-15 14:41:52 +0000163
164 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000165 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000166 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000167 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000168 ClassStructType = 0;
169
Ted Kremenek42730c52008-01-07 19:49:32 +0000170 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000171
172 // void * type
173 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000174}
175
176//===----------------------------------------------------------------------===//
177// Type Sizing and Analysis
178//===----------------------------------------------------------------------===//
179
180/// getTypeSize - Return the size of the specified type, in bits. This method
181/// does not work on incomplete types.
182std::pair<uint64_t, unsigned>
Chris Lattner8cd0e932008-03-05 18:54:05 +0000183ASTContext::getTypeInfo(QualType T) {
Chris Lattner4b009652007-07-25 00:24:17 +0000184 T = T.getCanonicalType();
185 uint64_t Size;
186 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());
Steve Naroff83c13012007-08-30 01:06:46 +0000199 Size = 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 Lattner4b009652007-07-25 00:24:17 +0000207 Size = EltInfo.first*cast<VectorType>(T)->getNumElements();
208 // FIXME: Vector alignment is not the alignment of its elements.
209 Align = EltInfo.second;
210 break;
211 }
212
213 case Type::Builtin: {
214 // FIXME: need to use TargetInfo to derive the target specific sizes. This
215 // implementation will suffice for play with vector support.
Chris Lattner858eece2007-09-22 18:29:59 +0000216 const llvm::fltSemantics *F;
Chris Lattner4b009652007-07-25 00:24:17 +0000217 switch (cast<BuiltinType>(T)->getKind()) {
218 default: assert(0 && "Unknown builtin type!");
219 case BuiltinType::Void:
220 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000221 case BuiltinType::Bool:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000222 Target.getBoolInfo(Size, Align);
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 Lattner8cd0e932008-03-05 18:54:05 +0000228 Target.getCharInfo(Size, Align);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000229 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000230 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000231 case BuiltinType::Short:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000232 Target.getShortInfo(Size, Align);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000233 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000234 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000235 case BuiltinType::Int:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000236 Target.getIntInfo(Size, Align);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000237 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000238 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000239 case BuiltinType::Long:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000240 Target.getLongInfo(Size, Align);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000241 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000242 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000243 case BuiltinType::LongLong:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000244 Target.getLongLongInfo(Size, Align);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000245 break;
246 case BuiltinType::Float:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000247 Target.getFloatInfo(Size, Align, F);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000248 break;
249 case BuiltinType::Double:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000250 Target.getDoubleInfo(Size, Align, F);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000251 break;
252 case BuiltinType::LongDouble:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000253 Target.getLongDoubleInfo(Size, Align, F);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000254 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000255 }
256 break;
257 }
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000258 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000259 // FIXME: Pointers into different addr spaces could have different sizes and
260 // alignment requirements: getPointerInfo should take an AddrSpace.
261 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000262 case Type::ObjCQualifiedId:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000263 case Type::Pointer:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000264 Target.getPointerInfo(Size, Align);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000265 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000266 case Type::Reference:
267 // "When applied to a reference or a reference type, the result is the size
268 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000269 // FIXME: This is wrong for struct layout: a reference in a struct has
270 // pointer size.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000271 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType());
Chris Lattner4b009652007-07-25 00:24:17 +0000272
273 case Type::Complex: {
274 // Complex types have the same alignment as their elements, but twice the
275 // size.
276 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000277 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner4b009652007-07-25 00:24:17 +0000278 Size = EltInfo.first*2;
279 Align = EltInfo.second;
280 break;
281 }
282 case Type::Tagged:
Chris Lattnereb56d292007-08-27 17:38:00 +0000283 TagType *TT = cast<TagType>(T);
284 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000285 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
Chris Lattnereb56d292007-08-27 17:38:00 +0000286 Size = Layout.getSize();
287 Align = Layout.getAlignment();
288 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000289 return getTypeInfo(ED->getIntegerType());
Chris Lattnereb56d292007-08-27 17:38:00 +0000290 } else {
Chris Lattner4b009652007-07-25 00:24:17 +0000291 assert(0 && "Unimplemented type sizes!");
Chris Lattnereb56d292007-08-27 17:38:00 +0000292 }
Chris Lattner4b009652007-07-25 00:24:17 +0000293 break;
294 }
295
296 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
297 return std::make_pair(Size, Align);
298}
299
Devang Patel7a78e432007-11-01 19:11:01 +0000300/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000301/// specified record (struct/union/class), which indicates its size and field
302/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000303const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000304 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
305
306 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000307 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000308 if (Entry) return *Entry;
309
Devang Patel7a78e432007-11-01 19:11:01 +0000310 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
311 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
312 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000313 Entry = NewEntry;
314
315 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
316 uint64_t RecordSize = 0;
317 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
318
319 if (D->getKind() != Decl::Union) {
Anders Carlsson7dce0292008-02-16 19:51:27 +0000320 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
321 RecordAlign = std::max(RecordAlign, AA->getAlignment());
322
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000323 bool StructIsPacked = D->getAttr<PackedAttr>();
324
Chris Lattner4b009652007-07-25 00:24:17 +0000325 // Layout each field, for now, just sequentially, respecting alignment. In
326 // the future, this will need to be tweakable by targets.
327 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
328 const FieldDecl *FD = D->getMember(i);
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000329 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
Eli Friedman67571ac2008-02-06 05:33:51 +0000330 uint64_t FieldSize;
331 unsigned FieldAlign;
Anders Carlsson058237f2008-02-18 07:13:09 +0000332
333 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
334 llvm::APSInt I(32);
335 bool BitWidthIsICE =
336 BitWidthExpr->isIntegerConstantExpr(I, *this);
337 assert (BitWidthIsICE && "Invalid BitField size expression");
338 FieldSize = I.getZExtValue();
339
Chris Lattner8cd0e932008-03-05 18:54:05 +0000340 std::pair<uint64_t, unsigned> TypeInfo = getTypeInfo(FD->getType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000341 uint64_t TypeSize = TypeInfo.first;
Anders Carlsson7dce0292008-02-16 19:51:27 +0000342
343 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
344 FieldAlign = AA->getAlignment();
345 else if (FieldIsPacked)
346 FieldAlign = 8;
347 else {
Anders Carlsson058237f2008-02-18 07:13:09 +0000348 // FIXME: This is X86 specific, use 32-bit alignment for long long.
349 if (FD->getType()->isIntegerType() && TypeInfo.second > 32)
350 FieldAlign = 32;
351 else
352 FieldAlign = TypeInfo.second;
Anders Carlsson7dce0292008-02-16 19:51:27 +0000353 }
Eli Friedman67571ac2008-02-06 05:33:51 +0000354
Anders Carlsson058237f2008-02-18 07:13:09 +0000355 // Check if we need to add padding to give the field the correct
356 // alignment.
357 if (RecordSize % FieldAlign + FieldSize > TypeSize)
358 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
359
360 } else {
361 if (FD->getType()->isIncompleteType()) {
362 // This must be a flexible array member; we can't directly
363 // query getTypeInfo about these, so we figure it out here.
364 // Flexible array members don't have any size, but they
365 // have to be aligned appropriately for their element type.
366
367 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
368 FieldAlign = AA->getAlignment();
369 else if (FieldIsPacked)
370 FieldAlign = 8;
371 else {
372 const ArrayType* ATy = FD->getType()->getAsArrayType();
Chris Lattner8cd0e932008-03-05 18:54:05 +0000373 FieldAlign = getTypeAlign(ATy->getElementType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000374 }
375 FieldSize = 0;
376 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000377 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000378 FieldSize = FieldInfo.first;
379
380 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
381 FieldAlign = AA->getAlignment();
382 else if (FieldIsPacked)
383 FieldAlign = 8;
384 else
385 FieldAlign = FieldInfo.second;
386 }
387
388 // Round up the current record size to the field's alignment boundary.
389 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
390 }
Chris Lattner4b009652007-07-25 00:24:17 +0000391
392 // Place this field at the current location.
393 FieldOffsets[i] = RecordSize;
394
395 // Reserve space for this field.
396 RecordSize += FieldSize;
397
398 // Remember max struct/class alignment.
399 RecordAlign = std::max(RecordAlign, FieldAlign);
400 }
401
402 // Finally, round the size of the total struct up to the alignment of the
403 // struct itself.
404 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
405 } else {
406 // Union layout just puts each member at the start of the record.
407 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
408 const FieldDecl *FD = D->getMember(i);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000409 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Chris Lattner4b009652007-07-25 00:24:17 +0000410 uint64_t FieldSize = FieldInfo.first;
411 unsigned FieldAlign = FieldInfo.second;
412
Anders Carlsson058237f2008-02-18 07:13:09 +0000413 // FIXME: This is X86 specific, use 32-bit alignment for long long.
414 if (FD->getType()->isIntegerType() && FieldAlign > 32)
415 FieldAlign = 32;
416
Chris Lattner4b009652007-07-25 00:24:17 +0000417 // Round up the current record size to the field's alignment boundary.
418 RecordSize = std::max(RecordSize, FieldSize);
419
420 // Place this field at the start of the record.
421 FieldOffsets[i] = 0;
422
423 // Remember max struct/class alignment.
424 RecordAlign = std::max(RecordAlign, FieldAlign);
425 }
426 }
427
428 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
429 return *NewEntry;
430}
431
Chris Lattner4b009652007-07-25 00:24:17 +0000432//===----------------------------------------------------------------------===//
433// Type creation/memoization methods
434//===----------------------------------------------------------------------===//
435
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000436QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattner35fef522008-02-20 20:55:12 +0000437 if (T.getCanonicalType().getAddressSpace() == AddressSpace)
438 return T;
439
440 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
441 // with CVR qualifiers from here on out.
442 assert(T.getCanonicalType().getAddressSpace() == 0 &&
443 "Type is already address space qualified");
444
445 // Check if we've already instantiated an address space qual'd type of this
446 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000447 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000448 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000449 void *InsertPos = 0;
450 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
451 return QualType(ASQy, 0);
452
453 // If the base type isn't canonical, this won't be a canonical type either,
454 // so fill in the canonical type field.
455 QualType Canonical;
456 if (!T->isCanonical()) {
457 Canonical = getASQualType(T.getCanonicalType(), AddressSpace);
458
459 // Get the new insert position for the node we care about.
460 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
461 assert(NewIP == 0 && "Shouldn't be in the map!");
462 }
Chris Lattner35fef522008-02-20 20:55:12 +0000463 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000464 ASQualTypes.InsertNode(New, InsertPos);
465 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000466 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000467}
468
Chris Lattner4b009652007-07-25 00:24:17 +0000469
470/// getComplexType - Return the uniqued reference to the type for a complex
471/// number with the specified element type.
472QualType ASTContext::getComplexType(QualType T) {
473 // Unique pointers, to guarantee there is only one pointer of a particular
474 // structure.
475 llvm::FoldingSetNodeID ID;
476 ComplexType::Profile(ID, T);
477
478 void *InsertPos = 0;
479 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
480 return QualType(CT, 0);
481
482 // If the pointee type isn't canonical, this won't be a canonical type either,
483 // so fill in the canonical type field.
484 QualType Canonical;
485 if (!T->isCanonical()) {
486 Canonical = getComplexType(T.getCanonicalType());
487
488 // Get the new insert position for the node we care about.
489 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
490 assert(NewIP == 0 && "Shouldn't be in the map!");
491 }
492 ComplexType *New = new ComplexType(T, Canonical);
493 Types.push_back(New);
494 ComplexTypes.InsertNode(New, InsertPos);
495 return QualType(New, 0);
496}
497
498
499/// getPointerType - Return the uniqued reference to the type for a pointer to
500/// the specified type.
501QualType ASTContext::getPointerType(QualType T) {
502 // Unique pointers, to guarantee there is only one pointer of a particular
503 // structure.
504 llvm::FoldingSetNodeID ID;
505 PointerType::Profile(ID, T);
506
507 void *InsertPos = 0;
508 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
509 return QualType(PT, 0);
510
511 // If the pointee type isn't canonical, this won't be a canonical type either,
512 // so fill in the canonical type field.
513 QualType Canonical;
514 if (!T->isCanonical()) {
515 Canonical = getPointerType(T.getCanonicalType());
516
517 // Get the new insert position for the node we care about.
518 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
519 assert(NewIP == 0 && "Shouldn't be in the map!");
520 }
521 PointerType *New = new PointerType(T, Canonical);
522 Types.push_back(New);
523 PointerTypes.InsertNode(New, InsertPos);
524 return QualType(New, 0);
525}
526
527/// getReferenceType - Return the uniqued reference to the type for a reference
528/// to the specified type.
529QualType ASTContext::getReferenceType(QualType T) {
530 // Unique pointers, to guarantee there is only one pointer of a particular
531 // structure.
532 llvm::FoldingSetNodeID ID;
533 ReferenceType::Profile(ID, T);
534
535 void *InsertPos = 0;
536 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
537 return QualType(RT, 0);
538
539 // If the referencee type isn't canonical, this won't be a canonical type
540 // either, so fill in the canonical type field.
541 QualType Canonical;
542 if (!T->isCanonical()) {
543 Canonical = getReferenceType(T.getCanonicalType());
544
545 // Get the new insert position for the node we care about.
546 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
547 assert(NewIP == 0 && "Shouldn't be in the map!");
548 }
549
550 ReferenceType *New = new ReferenceType(T, Canonical);
551 Types.push_back(New);
552 ReferenceTypes.InsertNode(New, InsertPos);
553 return QualType(New, 0);
554}
555
Steve Naroff83c13012007-08-30 01:06:46 +0000556/// getConstantArrayType - Return the unique reference to the type for an
557/// array of the specified element type.
558QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000559 const llvm::APInt &ArySize,
560 ArrayType::ArraySizeModifier ASM,
561 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000562 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000563 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000564
565 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000566 if (ConstantArrayType *ATP =
567 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000568 return QualType(ATP, 0);
569
570 // If the element type isn't canonical, this won't be a canonical type either,
571 // so fill in the canonical type field.
572 QualType Canonical;
573 if (!EltTy->isCanonical()) {
Steve Naroff24c9b982007-08-30 18:10:14 +0000574 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
575 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000576 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000577 ConstantArrayType *NewIP =
578 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
579
Chris Lattner4b009652007-07-25 00:24:17 +0000580 assert(NewIP == 0 && "Shouldn't be in the map!");
581 }
582
Steve Naroff24c9b982007-08-30 18:10:14 +0000583 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
584 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000585 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000586 Types.push_back(New);
587 return QualType(New, 0);
588}
589
Steve Naroffe2579e32007-08-30 18:14:25 +0000590/// getVariableArrayType - Returns a non-unique reference to the type for a
591/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000592QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
593 ArrayType::ArraySizeModifier ASM,
594 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000595 // Since we don't unique expressions, it isn't possible to unique VLA's
596 // that have an expression provided for their size.
597
598 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
599 ASM, EltTypeQuals);
600
601 VariableArrayTypes.push_back(New);
602 Types.push_back(New);
603 return QualType(New, 0);
604}
605
606QualType ASTContext::getIncompleteArrayType(QualType EltTy,
607 ArrayType::ArraySizeModifier ASM,
608 unsigned EltTypeQuals) {
609 llvm::FoldingSetNodeID ID;
610 IncompleteArrayType::Profile(ID, EltTy);
611
612 void *InsertPos = 0;
613 if (IncompleteArrayType *ATP =
614 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
615 return QualType(ATP, 0);
616
617 // If the element type isn't canonical, this won't be a canonical type
618 // either, so fill in the canonical type field.
619 QualType Canonical;
620
621 if (!EltTy->isCanonical()) {
622 Canonical = getIncompleteArrayType(EltTy.getCanonicalType(),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000623 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000624
625 // Get the new insert position for the node we care about.
626 IncompleteArrayType *NewIP =
627 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
628
629 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000630 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000631
632 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
633 ASM, EltTypeQuals);
634
635 IncompleteArrayTypes.InsertNode(New, InsertPos);
636 Types.push_back(New);
637 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000638}
639
Chris Lattner4b009652007-07-25 00:24:17 +0000640/// getVectorType - Return the unique reference to a vector type of
641/// the specified element type and size. VectorType must be a built-in type.
642QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
643 BuiltinType *baseType;
644
645 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
646 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
647
648 // Check if we've already instantiated a vector of this type.
649 llvm::FoldingSetNodeID ID;
650 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
651 void *InsertPos = 0;
652 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
653 return QualType(VTP, 0);
654
655 // If the element type isn't canonical, this won't be a canonical type either,
656 // so fill in the canonical type field.
657 QualType Canonical;
658 if (!vecType->isCanonical()) {
659 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
660
661 // Get the new insert position for the node we care about.
662 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
663 assert(NewIP == 0 && "Shouldn't be in the map!");
664 }
665 VectorType *New = new VectorType(vecType, NumElts, Canonical);
666 VectorTypes.InsertNode(New, InsertPos);
667 Types.push_back(New);
668 return QualType(New, 0);
669}
670
671/// getOCUVectorType - Return the unique reference to an OCU vector type of
672/// the specified element type and size. VectorType must be a built-in type.
673QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
674 BuiltinType *baseType;
675
676 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
677 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
678
679 // Check if we've already instantiated a vector of this type.
680 llvm::FoldingSetNodeID ID;
681 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
682 void *InsertPos = 0;
683 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
684 return QualType(VTP, 0);
685
686 // If the element type isn't canonical, this won't be a canonical type either,
687 // so fill in the canonical type field.
688 QualType Canonical;
689 if (!vecType->isCanonical()) {
690 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
691
692 // Get the new insert position for the node we care about.
693 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
694 assert(NewIP == 0 && "Shouldn't be in the map!");
695 }
696 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
697 VectorTypes.InsertNode(New, InsertPos);
698 Types.push_back(New);
699 return QualType(New, 0);
700}
701
702/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
703///
704QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
705 // Unique functions, to guarantee there is only one function of a particular
706 // structure.
707 llvm::FoldingSetNodeID ID;
708 FunctionTypeNoProto::Profile(ID, ResultTy);
709
710 void *InsertPos = 0;
711 if (FunctionTypeNoProto *FT =
712 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
713 return QualType(FT, 0);
714
715 QualType Canonical;
716 if (!ResultTy->isCanonical()) {
717 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
718
719 // Get the new insert position for the node we care about.
720 FunctionTypeNoProto *NewIP =
721 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
722 assert(NewIP == 0 && "Shouldn't be in the map!");
723 }
724
725 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
726 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000727 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000728 return QualType(New, 0);
729}
730
731/// getFunctionType - Return a normal function type with a typed argument
732/// list. isVariadic indicates whether the argument list includes '...'.
733QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
734 unsigned NumArgs, bool isVariadic) {
735 // Unique functions, to guarantee there is only one function of a particular
736 // structure.
737 llvm::FoldingSetNodeID ID;
738 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
739
740 void *InsertPos = 0;
741 if (FunctionTypeProto *FTP =
742 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
743 return QualType(FTP, 0);
744
745 // Determine whether the type being created is already canonical or not.
746 bool isCanonical = ResultTy->isCanonical();
747 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
748 if (!ArgArray[i]->isCanonical())
749 isCanonical = false;
750
751 // If this type isn't canonical, get the canonical version of it.
752 QualType Canonical;
753 if (!isCanonical) {
754 llvm::SmallVector<QualType, 16> CanonicalArgs;
755 CanonicalArgs.reserve(NumArgs);
756 for (unsigned i = 0; i != NumArgs; ++i)
757 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
758
759 Canonical = getFunctionType(ResultTy.getCanonicalType(),
760 &CanonicalArgs[0], NumArgs,
761 isVariadic);
762
763 // Get the new insert position for the node we care about.
764 FunctionTypeProto *NewIP =
765 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
766 assert(NewIP == 0 && "Shouldn't be in the map!");
767 }
768
769 // FunctionTypeProto objects are not allocated with new because they have a
770 // variable size array (for parameter types) at the end of them.
771 FunctionTypeProto *FTP =
772 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
773 NumArgs*sizeof(QualType));
774 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
775 Canonical);
776 Types.push_back(FTP);
777 FunctionTypeProtos.InsertNode(FTP, InsertPos);
778 return QualType(FTP, 0);
779}
780
781/// getTypedefType - Return the unique reference to the type for the
782/// specified typename decl.
783QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
784 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
785
786 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000787 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000788 Types.push_back(Decl->TypeForDecl);
789 return QualType(Decl->TypeForDecl, 0);
790}
791
Ted Kremenek42730c52008-01-07 19:49:32 +0000792/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000793/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000794QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000795 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
796
Ted Kremenek42730c52008-01-07 19:49:32 +0000797 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000798 Types.push_back(Decl->TypeForDecl);
799 return QualType(Decl->TypeForDecl, 0);
800}
801
Ted Kremenek42730c52008-01-07 19:49:32 +0000802/// getObjCQualifiedInterfaceType - Return a
803/// ObjCQualifiedInterfaceType type for the given interface decl and
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000804/// the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000805QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
806 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000807 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000808 ObjCQualifiedInterfaceType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000809
810 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000811 if (ObjCQualifiedInterfaceType *QT =
812 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000813 return QualType(QT, 0);
814
815 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +0000816 ObjCQualifiedInterfaceType *QType =
817 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000818 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000819 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000820 return QualType(QType, 0);
821}
822
Ted Kremenek42730c52008-01-07 19:49:32 +0000823/// getObjCQualifiedIdType - Return a
824/// getObjCQualifiedIdType type for the 'id' decl and
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000825/// the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000826QualType ASTContext::getObjCQualifiedIdType(QualType idType,
827 ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000828 unsigned NumProtocols) {
829 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000830 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000831
832 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000833 if (ObjCQualifiedIdType *QT =
834 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000835 return QualType(QT, 0);
836
837 // No Match;
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000838 QualType Canonical;
839 if (!idType->isCanonical()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000840 Canonical = getObjCQualifiedIdType(idType.getCanonicalType(),
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000841 Protocols, NumProtocols);
Ted Kremenek42730c52008-01-07 19:49:32 +0000842 ObjCQualifiedIdType *NewQT =
843 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000844 assert(NewQT == 0 && "Shouldn't be in the map!");
845 }
846
Ted Kremenek42730c52008-01-07 19:49:32 +0000847 ObjCQualifiedIdType *QType =
848 new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000849 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000850 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000851 return QualType(QType, 0);
852}
853
Steve Naroff0604dd92007-08-01 18:02:17 +0000854/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
855/// TypeOfExpr AST's (since expression's are never shared). For example,
856/// multiple declarations that refer to "typeof(x)" all contain different
857/// DeclRefExpr's. This doesn't effect the type checker, since it operates
858/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000859QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroff7cbb1462007-07-31 12:34:36 +0000860 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000861 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
862 Types.push_back(toe);
863 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000864}
865
Steve Naroff0604dd92007-08-01 18:02:17 +0000866/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
867/// TypeOfType AST's. The only motivation to unique these nodes would be
868/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
869/// an issue. This doesn't effect the type checker, since it operates
870/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000871QualType ASTContext::getTypeOfType(QualType tofType) {
872 QualType Canonical = tofType.getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000873 TypeOfType *tot = new TypeOfType(tofType, Canonical);
874 Types.push_back(tot);
875 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000876}
877
Chris Lattner4b009652007-07-25 00:24:17 +0000878/// getTagDeclType - Return the unique reference to the type for the
879/// specified TagDecl (struct/union/class/enum) decl.
880QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +0000881 assert (Decl);
882
Ted Kremenekf05026d2007-11-14 00:03:20 +0000883 // The decl stores the type cache.
Ted Kremenekae8fa032007-11-26 21:16:01 +0000884 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Ted Kremenekf05026d2007-11-14 00:03:20 +0000885
886 TagType* T = new TagType(Decl, QualType());
Ted Kremenekae8fa032007-11-26 21:16:01 +0000887 Types.push_back(T);
888 Decl->TypeForDecl = T;
Ted Kremenekf05026d2007-11-14 00:03:20 +0000889
890 return QualType(T, 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000891}
892
893/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
894/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
895/// needs to agree with the definition in <stddef.h>.
896QualType ASTContext::getSizeType() const {
897 // On Darwin, size_t is defined as a "long unsigned int".
898 // FIXME: should derive from "Target".
899 return UnsignedLongTy;
900}
901
Eli Friedmanfdd35d72008-02-12 08:29:21 +0000902/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
903/// width of characters in wide strings, The value is target dependent and
904/// needs to agree with the definition in <stddef.h>.
905QualType ASTContext::getWcharType() const {
906 // On Darwin, wchar_t is defined as a "int".
907 // FIXME: should derive from "Target".
908 return IntTy;
909}
910
Chris Lattner4b009652007-07-25 00:24:17 +0000911/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
912/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
913QualType ASTContext::getPointerDiffType() const {
914 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
915 // FIXME: should derive from "Target".
916 return IntTy;
917}
918
919/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
920/// routine will assert if passed a built-in type that isn't an integer or enum.
921static int getIntegerRank(QualType t) {
922 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
923 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
924 return 4;
925 }
926
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000927 const BuiltinType *BT = t.getCanonicalType()->getAsBuiltinType();
Chris Lattner4b009652007-07-25 00:24:17 +0000928 switch (BT->getKind()) {
929 default:
930 assert(0 && "getIntegerRank(): not a built-in integer");
931 case BuiltinType::Bool:
932 return 1;
933 case BuiltinType::Char_S:
934 case BuiltinType::Char_U:
935 case BuiltinType::SChar:
936 case BuiltinType::UChar:
937 return 2;
938 case BuiltinType::Short:
939 case BuiltinType::UShort:
940 return 3;
941 case BuiltinType::Int:
942 case BuiltinType::UInt:
943 return 4;
944 case BuiltinType::Long:
945 case BuiltinType::ULong:
946 return 5;
947 case BuiltinType::LongLong:
948 case BuiltinType::ULongLong:
949 return 6;
950 }
951}
952
953/// getFloatingRank - Return a relative rank for floating point types.
954/// This routine will assert if passed a built-in type that isn't a float.
955static int getFloatingRank(QualType T) {
956 T = T.getCanonicalType();
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000957 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +0000958 return getFloatingRank(CT->getElementType());
959
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000960 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattner5003e8b2007-11-01 05:03:41 +0000961 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +0000962 case BuiltinType::Float: return FloatRank;
963 case BuiltinType::Double: return DoubleRank;
964 case BuiltinType::LongDouble: return LongDoubleRank;
965 }
966}
967
Steve Narofffa0c4532007-08-27 01:41:48 +0000968/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
969/// point or a complex type (based on typeDomain/typeSize).
970/// 'typeDomain' is a real floating point or complex type.
971/// 'typeSize' is a real floating point or complex type.
Steve Naroff3cf497f2007-08-27 01:27:54 +0000972QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
973 QualType typeSize, QualType typeDomain) const {
974 if (typeDomain->isComplexType()) {
975 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000976 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000977 case FloatRank: return FloatComplexTy;
978 case DoubleRank: return DoubleComplexTy;
979 case LongDoubleRank: return LongDoubleComplexTy;
980 }
Chris Lattner4b009652007-07-25 00:24:17 +0000981 }
Steve Naroff3cf497f2007-08-27 01:27:54 +0000982 if (typeDomain->isRealFloatingType()) {
983 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000984 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000985 case FloatRank: return FloatTy;
986 case DoubleRank: return DoubleTy;
987 case LongDoubleRank: return LongDoubleTy;
988 }
989 }
990 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattner1d2b4612007-09-16 19:23:47 +0000991 //an invalid return value, but the assert
992 //will ensure that this code is never reached.
993 return VoidTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000994}
995
Steve Naroff45fc9822007-08-27 15:30:22 +0000996/// compareFloatingType - Handles 3 different combos:
997/// float/float, float/complex, complex/complex.
998/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
999int ASTContext::compareFloatingType(QualType lt, QualType rt) {
1000 if (getFloatingRank(lt) == getFloatingRank(rt))
1001 return 0;
1002 if (getFloatingRank(lt) > getFloatingRank(rt))
1003 return 1;
1004 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001005}
1006
1007// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
1008// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
1009QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
1010 if (lhs == rhs) return lhs;
1011
1012 bool t1Unsigned = lhs->isUnsignedIntegerType();
1013 bool t2Unsigned = rhs->isUnsignedIntegerType();
1014
1015 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
1016 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
1017
1018 // We have two integer types with differing signs
1019 QualType unsignedType = t1Unsigned ? lhs : rhs;
1020 QualType signedType = t1Unsigned ? rhs : lhs;
1021
1022 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
1023 return unsignedType;
1024 else {
1025 // FIXME: Need to check if the signed type can represent all values of the
1026 // unsigned type. If it can, then the result is the signed type.
1027 // If it can't, then the result is the unsigned version of the signed type.
1028 // Should probably add a helper that returns a signed integer type from
1029 // an unsigned (and vice versa). C99 6.3.1.8.
1030 return signedType;
1031 }
1032}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001033
1034// getCFConstantStringType - Return the type used for constant CFStrings.
1035QualType ASTContext::getCFConstantStringType() {
1036 if (!CFConstantStringTypeDecl) {
1037 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
Steve Naroff0add5d22007-11-03 11:27:19 +00001038 &Idents.get("NSConstantString"),
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001039 0);
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001040 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001041
1042 // const int *isa;
1043 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001044 // int flags;
1045 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001046 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001047 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001048 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001049 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001050 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001051 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001052
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001053 for (unsigned i = 0; i < 4; ++i)
Steve Naroffdc1ad762007-09-14 02:20:46 +00001054 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001055
1056 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1057 }
1058
1059 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001060}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001061
Anders Carlssone3f02572007-10-29 06:33:42 +00001062// This returns true if a type has been typedefed to BOOL:
1063// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001064static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001065 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001066 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001067
1068 return false;
1069}
1070
Ted Kremenek42730c52008-01-07 19:49:32 +00001071/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001072/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001073int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001074 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001075
1076 // Make all integer and enum types at least as large as an int
1077 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001078 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001079 // Treat arrays as pointers, since that's how they're passed in.
1080 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001081 sz = getTypeSize(VoidPtrTy);
1082 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001083}
1084
Ted Kremenek42730c52008-01-07 19:49:32 +00001085/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001086/// declaration.
Ted Kremenek42730c52008-01-07 19:49:32 +00001087void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001088 std::string& S)
1089{
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001090 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001091 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001092 // Encode result type.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001093 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001094 // Compute size of all parameters.
1095 // Start with computing size of a pointer in number of bytes.
1096 // FIXME: There might(should) be a better way of doing this computation!
1097 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001098 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001099 // The first two arguments (self and _cmd) are pointers; account for
1100 // their size.
1101 int ParmOffset = 2 * PtrSize;
1102 int NumOfParams = Decl->getNumParams();
1103 for (int i = 0; i < NumOfParams; i++) {
1104 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001105 int sz = getObjCEncodingTypeSize (PType);
1106 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001107 ParmOffset += sz;
1108 }
1109 S += llvm::utostr(ParmOffset);
1110 S += "@0:";
1111 S += llvm::utostr(PtrSize);
1112
1113 // Argument types.
1114 ParmOffset = 2 * PtrSize;
1115 for (int i = 0; i < NumOfParams; i++) {
1116 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001117 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001118 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001119 getObjCEncodingForTypeQualifier(
1120 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian248db262008-01-22 22:44:46 +00001121 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001122 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001123 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001124 }
1125}
1126
Fariborz Jahanian248db262008-01-22 22:44:46 +00001127void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1128 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson36f07d82007-10-29 05:01:08 +00001129{
Anders Carlssone3f02572007-10-29 06:33:42 +00001130 // FIXME: This currently doesn't encode:
1131 // @ An object (whether statically typed or typed id)
1132 // # A class object (Class)
1133 // : A method selector (SEL)
1134 // {name=type...} A structure
1135 // (name=type...) A union
1136 // bnum A bit field of num bits
1137
1138 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001139 char encoding;
1140 switch (BT->getKind()) {
1141 case BuiltinType::Void:
1142 encoding = 'v';
1143 break;
1144 case BuiltinType::Bool:
1145 encoding = 'B';
1146 break;
1147 case BuiltinType::Char_U:
1148 case BuiltinType::UChar:
1149 encoding = 'C';
1150 break;
1151 case BuiltinType::UShort:
1152 encoding = 'S';
1153 break;
1154 case BuiltinType::UInt:
1155 encoding = 'I';
1156 break;
1157 case BuiltinType::ULong:
1158 encoding = 'L';
1159 break;
1160 case BuiltinType::ULongLong:
1161 encoding = 'Q';
1162 break;
1163 case BuiltinType::Char_S:
1164 case BuiltinType::SChar:
1165 encoding = 'c';
1166 break;
1167 case BuiltinType::Short:
1168 encoding = 's';
1169 break;
1170 case BuiltinType::Int:
1171 encoding = 'i';
1172 break;
1173 case BuiltinType::Long:
1174 encoding = 'l';
1175 break;
1176 case BuiltinType::LongLong:
1177 encoding = 'q';
1178 break;
1179 case BuiltinType::Float:
1180 encoding = 'f';
1181 break;
1182 case BuiltinType::Double:
1183 encoding = 'd';
1184 break;
1185 case BuiltinType::LongDouble:
1186 encoding = 'd';
1187 break;
1188 default:
1189 assert(0 && "Unhandled builtin type kind");
1190 }
1191
1192 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001193 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001194 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001195 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001196 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001197
1198 }
1199 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001200 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001201 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001202 S += '@';
1203 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001204 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001205 S += '#';
1206 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001207 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001208 S += ':';
1209 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001210 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001211
1212 if (PointeeTy->isCharType()) {
1213 // char pointer types should be encoded as '*' unless it is a
1214 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001215 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001216 S += '*';
1217 return;
1218 }
1219 }
1220
1221 S += '^';
Fariborz Jahanian248db262008-01-22 22:44:46 +00001222 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone3f02572007-10-29 06:33:42 +00001223 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001224 S += '[';
1225
1226 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1227 S += llvm::utostr(CAT->getSize().getZExtValue());
1228 else
1229 assert(0 && "Unhandled array type!");
1230
Fariborz Jahanian248db262008-01-22 22:44:46 +00001231 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001232 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001233 } else if (T->getAsFunctionType()) {
1234 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001235 } else if (const RecordType *RTy = T->getAsRecordType()) {
1236 RecordDecl *RDecl= RTy->getDecl();
1237 S += '{';
1238 S += RDecl->getName();
Fariborz Jahanian248db262008-01-22 22:44:46 +00001239 bool found = false;
1240 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1241 if (ERType[i] == RTy) {
1242 found = true;
1243 break;
1244 }
1245 if (!found) {
1246 ERType.push_back(RTy);
1247 S += '=';
1248 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1249 FieldDecl *field = RDecl->getMember(i);
1250 getObjCEncodingForType(field->getType(), S, ERType);
1251 }
1252 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1253 ERType.pop_back();
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001254 }
1255 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001256 } else if (T->isEnumeralType()) {
1257 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001258 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001259 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001260}
1261
Ted Kremenek42730c52008-01-07 19:49:32 +00001262void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001263 std::string& S) const {
1264 if (QT & Decl::OBJC_TQ_In)
1265 S += 'n';
1266 if (QT & Decl::OBJC_TQ_Inout)
1267 S += 'N';
1268 if (QT & Decl::OBJC_TQ_Out)
1269 S += 'o';
1270 if (QT & Decl::OBJC_TQ_Bycopy)
1271 S += 'O';
1272 if (QT & Decl::OBJC_TQ_Byref)
1273 S += 'R';
1274 if (QT & Decl::OBJC_TQ_Oneway)
1275 S += 'V';
1276}
1277
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001278void ASTContext::setBuiltinVaListType(QualType T)
1279{
1280 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1281
1282 BuiltinVaListType = T;
1283}
1284
Ted Kremenek42730c52008-01-07 19:49:32 +00001285void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001286{
Ted Kremenek42730c52008-01-07 19:49:32 +00001287 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff9d12c902007-10-15 14:41:52 +00001288
Ted Kremenek42730c52008-01-07 19:49:32 +00001289 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001290
1291 // typedef struct objc_object *id;
1292 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1293 assert(ptr && "'id' incorrectly typed");
1294 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1295 assert(rec && "'id' incorrectly typed");
1296 IdStructType = rec;
1297}
1298
Ted Kremenek42730c52008-01-07 19:49:32 +00001299void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001300{
Ted Kremenek42730c52008-01-07 19:49:32 +00001301 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001302
Ted Kremenek42730c52008-01-07 19:49:32 +00001303 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001304
1305 // typedef struct objc_selector *SEL;
1306 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1307 assert(ptr && "'SEL' incorrectly typed");
1308 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1309 assert(rec && "'SEL' incorrectly typed");
1310 SelStructType = rec;
1311}
1312
Ted Kremenek42730c52008-01-07 19:49:32 +00001313void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001314{
Ted Kremenek42730c52008-01-07 19:49:32 +00001315 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1316 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001317}
1318
Ted Kremenek42730c52008-01-07 19:49:32 +00001319void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001320{
Ted Kremenek42730c52008-01-07 19:49:32 +00001321 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001322
Ted Kremenek42730c52008-01-07 19:49:32 +00001323 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001324
1325 // typedef struct objc_class *Class;
1326 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1327 assert(ptr && "'Class' incorrectly typed");
1328 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1329 assert(rec && "'Class' incorrectly typed");
1330 ClassStructType = rec;
1331}
1332
Ted Kremenek42730c52008-01-07 19:49:32 +00001333void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1334 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001335 "'NSConstantString' type already set!");
1336
Ted Kremenek42730c52008-01-07 19:49:32 +00001337 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001338}
1339
Steve Naroff85f0dc52007-10-15 20:41:53 +00001340bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1341 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1342 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1343
1344 return lBuiltin->getKind() == rBuiltin->getKind();
1345}
1346
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001347/// objcTypesAreCompatible - This routine is called when two types
1348/// are of different class; one is interface type or is
1349/// a qualified interface type and the other type is of a different class.
1350/// Example, II or II<P>.
Steve Naroff85f0dc52007-10-15 20:41:53 +00001351bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001352 if (lhs->isObjCInterfaceType() && isObjCIdType(rhs))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001353 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001354 else if (isObjCIdType(lhs) && rhs->isObjCInterfaceType())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001355 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001356 if (ObjCInterfaceType *lhsIT =
1357 dyn_cast<ObjCInterfaceType>(lhs.getCanonicalType().getTypePtr())) {
1358 ObjCQualifiedInterfaceType *rhsQI =
1359 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001360 return rhsQI && (lhsIT->getDecl() == rhsQI->getDecl());
1361 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001362 else if (ObjCInterfaceType *rhsIT =
1363 dyn_cast<ObjCInterfaceType>(rhs.getCanonicalType().getTypePtr())) {
1364 ObjCQualifiedInterfaceType *lhsQI =
1365 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001366 return lhsQI && (rhsIT->getDecl() == lhsQI->getDecl());
1367 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00001368 return false;
1369}
1370
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001371/// Check that 'lhs' and 'rhs' are compatible interface types. Both types
1372/// must be canonical types.
Steve Naroff85f0dc52007-10-15 20:41:53 +00001373bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001374 assert (lhs->isCanonical() &&
1375 "interfaceTypesAreCompatible strip typedefs of lhs");
1376 assert (rhs->isCanonical() &&
1377 "interfaceTypesAreCompatible strip typedefs of rhs");
Fariborz Jahaniance2de812007-12-20 22:37:58 +00001378 if (lhs == rhs)
1379 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001380 ObjCInterfaceType *lhsIT = cast<ObjCInterfaceType>(lhs.getTypePtr());
1381 ObjCInterfaceType *rhsIT = cast<ObjCInterfaceType>(rhs.getTypePtr());
1382 ObjCInterfaceDecl *rhsIDecl = rhsIT->getDecl();
1383 ObjCInterfaceDecl *lhsIDecl = lhsIT->getDecl();
Fariborz Jahaniance2de812007-12-20 22:37:58 +00001384 // rhs is derived from lhs it is OK; else it is not OK.
1385 while (rhsIDecl != NULL) {
1386 if (rhsIDecl == lhsIDecl)
1387 return true;
1388 rhsIDecl = rhsIDecl->getSuperClass();
1389 }
1390 return false;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001391}
1392
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001393bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
1394 QualType rhs) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001395 ObjCQualifiedInterfaceType *lhsQI =
1396 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001397 assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
Ted Kremenek42730c52008-01-07 19:49:32 +00001398 ObjCQualifiedInterfaceType *rhsQI =
1399 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001400 assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001401 if (!interfaceTypesAreCompatible(
1402 getObjCInterfaceType(lhsQI->getDecl()).getCanonicalType(),
1403 getObjCInterfaceType(rhsQI->getDecl()).getCanonicalType()))
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001404 return false;
1405 /* All protocols in lhs must have a presense in rhs. */
1406 for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
1407 bool match = false;
Ted Kremenek42730c52008-01-07 19:49:32 +00001408 ObjCProtocolDecl *lhsProto = lhsQI->getProtocols(i);
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001409 for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001410 ObjCProtocolDecl *rhsProto = rhsQI->getProtocols(j);
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001411 if (lhsProto == rhsProto) {
1412 match = true;
1413 break;
1414 }
1415 }
1416 if (!match)
1417 return false;
1418 }
1419 return true;
1420}
1421
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001422/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
1423/// inheritance hierarchy of 'rProto'.
Ted Kremenek42730c52008-01-07 19:49:32 +00001424static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
1425 ObjCProtocolDecl *rProto) {
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001426 if (lProto == rProto)
1427 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001428 ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001429 for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
1430 if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
1431 return true;
1432 return false;
1433}
1434
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001435/// ClassImplementsProtocol - Checks that 'lProto' protocol
1436/// has been implemented in IDecl class, its super class or categories (if
1437/// lookupCategory is true).
Ted Kremenek42730c52008-01-07 19:49:32 +00001438static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1439 ObjCInterfaceDecl *IDecl,
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001440 bool lookupCategory) {
1441
1442 // 1st, look up the class.
Ted Kremenek42730c52008-01-07 19:49:32 +00001443 ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols();
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001444 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
1445 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1446 return true;
1447 }
1448
1449 // 2nd, look up the category.
1450 if (lookupCategory)
Ted Kremenek42730c52008-01-07 19:49:32 +00001451 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001452 CDecl = CDecl->getNextClassCategory()) {
1453 protoList = CDecl->getReferencedProtocols();
1454 for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
1455 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1456 return true;
1457 }
1458 }
1459
1460 // 3rd, look up the super class(s)
1461 if (IDecl->getSuperClass())
1462 return
1463 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory);
1464
1465 return false;
1466}
1467
Ted Kremenek42730c52008-01-07 19:49:32 +00001468/// ObjCQualifiedIdTypesAreCompatible - Compares two types, at least
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001469/// one of which is a protocol qualified 'id' type. When 'compare'
1470/// is true it is for comparison; when false, for assignment/initialization.
Ted Kremenek42730c52008-01-07 19:49:32 +00001471bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs,
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001472 QualType rhs,
1473 bool compare) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001474 // match id<P..> with an 'id' type in all cases.
1475 if (const PointerType *PT = lhs->getAsPointerType()) {
1476 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001477 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001478 return true;
1479
1480 }
1481 else if (const PointerType *PT = rhs->getAsPointerType()) {
1482 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001483 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001484 return true;
1485
1486 }
1487
Ted Kremenek42730c52008-01-07 19:49:32 +00001488 ObjCQualifiedInterfaceType *lhsQI = 0;
1489 ObjCQualifiedInterfaceType *rhsQI = 0;
1490 ObjCInterfaceDecl *lhsID = 0;
1491 ObjCInterfaceDecl *rhsID = 0;
1492 ObjCQualifiedIdType *lhsQID = dyn_cast<ObjCQualifiedIdType>(lhs);
1493 ObjCQualifiedIdType *rhsQID = dyn_cast<ObjCQualifiedIdType>(rhs);
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001494
1495 if (lhsQID) {
1496 if (!rhsQID && rhs->getTypeClass() == Type::Pointer) {
1497 QualType rtype =
1498 cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1499 rhsQI =
Ted Kremenek42730c52008-01-07 19:49:32 +00001500 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001501 rtype.getCanonicalType().getTypePtr());
Fariborz Jahanian87829072007-12-20 19:24:10 +00001502 if (!rhsQI) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001503 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanian87829072007-12-20 19:24:10 +00001504 rtype.getCanonicalType().getTypePtr());
1505 if (IT)
1506 rhsID = IT->getDecl();
1507 }
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001508 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001509 if (!rhsQI && !rhsQID && !rhsID)
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001510 return false;
1511
Fariborz Jahaniance5528d2008-01-03 20:01:35 +00001512 unsigned numRhsProtocols = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001513 ObjCProtocolDecl **rhsProtoList = 0;
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001514 if (rhsQI) {
1515 numRhsProtocols = rhsQI->getNumProtocols();
1516 rhsProtoList = rhsQI->getReferencedProtocols();
1517 }
1518 else if (rhsQID) {
1519 numRhsProtocols = rhsQID->getNumProtocols();
1520 rhsProtoList = rhsQID->getReferencedProtocols();
1521 }
1522
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001523 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001524 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001525 bool match = false;
1526
1527 // when comparing an id<P> on lhs with a static type on rhs,
1528 // see if static class implements all of id's protocols, directly or
1529 // through its super class and categories.
1530 if (rhsID) {
1531 if (ClassImplementsProtocol(lhsProto, rhsID, true))
1532 match = true;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001533 }
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001534 else for (unsigned j = 0; j < numRhsProtocols; j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001535 ObjCProtocolDecl *rhsProto = rhsProtoList[j];
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001536 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1537 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001538 match = true;
1539 break;
1540 }
1541 }
1542 if (!match)
1543 return false;
1544 }
1545 }
1546 else if (rhsQID) {
1547 if (!lhsQID && lhs->getTypeClass() == Type::Pointer) {
1548 QualType ltype =
1549 cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1550 lhsQI =
Ted Kremenek42730c52008-01-07 19:49:32 +00001551 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001552 ltype.getCanonicalType().getTypePtr());
Fariborz Jahanian87829072007-12-20 19:24:10 +00001553 if (!lhsQI) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001554 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanian87829072007-12-20 19:24:10 +00001555 ltype.getCanonicalType().getTypePtr());
1556 if (IT)
1557 lhsID = IT->getDecl();
1558 }
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001559 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001560 if (!lhsQI && !lhsQID && !lhsID)
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001561 return false;
Fariborz Jahanian87829072007-12-20 19:24:10 +00001562
Fariborz Jahaniance5528d2008-01-03 20:01:35 +00001563 unsigned numLhsProtocols = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001564 ObjCProtocolDecl **lhsProtoList = 0;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001565 if (lhsQI) {
1566 numLhsProtocols = lhsQI->getNumProtocols();
1567 lhsProtoList = lhsQI->getReferencedProtocols();
1568 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001569 else if (lhsQID) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001570 numLhsProtocols = lhsQID->getNumProtocols();
1571 lhsProtoList = lhsQID->getReferencedProtocols();
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001572 }
1573 bool match = false;
1574 // for static type vs. qualified 'id' type, check that class implements
1575 // one of 'id's protocols.
1576 if (lhsID) {
1577 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001578 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001579 if (ClassImplementsProtocol(rhsProto, lhsID, compare)) {
1580 match = true;
1581 break;
1582 }
1583 }
1584 }
1585 else for (unsigned i =0; i < numLhsProtocols; i++) {
1586 match = false;
Ted Kremenek42730c52008-01-07 19:49:32 +00001587 ObjCProtocolDecl *lhsProto = lhsProtoList[i];
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001588 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001589 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001590 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1591 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001592 match = true;
1593 break;
1594 }
1595 }
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001596 }
1597 if (!match)
1598 return false;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001599 }
1600 return true;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001601}
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001602
Chris Lattner5003e8b2007-11-01 05:03:41 +00001603bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1604 const VectorType *lVector = lhs->getAsVectorType();
1605 const VectorType *rVector = rhs->getAsVectorType();
1606
1607 if ((lVector->getElementType().getCanonicalType() ==
1608 rVector->getElementType().getCanonicalType()) &&
1609 (lVector->getNumElements() == rVector->getNumElements()))
1610 return true;
1611 return false;
1612}
1613
Steve Naroff85f0dc52007-10-15 20:41:53 +00001614// C99 6.2.7p1: If both are complete types, then the following additional
1615// requirements apply...FIXME (handle compatibility across source files).
1616bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
Steve Naroff4a5e2072007-11-07 06:03:51 +00001617 // "Class" and "id" are compatible built-in structure types.
Ted Kremenek42730c52008-01-07 19:49:32 +00001618 if (isObjCIdType(lhs) && isObjCClassType(rhs) ||
1619 isObjCClassType(lhs) && isObjCIdType(rhs))
Steve Naroff4a5e2072007-11-07 06:03:51 +00001620 return true;
Eli Friedmane7fb03a2008-02-15 06:03:44 +00001621
1622 // Within a translation unit a tag type is
1623 // only compatible with itself.
1624 return lhs.getCanonicalType() == rhs.getCanonicalType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001625}
1626
1627bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1628 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1629 // identically qualified and both shall be pointers to compatible types.
Chris Lattner35fef522008-02-20 20:55:12 +00001630 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1631 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001632 return false;
1633
1634 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1635 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1636
1637 return typesAreCompatible(ltype, rtype);
1638}
1639
Bill Wendling6a9d8542007-12-03 07:33:35 +00001640// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroff85f0dc52007-10-15 20:41:53 +00001641// reference to T, the operation assigns to the object of type T denoted by the
1642// reference.
1643bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1644 QualType ltype = lhs;
1645
1646 if (lhs->isReferenceType())
1647 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1648
1649 QualType rtype = rhs;
1650
1651 if (rhs->isReferenceType())
1652 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1653
1654 return typesAreCompatible(ltype, rtype);
1655}
1656
1657bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1658 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1659 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1660 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1661 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1662
1663 // first check the return types (common between C99 and K&R).
1664 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1665 return false;
1666
1667 if (lproto && rproto) { // two C99 style function prototypes
1668 unsigned lproto_nargs = lproto->getNumArgs();
1669 unsigned rproto_nargs = rproto->getNumArgs();
1670
1671 if (lproto_nargs != rproto_nargs)
1672 return false;
1673
1674 // both prototypes have the same number of arguments.
1675 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1676 (rproto->isVariadic() && !lproto->isVariadic()))
1677 return false;
1678
1679 // The use of ellipsis agree...now check the argument types.
1680 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001681 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1682 // is taken as having the unqualified version of it's declared type.
Steve Naroffdec17fe2008-01-29 00:15:50 +00001683 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001684 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001685 return false;
1686 return true;
1687 }
1688 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1689 return true;
1690
1691 // we have a mixture of K&R style with C99 prototypes
1692 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1693
1694 if (proto->isVariadic())
1695 return false;
1696
1697 // FIXME: Each parameter type T in the prototype must be compatible with the
1698 // type resulting from applying the usual argument conversions to T.
1699 return true;
1700}
1701
1702bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
Eli Friedman1e7537832008-02-06 04:53:22 +00001703 // Compatible arrays must have compatible element types
1704 QualType ltype = lhs->getAsArrayType()->getElementType();
1705 QualType rtype = rhs->getAsArrayType()->getElementType();
1706
Steve Naroff85f0dc52007-10-15 20:41:53 +00001707 if (!typesAreCompatible(ltype, rtype))
1708 return false;
Eli Friedman1e7537832008-02-06 04:53:22 +00001709
1710 // Compatible arrays must be the same size
1711 if (const ConstantArrayType* LCAT = lhs->getAsConstantArrayType())
1712 if (const ConstantArrayType* RCAT = rhs->getAsConstantArrayType())
1713 return RCAT->getSize() == LCAT->getSize();
1714
Steve Naroff85f0dc52007-10-15 20:41:53 +00001715 return true;
1716}
1717
1718/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1719/// both shall have the identically qualified version of a compatible type.
1720/// C99 6.2.7p1: Two types have compatible types if their types are the
1721/// same. See 6.7.[2,3,5] for additional rules.
1722bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
Chris Lattner35fef522008-02-20 20:55:12 +00001723 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1724 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroff577f9722008-01-29 18:58:14 +00001725 return false;
1726
Steve Naroff85f0dc52007-10-15 20:41:53 +00001727 QualType lcanon = lhs.getCanonicalType();
1728 QualType rcanon = rhs.getCanonicalType();
1729
1730 // If two types are identical, they are are compatible
1731 if (lcanon == rcanon)
1732 return true;
Bill Wendling6a9d8542007-12-03 07:33:35 +00001733
1734 // C++ [expr]: If an expression initially has the type "reference to T", the
1735 // type is adjusted to "T" prior to any further analysis, the expression
1736 // designates the object or function denoted by the reference, and the
1737 // expression is an lvalue.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001738 if (ReferenceType *RT = dyn_cast<ReferenceType>(lcanon))
1739 lcanon = RT->getReferenceeType();
1740 if (ReferenceType *RT = dyn_cast<ReferenceType>(rcanon))
1741 rcanon = RT->getReferenceeType();
1742
1743 Type::TypeClass LHSClass = lcanon->getTypeClass();
1744 Type::TypeClass RHSClass = rcanon->getTypeClass();
1745
1746 // We want to consider the two function types to be the same for these
1747 // comparisons, just force one to the other.
1748 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1749 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00001750
1751 // Same as above for arrays
1752 if (LHSClass == Type::VariableArray) LHSClass = Type::ConstantArray;
1753 if (RHSClass == Type::VariableArray) RHSClass = Type::ConstantArray;
Eli Friedman8ff07782008-02-15 18:16:39 +00001754 if (LHSClass == Type::IncompleteArray) LHSClass = Type::ConstantArray;
1755 if (RHSClass == Type::IncompleteArray) RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001756
Steve Naroffc88babe2008-01-09 22:43:08 +00001757 // If the canonical type classes don't match...
Chris Lattnerc38d4522008-01-14 05:45:46 +00001758 if (LHSClass != RHSClass) {
Steve Naroff85f0dc52007-10-15 20:41:53 +00001759 // For Objective-C, it is possible for two types to be compatible
1760 // when their classes don't match (when dealing with "id"). If either type
1761 // is an interface, we defer to objcTypesAreCompatible().
Ted Kremenek42730c52008-01-07 19:49:32 +00001762 if (lcanon->isObjCInterfaceType() || rcanon->isObjCInterfaceType())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001763 return objcTypesAreCompatible(lcanon, rcanon);
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001764
Chris Lattnerc38d4522008-01-14 05:45:46 +00001765 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1766 // a signed integer type, or an unsigned integer type.
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001767 if (lcanon->isEnumeralType() && rcanon->isIntegralType()) {
1768 EnumDecl* EDecl = cast<EnumDecl>(cast<TagType>(lcanon)->getDecl());
1769 return EDecl->getIntegerType() == rcanon;
1770 }
1771 if (rcanon->isEnumeralType() && lcanon->isIntegralType()) {
1772 EnumDecl* EDecl = cast<EnumDecl>(cast<TagType>(rcanon)->getDecl());
1773 return EDecl->getIntegerType() == lcanon;
1774 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001775
Steve Naroff85f0dc52007-10-15 20:41:53 +00001776 return false;
1777 }
Steve Naroffc88babe2008-01-09 22:43:08 +00001778 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001779 switch (LHSClass) {
1780 case Type::FunctionProto: assert(0 && "Canonicalized away above");
1781 case Type::Pointer:
1782 return pointerTypesAreCompatible(lcanon, rcanon);
1783 case Type::ConstantArray:
1784 case Type::VariableArray:
Eli Friedman8ff07782008-02-15 18:16:39 +00001785 case Type::IncompleteArray:
Chris Lattnerc38d4522008-01-14 05:45:46 +00001786 return arrayTypesAreCompatible(lcanon, rcanon);
1787 case Type::FunctionNoProto:
1788 return functionTypesAreCompatible(lcanon, rcanon);
1789 case Type::Tagged: // handle structures, unions
1790 return tagTypesAreCompatible(lcanon, rcanon);
1791 case Type::Builtin:
1792 return builtinTypesAreCompatible(lcanon, rcanon);
1793 case Type::ObjCInterface:
1794 return interfaceTypesAreCompatible(lcanon, rcanon);
1795 case Type::Vector:
1796 case Type::OCUVector:
1797 return vectorTypesAreCompatible(lcanon, rcanon);
1798 case Type::ObjCQualifiedInterface:
1799 return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
1800 default:
1801 assert(0 && "unexpected type");
Steve Naroff85f0dc52007-10-15 20:41:53 +00001802 }
1803 return true; // should never get here...
1804}
Ted Kremenek738e6c02007-10-31 17:10:13 +00001805
Ted Kremenek738e6c02007-10-31 17:10:13 +00001806/// Emit - Serialize an ASTContext object to Bitcode.
1807void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00001808 S.EmitRef(SourceMgr);
1809 S.EmitRef(Target);
1810 S.EmitRef(Idents);
1811 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001812
Ted Kremenek68228a92007-10-31 22:44:07 +00001813 // Emit the size of the type vector so that we can reserve that size
1814 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001815 S.EmitInt(Types.size());
1816
Ted Kremenek034a78c2007-11-13 22:02:55 +00001817 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1818 I!=E;++I)
1819 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001820
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001821 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001822}
1823
Ted Kremenekacba3612007-11-13 00:25:37 +00001824ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek68228a92007-10-31 22:44:07 +00001825 SourceManager &SM = D.ReadRef<SourceManager>();
1826 TargetInfo &t = D.ReadRef<TargetInfo>();
1827 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1828 SelectorTable &sels = D.ReadRef<SelectorTable>();
1829
1830 unsigned size_reserve = D.ReadInt();
1831
1832 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1833
Ted Kremenek034a78c2007-11-13 22:02:55 +00001834 for (unsigned i = 0; i < size_reserve; ++i)
1835 Type::Create(*A,i,D);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001836
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001837 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00001838
1839 return A;
1840}