blob: a636e8d8bbd9e8c54879bab2a98059a401f62aed [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.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000136 if (Target.isCharSigned(FullSourceLoc()))
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>
183ASTContext::getTypeInfo(QualType T, SourceLocation L) {
184 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 Lattner4b009652007-07-25 00:24:17 +0000198 std::pair<uint64_t, unsigned> EltInfo =
Steve Naroff83c13012007-08-30 01:06:46 +0000199 getTypeInfo(CAT->getElementType(), L);
200 Size = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000201 Align = EltInfo.second;
202 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000203 }
204 case Type::OCUVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000205 case Type::Vector: {
206 std::pair<uint64_t, unsigned> EltInfo =
207 getTypeInfo(cast<VectorType>(T)->getElementType(), L);
208 Size = EltInfo.first*cast<VectorType>(T)->getNumElements();
209 // FIXME: Vector alignment is not the alignment of its elements.
210 Align = EltInfo.second;
211 break;
212 }
213
214 case Type::Builtin: {
215 // FIXME: need to use TargetInfo to derive the target specific sizes. This
216 // implementation will suffice for play with vector support.
Chris Lattner858eece2007-09-22 18:29:59 +0000217 const llvm::fltSemantics *F;
Chris Lattner4b009652007-07-25 00:24:17 +0000218 switch (cast<BuiltinType>(T)->getKind()) {
219 default: assert(0 && "Unknown builtin type!");
220 case BuiltinType::Void:
221 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000222 case BuiltinType::Bool:
223 Target.getBoolInfo(Size, Align, getFullLoc(L));
224 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000225 case BuiltinType::Char_S:
226 case BuiltinType::Char_U:
227 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000228 case BuiltinType::SChar:
229 Target.getCharInfo(Size, Align, getFullLoc(L));
230 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000231 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000232 case BuiltinType::Short:
233 Target.getShortInfo(Size, Align, getFullLoc(L));
234 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000235 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000236 case BuiltinType::Int:
237 Target.getIntInfo(Size, Align, getFullLoc(L));
238 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000239 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000240 case BuiltinType::Long:
241 Target.getLongInfo(Size, Align, getFullLoc(L));
242 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000243 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000244 case BuiltinType::LongLong:
245 Target.getLongLongInfo(Size, Align, getFullLoc(L));
246 break;
247 case BuiltinType::Float:
248 Target.getFloatInfo(Size, Align, F, getFullLoc(L));
249 break;
250 case BuiltinType::Double:
251 Target.getDoubleInfo(Size, Align, F, getFullLoc(L));
252 break;
253 case BuiltinType::LongDouble:
254 Target.getLongDoubleInfo(Size, Align, F, getFullLoc(L));
255 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000256 }
257 break;
258 }
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000259 case Type::ASQual:
Chris Lattner35fef522008-02-20 20:55:12 +0000260 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0), L);
Ted Kremenek42730c52008-01-07 19:49:32 +0000261 case Type::ObjCQualifiedId:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000262 Target.getPointerInfo(Size, Align, getFullLoc(L));
263 break;
264 case Type::Pointer:
265 Target.getPointerInfo(Size, Align, getFullLoc(L));
266 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000267 case Type::Reference:
268 // "When applied to a reference or a reference type, the result is the size
269 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000270 // FIXME: This is wrong for struct layout: a reference in a struct has
271 // pointer size.
Chris Lattner4b009652007-07-25 00:24:17 +0000272 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
273
274 case Type::Complex: {
275 // Complex types have the same alignment as their elements, but twice the
276 // size.
277 std::pair<uint64_t, unsigned> EltInfo =
278 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
279 Size = EltInfo.first*2;
280 Align = EltInfo.second;
281 break;
282 }
283 case Type::Tagged:
Chris Lattnereb56d292007-08-27 17:38:00 +0000284 TagType *TT = cast<TagType>(T);
285 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
Devang Patel7a78e432007-11-01 19:11:01 +0000286 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl(), L);
Chris Lattnereb56d292007-08-27 17:38:00 +0000287 Size = Layout.getSize();
288 Align = Layout.getAlignment();
289 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattner90a018d2007-08-28 18:24:31 +0000290 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattnereb56d292007-08-27 17:38:00 +0000291 } else {
Chris Lattner4b009652007-07-25 00:24:17 +0000292 assert(0 && "Unimplemented type sizes!");
Chris Lattnereb56d292007-08-27 17:38:00 +0000293 }
Chris Lattner4b009652007-07-25 00:24:17 +0000294 break;
295 }
296
297 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
298 return std::make_pair(Size, Align);
299}
300
Devang Patel7a78e432007-11-01 19:11:01 +0000301/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000302/// specified record (struct/union/class), which indicates its size and field
303/// position information.
Devang Patel7a78e432007-11-01 19:11:01 +0000304const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D,
305 SourceLocation L) {
Chris Lattner4b009652007-07-25 00:24:17 +0000306 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
307
308 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000309 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000310 if (Entry) return *Entry;
311
Devang Patel7a78e432007-11-01 19:11:01 +0000312 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
313 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
314 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000315 Entry = NewEntry;
316
317 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
318 uint64_t RecordSize = 0;
319 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
320
321 if (D->getKind() != Decl::Union) {
Anders Carlsson7dce0292008-02-16 19:51:27 +0000322 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
323 RecordAlign = std::max(RecordAlign, AA->getAlignment());
324
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000325 bool StructIsPacked = D->getAttr<PackedAttr>();
326
Chris Lattner4b009652007-07-25 00:24:17 +0000327 // Layout each field, for now, just sequentially, respecting alignment. In
328 // the future, this will need to be tweakable by targets.
329 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
330 const FieldDecl *FD = D->getMember(i);
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000331 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
Eli Friedman67571ac2008-02-06 05:33:51 +0000332 uint64_t FieldSize;
333 unsigned FieldAlign;
Anders Carlsson058237f2008-02-18 07:13:09 +0000334
335 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
336 llvm::APSInt I(32);
337 bool BitWidthIsICE =
338 BitWidthExpr->isIntegerConstantExpr(I, *this);
339 assert (BitWidthIsICE && "Invalid BitField size expression");
340 FieldSize = I.getZExtValue();
341
342 std::pair<uint64_t, unsigned> TypeInfo = getTypeInfo(FD->getType(), L);
343 uint64_t TypeSize = TypeInfo.first;
Anders Carlsson7dce0292008-02-16 19:51:27 +0000344
345 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
346 FieldAlign = AA->getAlignment();
347 else if (FieldIsPacked)
348 FieldAlign = 8;
349 else {
Anders Carlsson058237f2008-02-18 07:13:09 +0000350 // FIXME: This is X86 specific, use 32-bit alignment for long long.
351 if (FD->getType()->isIntegerType() && TypeInfo.second > 32)
352 FieldAlign = 32;
353 else
354 FieldAlign = TypeInfo.second;
Anders Carlsson7dce0292008-02-16 19:51:27 +0000355 }
Eli Friedman67571ac2008-02-06 05:33:51 +0000356
Anders Carlsson058237f2008-02-18 07:13:09 +0000357 // Check if we need to add padding to give the field the correct
358 // alignment.
359 if (RecordSize % FieldAlign + FieldSize > TypeSize)
360 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
361
362 } else {
363 if (FD->getType()->isIncompleteType()) {
364 // This must be a flexible array member; we can't directly
365 // query getTypeInfo about these, so we figure it out here.
366 // Flexible array members don't have any size, but they
367 // have to be aligned appropriately for their element type.
368
369 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
370 FieldAlign = AA->getAlignment();
371 else if (FieldIsPacked)
372 FieldAlign = 8;
373 else {
374 const ArrayType* ATy = FD->getType()->getAsArrayType();
375 FieldAlign = getTypeAlign(ATy->getElementType(), L);
376 }
377 FieldSize = 0;
378 } else {
379 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
380 FieldSize = FieldInfo.first;
381
382 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
383 FieldAlign = AA->getAlignment();
384 else if (FieldIsPacked)
385 FieldAlign = 8;
386 else
387 FieldAlign = FieldInfo.second;
388 }
389
390 // Round up the current record size to the field's alignment boundary.
391 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
392 }
Chris Lattner4b009652007-07-25 00:24:17 +0000393
394 // Place this field at the current location.
395 FieldOffsets[i] = RecordSize;
396
397 // Reserve space for this field.
398 RecordSize += FieldSize;
399
400 // Remember max struct/class alignment.
401 RecordAlign = std::max(RecordAlign, FieldAlign);
402 }
403
404 // Finally, round the size of the total struct up to the alignment of the
405 // struct itself.
406 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
407 } else {
408 // Union layout just puts each member at the start of the record.
409 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
410 const FieldDecl *FD = D->getMember(i);
411 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
412 uint64_t FieldSize = FieldInfo.first;
413 unsigned FieldAlign = FieldInfo.second;
414
Anders Carlsson058237f2008-02-18 07:13:09 +0000415 // FIXME: This is X86 specific, use 32-bit alignment for long long.
416 if (FD->getType()->isIntegerType() && FieldAlign > 32)
417 FieldAlign = 32;
418
Chris Lattner4b009652007-07-25 00:24:17 +0000419 // Round up the current record size to the field's alignment boundary.
420 RecordSize = std::max(RecordSize, FieldSize);
421
422 // Place this field at the start of the record.
423 FieldOffsets[i] = 0;
424
425 // Remember max struct/class alignment.
426 RecordAlign = std::max(RecordAlign, FieldAlign);
427 }
428 }
429
430 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
431 return *NewEntry;
432}
433
Chris Lattner4b009652007-07-25 00:24:17 +0000434//===----------------------------------------------------------------------===//
435// Type creation/memoization methods
436//===----------------------------------------------------------------------===//
437
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000438QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattner35fef522008-02-20 20:55:12 +0000439 if (T.getCanonicalType().getAddressSpace() == AddressSpace)
440 return T;
441
442 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
443 // with CVR qualifiers from here on out.
444 assert(T.getCanonicalType().getAddressSpace() == 0 &&
445 "Type is already address space qualified");
446
447 // Check if we've already instantiated an address space qual'd type of this
448 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000449 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000450 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000451 void *InsertPos = 0;
452 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
453 return QualType(ASQy, 0);
454
455 // If the base type isn't canonical, this won't be a canonical type either,
456 // so fill in the canonical type field.
457 QualType Canonical;
458 if (!T->isCanonical()) {
459 Canonical = getASQualType(T.getCanonicalType(), AddressSpace);
460
461 // Get the new insert position for the node we care about.
462 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
463 assert(NewIP == 0 && "Shouldn't be in the map!");
464 }
Chris Lattner35fef522008-02-20 20:55:12 +0000465 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000466 ASQualTypes.InsertNode(New, InsertPos);
467 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000468 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000469}
470
Chris Lattner4b009652007-07-25 00:24:17 +0000471
472/// getComplexType - Return the uniqued reference to the type for a complex
473/// number with the specified element type.
474QualType ASTContext::getComplexType(QualType T) {
475 // Unique pointers, to guarantee there is only one pointer of a particular
476 // structure.
477 llvm::FoldingSetNodeID ID;
478 ComplexType::Profile(ID, T);
479
480 void *InsertPos = 0;
481 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
482 return QualType(CT, 0);
483
484 // If the pointee type isn't canonical, this won't be a canonical type either,
485 // so fill in the canonical type field.
486 QualType Canonical;
487 if (!T->isCanonical()) {
488 Canonical = getComplexType(T.getCanonicalType());
489
490 // Get the new insert position for the node we care about.
491 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
492 assert(NewIP == 0 && "Shouldn't be in the map!");
493 }
494 ComplexType *New = new ComplexType(T, Canonical);
495 Types.push_back(New);
496 ComplexTypes.InsertNode(New, InsertPos);
497 return QualType(New, 0);
498}
499
500
501/// getPointerType - Return the uniqued reference to the type for a pointer to
502/// the specified type.
503QualType ASTContext::getPointerType(QualType T) {
504 // Unique pointers, to guarantee there is only one pointer of a particular
505 // structure.
506 llvm::FoldingSetNodeID ID;
507 PointerType::Profile(ID, T);
508
509 void *InsertPos = 0;
510 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
511 return QualType(PT, 0);
512
513 // If the pointee type isn't canonical, this won't be a canonical type either,
514 // so fill in the canonical type field.
515 QualType Canonical;
516 if (!T->isCanonical()) {
517 Canonical = getPointerType(T.getCanonicalType());
518
519 // Get the new insert position for the node we care about.
520 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
521 assert(NewIP == 0 && "Shouldn't be in the map!");
522 }
523 PointerType *New = new PointerType(T, Canonical);
524 Types.push_back(New);
525 PointerTypes.InsertNode(New, InsertPos);
526 return QualType(New, 0);
527}
528
529/// getReferenceType - Return the uniqued reference to the type for a reference
530/// to the specified type.
531QualType ASTContext::getReferenceType(QualType T) {
532 // Unique pointers, to guarantee there is only one pointer of a particular
533 // structure.
534 llvm::FoldingSetNodeID ID;
535 ReferenceType::Profile(ID, T);
536
537 void *InsertPos = 0;
538 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
539 return QualType(RT, 0);
540
541 // If the referencee type isn't canonical, this won't be a canonical type
542 // either, so fill in the canonical type field.
543 QualType Canonical;
544 if (!T->isCanonical()) {
545 Canonical = getReferenceType(T.getCanonicalType());
546
547 // Get the new insert position for the node we care about.
548 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
549 assert(NewIP == 0 && "Shouldn't be in the map!");
550 }
551
552 ReferenceType *New = new ReferenceType(T, Canonical);
553 Types.push_back(New);
554 ReferenceTypes.InsertNode(New, InsertPos);
555 return QualType(New, 0);
556}
557
Steve Naroff83c13012007-08-30 01:06:46 +0000558/// getConstantArrayType - Return the unique reference to the type for an
559/// array of the specified element type.
560QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000561 const llvm::APInt &ArySize,
562 ArrayType::ArraySizeModifier ASM,
563 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000564 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000565 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000566
567 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000568 if (ConstantArrayType *ATP =
569 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000570 return QualType(ATP, 0);
571
572 // If the element type isn't canonical, this won't be a canonical type either,
573 // so fill in the canonical type field.
574 QualType Canonical;
575 if (!EltTy->isCanonical()) {
Steve Naroff24c9b982007-08-30 18:10:14 +0000576 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
577 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000578 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000579 ConstantArrayType *NewIP =
580 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
581
Chris Lattner4b009652007-07-25 00:24:17 +0000582 assert(NewIP == 0 && "Shouldn't be in the map!");
583 }
584
Steve Naroff24c9b982007-08-30 18:10:14 +0000585 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
586 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000587 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000588 Types.push_back(New);
589 return QualType(New, 0);
590}
591
Steve Naroffe2579e32007-08-30 18:14:25 +0000592/// getVariableArrayType - Returns a non-unique reference to the type for a
593/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000594QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
595 ArrayType::ArraySizeModifier ASM,
596 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000597 // Since we don't unique expressions, it isn't possible to unique VLA's
598 // that have an expression provided for their size.
599
600 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
601 ASM, EltTypeQuals);
602
603 VariableArrayTypes.push_back(New);
604 Types.push_back(New);
605 return QualType(New, 0);
606}
607
608QualType ASTContext::getIncompleteArrayType(QualType EltTy,
609 ArrayType::ArraySizeModifier ASM,
610 unsigned EltTypeQuals) {
611 llvm::FoldingSetNodeID ID;
612 IncompleteArrayType::Profile(ID, EltTy);
613
614 void *InsertPos = 0;
615 if (IncompleteArrayType *ATP =
616 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
617 return QualType(ATP, 0);
618
619 // If the element type isn't canonical, this won't be a canonical type
620 // either, so fill in the canonical type field.
621 QualType Canonical;
622
623 if (!EltTy->isCanonical()) {
624 Canonical = getIncompleteArrayType(EltTy.getCanonicalType(),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000625 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000626
627 // Get the new insert position for the node we care about.
628 IncompleteArrayType *NewIP =
629 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
630
631 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000632 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000633
634 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
635 ASM, EltTypeQuals);
636
637 IncompleteArrayTypes.InsertNode(New, InsertPos);
638 Types.push_back(New);
639 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000640}
641
Chris Lattner4b009652007-07-25 00:24:17 +0000642/// getVectorType - Return the unique reference to a vector type of
643/// the specified element type and size. VectorType must be a built-in type.
644QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
645 BuiltinType *baseType;
646
647 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
648 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
649
650 // Check if we've already instantiated a vector of this type.
651 llvm::FoldingSetNodeID ID;
652 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
653 void *InsertPos = 0;
654 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
655 return QualType(VTP, 0);
656
657 // If the element type isn't canonical, this won't be a canonical type either,
658 // so fill in the canonical type field.
659 QualType Canonical;
660 if (!vecType->isCanonical()) {
661 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
662
663 // Get the new insert position for the node we care about.
664 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
665 assert(NewIP == 0 && "Shouldn't be in the map!");
666 }
667 VectorType *New = new VectorType(vecType, NumElts, Canonical);
668 VectorTypes.InsertNode(New, InsertPos);
669 Types.push_back(New);
670 return QualType(New, 0);
671}
672
673/// getOCUVectorType - Return the unique reference to an OCU vector type of
674/// the specified element type and size. VectorType must be a built-in type.
675QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
676 BuiltinType *baseType;
677
678 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
679 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
680
681 // Check if we've already instantiated a vector of this type.
682 llvm::FoldingSetNodeID ID;
683 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
684 void *InsertPos = 0;
685 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
686 return QualType(VTP, 0);
687
688 // If the element type isn't canonical, this won't be a canonical type either,
689 // so fill in the canonical type field.
690 QualType Canonical;
691 if (!vecType->isCanonical()) {
692 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
693
694 // Get the new insert position for the node we care about.
695 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
696 assert(NewIP == 0 && "Shouldn't be in the map!");
697 }
698 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
699 VectorTypes.InsertNode(New, InsertPos);
700 Types.push_back(New);
701 return QualType(New, 0);
702}
703
704/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
705///
706QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
707 // Unique functions, to guarantee there is only one function of a particular
708 // structure.
709 llvm::FoldingSetNodeID ID;
710 FunctionTypeNoProto::Profile(ID, ResultTy);
711
712 void *InsertPos = 0;
713 if (FunctionTypeNoProto *FT =
714 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
715 return QualType(FT, 0);
716
717 QualType Canonical;
718 if (!ResultTy->isCanonical()) {
719 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
720
721 // Get the new insert position for the node we care about.
722 FunctionTypeNoProto *NewIP =
723 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
724 assert(NewIP == 0 && "Shouldn't be in the map!");
725 }
726
727 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
728 Types.push_back(New);
729 FunctionTypeProtos.InsertNode(New, InsertPos);
730 return QualType(New, 0);
731}
732
733/// getFunctionType - Return a normal function type with a typed argument
734/// list. isVariadic indicates whether the argument list includes '...'.
735QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
736 unsigned NumArgs, bool isVariadic) {
737 // Unique functions, to guarantee there is only one function of a particular
738 // structure.
739 llvm::FoldingSetNodeID ID;
740 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
741
742 void *InsertPos = 0;
743 if (FunctionTypeProto *FTP =
744 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
745 return QualType(FTP, 0);
746
747 // Determine whether the type being created is already canonical or not.
748 bool isCanonical = ResultTy->isCanonical();
749 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
750 if (!ArgArray[i]->isCanonical())
751 isCanonical = false;
752
753 // If this type isn't canonical, get the canonical version of it.
754 QualType Canonical;
755 if (!isCanonical) {
756 llvm::SmallVector<QualType, 16> CanonicalArgs;
757 CanonicalArgs.reserve(NumArgs);
758 for (unsigned i = 0; i != NumArgs; ++i)
759 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
760
761 Canonical = getFunctionType(ResultTy.getCanonicalType(),
762 &CanonicalArgs[0], NumArgs,
763 isVariadic);
764
765 // Get the new insert position for the node we care about.
766 FunctionTypeProto *NewIP =
767 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
768 assert(NewIP == 0 && "Shouldn't be in the map!");
769 }
770
771 // FunctionTypeProto objects are not allocated with new because they have a
772 // variable size array (for parameter types) at the end of them.
773 FunctionTypeProto *FTP =
774 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
775 NumArgs*sizeof(QualType));
776 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
777 Canonical);
778 Types.push_back(FTP);
779 FunctionTypeProtos.InsertNode(FTP, InsertPos);
780 return QualType(FTP, 0);
781}
782
783/// getTypedefType - Return the unique reference to the type for the
784/// specified typename decl.
785QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
786 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
787
788 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000789 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000790 Types.push_back(Decl->TypeForDecl);
791 return QualType(Decl->TypeForDecl, 0);
792}
793
Ted Kremenek42730c52008-01-07 19:49:32 +0000794/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000795/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000796QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000797 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
798
Ted Kremenek42730c52008-01-07 19:49:32 +0000799 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000800 Types.push_back(Decl->TypeForDecl);
801 return QualType(Decl->TypeForDecl, 0);
802}
803
Ted Kremenek42730c52008-01-07 19:49:32 +0000804/// getObjCQualifiedInterfaceType - Return a
805/// ObjCQualifiedInterfaceType type for the given interface decl and
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000806/// the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000807QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
808 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000809 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000810 ObjCQualifiedInterfaceType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000811
812 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000813 if (ObjCQualifiedInterfaceType *QT =
814 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000815 return QualType(QT, 0);
816
817 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +0000818 ObjCQualifiedInterfaceType *QType =
819 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000820 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000821 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000822 return QualType(QType, 0);
823}
824
Ted Kremenek42730c52008-01-07 19:49:32 +0000825/// getObjCQualifiedIdType - Return a
826/// getObjCQualifiedIdType type for the 'id' decl and
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000827/// the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000828QualType ASTContext::getObjCQualifiedIdType(QualType idType,
829 ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000830 unsigned NumProtocols) {
831 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000832 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000833
834 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000835 if (ObjCQualifiedIdType *QT =
836 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000837 return QualType(QT, 0);
838
839 // No Match;
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000840 QualType Canonical;
841 if (!idType->isCanonical()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000842 Canonical = getObjCQualifiedIdType(idType.getCanonicalType(),
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000843 Protocols, NumProtocols);
Ted Kremenek42730c52008-01-07 19:49:32 +0000844 ObjCQualifiedIdType *NewQT =
845 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000846 assert(NewQT == 0 && "Shouldn't be in the map!");
847 }
848
Ted Kremenek42730c52008-01-07 19:49:32 +0000849 ObjCQualifiedIdType *QType =
850 new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000851 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000852 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000853 return QualType(QType, 0);
854}
855
Steve Naroff0604dd92007-08-01 18:02:17 +0000856/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
857/// TypeOfExpr AST's (since expression's are never shared). For example,
858/// multiple declarations that refer to "typeof(x)" all contain different
859/// DeclRefExpr's. This doesn't effect the type checker, since it operates
860/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000861QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroff7cbb1462007-07-31 12:34:36 +0000862 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000863 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
864 Types.push_back(toe);
865 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000866}
867
Steve Naroff0604dd92007-08-01 18:02:17 +0000868/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
869/// TypeOfType AST's. The only motivation to unique these nodes would be
870/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
871/// an issue. This doesn't effect the type checker, since it operates
872/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000873QualType ASTContext::getTypeOfType(QualType tofType) {
874 QualType Canonical = tofType.getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000875 TypeOfType *tot = new TypeOfType(tofType, Canonical);
876 Types.push_back(tot);
877 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000878}
879
Chris Lattner4b009652007-07-25 00:24:17 +0000880/// getTagDeclType - Return the unique reference to the type for the
881/// specified TagDecl (struct/union/class/enum) decl.
882QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +0000883 assert (Decl);
884
Ted Kremenekf05026d2007-11-14 00:03:20 +0000885 // The decl stores the type cache.
Ted Kremenekae8fa032007-11-26 21:16:01 +0000886 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Ted Kremenekf05026d2007-11-14 00:03:20 +0000887
888 TagType* T = new TagType(Decl, QualType());
Ted Kremenekae8fa032007-11-26 21:16:01 +0000889 Types.push_back(T);
890 Decl->TypeForDecl = T;
Ted Kremenekf05026d2007-11-14 00:03:20 +0000891
892 return QualType(T, 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000893}
894
895/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
896/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
897/// needs to agree with the definition in <stddef.h>.
898QualType ASTContext::getSizeType() const {
899 // On Darwin, size_t is defined as a "long unsigned int".
900 // FIXME: should derive from "Target".
901 return UnsignedLongTy;
902}
903
Eli Friedmanfdd35d72008-02-12 08:29:21 +0000904/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
905/// width of characters in wide strings, The value is target dependent and
906/// needs to agree with the definition in <stddef.h>.
907QualType ASTContext::getWcharType() const {
908 // On Darwin, wchar_t is defined as a "int".
909 // FIXME: should derive from "Target".
910 return IntTy;
911}
912
Chris Lattner4b009652007-07-25 00:24:17 +0000913/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
914/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
915QualType ASTContext::getPointerDiffType() const {
916 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
917 // FIXME: should derive from "Target".
918 return IntTy;
919}
920
921/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
922/// routine will assert if passed a built-in type that isn't an integer or enum.
923static int getIntegerRank(QualType t) {
924 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
925 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
926 return 4;
927 }
928
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000929 const BuiltinType *BT = t.getCanonicalType()->getAsBuiltinType();
Chris Lattner4b009652007-07-25 00:24:17 +0000930 switch (BT->getKind()) {
931 default:
932 assert(0 && "getIntegerRank(): not a built-in integer");
933 case BuiltinType::Bool:
934 return 1;
935 case BuiltinType::Char_S:
936 case BuiltinType::Char_U:
937 case BuiltinType::SChar:
938 case BuiltinType::UChar:
939 return 2;
940 case BuiltinType::Short:
941 case BuiltinType::UShort:
942 return 3;
943 case BuiltinType::Int:
944 case BuiltinType::UInt:
945 return 4;
946 case BuiltinType::Long:
947 case BuiltinType::ULong:
948 return 5;
949 case BuiltinType::LongLong:
950 case BuiltinType::ULongLong:
951 return 6;
952 }
953}
954
955/// getFloatingRank - Return a relative rank for floating point types.
956/// This routine will assert if passed a built-in type that isn't a float.
957static int getFloatingRank(QualType T) {
958 T = T.getCanonicalType();
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000959 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +0000960 return getFloatingRank(CT->getElementType());
961
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000962 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattner5003e8b2007-11-01 05:03:41 +0000963 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +0000964 case BuiltinType::Float: return FloatRank;
965 case BuiltinType::Double: return DoubleRank;
966 case BuiltinType::LongDouble: return LongDoubleRank;
967 }
968}
969
Steve Narofffa0c4532007-08-27 01:41:48 +0000970/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
971/// point or a complex type (based on typeDomain/typeSize).
972/// 'typeDomain' is a real floating point or complex type.
973/// 'typeSize' is a real floating point or complex type.
Steve Naroff3cf497f2007-08-27 01:27:54 +0000974QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
975 QualType typeSize, QualType typeDomain) const {
976 if (typeDomain->isComplexType()) {
977 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000978 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000979 case FloatRank: return FloatComplexTy;
980 case DoubleRank: return DoubleComplexTy;
981 case LongDoubleRank: return LongDoubleComplexTy;
982 }
Chris Lattner4b009652007-07-25 00:24:17 +0000983 }
Steve Naroff3cf497f2007-08-27 01:27:54 +0000984 if (typeDomain->isRealFloatingType()) {
985 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000986 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000987 case FloatRank: return FloatTy;
988 case DoubleRank: return DoubleTy;
989 case LongDoubleRank: return LongDoubleTy;
990 }
991 }
992 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattner1d2b4612007-09-16 19:23:47 +0000993 //an invalid return value, but the assert
994 //will ensure that this code is never reached.
995 return VoidTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000996}
997
Steve Naroff45fc9822007-08-27 15:30:22 +0000998/// compareFloatingType - Handles 3 different combos:
999/// float/float, float/complex, complex/complex.
1000/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
1001int ASTContext::compareFloatingType(QualType lt, QualType rt) {
1002 if (getFloatingRank(lt) == getFloatingRank(rt))
1003 return 0;
1004 if (getFloatingRank(lt) > getFloatingRank(rt))
1005 return 1;
1006 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001007}
1008
1009// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
1010// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
1011QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
1012 if (lhs == rhs) return lhs;
1013
1014 bool t1Unsigned = lhs->isUnsignedIntegerType();
1015 bool t2Unsigned = rhs->isUnsignedIntegerType();
1016
1017 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
1018 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
1019
1020 // We have two integer types with differing signs
1021 QualType unsignedType = t1Unsigned ? lhs : rhs;
1022 QualType signedType = t1Unsigned ? rhs : lhs;
1023
1024 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
1025 return unsignedType;
1026 else {
1027 // FIXME: Need to check if the signed type can represent all values of the
1028 // unsigned type. If it can, then the result is the signed type.
1029 // If it can't, then the result is the unsigned version of the signed type.
1030 // Should probably add a helper that returns a signed integer type from
1031 // an unsigned (and vice versa). C99 6.3.1.8.
1032 return signedType;
1033 }
1034}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001035
1036// getCFConstantStringType - Return the type used for constant CFStrings.
1037QualType ASTContext::getCFConstantStringType() {
1038 if (!CFConstantStringTypeDecl) {
1039 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
Steve Naroff0add5d22007-11-03 11:27:19 +00001040 &Idents.get("NSConstantString"),
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001041 0);
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001042 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001043
1044 // const int *isa;
1045 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001046 // int flags;
1047 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001048 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001049 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001050 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001051 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001052 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001053 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001054
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001055 for (unsigned i = 0; i < 4; ++i)
Steve Naroffdc1ad762007-09-14 02:20:46 +00001056 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001057
1058 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1059 }
1060
1061 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001062}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001063
Anders Carlssone3f02572007-10-29 06:33:42 +00001064// This returns true if a type has been typedefed to BOOL:
1065// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001066static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001067 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001068 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001069
1070 return false;
1071}
1072
Ted Kremenek42730c52008-01-07 19:49:32 +00001073/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001074/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001075int ASTContext::getObjCEncodingTypeSize(QualType type) {
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001076 SourceLocation Loc;
1077 uint64_t sz = getTypeSize(type, Loc);
1078
1079 // Make all integer and enum types at least as large as an int
1080 if (sz > 0 && type->isIntegralType())
1081 sz = std::max(sz, getTypeSize(IntTy, Loc));
1082 // Treat arrays as pointers, since that's how they're passed in.
1083 else if (type->isArrayType())
1084 sz = getTypeSize(VoidPtrTy, Loc);
1085 return sz / getTypeSize(CharTy, Loc);
1086}
1087
Ted Kremenek42730c52008-01-07 19:49:32 +00001088/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001089/// declaration.
Ted Kremenek42730c52008-01-07 19:49:32 +00001090void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001091 std::string& S)
1092{
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001093 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001094 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001095 // Encode result type.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001096 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001097 // Compute size of all parameters.
1098 // Start with computing size of a pointer in number of bytes.
1099 // FIXME: There might(should) be a better way of doing this computation!
1100 SourceLocation Loc;
1101 int PtrSize = getTypeSize(VoidPtrTy, Loc) / getTypeSize(CharTy, Loc);
1102 // The first two arguments (self and _cmd) are pointers; account for
1103 // their size.
1104 int ParmOffset = 2 * PtrSize;
1105 int NumOfParams = Decl->getNumParams();
1106 for (int i = 0; i < NumOfParams; i++) {
1107 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001108 int sz = getObjCEncodingTypeSize (PType);
1109 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001110 ParmOffset += sz;
1111 }
1112 S += llvm::utostr(ParmOffset);
1113 S += "@0:";
1114 S += llvm::utostr(PtrSize);
1115
1116 // Argument types.
1117 ParmOffset = 2 * PtrSize;
1118 for (int i = 0; i < NumOfParams; i++) {
1119 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001120 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001121 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001122 getObjCEncodingForTypeQualifier(
1123 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian248db262008-01-22 22:44:46 +00001124 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001125 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001126 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001127 }
1128}
1129
Fariborz Jahanian248db262008-01-22 22:44:46 +00001130void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1131 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson36f07d82007-10-29 05:01:08 +00001132{
Anders Carlssone3f02572007-10-29 06:33:42 +00001133 // FIXME: This currently doesn't encode:
1134 // @ An object (whether statically typed or typed id)
1135 // # A class object (Class)
1136 // : A method selector (SEL)
1137 // {name=type...} A structure
1138 // (name=type...) A union
1139 // bnum A bit field of num bits
1140
1141 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001142 char encoding;
1143 switch (BT->getKind()) {
1144 case BuiltinType::Void:
1145 encoding = 'v';
1146 break;
1147 case BuiltinType::Bool:
1148 encoding = 'B';
1149 break;
1150 case BuiltinType::Char_U:
1151 case BuiltinType::UChar:
1152 encoding = 'C';
1153 break;
1154 case BuiltinType::UShort:
1155 encoding = 'S';
1156 break;
1157 case BuiltinType::UInt:
1158 encoding = 'I';
1159 break;
1160 case BuiltinType::ULong:
1161 encoding = 'L';
1162 break;
1163 case BuiltinType::ULongLong:
1164 encoding = 'Q';
1165 break;
1166 case BuiltinType::Char_S:
1167 case BuiltinType::SChar:
1168 encoding = 'c';
1169 break;
1170 case BuiltinType::Short:
1171 encoding = 's';
1172 break;
1173 case BuiltinType::Int:
1174 encoding = 'i';
1175 break;
1176 case BuiltinType::Long:
1177 encoding = 'l';
1178 break;
1179 case BuiltinType::LongLong:
1180 encoding = 'q';
1181 break;
1182 case BuiltinType::Float:
1183 encoding = 'f';
1184 break;
1185 case BuiltinType::Double:
1186 encoding = 'd';
1187 break;
1188 case BuiltinType::LongDouble:
1189 encoding = 'd';
1190 break;
1191 default:
1192 assert(0 && "Unhandled builtin type kind");
1193 }
1194
1195 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001196 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001197 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001198 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001199 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001200
1201 }
1202 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001203 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001204 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001205 S += '@';
1206 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001207 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001208 S += '#';
1209 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001210 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001211 S += ':';
1212 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001213 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001214
1215 if (PointeeTy->isCharType()) {
1216 // char pointer types should be encoded as '*' unless it is a
1217 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001218 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001219 S += '*';
1220 return;
1221 }
1222 }
1223
1224 S += '^';
Fariborz Jahanian248db262008-01-22 22:44:46 +00001225 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone3f02572007-10-29 06:33:42 +00001226 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001227 S += '[';
1228
1229 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1230 S += llvm::utostr(CAT->getSize().getZExtValue());
1231 else
1232 assert(0 && "Unhandled array type!");
1233
Fariborz Jahanian248db262008-01-22 22:44:46 +00001234 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001235 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001236 } else if (T->getAsFunctionType()) {
1237 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001238 } else if (const RecordType *RTy = T->getAsRecordType()) {
1239 RecordDecl *RDecl= RTy->getDecl();
1240 S += '{';
1241 S += RDecl->getName();
Fariborz Jahanian248db262008-01-22 22:44:46 +00001242 bool found = false;
1243 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1244 if (ERType[i] == RTy) {
1245 found = true;
1246 break;
1247 }
1248 if (!found) {
1249 ERType.push_back(RTy);
1250 S += '=';
1251 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1252 FieldDecl *field = RDecl->getMember(i);
1253 getObjCEncodingForType(field->getType(), S, ERType);
1254 }
1255 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1256 ERType.pop_back();
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001257 }
1258 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001259 } else if (T->isEnumeralType()) {
1260 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001261 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001262 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001263}
1264
Ted Kremenek42730c52008-01-07 19:49:32 +00001265void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001266 std::string& S) const {
1267 if (QT & Decl::OBJC_TQ_In)
1268 S += 'n';
1269 if (QT & Decl::OBJC_TQ_Inout)
1270 S += 'N';
1271 if (QT & Decl::OBJC_TQ_Out)
1272 S += 'o';
1273 if (QT & Decl::OBJC_TQ_Bycopy)
1274 S += 'O';
1275 if (QT & Decl::OBJC_TQ_Byref)
1276 S += 'R';
1277 if (QT & Decl::OBJC_TQ_Oneway)
1278 S += 'V';
1279}
1280
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001281void ASTContext::setBuiltinVaListType(QualType T)
1282{
1283 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1284
1285 BuiltinVaListType = T;
1286}
1287
Ted Kremenek42730c52008-01-07 19:49:32 +00001288void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001289{
Ted Kremenek42730c52008-01-07 19:49:32 +00001290 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff9d12c902007-10-15 14:41:52 +00001291
Ted Kremenek42730c52008-01-07 19:49:32 +00001292 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001293
1294 // typedef struct objc_object *id;
1295 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1296 assert(ptr && "'id' incorrectly typed");
1297 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1298 assert(rec && "'id' incorrectly typed");
1299 IdStructType = rec;
1300}
1301
Ted Kremenek42730c52008-01-07 19:49:32 +00001302void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001303{
Ted Kremenek42730c52008-01-07 19:49:32 +00001304 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001305
Ted Kremenek42730c52008-01-07 19:49:32 +00001306 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001307
1308 // typedef struct objc_selector *SEL;
1309 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1310 assert(ptr && "'SEL' incorrectly typed");
1311 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1312 assert(rec && "'SEL' incorrectly typed");
1313 SelStructType = rec;
1314}
1315
Ted Kremenek42730c52008-01-07 19:49:32 +00001316void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001317{
Ted Kremenek42730c52008-01-07 19:49:32 +00001318 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1319 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001320}
1321
Ted Kremenek42730c52008-01-07 19:49:32 +00001322void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001323{
Ted Kremenek42730c52008-01-07 19:49:32 +00001324 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001325
Ted Kremenek42730c52008-01-07 19:49:32 +00001326 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001327
1328 // typedef struct objc_class *Class;
1329 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1330 assert(ptr && "'Class' incorrectly typed");
1331 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1332 assert(rec && "'Class' incorrectly typed");
1333 ClassStructType = rec;
1334}
1335
Ted Kremenek42730c52008-01-07 19:49:32 +00001336void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1337 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001338 "'NSConstantString' type already set!");
1339
Ted Kremenek42730c52008-01-07 19:49:32 +00001340 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001341}
1342
Steve Naroff85f0dc52007-10-15 20:41:53 +00001343bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1344 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1345 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1346
1347 return lBuiltin->getKind() == rBuiltin->getKind();
1348}
1349
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001350/// objcTypesAreCompatible - This routine is called when two types
1351/// are of different class; one is interface type or is
1352/// a qualified interface type and the other type is of a different class.
1353/// Example, II or II<P>.
Steve Naroff85f0dc52007-10-15 20:41:53 +00001354bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001355 if (lhs->isObjCInterfaceType() && isObjCIdType(rhs))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001356 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001357 else if (isObjCIdType(lhs) && rhs->isObjCInterfaceType())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001358 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001359 if (ObjCInterfaceType *lhsIT =
1360 dyn_cast<ObjCInterfaceType>(lhs.getCanonicalType().getTypePtr())) {
1361 ObjCQualifiedInterfaceType *rhsQI =
1362 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001363 return rhsQI && (lhsIT->getDecl() == rhsQI->getDecl());
1364 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001365 else if (ObjCInterfaceType *rhsIT =
1366 dyn_cast<ObjCInterfaceType>(rhs.getCanonicalType().getTypePtr())) {
1367 ObjCQualifiedInterfaceType *lhsQI =
1368 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001369 return lhsQI && (rhsIT->getDecl() == lhsQI->getDecl());
1370 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00001371 return false;
1372}
1373
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001374/// Check that 'lhs' and 'rhs' are compatible interface types. Both types
1375/// must be canonical types.
Steve Naroff85f0dc52007-10-15 20:41:53 +00001376bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001377 assert (lhs->isCanonical() &&
1378 "interfaceTypesAreCompatible strip typedefs of lhs");
1379 assert (rhs->isCanonical() &&
1380 "interfaceTypesAreCompatible strip typedefs of rhs");
Fariborz Jahaniance2de812007-12-20 22:37:58 +00001381 if (lhs == rhs)
1382 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001383 ObjCInterfaceType *lhsIT = cast<ObjCInterfaceType>(lhs.getTypePtr());
1384 ObjCInterfaceType *rhsIT = cast<ObjCInterfaceType>(rhs.getTypePtr());
1385 ObjCInterfaceDecl *rhsIDecl = rhsIT->getDecl();
1386 ObjCInterfaceDecl *lhsIDecl = lhsIT->getDecl();
Fariborz Jahaniance2de812007-12-20 22:37:58 +00001387 // rhs is derived from lhs it is OK; else it is not OK.
1388 while (rhsIDecl != NULL) {
1389 if (rhsIDecl == lhsIDecl)
1390 return true;
1391 rhsIDecl = rhsIDecl->getSuperClass();
1392 }
1393 return false;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001394}
1395
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001396bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
1397 QualType rhs) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001398 ObjCQualifiedInterfaceType *lhsQI =
1399 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001400 assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
Ted Kremenek42730c52008-01-07 19:49:32 +00001401 ObjCQualifiedInterfaceType *rhsQI =
1402 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001403 assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001404 if (!interfaceTypesAreCompatible(
1405 getObjCInterfaceType(lhsQI->getDecl()).getCanonicalType(),
1406 getObjCInterfaceType(rhsQI->getDecl()).getCanonicalType()))
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001407 return false;
1408 /* All protocols in lhs must have a presense in rhs. */
1409 for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
1410 bool match = false;
Ted Kremenek42730c52008-01-07 19:49:32 +00001411 ObjCProtocolDecl *lhsProto = lhsQI->getProtocols(i);
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001412 for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001413 ObjCProtocolDecl *rhsProto = rhsQI->getProtocols(j);
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001414 if (lhsProto == rhsProto) {
1415 match = true;
1416 break;
1417 }
1418 }
1419 if (!match)
1420 return false;
1421 }
1422 return true;
1423}
1424
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001425/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
1426/// inheritance hierarchy of 'rProto'.
Ted Kremenek42730c52008-01-07 19:49:32 +00001427static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
1428 ObjCProtocolDecl *rProto) {
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001429 if (lProto == rProto)
1430 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001431 ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001432 for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
1433 if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
1434 return true;
1435 return false;
1436}
1437
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001438/// ClassImplementsProtocol - Checks that 'lProto' protocol
1439/// has been implemented in IDecl class, its super class or categories (if
1440/// lookupCategory is true).
Ted Kremenek42730c52008-01-07 19:49:32 +00001441static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1442 ObjCInterfaceDecl *IDecl,
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001443 bool lookupCategory) {
1444
1445 // 1st, look up the class.
Ted Kremenek42730c52008-01-07 19:49:32 +00001446 ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols();
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001447 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
1448 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1449 return true;
1450 }
1451
1452 // 2nd, look up the category.
1453 if (lookupCategory)
Ted Kremenek42730c52008-01-07 19:49:32 +00001454 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001455 CDecl = CDecl->getNextClassCategory()) {
1456 protoList = CDecl->getReferencedProtocols();
1457 for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
1458 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1459 return true;
1460 }
1461 }
1462
1463 // 3rd, look up the super class(s)
1464 if (IDecl->getSuperClass())
1465 return
1466 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory);
1467
1468 return false;
1469}
1470
Ted Kremenek42730c52008-01-07 19:49:32 +00001471/// ObjCQualifiedIdTypesAreCompatible - Compares two types, at least
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001472/// one of which is a protocol qualified 'id' type. When 'compare'
1473/// is true it is for comparison; when false, for assignment/initialization.
Ted Kremenek42730c52008-01-07 19:49:32 +00001474bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs,
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001475 QualType rhs,
1476 bool compare) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001477 // match id<P..> with an 'id' type in all cases.
1478 if (const PointerType *PT = lhs->getAsPointerType()) {
1479 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001480 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001481 return true;
1482
1483 }
1484 else if (const PointerType *PT = rhs->getAsPointerType()) {
1485 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001486 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001487 return true;
1488
1489 }
1490
Ted Kremenek42730c52008-01-07 19:49:32 +00001491 ObjCQualifiedInterfaceType *lhsQI = 0;
1492 ObjCQualifiedInterfaceType *rhsQI = 0;
1493 ObjCInterfaceDecl *lhsID = 0;
1494 ObjCInterfaceDecl *rhsID = 0;
1495 ObjCQualifiedIdType *lhsQID = dyn_cast<ObjCQualifiedIdType>(lhs);
1496 ObjCQualifiedIdType *rhsQID = dyn_cast<ObjCQualifiedIdType>(rhs);
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001497
1498 if (lhsQID) {
1499 if (!rhsQID && rhs->getTypeClass() == Type::Pointer) {
1500 QualType rtype =
1501 cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1502 rhsQI =
Ted Kremenek42730c52008-01-07 19:49:32 +00001503 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001504 rtype.getCanonicalType().getTypePtr());
Fariborz Jahanian87829072007-12-20 19:24:10 +00001505 if (!rhsQI) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001506 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanian87829072007-12-20 19:24:10 +00001507 rtype.getCanonicalType().getTypePtr());
1508 if (IT)
1509 rhsID = IT->getDecl();
1510 }
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001511 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001512 if (!rhsQI && !rhsQID && !rhsID)
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001513 return false;
1514
Fariborz Jahaniance5528d2008-01-03 20:01:35 +00001515 unsigned numRhsProtocols = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001516 ObjCProtocolDecl **rhsProtoList = 0;
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001517 if (rhsQI) {
1518 numRhsProtocols = rhsQI->getNumProtocols();
1519 rhsProtoList = rhsQI->getReferencedProtocols();
1520 }
1521 else if (rhsQID) {
1522 numRhsProtocols = rhsQID->getNumProtocols();
1523 rhsProtoList = rhsQID->getReferencedProtocols();
1524 }
1525
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001526 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001527 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001528 bool match = false;
1529
1530 // when comparing an id<P> on lhs with a static type on rhs,
1531 // see if static class implements all of id's protocols, directly or
1532 // through its super class and categories.
1533 if (rhsID) {
1534 if (ClassImplementsProtocol(lhsProto, rhsID, true))
1535 match = true;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001536 }
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001537 else for (unsigned j = 0; j < numRhsProtocols; j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001538 ObjCProtocolDecl *rhsProto = rhsProtoList[j];
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001539 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1540 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001541 match = true;
1542 break;
1543 }
1544 }
1545 if (!match)
1546 return false;
1547 }
1548 }
1549 else if (rhsQID) {
1550 if (!lhsQID && lhs->getTypeClass() == Type::Pointer) {
1551 QualType ltype =
1552 cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1553 lhsQI =
Ted Kremenek42730c52008-01-07 19:49:32 +00001554 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001555 ltype.getCanonicalType().getTypePtr());
Fariborz Jahanian87829072007-12-20 19:24:10 +00001556 if (!lhsQI) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001557 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanian87829072007-12-20 19:24:10 +00001558 ltype.getCanonicalType().getTypePtr());
1559 if (IT)
1560 lhsID = IT->getDecl();
1561 }
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001562 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001563 if (!lhsQI && !lhsQID && !lhsID)
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001564 return false;
Fariborz Jahanian87829072007-12-20 19:24:10 +00001565
Fariborz Jahaniance5528d2008-01-03 20:01:35 +00001566 unsigned numLhsProtocols = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001567 ObjCProtocolDecl **lhsProtoList = 0;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001568 if (lhsQI) {
1569 numLhsProtocols = lhsQI->getNumProtocols();
1570 lhsProtoList = lhsQI->getReferencedProtocols();
1571 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001572 else if (lhsQID) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001573 numLhsProtocols = lhsQID->getNumProtocols();
1574 lhsProtoList = lhsQID->getReferencedProtocols();
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001575 }
1576 bool match = false;
1577 // for static type vs. qualified 'id' type, check that class implements
1578 // one of 'id's protocols.
1579 if (lhsID) {
1580 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001581 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001582 if (ClassImplementsProtocol(rhsProto, lhsID, compare)) {
1583 match = true;
1584 break;
1585 }
1586 }
1587 }
1588 else for (unsigned i =0; i < numLhsProtocols; i++) {
1589 match = false;
Ted Kremenek42730c52008-01-07 19:49:32 +00001590 ObjCProtocolDecl *lhsProto = lhsProtoList[i];
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001591 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001592 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001593 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1594 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001595 match = true;
1596 break;
1597 }
1598 }
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001599 }
1600 if (!match)
1601 return false;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001602 }
1603 return true;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001604}
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001605
Chris Lattner5003e8b2007-11-01 05:03:41 +00001606bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1607 const VectorType *lVector = lhs->getAsVectorType();
1608 const VectorType *rVector = rhs->getAsVectorType();
1609
1610 if ((lVector->getElementType().getCanonicalType() ==
1611 rVector->getElementType().getCanonicalType()) &&
1612 (lVector->getNumElements() == rVector->getNumElements()))
1613 return true;
1614 return false;
1615}
1616
Steve Naroff85f0dc52007-10-15 20:41:53 +00001617// C99 6.2.7p1: If both are complete types, then the following additional
1618// requirements apply...FIXME (handle compatibility across source files).
1619bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
Steve Naroff4a5e2072007-11-07 06:03:51 +00001620 // "Class" and "id" are compatible built-in structure types.
Ted Kremenek42730c52008-01-07 19:49:32 +00001621 if (isObjCIdType(lhs) && isObjCClassType(rhs) ||
1622 isObjCClassType(lhs) && isObjCIdType(rhs))
Steve Naroff4a5e2072007-11-07 06:03:51 +00001623 return true;
Eli Friedmane7fb03a2008-02-15 06:03:44 +00001624
1625 // Within a translation unit a tag type is
1626 // only compatible with itself.
1627 return lhs.getCanonicalType() == rhs.getCanonicalType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001628}
1629
1630bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1631 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1632 // identically qualified and both shall be pointers to compatible types.
Chris Lattner35fef522008-02-20 20:55:12 +00001633 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1634 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001635 return false;
1636
1637 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1638 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1639
1640 return typesAreCompatible(ltype, rtype);
1641}
1642
Bill Wendling6a9d8542007-12-03 07:33:35 +00001643// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroff85f0dc52007-10-15 20:41:53 +00001644// reference to T, the operation assigns to the object of type T denoted by the
1645// reference.
1646bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1647 QualType ltype = lhs;
1648
1649 if (lhs->isReferenceType())
1650 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1651
1652 QualType rtype = rhs;
1653
1654 if (rhs->isReferenceType())
1655 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1656
1657 return typesAreCompatible(ltype, rtype);
1658}
1659
1660bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1661 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1662 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1663 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1664 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1665
1666 // first check the return types (common between C99 and K&R).
1667 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1668 return false;
1669
1670 if (lproto && rproto) { // two C99 style function prototypes
1671 unsigned lproto_nargs = lproto->getNumArgs();
1672 unsigned rproto_nargs = rproto->getNumArgs();
1673
1674 if (lproto_nargs != rproto_nargs)
1675 return false;
1676
1677 // both prototypes have the same number of arguments.
1678 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1679 (rproto->isVariadic() && !lproto->isVariadic()))
1680 return false;
1681
1682 // The use of ellipsis agree...now check the argument types.
1683 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001684 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1685 // is taken as having the unqualified version of it's declared type.
Steve Naroffdec17fe2008-01-29 00:15:50 +00001686 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001687 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001688 return false;
1689 return true;
1690 }
1691 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1692 return true;
1693
1694 // we have a mixture of K&R style with C99 prototypes
1695 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1696
1697 if (proto->isVariadic())
1698 return false;
1699
1700 // FIXME: Each parameter type T in the prototype must be compatible with the
1701 // type resulting from applying the usual argument conversions to T.
1702 return true;
1703}
1704
1705bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
Eli Friedman1e7537832008-02-06 04:53:22 +00001706 // Compatible arrays must have compatible element types
1707 QualType ltype = lhs->getAsArrayType()->getElementType();
1708 QualType rtype = rhs->getAsArrayType()->getElementType();
1709
Steve Naroff85f0dc52007-10-15 20:41:53 +00001710 if (!typesAreCompatible(ltype, rtype))
1711 return false;
Eli Friedman1e7537832008-02-06 04:53:22 +00001712
1713 // Compatible arrays must be the same size
1714 if (const ConstantArrayType* LCAT = lhs->getAsConstantArrayType())
1715 if (const ConstantArrayType* RCAT = rhs->getAsConstantArrayType())
1716 return RCAT->getSize() == LCAT->getSize();
1717
Steve Naroff85f0dc52007-10-15 20:41:53 +00001718 return true;
1719}
1720
1721/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1722/// both shall have the identically qualified version of a compatible type.
1723/// C99 6.2.7p1: Two types have compatible types if their types are the
1724/// same. See 6.7.[2,3,5] for additional rules.
1725bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
Chris Lattner35fef522008-02-20 20:55:12 +00001726 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1727 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroff577f9722008-01-29 18:58:14 +00001728 return false;
1729
Steve Naroff85f0dc52007-10-15 20:41:53 +00001730 QualType lcanon = lhs.getCanonicalType();
1731 QualType rcanon = rhs.getCanonicalType();
1732
1733 // If two types are identical, they are are compatible
1734 if (lcanon == rcanon)
1735 return true;
Bill Wendling6a9d8542007-12-03 07:33:35 +00001736
1737 // C++ [expr]: If an expression initially has the type "reference to T", the
1738 // type is adjusted to "T" prior to any further analysis, the expression
1739 // designates the object or function denoted by the reference, and the
1740 // expression is an lvalue.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001741 if (ReferenceType *RT = dyn_cast<ReferenceType>(lcanon))
1742 lcanon = RT->getReferenceeType();
1743 if (ReferenceType *RT = dyn_cast<ReferenceType>(rcanon))
1744 rcanon = RT->getReferenceeType();
1745
1746 Type::TypeClass LHSClass = lcanon->getTypeClass();
1747 Type::TypeClass RHSClass = rcanon->getTypeClass();
1748
1749 // We want to consider the two function types to be the same for these
1750 // comparisons, just force one to the other.
1751 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1752 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00001753
1754 // Same as above for arrays
1755 if (LHSClass == Type::VariableArray) LHSClass = Type::ConstantArray;
1756 if (RHSClass == Type::VariableArray) RHSClass = Type::ConstantArray;
Eli Friedman8ff07782008-02-15 18:16:39 +00001757 if (LHSClass == Type::IncompleteArray) LHSClass = Type::ConstantArray;
1758 if (RHSClass == Type::IncompleteArray) RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001759
Steve Naroffc88babe2008-01-09 22:43:08 +00001760 // If the canonical type classes don't match...
Chris Lattnerc38d4522008-01-14 05:45:46 +00001761 if (LHSClass != RHSClass) {
Steve Naroff85f0dc52007-10-15 20:41:53 +00001762 // For Objective-C, it is possible for two types to be compatible
1763 // when their classes don't match (when dealing with "id"). If either type
1764 // is an interface, we defer to objcTypesAreCompatible().
Ted Kremenek42730c52008-01-07 19:49:32 +00001765 if (lcanon->isObjCInterfaceType() || rcanon->isObjCInterfaceType())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001766 return objcTypesAreCompatible(lcanon, rcanon);
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001767
Chris Lattnerc38d4522008-01-14 05:45:46 +00001768 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1769 // a signed integer type, or an unsigned integer type.
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001770 if (lcanon->isEnumeralType() && rcanon->isIntegralType()) {
1771 EnumDecl* EDecl = cast<EnumDecl>(cast<TagType>(lcanon)->getDecl());
1772 return EDecl->getIntegerType() == rcanon;
1773 }
1774 if (rcanon->isEnumeralType() && lcanon->isIntegralType()) {
1775 EnumDecl* EDecl = cast<EnumDecl>(cast<TagType>(rcanon)->getDecl());
1776 return EDecl->getIntegerType() == lcanon;
1777 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001778
Steve Naroff85f0dc52007-10-15 20:41:53 +00001779 return false;
1780 }
Steve Naroffc88babe2008-01-09 22:43:08 +00001781 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001782 switch (LHSClass) {
1783 case Type::FunctionProto: assert(0 && "Canonicalized away above");
1784 case Type::Pointer:
1785 return pointerTypesAreCompatible(lcanon, rcanon);
1786 case Type::ConstantArray:
1787 case Type::VariableArray:
Eli Friedman8ff07782008-02-15 18:16:39 +00001788 case Type::IncompleteArray:
Chris Lattnerc38d4522008-01-14 05:45:46 +00001789 return arrayTypesAreCompatible(lcanon, rcanon);
1790 case Type::FunctionNoProto:
1791 return functionTypesAreCompatible(lcanon, rcanon);
1792 case Type::Tagged: // handle structures, unions
1793 return tagTypesAreCompatible(lcanon, rcanon);
1794 case Type::Builtin:
1795 return builtinTypesAreCompatible(lcanon, rcanon);
1796 case Type::ObjCInterface:
1797 return interfaceTypesAreCompatible(lcanon, rcanon);
1798 case Type::Vector:
1799 case Type::OCUVector:
1800 return vectorTypesAreCompatible(lcanon, rcanon);
1801 case Type::ObjCQualifiedInterface:
1802 return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
1803 default:
1804 assert(0 && "unexpected type");
Steve Naroff85f0dc52007-10-15 20:41:53 +00001805 }
1806 return true; // should never get here...
1807}
Ted Kremenek738e6c02007-10-31 17:10:13 +00001808
Ted Kremenek738e6c02007-10-31 17:10:13 +00001809/// Emit - Serialize an ASTContext object to Bitcode.
1810void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00001811 S.EmitRef(SourceMgr);
1812 S.EmitRef(Target);
1813 S.EmitRef(Idents);
1814 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001815
Ted Kremenek68228a92007-10-31 22:44:07 +00001816 // Emit the size of the type vector so that we can reserve that size
1817 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001818 S.EmitInt(Types.size());
1819
Ted Kremenek034a78c2007-11-13 22:02:55 +00001820 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1821 I!=E;++I)
1822 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001823
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001824 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001825}
1826
Ted Kremenekacba3612007-11-13 00:25:37 +00001827ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek68228a92007-10-31 22:44:07 +00001828 SourceManager &SM = D.ReadRef<SourceManager>();
1829 TargetInfo &t = D.ReadRef<TargetInfo>();
1830 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1831 SelectorTable &sels = D.ReadRef<SelectorTable>();
1832
1833 unsigned size_reserve = D.ReadInt();
1834
1835 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1836
Ted Kremenek034a78c2007-11-13 22:02:55 +00001837 for (unsigned i = 0; i < size_reserve; ++i)
1838 Type::Create(*A,i,D);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001839
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001840 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00001841
1842 return A;
1843}