blob: bd7215dd4a20f2b7c153535ecf994faddfa9f7bd [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:
260 return getTypeInfo(cast<ASQualType>(T)->getBaseType(), 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) {
439 // Check if we've already instantiated an address space qual'd type of this type.
440 llvm::FoldingSetNodeID ID;
441 ASQualType::Profile(ID, T, AddressSpace);
442 void *InsertPos = 0;
443 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
444 return QualType(ASQy, 0);
445
446 // If the base type isn't canonical, this won't be a canonical type either,
447 // so fill in the canonical type field.
448 QualType Canonical;
449 if (!T->isCanonical()) {
450 Canonical = getASQualType(T.getCanonicalType(), AddressSpace);
451
452 // Get the new insert position for the node we care about.
453 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
454 assert(NewIP == 0 && "Shouldn't be in the map!");
455 }
456 ASQualType *New = new ASQualType(T, Canonical, AddressSpace);
457 ASQualTypes.InsertNode(New, InsertPos);
458 Types.push_back(New);
459 return QualType(New, 0);
460}
461
Chris Lattner4b009652007-07-25 00:24:17 +0000462
463/// getComplexType - Return the uniqued reference to the type for a complex
464/// number with the specified element type.
465QualType ASTContext::getComplexType(QualType T) {
466 // Unique pointers, to guarantee there is only one pointer of a particular
467 // structure.
468 llvm::FoldingSetNodeID ID;
469 ComplexType::Profile(ID, T);
470
471 void *InsertPos = 0;
472 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
473 return QualType(CT, 0);
474
475 // If the pointee type isn't canonical, this won't be a canonical type either,
476 // so fill in the canonical type field.
477 QualType Canonical;
478 if (!T->isCanonical()) {
479 Canonical = getComplexType(T.getCanonicalType());
480
481 // Get the new insert position for the node we care about.
482 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
483 assert(NewIP == 0 && "Shouldn't be in the map!");
484 }
485 ComplexType *New = new ComplexType(T, Canonical);
486 Types.push_back(New);
487 ComplexTypes.InsertNode(New, InsertPos);
488 return QualType(New, 0);
489}
490
491
492/// getPointerType - Return the uniqued reference to the type for a pointer to
493/// the specified type.
494QualType ASTContext::getPointerType(QualType T) {
495 // Unique pointers, to guarantee there is only one pointer of a particular
496 // structure.
497 llvm::FoldingSetNodeID ID;
498 PointerType::Profile(ID, T);
499
500 void *InsertPos = 0;
501 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
502 return QualType(PT, 0);
503
504 // If the pointee type isn't canonical, this won't be a canonical type either,
505 // so fill in the canonical type field.
506 QualType Canonical;
507 if (!T->isCanonical()) {
508 Canonical = getPointerType(T.getCanonicalType());
509
510 // Get the new insert position for the node we care about.
511 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
512 assert(NewIP == 0 && "Shouldn't be in the map!");
513 }
514 PointerType *New = new PointerType(T, Canonical);
515 Types.push_back(New);
516 PointerTypes.InsertNode(New, InsertPos);
517 return QualType(New, 0);
518}
519
520/// getReferenceType - Return the uniqued reference to the type for a reference
521/// to the specified type.
522QualType ASTContext::getReferenceType(QualType T) {
523 // Unique pointers, to guarantee there is only one pointer of a particular
524 // structure.
525 llvm::FoldingSetNodeID ID;
526 ReferenceType::Profile(ID, T);
527
528 void *InsertPos = 0;
529 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
530 return QualType(RT, 0);
531
532 // If the referencee type isn't canonical, this won't be a canonical type
533 // either, so fill in the canonical type field.
534 QualType Canonical;
535 if (!T->isCanonical()) {
536 Canonical = getReferenceType(T.getCanonicalType());
537
538 // Get the new insert position for the node we care about.
539 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
540 assert(NewIP == 0 && "Shouldn't be in the map!");
541 }
542
543 ReferenceType *New = new ReferenceType(T, Canonical);
544 Types.push_back(New);
545 ReferenceTypes.InsertNode(New, InsertPos);
546 return QualType(New, 0);
547}
548
Steve Naroff83c13012007-08-30 01:06:46 +0000549/// getConstantArrayType - Return the unique reference to the type for an
550/// array of the specified element type.
551QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000552 const llvm::APInt &ArySize,
553 ArrayType::ArraySizeModifier ASM,
554 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000555 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000556 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000557
558 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000559 if (ConstantArrayType *ATP =
560 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000561 return QualType(ATP, 0);
562
563 // If the element type isn't canonical, this won't be a canonical type either,
564 // so fill in the canonical type field.
565 QualType Canonical;
566 if (!EltTy->isCanonical()) {
Steve Naroff24c9b982007-08-30 18:10:14 +0000567 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
568 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000569 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000570 ConstantArrayType *NewIP =
571 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
572
Chris Lattner4b009652007-07-25 00:24:17 +0000573 assert(NewIP == 0 && "Shouldn't be in the map!");
574 }
575
Steve Naroff24c9b982007-08-30 18:10:14 +0000576 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
577 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000578 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000579 Types.push_back(New);
580 return QualType(New, 0);
581}
582
Steve Naroffe2579e32007-08-30 18:14:25 +0000583/// getVariableArrayType - Returns a non-unique reference to the type for a
584/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000585QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
586 ArrayType::ArraySizeModifier ASM,
587 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000588 // Since we don't unique expressions, it isn't possible to unique VLA's
589 // that have an expression provided for their size.
590
591 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
592 ASM, EltTypeQuals);
593
594 VariableArrayTypes.push_back(New);
595 Types.push_back(New);
596 return QualType(New, 0);
597}
598
599QualType ASTContext::getIncompleteArrayType(QualType EltTy,
600 ArrayType::ArraySizeModifier ASM,
601 unsigned EltTypeQuals) {
602 llvm::FoldingSetNodeID ID;
603 IncompleteArrayType::Profile(ID, EltTy);
604
605 void *InsertPos = 0;
606 if (IncompleteArrayType *ATP =
607 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
608 return QualType(ATP, 0);
609
610 // If the element type isn't canonical, this won't be a canonical type
611 // either, so fill in the canonical type field.
612 QualType Canonical;
613
614 if (!EltTy->isCanonical()) {
615 Canonical = getIncompleteArrayType(EltTy.getCanonicalType(),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000616 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000617
618 // Get the new insert position for the node we care about.
619 IncompleteArrayType *NewIP =
620 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
621
622 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000623 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000624
625 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
626 ASM, EltTypeQuals);
627
628 IncompleteArrayTypes.InsertNode(New, InsertPos);
629 Types.push_back(New);
630 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000631}
632
Chris Lattner4b009652007-07-25 00:24:17 +0000633/// getVectorType - Return the unique reference to a vector type of
634/// the specified element type and size. VectorType must be a built-in type.
635QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
636 BuiltinType *baseType;
637
638 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
639 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
640
641 // Check if we've already instantiated a vector of this type.
642 llvm::FoldingSetNodeID ID;
643 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
644 void *InsertPos = 0;
645 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
646 return QualType(VTP, 0);
647
648 // If the element type isn't canonical, this won't be a canonical type either,
649 // so fill in the canonical type field.
650 QualType Canonical;
651 if (!vecType->isCanonical()) {
652 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
653
654 // Get the new insert position for the node we care about.
655 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
656 assert(NewIP == 0 && "Shouldn't be in the map!");
657 }
658 VectorType *New = new VectorType(vecType, NumElts, Canonical);
659 VectorTypes.InsertNode(New, InsertPos);
660 Types.push_back(New);
661 return QualType(New, 0);
662}
663
664/// getOCUVectorType - Return the unique reference to an OCU vector type of
665/// the specified element type and size. VectorType must be a built-in type.
666QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
667 BuiltinType *baseType;
668
669 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
670 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
671
672 // Check if we've already instantiated a vector of this type.
673 llvm::FoldingSetNodeID ID;
674 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
675 void *InsertPos = 0;
676 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
677 return QualType(VTP, 0);
678
679 // If the element type isn't canonical, this won't be a canonical type either,
680 // so fill in the canonical type field.
681 QualType Canonical;
682 if (!vecType->isCanonical()) {
683 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
684
685 // Get the new insert position for the node we care about.
686 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
687 assert(NewIP == 0 && "Shouldn't be in the map!");
688 }
689 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
690 VectorTypes.InsertNode(New, InsertPos);
691 Types.push_back(New);
692 return QualType(New, 0);
693}
694
695/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
696///
697QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
698 // Unique functions, to guarantee there is only one function of a particular
699 // structure.
700 llvm::FoldingSetNodeID ID;
701 FunctionTypeNoProto::Profile(ID, ResultTy);
702
703 void *InsertPos = 0;
704 if (FunctionTypeNoProto *FT =
705 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
706 return QualType(FT, 0);
707
708 QualType Canonical;
709 if (!ResultTy->isCanonical()) {
710 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
711
712 // Get the new insert position for the node we care about.
713 FunctionTypeNoProto *NewIP =
714 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
715 assert(NewIP == 0 && "Shouldn't be in the map!");
716 }
717
718 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
719 Types.push_back(New);
720 FunctionTypeProtos.InsertNode(New, InsertPos);
721 return QualType(New, 0);
722}
723
724/// getFunctionType - Return a normal function type with a typed argument
725/// list. isVariadic indicates whether the argument list includes '...'.
726QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
727 unsigned NumArgs, bool isVariadic) {
728 // Unique functions, to guarantee there is only one function of a particular
729 // structure.
730 llvm::FoldingSetNodeID ID;
731 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
732
733 void *InsertPos = 0;
734 if (FunctionTypeProto *FTP =
735 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
736 return QualType(FTP, 0);
737
738 // Determine whether the type being created is already canonical or not.
739 bool isCanonical = ResultTy->isCanonical();
740 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
741 if (!ArgArray[i]->isCanonical())
742 isCanonical = false;
743
744 // If this type isn't canonical, get the canonical version of it.
745 QualType Canonical;
746 if (!isCanonical) {
747 llvm::SmallVector<QualType, 16> CanonicalArgs;
748 CanonicalArgs.reserve(NumArgs);
749 for (unsigned i = 0; i != NumArgs; ++i)
750 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
751
752 Canonical = getFunctionType(ResultTy.getCanonicalType(),
753 &CanonicalArgs[0], NumArgs,
754 isVariadic);
755
756 // Get the new insert position for the node we care about.
757 FunctionTypeProto *NewIP =
758 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
759 assert(NewIP == 0 && "Shouldn't be in the map!");
760 }
761
762 // FunctionTypeProto objects are not allocated with new because they have a
763 // variable size array (for parameter types) at the end of them.
764 FunctionTypeProto *FTP =
765 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
766 NumArgs*sizeof(QualType));
767 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
768 Canonical);
769 Types.push_back(FTP);
770 FunctionTypeProtos.InsertNode(FTP, InsertPos);
771 return QualType(FTP, 0);
772}
773
774/// getTypedefType - Return the unique reference to the type for the
775/// specified typename decl.
776QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
777 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
778
779 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000780 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000781 Types.push_back(Decl->TypeForDecl);
782 return QualType(Decl->TypeForDecl, 0);
783}
784
Ted Kremenek42730c52008-01-07 19:49:32 +0000785/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000786/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000787QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000788 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
789
Ted Kremenek42730c52008-01-07 19:49:32 +0000790 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000791 Types.push_back(Decl->TypeForDecl);
792 return QualType(Decl->TypeForDecl, 0);
793}
794
Ted Kremenek42730c52008-01-07 19:49:32 +0000795/// getObjCQualifiedInterfaceType - Return a
796/// ObjCQualifiedInterfaceType type for the given interface decl and
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000797/// the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000798QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
799 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000800 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000801 ObjCQualifiedInterfaceType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000802
803 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000804 if (ObjCQualifiedInterfaceType *QT =
805 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000806 return QualType(QT, 0);
807
808 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +0000809 ObjCQualifiedInterfaceType *QType =
810 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000811 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000812 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000813 return QualType(QType, 0);
814}
815
Ted Kremenek42730c52008-01-07 19:49:32 +0000816/// getObjCQualifiedIdType - Return a
817/// getObjCQualifiedIdType type for the 'id' decl and
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000818/// the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000819QualType ASTContext::getObjCQualifiedIdType(QualType idType,
820 ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000821 unsigned NumProtocols) {
822 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000823 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000824
825 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000826 if (ObjCQualifiedIdType *QT =
827 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000828 return QualType(QT, 0);
829
830 // No Match;
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000831 QualType Canonical;
832 if (!idType->isCanonical()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000833 Canonical = getObjCQualifiedIdType(idType.getCanonicalType(),
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000834 Protocols, NumProtocols);
Ted Kremenek42730c52008-01-07 19:49:32 +0000835 ObjCQualifiedIdType *NewQT =
836 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000837 assert(NewQT == 0 && "Shouldn't be in the map!");
838 }
839
Ted Kremenek42730c52008-01-07 19:49:32 +0000840 ObjCQualifiedIdType *QType =
841 new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000842 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000843 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000844 return QualType(QType, 0);
845}
846
Steve Naroff0604dd92007-08-01 18:02:17 +0000847/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
848/// TypeOfExpr AST's (since expression's are never shared). For example,
849/// multiple declarations that refer to "typeof(x)" all contain different
850/// DeclRefExpr's. This doesn't effect the type checker, since it operates
851/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000852QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroff7cbb1462007-07-31 12:34:36 +0000853 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000854 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
855 Types.push_back(toe);
856 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000857}
858
Steve Naroff0604dd92007-08-01 18:02:17 +0000859/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
860/// TypeOfType AST's. The only motivation to unique these nodes would be
861/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
862/// an issue. This doesn't effect the type checker, since it operates
863/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000864QualType ASTContext::getTypeOfType(QualType tofType) {
865 QualType Canonical = tofType.getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000866 TypeOfType *tot = new TypeOfType(tofType, Canonical);
867 Types.push_back(tot);
868 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000869}
870
Chris Lattner4b009652007-07-25 00:24:17 +0000871/// getTagDeclType - Return the unique reference to the type for the
872/// specified TagDecl (struct/union/class/enum) decl.
873QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +0000874 assert (Decl);
875
Ted Kremenekf05026d2007-11-14 00:03:20 +0000876 // The decl stores the type cache.
Ted Kremenekae8fa032007-11-26 21:16:01 +0000877 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Ted Kremenekf05026d2007-11-14 00:03:20 +0000878
879 TagType* T = new TagType(Decl, QualType());
Ted Kremenekae8fa032007-11-26 21:16:01 +0000880 Types.push_back(T);
881 Decl->TypeForDecl = T;
Ted Kremenekf05026d2007-11-14 00:03:20 +0000882
883 return QualType(T, 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000884}
885
886/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
887/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
888/// needs to agree with the definition in <stddef.h>.
889QualType ASTContext::getSizeType() const {
890 // On Darwin, size_t is defined as a "long unsigned int".
891 // FIXME: should derive from "Target".
892 return UnsignedLongTy;
893}
894
Eli Friedmanfdd35d72008-02-12 08:29:21 +0000895/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
896/// width of characters in wide strings, The value is target dependent and
897/// needs to agree with the definition in <stddef.h>.
898QualType ASTContext::getWcharType() const {
899 // On Darwin, wchar_t is defined as a "int".
900 // FIXME: should derive from "Target".
901 return IntTy;
902}
903
Chris Lattner4b009652007-07-25 00:24:17 +0000904/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
905/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
906QualType ASTContext::getPointerDiffType() const {
907 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
908 // FIXME: should derive from "Target".
909 return IntTy;
910}
911
912/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
913/// routine will assert if passed a built-in type that isn't an integer or enum.
914static int getIntegerRank(QualType t) {
915 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
916 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
917 return 4;
918 }
919
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000920 const BuiltinType *BT = t.getCanonicalType()->getAsBuiltinType();
Chris Lattner4b009652007-07-25 00:24:17 +0000921 switch (BT->getKind()) {
922 default:
923 assert(0 && "getIntegerRank(): not a built-in integer");
924 case BuiltinType::Bool:
925 return 1;
926 case BuiltinType::Char_S:
927 case BuiltinType::Char_U:
928 case BuiltinType::SChar:
929 case BuiltinType::UChar:
930 return 2;
931 case BuiltinType::Short:
932 case BuiltinType::UShort:
933 return 3;
934 case BuiltinType::Int:
935 case BuiltinType::UInt:
936 return 4;
937 case BuiltinType::Long:
938 case BuiltinType::ULong:
939 return 5;
940 case BuiltinType::LongLong:
941 case BuiltinType::ULongLong:
942 return 6;
943 }
944}
945
946/// getFloatingRank - Return a relative rank for floating point types.
947/// This routine will assert if passed a built-in type that isn't a float.
948static int getFloatingRank(QualType T) {
949 T = T.getCanonicalType();
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000950 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +0000951 return getFloatingRank(CT->getElementType());
952
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000953 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattner5003e8b2007-11-01 05:03:41 +0000954 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +0000955 case BuiltinType::Float: return FloatRank;
956 case BuiltinType::Double: return DoubleRank;
957 case BuiltinType::LongDouble: return LongDoubleRank;
958 }
959}
960
Steve Narofffa0c4532007-08-27 01:41:48 +0000961/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
962/// point or a complex type (based on typeDomain/typeSize).
963/// 'typeDomain' is a real floating point or complex type.
964/// 'typeSize' is a real floating point or complex type.
Steve Naroff3cf497f2007-08-27 01:27:54 +0000965QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
966 QualType typeSize, QualType typeDomain) const {
967 if (typeDomain->isComplexType()) {
968 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000969 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000970 case FloatRank: return FloatComplexTy;
971 case DoubleRank: return DoubleComplexTy;
972 case LongDoubleRank: return LongDoubleComplexTy;
973 }
Chris Lattner4b009652007-07-25 00:24:17 +0000974 }
Steve Naroff3cf497f2007-08-27 01:27:54 +0000975 if (typeDomain->isRealFloatingType()) {
976 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000977 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000978 case FloatRank: return FloatTy;
979 case DoubleRank: return DoubleTy;
980 case LongDoubleRank: return LongDoubleTy;
981 }
982 }
983 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattner1d2b4612007-09-16 19:23:47 +0000984 //an invalid return value, but the assert
985 //will ensure that this code is never reached.
986 return VoidTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000987}
988
Steve Naroff45fc9822007-08-27 15:30:22 +0000989/// compareFloatingType - Handles 3 different combos:
990/// float/float, float/complex, complex/complex.
991/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
992int ASTContext::compareFloatingType(QualType lt, QualType rt) {
993 if (getFloatingRank(lt) == getFloatingRank(rt))
994 return 0;
995 if (getFloatingRank(lt) > getFloatingRank(rt))
996 return 1;
997 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +0000998}
999
1000// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
1001// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
1002QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
1003 if (lhs == rhs) return lhs;
1004
1005 bool t1Unsigned = lhs->isUnsignedIntegerType();
1006 bool t2Unsigned = rhs->isUnsignedIntegerType();
1007
1008 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
1009 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
1010
1011 // We have two integer types with differing signs
1012 QualType unsignedType = t1Unsigned ? lhs : rhs;
1013 QualType signedType = t1Unsigned ? rhs : lhs;
1014
1015 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
1016 return unsignedType;
1017 else {
1018 // FIXME: Need to check if the signed type can represent all values of the
1019 // unsigned type. If it can, then the result is the signed type.
1020 // If it can't, then the result is the unsigned version of the signed type.
1021 // Should probably add a helper that returns a signed integer type from
1022 // an unsigned (and vice versa). C99 6.3.1.8.
1023 return signedType;
1024 }
1025}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001026
1027// getCFConstantStringType - Return the type used for constant CFStrings.
1028QualType ASTContext::getCFConstantStringType() {
1029 if (!CFConstantStringTypeDecl) {
1030 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
Steve Naroff0add5d22007-11-03 11:27:19 +00001031 &Idents.get("NSConstantString"),
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001032 0);
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001033 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001034
1035 // const int *isa;
1036 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001037 // int flags;
1038 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001039 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001040 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001041 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001042 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001043 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001044 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001045
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001046 for (unsigned i = 0; i < 4; ++i)
Steve Naroffdc1ad762007-09-14 02:20:46 +00001047 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001048
1049 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1050 }
1051
1052 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001053}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001054
Anders Carlssone3f02572007-10-29 06:33:42 +00001055// This returns true if a type has been typedefed to BOOL:
1056// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001057static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001058 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001059 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001060
1061 return false;
1062}
1063
Ted Kremenek42730c52008-01-07 19:49:32 +00001064/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001065/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001066int ASTContext::getObjCEncodingTypeSize(QualType type) {
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001067 SourceLocation Loc;
1068 uint64_t sz = getTypeSize(type, Loc);
1069
1070 // Make all integer and enum types at least as large as an int
1071 if (sz > 0 && type->isIntegralType())
1072 sz = std::max(sz, getTypeSize(IntTy, Loc));
1073 // Treat arrays as pointers, since that's how they're passed in.
1074 else if (type->isArrayType())
1075 sz = getTypeSize(VoidPtrTy, Loc);
1076 return sz / getTypeSize(CharTy, Loc);
1077}
1078
Ted Kremenek42730c52008-01-07 19:49:32 +00001079/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001080/// declaration.
Ted Kremenek42730c52008-01-07 19:49:32 +00001081void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001082 std::string& S)
1083{
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001084 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001085 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001086 // Encode result type.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001087 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001088 // Compute size of all parameters.
1089 // Start with computing size of a pointer in number of bytes.
1090 // FIXME: There might(should) be a better way of doing this computation!
1091 SourceLocation Loc;
1092 int PtrSize = getTypeSize(VoidPtrTy, Loc) / getTypeSize(CharTy, Loc);
1093 // The first two arguments (self and _cmd) are pointers; account for
1094 // their size.
1095 int ParmOffset = 2 * PtrSize;
1096 int NumOfParams = Decl->getNumParams();
1097 for (int i = 0; i < NumOfParams; i++) {
1098 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001099 int sz = getObjCEncodingTypeSize (PType);
1100 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001101 ParmOffset += sz;
1102 }
1103 S += llvm::utostr(ParmOffset);
1104 S += "@0:";
1105 S += llvm::utostr(PtrSize);
1106
1107 // Argument types.
1108 ParmOffset = 2 * PtrSize;
1109 for (int i = 0; i < NumOfParams; i++) {
1110 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001111 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001112 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001113 getObjCEncodingForTypeQualifier(
1114 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian248db262008-01-22 22:44:46 +00001115 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001116 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001117 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001118 }
1119}
1120
Fariborz Jahanian248db262008-01-22 22:44:46 +00001121void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1122 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson36f07d82007-10-29 05:01:08 +00001123{
Anders Carlssone3f02572007-10-29 06:33:42 +00001124 // FIXME: This currently doesn't encode:
1125 // @ An object (whether statically typed or typed id)
1126 // # A class object (Class)
1127 // : A method selector (SEL)
1128 // {name=type...} A structure
1129 // (name=type...) A union
1130 // bnum A bit field of num bits
1131
1132 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001133 char encoding;
1134 switch (BT->getKind()) {
1135 case BuiltinType::Void:
1136 encoding = 'v';
1137 break;
1138 case BuiltinType::Bool:
1139 encoding = 'B';
1140 break;
1141 case BuiltinType::Char_U:
1142 case BuiltinType::UChar:
1143 encoding = 'C';
1144 break;
1145 case BuiltinType::UShort:
1146 encoding = 'S';
1147 break;
1148 case BuiltinType::UInt:
1149 encoding = 'I';
1150 break;
1151 case BuiltinType::ULong:
1152 encoding = 'L';
1153 break;
1154 case BuiltinType::ULongLong:
1155 encoding = 'Q';
1156 break;
1157 case BuiltinType::Char_S:
1158 case BuiltinType::SChar:
1159 encoding = 'c';
1160 break;
1161 case BuiltinType::Short:
1162 encoding = 's';
1163 break;
1164 case BuiltinType::Int:
1165 encoding = 'i';
1166 break;
1167 case BuiltinType::Long:
1168 encoding = 'l';
1169 break;
1170 case BuiltinType::LongLong:
1171 encoding = 'q';
1172 break;
1173 case BuiltinType::Float:
1174 encoding = 'f';
1175 break;
1176 case BuiltinType::Double:
1177 encoding = 'd';
1178 break;
1179 case BuiltinType::LongDouble:
1180 encoding = 'd';
1181 break;
1182 default:
1183 assert(0 && "Unhandled builtin type kind");
1184 }
1185
1186 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001187 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001188 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001189 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001190 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001191
1192 }
1193 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001194 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001195 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001196 S += '@';
1197 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001198 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001199 S += '#';
1200 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001201 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001202 S += ':';
1203 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001204 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001205
1206 if (PointeeTy->isCharType()) {
1207 // char pointer types should be encoded as '*' unless it is a
1208 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001209 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001210 S += '*';
1211 return;
1212 }
1213 }
1214
1215 S += '^';
Fariborz Jahanian248db262008-01-22 22:44:46 +00001216 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone3f02572007-10-29 06:33:42 +00001217 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001218 S += '[';
1219
1220 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1221 S += llvm::utostr(CAT->getSize().getZExtValue());
1222 else
1223 assert(0 && "Unhandled array type!");
1224
Fariborz Jahanian248db262008-01-22 22:44:46 +00001225 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001226 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001227 } else if (T->getAsFunctionType()) {
1228 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001229 } else if (const RecordType *RTy = T->getAsRecordType()) {
1230 RecordDecl *RDecl= RTy->getDecl();
1231 S += '{';
1232 S += RDecl->getName();
Fariborz Jahanian248db262008-01-22 22:44:46 +00001233 bool found = false;
1234 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1235 if (ERType[i] == RTy) {
1236 found = true;
1237 break;
1238 }
1239 if (!found) {
1240 ERType.push_back(RTy);
1241 S += '=';
1242 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1243 FieldDecl *field = RDecl->getMember(i);
1244 getObjCEncodingForType(field->getType(), S, ERType);
1245 }
1246 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1247 ERType.pop_back();
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001248 }
1249 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001250 } else if (T->isEnumeralType()) {
1251 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001252 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001253 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001254}
1255
Ted Kremenek42730c52008-01-07 19:49:32 +00001256void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001257 std::string& S) const {
1258 if (QT & Decl::OBJC_TQ_In)
1259 S += 'n';
1260 if (QT & Decl::OBJC_TQ_Inout)
1261 S += 'N';
1262 if (QT & Decl::OBJC_TQ_Out)
1263 S += 'o';
1264 if (QT & Decl::OBJC_TQ_Bycopy)
1265 S += 'O';
1266 if (QT & Decl::OBJC_TQ_Byref)
1267 S += 'R';
1268 if (QT & Decl::OBJC_TQ_Oneway)
1269 S += 'V';
1270}
1271
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001272void ASTContext::setBuiltinVaListType(QualType T)
1273{
1274 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1275
1276 BuiltinVaListType = T;
1277}
1278
Ted Kremenek42730c52008-01-07 19:49:32 +00001279void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001280{
Ted Kremenek42730c52008-01-07 19:49:32 +00001281 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff9d12c902007-10-15 14:41:52 +00001282
Ted Kremenek42730c52008-01-07 19:49:32 +00001283 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001284
1285 // typedef struct objc_object *id;
1286 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1287 assert(ptr && "'id' incorrectly typed");
1288 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1289 assert(rec && "'id' incorrectly typed");
1290 IdStructType = rec;
1291}
1292
Ted Kremenek42730c52008-01-07 19:49:32 +00001293void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001294{
Ted Kremenek42730c52008-01-07 19:49:32 +00001295 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001296
Ted Kremenek42730c52008-01-07 19:49:32 +00001297 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001298
1299 // typedef struct objc_selector *SEL;
1300 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1301 assert(ptr && "'SEL' incorrectly typed");
1302 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1303 assert(rec && "'SEL' incorrectly typed");
1304 SelStructType = rec;
1305}
1306
Ted Kremenek42730c52008-01-07 19:49:32 +00001307void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001308{
Ted Kremenek42730c52008-01-07 19:49:32 +00001309 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1310 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001311}
1312
Ted Kremenek42730c52008-01-07 19:49:32 +00001313void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001314{
Ted Kremenek42730c52008-01-07 19:49:32 +00001315 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001316
Ted Kremenek42730c52008-01-07 19:49:32 +00001317 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001318
1319 // typedef struct objc_class *Class;
1320 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1321 assert(ptr && "'Class' incorrectly typed");
1322 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1323 assert(rec && "'Class' incorrectly typed");
1324 ClassStructType = rec;
1325}
1326
Ted Kremenek42730c52008-01-07 19:49:32 +00001327void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1328 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001329 "'NSConstantString' type already set!");
1330
Ted Kremenek42730c52008-01-07 19:49:32 +00001331 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001332}
1333
Steve Naroff85f0dc52007-10-15 20:41:53 +00001334bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1335 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1336 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1337
1338 return lBuiltin->getKind() == rBuiltin->getKind();
1339}
1340
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001341/// objcTypesAreCompatible - This routine is called when two types
1342/// are of different class; one is interface type or is
1343/// a qualified interface type and the other type is of a different class.
1344/// Example, II or II<P>.
Steve Naroff85f0dc52007-10-15 20:41:53 +00001345bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001346 if (lhs->isObjCInterfaceType() && isObjCIdType(rhs))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001347 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001348 else if (isObjCIdType(lhs) && rhs->isObjCInterfaceType())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001349 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001350 if (ObjCInterfaceType *lhsIT =
1351 dyn_cast<ObjCInterfaceType>(lhs.getCanonicalType().getTypePtr())) {
1352 ObjCQualifiedInterfaceType *rhsQI =
1353 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001354 return rhsQI && (lhsIT->getDecl() == rhsQI->getDecl());
1355 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001356 else if (ObjCInterfaceType *rhsIT =
1357 dyn_cast<ObjCInterfaceType>(rhs.getCanonicalType().getTypePtr())) {
1358 ObjCQualifiedInterfaceType *lhsQI =
1359 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001360 return lhsQI && (rhsIT->getDecl() == lhsQI->getDecl());
1361 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00001362 return false;
1363}
1364
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001365/// Check that 'lhs' and 'rhs' are compatible interface types. Both types
1366/// must be canonical types.
Steve Naroff85f0dc52007-10-15 20:41:53 +00001367bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001368 assert (lhs->isCanonical() &&
1369 "interfaceTypesAreCompatible strip typedefs of lhs");
1370 assert (rhs->isCanonical() &&
1371 "interfaceTypesAreCompatible strip typedefs of rhs");
Fariborz Jahaniance2de812007-12-20 22:37:58 +00001372 if (lhs == rhs)
1373 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001374 ObjCInterfaceType *lhsIT = cast<ObjCInterfaceType>(lhs.getTypePtr());
1375 ObjCInterfaceType *rhsIT = cast<ObjCInterfaceType>(rhs.getTypePtr());
1376 ObjCInterfaceDecl *rhsIDecl = rhsIT->getDecl();
1377 ObjCInterfaceDecl *lhsIDecl = lhsIT->getDecl();
Fariborz Jahaniance2de812007-12-20 22:37:58 +00001378 // rhs is derived from lhs it is OK; else it is not OK.
1379 while (rhsIDecl != NULL) {
1380 if (rhsIDecl == lhsIDecl)
1381 return true;
1382 rhsIDecl = rhsIDecl->getSuperClass();
1383 }
1384 return false;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001385}
1386
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001387bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
1388 QualType rhs) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001389 ObjCQualifiedInterfaceType *lhsQI =
1390 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001391 assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
Ted Kremenek42730c52008-01-07 19:49:32 +00001392 ObjCQualifiedInterfaceType *rhsQI =
1393 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001394 assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001395 if (!interfaceTypesAreCompatible(
1396 getObjCInterfaceType(lhsQI->getDecl()).getCanonicalType(),
1397 getObjCInterfaceType(rhsQI->getDecl()).getCanonicalType()))
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001398 return false;
1399 /* All protocols in lhs must have a presense in rhs. */
1400 for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
1401 bool match = false;
Ted Kremenek42730c52008-01-07 19:49:32 +00001402 ObjCProtocolDecl *lhsProto = lhsQI->getProtocols(i);
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001403 for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001404 ObjCProtocolDecl *rhsProto = rhsQI->getProtocols(j);
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001405 if (lhsProto == rhsProto) {
1406 match = true;
1407 break;
1408 }
1409 }
1410 if (!match)
1411 return false;
1412 }
1413 return true;
1414}
1415
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001416/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
1417/// inheritance hierarchy of 'rProto'.
Ted Kremenek42730c52008-01-07 19:49:32 +00001418static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
1419 ObjCProtocolDecl *rProto) {
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001420 if (lProto == rProto)
1421 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001422 ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001423 for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
1424 if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
1425 return true;
1426 return false;
1427}
1428
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001429/// ClassImplementsProtocol - Checks that 'lProto' protocol
1430/// has been implemented in IDecl class, its super class or categories (if
1431/// lookupCategory is true).
Ted Kremenek42730c52008-01-07 19:49:32 +00001432static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1433 ObjCInterfaceDecl *IDecl,
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001434 bool lookupCategory) {
1435
1436 // 1st, look up the class.
Ted Kremenek42730c52008-01-07 19:49:32 +00001437 ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols();
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001438 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
1439 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1440 return true;
1441 }
1442
1443 // 2nd, look up the category.
1444 if (lookupCategory)
Ted Kremenek42730c52008-01-07 19:49:32 +00001445 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001446 CDecl = CDecl->getNextClassCategory()) {
1447 protoList = CDecl->getReferencedProtocols();
1448 for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
1449 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1450 return true;
1451 }
1452 }
1453
1454 // 3rd, look up the super class(s)
1455 if (IDecl->getSuperClass())
1456 return
1457 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory);
1458
1459 return false;
1460}
1461
Ted Kremenek42730c52008-01-07 19:49:32 +00001462/// ObjCQualifiedIdTypesAreCompatible - Compares two types, at least
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001463/// one of which is a protocol qualified 'id' type. When 'compare'
1464/// is true it is for comparison; when false, for assignment/initialization.
Ted Kremenek42730c52008-01-07 19:49:32 +00001465bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs,
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001466 QualType rhs,
1467 bool compare) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001468 // match id<P..> with an 'id' type in all cases.
1469 if (const PointerType *PT = lhs->getAsPointerType()) {
1470 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001471 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001472 return true;
1473
1474 }
1475 else if (const PointerType *PT = rhs->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
Ted Kremenek42730c52008-01-07 19:49:32 +00001482 ObjCQualifiedInterfaceType *lhsQI = 0;
1483 ObjCQualifiedInterfaceType *rhsQI = 0;
1484 ObjCInterfaceDecl *lhsID = 0;
1485 ObjCInterfaceDecl *rhsID = 0;
1486 ObjCQualifiedIdType *lhsQID = dyn_cast<ObjCQualifiedIdType>(lhs);
1487 ObjCQualifiedIdType *rhsQID = dyn_cast<ObjCQualifiedIdType>(rhs);
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001488
1489 if (lhsQID) {
1490 if (!rhsQID && rhs->getTypeClass() == Type::Pointer) {
1491 QualType rtype =
1492 cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1493 rhsQI =
Ted Kremenek42730c52008-01-07 19:49:32 +00001494 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001495 rtype.getCanonicalType().getTypePtr());
Fariborz Jahanian87829072007-12-20 19:24:10 +00001496 if (!rhsQI) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001497 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanian87829072007-12-20 19:24:10 +00001498 rtype.getCanonicalType().getTypePtr());
1499 if (IT)
1500 rhsID = IT->getDecl();
1501 }
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001502 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001503 if (!rhsQI && !rhsQID && !rhsID)
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001504 return false;
1505
Fariborz Jahaniance5528d2008-01-03 20:01:35 +00001506 unsigned numRhsProtocols = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001507 ObjCProtocolDecl **rhsProtoList = 0;
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001508 if (rhsQI) {
1509 numRhsProtocols = rhsQI->getNumProtocols();
1510 rhsProtoList = rhsQI->getReferencedProtocols();
1511 }
1512 else if (rhsQID) {
1513 numRhsProtocols = rhsQID->getNumProtocols();
1514 rhsProtoList = rhsQID->getReferencedProtocols();
1515 }
1516
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001517 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001518 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001519 bool match = false;
1520
1521 // when comparing an id<P> on lhs with a static type on rhs,
1522 // see if static class implements all of id's protocols, directly or
1523 // through its super class and categories.
1524 if (rhsID) {
1525 if (ClassImplementsProtocol(lhsProto, rhsID, true))
1526 match = true;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001527 }
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001528 else for (unsigned j = 0; j < numRhsProtocols; j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001529 ObjCProtocolDecl *rhsProto = rhsProtoList[j];
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001530 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1531 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001532 match = true;
1533 break;
1534 }
1535 }
1536 if (!match)
1537 return false;
1538 }
1539 }
1540 else if (rhsQID) {
1541 if (!lhsQID && lhs->getTypeClass() == Type::Pointer) {
1542 QualType ltype =
1543 cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1544 lhsQI =
Ted Kremenek42730c52008-01-07 19:49:32 +00001545 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001546 ltype.getCanonicalType().getTypePtr());
Fariborz Jahanian87829072007-12-20 19:24:10 +00001547 if (!lhsQI) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001548 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanian87829072007-12-20 19:24:10 +00001549 ltype.getCanonicalType().getTypePtr());
1550 if (IT)
1551 lhsID = IT->getDecl();
1552 }
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001553 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001554 if (!lhsQI && !lhsQID && !lhsID)
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001555 return false;
Fariborz Jahanian87829072007-12-20 19:24:10 +00001556
Fariborz Jahaniance5528d2008-01-03 20:01:35 +00001557 unsigned numLhsProtocols = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001558 ObjCProtocolDecl **lhsProtoList = 0;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001559 if (lhsQI) {
1560 numLhsProtocols = lhsQI->getNumProtocols();
1561 lhsProtoList = lhsQI->getReferencedProtocols();
1562 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001563 else if (lhsQID) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001564 numLhsProtocols = lhsQID->getNumProtocols();
1565 lhsProtoList = lhsQID->getReferencedProtocols();
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001566 }
1567 bool match = false;
1568 // for static type vs. qualified 'id' type, check that class implements
1569 // one of 'id's protocols.
1570 if (lhsID) {
1571 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001572 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001573 if (ClassImplementsProtocol(rhsProto, lhsID, compare)) {
1574 match = true;
1575 break;
1576 }
1577 }
1578 }
1579 else for (unsigned i =0; i < numLhsProtocols; i++) {
1580 match = false;
Ted Kremenek42730c52008-01-07 19:49:32 +00001581 ObjCProtocolDecl *lhsProto = lhsProtoList[i];
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001582 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001583 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001584 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1585 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001586 match = true;
1587 break;
1588 }
1589 }
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001590 }
1591 if (!match)
1592 return false;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001593 }
1594 return true;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001595}
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001596
Chris Lattner5003e8b2007-11-01 05:03:41 +00001597bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1598 const VectorType *lVector = lhs->getAsVectorType();
1599 const VectorType *rVector = rhs->getAsVectorType();
1600
1601 if ((lVector->getElementType().getCanonicalType() ==
1602 rVector->getElementType().getCanonicalType()) &&
1603 (lVector->getNumElements() == rVector->getNumElements()))
1604 return true;
1605 return false;
1606}
1607
Steve Naroff85f0dc52007-10-15 20:41:53 +00001608// C99 6.2.7p1: If both are complete types, then the following additional
1609// requirements apply...FIXME (handle compatibility across source files).
1610bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
Steve Naroff4a5e2072007-11-07 06:03:51 +00001611 // "Class" and "id" are compatible built-in structure types.
Ted Kremenek42730c52008-01-07 19:49:32 +00001612 if (isObjCIdType(lhs) && isObjCClassType(rhs) ||
1613 isObjCClassType(lhs) && isObjCIdType(rhs))
Steve Naroff4a5e2072007-11-07 06:03:51 +00001614 return true;
Eli Friedmane7fb03a2008-02-15 06:03:44 +00001615
1616 // Within a translation unit a tag type is
1617 // only compatible with itself.
1618 return lhs.getCanonicalType() == rhs.getCanonicalType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001619}
1620
1621bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1622 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1623 // identically qualified and both shall be pointers to compatible types.
1624 if (lhs.getQualifiers() != rhs.getQualifiers())
1625 return false;
1626
1627 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1628 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1629
1630 return typesAreCompatible(ltype, rtype);
1631}
1632
Bill Wendling6a9d8542007-12-03 07:33:35 +00001633// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroff85f0dc52007-10-15 20:41:53 +00001634// reference to T, the operation assigns to the object of type T denoted by the
1635// reference.
1636bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1637 QualType ltype = lhs;
1638
1639 if (lhs->isReferenceType())
1640 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1641
1642 QualType rtype = rhs;
1643
1644 if (rhs->isReferenceType())
1645 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1646
1647 return typesAreCompatible(ltype, rtype);
1648}
1649
1650bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1651 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1652 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1653 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1654 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1655
1656 // first check the return types (common between C99 and K&R).
1657 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1658 return false;
1659
1660 if (lproto && rproto) { // two C99 style function prototypes
1661 unsigned lproto_nargs = lproto->getNumArgs();
1662 unsigned rproto_nargs = rproto->getNumArgs();
1663
1664 if (lproto_nargs != rproto_nargs)
1665 return false;
1666
1667 // both prototypes have the same number of arguments.
1668 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1669 (rproto->isVariadic() && !lproto->isVariadic()))
1670 return false;
1671
1672 // The use of ellipsis agree...now check the argument types.
1673 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001674 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1675 // is taken as having the unqualified version of it's declared type.
Steve Naroffdec17fe2008-01-29 00:15:50 +00001676 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001677 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001678 return false;
1679 return true;
1680 }
1681 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1682 return true;
1683
1684 // we have a mixture of K&R style with C99 prototypes
1685 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1686
1687 if (proto->isVariadic())
1688 return false;
1689
1690 // FIXME: Each parameter type T in the prototype must be compatible with the
1691 // type resulting from applying the usual argument conversions to T.
1692 return true;
1693}
1694
1695bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
Eli Friedman1e7537832008-02-06 04:53:22 +00001696 // Compatible arrays must have compatible element types
1697 QualType ltype = lhs->getAsArrayType()->getElementType();
1698 QualType rtype = rhs->getAsArrayType()->getElementType();
1699
Steve Naroff85f0dc52007-10-15 20:41:53 +00001700 if (!typesAreCompatible(ltype, rtype))
1701 return false;
Eli Friedman1e7537832008-02-06 04:53:22 +00001702
1703 // Compatible arrays must be the same size
1704 if (const ConstantArrayType* LCAT = lhs->getAsConstantArrayType())
1705 if (const ConstantArrayType* RCAT = rhs->getAsConstantArrayType())
1706 return RCAT->getSize() == LCAT->getSize();
1707
Steve Naroff85f0dc52007-10-15 20:41:53 +00001708 return true;
1709}
1710
1711/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1712/// both shall have the identically qualified version of a compatible type.
1713/// C99 6.2.7p1: Two types have compatible types if their types are the
1714/// same. See 6.7.[2,3,5] for additional rules.
1715bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
Steve Naroff577f9722008-01-29 18:58:14 +00001716 if (lhs.getQualifiers() != rhs.getQualifiers())
1717 return false;
1718
Steve Naroff85f0dc52007-10-15 20:41:53 +00001719 QualType lcanon = lhs.getCanonicalType();
1720 QualType rcanon = rhs.getCanonicalType();
1721
1722 // If two types are identical, they are are compatible
1723 if (lcanon == rcanon)
1724 return true;
Bill Wendling6a9d8542007-12-03 07:33:35 +00001725
1726 // C++ [expr]: If an expression initially has the type "reference to T", the
1727 // type is adjusted to "T" prior to any further analysis, the expression
1728 // designates the object or function denoted by the reference, and the
1729 // expression is an lvalue.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001730 if (ReferenceType *RT = dyn_cast<ReferenceType>(lcanon))
1731 lcanon = RT->getReferenceeType();
1732 if (ReferenceType *RT = dyn_cast<ReferenceType>(rcanon))
1733 rcanon = RT->getReferenceeType();
1734
1735 Type::TypeClass LHSClass = lcanon->getTypeClass();
1736 Type::TypeClass RHSClass = rcanon->getTypeClass();
1737
1738 // We want to consider the two function types to be the same for these
1739 // comparisons, just force one to the other.
1740 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1741 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00001742
1743 // Same as above for arrays
1744 if (LHSClass == Type::VariableArray) LHSClass = Type::ConstantArray;
1745 if (RHSClass == Type::VariableArray) RHSClass = Type::ConstantArray;
Eli Friedman8ff07782008-02-15 18:16:39 +00001746 if (LHSClass == Type::IncompleteArray) LHSClass = Type::ConstantArray;
1747 if (RHSClass == Type::IncompleteArray) RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001748
Steve Naroffc88babe2008-01-09 22:43:08 +00001749 // If the canonical type classes don't match...
Chris Lattnerc38d4522008-01-14 05:45:46 +00001750 if (LHSClass != RHSClass) {
Steve Naroff85f0dc52007-10-15 20:41:53 +00001751 // For Objective-C, it is possible for two types to be compatible
1752 // when their classes don't match (when dealing with "id"). If either type
1753 // is an interface, we defer to objcTypesAreCompatible().
Ted Kremenek42730c52008-01-07 19:49:32 +00001754 if (lcanon->isObjCInterfaceType() || rcanon->isObjCInterfaceType())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001755 return objcTypesAreCompatible(lcanon, rcanon);
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001756
Chris Lattnerc38d4522008-01-14 05:45:46 +00001757 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1758 // a signed integer type, or an unsigned integer type.
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001759 if (lcanon->isEnumeralType() && rcanon->isIntegralType()) {
1760 EnumDecl* EDecl = cast<EnumDecl>(cast<TagType>(lcanon)->getDecl());
1761 return EDecl->getIntegerType() == rcanon;
1762 }
1763 if (rcanon->isEnumeralType() && lcanon->isIntegralType()) {
1764 EnumDecl* EDecl = cast<EnumDecl>(cast<TagType>(rcanon)->getDecl());
1765 return EDecl->getIntegerType() == lcanon;
1766 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001767
Steve Naroff85f0dc52007-10-15 20:41:53 +00001768 return false;
1769 }
Steve Naroffc88babe2008-01-09 22:43:08 +00001770 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001771 switch (LHSClass) {
1772 case Type::FunctionProto: assert(0 && "Canonicalized away above");
1773 case Type::Pointer:
1774 return pointerTypesAreCompatible(lcanon, rcanon);
1775 case Type::ConstantArray:
1776 case Type::VariableArray:
Eli Friedman8ff07782008-02-15 18:16:39 +00001777 case Type::IncompleteArray:
Chris Lattnerc38d4522008-01-14 05:45:46 +00001778 return arrayTypesAreCompatible(lcanon, rcanon);
1779 case Type::FunctionNoProto:
1780 return functionTypesAreCompatible(lcanon, rcanon);
1781 case Type::Tagged: // handle structures, unions
1782 return tagTypesAreCompatible(lcanon, rcanon);
1783 case Type::Builtin:
1784 return builtinTypesAreCompatible(lcanon, rcanon);
1785 case Type::ObjCInterface:
1786 return interfaceTypesAreCompatible(lcanon, rcanon);
1787 case Type::Vector:
1788 case Type::OCUVector:
1789 return vectorTypesAreCompatible(lcanon, rcanon);
1790 case Type::ObjCQualifiedInterface:
1791 return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
1792 default:
1793 assert(0 && "unexpected type");
Steve Naroff85f0dc52007-10-15 20:41:53 +00001794 }
1795 return true; // should never get here...
1796}
Ted Kremenek738e6c02007-10-31 17:10:13 +00001797
Ted Kremenek738e6c02007-10-31 17:10:13 +00001798/// Emit - Serialize an ASTContext object to Bitcode.
1799void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00001800 S.EmitRef(SourceMgr);
1801 S.EmitRef(Target);
1802 S.EmitRef(Idents);
1803 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001804
Ted Kremenek68228a92007-10-31 22:44:07 +00001805 // Emit the size of the type vector so that we can reserve that size
1806 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001807 S.EmitInt(Types.size());
1808
Ted Kremenek034a78c2007-11-13 22:02:55 +00001809 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1810 I!=E;++I)
1811 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001812
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001813 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001814}
1815
Ted Kremenekacba3612007-11-13 00:25:37 +00001816ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek68228a92007-10-31 22:44:07 +00001817 SourceManager &SM = D.ReadRef<SourceManager>();
1818 TargetInfo &t = D.ReadRef<TargetInfo>();
1819 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1820 SelectorTable &sels = D.ReadRef<SelectorTable>();
1821
1822 unsigned size_reserve = D.ReadInt();
1823
1824 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1825
Ted Kremenek034a78c2007-11-13 22:02:55 +00001826 for (unsigned i = 0; i < size_reserve; ++i)
1827 Type::Create(*A,i,D);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001828
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001829 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00001830
1831 return A;
1832}