blob: 3aead2f93113ffc4cd3890864f31804cdb2d167e [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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;
Chris Lattnerbeb66362007-12-12 06:43:05 +000051 unsigned NumObjcInterfaces = 0, NumObjcQualifiedInterfaces = 0;
Fariborz Jahanianc5692492007-12-17 21:03:50 +000052 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 }
Steve Naroff3f128ad2007-09-17 14:16:13 +000083 } else if (isa<ObjcInterfaceType>(T))
84 ++NumObjcInterfaces;
Chris Lattnerbeb66362007-12-12 06:43:05 +000085 else if (isa<ObjcQualifiedInterfaceType>(T))
86 ++NumObjcQualifiedInterfaces;
Fariborz Jahanianc5692492007-12-17 21:03:50 +000087 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);
Steve Naroff3f128ad2007-09-17 14:16:13 +0000109 fprintf(stderr, " %d interface types\n", NumObjcInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000110 fprintf(stderr, " %d protocol qualified interface types\n",
111 NumObjcQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000112 fprintf(stderr, " %d protocol qualified id types\n",
113 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();
165 ObjcIdType = QualType();
166 IdStructType = 0;
Anders Carlsson8baaca52007-10-31 02:53:19 +0000167 ObjcClassType = QualType();
168 ClassStructType = 0;
169
Steve Naroff21988912007-10-15 23:35:17 +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 }
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000259 case Type::ObjcQualifiedId:
260 Target.getPointerInfo(Size, Align, getFullLoc(L));
261 break;
262 case Type::Pointer:
263 Target.getPointerInfo(Size, Align, getFullLoc(L));
264 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000265 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000266 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000267 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000268 // FIXME: This is wrong for struct layout: a reference in a struct has
269 // pointer size.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000270 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
Chris Lattner5d2a6302007-07-18 18:26:58 +0000271
272 case Type::Complex: {
273 // Complex types have the same alignment as their elements, but twice the
274 // size.
275 std::pair<uint64_t, unsigned> EltInfo =
276 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
277 Size = EltInfo.first*2;
278 Align = EltInfo.second;
279 break;
280 }
281 case Type::Tagged:
Chris Lattner6cd862c2007-08-27 17:38:00 +0000282 TagType *TT = cast<TagType>(T);
283 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
Devang Patel88a981b2007-11-01 19:11:01 +0000284 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl(), L);
Chris Lattner6cd862c2007-08-27 17:38:00 +0000285 Size = Layout.getSize();
286 Align = Layout.getAlignment();
287 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattnere00b18c2007-08-28 18:24:31 +0000288 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattner6cd862c2007-08-27 17:38:00 +0000289 } else {
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000290 assert(0 && "Unimplemented type sizes!");
Chris Lattner6cd862c2007-08-27 17:38:00 +0000291 }
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000292 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000293 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000294
Chris Lattner464175b2007-07-18 17:52:12 +0000295 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000296 return std::make_pair(Size, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000297}
298
Devang Patel88a981b2007-11-01 19:11:01 +0000299/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000300/// specified record (struct/union/class), which indicates its size and field
301/// position information.
Devang Patel88a981b2007-11-01 19:11:01 +0000302const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D,
303 SourceLocation L) {
Chris Lattner464175b2007-07-18 17:52:12 +0000304 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
305
306 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000307 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000308 if (Entry) return *Entry;
309
Devang Patel88a981b2007-11-01 19:11:01 +0000310 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
311 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
312 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000313 Entry = NewEntry;
314
315 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
316 uint64_t RecordSize = 0;
317 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
318
319 if (D->getKind() != Decl::Union) {
320 // Layout each field, for now, just sequentially, respecting alignment. In
321 // the future, this will need to be tweakable by targets.
322 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
323 const FieldDecl *FD = D->getMember(i);
324 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
325 uint64_t FieldSize = FieldInfo.first;
326 unsigned FieldAlign = FieldInfo.second;
327
328 // Round up the current record size to the field's alignment boundary.
329 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
330
331 // Place this field at the current location.
332 FieldOffsets[i] = RecordSize;
333
334 // Reserve space for this field.
335 RecordSize += FieldSize;
336
337 // Remember max struct/class alignment.
338 RecordAlign = std::max(RecordAlign, FieldAlign);
339 }
340
341 // Finally, round the size of the total struct up to the alignment of the
342 // struct itself.
343 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
344 } else {
345 // Union layout just puts each member at the start of the record.
346 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
347 const FieldDecl *FD = D->getMember(i);
348 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
349 uint64_t FieldSize = FieldInfo.first;
350 unsigned FieldAlign = FieldInfo.second;
351
352 // Round up the current record size to the field's alignment boundary.
353 RecordSize = std::max(RecordSize, FieldSize);
354
355 // Place this field at the start of the record.
356 FieldOffsets[i] = 0;
357
358 // Remember max struct/class alignment.
359 RecordAlign = std::max(RecordAlign, FieldAlign);
360 }
361 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000362
363 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
364 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000365}
366
Chris Lattnera7674d82007-07-13 22:13:22 +0000367//===----------------------------------------------------------------------===//
368// Type creation/memoization methods
369//===----------------------------------------------------------------------===//
370
371
Reid Spencer5f016e22007-07-11 17:01:13 +0000372/// getComplexType - Return the uniqued reference to the type for a complex
373/// number with the specified element type.
374QualType ASTContext::getComplexType(QualType T) {
375 // Unique pointers, to guarantee there is only one pointer of a particular
376 // structure.
377 llvm::FoldingSetNodeID ID;
378 ComplexType::Profile(ID, T);
379
380 void *InsertPos = 0;
381 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
382 return QualType(CT, 0);
383
384 // If the pointee type isn't canonical, this won't be a canonical type either,
385 // so fill in the canonical type field.
386 QualType Canonical;
387 if (!T->isCanonical()) {
388 Canonical = getComplexType(T.getCanonicalType());
389
390 // Get the new insert position for the node we care about.
391 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
392 assert(NewIP == 0 && "Shouldn't be in the map!");
393 }
394 ComplexType *New = new ComplexType(T, Canonical);
395 Types.push_back(New);
396 ComplexTypes.InsertNode(New, InsertPos);
397 return QualType(New, 0);
398}
399
400
401/// getPointerType - Return the uniqued reference to the type for a pointer to
402/// the specified type.
403QualType ASTContext::getPointerType(QualType T) {
404 // Unique pointers, to guarantee there is only one pointer of a particular
405 // structure.
406 llvm::FoldingSetNodeID ID;
407 PointerType::Profile(ID, T);
408
409 void *InsertPos = 0;
410 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
411 return QualType(PT, 0);
412
413 // If the pointee type isn't canonical, this won't be a canonical type either,
414 // so fill in the canonical type field.
415 QualType Canonical;
416 if (!T->isCanonical()) {
417 Canonical = getPointerType(T.getCanonicalType());
418
419 // Get the new insert position for the node we care about.
420 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
421 assert(NewIP == 0 && "Shouldn't be in the map!");
422 }
423 PointerType *New = new PointerType(T, Canonical);
424 Types.push_back(New);
425 PointerTypes.InsertNode(New, InsertPos);
426 return QualType(New, 0);
427}
428
429/// getReferenceType - Return the uniqued reference to the type for a reference
430/// to the specified type.
431QualType ASTContext::getReferenceType(QualType T) {
432 // Unique pointers, to guarantee there is only one pointer of a particular
433 // structure.
434 llvm::FoldingSetNodeID ID;
435 ReferenceType::Profile(ID, T);
436
437 void *InsertPos = 0;
438 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
439 return QualType(RT, 0);
440
441 // If the referencee type isn't canonical, this won't be a canonical type
442 // either, so fill in the canonical type field.
443 QualType Canonical;
444 if (!T->isCanonical()) {
445 Canonical = getReferenceType(T.getCanonicalType());
446
447 // Get the new insert position for the node we care about.
448 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
449 assert(NewIP == 0 && "Shouldn't be in the map!");
450 }
451
452 ReferenceType *New = new ReferenceType(T, Canonical);
453 Types.push_back(New);
454 ReferenceTypes.InsertNode(New, InsertPos);
455 return QualType(New, 0);
456}
457
Steve Narofffb22d962007-08-30 01:06:46 +0000458/// getConstantArrayType - Return the unique reference to the type for an
459/// array of the specified element type.
460QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000461 const llvm::APInt &ArySize,
462 ArrayType::ArraySizeModifier ASM,
463 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000464 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000465 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000466
467 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000468 if (ConstantArrayType *ATP =
469 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 return QualType(ATP, 0);
471
472 // If the element type isn't canonical, this won't be a canonical type either,
473 // so fill in the canonical type field.
474 QualType Canonical;
475 if (!EltTy->isCanonical()) {
Steve Naroffc9406122007-08-30 18:10:14 +0000476 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
477 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000478 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000479 ConstantArrayType *NewIP =
480 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
481
Reid Spencer5f016e22007-07-11 17:01:13 +0000482 assert(NewIP == 0 && "Shouldn't be in the map!");
483 }
484
Steve Naroffc9406122007-08-30 18:10:14 +0000485 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
486 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000487 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000488 Types.push_back(New);
489 return QualType(New, 0);
490}
491
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000492/// getVariableArrayType - Returns a non-unique reference to the type for a
493/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000494QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
495 ArrayType::ArraySizeModifier ASM,
496 unsigned EltTypeQuals) {
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000497 if (NumElts) {
498 // Since we don't unique expressions, it isn't possible to unique VLA's
499 // that have an expression provided for their size.
500
Ted Kremenek347b9f32007-10-30 16:41:53 +0000501 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
502 ASM, EltTypeQuals);
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000503
Ted Kremenek347b9f32007-10-30 16:41:53 +0000504 CompleteVariableArrayTypes.push_back(New);
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000505 Types.push_back(New);
506 return QualType(New, 0);
507 }
508 else {
509 // No size is provided for the VLA. These we can unique.
510 llvm::FoldingSetNodeID ID;
511 VariableArrayType::Profile(ID, EltTy);
512
513 void *InsertPos = 0;
514 if (VariableArrayType *ATP =
515 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
516 return QualType(ATP, 0);
517
518 // If the element type isn't canonical, this won't be a canonical type
519 // either, so fill in the canonical type field.
520 QualType Canonical;
521
522 if (!EltTy->isCanonical()) {
523 Canonical = getVariableArrayType(EltTy.getCanonicalType(), NumElts,
524 ASM, EltTypeQuals);
525
526 // Get the new insert position for the node we care about.
527 VariableArrayType *NewIP =
528 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
529
530 assert(NewIP == 0 && "Shouldn't be in the map!");
531 }
532
533 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
534 ASM, EltTypeQuals);
535
536 IncompleteVariableArrayTypes.InsertNode(New, InsertPos);
537 Types.push_back(New);
538 return QualType(New, 0);
539 }
Steve Narofffb22d962007-08-30 01:06:46 +0000540}
541
Steve Naroff73322922007-07-18 18:00:27 +0000542/// getVectorType - Return the unique reference to a vector type of
543/// the specified element type and size. VectorType must be a built-in type.
544QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000545 BuiltinType *baseType;
546
547 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000548 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000549
550 // Check if we've already instantiated a vector of this type.
551 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000552 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000553 void *InsertPos = 0;
554 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
555 return QualType(VTP, 0);
556
557 // If the element type isn't canonical, this won't be a canonical type either,
558 // so fill in the canonical type field.
559 QualType Canonical;
560 if (!vecType->isCanonical()) {
Steve Naroff73322922007-07-18 18:00:27 +0000561 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000562
563 // Get the new insert position for the node we care about.
564 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
565 assert(NewIP == 0 && "Shouldn't be in the map!");
566 }
567 VectorType *New = new VectorType(vecType, NumElts, Canonical);
568 VectorTypes.InsertNode(New, InsertPos);
569 Types.push_back(New);
570 return QualType(New, 0);
571}
572
Steve Naroff73322922007-07-18 18:00:27 +0000573/// getOCUVectorType - Return the unique reference to an OCU vector type of
574/// the specified element type and size. VectorType must be a built-in type.
575QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
576 BuiltinType *baseType;
577
578 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
579 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
580
581 // Check if we've already instantiated a vector of this type.
582 llvm::FoldingSetNodeID ID;
583 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
584 void *InsertPos = 0;
585 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
586 return QualType(VTP, 0);
587
588 // If the element type isn't canonical, this won't be a canonical type either,
589 // so fill in the canonical type field.
590 QualType Canonical;
591 if (!vecType->isCanonical()) {
592 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
593
594 // Get the new insert position for the node we care about.
595 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
596 assert(NewIP == 0 && "Shouldn't be in the map!");
597 }
598 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
599 VectorTypes.InsertNode(New, InsertPos);
600 Types.push_back(New);
601 return QualType(New, 0);
602}
603
Reid Spencer5f016e22007-07-11 17:01:13 +0000604/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
605///
606QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
607 // Unique functions, to guarantee there is only one function of a particular
608 // structure.
609 llvm::FoldingSetNodeID ID;
610 FunctionTypeNoProto::Profile(ID, ResultTy);
611
612 void *InsertPos = 0;
613 if (FunctionTypeNoProto *FT =
614 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
615 return QualType(FT, 0);
616
617 QualType Canonical;
618 if (!ResultTy->isCanonical()) {
619 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
620
621 // Get the new insert position for the node we care about.
622 FunctionTypeNoProto *NewIP =
623 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
624 assert(NewIP == 0 && "Shouldn't be in the map!");
625 }
626
627 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
628 Types.push_back(New);
629 FunctionTypeProtos.InsertNode(New, InsertPos);
630 return QualType(New, 0);
631}
632
633/// getFunctionType - Return a normal function type with a typed argument
634/// list. isVariadic indicates whether the argument list includes '...'.
635QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
636 unsigned NumArgs, bool isVariadic) {
637 // Unique functions, to guarantee there is only one function of a particular
638 // structure.
639 llvm::FoldingSetNodeID ID;
640 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
641
642 void *InsertPos = 0;
643 if (FunctionTypeProto *FTP =
644 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
645 return QualType(FTP, 0);
646
647 // Determine whether the type being created is already canonical or not.
648 bool isCanonical = ResultTy->isCanonical();
649 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
650 if (!ArgArray[i]->isCanonical())
651 isCanonical = false;
652
653 // If this type isn't canonical, get the canonical version of it.
654 QualType Canonical;
655 if (!isCanonical) {
656 llvm::SmallVector<QualType, 16> CanonicalArgs;
657 CanonicalArgs.reserve(NumArgs);
658 for (unsigned i = 0; i != NumArgs; ++i)
659 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
660
661 Canonical = getFunctionType(ResultTy.getCanonicalType(),
662 &CanonicalArgs[0], NumArgs,
663 isVariadic);
664
665 // Get the new insert position for the node we care about.
666 FunctionTypeProto *NewIP =
667 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
668 assert(NewIP == 0 && "Shouldn't be in the map!");
669 }
670
671 // FunctionTypeProto objects are not allocated with new because they have a
672 // variable size array (for parameter types) at the end of them.
673 FunctionTypeProto *FTP =
674 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000675 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
677 Canonical);
678 Types.push_back(FTP);
679 FunctionTypeProtos.InsertNode(FTP, InsertPos);
680 return QualType(FTP, 0);
681}
682
683/// getTypedefType - Return the unique reference to the type for the
684/// specified typename decl.
685QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
686 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
687
688 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000689 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 Types.push_back(Decl->TypeForDecl);
691 return QualType(Decl->TypeForDecl, 0);
692}
693
Steve Naroff3536b442007-09-06 21:24:23 +0000694/// getObjcInterfaceType - Return the unique reference to the type for the
695/// specified ObjC interface decl.
696QualType ASTContext::getObjcInterfaceType(ObjcInterfaceDecl *Decl) {
697 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
698
Fariborz Jahanian06cef252007-12-13 20:47:42 +0000699 Decl->TypeForDecl = new ObjcInterfaceType(Type::ObjcInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +0000700 Types.push_back(Decl->TypeForDecl);
701 return QualType(Decl->TypeForDecl, 0);
702}
703
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000704/// getObjcQualifiedInterfaceType - Return a
705/// ObjcQualifiedInterfaceType type for the given interface decl and
706/// the conforming protocol list.
707QualType ASTContext::getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl,
708 ObjcProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000709 llvm::FoldingSetNodeID ID;
Fariborz Jahanian06cef252007-12-13 20:47:42 +0000710 ObjcQualifiedInterfaceType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000711
712 void *InsertPos = 0;
713 if (ObjcQualifiedInterfaceType *QT =
714 ObjcQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
715 return QualType(QT, 0);
716
717 // No Match;
Chris Lattner00bb2832007-10-11 03:36:41 +0000718 ObjcQualifiedInterfaceType *QType =
Fariborz Jahanian06cef252007-12-13 20:47:42 +0000719 new ObjcQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000720 Types.push_back(QType);
721 ObjcQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
722 return QualType(QType, 0);
723}
724
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000725/// getObjcQualifiedIdType - Return a
Fariborz Jahanianc2937252007-12-17 21:48:49 +0000726/// getObjcQualifiedIdType type for the 'id' decl and
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000727/// the conforming protocol list.
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000728QualType ASTContext::getObjcQualifiedIdType(QualType idType,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000729 ObjcProtocolDecl **Protocols,
730 unsigned NumProtocols) {
731 llvm::FoldingSetNodeID ID;
732 ObjcQualifiedIdType::Profile(ID, Protocols, NumProtocols);
733
734 void *InsertPos = 0;
735 if (ObjcQualifiedIdType *QT =
736 ObjcQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
737 return QualType(QT, 0);
738
739 // No Match;
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000740 QualType Canonical;
741 if (!idType->isCanonical()) {
742 Canonical = getObjcQualifiedIdType(idType.getCanonicalType(),
743 Protocols, NumProtocols);
744 ObjcQualifiedIdType *NewQT =
745 ObjcQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
746 assert(NewQT == 0 && "Shouldn't be in the map!");
747 }
748
749 ObjcQualifiedIdType *QType =
750 new ObjcQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000751 Types.push_back(QType);
752 ObjcQualifiedIdTypes.InsertNode(QType, InsertPos);
753 return QualType(QType, 0);
754}
755
Steve Naroff9752f252007-08-01 18:02:17 +0000756/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
757/// TypeOfExpr AST's (since expression's are never shared). For example,
758/// multiple declarations that refer to "typeof(x)" all contain different
759/// DeclRefExpr's. This doesn't effect the type checker, since it operates
760/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000761QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroffd1861fd2007-07-31 12:34:36 +0000762 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000763 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
764 Types.push_back(toe);
765 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000766}
767
Steve Naroff9752f252007-08-01 18:02:17 +0000768/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
769/// TypeOfType AST's. The only motivation to unique these nodes would be
770/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
771/// an issue. This doesn't effect the type checker, since it operates
772/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +0000773QualType ASTContext::getTypeOfType(QualType tofType) {
774 QualType Canonical = tofType.getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000775 TypeOfType *tot = new TypeOfType(tofType, Canonical);
776 Types.push_back(tot);
777 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000778}
779
Reid Spencer5f016e22007-07-11 17:01:13 +0000780/// getTagDeclType - Return the unique reference to the type for the
781/// specified TagDecl (struct/union/class/enum) decl.
782QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +0000783 assert (Decl);
784
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000785 // The decl stores the type cache.
Ted Kremenekd778f882007-11-26 21:16:01 +0000786 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000787
788 TagType* T = new TagType(Decl, QualType());
Ted Kremenekd778f882007-11-26 21:16:01 +0000789 Types.push_back(T);
790 Decl->TypeForDecl = T;
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000791
792 return QualType(T, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000793}
794
795/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
796/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
797/// needs to agree with the definition in <stddef.h>.
798QualType ASTContext::getSizeType() const {
799 // On Darwin, size_t is defined as a "long unsigned int".
800 // FIXME: should derive from "Target".
801 return UnsignedLongTy;
802}
803
Chris Lattner8b9023b2007-07-13 03:05:23 +0000804/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
805/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
806QualType ASTContext::getPointerDiffType() const {
807 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
808 // FIXME: should derive from "Target".
809 return IntTy;
810}
811
Reid Spencer5f016e22007-07-11 17:01:13 +0000812/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
813/// routine will assert if passed a built-in type that isn't an integer or enum.
814static int getIntegerRank(QualType t) {
815 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
816 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
817 return 4;
818 }
819
820 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
821 switch (BT->getKind()) {
822 default:
823 assert(0 && "getIntegerRank(): not a built-in integer");
824 case BuiltinType::Bool:
825 return 1;
826 case BuiltinType::Char_S:
827 case BuiltinType::Char_U:
828 case BuiltinType::SChar:
829 case BuiltinType::UChar:
830 return 2;
831 case BuiltinType::Short:
832 case BuiltinType::UShort:
833 return 3;
834 case BuiltinType::Int:
835 case BuiltinType::UInt:
836 return 4;
837 case BuiltinType::Long:
838 case BuiltinType::ULong:
839 return 5;
840 case BuiltinType::LongLong:
841 case BuiltinType::ULongLong:
842 return 6;
843 }
844}
845
846/// getFloatingRank - Return a relative rank for floating point types.
847/// This routine will assert if passed a built-in type that isn't a float.
848static int getFloatingRank(QualType T) {
849 T = T.getCanonicalType();
850 if (ComplexType *CT = dyn_cast<ComplexType>(T))
851 return getFloatingRank(CT->getElementType());
852
853 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner770951b2007-11-01 05:03:41 +0000854 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000855 case BuiltinType::Float: return FloatRank;
856 case BuiltinType::Double: return DoubleRank;
857 case BuiltinType::LongDouble: return LongDoubleRank;
858 }
859}
860
Steve Naroff716c7302007-08-27 01:41:48 +0000861/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
862/// point or a complex type (based on typeDomain/typeSize).
863/// 'typeDomain' is a real floating point or complex type.
864/// 'typeSize' is a real floating point or complex type.
Steve Narofff1448a02007-08-27 01:27:54 +0000865QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
866 QualType typeSize, QualType typeDomain) const {
867 if (typeDomain->isComplexType()) {
868 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000869 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000870 case FloatRank: return FloatComplexTy;
871 case DoubleRank: return DoubleComplexTy;
872 case LongDoubleRank: return LongDoubleComplexTy;
873 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000874 }
Steve Narofff1448a02007-08-27 01:27:54 +0000875 if (typeDomain->isRealFloatingType()) {
876 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000877 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000878 case FloatRank: return FloatTy;
879 case DoubleRank: return DoubleTy;
880 case LongDoubleRank: return LongDoubleTy;
881 }
882 }
883 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattnerb1776cb2007-09-16 19:23:47 +0000884 //an invalid return value, but the assert
885 //will ensure that this code is never reached.
886 return VoidTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000887}
888
Steve Narofffb0d4962007-08-27 15:30:22 +0000889/// compareFloatingType - Handles 3 different combos:
890/// float/float, float/complex, complex/complex.
891/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
892int ASTContext::compareFloatingType(QualType lt, QualType rt) {
893 if (getFloatingRank(lt) == getFloatingRank(rt))
894 return 0;
895 if (getFloatingRank(lt) > getFloatingRank(rt))
896 return 1;
897 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000898}
899
900// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
901// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
902QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
903 if (lhs == rhs) return lhs;
904
905 bool t1Unsigned = lhs->isUnsignedIntegerType();
906 bool t2Unsigned = rhs->isUnsignedIntegerType();
907
908 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
909 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
910
911 // We have two integer types with differing signs
912 QualType unsignedType = t1Unsigned ? lhs : rhs;
913 QualType signedType = t1Unsigned ? rhs : lhs;
914
915 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
916 return unsignedType;
917 else {
918 // FIXME: Need to check if the signed type can represent all values of the
919 // unsigned type. If it can, then the result is the signed type.
920 // If it can't, then the result is the unsigned version of the signed type.
921 // Should probably add a helper that returns a signed integer type from
922 // an unsigned (and vice versa). C99 6.3.1.8.
923 return signedType;
924 }
925}
Anders Carlsson71993dd2007-08-17 05:31:46 +0000926
927// getCFConstantStringType - Return the type used for constant CFStrings.
928QualType ASTContext::getCFConstantStringType() {
929 if (!CFConstantStringTypeDecl) {
930 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
Steve Naroffbeaf2992007-11-03 11:27:19 +0000931 &Idents.get("NSConstantString"),
Anders Carlsson71993dd2007-08-17 05:31:46 +0000932 0);
Anders Carlssonf06273f2007-11-19 00:25:30 +0000933 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +0000934
935 // const int *isa;
936 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +0000937 // int flags;
938 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000939 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +0000940 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +0000941 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +0000942 FieldTypes[3] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000943 // Create fields
Anders Carlssonf06273f2007-11-19 00:25:30 +0000944 FieldDecl *FieldDecls[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +0000945
Anders Carlssonf06273f2007-11-19 00:25:30 +0000946 for (unsigned i = 0; i < 4; ++i)
Steve Narofff38661e2007-09-14 02:20:46 +0000947 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +0000948
949 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
950 }
951
952 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +0000953}
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000954
Anders Carlssone8c49532007-10-29 06:33:42 +0000955// This returns true if a type has been typedefed to BOOL:
956// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +0000957static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +0000958 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner2d998332007-10-30 20:27:44 +0000959 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000960
961 return false;
962}
963
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000964/// getObjcEncodingTypeSize returns size of type for objective-c encoding
965/// purpose.
966int ASTContext::getObjcEncodingTypeSize(QualType type) {
967 SourceLocation Loc;
968 uint64_t sz = getTypeSize(type, Loc);
969
970 // Make all integer and enum types at least as large as an int
971 if (sz > 0 && type->isIntegralType())
972 sz = std::max(sz, getTypeSize(IntTy, Loc));
973 // Treat arrays as pointers, since that's how they're passed in.
974 else if (type->isArrayType())
975 sz = getTypeSize(VoidPtrTy, Loc);
976 return sz / getTypeSize(CharTy, Loc);
977}
978
979/// getObjcEncodingForMethodDecl - Return the encoded type for this method
980/// declaration.
981void ASTContext::getObjcEncodingForMethodDecl(ObjcMethodDecl *Decl,
982 std::string& S)
983{
Fariborz Jahanianecb01e62007-11-01 17:18:37 +0000984 // Encode type qualifer, 'in', 'inout', etc. for the return type.
985 getObjcEncodingForTypeQualifier(Decl->getObjcDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000986 // Encode result type.
987 getObjcEncodingForType(Decl->getResultType(), S);
988 // Compute size of all parameters.
989 // Start with computing size of a pointer in number of bytes.
990 // FIXME: There might(should) be a better way of doing this computation!
991 SourceLocation Loc;
992 int PtrSize = getTypeSize(VoidPtrTy, Loc) / getTypeSize(CharTy, Loc);
993 // The first two arguments (self and _cmd) are pointers; account for
994 // their size.
995 int ParmOffset = 2 * PtrSize;
996 int NumOfParams = Decl->getNumParams();
997 for (int i = 0; i < NumOfParams; i++) {
998 QualType PType = Decl->getParamDecl(i)->getType();
999 int sz = getObjcEncodingTypeSize (PType);
1000 assert (sz > 0 && "getObjcEncodingForMethodDecl - Incomplete param type");
1001 ParmOffset += sz;
1002 }
1003 S += llvm::utostr(ParmOffset);
1004 S += "@0:";
1005 S += llvm::utostr(PtrSize);
1006
1007 // Argument types.
1008 ParmOffset = 2 * PtrSize;
1009 for (int i = 0; i < NumOfParams; i++) {
1010 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001011 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001012 // 'in', 'inout', etc.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001013 getObjcEncodingForTypeQualifier(
1014 Decl->getParamDecl(i)->getObjcDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001015 getObjcEncodingForType(PType, S);
1016 S += llvm::utostr(ParmOffset);
1017 ParmOffset += getObjcEncodingTypeSize(PType);
1018 }
1019}
1020
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001021void ASTContext::getObjcEncodingForType(QualType T, std::string& S) const
1022{
Anders Carlssone8c49532007-10-29 06:33:42 +00001023 // FIXME: This currently doesn't encode:
1024 // @ An object (whether statically typed or typed id)
1025 // # A class object (Class)
1026 // : A method selector (SEL)
1027 // {name=type...} A structure
1028 // (name=type...) A union
1029 // bnum A bit field of num bits
1030
1031 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001032 char encoding;
1033 switch (BT->getKind()) {
1034 case BuiltinType::Void:
1035 encoding = 'v';
1036 break;
1037 case BuiltinType::Bool:
1038 encoding = 'B';
1039 break;
1040 case BuiltinType::Char_U:
1041 case BuiltinType::UChar:
1042 encoding = 'C';
1043 break;
1044 case BuiltinType::UShort:
1045 encoding = 'S';
1046 break;
1047 case BuiltinType::UInt:
1048 encoding = 'I';
1049 break;
1050 case BuiltinType::ULong:
1051 encoding = 'L';
1052 break;
1053 case BuiltinType::ULongLong:
1054 encoding = 'Q';
1055 break;
1056 case BuiltinType::Char_S:
1057 case BuiltinType::SChar:
1058 encoding = 'c';
1059 break;
1060 case BuiltinType::Short:
1061 encoding = 's';
1062 break;
1063 case BuiltinType::Int:
1064 encoding = 'i';
1065 break;
1066 case BuiltinType::Long:
1067 encoding = 'l';
1068 break;
1069 case BuiltinType::LongLong:
1070 encoding = 'q';
1071 break;
1072 case BuiltinType::Float:
1073 encoding = 'f';
1074 break;
1075 case BuiltinType::Double:
1076 encoding = 'd';
1077 break;
1078 case BuiltinType::LongDouble:
1079 encoding = 'd';
1080 break;
1081 default:
1082 assert(0 && "Unhandled builtin type kind");
1083 }
1084
1085 S += encoding;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001086 }
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +00001087 else if (T->isObjcQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001088 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +00001089 return getObjcEncodingForType(getObjcIdType(), S);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001090
1091 }
1092 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001093 QualType PointeeTy = PT->getPointeeType();
Anders Carlsson8baaca52007-10-31 02:53:19 +00001094 if (isObjcIdType(PointeeTy) || PointeeTy->isObjcInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001095 S += '@';
1096 return;
Anders Carlsson8baaca52007-10-31 02:53:19 +00001097 } else if (isObjcClassType(PointeeTy)) {
1098 S += '#';
1099 return;
1100 } else if (isObjcSelType(PointeeTy)) {
1101 S += ':';
1102 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001103 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001104
1105 if (PointeeTy->isCharType()) {
1106 // char pointer types should be encoded as '*' unless it is a
1107 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001108 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001109 S += '*';
1110 return;
1111 }
1112 }
1113
1114 S += '^';
1115 getObjcEncodingForType(PT->getPointeeType(), S);
Anders Carlssone8c49532007-10-29 06:33:42 +00001116 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001117 S += '[';
1118
1119 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1120 S += llvm::utostr(CAT->getSize().getZExtValue());
1121 else
1122 assert(0 && "Unhandled array type!");
1123
1124 getObjcEncodingForType(AT->getElementType(), S);
1125 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001126 } else if (T->getAsFunctionType()) {
1127 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001128 } else if (const RecordType *RTy = T->getAsRecordType()) {
1129 RecordDecl *RDecl= RTy->getDecl();
1130 S += '{';
1131 S += RDecl->getName();
1132 S += '=';
1133 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1134 FieldDecl *field = RDecl->getMember(i);
1135 getObjcEncodingForType(field->getType(), S);
1136 }
1137 S += '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001138 } else if (T->isEnumeralType()) {
1139 S += 'i';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001140 } else
Steve Naroff5e711242007-12-12 22:30:11 +00001141 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001142}
1143
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001144void ASTContext::getObjcEncodingForTypeQualifier(Decl::ObjcDeclQualifier QT,
1145 std::string& S) const {
1146 if (QT & Decl::OBJC_TQ_In)
1147 S += 'n';
1148 if (QT & Decl::OBJC_TQ_Inout)
1149 S += 'N';
1150 if (QT & Decl::OBJC_TQ_Out)
1151 S += 'o';
1152 if (QT & Decl::OBJC_TQ_Bycopy)
1153 S += 'O';
1154 if (QT & Decl::OBJC_TQ_Byref)
1155 S += 'R';
1156 if (QT & Decl::OBJC_TQ_Oneway)
1157 S += 'V';
1158}
1159
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001160void ASTContext::setBuiltinVaListType(QualType T)
1161{
1162 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1163
1164 BuiltinVaListType = T;
1165}
1166
Steve Naroff7e219e42007-10-15 14:41:52 +00001167void ASTContext::setObjcIdType(TypedefDecl *TD)
1168{
1169 assert(ObjcIdType.isNull() && "'id' type already set!");
1170
1171 ObjcIdType = getTypedefType(TD);
1172
1173 // typedef struct objc_object *id;
1174 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1175 assert(ptr && "'id' incorrectly typed");
1176 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1177 assert(rec && "'id' incorrectly typed");
1178 IdStructType = rec;
1179}
1180
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001181void ASTContext::setObjcSelType(TypedefDecl *TD)
1182{
1183 assert(ObjcSelType.isNull() && "'SEL' type already set!");
1184
1185 ObjcSelType = getTypedefType(TD);
1186
1187 // typedef struct objc_selector *SEL;
1188 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1189 assert(ptr && "'SEL' incorrectly typed");
1190 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1191 assert(rec && "'SEL' incorrectly typed");
1192 SelStructType = rec;
1193}
1194
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +00001195void ASTContext::setObjcProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001196{
1197 assert(ObjcProtoType.isNull() && "'Protocol' type already set!");
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +00001198 ObjcProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001199}
1200
Anders Carlsson8baaca52007-10-31 02:53:19 +00001201void ASTContext::setObjcClassType(TypedefDecl *TD)
1202{
1203 assert(ObjcClassType.isNull() && "'Class' type already set!");
1204
1205 ObjcClassType = getTypedefType(TD);
1206
1207 // typedef struct objc_class *Class;
1208 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1209 assert(ptr && "'Class' incorrectly typed");
1210 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1211 assert(rec && "'Class' incorrectly typed");
1212 ClassStructType = rec;
1213}
1214
Steve Naroff21988912007-10-15 23:35:17 +00001215void ASTContext::setObjcConstantStringInterface(ObjcInterfaceDecl *Decl) {
1216 assert(ObjcConstantStringType.isNull() &&
1217 "'NSConstantString' type already set!");
1218
1219 ObjcConstantStringType = getObjcInterfaceType(Decl);
1220}
1221
Steve Naroffec0550f2007-10-15 20:41:53 +00001222bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1223 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1224 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1225
1226 return lBuiltin->getKind() == rBuiltin->getKind();
1227}
1228
Fariborz Jahanianb145e7d2007-12-21 17:34:43 +00001229/// objcTypesAreCompatible - This routine is called when two types
1230/// are of different class; one is interface type or is
1231/// a qualified interface type and the other type is of a different class.
1232/// Example, II or II<P>.
Steve Naroffec0550f2007-10-15 20:41:53 +00001233bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
1234 if (lhs->isObjcInterfaceType() && isObjcIdType(rhs))
1235 return true;
1236 else if (isObjcIdType(lhs) && rhs->isObjcInterfaceType())
1237 return true;
Fariborz Jahanianb145e7d2007-12-21 17:34:43 +00001238 if (ObjcInterfaceType *lhsIT =
1239 dyn_cast<ObjcInterfaceType>(lhs.getCanonicalType().getTypePtr())) {
1240 ObjcQualifiedInterfaceType *rhsQI =
1241 dyn_cast<ObjcQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
1242 return rhsQI && (lhsIT->getDecl() == rhsQI->getDecl());
1243 }
1244 else if (ObjcInterfaceType *rhsIT =
1245 dyn_cast<ObjcInterfaceType>(rhs.getCanonicalType().getTypePtr())) {
1246 ObjcQualifiedInterfaceType *lhsQI =
1247 dyn_cast<ObjcQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
1248 return lhsQI && (rhsIT->getDecl() == lhsQI->getDecl());
1249 }
Steve Naroffec0550f2007-10-15 20:41:53 +00001250 return false;
1251}
1252
1253bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
Fariborz Jahanian0f01deb2007-12-20 22:37:58 +00001254 if (lhs == rhs)
1255 return true;
1256 ObjcInterfaceType *lhsIT = cast<ObjcInterfaceType>(lhs.getTypePtr());
1257 ObjcInterfaceType *rhsIT = cast<ObjcInterfaceType>(rhs.getTypePtr());
1258 ObjcInterfaceDecl *rhsIDecl = rhsIT->getDecl();
1259 ObjcInterfaceDecl *lhsIDecl = lhsIT->getDecl();
1260 // rhs is derived from lhs it is OK; else it is not OK.
1261 while (rhsIDecl != NULL) {
1262 if (rhsIDecl == lhsIDecl)
1263 return true;
1264 rhsIDecl = rhsIDecl->getSuperClass();
1265 }
1266 return false;
Steve Naroffec0550f2007-10-15 20:41:53 +00001267}
1268
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001269bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
1270 QualType rhs) {
1271 ObjcQualifiedInterfaceType *lhsQI =
1272 dyn_cast<ObjcQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
1273 assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
1274 ObjcQualifiedInterfaceType *rhsQI =
1275 dyn_cast<ObjcQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
1276 assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
Fariborz Jahanian06cef252007-12-13 20:47:42 +00001277 if (!interfaceTypesAreCompatible(getObjcInterfaceType(lhsQI->getDecl()),
1278 getObjcInterfaceType(rhsQI->getDecl())))
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001279 return false;
1280 /* All protocols in lhs must have a presense in rhs. */
1281 for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
1282 bool match = false;
1283 ObjcProtocolDecl *lhsProto = lhsQI->getProtocols(i);
1284 for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
1285 ObjcProtocolDecl *rhsProto = rhsQI->getProtocols(j);
1286 if (lhsProto == rhsProto) {
1287 match = true;
1288 break;
1289 }
1290 }
1291 if (!match)
1292 return false;
1293 }
1294 return true;
1295}
1296
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001297/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
1298/// inheritance hierarchy of 'rProto'.
1299static bool ProtocolCompatibleWithProtocol(ObjcProtocolDecl *lProto,
1300 ObjcProtocolDecl *rProto) {
1301 if (lProto == rProto)
1302 return true;
1303 ObjcProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
1304 for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
1305 if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
1306 return true;
1307 return false;
1308}
1309
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001310/// ClassImplementsProtocol - Checks that 'lProto' protocol
1311/// has been implemented in IDecl class, its super class or categories (if
1312/// lookupCategory is true).
1313static bool ClassImplementsProtocol(ObjcProtocolDecl *lProto,
1314 ObjcInterfaceDecl *IDecl,
1315 bool lookupCategory) {
1316
1317 // 1st, look up the class.
1318 ObjcProtocolDecl **protoList = IDecl->getReferencedProtocols();
1319 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
1320 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1321 return true;
1322 }
1323
1324 // 2nd, look up the category.
1325 if (lookupCategory)
1326 for (ObjcCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
1327 CDecl = CDecl->getNextClassCategory()) {
1328 protoList = CDecl->getReferencedProtocols();
1329 for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
1330 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1331 return true;
1332 }
1333 }
1334
1335 // 3rd, look up the super class(s)
1336 if (IDecl->getSuperClass())
1337 return
1338 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory);
1339
1340 return false;
1341}
1342
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001343/// ObjcQualifiedIdTypesAreCompatible - Compares two types, at least
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001344/// one of which is a protocol qualified 'id' type. When 'compare'
1345/// is true it is for comparison; when false, for assignment/initialization.
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001346bool ASTContext::ObjcQualifiedIdTypesAreCompatible(QualType lhs,
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001347 QualType rhs,
1348 bool compare) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001349 // match id<P..> with an 'id' type in all cases.
1350 if (const PointerType *PT = lhs->getAsPointerType()) {
1351 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001352 if (isObjcIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001353 return true;
1354
1355 }
1356 else if (const PointerType *PT = rhs->getAsPointerType()) {
1357 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001358 if (isObjcIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001359 return true;
1360
1361 }
1362
1363 ObjcQualifiedInterfaceType *lhsQI = 0;
1364 ObjcQualifiedInterfaceType *rhsQI = 0;
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001365 ObjcInterfaceDecl *lhsID = 0;
1366 ObjcInterfaceDecl *rhsID = 0;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001367 ObjcQualifiedIdType *lhsQID = dyn_cast<ObjcQualifiedIdType>(lhs);
1368 ObjcQualifiedIdType *rhsQID = dyn_cast<ObjcQualifiedIdType>(rhs);
1369
1370 if (lhsQID) {
1371 if (!rhsQID && rhs->getTypeClass() == Type::Pointer) {
1372 QualType rtype =
1373 cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1374 rhsQI =
1375 dyn_cast<ObjcQualifiedInterfaceType>(
1376 rtype.getCanonicalType().getTypePtr());
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001377 if (!rhsQI) {
1378 ObjcInterfaceType *IT = dyn_cast<ObjcInterfaceType>(
1379 rtype.getCanonicalType().getTypePtr());
1380 if (IT)
1381 rhsID = IT->getDecl();
1382 }
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001383 }
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001384 if (!rhsQI && !rhsQID && !rhsID)
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001385 return false;
1386
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001387 unsigned numRhsProtocols;
1388 ObjcProtocolDecl **rhsProtoList;
1389 if (rhsQI) {
1390 numRhsProtocols = rhsQI->getNumProtocols();
1391 rhsProtoList = rhsQI->getReferencedProtocols();
1392 }
1393 else if (rhsQID) {
1394 numRhsProtocols = rhsQID->getNumProtocols();
1395 rhsProtoList = rhsQID->getReferencedProtocols();
1396 }
1397
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001398 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001399 ObjcProtocolDecl *lhsProto = lhsQID->getProtocols(i);
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001400 bool match = false;
1401
1402 // when comparing an id<P> on lhs with a static type on rhs,
1403 // see if static class implements all of id's protocols, directly or
1404 // through its super class and categories.
1405 if (rhsID) {
1406 if (ClassImplementsProtocol(lhsProto, rhsID, true))
1407 match = true;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001408 }
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001409 else for (unsigned j = 0; j < numRhsProtocols; j++) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001410 ObjcProtocolDecl *rhsProto = rhsProtoList[j];
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001411 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1412 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001413 match = true;
1414 break;
1415 }
1416 }
1417 if (!match)
1418 return false;
1419 }
1420 }
1421 else if (rhsQID) {
1422 if (!lhsQID && lhs->getTypeClass() == Type::Pointer) {
1423 QualType ltype =
1424 cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1425 lhsQI =
1426 dyn_cast<ObjcQualifiedInterfaceType>(
1427 ltype.getCanonicalType().getTypePtr());
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001428 if (!lhsQI) {
1429 ObjcInterfaceType *IT = dyn_cast<ObjcInterfaceType>(
1430 ltype.getCanonicalType().getTypePtr());
1431 if (IT)
1432 lhsID = IT->getDecl();
1433 }
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001434 }
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001435 if (!lhsQI && !lhsQID && !lhsID)
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001436 return false;
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001437
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001438 unsigned numLhsProtocols;
1439 ObjcProtocolDecl **lhsProtoList;
1440 if (lhsQI) {
1441 numLhsProtocols = lhsQI->getNumProtocols();
1442 lhsProtoList = lhsQI->getReferencedProtocols();
1443 }
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001444 else if (lhsQID) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001445 numLhsProtocols = lhsQID->getNumProtocols();
1446 lhsProtoList = lhsQID->getReferencedProtocols();
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001447 }
1448 bool match = false;
1449 // for static type vs. qualified 'id' type, check that class implements
1450 // one of 'id's protocols.
1451 if (lhsID) {
1452 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
1453 ObjcProtocolDecl *rhsProto = rhsQID->getProtocols(j);
1454 if (ClassImplementsProtocol(rhsProto, lhsID, compare)) {
1455 match = true;
1456 break;
1457 }
1458 }
1459 }
1460 else for (unsigned i =0; i < numLhsProtocols; i++) {
1461 match = false;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001462 ObjcProtocolDecl *lhsProto = lhsProtoList[i];
1463 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
1464 ObjcProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001465 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1466 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001467 match = true;
1468 break;
1469 }
1470 }
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001471 }
1472 if (!match)
1473 return false;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001474 }
1475 return true;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001476}
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001477
Chris Lattner770951b2007-11-01 05:03:41 +00001478bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1479 const VectorType *lVector = lhs->getAsVectorType();
1480 const VectorType *rVector = rhs->getAsVectorType();
1481
1482 if ((lVector->getElementType().getCanonicalType() ==
1483 rVector->getElementType().getCanonicalType()) &&
1484 (lVector->getNumElements() == rVector->getNumElements()))
1485 return true;
1486 return false;
1487}
1488
Steve Naroffec0550f2007-10-15 20:41:53 +00001489// C99 6.2.7p1: If both are complete types, then the following additional
1490// requirements apply...FIXME (handle compatibility across source files).
1491bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
1492 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
1493 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
1494
1495 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
1496 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1497 return true;
1498 }
1499 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
1500 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1501 return true;
1502 }
Steve Naroffab373092007-11-07 06:03:51 +00001503 // "Class" and "id" are compatible built-in structure types.
1504 if (isObjcIdType(lhs) && isObjcClassType(rhs) ||
1505 isObjcClassType(lhs) && isObjcIdType(rhs))
1506 return true;
Steve Naroffec0550f2007-10-15 20:41:53 +00001507 return false;
1508}
1509
1510bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1511 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1512 // identically qualified and both shall be pointers to compatible types.
1513 if (lhs.getQualifiers() != rhs.getQualifiers())
1514 return false;
1515
1516 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1517 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1518
1519 return typesAreCompatible(ltype, rtype);
1520}
1521
Bill Wendling43d69752007-12-03 07:33:35 +00001522// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroffec0550f2007-10-15 20:41:53 +00001523// reference to T, the operation assigns to the object of type T denoted by the
1524// reference.
1525bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1526 QualType ltype = lhs;
1527
1528 if (lhs->isReferenceType())
1529 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1530
1531 QualType rtype = rhs;
1532
1533 if (rhs->isReferenceType())
1534 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1535
1536 return typesAreCompatible(ltype, rtype);
1537}
1538
1539bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1540 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1541 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1542 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1543 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1544
1545 // first check the return types (common between C99 and K&R).
1546 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1547 return false;
1548
1549 if (lproto && rproto) { // two C99 style function prototypes
1550 unsigned lproto_nargs = lproto->getNumArgs();
1551 unsigned rproto_nargs = rproto->getNumArgs();
1552
1553 if (lproto_nargs != rproto_nargs)
1554 return false;
1555
1556 // both prototypes have the same number of arguments.
1557 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1558 (rproto->isVariadic() && !lproto->isVariadic()))
1559 return false;
1560
1561 // The use of ellipsis agree...now check the argument types.
1562 for (unsigned i = 0; i < lproto_nargs; i++)
1563 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
1564 return false;
1565 return true;
1566 }
1567 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1568 return true;
1569
1570 // we have a mixture of K&R style with C99 prototypes
1571 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1572
1573 if (proto->isVariadic())
1574 return false;
1575
1576 // FIXME: Each parameter type T in the prototype must be compatible with the
1577 // type resulting from applying the usual argument conversions to T.
1578 return true;
1579}
1580
1581bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
1582 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
1583 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
1584
1585 if (!typesAreCompatible(ltype, rtype))
1586 return false;
1587
1588 // FIXME: If both types specify constant sizes, then the sizes must also be
1589 // the same. Even if the sizes are the same, GCC produces an error.
1590 return true;
1591}
1592
1593/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1594/// both shall have the identically qualified version of a compatible type.
1595/// C99 6.2.7p1: Two types have compatible types if their types are the
1596/// same. See 6.7.[2,3,5] for additional rules.
1597bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
1598 QualType lcanon = lhs.getCanonicalType();
1599 QualType rcanon = rhs.getCanonicalType();
1600
1601 // If two types are identical, they are are compatible
1602 if (lcanon == rcanon)
1603 return true;
Bill Wendling43d69752007-12-03 07:33:35 +00001604
1605 // C++ [expr]: If an expression initially has the type "reference to T", the
1606 // type is adjusted to "T" prior to any further analysis, the expression
1607 // designates the object or function denoted by the reference, and the
1608 // expression is an lvalue.
1609 if (lcanon->getTypeClass() == Type::Reference)
1610 lcanon = cast<ReferenceType>(lcanon)->getReferenceeType();
1611 if (rcanon->getTypeClass() == Type::Reference)
1612 rcanon = cast<ReferenceType>(rcanon)->getReferenceeType();
Steve Naroffec0550f2007-10-15 20:41:53 +00001613
1614 // If the canonical type classes don't match, they can't be compatible
1615 if (lcanon->getTypeClass() != rcanon->getTypeClass()) {
1616 // For Objective-C, it is possible for two types to be compatible
1617 // when their classes don't match (when dealing with "id"). If either type
1618 // is an interface, we defer to objcTypesAreCompatible().
1619 if (lcanon->isObjcInterfaceType() || rcanon->isObjcInterfaceType())
1620 return objcTypesAreCompatible(lcanon, rcanon);
1621 return false;
1622 }
1623 switch (lcanon->getTypeClass()) {
1624 case Type::Pointer:
1625 return pointerTypesAreCompatible(lcanon, rcanon);
Steve Naroffec0550f2007-10-15 20:41:53 +00001626 case Type::ConstantArray:
1627 case Type::VariableArray:
1628 return arrayTypesAreCompatible(lcanon, rcanon);
1629 case Type::FunctionNoProto:
1630 case Type::FunctionProto:
1631 return functionTypesAreCompatible(lcanon, rcanon);
1632 case Type::Tagged: // handle structures, unions
1633 return tagTypesAreCompatible(lcanon, rcanon);
1634 case Type::Builtin:
1635 return builtinTypesAreCompatible(lcanon, rcanon);
1636 case Type::ObjcInterface:
1637 return interfaceTypesAreCompatible(lcanon, rcanon);
Chris Lattner770951b2007-11-01 05:03:41 +00001638 case Type::Vector:
1639 case Type::OCUVector:
1640 return vectorTypesAreCompatible(lcanon, rcanon);
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001641 case Type::ObjcQualifiedInterface:
1642 return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
Steve Naroffec0550f2007-10-15 20:41:53 +00001643 default:
1644 assert(0 && "unexpected type");
1645 }
1646 return true; // should never get here...
1647}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001648
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001649/// Emit - Serialize an ASTContext object to Bitcode.
1650void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek54513502007-10-31 20:00:03 +00001651 S.EmitRef(SourceMgr);
1652 S.EmitRef(Target);
1653 S.EmitRef(Idents);
1654 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001655
Ted Kremenekfee04522007-10-31 22:44:07 +00001656 // Emit the size of the type vector so that we can reserve that size
1657 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00001658 S.EmitInt(Types.size());
1659
Ted Kremenek03ed4402007-11-13 22:02:55 +00001660 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1661 I!=E;++I)
1662 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001663
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001664 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001665}
1666
Ted Kremenek0f84c002007-11-13 00:25:37 +00001667ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenekfee04522007-10-31 22:44:07 +00001668 SourceManager &SM = D.ReadRef<SourceManager>();
1669 TargetInfo &t = D.ReadRef<TargetInfo>();
1670 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1671 SelectorTable &sels = D.ReadRef<SelectorTable>();
1672
1673 unsigned size_reserve = D.ReadInt();
1674
1675 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1676
Ted Kremenek03ed4402007-11-13 22:02:55 +00001677 for (unsigned i = 0; i < size_reserve; ++i)
1678 Type::Create(*A,i,D);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001679
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001680 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00001681
1682 return A;
1683}