blob: dd8a9d4ecf3cfc4c99c537423394b80a841728bc [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Basic/TargetInfo.h"
18#include "llvm/ADT/SmallVector.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000019#include "llvm/ADT/StringExtras.h"
Ted Kremenek7192f8e2007-10-31 17:10:13 +000020#include "llvm/Bitcode/Serialize.h"
21#include "llvm/Bitcode/Deserialize.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000022
Reid Spencer5f016e22007-07-11 17:01:13 +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;
Chris Lattner6d87fc62007-07-18 05:50:59 +000047 unsigned NumVector = 0, NumComplex = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000048 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
49
50 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000051 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
52 unsigned NumObjCQualifiedIds = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +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;
Chris Lattner6d87fc62007-07-18 05:50:59 +000062 else if (isa<ComplexType>(T))
63 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +000064 else if (isa<ArrayType>(T))
65 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +000066 else if (isa<VectorType>(T))
67 ++NumVector;
Reid Spencer5f016e22007-07-11 17:01:13 +000068 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 Kremeneka526c5c2008-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 Naroff3f128ad2007-09-17 14:16:13 +000089 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +000090 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +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);
Chris Lattner6d87fc62007-07-18 05:50:59 +000098 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +000099 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000100 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 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 Kremeneka526c5c2008-01-07 19:49:32 +0000109 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000110 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000111 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000112 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000113 NumObjCQualifiedIds);
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
115 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000116 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 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
Reid Spencer5f016e22007-07-11 17:01:13 +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 Kremenek9c728dc2007-12-12 22:39:36 +0000136 if (Target.isCharSigned(FullSourceLoc()))
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff7e219e42007-10-15 14:41:52 +0000163
164 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000165 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000166 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000167 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000168 ClassStructType = 0;
169
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000170 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000171
172 // void * type
173 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000174}
175
Chris Lattner464175b2007-07-18 17:52:12 +0000176//===----------------------------------------------------------------------===//
177// Type Sizing and Analysis
178//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000179
180/// getTypeSize - Return the size of the specified type, in bits. This method
181/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000182std::pair<uint64_t, unsigned>
183ASTContext::getTypeInfo(QualType T, SourceLocation L) {
Chris Lattnera7674d82007-07-13 22:13:22 +0000184 T = T.getCanonicalType();
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000185 uint64_t Size;
186 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000187 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000188 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000189 case Type::FunctionNoProto:
190 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000191 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000192 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-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 Lattner030d8842007-07-19 22:06:24 +0000198 std::pair<uint64_t, unsigned> EltInfo =
Steve Narofffb22d962007-08-30 01:06:46 +0000199 getTypeInfo(CAT->getElementType(), L);
200 Size = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000201 Align = EltInfo.second;
202 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000203 }
204 case Type::OCUVector:
Chris Lattner030d8842007-07-19 22:06:24 +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 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000213
Chris Lattnera7674d82007-07-13 22:13:22 +0000214 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 Lattner525a0502007-09-22 18:29:59 +0000217 const llvm::fltSemantics *F;
Chris Lattnera7674d82007-07-13 22:13:22 +0000218 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000219 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000220 case BuiltinType::Void:
221 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000222 case BuiltinType::Bool:
223 Target.getBoolInfo(Size, Align, getFullLoc(L));
224 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000225 case BuiltinType::Char_S:
226 case BuiltinType::Char_U:
227 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000228 case BuiltinType::SChar:
229 Target.getCharInfo(Size, Align, getFullLoc(L));
230 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000231 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000232 case BuiltinType::Short:
233 Target.getShortInfo(Size, Align, getFullLoc(L));
234 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000235 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000236 case BuiltinType::Int:
237 Target.getIntInfo(Size, Align, getFullLoc(L));
238 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000239 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000240 case BuiltinType::Long:
241 Target.getLongInfo(Size, Align, getFullLoc(L));
242 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000243 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-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 Lattnera7674d82007-07-13 22:13:22 +0000256 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000257 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000258 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000259 case Type::ASQual:
260 return getTypeInfo(cast<ASQualType>(T)->getBaseType(), L);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000261 case Type::ObjCQualifiedId:
Chris Lattner6f62c2a2007-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 Lattnera7674d82007-07-13 22:13:22 +0000267 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000268 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000269 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000270 // FIXME: This is wrong for struct layout: a reference in a struct has
271 // pointer size.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000272 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
Chris Lattner5d2a6302007-07-18 18:26:58 +0000273
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 Lattner6cd862c2007-08-27 17:38:00 +0000284 TagType *TT = cast<TagType>(T);
285 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
Devang Patel88a981b2007-11-01 19:11:01 +0000286 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl(), L);
Chris Lattner6cd862c2007-08-27 17:38:00 +0000287 Size = Layout.getSize();
288 Align = Layout.getAlignment();
289 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattnere00b18c2007-08-28 18:24:31 +0000290 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattner6cd862c2007-08-27 17:38:00 +0000291 } else {
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000292 assert(0 && "Unimplemented type sizes!");
Chris Lattner6cd862c2007-08-27 17:38:00 +0000293 }
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000294 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000295 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000296
Chris Lattner464175b2007-07-18 17:52:12 +0000297 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000298 return std::make_pair(Size, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000299}
300
Devang Patel88a981b2007-11-01 19:11:01 +0000301/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000302/// specified record (struct/union/class), which indicates its size and field
303/// position information.
Devang Patel88a981b2007-11-01 19:11:01 +0000304const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D,
305 SourceLocation L) {
Chris Lattner464175b2007-07-18 17:52:12 +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 Patel88a981b2007-11-01 19:11:01 +0000309 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000310 if (Entry) return *Entry;
311
Devang Patel88a981b2007-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 Lattner464175b2007-07-18 17:52:12 +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) {
322 // Layout each field, for now, just sequentially, respecting alignment. In
323 // the future, this will need to be tweakable by targets.
324 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
325 const FieldDecl *FD = D->getMember(i);
Eli Friedman75afb582008-02-06 05:33:51 +0000326 uint64_t FieldSize;
327 unsigned FieldAlign;
328 if (FD->getType()->isIncompleteType()) {
329 // This must be a flexible array member; we can't directly
330 // query getTypeInfo about these, so we figure it out here.
331 // Flexible array members don't have any size, but they
332 // have to be aligned appropriately for their element type.
333 const ArrayType* ATy = FD->getType()->getAsArrayType();
334 FieldAlign = getTypeAlign(ATy->getElementType(), L);
335 FieldSize = 0;
336 } else {
337 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
338 FieldSize = FieldInfo.first;
339 FieldAlign = FieldInfo.second;
340 }
341
Chris Lattner464175b2007-07-18 17:52:12 +0000342 // Round up the current record size to the field's alignment boundary.
343 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
344
345 // Place this field at the current location.
346 FieldOffsets[i] = RecordSize;
347
348 // Reserve space for this field.
349 RecordSize += FieldSize;
350
351 // Remember max struct/class alignment.
352 RecordAlign = std::max(RecordAlign, FieldAlign);
353 }
354
355 // Finally, round the size of the total struct up to the alignment of the
356 // struct itself.
357 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
358 } else {
359 // Union layout just puts each member at the start of the record.
360 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
361 const FieldDecl *FD = D->getMember(i);
362 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
363 uint64_t FieldSize = FieldInfo.first;
364 unsigned FieldAlign = FieldInfo.second;
365
366 // Round up the current record size to the field's alignment boundary.
367 RecordSize = std::max(RecordSize, FieldSize);
368
369 // Place this field at the start of the record.
370 FieldOffsets[i] = 0;
371
372 // Remember max struct/class alignment.
373 RecordAlign = std::max(RecordAlign, FieldAlign);
374 }
375 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000376
377 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
378 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000379}
380
Chris Lattnera7674d82007-07-13 22:13:22 +0000381//===----------------------------------------------------------------------===//
382// Type creation/memoization methods
383//===----------------------------------------------------------------------===//
384
Christopher Lambebb97e92008-02-04 02:31:56 +0000385QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
386 // Check if we've already instantiated an address space qual'd type of this type.
387 llvm::FoldingSetNodeID ID;
388 ASQualType::Profile(ID, T, AddressSpace);
389 void *InsertPos = 0;
390 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
391 return QualType(ASQy, 0);
392
393 // If the base type isn't canonical, this won't be a canonical type either,
394 // so fill in the canonical type field.
395 QualType Canonical;
396 if (!T->isCanonical()) {
397 Canonical = getASQualType(T.getCanonicalType(), AddressSpace);
398
399 // Get the new insert position for the node we care about.
400 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
401 assert(NewIP == 0 && "Shouldn't be in the map!");
402 }
403 ASQualType *New = new ASQualType(T, Canonical, AddressSpace);
404 ASQualTypes.InsertNode(New, InsertPos);
405 Types.push_back(New);
406 return QualType(New, 0);
407}
408
Chris Lattnera7674d82007-07-13 22:13:22 +0000409
Reid Spencer5f016e22007-07-11 17:01:13 +0000410/// getComplexType - Return the uniqued reference to the type for a complex
411/// number with the specified element type.
412QualType ASTContext::getComplexType(QualType T) {
413 // Unique pointers, to guarantee there is only one pointer of a particular
414 // structure.
415 llvm::FoldingSetNodeID ID;
416 ComplexType::Profile(ID, T);
417
418 void *InsertPos = 0;
419 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
420 return QualType(CT, 0);
421
422 // If the pointee type isn't canonical, this won't be a canonical type either,
423 // so fill in the canonical type field.
424 QualType Canonical;
425 if (!T->isCanonical()) {
426 Canonical = getComplexType(T.getCanonicalType());
427
428 // Get the new insert position for the node we care about.
429 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
430 assert(NewIP == 0 && "Shouldn't be in the map!");
431 }
432 ComplexType *New = new ComplexType(T, Canonical);
433 Types.push_back(New);
434 ComplexTypes.InsertNode(New, InsertPos);
435 return QualType(New, 0);
436}
437
438
439/// getPointerType - Return the uniqued reference to the type for a pointer to
440/// the specified type.
441QualType ASTContext::getPointerType(QualType T) {
442 // Unique pointers, to guarantee there is only one pointer of a particular
443 // structure.
444 llvm::FoldingSetNodeID ID;
445 PointerType::Profile(ID, T);
446
447 void *InsertPos = 0;
448 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
449 return QualType(PT, 0);
450
451 // If the pointee type isn't canonical, this won't be a canonical type either,
452 // so fill in the canonical type field.
453 QualType Canonical;
454 if (!T->isCanonical()) {
455 Canonical = getPointerType(T.getCanonicalType());
456
457 // Get the new insert position for the node we care about.
458 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
459 assert(NewIP == 0 && "Shouldn't be in the map!");
460 }
461 PointerType *New = new PointerType(T, Canonical);
462 Types.push_back(New);
463 PointerTypes.InsertNode(New, InsertPos);
464 return QualType(New, 0);
465}
466
467/// getReferenceType - Return the uniqued reference to the type for a reference
468/// to the specified type.
469QualType ASTContext::getReferenceType(QualType T) {
470 // Unique pointers, to guarantee there is only one pointer of a particular
471 // structure.
472 llvm::FoldingSetNodeID ID;
473 ReferenceType::Profile(ID, T);
474
475 void *InsertPos = 0;
476 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
477 return QualType(RT, 0);
478
479 // If the referencee type isn't canonical, this won't be a canonical type
480 // either, so fill in the canonical type field.
481 QualType Canonical;
482 if (!T->isCanonical()) {
483 Canonical = getReferenceType(T.getCanonicalType());
484
485 // Get the new insert position for the node we care about.
486 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
487 assert(NewIP == 0 && "Shouldn't be in the map!");
488 }
489
490 ReferenceType *New = new ReferenceType(T, Canonical);
491 Types.push_back(New);
492 ReferenceTypes.InsertNode(New, InsertPos);
493 return QualType(New, 0);
494}
495
Steve Narofffb22d962007-08-30 01:06:46 +0000496/// getConstantArrayType - Return the unique reference to the type for an
497/// array of the specified element type.
498QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000499 const llvm::APInt &ArySize,
500 ArrayType::ArraySizeModifier ASM,
501 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000502 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000503 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000504
505 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000506 if (ConstantArrayType *ATP =
507 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000508 return QualType(ATP, 0);
509
510 // If the element type isn't canonical, this won't be a canonical type either,
511 // so fill in the canonical type field.
512 QualType Canonical;
513 if (!EltTy->isCanonical()) {
Steve Naroffc9406122007-08-30 18:10:14 +0000514 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
515 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000517 ConstantArrayType *NewIP =
518 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
519
Reid Spencer5f016e22007-07-11 17:01:13 +0000520 assert(NewIP == 0 && "Shouldn't be in the map!");
521 }
522
Steve Naroffc9406122007-08-30 18:10:14 +0000523 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
524 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000525 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000526 Types.push_back(New);
527 return QualType(New, 0);
528}
529
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000530/// getVariableArrayType - Returns a non-unique reference to the type for a
531/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000532QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
533 ArrayType::ArraySizeModifier ASM,
534 unsigned EltTypeQuals) {
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000535 if (NumElts) {
536 // Since we don't unique expressions, it isn't possible to unique VLA's
537 // that have an expression provided for their size.
538
Ted Kremenek347b9f32007-10-30 16:41:53 +0000539 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
540 ASM, EltTypeQuals);
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000541
Ted Kremenek347b9f32007-10-30 16:41:53 +0000542 CompleteVariableArrayTypes.push_back(New);
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000543 Types.push_back(New);
544 return QualType(New, 0);
545 }
546 else {
547 // No size is provided for the VLA. These we can unique.
548 llvm::FoldingSetNodeID ID;
549 VariableArrayType::Profile(ID, EltTy);
550
551 void *InsertPos = 0;
552 if (VariableArrayType *ATP =
553 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
554 return QualType(ATP, 0);
555
556 // If the element type isn't canonical, this won't be a canonical type
557 // either, so fill in the canonical type field.
558 QualType Canonical;
559
560 if (!EltTy->isCanonical()) {
561 Canonical = getVariableArrayType(EltTy.getCanonicalType(), NumElts,
562 ASM, EltTypeQuals);
563
564 // Get the new insert position for the node we care about.
565 VariableArrayType *NewIP =
566 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
567
568 assert(NewIP == 0 && "Shouldn't be in the map!");
569 }
570
571 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
572 ASM, EltTypeQuals);
573
574 IncompleteVariableArrayTypes.InsertNode(New, InsertPos);
575 Types.push_back(New);
576 return QualType(New, 0);
577 }
Steve Narofffb22d962007-08-30 01:06:46 +0000578}
579
Steve Naroff73322922007-07-18 18:00:27 +0000580/// getVectorType - Return the unique reference to a vector type of
581/// the specified element type and size. VectorType must be a built-in type.
582QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 BuiltinType *baseType;
584
585 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000586 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000587
588 // Check if we've already instantiated a vector of this type.
589 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000590 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 void *InsertPos = 0;
592 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
593 return QualType(VTP, 0);
594
595 // If the element type isn't canonical, this won't be a canonical type either,
596 // so fill in the canonical type field.
597 QualType Canonical;
598 if (!vecType->isCanonical()) {
Steve Naroff73322922007-07-18 18:00:27 +0000599 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000600
601 // Get the new insert position for the node we care about.
602 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
603 assert(NewIP == 0 && "Shouldn't be in the map!");
604 }
605 VectorType *New = new VectorType(vecType, NumElts, Canonical);
606 VectorTypes.InsertNode(New, InsertPos);
607 Types.push_back(New);
608 return QualType(New, 0);
609}
610
Steve Naroff73322922007-07-18 18:00:27 +0000611/// getOCUVectorType - Return the unique reference to an OCU vector type of
612/// the specified element type and size. VectorType must be a built-in type.
613QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
614 BuiltinType *baseType;
615
616 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
617 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
618
619 // Check if we've already instantiated a vector of this type.
620 llvm::FoldingSetNodeID ID;
621 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
622 void *InsertPos = 0;
623 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
624 return QualType(VTP, 0);
625
626 // If the element type isn't canonical, this won't be a canonical type either,
627 // so fill in the canonical type field.
628 QualType Canonical;
629 if (!vecType->isCanonical()) {
630 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
631
632 // Get the new insert position for the node we care about.
633 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
634 assert(NewIP == 0 && "Shouldn't be in the map!");
635 }
636 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
637 VectorTypes.InsertNode(New, InsertPos);
638 Types.push_back(New);
639 return QualType(New, 0);
640}
641
Reid Spencer5f016e22007-07-11 17:01:13 +0000642/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
643///
644QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
645 // Unique functions, to guarantee there is only one function of a particular
646 // structure.
647 llvm::FoldingSetNodeID ID;
648 FunctionTypeNoProto::Profile(ID, ResultTy);
649
650 void *InsertPos = 0;
651 if (FunctionTypeNoProto *FT =
652 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
653 return QualType(FT, 0);
654
655 QualType Canonical;
656 if (!ResultTy->isCanonical()) {
657 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
658
659 // Get the new insert position for the node we care about.
660 FunctionTypeNoProto *NewIP =
661 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
662 assert(NewIP == 0 && "Shouldn't be in the map!");
663 }
664
665 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
666 Types.push_back(New);
667 FunctionTypeProtos.InsertNode(New, InsertPos);
668 return QualType(New, 0);
669}
670
671/// getFunctionType - Return a normal function type with a typed argument
672/// list. isVariadic indicates whether the argument list includes '...'.
673QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
674 unsigned NumArgs, bool isVariadic) {
675 // Unique functions, to guarantee there is only one function of a particular
676 // structure.
677 llvm::FoldingSetNodeID ID;
678 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
679
680 void *InsertPos = 0;
681 if (FunctionTypeProto *FTP =
682 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
683 return QualType(FTP, 0);
684
685 // Determine whether the type being created is already canonical or not.
686 bool isCanonical = ResultTy->isCanonical();
687 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
688 if (!ArgArray[i]->isCanonical())
689 isCanonical = false;
690
691 // If this type isn't canonical, get the canonical version of it.
692 QualType Canonical;
693 if (!isCanonical) {
694 llvm::SmallVector<QualType, 16> CanonicalArgs;
695 CanonicalArgs.reserve(NumArgs);
696 for (unsigned i = 0; i != NumArgs; ++i)
697 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
698
699 Canonical = getFunctionType(ResultTy.getCanonicalType(),
700 &CanonicalArgs[0], NumArgs,
701 isVariadic);
702
703 // Get the new insert position for the node we care about.
704 FunctionTypeProto *NewIP =
705 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
706 assert(NewIP == 0 && "Shouldn't be in the map!");
707 }
708
709 // FunctionTypeProto objects are not allocated with new because they have a
710 // variable size array (for parameter types) at the end of them.
711 FunctionTypeProto *FTP =
712 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000713 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
715 Canonical);
716 Types.push_back(FTP);
717 FunctionTypeProtos.InsertNode(FTP, InsertPos);
718 return QualType(FTP, 0);
719}
720
721/// getTypedefType - Return the unique reference to the type for the
722/// specified typename decl.
723QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
724 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
725
726 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000727 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 Types.push_back(Decl->TypeForDecl);
729 return QualType(Decl->TypeForDecl, 0);
730}
731
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000732/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +0000733/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000734QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +0000735 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
736
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000737 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +0000738 Types.push_back(Decl->TypeForDecl);
739 return QualType(Decl->TypeForDecl, 0);
740}
741
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000742/// getObjCQualifiedInterfaceType - Return a
743/// ObjCQualifiedInterfaceType type for the given interface decl and
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000744/// the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000745QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
746 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000747 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000748 ObjCQualifiedInterfaceType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000749
750 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000751 if (ObjCQualifiedInterfaceType *QT =
752 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000753 return QualType(QT, 0);
754
755 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000756 ObjCQualifiedInterfaceType *QType =
757 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000758 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000759 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000760 return QualType(QType, 0);
761}
762
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000763/// getObjCQualifiedIdType - Return a
764/// getObjCQualifiedIdType type for the 'id' decl and
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000765/// the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000766QualType ASTContext::getObjCQualifiedIdType(QualType idType,
767 ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000768 unsigned NumProtocols) {
769 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000770 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000771
772 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000773 if (ObjCQualifiedIdType *QT =
774 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000775 return QualType(QT, 0);
776
777 // No Match;
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000778 QualType Canonical;
779 if (!idType->isCanonical()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000780 Canonical = getObjCQualifiedIdType(idType.getCanonicalType(),
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000781 Protocols, NumProtocols);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000782 ObjCQualifiedIdType *NewQT =
783 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000784 assert(NewQT == 0 && "Shouldn't be in the map!");
785 }
786
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000787 ObjCQualifiedIdType *QType =
788 new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000789 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000790 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000791 return QualType(QType, 0);
792}
793
Steve Naroff9752f252007-08-01 18:02:17 +0000794/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
795/// TypeOfExpr AST's (since expression's are never shared). For example,
796/// multiple declarations that refer to "typeof(x)" all contain different
797/// DeclRefExpr's. This doesn't effect the type checker, since it operates
798/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000799QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroffd1861fd2007-07-31 12:34:36 +0000800 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000801 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
802 Types.push_back(toe);
803 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000804}
805
Steve Naroff9752f252007-08-01 18:02:17 +0000806/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
807/// TypeOfType AST's. The only motivation to unique these nodes would be
808/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
809/// an issue. This doesn't effect the type checker, since it operates
810/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +0000811QualType ASTContext::getTypeOfType(QualType tofType) {
812 QualType Canonical = tofType.getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000813 TypeOfType *tot = new TypeOfType(tofType, Canonical);
814 Types.push_back(tot);
815 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000816}
817
Reid Spencer5f016e22007-07-11 17:01:13 +0000818/// getTagDeclType - Return the unique reference to the type for the
819/// specified TagDecl (struct/union/class/enum) decl.
820QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +0000821 assert (Decl);
822
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000823 // The decl stores the type cache.
Ted Kremenekd778f882007-11-26 21:16:01 +0000824 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000825
826 TagType* T = new TagType(Decl, QualType());
Ted Kremenekd778f882007-11-26 21:16:01 +0000827 Types.push_back(T);
828 Decl->TypeForDecl = T;
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000829
830 return QualType(T, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000831}
832
833/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
834/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
835/// needs to agree with the definition in <stddef.h>.
836QualType ASTContext::getSizeType() const {
837 // On Darwin, size_t is defined as a "long unsigned int".
838 // FIXME: should derive from "Target".
839 return UnsignedLongTy;
840}
841
Chris Lattner8b9023b2007-07-13 03:05:23 +0000842/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
843/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
844QualType ASTContext::getPointerDiffType() const {
845 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
846 // FIXME: should derive from "Target".
847 return IntTy;
848}
849
Reid Spencer5f016e22007-07-11 17:01:13 +0000850/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
851/// routine will assert if passed a built-in type that isn't an integer or enum.
852static int getIntegerRank(QualType t) {
853 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
854 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
855 return 4;
856 }
857
Christopher Lambebb97e92008-02-04 02:31:56 +0000858 const BuiltinType *BT = t.getCanonicalType()->getAsBuiltinType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 switch (BT->getKind()) {
860 default:
861 assert(0 && "getIntegerRank(): not a built-in integer");
862 case BuiltinType::Bool:
863 return 1;
864 case BuiltinType::Char_S:
865 case BuiltinType::Char_U:
866 case BuiltinType::SChar:
867 case BuiltinType::UChar:
868 return 2;
869 case BuiltinType::Short:
870 case BuiltinType::UShort:
871 return 3;
872 case BuiltinType::Int:
873 case BuiltinType::UInt:
874 return 4;
875 case BuiltinType::Long:
876 case BuiltinType::ULong:
877 return 5;
878 case BuiltinType::LongLong:
879 case BuiltinType::ULongLong:
880 return 6;
881 }
882}
883
884/// getFloatingRank - Return a relative rank for floating point types.
885/// This routine will assert if passed a built-in type that isn't a float.
886static int getFloatingRank(QualType T) {
887 T = T.getCanonicalType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000888 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 return getFloatingRank(CT->getElementType());
890
Christopher Lambebb97e92008-02-04 02:31:56 +0000891 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattner770951b2007-11-01 05:03:41 +0000892 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000893 case BuiltinType::Float: return FloatRank;
894 case BuiltinType::Double: return DoubleRank;
895 case BuiltinType::LongDouble: return LongDoubleRank;
896 }
897}
898
Steve Naroff716c7302007-08-27 01:41:48 +0000899/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
900/// point or a complex type (based on typeDomain/typeSize).
901/// 'typeDomain' is a real floating point or complex type.
902/// 'typeSize' is a real floating point or complex type.
Steve Narofff1448a02007-08-27 01:27:54 +0000903QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
904 QualType typeSize, QualType typeDomain) const {
905 if (typeDomain->isComplexType()) {
906 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000907 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000908 case FloatRank: return FloatComplexTy;
909 case DoubleRank: return DoubleComplexTy;
910 case LongDoubleRank: return LongDoubleComplexTy;
911 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000912 }
Steve Narofff1448a02007-08-27 01:27:54 +0000913 if (typeDomain->isRealFloatingType()) {
914 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000915 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000916 case FloatRank: return FloatTy;
917 case DoubleRank: return DoubleTy;
918 case LongDoubleRank: return LongDoubleTy;
919 }
920 }
921 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattnerb1776cb2007-09-16 19:23:47 +0000922 //an invalid return value, but the assert
923 //will ensure that this code is never reached.
924 return VoidTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000925}
926
Steve Narofffb0d4962007-08-27 15:30:22 +0000927/// compareFloatingType - Handles 3 different combos:
928/// float/float, float/complex, complex/complex.
929/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
930int ASTContext::compareFloatingType(QualType lt, QualType rt) {
931 if (getFloatingRank(lt) == getFloatingRank(rt))
932 return 0;
933 if (getFloatingRank(lt) > getFloatingRank(rt))
934 return 1;
935 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000936}
937
938// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
939// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
940QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
941 if (lhs == rhs) return lhs;
942
943 bool t1Unsigned = lhs->isUnsignedIntegerType();
944 bool t2Unsigned = rhs->isUnsignedIntegerType();
945
946 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
947 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
948
949 // We have two integer types with differing signs
950 QualType unsignedType = t1Unsigned ? lhs : rhs;
951 QualType signedType = t1Unsigned ? rhs : lhs;
952
953 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
954 return unsignedType;
955 else {
956 // FIXME: Need to check if the signed type can represent all values of the
957 // unsigned type. If it can, then the result is the signed type.
958 // If it can't, then the result is the unsigned version of the signed type.
959 // Should probably add a helper that returns a signed integer type from
960 // an unsigned (and vice versa). C99 6.3.1.8.
961 return signedType;
962 }
963}
Anders Carlsson71993dd2007-08-17 05:31:46 +0000964
965// getCFConstantStringType - Return the type used for constant CFStrings.
966QualType ASTContext::getCFConstantStringType() {
967 if (!CFConstantStringTypeDecl) {
968 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
Steve Naroffbeaf2992007-11-03 11:27:19 +0000969 &Idents.get("NSConstantString"),
Anders Carlsson71993dd2007-08-17 05:31:46 +0000970 0);
Anders Carlssonf06273f2007-11-19 00:25:30 +0000971 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +0000972
973 // const int *isa;
974 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +0000975 // int flags;
976 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000977 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +0000978 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +0000979 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +0000980 FieldTypes[3] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000981 // Create fields
Anders Carlssonf06273f2007-11-19 00:25:30 +0000982 FieldDecl *FieldDecls[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +0000983
Anders Carlssonf06273f2007-11-19 00:25:30 +0000984 for (unsigned i = 0; i < 4; ++i)
Steve Narofff38661e2007-09-14 02:20:46 +0000985 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +0000986
987 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
988 }
989
990 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +0000991}
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000992
Anders Carlssone8c49532007-10-29 06:33:42 +0000993// This returns true if a type has been typedefed to BOOL:
994// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +0000995static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +0000996 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner2d998332007-10-30 20:27:44 +0000997 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000998
999 return false;
1000}
1001
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001002/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001003/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001004int ASTContext::getObjCEncodingTypeSize(QualType type) {
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001005 SourceLocation Loc;
1006 uint64_t sz = getTypeSize(type, Loc);
1007
1008 // Make all integer and enum types at least as large as an int
1009 if (sz > 0 && type->isIntegralType())
1010 sz = std::max(sz, getTypeSize(IntTy, Loc));
1011 // Treat arrays as pointers, since that's how they're passed in.
1012 else if (type->isArrayType())
1013 sz = getTypeSize(VoidPtrTy, Loc);
1014 return sz / getTypeSize(CharTy, Loc);
1015}
1016
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001017/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001018/// declaration.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001019void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001020 std::string& S)
1021{
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001022 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001023 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001024 // Encode result type.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001025 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001026 // Compute size of all parameters.
1027 // Start with computing size of a pointer in number of bytes.
1028 // FIXME: There might(should) be a better way of doing this computation!
1029 SourceLocation Loc;
1030 int PtrSize = getTypeSize(VoidPtrTy, Loc) / getTypeSize(CharTy, Loc);
1031 // The first two arguments (self and _cmd) are pointers; account for
1032 // their size.
1033 int ParmOffset = 2 * PtrSize;
1034 int NumOfParams = Decl->getNumParams();
1035 for (int i = 0; i < NumOfParams; i++) {
1036 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001037 int sz = getObjCEncodingTypeSize (PType);
1038 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001039 ParmOffset += sz;
1040 }
1041 S += llvm::utostr(ParmOffset);
1042 S += "@0:";
1043 S += llvm::utostr(PtrSize);
1044
1045 // Argument types.
1046 ParmOffset = 2 * PtrSize;
1047 for (int i = 0; i < NumOfParams; i++) {
1048 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001049 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001050 // 'in', 'inout', etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001051 getObjCEncodingForTypeQualifier(
1052 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001053 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001054 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001055 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001056 }
1057}
1058
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001059void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1060 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001061{
Anders Carlssone8c49532007-10-29 06:33:42 +00001062 // FIXME: This currently doesn't encode:
1063 // @ An object (whether statically typed or typed id)
1064 // # A class object (Class)
1065 // : A method selector (SEL)
1066 // {name=type...} A structure
1067 // (name=type...) A union
1068 // bnum A bit field of num bits
1069
1070 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001071 char encoding;
1072 switch (BT->getKind()) {
1073 case BuiltinType::Void:
1074 encoding = 'v';
1075 break;
1076 case BuiltinType::Bool:
1077 encoding = 'B';
1078 break;
1079 case BuiltinType::Char_U:
1080 case BuiltinType::UChar:
1081 encoding = 'C';
1082 break;
1083 case BuiltinType::UShort:
1084 encoding = 'S';
1085 break;
1086 case BuiltinType::UInt:
1087 encoding = 'I';
1088 break;
1089 case BuiltinType::ULong:
1090 encoding = 'L';
1091 break;
1092 case BuiltinType::ULongLong:
1093 encoding = 'Q';
1094 break;
1095 case BuiltinType::Char_S:
1096 case BuiltinType::SChar:
1097 encoding = 'c';
1098 break;
1099 case BuiltinType::Short:
1100 encoding = 's';
1101 break;
1102 case BuiltinType::Int:
1103 encoding = 'i';
1104 break;
1105 case BuiltinType::Long:
1106 encoding = 'l';
1107 break;
1108 case BuiltinType::LongLong:
1109 encoding = 'q';
1110 break;
1111 case BuiltinType::Float:
1112 encoding = 'f';
1113 break;
1114 case BuiltinType::Double:
1115 encoding = 'd';
1116 break;
1117 case BuiltinType::LongDouble:
1118 encoding = 'd';
1119 break;
1120 default:
1121 assert(0 && "Unhandled builtin type kind");
1122 }
1123
1124 S += encoding;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001125 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001126 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001127 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001128 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001129
1130 }
1131 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001132 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001133 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001134 S += '@';
1135 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001136 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001137 S += '#';
1138 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001139 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001140 S += ':';
1141 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001142 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001143
1144 if (PointeeTy->isCharType()) {
1145 // char pointer types should be encoded as '*' unless it is a
1146 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001147 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001148 S += '*';
1149 return;
1150 }
1151 }
1152
1153 S += '^';
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001154 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone8c49532007-10-29 06:33:42 +00001155 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001156 S += '[';
1157
1158 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1159 S += llvm::utostr(CAT->getSize().getZExtValue());
1160 else
1161 assert(0 && "Unhandled array type!");
1162
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001163 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001164 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001165 } else if (T->getAsFunctionType()) {
1166 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001167 } else if (const RecordType *RTy = T->getAsRecordType()) {
1168 RecordDecl *RDecl= RTy->getDecl();
1169 S += '{';
1170 S += RDecl->getName();
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001171 bool found = false;
1172 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1173 if (ERType[i] == RTy) {
1174 found = true;
1175 break;
1176 }
1177 if (!found) {
1178 ERType.push_back(RTy);
1179 S += '=';
1180 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1181 FieldDecl *field = RDecl->getMember(i);
1182 getObjCEncodingForType(field->getType(), S, ERType);
1183 }
1184 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1185 ERType.pop_back();
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001186 }
1187 S += '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001188 } else if (T->isEnumeralType()) {
1189 S += 'i';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001190 } else
Steve Narofff69cc5d2008-01-30 19:17:43 +00001191 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001192}
1193
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001194void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001195 std::string& S) const {
1196 if (QT & Decl::OBJC_TQ_In)
1197 S += 'n';
1198 if (QT & Decl::OBJC_TQ_Inout)
1199 S += 'N';
1200 if (QT & Decl::OBJC_TQ_Out)
1201 S += 'o';
1202 if (QT & Decl::OBJC_TQ_Bycopy)
1203 S += 'O';
1204 if (QT & Decl::OBJC_TQ_Byref)
1205 S += 'R';
1206 if (QT & Decl::OBJC_TQ_Oneway)
1207 S += 'V';
1208}
1209
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001210void ASTContext::setBuiltinVaListType(QualType T)
1211{
1212 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1213
1214 BuiltinVaListType = T;
1215}
1216
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001217void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00001218{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001219 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff7e219e42007-10-15 14:41:52 +00001220
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001221 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00001222
1223 // typedef struct objc_object *id;
1224 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1225 assert(ptr && "'id' incorrectly typed");
1226 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1227 assert(rec && "'id' incorrectly typed");
1228 IdStructType = rec;
1229}
1230
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001231void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001232{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001233 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001234
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001235 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001236
1237 // typedef struct objc_selector *SEL;
1238 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1239 assert(ptr && "'SEL' incorrectly typed");
1240 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1241 assert(rec && "'SEL' incorrectly typed");
1242 SelStructType = rec;
1243}
1244
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001245void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001246{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001247 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1248 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001249}
1250
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001251void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00001252{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001253 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson8baaca52007-10-31 02:53:19 +00001254
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001255 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00001256
1257 // typedef struct objc_class *Class;
1258 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1259 assert(ptr && "'Class' incorrectly typed");
1260 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1261 assert(rec && "'Class' incorrectly typed");
1262 ClassStructType = rec;
1263}
1264
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001265void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1266 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00001267 "'NSConstantString' type already set!");
1268
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001269 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00001270}
1271
Steve Naroffec0550f2007-10-15 20:41:53 +00001272bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1273 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1274 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1275
1276 return lBuiltin->getKind() == rBuiltin->getKind();
1277}
1278
Fariborz Jahanianb145e7d2007-12-21 17:34:43 +00001279/// objcTypesAreCompatible - This routine is called when two types
1280/// are of different class; one is interface type or is
1281/// a qualified interface type and the other type is of a different class.
1282/// Example, II or II<P>.
Steve Naroffec0550f2007-10-15 20:41:53 +00001283bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001284 if (lhs->isObjCInterfaceType() && isObjCIdType(rhs))
Steve Naroffec0550f2007-10-15 20:41:53 +00001285 return true;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001286 else if (isObjCIdType(lhs) && rhs->isObjCInterfaceType())
Steve Naroffec0550f2007-10-15 20:41:53 +00001287 return true;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001288 if (ObjCInterfaceType *lhsIT =
1289 dyn_cast<ObjCInterfaceType>(lhs.getCanonicalType().getTypePtr())) {
1290 ObjCQualifiedInterfaceType *rhsQI =
1291 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanianb145e7d2007-12-21 17:34:43 +00001292 return rhsQI && (lhsIT->getDecl() == rhsQI->getDecl());
1293 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001294 else if (ObjCInterfaceType *rhsIT =
1295 dyn_cast<ObjCInterfaceType>(rhs.getCanonicalType().getTypePtr())) {
1296 ObjCQualifiedInterfaceType *lhsQI =
1297 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanianb145e7d2007-12-21 17:34:43 +00001298 return lhsQI && (rhsIT->getDecl() == lhsQI->getDecl());
1299 }
Steve Naroffec0550f2007-10-15 20:41:53 +00001300 return false;
1301}
1302
Fariborz Jahanianc5ae5cf2008-01-07 20:12:21 +00001303/// Check that 'lhs' and 'rhs' are compatible interface types. Both types
1304/// must be canonical types.
Steve Naroffec0550f2007-10-15 20:41:53 +00001305bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
Fariborz Jahanianc5ae5cf2008-01-07 20:12:21 +00001306 assert (lhs->isCanonical() &&
1307 "interfaceTypesAreCompatible strip typedefs of lhs");
1308 assert (rhs->isCanonical() &&
1309 "interfaceTypesAreCompatible strip typedefs of rhs");
Fariborz Jahanian0f01deb2007-12-20 22:37:58 +00001310 if (lhs == rhs)
1311 return true;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001312 ObjCInterfaceType *lhsIT = cast<ObjCInterfaceType>(lhs.getTypePtr());
1313 ObjCInterfaceType *rhsIT = cast<ObjCInterfaceType>(rhs.getTypePtr());
1314 ObjCInterfaceDecl *rhsIDecl = rhsIT->getDecl();
1315 ObjCInterfaceDecl *lhsIDecl = lhsIT->getDecl();
Fariborz Jahanian0f01deb2007-12-20 22:37:58 +00001316 // rhs is derived from lhs it is OK; else it is not OK.
1317 while (rhsIDecl != NULL) {
1318 if (rhsIDecl == lhsIDecl)
1319 return true;
1320 rhsIDecl = rhsIDecl->getSuperClass();
1321 }
1322 return false;
Steve Naroffec0550f2007-10-15 20:41:53 +00001323}
1324
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001325bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
1326 QualType rhs) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001327 ObjCQualifiedInterfaceType *lhsQI =
1328 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001329 assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001330 ObjCQualifiedInterfaceType *rhsQI =
1331 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001332 assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
Fariborz Jahanianc5ae5cf2008-01-07 20:12:21 +00001333 if (!interfaceTypesAreCompatible(
1334 getObjCInterfaceType(lhsQI->getDecl()).getCanonicalType(),
1335 getObjCInterfaceType(rhsQI->getDecl()).getCanonicalType()))
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001336 return false;
1337 /* All protocols in lhs must have a presense in rhs. */
1338 for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
1339 bool match = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001340 ObjCProtocolDecl *lhsProto = lhsQI->getProtocols(i);
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001341 for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001342 ObjCProtocolDecl *rhsProto = rhsQI->getProtocols(j);
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001343 if (lhsProto == rhsProto) {
1344 match = true;
1345 break;
1346 }
1347 }
1348 if (!match)
1349 return false;
1350 }
1351 return true;
1352}
1353
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001354/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
1355/// inheritance hierarchy of 'rProto'.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001356static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
1357 ObjCProtocolDecl *rProto) {
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001358 if (lProto == rProto)
1359 return true;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001360 ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001361 for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
1362 if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
1363 return true;
1364 return false;
1365}
1366
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001367/// ClassImplementsProtocol - Checks that 'lProto' protocol
1368/// has been implemented in IDecl class, its super class or categories (if
1369/// lookupCategory is true).
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001370static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1371 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001372 bool lookupCategory) {
1373
1374 // 1st, look up the class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001375 ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols();
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001376 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
1377 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1378 return true;
1379 }
1380
1381 // 2nd, look up the category.
1382 if (lookupCategory)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001383 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001384 CDecl = CDecl->getNextClassCategory()) {
1385 protoList = CDecl->getReferencedProtocols();
1386 for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
1387 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1388 return true;
1389 }
1390 }
1391
1392 // 3rd, look up the super class(s)
1393 if (IDecl->getSuperClass())
1394 return
1395 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory);
1396
1397 return false;
1398}
1399
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001400/// ObjCQualifiedIdTypesAreCompatible - Compares two types, at least
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001401/// one of which is a protocol qualified 'id' type. When 'compare'
1402/// is true it is for comparison; when false, for assignment/initialization.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001403bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs,
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001404 QualType rhs,
1405 bool compare) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001406 // match id<P..> with an 'id' type in all cases.
1407 if (const PointerType *PT = lhs->getAsPointerType()) {
1408 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001409 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001410 return true;
1411
1412 }
1413 else if (const PointerType *PT = rhs->getAsPointerType()) {
1414 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001415 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001416 return true;
1417
1418 }
1419
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001420 ObjCQualifiedInterfaceType *lhsQI = 0;
1421 ObjCQualifiedInterfaceType *rhsQI = 0;
1422 ObjCInterfaceDecl *lhsID = 0;
1423 ObjCInterfaceDecl *rhsID = 0;
1424 ObjCQualifiedIdType *lhsQID = dyn_cast<ObjCQualifiedIdType>(lhs);
1425 ObjCQualifiedIdType *rhsQID = dyn_cast<ObjCQualifiedIdType>(rhs);
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001426
1427 if (lhsQID) {
1428 if (!rhsQID && rhs->getTypeClass() == Type::Pointer) {
1429 QualType rtype =
1430 cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1431 rhsQI =
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001432 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001433 rtype.getCanonicalType().getTypePtr());
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001434 if (!rhsQI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001435 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001436 rtype.getCanonicalType().getTypePtr());
1437 if (IT)
1438 rhsID = IT->getDecl();
1439 }
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001440 }
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001441 if (!rhsQI && !rhsQID && !rhsID)
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001442 return false;
1443
Fariborz Jahanianbca14a22008-01-03 20:01:35 +00001444 unsigned numRhsProtocols = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001445 ObjCProtocolDecl **rhsProtoList = 0;
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001446 if (rhsQI) {
1447 numRhsProtocols = rhsQI->getNumProtocols();
1448 rhsProtoList = rhsQI->getReferencedProtocols();
1449 }
1450 else if (rhsQID) {
1451 numRhsProtocols = rhsQID->getNumProtocols();
1452 rhsProtoList = rhsQID->getReferencedProtocols();
1453 }
1454
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001455 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001456 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001457 bool match = false;
1458
1459 // when comparing an id<P> on lhs with a static type on rhs,
1460 // see if static class implements all of id's protocols, directly or
1461 // through its super class and categories.
1462 if (rhsID) {
1463 if (ClassImplementsProtocol(lhsProto, rhsID, true))
1464 match = true;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001465 }
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001466 else for (unsigned j = 0; j < numRhsProtocols; j++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001467 ObjCProtocolDecl *rhsProto = rhsProtoList[j];
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001468 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1469 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001470 match = true;
1471 break;
1472 }
1473 }
1474 if (!match)
1475 return false;
1476 }
1477 }
1478 else if (rhsQID) {
1479 if (!lhsQID && lhs->getTypeClass() == Type::Pointer) {
1480 QualType ltype =
1481 cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1482 lhsQI =
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001483 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001484 ltype.getCanonicalType().getTypePtr());
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001485 if (!lhsQI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001486 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001487 ltype.getCanonicalType().getTypePtr());
1488 if (IT)
1489 lhsID = IT->getDecl();
1490 }
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001491 }
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001492 if (!lhsQI && !lhsQID && !lhsID)
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001493 return false;
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001494
Fariborz Jahanianbca14a22008-01-03 20:01:35 +00001495 unsigned numLhsProtocols = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001496 ObjCProtocolDecl **lhsProtoList = 0;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001497 if (lhsQI) {
1498 numLhsProtocols = lhsQI->getNumProtocols();
1499 lhsProtoList = lhsQI->getReferencedProtocols();
1500 }
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001501 else if (lhsQID) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001502 numLhsProtocols = lhsQID->getNumProtocols();
1503 lhsProtoList = lhsQID->getReferencedProtocols();
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001504 }
1505 bool match = false;
1506 // for static type vs. qualified 'id' type, check that class implements
1507 // one of 'id's protocols.
1508 if (lhsID) {
1509 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001510 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001511 if (ClassImplementsProtocol(rhsProto, lhsID, compare)) {
1512 match = true;
1513 break;
1514 }
1515 }
1516 }
1517 else for (unsigned i =0; i < numLhsProtocols; i++) {
1518 match = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001519 ObjCProtocolDecl *lhsProto = lhsProtoList[i];
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001520 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001521 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001522 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1523 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001524 match = true;
1525 break;
1526 }
1527 }
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001528 }
1529 if (!match)
1530 return false;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001531 }
1532 return true;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001533}
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001534
Chris Lattner770951b2007-11-01 05:03:41 +00001535bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1536 const VectorType *lVector = lhs->getAsVectorType();
1537 const VectorType *rVector = rhs->getAsVectorType();
1538
1539 if ((lVector->getElementType().getCanonicalType() ==
1540 rVector->getElementType().getCanonicalType()) &&
1541 (lVector->getNumElements() == rVector->getNumElements()))
1542 return true;
1543 return false;
1544}
1545
Steve Naroffec0550f2007-10-15 20:41:53 +00001546// C99 6.2.7p1: If both are complete types, then the following additional
1547// requirements apply...FIXME (handle compatibility across source files).
1548bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
1549 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
1550 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
1551
1552 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
1553 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1554 return true;
1555 }
1556 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
1557 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1558 return true;
1559 }
Steve Naroffab373092007-11-07 06:03:51 +00001560 // "Class" and "id" are compatible built-in structure types.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001561 if (isObjCIdType(lhs) && isObjCClassType(rhs) ||
1562 isObjCClassType(lhs) && isObjCIdType(rhs))
Steve Naroffab373092007-11-07 06:03:51 +00001563 return true;
Steve Naroffec0550f2007-10-15 20:41:53 +00001564 return false;
1565}
1566
1567bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1568 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1569 // identically qualified and both shall be pointers to compatible types.
1570 if (lhs.getQualifiers() != rhs.getQualifiers())
1571 return false;
1572
1573 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1574 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1575
1576 return typesAreCompatible(ltype, rtype);
1577}
1578
Bill Wendling43d69752007-12-03 07:33:35 +00001579// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroffec0550f2007-10-15 20:41:53 +00001580// reference to T, the operation assigns to the object of type T denoted by the
1581// reference.
1582bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1583 QualType ltype = lhs;
1584
1585 if (lhs->isReferenceType())
1586 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1587
1588 QualType rtype = rhs;
1589
1590 if (rhs->isReferenceType())
1591 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1592
1593 return typesAreCompatible(ltype, rtype);
1594}
1595
1596bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1597 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1598 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1599 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1600 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1601
1602 // first check the return types (common between C99 and K&R).
1603 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1604 return false;
1605
1606 if (lproto && rproto) { // two C99 style function prototypes
1607 unsigned lproto_nargs = lproto->getNumArgs();
1608 unsigned rproto_nargs = rproto->getNumArgs();
1609
1610 if (lproto_nargs != rproto_nargs)
1611 return false;
1612
1613 // both prototypes have the same number of arguments.
1614 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1615 (rproto->isVariadic() && !lproto->isVariadic()))
1616 return false;
1617
1618 // The use of ellipsis agree...now check the argument types.
1619 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Narofff69cc5d2008-01-30 19:17:43 +00001620 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1621 // is taken as having the unqualified version of it's declared type.
Steve Naroffba03eda2008-01-29 00:15:50 +00001622 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Narofff69cc5d2008-01-30 19:17:43 +00001623 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroffec0550f2007-10-15 20:41:53 +00001624 return false;
1625 return true;
1626 }
1627 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1628 return true;
1629
1630 // we have a mixture of K&R style with C99 prototypes
1631 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1632
1633 if (proto->isVariadic())
1634 return false;
1635
1636 // FIXME: Each parameter type T in the prototype must be compatible with the
1637 // type resulting from applying the usual argument conversions to T.
1638 return true;
1639}
1640
1641bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
Eli Friedman4e92acf2008-02-06 04:53:22 +00001642 // Compatible arrays must have compatible element types
1643 QualType ltype = lhs->getAsArrayType()->getElementType();
1644 QualType rtype = rhs->getAsArrayType()->getElementType();
1645
Steve Naroffec0550f2007-10-15 20:41:53 +00001646 if (!typesAreCompatible(ltype, rtype))
1647 return false;
Eli Friedman4e92acf2008-02-06 04:53:22 +00001648
1649 // Compatible arrays must be the same size
1650 if (const ConstantArrayType* LCAT = lhs->getAsConstantArrayType())
1651 if (const ConstantArrayType* RCAT = rhs->getAsConstantArrayType())
1652 return RCAT->getSize() == LCAT->getSize();
1653
Steve Naroffec0550f2007-10-15 20:41:53 +00001654 return true;
1655}
1656
1657/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1658/// both shall have the identically qualified version of a compatible type.
1659/// C99 6.2.7p1: Two types have compatible types if their types are the
1660/// same. See 6.7.[2,3,5] for additional rules.
1661bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
Steve Naroff2565eef2008-01-29 18:58:14 +00001662 if (lhs.getQualifiers() != rhs.getQualifiers())
1663 return false;
1664
Steve Naroffec0550f2007-10-15 20:41:53 +00001665 QualType lcanon = lhs.getCanonicalType();
1666 QualType rcanon = rhs.getCanonicalType();
1667
1668 // If two types are identical, they are are compatible
1669 if (lcanon == rcanon)
1670 return true;
Bill Wendling43d69752007-12-03 07:33:35 +00001671
1672 // C++ [expr]: If an expression initially has the type "reference to T", the
1673 // type is adjusted to "T" prior to any further analysis, the expression
1674 // designates the object or function denoted by the reference, and the
1675 // expression is an lvalue.
Chris Lattner1adb8832008-01-14 05:45:46 +00001676 if (ReferenceType *RT = dyn_cast<ReferenceType>(lcanon))
1677 lcanon = RT->getReferenceeType();
1678 if (ReferenceType *RT = dyn_cast<ReferenceType>(rcanon))
1679 rcanon = RT->getReferenceeType();
1680
1681 Type::TypeClass LHSClass = lcanon->getTypeClass();
1682 Type::TypeClass RHSClass = rcanon->getTypeClass();
1683
1684 // We want to consider the two function types to be the same for these
1685 // comparisons, just force one to the other.
1686 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1687 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00001688
1689 // Same as above for arrays
1690 if (LHSClass == Type::VariableArray) LHSClass = Type::ConstantArray;
1691 if (RHSClass == Type::VariableArray) RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00001692
Steve Naroff4a746782008-01-09 22:43:08 +00001693 // If the canonical type classes don't match...
Chris Lattner1adb8832008-01-14 05:45:46 +00001694 if (LHSClass != RHSClass) {
Steve Naroffec0550f2007-10-15 20:41:53 +00001695 // For Objective-C, it is possible for two types to be compatible
1696 // when their classes don't match (when dealing with "id"). If either type
1697 // is an interface, we defer to objcTypesAreCompatible().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001698 if (lcanon->isObjCInterfaceType() || rcanon->isObjCInterfaceType())
Steve Naroffec0550f2007-10-15 20:41:53 +00001699 return objcTypesAreCompatible(lcanon, rcanon);
Steve Narofff69cc5d2008-01-30 19:17:43 +00001700
Chris Lattner1adb8832008-01-14 05:45:46 +00001701 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1702 // a signed integer type, or an unsigned integer type.
1703 // FIXME: need to check the size and ensure it's the same.
1704 if ((lcanon->isEnumeralType() && rcanon->isIntegralType()) ||
1705 (rcanon->isEnumeralType() && lcanon->isIntegralType()))
1706 return true;
1707
Steve Naroffec0550f2007-10-15 20:41:53 +00001708 return false;
1709 }
Steve Naroff4a746782008-01-09 22:43:08 +00001710 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00001711 switch (LHSClass) {
1712 case Type::FunctionProto: assert(0 && "Canonicalized away above");
1713 case Type::Pointer:
1714 return pointerTypesAreCompatible(lcanon, rcanon);
1715 case Type::ConstantArray:
1716 case Type::VariableArray:
1717 return arrayTypesAreCompatible(lcanon, rcanon);
1718 case Type::FunctionNoProto:
1719 return functionTypesAreCompatible(lcanon, rcanon);
1720 case Type::Tagged: // handle structures, unions
1721 return tagTypesAreCompatible(lcanon, rcanon);
1722 case Type::Builtin:
1723 return builtinTypesAreCompatible(lcanon, rcanon);
1724 case Type::ObjCInterface:
1725 return interfaceTypesAreCompatible(lcanon, rcanon);
1726 case Type::Vector:
1727 case Type::OCUVector:
1728 return vectorTypesAreCompatible(lcanon, rcanon);
1729 case Type::ObjCQualifiedInterface:
1730 return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
1731 default:
1732 assert(0 && "unexpected type");
Steve Naroffec0550f2007-10-15 20:41:53 +00001733 }
1734 return true; // should never get here...
1735}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001736
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001737/// Emit - Serialize an ASTContext object to Bitcode.
1738void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek54513502007-10-31 20:00:03 +00001739 S.EmitRef(SourceMgr);
1740 S.EmitRef(Target);
1741 S.EmitRef(Idents);
1742 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001743
Ted Kremenekfee04522007-10-31 22:44:07 +00001744 // Emit the size of the type vector so that we can reserve that size
1745 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00001746 S.EmitInt(Types.size());
1747
Ted Kremenek03ed4402007-11-13 22:02:55 +00001748 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1749 I!=E;++I)
1750 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001751
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001752 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001753}
1754
Ted Kremenek0f84c002007-11-13 00:25:37 +00001755ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenekfee04522007-10-31 22:44:07 +00001756 SourceManager &SM = D.ReadRef<SourceManager>();
1757 TargetInfo &t = D.ReadRef<TargetInfo>();
1758 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1759 SelectorTable &sels = D.ReadRef<SelectorTable>();
1760
1761 unsigned size_reserve = D.ReadInt();
1762
1763 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1764
Ted Kremenek03ed4402007-11-13 22:02:55 +00001765 for (unsigned i = 0; i < size_reserve; ++i)
1766 Type::Create(*A,i,D);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001767
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001768 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00001769
1770 return A;
1771}