blob: 823a20bb03da2f437cd98d2fad6ca3a23dfb7991 [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);
326 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
327 uint64_t FieldSize = FieldInfo.first;
328 unsigned FieldAlign = FieldInfo.second;
329
330 // Round up the current record size to the field's alignment boundary.
331 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
332
333 // Place this field at the current location.
334 FieldOffsets[i] = RecordSize;
335
336 // Reserve space for this field.
337 RecordSize += FieldSize;
338
339 // Remember max struct/class alignment.
340 RecordAlign = std::max(RecordAlign, FieldAlign);
341 }
342
343 // Finally, round the size of the total struct up to the alignment of the
344 // struct itself.
345 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
346 } else {
347 // Union layout just puts each member at the start of the record.
348 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
349 const FieldDecl *FD = D->getMember(i);
350 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
351 uint64_t FieldSize = FieldInfo.first;
352 unsigned FieldAlign = FieldInfo.second;
353
354 // Round up the current record size to the field's alignment boundary.
355 RecordSize = std::max(RecordSize, FieldSize);
356
357 // Place this field at the start of the record.
358 FieldOffsets[i] = 0;
359
360 // Remember max struct/class alignment.
361 RecordAlign = std::max(RecordAlign, FieldAlign);
362 }
363 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000364
365 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
366 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000367}
368
Chris Lattnera7674d82007-07-13 22:13:22 +0000369//===----------------------------------------------------------------------===//
370// Type creation/memoization methods
371//===----------------------------------------------------------------------===//
372
Christopher Lambebb97e92008-02-04 02:31:56 +0000373QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
374 // Check if we've already instantiated an address space qual'd type of this type.
375 llvm::FoldingSetNodeID ID;
376 ASQualType::Profile(ID, T, AddressSpace);
377 void *InsertPos = 0;
378 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
379 return QualType(ASQy, 0);
380
381 // If the base type isn't canonical, this won't be a canonical type either,
382 // so fill in the canonical type field.
383 QualType Canonical;
384 if (!T->isCanonical()) {
385 Canonical = getASQualType(T.getCanonicalType(), AddressSpace);
386
387 // Get the new insert position for the node we care about.
388 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
389 assert(NewIP == 0 && "Shouldn't be in the map!");
390 }
391 ASQualType *New = new ASQualType(T, Canonical, AddressSpace);
392 ASQualTypes.InsertNode(New, InsertPos);
393 Types.push_back(New);
394 return QualType(New, 0);
395}
396
Chris Lattnera7674d82007-07-13 22:13:22 +0000397
Reid Spencer5f016e22007-07-11 17:01:13 +0000398/// getComplexType - Return the uniqued reference to the type for a complex
399/// number with the specified element type.
400QualType ASTContext::getComplexType(QualType T) {
401 // Unique pointers, to guarantee there is only one pointer of a particular
402 // structure.
403 llvm::FoldingSetNodeID ID;
404 ComplexType::Profile(ID, T);
405
406 void *InsertPos = 0;
407 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
408 return QualType(CT, 0);
409
410 // If the pointee type isn't canonical, this won't be a canonical type either,
411 // so fill in the canonical type field.
412 QualType Canonical;
413 if (!T->isCanonical()) {
414 Canonical = getComplexType(T.getCanonicalType());
415
416 // Get the new insert position for the node we care about.
417 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
418 assert(NewIP == 0 && "Shouldn't be in the map!");
419 }
420 ComplexType *New = new ComplexType(T, Canonical);
421 Types.push_back(New);
422 ComplexTypes.InsertNode(New, InsertPos);
423 return QualType(New, 0);
424}
425
426
427/// getPointerType - Return the uniqued reference to the type for a pointer to
428/// the specified type.
429QualType ASTContext::getPointerType(QualType T) {
430 // Unique pointers, to guarantee there is only one pointer of a particular
431 // structure.
432 llvm::FoldingSetNodeID ID;
433 PointerType::Profile(ID, T);
434
435 void *InsertPos = 0;
436 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
437 return QualType(PT, 0);
438
439 // If the pointee type isn't canonical, this won't be a canonical type either,
440 // so fill in the canonical type field.
441 QualType Canonical;
442 if (!T->isCanonical()) {
443 Canonical = getPointerType(T.getCanonicalType());
444
445 // Get the new insert position for the node we care about.
446 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
447 assert(NewIP == 0 && "Shouldn't be in the map!");
448 }
449 PointerType *New = new PointerType(T, Canonical);
450 Types.push_back(New);
451 PointerTypes.InsertNode(New, InsertPos);
452 return QualType(New, 0);
453}
454
455/// getReferenceType - Return the uniqued reference to the type for a reference
456/// to the specified type.
457QualType ASTContext::getReferenceType(QualType T) {
458 // Unique pointers, to guarantee there is only one pointer of a particular
459 // structure.
460 llvm::FoldingSetNodeID ID;
461 ReferenceType::Profile(ID, T);
462
463 void *InsertPos = 0;
464 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
465 return QualType(RT, 0);
466
467 // If the referencee type isn't canonical, this won't be a canonical type
468 // either, so fill in the canonical type field.
469 QualType Canonical;
470 if (!T->isCanonical()) {
471 Canonical = getReferenceType(T.getCanonicalType());
472
473 // Get the new insert position for the node we care about.
474 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
475 assert(NewIP == 0 && "Shouldn't be in the map!");
476 }
477
478 ReferenceType *New = new ReferenceType(T, Canonical);
479 Types.push_back(New);
480 ReferenceTypes.InsertNode(New, InsertPos);
481 return QualType(New, 0);
482}
483
Steve Narofffb22d962007-08-30 01:06:46 +0000484/// getConstantArrayType - Return the unique reference to the type for an
485/// array of the specified element type.
486QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000487 const llvm::APInt &ArySize,
488 ArrayType::ArraySizeModifier ASM,
489 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000490 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000491 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000492
493 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000494 if (ConstantArrayType *ATP =
495 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000496 return QualType(ATP, 0);
497
498 // If the element type isn't canonical, this won't be a canonical type either,
499 // so fill in the canonical type field.
500 QualType Canonical;
501 if (!EltTy->isCanonical()) {
Steve Naroffc9406122007-08-30 18:10:14 +0000502 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
503 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000504 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000505 ConstantArrayType *NewIP =
506 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
507
Reid Spencer5f016e22007-07-11 17:01:13 +0000508 assert(NewIP == 0 && "Shouldn't be in the map!");
509 }
510
Steve Naroffc9406122007-08-30 18:10:14 +0000511 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
512 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000513 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000514 Types.push_back(New);
515 return QualType(New, 0);
516}
517
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000518/// getVariableArrayType - Returns a non-unique reference to the type for a
519/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000520QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
521 ArrayType::ArraySizeModifier ASM,
522 unsigned EltTypeQuals) {
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000523 if (NumElts) {
524 // Since we don't unique expressions, it isn't possible to unique VLA's
525 // that have an expression provided for their size.
526
Ted Kremenek347b9f32007-10-30 16:41:53 +0000527 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
528 ASM, EltTypeQuals);
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000529
Ted Kremenek347b9f32007-10-30 16:41:53 +0000530 CompleteVariableArrayTypes.push_back(New);
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000531 Types.push_back(New);
532 return QualType(New, 0);
533 }
534 else {
535 // No size is provided for the VLA. These we can unique.
536 llvm::FoldingSetNodeID ID;
537 VariableArrayType::Profile(ID, EltTy);
538
539 void *InsertPos = 0;
540 if (VariableArrayType *ATP =
541 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
542 return QualType(ATP, 0);
543
544 // If the element type isn't canonical, this won't be a canonical type
545 // either, so fill in the canonical type field.
546 QualType Canonical;
547
548 if (!EltTy->isCanonical()) {
549 Canonical = getVariableArrayType(EltTy.getCanonicalType(), NumElts,
550 ASM, EltTypeQuals);
551
552 // Get the new insert position for the node we care about.
553 VariableArrayType *NewIP =
554 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
555
556 assert(NewIP == 0 && "Shouldn't be in the map!");
557 }
558
559 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
560 ASM, EltTypeQuals);
561
562 IncompleteVariableArrayTypes.InsertNode(New, InsertPos);
563 Types.push_back(New);
564 return QualType(New, 0);
565 }
Steve Narofffb22d962007-08-30 01:06:46 +0000566}
567
Steve Naroff73322922007-07-18 18:00:27 +0000568/// getVectorType - Return the unique reference to a vector type of
569/// the specified element type and size. VectorType must be a built-in type.
570QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 BuiltinType *baseType;
572
573 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000574 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000575
576 // Check if we've already instantiated a vector of this type.
577 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000578 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 void *InsertPos = 0;
580 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
581 return QualType(VTP, 0);
582
583 // If the element type isn't canonical, this won't be a canonical type either,
584 // so fill in the canonical type field.
585 QualType Canonical;
586 if (!vecType->isCanonical()) {
Steve Naroff73322922007-07-18 18:00:27 +0000587 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000588
589 // Get the new insert position for the node we care about.
590 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
591 assert(NewIP == 0 && "Shouldn't be in the map!");
592 }
593 VectorType *New = new VectorType(vecType, NumElts, Canonical);
594 VectorTypes.InsertNode(New, InsertPos);
595 Types.push_back(New);
596 return QualType(New, 0);
597}
598
Steve Naroff73322922007-07-18 18:00:27 +0000599/// getOCUVectorType - Return the unique reference to an OCU vector type of
600/// the specified element type and size. VectorType must be a built-in type.
601QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
602 BuiltinType *baseType;
603
604 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
605 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
606
607 // Check if we've already instantiated a vector of this type.
608 llvm::FoldingSetNodeID ID;
609 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
610 void *InsertPos = 0;
611 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
612 return QualType(VTP, 0);
613
614 // If the element type isn't canonical, this won't be a canonical type either,
615 // so fill in the canonical type field.
616 QualType Canonical;
617 if (!vecType->isCanonical()) {
618 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
619
620 // Get the new insert position for the node we care about.
621 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
622 assert(NewIP == 0 && "Shouldn't be in the map!");
623 }
624 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
625 VectorTypes.InsertNode(New, InsertPos);
626 Types.push_back(New);
627 return QualType(New, 0);
628}
629
Reid Spencer5f016e22007-07-11 17:01:13 +0000630/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
631///
632QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
633 // Unique functions, to guarantee there is only one function of a particular
634 // structure.
635 llvm::FoldingSetNodeID ID;
636 FunctionTypeNoProto::Profile(ID, ResultTy);
637
638 void *InsertPos = 0;
639 if (FunctionTypeNoProto *FT =
640 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
641 return QualType(FT, 0);
642
643 QualType Canonical;
644 if (!ResultTy->isCanonical()) {
645 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
646
647 // Get the new insert position for the node we care about.
648 FunctionTypeNoProto *NewIP =
649 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
650 assert(NewIP == 0 && "Shouldn't be in the map!");
651 }
652
653 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
654 Types.push_back(New);
655 FunctionTypeProtos.InsertNode(New, InsertPos);
656 return QualType(New, 0);
657}
658
659/// getFunctionType - Return a normal function type with a typed argument
660/// list. isVariadic indicates whether the argument list includes '...'.
661QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
662 unsigned NumArgs, bool isVariadic) {
663 // Unique functions, to guarantee there is only one function of a particular
664 // structure.
665 llvm::FoldingSetNodeID ID;
666 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
667
668 void *InsertPos = 0;
669 if (FunctionTypeProto *FTP =
670 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
671 return QualType(FTP, 0);
672
673 // Determine whether the type being created is already canonical or not.
674 bool isCanonical = ResultTy->isCanonical();
675 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
676 if (!ArgArray[i]->isCanonical())
677 isCanonical = false;
678
679 // If this type isn't canonical, get the canonical version of it.
680 QualType Canonical;
681 if (!isCanonical) {
682 llvm::SmallVector<QualType, 16> CanonicalArgs;
683 CanonicalArgs.reserve(NumArgs);
684 for (unsigned i = 0; i != NumArgs; ++i)
685 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
686
687 Canonical = getFunctionType(ResultTy.getCanonicalType(),
688 &CanonicalArgs[0], NumArgs,
689 isVariadic);
690
691 // Get the new insert position for the node we care about.
692 FunctionTypeProto *NewIP =
693 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
694 assert(NewIP == 0 && "Shouldn't be in the map!");
695 }
696
697 // FunctionTypeProto objects are not allocated with new because they have a
698 // variable size array (for parameter types) at the end of them.
699 FunctionTypeProto *FTP =
700 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000701 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
703 Canonical);
704 Types.push_back(FTP);
705 FunctionTypeProtos.InsertNode(FTP, InsertPos);
706 return QualType(FTP, 0);
707}
708
709/// getTypedefType - Return the unique reference to the type for the
710/// specified typename decl.
711QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
712 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
713
714 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000715 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 Types.push_back(Decl->TypeForDecl);
717 return QualType(Decl->TypeForDecl, 0);
718}
719
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000720/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +0000721/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000722QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +0000723 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
724
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000725 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +0000726 Types.push_back(Decl->TypeForDecl);
727 return QualType(Decl->TypeForDecl, 0);
728}
729
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000730/// getObjCQualifiedInterfaceType - Return a
731/// ObjCQualifiedInterfaceType type for the given interface decl and
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000732/// the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000733QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
734 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000735 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000736 ObjCQualifiedInterfaceType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000737
738 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000739 if (ObjCQualifiedInterfaceType *QT =
740 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000741 return QualType(QT, 0);
742
743 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000744 ObjCQualifiedInterfaceType *QType =
745 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000746 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000747 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000748 return QualType(QType, 0);
749}
750
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000751/// getObjCQualifiedIdType - Return a
752/// getObjCQualifiedIdType type for the 'id' decl and
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000753/// the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000754QualType ASTContext::getObjCQualifiedIdType(QualType idType,
755 ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000756 unsigned NumProtocols) {
757 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000758 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000759
760 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000761 if (ObjCQualifiedIdType *QT =
762 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000763 return QualType(QT, 0);
764
765 // No Match;
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000766 QualType Canonical;
767 if (!idType->isCanonical()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000768 Canonical = getObjCQualifiedIdType(idType.getCanonicalType(),
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000769 Protocols, NumProtocols);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000770 ObjCQualifiedIdType *NewQT =
771 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000772 assert(NewQT == 0 && "Shouldn't be in the map!");
773 }
774
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000775 ObjCQualifiedIdType *QType =
776 new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000777 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000778 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000779 return QualType(QType, 0);
780}
781
Steve Naroff9752f252007-08-01 18:02:17 +0000782/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
783/// TypeOfExpr AST's (since expression's are never shared). For example,
784/// multiple declarations that refer to "typeof(x)" all contain different
785/// DeclRefExpr's. This doesn't effect the type checker, since it operates
786/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000787QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroffd1861fd2007-07-31 12:34:36 +0000788 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000789 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
790 Types.push_back(toe);
791 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000792}
793
Steve Naroff9752f252007-08-01 18:02:17 +0000794/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
795/// TypeOfType AST's. The only motivation to unique these nodes would be
796/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
797/// an issue. This doesn't effect the type checker, since it operates
798/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +0000799QualType ASTContext::getTypeOfType(QualType tofType) {
800 QualType Canonical = tofType.getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000801 TypeOfType *tot = new TypeOfType(tofType, Canonical);
802 Types.push_back(tot);
803 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000804}
805
Reid Spencer5f016e22007-07-11 17:01:13 +0000806/// getTagDeclType - Return the unique reference to the type for the
807/// specified TagDecl (struct/union/class/enum) decl.
808QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +0000809 assert (Decl);
810
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000811 // The decl stores the type cache.
Ted Kremenekd778f882007-11-26 21:16:01 +0000812 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000813
814 TagType* T = new TagType(Decl, QualType());
Ted Kremenekd778f882007-11-26 21:16:01 +0000815 Types.push_back(T);
816 Decl->TypeForDecl = T;
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000817
818 return QualType(T, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000819}
820
821/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
822/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
823/// needs to agree with the definition in <stddef.h>.
824QualType ASTContext::getSizeType() const {
825 // On Darwin, size_t is defined as a "long unsigned int".
826 // FIXME: should derive from "Target".
827 return UnsignedLongTy;
828}
829
Chris Lattner8b9023b2007-07-13 03:05:23 +0000830/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
831/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
832QualType ASTContext::getPointerDiffType() const {
833 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
834 // FIXME: should derive from "Target".
835 return IntTy;
836}
837
Reid Spencer5f016e22007-07-11 17:01:13 +0000838/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
839/// routine will assert if passed a built-in type that isn't an integer or enum.
840static int getIntegerRank(QualType t) {
841 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
842 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
843 return 4;
844 }
845
Christopher Lambebb97e92008-02-04 02:31:56 +0000846 const BuiltinType *BT = t.getCanonicalType()->getAsBuiltinType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000847 switch (BT->getKind()) {
848 default:
849 assert(0 && "getIntegerRank(): not a built-in integer");
850 case BuiltinType::Bool:
851 return 1;
852 case BuiltinType::Char_S:
853 case BuiltinType::Char_U:
854 case BuiltinType::SChar:
855 case BuiltinType::UChar:
856 return 2;
857 case BuiltinType::Short:
858 case BuiltinType::UShort:
859 return 3;
860 case BuiltinType::Int:
861 case BuiltinType::UInt:
862 return 4;
863 case BuiltinType::Long:
864 case BuiltinType::ULong:
865 return 5;
866 case BuiltinType::LongLong:
867 case BuiltinType::ULongLong:
868 return 6;
869 }
870}
871
872/// getFloatingRank - Return a relative rank for floating point types.
873/// This routine will assert if passed a built-in type that isn't a float.
874static int getFloatingRank(QualType T) {
875 T = T.getCanonicalType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000876 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 return getFloatingRank(CT->getElementType());
878
Christopher Lambebb97e92008-02-04 02:31:56 +0000879 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattner770951b2007-11-01 05:03:41 +0000880 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000881 case BuiltinType::Float: return FloatRank;
882 case BuiltinType::Double: return DoubleRank;
883 case BuiltinType::LongDouble: return LongDoubleRank;
884 }
885}
886
Steve Naroff716c7302007-08-27 01:41:48 +0000887/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
888/// point or a complex type (based on typeDomain/typeSize).
889/// 'typeDomain' is a real floating point or complex type.
890/// 'typeSize' is a real floating point or complex type.
Steve Narofff1448a02007-08-27 01:27:54 +0000891QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
892 QualType typeSize, QualType typeDomain) const {
893 if (typeDomain->isComplexType()) {
894 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000895 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000896 case FloatRank: return FloatComplexTy;
897 case DoubleRank: return DoubleComplexTy;
898 case LongDoubleRank: return LongDoubleComplexTy;
899 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000900 }
Steve Narofff1448a02007-08-27 01:27:54 +0000901 if (typeDomain->isRealFloatingType()) {
902 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000903 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000904 case FloatRank: return FloatTy;
905 case DoubleRank: return DoubleTy;
906 case LongDoubleRank: return LongDoubleTy;
907 }
908 }
909 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattnerb1776cb2007-09-16 19:23:47 +0000910 //an invalid return value, but the assert
911 //will ensure that this code is never reached.
912 return VoidTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000913}
914
Steve Narofffb0d4962007-08-27 15:30:22 +0000915/// compareFloatingType - Handles 3 different combos:
916/// float/float, float/complex, complex/complex.
917/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
918int ASTContext::compareFloatingType(QualType lt, QualType rt) {
919 if (getFloatingRank(lt) == getFloatingRank(rt))
920 return 0;
921 if (getFloatingRank(lt) > getFloatingRank(rt))
922 return 1;
923 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000924}
925
926// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
927// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
928QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
929 if (lhs == rhs) return lhs;
930
931 bool t1Unsigned = lhs->isUnsignedIntegerType();
932 bool t2Unsigned = rhs->isUnsignedIntegerType();
933
934 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
935 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
936
937 // We have two integer types with differing signs
938 QualType unsignedType = t1Unsigned ? lhs : rhs;
939 QualType signedType = t1Unsigned ? rhs : lhs;
940
941 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
942 return unsignedType;
943 else {
944 // FIXME: Need to check if the signed type can represent all values of the
945 // unsigned type. If it can, then the result is the signed type.
946 // If it can't, then the result is the unsigned version of the signed type.
947 // Should probably add a helper that returns a signed integer type from
948 // an unsigned (and vice versa). C99 6.3.1.8.
949 return signedType;
950 }
951}
Anders Carlsson71993dd2007-08-17 05:31:46 +0000952
953// getCFConstantStringType - Return the type used for constant CFStrings.
954QualType ASTContext::getCFConstantStringType() {
955 if (!CFConstantStringTypeDecl) {
956 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
Steve Naroffbeaf2992007-11-03 11:27:19 +0000957 &Idents.get("NSConstantString"),
Anders Carlsson71993dd2007-08-17 05:31:46 +0000958 0);
Anders Carlssonf06273f2007-11-19 00:25:30 +0000959 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +0000960
961 // const int *isa;
962 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +0000963 // int flags;
964 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000965 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +0000966 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +0000967 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +0000968 FieldTypes[3] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000969 // Create fields
Anders Carlssonf06273f2007-11-19 00:25:30 +0000970 FieldDecl *FieldDecls[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +0000971
Anders Carlssonf06273f2007-11-19 00:25:30 +0000972 for (unsigned i = 0; i < 4; ++i)
Steve Narofff38661e2007-09-14 02:20:46 +0000973 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +0000974
975 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
976 }
977
978 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +0000979}
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000980
Anders Carlssone8c49532007-10-29 06:33:42 +0000981// This returns true if a type has been typedefed to BOOL:
982// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +0000983static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +0000984 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner2d998332007-10-30 20:27:44 +0000985 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000986
987 return false;
988}
989
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000990/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000991/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000992int ASTContext::getObjCEncodingTypeSize(QualType type) {
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000993 SourceLocation Loc;
994 uint64_t sz = getTypeSize(type, Loc);
995
996 // Make all integer and enum types at least as large as an int
997 if (sz > 0 && type->isIntegralType())
998 sz = std::max(sz, getTypeSize(IntTy, Loc));
999 // Treat arrays as pointers, since that's how they're passed in.
1000 else if (type->isArrayType())
1001 sz = getTypeSize(VoidPtrTy, Loc);
1002 return sz / getTypeSize(CharTy, Loc);
1003}
1004
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001005/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001006/// declaration.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001007void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001008 std::string& S)
1009{
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001010 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001011 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001012 // Encode result type.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001013 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001014 // Compute size of all parameters.
1015 // Start with computing size of a pointer in number of bytes.
1016 // FIXME: There might(should) be a better way of doing this computation!
1017 SourceLocation Loc;
1018 int PtrSize = getTypeSize(VoidPtrTy, Loc) / getTypeSize(CharTy, Loc);
1019 // The first two arguments (self and _cmd) are pointers; account for
1020 // their size.
1021 int ParmOffset = 2 * PtrSize;
1022 int NumOfParams = Decl->getNumParams();
1023 for (int i = 0; i < NumOfParams; i++) {
1024 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001025 int sz = getObjCEncodingTypeSize (PType);
1026 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001027 ParmOffset += sz;
1028 }
1029 S += llvm::utostr(ParmOffset);
1030 S += "@0:";
1031 S += llvm::utostr(PtrSize);
1032
1033 // Argument types.
1034 ParmOffset = 2 * PtrSize;
1035 for (int i = 0; i < NumOfParams; i++) {
1036 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001037 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001038 // 'in', 'inout', etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001039 getObjCEncodingForTypeQualifier(
1040 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001041 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001042 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001043 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001044 }
1045}
1046
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001047void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1048 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001049{
Anders Carlssone8c49532007-10-29 06:33:42 +00001050 // FIXME: This currently doesn't encode:
1051 // @ An object (whether statically typed or typed id)
1052 // # A class object (Class)
1053 // : A method selector (SEL)
1054 // {name=type...} A structure
1055 // (name=type...) A union
1056 // bnum A bit field of num bits
1057
1058 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001059 char encoding;
1060 switch (BT->getKind()) {
1061 case BuiltinType::Void:
1062 encoding = 'v';
1063 break;
1064 case BuiltinType::Bool:
1065 encoding = 'B';
1066 break;
1067 case BuiltinType::Char_U:
1068 case BuiltinType::UChar:
1069 encoding = 'C';
1070 break;
1071 case BuiltinType::UShort:
1072 encoding = 'S';
1073 break;
1074 case BuiltinType::UInt:
1075 encoding = 'I';
1076 break;
1077 case BuiltinType::ULong:
1078 encoding = 'L';
1079 break;
1080 case BuiltinType::ULongLong:
1081 encoding = 'Q';
1082 break;
1083 case BuiltinType::Char_S:
1084 case BuiltinType::SChar:
1085 encoding = 'c';
1086 break;
1087 case BuiltinType::Short:
1088 encoding = 's';
1089 break;
1090 case BuiltinType::Int:
1091 encoding = 'i';
1092 break;
1093 case BuiltinType::Long:
1094 encoding = 'l';
1095 break;
1096 case BuiltinType::LongLong:
1097 encoding = 'q';
1098 break;
1099 case BuiltinType::Float:
1100 encoding = 'f';
1101 break;
1102 case BuiltinType::Double:
1103 encoding = 'd';
1104 break;
1105 case BuiltinType::LongDouble:
1106 encoding = 'd';
1107 break;
1108 default:
1109 assert(0 && "Unhandled builtin type kind");
1110 }
1111
1112 S += encoding;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001113 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001114 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001115 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001116 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001117
1118 }
1119 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001120 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001121 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001122 S += '@';
1123 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001124 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001125 S += '#';
1126 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001127 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001128 S += ':';
1129 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001130 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001131
1132 if (PointeeTy->isCharType()) {
1133 // char pointer types should be encoded as '*' unless it is a
1134 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001135 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001136 S += '*';
1137 return;
1138 }
1139 }
1140
1141 S += '^';
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001142 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone8c49532007-10-29 06:33:42 +00001143 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001144 S += '[';
1145
1146 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1147 S += llvm::utostr(CAT->getSize().getZExtValue());
1148 else
1149 assert(0 && "Unhandled array type!");
1150
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001151 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001152 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001153 } else if (T->getAsFunctionType()) {
1154 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001155 } else if (const RecordType *RTy = T->getAsRecordType()) {
1156 RecordDecl *RDecl= RTy->getDecl();
1157 S += '{';
1158 S += RDecl->getName();
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001159 bool found = false;
1160 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1161 if (ERType[i] == RTy) {
1162 found = true;
1163 break;
1164 }
1165 if (!found) {
1166 ERType.push_back(RTy);
1167 S += '=';
1168 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1169 FieldDecl *field = RDecl->getMember(i);
1170 getObjCEncodingForType(field->getType(), S, ERType);
1171 }
1172 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1173 ERType.pop_back();
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001174 }
1175 S += '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001176 } else if (T->isEnumeralType()) {
1177 S += 'i';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001178 } else
Steve Narofff69cc5d2008-01-30 19:17:43 +00001179 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001180}
1181
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001182void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001183 std::string& S) const {
1184 if (QT & Decl::OBJC_TQ_In)
1185 S += 'n';
1186 if (QT & Decl::OBJC_TQ_Inout)
1187 S += 'N';
1188 if (QT & Decl::OBJC_TQ_Out)
1189 S += 'o';
1190 if (QT & Decl::OBJC_TQ_Bycopy)
1191 S += 'O';
1192 if (QT & Decl::OBJC_TQ_Byref)
1193 S += 'R';
1194 if (QT & Decl::OBJC_TQ_Oneway)
1195 S += 'V';
1196}
1197
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001198void ASTContext::setBuiltinVaListType(QualType T)
1199{
1200 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1201
1202 BuiltinVaListType = T;
1203}
1204
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001205void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00001206{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001207 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff7e219e42007-10-15 14:41:52 +00001208
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001209 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00001210
1211 // typedef struct objc_object *id;
1212 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1213 assert(ptr && "'id' incorrectly typed");
1214 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1215 assert(rec && "'id' incorrectly typed");
1216 IdStructType = rec;
1217}
1218
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001219void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001220{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001221 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001222
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001223 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001224
1225 // typedef struct objc_selector *SEL;
1226 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1227 assert(ptr && "'SEL' incorrectly typed");
1228 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1229 assert(rec && "'SEL' incorrectly typed");
1230 SelStructType = rec;
1231}
1232
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001233void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001234{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001235 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1236 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001237}
1238
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001239void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00001240{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001241 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson8baaca52007-10-31 02:53:19 +00001242
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001243 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00001244
1245 // typedef struct objc_class *Class;
1246 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1247 assert(ptr && "'Class' incorrectly typed");
1248 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1249 assert(rec && "'Class' incorrectly typed");
1250 ClassStructType = rec;
1251}
1252
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001253void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1254 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00001255 "'NSConstantString' type already set!");
1256
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001257 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00001258}
1259
Steve Naroffec0550f2007-10-15 20:41:53 +00001260bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1261 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1262 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1263
1264 return lBuiltin->getKind() == rBuiltin->getKind();
1265}
1266
Fariborz Jahanianb145e7d2007-12-21 17:34:43 +00001267/// objcTypesAreCompatible - This routine is called when two types
1268/// are of different class; one is interface type or is
1269/// a qualified interface type and the other type is of a different class.
1270/// Example, II or II<P>.
Steve Naroffec0550f2007-10-15 20:41:53 +00001271bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001272 if (lhs->isObjCInterfaceType() && isObjCIdType(rhs))
Steve Naroffec0550f2007-10-15 20:41:53 +00001273 return true;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001274 else if (isObjCIdType(lhs) && rhs->isObjCInterfaceType())
Steve Naroffec0550f2007-10-15 20:41:53 +00001275 return true;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001276 if (ObjCInterfaceType *lhsIT =
1277 dyn_cast<ObjCInterfaceType>(lhs.getCanonicalType().getTypePtr())) {
1278 ObjCQualifiedInterfaceType *rhsQI =
1279 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanianb145e7d2007-12-21 17:34:43 +00001280 return rhsQI && (lhsIT->getDecl() == rhsQI->getDecl());
1281 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001282 else if (ObjCInterfaceType *rhsIT =
1283 dyn_cast<ObjCInterfaceType>(rhs.getCanonicalType().getTypePtr())) {
1284 ObjCQualifiedInterfaceType *lhsQI =
1285 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanianb145e7d2007-12-21 17:34:43 +00001286 return lhsQI && (rhsIT->getDecl() == lhsQI->getDecl());
1287 }
Steve Naroffec0550f2007-10-15 20:41:53 +00001288 return false;
1289}
1290
Fariborz Jahanianc5ae5cf2008-01-07 20:12:21 +00001291/// Check that 'lhs' and 'rhs' are compatible interface types. Both types
1292/// must be canonical types.
Steve Naroffec0550f2007-10-15 20:41:53 +00001293bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
Fariborz Jahanianc5ae5cf2008-01-07 20:12:21 +00001294 assert (lhs->isCanonical() &&
1295 "interfaceTypesAreCompatible strip typedefs of lhs");
1296 assert (rhs->isCanonical() &&
1297 "interfaceTypesAreCompatible strip typedefs of rhs");
Fariborz Jahanian0f01deb2007-12-20 22:37:58 +00001298 if (lhs == rhs)
1299 return true;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001300 ObjCInterfaceType *lhsIT = cast<ObjCInterfaceType>(lhs.getTypePtr());
1301 ObjCInterfaceType *rhsIT = cast<ObjCInterfaceType>(rhs.getTypePtr());
1302 ObjCInterfaceDecl *rhsIDecl = rhsIT->getDecl();
1303 ObjCInterfaceDecl *lhsIDecl = lhsIT->getDecl();
Fariborz Jahanian0f01deb2007-12-20 22:37:58 +00001304 // rhs is derived from lhs it is OK; else it is not OK.
1305 while (rhsIDecl != NULL) {
1306 if (rhsIDecl == lhsIDecl)
1307 return true;
1308 rhsIDecl = rhsIDecl->getSuperClass();
1309 }
1310 return false;
Steve Naroffec0550f2007-10-15 20:41:53 +00001311}
1312
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001313bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
1314 QualType rhs) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001315 ObjCQualifiedInterfaceType *lhsQI =
1316 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001317 assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001318 ObjCQualifiedInterfaceType *rhsQI =
1319 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001320 assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
Fariborz Jahanianc5ae5cf2008-01-07 20:12:21 +00001321 if (!interfaceTypesAreCompatible(
1322 getObjCInterfaceType(lhsQI->getDecl()).getCanonicalType(),
1323 getObjCInterfaceType(rhsQI->getDecl()).getCanonicalType()))
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001324 return false;
1325 /* All protocols in lhs must have a presense in rhs. */
1326 for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
1327 bool match = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001328 ObjCProtocolDecl *lhsProto = lhsQI->getProtocols(i);
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001329 for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001330 ObjCProtocolDecl *rhsProto = rhsQI->getProtocols(j);
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001331 if (lhsProto == rhsProto) {
1332 match = true;
1333 break;
1334 }
1335 }
1336 if (!match)
1337 return false;
1338 }
1339 return true;
1340}
1341
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001342/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
1343/// inheritance hierarchy of 'rProto'.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001344static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
1345 ObjCProtocolDecl *rProto) {
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001346 if (lProto == rProto)
1347 return true;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001348 ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001349 for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
1350 if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
1351 return true;
1352 return false;
1353}
1354
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001355/// ClassImplementsProtocol - Checks that 'lProto' protocol
1356/// has been implemented in IDecl class, its super class or categories (if
1357/// lookupCategory is true).
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001358static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1359 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001360 bool lookupCategory) {
1361
1362 // 1st, look up the class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001363 ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols();
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001364 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
1365 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1366 return true;
1367 }
1368
1369 // 2nd, look up the category.
1370 if (lookupCategory)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001371 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001372 CDecl = CDecl->getNextClassCategory()) {
1373 protoList = CDecl->getReferencedProtocols();
1374 for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
1375 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1376 return true;
1377 }
1378 }
1379
1380 // 3rd, look up the super class(s)
1381 if (IDecl->getSuperClass())
1382 return
1383 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory);
1384
1385 return false;
1386}
1387
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001388/// ObjCQualifiedIdTypesAreCompatible - Compares two types, at least
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001389/// one of which is a protocol qualified 'id' type. When 'compare'
1390/// is true it is for comparison; when false, for assignment/initialization.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001391bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs,
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001392 QualType rhs,
1393 bool compare) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001394 // match id<P..> with an 'id' type in all cases.
1395 if (const PointerType *PT = lhs->getAsPointerType()) {
1396 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001397 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001398 return true;
1399
1400 }
1401 else if (const PointerType *PT = rhs->getAsPointerType()) {
1402 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001403 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001404 return true;
1405
1406 }
1407
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001408 ObjCQualifiedInterfaceType *lhsQI = 0;
1409 ObjCQualifiedInterfaceType *rhsQI = 0;
1410 ObjCInterfaceDecl *lhsID = 0;
1411 ObjCInterfaceDecl *rhsID = 0;
1412 ObjCQualifiedIdType *lhsQID = dyn_cast<ObjCQualifiedIdType>(lhs);
1413 ObjCQualifiedIdType *rhsQID = dyn_cast<ObjCQualifiedIdType>(rhs);
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001414
1415 if (lhsQID) {
1416 if (!rhsQID && rhs->getTypeClass() == Type::Pointer) {
1417 QualType rtype =
1418 cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1419 rhsQI =
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001420 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001421 rtype.getCanonicalType().getTypePtr());
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001422 if (!rhsQI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001423 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001424 rtype.getCanonicalType().getTypePtr());
1425 if (IT)
1426 rhsID = IT->getDecl();
1427 }
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001428 }
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001429 if (!rhsQI && !rhsQID && !rhsID)
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001430 return false;
1431
Fariborz Jahanianbca14a22008-01-03 20:01:35 +00001432 unsigned numRhsProtocols = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001433 ObjCProtocolDecl **rhsProtoList = 0;
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001434 if (rhsQI) {
1435 numRhsProtocols = rhsQI->getNumProtocols();
1436 rhsProtoList = rhsQI->getReferencedProtocols();
1437 }
1438 else if (rhsQID) {
1439 numRhsProtocols = rhsQID->getNumProtocols();
1440 rhsProtoList = rhsQID->getReferencedProtocols();
1441 }
1442
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001443 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001444 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001445 bool match = false;
1446
1447 // when comparing an id<P> on lhs with a static type on rhs,
1448 // see if static class implements all of id's protocols, directly or
1449 // through its super class and categories.
1450 if (rhsID) {
1451 if (ClassImplementsProtocol(lhsProto, rhsID, true))
1452 match = true;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001453 }
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001454 else for (unsigned j = 0; j < numRhsProtocols; j++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001455 ObjCProtocolDecl *rhsProto = rhsProtoList[j];
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001456 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1457 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001458 match = true;
1459 break;
1460 }
1461 }
1462 if (!match)
1463 return false;
1464 }
1465 }
1466 else if (rhsQID) {
1467 if (!lhsQID && lhs->getTypeClass() == Type::Pointer) {
1468 QualType ltype =
1469 cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1470 lhsQI =
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001471 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001472 ltype.getCanonicalType().getTypePtr());
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001473 if (!lhsQI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001474 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001475 ltype.getCanonicalType().getTypePtr());
1476 if (IT)
1477 lhsID = IT->getDecl();
1478 }
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001479 }
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001480 if (!lhsQI && !lhsQID && !lhsID)
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001481 return false;
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001482
Fariborz Jahanianbca14a22008-01-03 20:01:35 +00001483 unsigned numLhsProtocols = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001484 ObjCProtocolDecl **lhsProtoList = 0;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001485 if (lhsQI) {
1486 numLhsProtocols = lhsQI->getNumProtocols();
1487 lhsProtoList = lhsQI->getReferencedProtocols();
1488 }
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001489 else if (lhsQID) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001490 numLhsProtocols = lhsQID->getNumProtocols();
1491 lhsProtoList = lhsQID->getReferencedProtocols();
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001492 }
1493 bool match = false;
1494 // for static type vs. qualified 'id' type, check that class implements
1495 // one of 'id's protocols.
1496 if (lhsID) {
1497 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001498 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001499 if (ClassImplementsProtocol(rhsProto, lhsID, compare)) {
1500 match = true;
1501 break;
1502 }
1503 }
1504 }
1505 else for (unsigned i =0; i < numLhsProtocols; i++) {
1506 match = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001507 ObjCProtocolDecl *lhsProto = lhsProtoList[i];
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001508 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001509 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001510 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1511 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001512 match = true;
1513 break;
1514 }
1515 }
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001516 }
1517 if (!match)
1518 return false;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001519 }
1520 return true;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001521}
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001522
Chris Lattner770951b2007-11-01 05:03:41 +00001523bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1524 const VectorType *lVector = lhs->getAsVectorType();
1525 const VectorType *rVector = rhs->getAsVectorType();
1526
1527 if ((lVector->getElementType().getCanonicalType() ==
1528 rVector->getElementType().getCanonicalType()) &&
1529 (lVector->getNumElements() == rVector->getNumElements()))
1530 return true;
1531 return false;
1532}
1533
Steve Naroffec0550f2007-10-15 20:41:53 +00001534// C99 6.2.7p1: If both are complete types, then the following additional
1535// requirements apply...FIXME (handle compatibility across source files).
1536bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
1537 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
1538 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
1539
1540 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
1541 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1542 return true;
1543 }
1544 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
1545 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1546 return true;
1547 }
Steve Naroffab373092007-11-07 06:03:51 +00001548 // "Class" and "id" are compatible built-in structure types.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001549 if (isObjCIdType(lhs) && isObjCClassType(rhs) ||
1550 isObjCClassType(lhs) && isObjCIdType(rhs))
Steve Naroffab373092007-11-07 06:03:51 +00001551 return true;
Steve Naroffec0550f2007-10-15 20:41:53 +00001552 return false;
1553}
1554
1555bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1556 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1557 // identically qualified and both shall be pointers to compatible types.
1558 if (lhs.getQualifiers() != rhs.getQualifiers())
1559 return false;
1560
1561 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1562 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1563
1564 return typesAreCompatible(ltype, rtype);
1565}
1566
Bill Wendling43d69752007-12-03 07:33:35 +00001567// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroffec0550f2007-10-15 20:41:53 +00001568// reference to T, the operation assigns to the object of type T denoted by the
1569// reference.
1570bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1571 QualType ltype = lhs;
1572
1573 if (lhs->isReferenceType())
1574 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1575
1576 QualType rtype = rhs;
1577
1578 if (rhs->isReferenceType())
1579 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1580
1581 return typesAreCompatible(ltype, rtype);
1582}
1583
1584bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1585 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1586 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1587 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1588 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1589
1590 // first check the return types (common between C99 and K&R).
1591 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1592 return false;
1593
1594 if (lproto && rproto) { // two C99 style function prototypes
1595 unsigned lproto_nargs = lproto->getNumArgs();
1596 unsigned rproto_nargs = rproto->getNumArgs();
1597
1598 if (lproto_nargs != rproto_nargs)
1599 return false;
1600
1601 // both prototypes have the same number of arguments.
1602 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1603 (rproto->isVariadic() && !lproto->isVariadic()))
1604 return false;
1605
1606 // The use of ellipsis agree...now check the argument types.
1607 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Narofff69cc5d2008-01-30 19:17:43 +00001608 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1609 // is taken as having the unqualified version of it's declared type.
Steve Naroffba03eda2008-01-29 00:15:50 +00001610 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Narofff69cc5d2008-01-30 19:17:43 +00001611 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroffec0550f2007-10-15 20:41:53 +00001612 return false;
1613 return true;
1614 }
1615 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1616 return true;
1617
1618 // we have a mixture of K&R style with C99 prototypes
1619 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1620
1621 if (proto->isVariadic())
1622 return false;
1623
1624 // FIXME: Each parameter type T in the prototype must be compatible with the
1625 // type resulting from applying the usual argument conversions to T.
1626 return true;
1627}
1628
1629bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
1630 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
1631 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
1632
1633 if (!typesAreCompatible(ltype, rtype))
1634 return false;
1635
1636 // FIXME: If both types specify constant sizes, then the sizes must also be
1637 // the same. Even if the sizes are the same, GCC produces an error.
1638 return true;
1639}
1640
1641/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1642/// both shall have the identically qualified version of a compatible type.
1643/// C99 6.2.7p1: Two types have compatible types if their types are the
1644/// same. See 6.7.[2,3,5] for additional rules.
1645bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
Steve Naroff2565eef2008-01-29 18:58:14 +00001646 if (lhs.getQualifiers() != rhs.getQualifiers())
1647 return false;
1648
Steve Naroffec0550f2007-10-15 20:41:53 +00001649 QualType lcanon = lhs.getCanonicalType();
1650 QualType rcanon = rhs.getCanonicalType();
1651
1652 // If two types are identical, they are are compatible
1653 if (lcanon == rcanon)
1654 return true;
Bill Wendling43d69752007-12-03 07:33:35 +00001655
1656 // C++ [expr]: If an expression initially has the type "reference to T", the
1657 // type is adjusted to "T" prior to any further analysis, the expression
1658 // designates the object or function denoted by the reference, and the
1659 // expression is an lvalue.
Chris Lattner1adb8832008-01-14 05:45:46 +00001660 if (ReferenceType *RT = dyn_cast<ReferenceType>(lcanon))
1661 lcanon = RT->getReferenceeType();
1662 if (ReferenceType *RT = dyn_cast<ReferenceType>(rcanon))
1663 rcanon = RT->getReferenceeType();
1664
1665 Type::TypeClass LHSClass = lcanon->getTypeClass();
1666 Type::TypeClass RHSClass = rcanon->getTypeClass();
1667
1668 // We want to consider the two function types to be the same for these
1669 // comparisons, just force one to the other.
1670 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1671 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Steve Naroffec0550f2007-10-15 20:41:53 +00001672
Steve Naroff4a746782008-01-09 22:43:08 +00001673 // If the canonical type classes don't match...
Chris Lattner1adb8832008-01-14 05:45:46 +00001674 if (LHSClass != RHSClass) {
Steve Naroffec0550f2007-10-15 20:41:53 +00001675 // For Objective-C, it is possible for two types to be compatible
1676 // when their classes don't match (when dealing with "id"). If either type
1677 // is an interface, we defer to objcTypesAreCompatible().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001678 if (lcanon->isObjCInterfaceType() || rcanon->isObjCInterfaceType())
Steve Naroffec0550f2007-10-15 20:41:53 +00001679 return objcTypesAreCompatible(lcanon, rcanon);
Steve Narofff69cc5d2008-01-30 19:17:43 +00001680
Chris Lattner1adb8832008-01-14 05:45:46 +00001681 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1682 // a signed integer type, or an unsigned integer type.
1683 // FIXME: need to check the size and ensure it's the same.
1684 if ((lcanon->isEnumeralType() && rcanon->isIntegralType()) ||
1685 (rcanon->isEnumeralType() && lcanon->isIntegralType()))
1686 return true;
1687
Steve Naroffec0550f2007-10-15 20:41:53 +00001688 return false;
1689 }
Steve Naroff4a746782008-01-09 22:43:08 +00001690 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00001691 switch (LHSClass) {
1692 case Type::FunctionProto: assert(0 && "Canonicalized away above");
1693 case Type::Pointer:
1694 return pointerTypesAreCompatible(lcanon, rcanon);
1695 case Type::ConstantArray:
1696 case Type::VariableArray:
1697 return arrayTypesAreCompatible(lcanon, rcanon);
1698 case Type::FunctionNoProto:
1699 return functionTypesAreCompatible(lcanon, rcanon);
1700 case Type::Tagged: // handle structures, unions
1701 return tagTypesAreCompatible(lcanon, rcanon);
1702 case Type::Builtin:
1703 return builtinTypesAreCompatible(lcanon, rcanon);
1704 case Type::ObjCInterface:
1705 return interfaceTypesAreCompatible(lcanon, rcanon);
1706 case Type::Vector:
1707 case Type::OCUVector:
1708 return vectorTypesAreCompatible(lcanon, rcanon);
1709 case Type::ObjCQualifiedInterface:
1710 return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
1711 default:
1712 assert(0 && "unexpected type");
Steve Naroffec0550f2007-10-15 20:41:53 +00001713 }
1714 return true; // should never get here...
1715}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001716
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001717/// Emit - Serialize an ASTContext object to Bitcode.
1718void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek54513502007-10-31 20:00:03 +00001719 S.EmitRef(SourceMgr);
1720 S.EmitRef(Target);
1721 S.EmitRef(Idents);
1722 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001723
Ted Kremenekfee04522007-10-31 22:44:07 +00001724 // Emit the size of the type vector so that we can reserve that size
1725 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00001726 S.EmitInt(Types.size());
1727
Ted Kremenek03ed4402007-11-13 22:02:55 +00001728 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1729 I!=E;++I)
1730 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001731
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001732 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001733}
1734
Ted Kremenek0f84c002007-11-13 00:25:37 +00001735ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenekfee04522007-10-31 22:44:07 +00001736 SourceManager &SM = D.ReadRef<SourceManager>();
1737 TargetInfo &t = D.ReadRef<TargetInfo>();
1738 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1739 SelectorTable &sels = D.ReadRef<SelectorTable>();
1740
1741 unsigned size_reserve = D.ReadInt();
1742
1743 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1744
Ted Kremenek03ed4402007-11-13 22:02:55 +00001745 for (unsigned i = 0; i < size_reserve; ++i)
1746 Type::Create(*A,i,D);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001747
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001748 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00001749
1750 return A;
1751}