blob: 608858b55fd16dbcbd0975572e937dd5e335c222 [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) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000535 // Since we don't unique expressions, it isn't possible to unique VLA's
536 // that have an expression provided for their size.
537
538 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
539 ASM, EltTypeQuals);
540
541 VariableArrayTypes.push_back(New);
542 Types.push_back(New);
543 return QualType(New, 0);
544}
545
546QualType ASTContext::getIncompleteArrayType(QualType EltTy,
547 ArrayType::ArraySizeModifier ASM,
548 unsigned EltTypeQuals) {
549 llvm::FoldingSetNodeID ID;
550 IncompleteArrayType::Profile(ID, EltTy);
551
552 void *InsertPos = 0;
553 if (IncompleteArrayType *ATP =
554 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
555 return QualType(ATP, 0);
556
557 // If the element type isn't canonical, this won't be a canonical type
558 // either, so fill in the canonical type field.
559 QualType Canonical;
560
561 if (!EltTy->isCanonical()) {
562 Canonical = getIncompleteArrayType(EltTy.getCanonicalType(),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000563 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000564
565 // Get the new insert position for the node we care about.
566 IncompleteArrayType *NewIP =
567 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
568
569 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000570 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000571
572 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
573 ASM, EltTypeQuals);
574
575 IncompleteArrayTypes.InsertNode(New, InsertPos);
576 Types.push_back(New);
577 return QualType(New, 0);
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
Eli Friedmanfd888a52008-02-12 08:29:21 +0000842/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
843/// width of characters in wide strings, The value is target dependent and
844/// needs to agree with the definition in <stddef.h>.
845QualType ASTContext::getWcharType() const {
846 // On Darwin, wchar_t is defined as a "int".
847 // FIXME: should derive from "Target".
848 return IntTy;
849}
850
Chris Lattner8b9023b2007-07-13 03:05:23 +0000851/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
852/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
853QualType ASTContext::getPointerDiffType() const {
854 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
855 // FIXME: should derive from "Target".
856 return IntTy;
857}
858
Reid Spencer5f016e22007-07-11 17:01:13 +0000859/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
860/// routine will assert if passed a built-in type that isn't an integer or enum.
861static int getIntegerRank(QualType t) {
862 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
863 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
864 return 4;
865 }
866
Christopher Lambebb97e92008-02-04 02:31:56 +0000867 const BuiltinType *BT = t.getCanonicalType()->getAsBuiltinType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000868 switch (BT->getKind()) {
869 default:
870 assert(0 && "getIntegerRank(): not a built-in integer");
871 case BuiltinType::Bool:
872 return 1;
873 case BuiltinType::Char_S:
874 case BuiltinType::Char_U:
875 case BuiltinType::SChar:
876 case BuiltinType::UChar:
877 return 2;
878 case BuiltinType::Short:
879 case BuiltinType::UShort:
880 return 3;
881 case BuiltinType::Int:
882 case BuiltinType::UInt:
883 return 4;
884 case BuiltinType::Long:
885 case BuiltinType::ULong:
886 return 5;
887 case BuiltinType::LongLong:
888 case BuiltinType::ULongLong:
889 return 6;
890 }
891}
892
893/// getFloatingRank - Return a relative rank for floating point types.
894/// This routine will assert if passed a built-in type that isn't a float.
895static int getFloatingRank(QualType T) {
896 T = T.getCanonicalType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000897 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 return getFloatingRank(CT->getElementType());
899
Christopher Lambebb97e92008-02-04 02:31:56 +0000900 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattner770951b2007-11-01 05:03:41 +0000901 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000902 case BuiltinType::Float: return FloatRank;
903 case BuiltinType::Double: return DoubleRank;
904 case BuiltinType::LongDouble: return LongDoubleRank;
905 }
906}
907
Steve Naroff716c7302007-08-27 01:41:48 +0000908/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
909/// point or a complex type (based on typeDomain/typeSize).
910/// 'typeDomain' is a real floating point or complex type.
911/// 'typeSize' is a real floating point or complex type.
Steve Narofff1448a02007-08-27 01:27:54 +0000912QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
913 QualType typeSize, QualType typeDomain) const {
914 if (typeDomain->isComplexType()) {
915 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000916 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000917 case FloatRank: return FloatComplexTy;
918 case DoubleRank: return DoubleComplexTy;
919 case LongDoubleRank: return LongDoubleComplexTy;
920 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000921 }
Steve Narofff1448a02007-08-27 01:27:54 +0000922 if (typeDomain->isRealFloatingType()) {
923 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000924 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000925 case FloatRank: return FloatTy;
926 case DoubleRank: return DoubleTy;
927 case LongDoubleRank: return LongDoubleTy;
928 }
929 }
930 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattnerb1776cb2007-09-16 19:23:47 +0000931 //an invalid return value, but the assert
932 //will ensure that this code is never reached.
933 return VoidTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000934}
935
Steve Narofffb0d4962007-08-27 15:30:22 +0000936/// compareFloatingType - Handles 3 different combos:
937/// float/float, float/complex, complex/complex.
938/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
939int ASTContext::compareFloatingType(QualType lt, QualType rt) {
940 if (getFloatingRank(lt) == getFloatingRank(rt))
941 return 0;
942 if (getFloatingRank(lt) > getFloatingRank(rt))
943 return 1;
944 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000945}
946
947// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
948// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
949QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
950 if (lhs == rhs) return lhs;
951
952 bool t1Unsigned = lhs->isUnsignedIntegerType();
953 bool t2Unsigned = rhs->isUnsignedIntegerType();
954
955 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
956 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
957
958 // We have two integer types with differing signs
959 QualType unsignedType = t1Unsigned ? lhs : rhs;
960 QualType signedType = t1Unsigned ? rhs : lhs;
961
962 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
963 return unsignedType;
964 else {
965 // FIXME: Need to check if the signed type can represent all values of the
966 // unsigned type. If it can, then the result is the signed type.
967 // If it can't, then the result is the unsigned version of the signed type.
968 // Should probably add a helper that returns a signed integer type from
969 // an unsigned (and vice versa). C99 6.3.1.8.
970 return signedType;
971 }
972}
Anders Carlsson71993dd2007-08-17 05:31:46 +0000973
974// getCFConstantStringType - Return the type used for constant CFStrings.
975QualType ASTContext::getCFConstantStringType() {
976 if (!CFConstantStringTypeDecl) {
977 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
Steve Naroffbeaf2992007-11-03 11:27:19 +0000978 &Idents.get("NSConstantString"),
Anders Carlsson71993dd2007-08-17 05:31:46 +0000979 0);
Anders Carlssonf06273f2007-11-19 00:25:30 +0000980 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +0000981
982 // const int *isa;
983 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +0000984 // int flags;
985 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000986 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +0000987 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +0000988 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +0000989 FieldTypes[3] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000990 // Create fields
Anders Carlssonf06273f2007-11-19 00:25:30 +0000991 FieldDecl *FieldDecls[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +0000992
Anders Carlssonf06273f2007-11-19 00:25:30 +0000993 for (unsigned i = 0; i < 4; ++i)
Steve Narofff38661e2007-09-14 02:20:46 +0000994 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +0000995
996 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
997 }
998
999 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001000}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001001
Anders Carlssone8c49532007-10-29 06:33:42 +00001002// This returns true if a type has been typedefed to BOOL:
1003// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001004static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001005 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner2d998332007-10-30 20:27:44 +00001006 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001007
1008 return false;
1009}
1010
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001011/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001012/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001013int ASTContext::getObjCEncodingTypeSize(QualType type) {
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001014 SourceLocation Loc;
1015 uint64_t sz = getTypeSize(type, Loc);
1016
1017 // Make all integer and enum types at least as large as an int
1018 if (sz > 0 && type->isIntegralType())
1019 sz = std::max(sz, getTypeSize(IntTy, Loc));
1020 // Treat arrays as pointers, since that's how they're passed in.
1021 else if (type->isArrayType())
1022 sz = getTypeSize(VoidPtrTy, Loc);
1023 return sz / getTypeSize(CharTy, Loc);
1024}
1025
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001026/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001027/// declaration.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001028void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001029 std::string& S)
1030{
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001031 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001032 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001033 // Encode result type.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001034 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001035 // Compute size of all parameters.
1036 // Start with computing size of a pointer in number of bytes.
1037 // FIXME: There might(should) be a better way of doing this computation!
1038 SourceLocation Loc;
1039 int PtrSize = getTypeSize(VoidPtrTy, Loc) / getTypeSize(CharTy, Loc);
1040 // The first two arguments (self and _cmd) are pointers; account for
1041 // their size.
1042 int ParmOffset = 2 * PtrSize;
1043 int NumOfParams = Decl->getNumParams();
1044 for (int i = 0; i < NumOfParams; i++) {
1045 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001046 int sz = getObjCEncodingTypeSize (PType);
1047 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001048 ParmOffset += sz;
1049 }
1050 S += llvm::utostr(ParmOffset);
1051 S += "@0:";
1052 S += llvm::utostr(PtrSize);
1053
1054 // Argument types.
1055 ParmOffset = 2 * PtrSize;
1056 for (int i = 0; i < NumOfParams; i++) {
1057 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001058 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001059 // 'in', 'inout', etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001060 getObjCEncodingForTypeQualifier(
1061 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001062 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001063 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001064 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001065 }
1066}
1067
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001068void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1069 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001070{
Anders Carlssone8c49532007-10-29 06:33:42 +00001071 // FIXME: This currently doesn't encode:
1072 // @ An object (whether statically typed or typed id)
1073 // # A class object (Class)
1074 // : A method selector (SEL)
1075 // {name=type...} A structure
1076 // (name=type...) A union
1077 // bnum A bit field of num bits
1078
1079 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001080 char encoding;
1081 switch (BT->getKind()) {
1082 case BuiltinType::Void:
1083 encoding = 'v';
1084 break;
1085 case BuiltinType::Bool:
1086 encoding = 'B';
1087 break;
1088 case BuiltinType::Char_U:
1089 case BuiltinType::UChar:
1090 encoding = 'C';
1091 break;
1092 case BuiltinType::UShort:
1093 encoding = 'S';
1094 break;
1095 case BuiltinType::UInt:
1096 encoding = 'I';
1097 break;
1098 case BuiltinType::ULong:
1099 encoding = 'L';
1100 break;
1101 case BuiltinType::ULongLong:
1102 encoding = 'Q';
1103 break;
1104 case BuiltinType::Char_S:
1105 case BuiltinType::SChar:
1106 encoding = 'c';
1107 break;
1108 case BuiltinType::Short:
1109 encoding = 's';
1110 break;
1111 case BuiltinType::Int:
1112 encoding = 'i';
1113 break;
1114 case BuiltinType::Long:
1115 encoding = 'l';
1116 break;
1117 case BuiltinType::LongLong:
1118 encoding = 'q';
1119 break;
1120 case BuiltinType::Float:
1121 encoding = 'f';
1122 break;
1123 case BuiltinType::Double:
1124 encoding = 'd';
1125 break;
1126 case BuiltinType::LongDouble:
1127 encoding = 'd';
1128 break;
1129 default:
1130 assert(0 && "Unhandled builtin type kind");
1131 }
1132
1133 S += encoding;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001134 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001135 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001136 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001137 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001138
1139 }
1140 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001141 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001142 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001143 S += '@';
1144 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001145 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001146 S += '#';
1147 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001148 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001149 S += ':';
1150 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001151 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001152
1153 if (PointeeTy->isCharType()) {
1154 // char pointer types should be encoded as '*' unless it is a
1155 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001156 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001157 S += '*';
1158 return;
1159 }
1160 }
1161
1162 S += '^';
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001163 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone8c49532007-10-29 06:33:42 +00001164 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001165 S += '[';
1166
1167 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1168 S += llvm::utostr(CAT->getSize().getZExtValue());
1169 else
1170 assert(0 && "Unhandled array type!");
1171
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001172 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001173 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001174 } else if (T->getAsFunctionType()) {
1175 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001176 } else if (const RecordType *RTy = T->getAsRecordType()) {
1177 RecordDecl *RDecl= RTy->getDecl();
1178 S += '{';
1179 S += RDecl->getName();
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001180 bool found = false;
1181 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1182 if (ERType[i] == RTy) {
1183 found = true;
1184 break;
1185 }
1186 if (!found) {
1187 ERType.push_back(RTy);
1188 S += '=';
1189 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1190 FieldDecl *field = RDecl->getMember(i);
1191 getObjCEncodingForType(field->getType(), S, ERType);
1192 }
1193 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1194 ERType.pop_back();
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001195 }
1196 S += '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001197 } else if (T->isEnumeralType()) {
1198 S += 'i';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001199 } else
Steve Narofff69cc5d2008-01-30 19:17:43 +00001200 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001201}
1202
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001203void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001204 std::string& S) const {
1205 if (QT & Decl::OBJC_TQ_In)
1206 S += 'n';
1207 if (QT & Decl::OBJC_TQ_Inout)
1208 S += 'N';
1209 if (QT & Decl::OBJC_TQ_Out)
1210 S += 'o';
1211 if (QT & Decl::OBJC_TQ_Bycopy)
1212 S += 'O';
1213 if (QT & Decl::OBJC_TQ_Byref)
1214 S += 'R';
1215 if (QT & Decl::OBJC_TQ_Oneway)
1216 S += 'V';
1217}
1218
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001219void ASTContext::setBuiltinVaListType(QualType T)
1220{
1221 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1222
1223 BuiltinVaListType = T;
1224}
1225
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001226void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00001227{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001228 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff7e219e42007-10-15 14:41:52 +00001229
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001230 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00001231
1232 // typedef struct objc_object *id;
1233 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1234 assert(ptr && "'id' incorrectly typed");
1235 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1236 assert(rec && "'id' incorrectly typed");
1237 IdStructType = rec;
1238}
1239
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001240void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001241{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001242 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001243
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001244 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001245
1246 // typedef struct objc_selector *SEL;
1247 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1248 assert(ptr && "'SEL' incorrectly typed");
1249 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1250 assert(rec && "'SEL' incorrectly typed");
1251 SelStructType = rec;
1252}
1253
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001254void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001255{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001256 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1257 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001258}
1259
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001260void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00001261{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001262 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson8baaca52007-10-31 02:53:19 +00001263
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001264 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00001265
1266 // typedef struct objc_class *Class;
1267 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1268 assert(ptr && "'Class' incorrectly typed");
1269 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1270 assert(rec && "'Class' incorrectly typed");
1271 ClassStructType = rec;
1272}
1273
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001274void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1275 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00001276 "'NSConstantString' type already set!");
1277
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001278 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00001279}
1280
Steve Naroffec0550f2007-10-15 20:41:53 +00001281bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1282 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1283 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1284
1285 return lBuiltin->getKind() == rBuiltin->getKind();
1286}
1287
Fariborz Jahanianb145e7d2007-12-21 17:34:43 +00001288/// objcTypesAreCompatible - This routine is called when two types
1289/// are of different class; one is interface type or is
1290/// a qualified interface type and the other type is of a different class.
1291/// Example, II or II<P>.
Steve Naroffec0550f2007-10-15 20:41:53 +00001292bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001293 if (lhs->isObjCInterfaceType() && isObjCIdType(rhs))
Steve Naroffec0550f2007-10-15 20:41:53 +00001294 return true;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001295 else if (isObjCIdType(lhs) && rhs->isObjCInterfaceType())
Steve Naroffec0550f2007-10-15 20:41:53 +00001296 return true;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001297 if (ObjCInterfaceType *lhsIT =
1298 dyn_cast<ObjCInterfaceType>(lhs.getCanonicalType().getTypePtr())) {
1299 ObjCQualifiedInterfaceType *rhsQI =
1300 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanianb145e7d2007-12-21 17:34:43 +00001301 return rhsQI && (lhsIT->getDecl() == rhsQI->getDecl());
1302 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001303 else if (ObjCInterfaceType *rhsIT =
1304 dyn_cast<ObjCInterfaceType>(rhs.getCanonicalType().getTypePtr())) {
1305 ObjCQualifiedInterfaceType *lhsQI =
1306 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanianb145e7d2007-12-21 17:34:43 +00001307 return lhsQI && (rhsIT->getDecl() == lhsQI->getDecl());
1308 }
Steve Naroffec0550f2007-10-15 20:41:53 +00001309 return false;
1310}
1311
Fariborz Jahanianc5ae5cf2008-01-07 20:12:21 +00001312/// Check that 'lhs' and 'rhs' are compatible interface types. Both types
1313/// must be canonical types.
Steve Naroffec0550f2007-10-15 20:41:53 +00001314bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
Fariborz Jahanianc5ae5cf2008-01-07 20:12:21 +00001315 assert (lhs->isCanonical() &&
1316 "interfaceTypesAreCompatible strip typedefs of lhs");
1317 assert (rhs->isCanonical() &&
1318 "interfaceTypesAreCompatible strip typedefs of rhs");
Fariborz Jahanian0f01deb2007-12-20 22:37:58 +00001319 if (lhs == rhs)
1320 return true;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001321 ObjCInterfaceType *lhsIT = cast<ObjCInterfaceType>(lhs.getTypePtr());
1322 ObjCInterfaceType *rhsIT = cast<ObjCInterfaceType>(rhs.getTypePtr());
1323 ObjCInterfaceDecl *rhsIDecl = rhsIT->getDecl();
1324 ObjCInterfaceDecl *lhsIDecl = lhsIT->getDecl();
Fariborz Jahanian0f01deb2007-12-20 22:37:58 +00001325 // rhs is derived from lhs it is OK; else it is not OK.
1326 while (rhsIDecl != NULL) {
1327 if (rhsIDecl == lhsIDecl)
1328 return true;
1329 rhsIDecl = rhsIDecl->getSuperClass();
1330 }
1331 return false;
Steve Naroffec0550f2007-10-15 20:41:53 +00001332}
1333
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001334bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
1335 QualType rhs) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001336 ObjCQualifiedInterfaceType *lhsQI =
1337 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001338 assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001339 ObjCQualifiedInterfaceType *rhsQI =
1340 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001341 assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
Fariborz Jahanianc5ae5cf2008-01-07 20:12:21 +00001342 if (!interfaceTypesAreCompatible(
1343 getObjCInterfaceType(lhsQI->getDecl()).getCanonicalType(),
1344 getObjCInterfaceType(rhsQI->getDecl()).getCanonicalType()))
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001345 return false;
1346 /* All protocols in lhs must have a presense in rhs. */
1347 for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
1348 bool match = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001349 ObjCProtocolDecl *lhsProto = lhsQI->getProtocols(i);
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001350 for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001351 ObjCProtocolDecl *rhsProto = rhsQI->getProtocols(j);
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001352 if (lhsProto == rhsProto) {
1353 match = true;
1354 break;
1355 }
1356 }
1357 if (!match)
1358 return false;
1359 }
1360 return true;
1361}
1362
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001363/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
1364/// inheritance hierarchy of 'rProto'.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001365static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
1366 ObjCProtocolDecl *rProto) {
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001367 if (lProto == rProto)
1368 return true;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001369 ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001370 for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
1371 if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
1372 return true;
1373 return false;
1374}
1375
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001376/// ClassImplementsProtocol - Checks that 'lProto' protocol
1377/// has been implemented in IDecl class, its super class or categories (if
1378/// lookupCategory is true).
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001379static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1380 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001381 bool lookupCategory) {
1382
1383 // 1st, look up the class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001384 ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols();
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001385 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
1386 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1387 return true;
1388 }
1389
1390 // 2nd, look up the category.
1391 if (lookupCategory)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001392 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001393 CDecl = CDecl->getNextClassCategory()) {
1394 protoList = CDecl->getReferencedProtocols();
1395 for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
1396 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1397 return true;
1398 }
1399 }
1400
1401 // 3rd, look up the super class(s)
1402 if (IDecl->getSuperClass())
1403 return
1404 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory);
1405
1406 return false;
1407}
1408
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001409/// ObjCQualifiedIdTypesAreCompatible - Compares two types, at least
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001410/// one of which is a protocol qualified 'id' type. When 'compare'
1411/// is true it is for comparison; when false, for assignment/initialization.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001412bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs,
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001413 QualType rhs,
1414 bool compare) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001415 // match id<P..> with an 'id' type in all cases.
1416 if (const PointerType *PT = lhs->getAsPointerType()) {
1417 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001418 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001419 return true;
1420
1421 }
1422 else if (const PointerType *PT = rhs->getAsPointerType()) {
1423 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001424 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001425 return true;
1426
1427 }
1428
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001429 ObjCQualifiedInterfaceType *lhsQI = 0;
1430 ObjCQualifiedInterfaceType *rhsQI = 0;
1431 ObjCInterfaceDecl *lhsID = 0;
1432 ObjCInterfaceDecl *rhsID = 0;
1433 ObjCQualifiedIdType *lhsQID = dyn_cast<ObjCQualifiedIdType>(lhs);
1434 ObjCQualifiedIdType *rhsQID = dyn_cast<ObjCQualifiedIdType>(rhs);
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001435
1436 if (lhsQID) {
1437 if (!rhsQID && rhs->getTypeClass() == Type::Pointer) {
1438 QualType rtype =
1439 cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1440 rhsQI =
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001441 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001442 rtype.getCanonicalType().getTypePtr());
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001443 if (!rhsQI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001444 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001445 rtype.getCanonicalType().getTypePtr());
1446 if (IT)
1447 rhsID = IT->getDecl();
1448 }
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001449 }
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001450 if (!rhsQI && !rhsQID && !rhsID)
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001451 return false;
1452
Fariborz Jahanianbca14a22008-01-03 20:01:35 +00001453 unsigned numRhsProtocols = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001454 ObjCProtocolDecl **rhsProtoList = 0;
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001455 if (rhsQI) {
1456 numRhsProtocols = rhsQI->getNumProtocols();
1457 rhsProtoList = rhsQI->getReferencedProtocols();
1458 }
1459 else if (rhsQID) {
1460 numRhsProtocols = rhsQID->getNumProtocols();
1461 rhsProtoList = rhsQID->getReferencedProtocols();
1462 }
1463
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001464 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001465 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001466 bool match = false;
1467
1468 // when comparing an id<P> on lhs with a static type on rhs,
1469 // see if static class implements all of id's protocols, directly or
1470 // through its super class and categories.
1471 if (rhsID) {
1472 if (ClassImplementsProtocol(lhsProto, rhsID, true))
1473 match = true;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001474 }
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001475 else for (unsigned j = 0; j < numRhsProtocols; j++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001476 ObjCProtocolDecl *rhsProto = rhsProtoList[j];
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001477 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1478 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001479 match = true;
1480 break;
1481 }
1482 }
1483 if (!match)
1484 return false;
1485 }
1486 }
1487 else if (rhsQID) {
1488 if (!lhsQID && lhs->getTypeClass() == Type::Pointer) {
1489 QualType ltype =
1490 cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1491 lhsQI =
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001492 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001493 ltype.getCanonicalType().getTypePtr());
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001494 if (!lhsQI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001495 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001496 ltype.getCanonicalType().getTypePtr());
1497 if (IT)
1498 lhsID = IT->getDecl();
1499 }
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001500 }
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001501 if (!lhsQI && !lhsQID && !lhsID)
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001502 return false;
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001503
Fariborz Jahanianbca14a22008-01-03 20:01:35 +00001504 unsigned numLhsProtocols = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001505 ObjCProtocolDecl **lhsProtoList = 0;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001506 if (lhsQI) {
1507 numLhsProtocols = lhsQI->getNumProtocols();
1508 lhsProtoList = lhsQI->getReferencedProtocols();
1509 }
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001510 else if (lhsQID) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001511 numLhsProtocols = lhsQID->getNumProtocols();
1512 lhsProtoList = lhsQID->getReferencedProtocols();
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001513 }
1514 bool match = false;
1515 // for static type vs. qualified 'id' type, check that class implements
1516 // one of 'id's protocols.
1517 if (lhsID) {
1518 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001519 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001520 if (ClassImplementsProtocol(rhsProto, lhsID, compare)) {
1521 match = true;
1522 break;
1523 }
1524 }
1525 }
1526 else for (unsigned i =0; i < numLhsProtocols; i++) {
1527 match = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001528 ObjCProtocolDecl *lhsProto = lhsProtoList[i];
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001529 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001530 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001531 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1532 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001533 match = true;
1534 break;
1535 }
1536 }
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001537 }
1538 if (!match)
1539 return false;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001540 }
1541 return true;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001542}
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001543
Chris Lattner770951b2007-11-01 05:03:41 +00001544bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1545 const VectorType *lVector = lhs->getAsVectorType();
1546 const VectorType *rVector = rhs->getAsVectorType();
1547
1548 if ((lVector->getElementType().getCanonicalType() ==
1549 rVector->getElementType().getCanonicalType()) &&
1550 (lVector->getNumElements() == rVector->getNumElements()))
1551 return true;
1552 return false;
1553}
1554
Steve Naroffec0550f2007-10-15 20:41:53 +00001555// C99 6.2.7p1: If both are complete types, then the following additional
1556// requirements apply...FIXME (handle compatibility across source files).
1557bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
Steve Naroffab373092007-11-07 06:03:51 +00001558 // "Class" and "id" are compatible built-in structure types.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001559 if (isObjCIdType(lhs) && isObjCClassType(rhs) ||
1560 isObjCClassType(lhs) && isObjCIdType(rhs))
Steve Naroffab373092007-11-07 06:03:51 +00001561 return true;
Eli Friedmand5740522008-02-15 06:03:44 +00001562
1563 // Within a translation unit a tag type is
1564 // only compatible with itself.
1565 return lhs.getCanonicalType() == rhs.getCanonicalType();
Steve Naroffec0550f2007-10-15 20:41:53 +00001566}
1567
1568bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1569 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1570 // identically qualified and both shall be pointers to compatible types.
1571 if (lhs.getQualifiers() != rhs.getQualifiers())
1572 return false;
1573
1574 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1575 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1576
1577 return typesAreCompatible(ltype, rtype);
1578}
1579
Bill Wendling43d69752007-12-03 07:33:35 +00001580// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroffec0550f2007-10-15 20:41:53 +00001581// reference to T, the operation assigns to the object of type T denoted by the
1582// reference.
1583bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1584 QualType ltype = lhs;
1585
1586 if (lhs->isReferenceType())
1587 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1588
1589 QualType rtype = rhs;
1590
1591 if (rhs->isReferenceType())
1592 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1593
1594 return typesAreCompatible(ltype, rtype);
1595}
1596
1597bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1598 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1599 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1600 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1601 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1602
1603 // first check the return types (common between C99 and K&R).
1604 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1605 return false;
1606
1607 if (lproto && rproto) { // two C99 style function prototypes
1608 unsigned lproto_nargs = lproto->getNumArgs();
1609 unsigned rproto_nargs = rproto->getNumArgs();
1610
1611 if (lproto_nargs != rproto_nargs)
1612 return false;
1613
1614 // both prototypes have the same number of arguments.
1615 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1616 (rproto->isVariadic() && !lproto->isVariadic()))
1617 return false;
1618
1619 // The use of ellipsis agree...now check the argument types.
1620 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Narofff69cc5d2008-01-30 19:17:43 +00001621 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1622 // is taken as having the unqualified version of it's declared type.
Steve Naroffba03eda2008-01-29 00:15:50 +00001623 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Narofff69cc5d2008-01-30 19:17:43 +00001624 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroffec0550f2007-10-15 20:41:53 +00001625 return false;
1626 return true;
1627 }
1628 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1629 return true;
1630
1631 // we have a mixture of K&R style with C99 prototypes
1632 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1633
1634 if (proto->isVariadic())
1635 return false;
1636
1637 // FIXME: Each parameter type T in the prototype must be compatible with the
1638 // type resulting from applying the usual argument conversions to T.
1639 return true;
1640}
1641
1642bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
Eli Friedman4e92acf2008-02-06 04:53:22 +00001643 // Compatible arrays must have compatible element types
1644 QualType ltype = lhs->getAsArrayType()->getElementType();
1645 QualType rtype = rhs->getAsArrayType()->getElementType();
1646
Steve Naroffec0550f2007-10-15 20:41:53 +00001647 if (!typesAreCompatible(ltype, rtype))
1648 return false;
Eli Friedman4e92acf2008-02-06 04:53:22 +00001649
1650 // Compatible arrays must be the same size
1651 if (const ConstantArrayType* LCAT = lhs->getAsConstantArrayType())
1652 if (const ConstantArrayType* RCAT = rhs->getAsConstantArrayType())
1653 return RCAT->getSize() == LCAT->getSize();
1654
Steve Naroffec0550f2007-10-15 20:41:53 +00001655 return true;
1656}
1657
1658/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1659/// both shall have the identically qualified version of a compatible type.
1660/// C99 6.2.7p1: Two types have compatible types if their types are the
1661/// same. See 6.7.[2,3,5] for additional rules.
1662bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
Steve Naroff2565eef2008-01-29 18:58:14 +00001663 if (lhs.getQualifiers() != rhs.getQualifiers())
1664 return false;
1665
Steve Naroffec0550f2007-10-15 20:41:53 +00001666 QualType lcanon = lhs.getCanonicalType();
1667 QualType rcanon = rhs.getCanonicalType();
1668
1669 // If two types are identical, they are are compatible
1670 if (lcanon == rcanon)
1671 return true;
Bill Wendling43d69752007-12-03 07:33:35 +00001672
1673 // C++ [expr]: If an expression initially has the type "reference to T", the
1674 // type is adjusted to "T" prior to any further analysis, the expression
1675 // designates the object or function denoted by the reference, and the
1676 // expression is an lvalue.
Chris Lattner1adb8832008-01-14 05:45:46 +00001677 if (ReferenceType *RT = dyn_cast<ReferenceType>(lcanon))
1678 lcanon = RT->getReferenceeType();
1679 if (ReferenceType *RT = dyn_cast<ReferenceType>(rcanon))
1680 rcanon = RT->getReferenceeType();
1681
1682 Type::TypeClass LHSClass = lcanon->getTypeClass();
1683 Type::TypeClass RHSClass = rcanon->getTypeClass();
1684
1685 // We want to consider the two function types to be the same for these
1686 // comparisons, just force one to the other.
1687 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1688 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00001689
1690 // Same as above for arrays
1691 if (LHSClass == Type::VariableArray) LHSClass = Type::ConstantArray;
1692 if (RHSClass == Type::VariableArray) RHSClass = Type::ConstantArray;
Eli Friedmanc5773c42008-02-15 18:16:39 +00001693 if (LHSClass == Type::IncompleteArray) LHSClass = Type::ConstantArray;
1694 if (RHSClass == Type::IncompleteArray) RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00001695
Steve Naroff4a746782008-01-09 22:43:08 +00001696 // If the canonical type classes don't match...
Chris Lattner1adb8832008-01-14 05:45:46 +00001697 if (LHSClass != RHSClass) {
Steve Naroffec0550f2007-10-15 20:41:53 +00001698 // For Objective-C, it is possible for two types to be compatible
1699 // when their classes don't match (when dealing with "id"). If either type
1700 // is an interface, we defer to objcTypesAreCompatible().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001701 if (lcanon->isObjCInterfaceType() || rcanon->isObjCInterfaceType())
Steve Naroffec0550f2007-10-15 20:41:53 +00001702 return objcTypesAreCompatible(lcanon, rcanon);
Steve Narofff69cc5d2008-01-30 19:17:43 +00001703
Chris Lattner1adb8832008-01-14 05:45:46 +00001704 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1705 // a signed integer type, or an unsigned integer type.
Eli Friedmanbab96962008-02-12 08:46:17 +00001706 if (lcanon->isEnumeralType() && rcanon->isIntegralType()) {
1707 EnumDecl* EDecl = cast<EnumDecl>(cast<TagType>(lcanon)->getDecl());
1708 return EDecl->getIntegerType() == rcanon;
1709 }
1710 if (rcanon->isEnumeralType() && lcanon->isIntegralType()) {
1711 EnumDecl* EDecl = cast<EnumDecl>(cast<TagType>(rcanon)->getDecl());
1712 return EDecl->getIntegerType() == lcanon;
1713 }
Chris Lattner1adb8832008-01-14 05:45:46 +00001714
Steve Naroffec0550f2007-10-15 20:41:53 +00001715 return false;
1716 }
Steve Naroff4a746782008-01-09 22:43:08 +00001717 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00001718 switch (LHSClass) {
1719 case Type::FunctionProto: assert(0 && "Canonicalized away above");
1720 case Type::Pointer:
1721 return pointerTypesAreCompatible(lcanon, rcanon);
1722 case Type::ConstantArray:
1723 case Type::VariableArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +00001724 case Type::IncompleteArray:
Chris Lattner1adb8832008-01-14 05:45:46 +00001725 return arrayTypesAreCompatible(lcanon, rcanon);
1726 case Type::FunctionNoProto:
1727 return functionTypesAreCompatible(lcanon, rcanon);
1728 case Type::Tagged: // handle structures, unions
1729 return tagTypesAreCompatible(lcanon, rcanon);
1730 case Type::Builtin:
1731 return builtinTypesAreCompatible(lcanon, rcanon);
1732 case Type::ObjCInterface:
1733 return interfaceTypesAreCompatible(lcanon, rcanon);
1734 case Type::Vector:
1735 case Type::OCUVector:
1736 return vectorTypesAreCompatible(lcanon, rcanon);
1737 case Type::ObjCQualifiedInterface:
1738 return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
1739 default:
1740 assert(0 && "unexpected type");
Steve Naroffec0550f2007-10-15 20:41:53 +00001741 }
1742 return true; // should never get here...
1743}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001744
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001745/// Emit - Serialize an ASTContext object to Bitcode.
1746void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek54513502007-10-31 20:00:03 +00001747 S.EmitRef(SourceMgr);
1748 S.EmitRef(Target);
1749 S.EmitRef(Idents);
1750 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001751
Ted Kremenekfee04522007-10-31 22:44:07 +00001752 // Emit the size of the type vector so that we can reserve that size
1753 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00001754 S.EmitInt(Types.size());
1755
Ted Kremenek03ed4402007-11-13 22:02:55 +00001756 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1757 I!=E;++I)
1758 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001759
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001760 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001761}
1762
Ted Kremenek0f84c002007-11-13 00:25:37 +00001763ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenekfee04522007-10-31 22:44:07 +00001764 SourceManager &SM = D.ReadRef<SourceManager>();
1765 TargetInfo &t = D.ReadRef<TargetInfo>();
1766 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1767 SelectorTable &sels = D.ReadRef<SelectorTable>();
1768
1769 unsigned size_reserve = D.ReadInt();
1770
1771 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1772
Ted Kremenek03ed4402007-11-13 22:02:55 +00001773 for (unsigned i = 0; i < size_reserve; ++i)
1774 Type::Create(*A,i,D);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001775
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001776 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00001777
1778 return A;
1779}