blob: def254ac46410c6def21df381173657f6592c8be [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) {
Anders Carlsson042c4e72008-02-16 19:51:27 +0000322 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
323 RecordAlign = std::max(RecordAlign, AA->getAlignment());
324
Anders Carlsson6a24acb2008-02-16 01:20:23 +0000325 bool StructIsPacked = D->getAttr<PackedAttr>();
326
Chris Lattner464175b2007-07-18 17:52:12 +0000327 // Layout each field, for now, just sequentially, respecting alignment. In
328 // the future, this will need to be tweakable by targets.
329 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
330 const FieldDecl *FD = D->getMember(i);
Anders Carlsson6a24acb2008-02-16 01:20:23 +0000331 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
Eli Friedman75afb582008-02-06 05:33:51 +0000332 uint64_t FieldSize;
333 unsigned FieldAlign;
334 if (FD->getType()->isIncompleteType()) {
335 // This must be a flexible array member; we can't directly
336 // query getTypeInfo about these, so we figure it out here.
337 // Flexible array members don't have any size, but they
338 // have to be aligned appropriately for their element type.
Anders Carlsson042c4e72008-02-16 19:51:27 +0000339
340 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
341 FieldAlign = AA->getAlignment();
342 else if (FieldIsPacked)
343 FieldAlign = 8;
344 else {
345 const ArrayType* ATy = FD->getType()->getAsArrayType();
346 FieldAlign = getTypeAlign(ATy->getElementType(), L);
347 }
Eli Friedman75afb582008-02-06 05:33:51 +0000348 FieldSize = 0;
349 } else {
350 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
351 FieldSize = FieldInfo.first;
Anders Carlsson042c4e72008-02-16 19:51:27 +0000352
353 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
354 FieldAlign = AA->getAlignment();
355 else if (FieldIsPacked)
356 FieldAlign = 8;
357 else
358 FieldAlign = FieldInfo.second;
Eli Friedman75afb582008-02-06 05:33:51 +0000359 }
360
Chris Lattner464175b2007-07-18 17:52:12 +0000361 // Round up the current record size to the field's alignment boundary.
362 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
363
364 // Place this field at the current location.
365 FieldOffsets[i] = RecordSize;
366
367 // Reserve space for this field.
368 RecordSize += FieldSize;
369
370 // Remember max struct/class alignment.
371 RecordAlign = std::max(RecordAlign, FieldAlign);
372 }
373
374 // Finally, round the size of the total struct up to the alignment of the
375 // struct itself.
376 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
377 } else {
378 // Union layout just puts each member at the start of the record.
379 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
380 const FieldDecl *FD = D->getMember(i);
381 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
382 uint64_t FieldSize = FieldInfo.first;
383 unsigned FieldAlign = FieldInfo.second;
384
385 // Round up the current record size to the field's alignment boundary.
386 RecordSize = std::max(RecordSize, FieldSize);
387
388 // Place this field at the start of the record.
389 FieldOffsets[i] = 0;
390
391 // Remember max struct/class alignment.
392 RecordAlign = std::max(RecordAlign, FieldAlign);
393 }
394 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000395
396 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
397 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000398}
399
Chris Lattnera7674d82007-07-13 22:13:22 +0000400//===----------------------------------------------------------------------===//
401// Type creation/memoization methods
402//===----------------------------------------------------------------------===//
403
Christopher Lambebb97e92008-02-04 02:31:56 +0000404QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
405 // Check if we've already instantiated an address space qual'd type of this type.
406 llvm::FoldingSetNodeID ID;
407 ASQualType::Profile(ID, T, AddressSpace);
408 void *InsertPos = 0;
409 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
410 return QualType(ASQy, 0);
411
412 // If the base type isn't canonical, this won't be a canonical type either,
413 // so fill in the canonical type field.
414 QualType Canonical;
415 if (!T->isCanonical()) {
416 Canonical = getASQualType(T.getCanonicalType(), AddressSpace);
417
418 // Get the new insert position for the node we care about.
419 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
420 assert(NewIP == 0 && "Shouldn't be in the map!");
421 }
422 ASQualType *New = new ASQualType(T, Canonical, AddressSpace);
423 ASQualTypes.InsertNode(New, InsertPos);
424 Types.push_back(New);
425 return QualType(New, 0);
426}
427
Chris Lattnera7674d82007-07-13 22:13:22 +0000428
Reid Spencer5f016e22007-07-11 17:01:13 +0000429/// getComplexType - Return the uniqued reference to the type for a complex
430/// number with the specified element type.
431QualType ASTContext::getComplexType(QualType T) {
432 // Unique pointers, to guarantee there is only one pointer of a particular
433 // structure.
434 llvm::FoldingSetNodeID ID;
435 ComplexType::Profile(ID, T);
436
437 void *InsertPos = 0;
438 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
439 return QualType(CT, 0);
440
441 // If the pointee type isn't canonical, this won't be a canonical type either,
442 // so fill in the canonical type field.
443 QualType Canonical;
444 if (!T->isCanonical()) {
445 Canonical = getComplexType(T.getCanonicalType());
446
447 // Get the new insert position for the node we care about.
448 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
449 assert(NewIP == 0 && "Shouldn't be in the map!");
450 }
451 ComplexType *New = new ComplexType(T, Canonical);
452 Types.push_back(New);
453 ComplexTypes.InsertNode(New, InsertPos);
454 return QualType(New, 0);
455}
456
457
458/// getPointerType - Return the uniqued reference to the type for a pointer to
459/// the specified type.
460QualType ASTContext::getPointerType(QualType T) {
461 // Unique pointers, to guarantee there is only one pointer of a particular
462 // structure.
463 llvm::FoldingSetNodeID ID;
464 PointerType::Profile(ID, T);
465
466 void *InsertPos = 0;
467 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
468 return QualType(PT, 0);
469
470 // If the pointee type isn't canonical, this won't be a canonical type either,
471 // so fill in the canonical type field.
472 QualType Canonical;
473 if (!T->isCanonical()) {
474 Canonical = getPointerType(T.getCanonicalType());
475
476 // Get the new insert position for the node we care about.
477 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
478 assert(NewIP == 0 && "Shouldn't be in the map!");
479 }
480 PointerType *New = new PointerType(T, Canonical);
481 Types.push_back(New);
482 PointerTypes.InsertNode(New, InsertPos);
483 return QualType(New, 0);
484}
485
486/// getReferenceType - Return the uniqued reference to the type for a reference
487/// to the specified type.
488QualType ASTContext::getReferenceType(QualType T) {
489 // Unique pointers, to guarantee there is only one pointer of a particular
490 // structure.
491 llvm::FoldingSetNodeID ID;
492 ReferenceType::Profile(ID, T);
493
494 void *InsertPos = 0;
495 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
496 return QualType(RT, 0);
497
498 // If the referencee type isn't canonical, this won't be a canonical type
499 // either, so fill in the canonical type field.
500 QualType Canonical;
501 if (!T->isCanonical()) {
502 Canonical = getReferenceType(T.getCanonicalType());
503
504 // Get the new insert position for the node we care about.
505 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
506 assert(NewIP == 0 && "Shouldn't be in the map!");
507 }
508
509 ReferenceType *New = new ReferenceType(T, Canonical);
510 Types.push_back(New);
511 ReferenceTypes.InsertNode(New, InsertPos);
512 return QualType(New, 0);
513}
514
Steve Narofffb22d962007-08-30 01:06:46 +0000515/// getConstantArrayType - Return the unique reference to the type for an
516/// array of the specified element type.
517QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000518 const llvm::APInt &ArySize,
519 ArrayType::ArraySizeModifier ASM,
520 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000521 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000522 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000523
524 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000525 if (ConstantArrayType *ATP =
526 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000527 return QualType(ATP, 0);
528
529 // If the element type isn't canonical, this won't be a canonical type either,
530 // so fill in the canonical type field.
531 QualType Canonical;
532 if (!EltTy->isCanonical()) {
Steve Naroffc9406122007-08-30 18:10:14 +0000533 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
534 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000535 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000536 ConstantArrayType *NewIP =
537 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
538
Reid Spencer5f016e22007-07-11 17:01:13 +0000539 assert(NewIP == 0 && "Shouldn't be in the map!");
540 }
541
Steve Naroffc9406122007-08-30 18:10:14 +0000542 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
543 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000544 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000545 Types.push_back(New);
546 return QualType(New, 0);
547}
548
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000549/// getVariableArrayType - Returns a non-unique reference to the type for a
550/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000551QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
552 ArrayType::ArraySizeModifier ASM,
553 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000554 // Since we don't unique expressions, it isn't possible to unique VLA's
555 // that have an expression provided for their size.
556
557 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
558 ASM, EltTypeQuals);
559
560 VariableArrayTypes.push_back(New);
561 Types.push_back(New);
562 return QualType(New, 0);
563}
564
565QualType ASTContext::getIncompleteArrayType(QualType EltTy,
566 ArrayType::ArraySizeModifier ASM,
567 unsigned EltTypeQuals) {
568 llvm::FoldingSetNodeID ID;
569 IncompleteArrayType::Profile(ID, EltTy);
570
571 void *InsertPos = 0;
572 if (IncompleteArrayType *ATP =
573 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
574 return QualType(ATP, 0);
575
576 // If the element type isn't canonical, this won't be a canonical type
577 // either, so fill in the canonical type field.
578 QualType Canonical;
579
580 if (!EltTy->isCanonical()) {
581 Canonical = getIncompleteArrayType(EltTy.getCanonicalType(),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000582 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000583
584 // Get the new insert position for the node we care about.
585 IncompleteArrayType *NewIP =
586 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
587
588 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000589 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000590
591 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
592 ASM, EltTypeQuals);
593
594 IncompleteArrayTypes.InsertNode(New, InsertPos);
595 Types.push_back(New);
596 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000597}
598
Steve Naroff73322922007-07-18 18:00:27 +0000599/// getVectorType - Return the unique reference to a vector type of
600/// the specified element type and size. VectorType must be a built-in type.
601QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 BuiltinType *baseType;
603
604 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000605 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000606
607 // Check if we've already instantiated a vector of this type.
608 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000609 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 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()) {
Steve Naroff73322922007-07-18 18:00:27 +0000618 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000619
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 VectorType *New = new VectorType(vecType, NumElts, Canonical);
625 VectorTypes.InsertNode(New, InsertPos);
626 Types.push_back(New);
627 return QualType(New, 0);
628}
629
Steve Naroff73322922007-07-18 18:00:27 +0000630/// getOCUVectorType - Return the unique reference to an OCU vector type of
631/// the specified element type and size. VectorType must be a built-in type.
632QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
633 BuiltinType *baseType;
634
635 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
636 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
637
638 // Check if we've already instantiated a vector of this type.
639 llvm::FoldingSetNodeID ID;
640 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
641 void *InsertPos = 0;
642 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
643 return QualType(VTP, 0);
644
645 // If the element type isn't canonical, this won't be a canonical type either,
646 // so fill in the canonical type field.
647 QualType Canonical;
648 if (!vecType->isCanonical()) {
649 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
650
651 // Get the new insert position for the node we care about.
652 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
653 assert(NewIP == 0 && "Shouldn't be in the map!");
654 }
655 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
656 VectorTypes.InsertNode(New, InsertPos);
657 Types.push_back(New);
658 return QualType(New, 0);
659}
660
Reid Spencer5f016e22007-07-11 17:01:13 +0000661/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
662///
663QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
664 // Unique functions, to guarantee there is only one function of a particular
665 // structure.
666 llvm::FoldingSetNodeID ID;
667 FunctionTypeNoProto::Profile(ID, ResultTy);
668
669 void *InsertPos = 0;
670 if (FunctionTypeNoProto *FT =
671 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
672 return QualType(FT, 0);
673
674 QualType Canonical;
675 if (!ResultTy->isCanonical()) {
676 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
677
678 // Get the new insert position for the node we care about.
679 FunctionTypeNoProto *NewIP =
680 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
681 assert(NewIP == 0 && "Shouldn't be in the map!");
682 }
683
684 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
685 Types.push_back(New);
686 FunctionTypeProtos.InsertNode(New, InsertPos);
687 return QualType(New, 0);
688}
689
690/// getFunctionType - Return a normal function type with a typed argument
691/// list. isVariadic indicates whether the argument list includes '...'.
692QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
693 unsigned NumArgs, bool isVariadic) {
694 // Unique functions, to guarantee there is only one function of a particular
695 // structure.
696 llvm::FoldingSetNodeID ID;
697 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
698
699 void *InsertPos = 0;
700 if (FunctionTypeProto *FTP =
701 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
702 return QualType(FTP, 0);
703
704 // Determine whether the type being created is already canonical or not.
705 bool isCanonical = ResultTy->isCanonical();
706 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
707 if (!ArgArray[i]->isCanonical())
708 isCanonical = false;
709
710 // If this type isn't canonical, get the canonical version of it.
711 QualType Canonical;
712 if (!isCanonical) {
713 llvm::SmallVector<QualType, 16> CanonicalArgs;
714 CanonicalArgs.reserve(NumArgs);
715 for (unsigned i = 0; i != NumArgs; ++i)
716 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
717
718 Canonical = getFunctionType(ResultTy.getCanonicalType(),
719 &CanonicalArgs[0], NumArgs,
720 isVariadic);
721
722 // Get the new insert position for the node we care about.
723 FunctionTypeProto *NewIP =
724 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
725 assert(NewIP == 0 && "Shouldn't be in the map!");
726 }
727
728 // FunctionTypeProto objects are not allocated with new because they have a
729 // variable size array (for parameter types) at the end of them.
730 FunctionTypeProto *FTP =
731 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000732 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
734 Canonical);
735 Types.push_back(FTP);
736 FunctionTypeProtos.InsertNode(FTP, InsertPos);
737 return QualType(FTP, 0);
738}
739
740/// getTypedefType - Return the unique reference to the type for the
741/// specified typename decl.
742QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
743 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
744
745 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000746 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000747 Types.push_back(Decl->TypeForDecl);
748 return QualType(Decl->TypeForDecl, 0);
749}
750
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000751/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +0000752/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000753QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +0000754 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
755
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000756 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +0000757 Types.push_back(Decl->TypeForDecl);
758 return QualType(Decl->TypeForDecl, 0);
759}
760
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000761/// getObjCQualifiedInterfaceType - Return a
762/// ObjCQualifiedInterfaceType type for the given interface decl and
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000763/// the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000764QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
765 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000766 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000767 ObjCQualifiedInterfaceType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000768
769 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000770 if (ObjCQualifiedInterfaceType *QT =
771 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000772 return QualType(QT, 0);
773
774 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000775 ObjCQualifiedInterfaceType *QType =
776 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000777 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000778 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000779 return QualType(QType, 0);
780}
781
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000782/// getObjCQualifiedIdType - Return a
783/// getObjCQualifiedIdType type for the 'id' decl and
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000784/// the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000785QualType ASTContext::getObjCQualifiedIdType(QualType idType,
786 ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000787 unsigned NumProtocols) {
788 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000789 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000790
791 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000792 if (ObjCQualifiedIdType *QT =
793 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000794 return QualType(QT, 0);
795
796 // No Match;
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000797 QualType Canonical;
798 if (!idType->isCanonical()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000799 Canonical = getObjCQualifiedIdType(idType.getCanonicalType(),
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000800 Protocols, NumProtocols);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000801 ObjCQualifiedIdType *NewQT =
802 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000803 assert(NewQT == 0 && "Shouldn't be in the map!");
804 }
805
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000806 ObjCQualifiedIdType *QType =
807 new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000808 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000809 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000810 return QualType(QType, 0);
811}
812
Steve Naroff9752f252007-08-01 18:02:17 +0000813/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
814/// TypeOfExpr AST's (since expression's are never shared). For example,
815/// multiple declarations that refer to "typeof(x)" all contain different
816/// DeclRefExpr's. This doesn't effect the type checker, since it operates
817/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000818QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroffd1861fd2007-07-31 12:34:36 +0000819 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000820 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
821 Types.push_back(toe);
822 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000823}
824
Steve Naroff9752f252007-08-01 18:02:17 +0000825/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
826/// TypeOfType AST's. The only motivation to unique these nodes would be
827/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
828/// an issue. This doesn't effect the type checker, since it operates
829/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +0000830QualType ASTContext::getTypeOfType(QualType tofType) {
831 QualType Canonical = tofType.getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000832 TypeOfType *tot = new TypeOfType(tofType, Canonical);
833 Types.push_back(tot);
834 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000835}
836
Reid Spencer5f016e22007-07-11 17:01:13 +0000837/// getTagDeclType - Return the unique reference to the type for the
838/// specified TagDecl (struct/union/class/enum) decl.
839QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +0000840 assert (Decl);
841
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000842 // The decl stores the type cache.
Ted Kremenekd778f882007-11-26 21:16:01 +0000843 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000844
845 TagType* T = new TagType(Decl, QualType());
Ted Kremenekd778f882007-11-26 21:16:01 +0000846 Types.push_back(T);
847 Decl->TypeForDecl = T;
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000848
849 return QualType(T, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000850}
851
852/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
853/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
854/// needs to agree with the definition in <stddef.h>.
855QualType ASTContext::getSizeType() const {
856 // On Darwin, size_t is defined as a "long unsigned int".
857 // FIXME: should derive from "Target".
858 return UnsignedLongTy;
859}
860
Eli Friedmanfd888a52008-02-12 08:29:21 +0000861/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
862/// width of characters in wide strings, The value is target dependent and
863/// needs to agree with the definition in <stddef.h>.
864QualType ASTContext::getWcharType() const {
865 // On Darwin, wchar_t is defined as a "int".
866 // FIXME: should derive from "Target".
867 return IntTy;
868}
869
Chris Lattner8b9023b2007-07-13 03:05:23 +0000870/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
871/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
872QualType ASTContext::getPointerDiffType() const {
873 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
874 // FIXME: should derive from "Target".
875 return IntTy;
876}
877
Reid Spencer5f016e22007-07-11 17:01:13 +0000878/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
879/// routine will assert if passed a built-in type that isn't an integer or enum.
880static int getIntegerRank(QualType t) {
881 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
882 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
883 return 4;
884 }
885
Christopher Lambebb97e92008-02-04 02:31:56 +0000886 const BuiltinType *BT = t.getCanonicalType()->getAsBuiltinType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 switch (BT->getKind()) {
888 default:
889 assert(0 && "getIntegerRank(): not a built-in integer");
890 case BuiltinType::Bool:
891 return 1;
892 case BuiltinType::Char_S:
893 case BuiltinType::Char_U:
894 case BuiltinType::SChar:
895 case BuiltinType::UChar:
896 return 2;
897 case BuiltinType::Short:
898 case BuiltinType::UShort:
899 return 3;
900 case BuiltinType::Int:
901 case BuiltinType::UInt:
902 return 4;
903 case BuiltinType::Long:
904 case BuiltinType::ULong:
905 return 5;
906 case BuiltinType::LongLong:
907 case BuiltinType::ULongLong:
908 return 6;
909 }
910}
911
912/// getFloatingRank - Return a relative rank for floating point types.
913/// This routine will assert if passed a built-in type that isn't a float.
914static int getFloatingRank(QualType T) {
915 T = T.getCanonicalType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000916 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000917 return getFloatingRank(CT->getElementType());
918
Christopher Lambebb97e92008-02-04 02:31:56 +0000919 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattner770951b2007-11-01 05:03:41 +0000920 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000921 case BuiltinType::Float: return FloatRank;
922 case BuiltinType::Double: return DoubleRank;
923 case BuiltinType::LongDouble: return LongDoubleRank;
924 }
925}
926
Steve Naroff716c7302007-08-27 01:41:48 +0000927/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
928/// point or a complex type (based on typeDomain/typeSize).
929/// 'typeDomain' is a real floating point or complex type.
930/// 'typeSize' is a real floating point or complex type.
Steve Narofff1448a02007-08-27 01:27:54 +0000931QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
932 QualType typeSize, QualType typeDomain) const {
933 if (typeDomain->isComplexType()) {
934 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000935 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000936 case FloatRank: return FloatComplexTy;
937 case DoubleRank: return DoubleComplexTy;
938 case LongDoubleRank: return LongDoubleComplexTy;
939 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000940 }
Steve Narofff1448a02007-08-27 01:27:54 +0000941 if (typeDomain->isRealFloatingType()) {
942 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000943 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000944 case FloatRank: return FloatTy;
945 case DoubleRank: return DoubleTy;
946 case LongDoubleRank: return LongDoubleTy;
947 }
948 }
949 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattnerb1776cb2007-09-16 19:23:47 +0000950 //an invalid return value, but the assert
951 //will ensure that this code is never reached.
952 return VoidTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000953}
954
Steve Narofffb0d4962007-08-27 15:30:22 +0000955/// compareFloatingType - Handles 3 different combos:
956/// float/float, float/complex, complex/complex.
957/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
958int ASTContext::compareFloatingType(QualType lt, QualType rt) {
959 if (getFloatingRank(lt) == getFloatingRank(rt))
960 return 0;
961 if (getFloatingRank(lt) > getFloatingRank(rt))
962 return 1;
963 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000964}
965
966// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
967// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
968QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
969 if (lhs == rhs) return lhs;
970
971 bool t1Unsigned = lhs->isUnsignedIntegerType();
972 bool t2Unsigned = rhs->isUnsignedIntegerType();
973
974 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
975 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
976
977 // We have two integer types with differing signs
978 QualType unsignedType = t1Unsigned ? lhs : rhs;
979 QualType signedType = t1Unsigned ? rhs : lhs;
980
981 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
982 return unsignedType;
983 else {
984 // FIXME: Need to check if the signed type can represent all values of the
985 // unsigned type. If it can, then the result is the signed type.
986 // If it can't, then the result is the unsigned version of the signed type.
987 // Should probably add a helper that returns a signed integer type from
988 // an unsigned (and vice versa). C99 6.3.1.8.
989 return signedType;
990 }
991}
Anders Carlsson71993dd2007-08-17 05:31:46 +0000992
993// getCFConstantStringType - Return the type used for constant CFStrings.
994QualType ASTContext::getCFConstantStringType() {
995 if (!CFConstantStringTypeDecl) {
996 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
Steve Naroffbeaf2992007-11-03 11:27:19 +0000997 &Idents.get("NSConstantString"),
Anders Carlsson71993dd2007-08-17 05:31:46 +0000998 0);
Anders Carlssonf06273f2007-11-19 00:25:30 +0000999 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001000
1001 // const int *isa;
1002 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001003 // int flags;
1004 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001005 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001006 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001007 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001008 FieldTypes[3] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001009 // Create fields
Anders Carlssonf06273f2007-11-19 00:25:30 +00001010 FieldDecl *FieldDecls[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001011
Anders Carlssonf06273f2007-11-19 00:25:30 +00001012 for (unsigned i = 0; i < 4; ++i)
Steve Narofff38661e2007-09-14 02:20:46 +00001013 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001014
1015 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1016 }
1017
1018 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001019}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001020
Anders Carlssone8c49532007-10-29 06:33:42 +00001021// This returns true if a type has been typedefed to BOOL:
1022// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001023static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001024 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner2d998332007-10-30 20:27:44 +00001025 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001026
1027 return false;
1028}
1029
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001030/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001031/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001032int ASTContext::getObjCEncodingTypeSize(QualType type) {
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001033 SourceLocation Loc;
1034 uint64_t sz = getTypeSize(type, Loc);
1035
1036 // Make all integer and enum types at least as large as an int
1037 if (sz > 0 && type->isIntegralType())
1038 sz = std::max(sz, getTypeSize(IntTy, Loc));
1039 // Treat arrays as pointers, since that's how they're passed in.
1040 else if (type->isArrayType())
1041 sz = getTypeSize(VoidPtrTy, Loc);
1042 return sz / getTypeSize(CharTy, Loc);
1043}
1044
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001045/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001046/// declaration.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001047void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001048 std::string& S)
1049{
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001050 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001051 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001052 // Encode result type.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001053 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001054 // Compute size of all parameters.
1055 // Start with computing size of a pointer in number of bytes.
1056 // FIXME: There might(should) be a better way of doing this computation!
1057 SourceLocation Loc;
1058 int PtrSize = getTypeSize(VoidPtrTy, Loc) / getTypeSize(CharTy, Loc);
1059 // The first two arguments (self and _cmd) are pointers; account for
1060 // their size.
1061 int ParmOffset = 2 * PtrSize;
1062 int NumOfParams = Decl->getNumParams();
1063 for (int i = 0; i < NumOfParams; i++) {
1064 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001065 int sz = getObjCEncodingTypeSize (PType);
1066 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001067 ParmOffset += sz;
1068 }
1069 S += llvm::utostr(ParmOffset);
1070 S += "@0:";
1071 S += llvm::utostr(PtrSize);
1072
1073 // Argument types.
1074 ParmOffset = 2 * PtrSize;
1075 for (int i = 0; i < NumOfParams; i++) {
1076 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001077 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001078 // 'in', 'inout', etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001079 getObjCEncodingForTypeQualifier(
1080 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001081 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001082 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001083 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001084 }
1085}
1086
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001087void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1088 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001089{
Anders Carlssone8c49532007-10-29 06:33:42 +00001090 // FIXME: This currently doesn't encode:
1091 // @ An object (whether statically typed or typed id)
1092 // # A class object (Class)
1093 // : A method selector (SEL)
1094 // {name=type...} A structure
1095 // (name=type...) A union
1096 // bnum A bit field of num bits
1097
1098 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001099 char encoding;
1100 switch (BT->getKind()) {
1101 case BuiltinType::Void:
1102 encoding = 'v';
1103 break;
1104 case BuiltinType::Bool:
1105 encoding = 'B';
1106 break;
1107 case BuiltinType::Char_U:
1108 case BuiltinType::UChar:
1109 encoding = 'C';
1110 break;
1111 case BuiltinType::UShort:
1112 encoding = 'S';
1113 break;
1114 case BuiltinType::UInt:
1115 encoding = 'I';
1116 break;
1117 case BuiltinType::ULong:
1118 encoding = 'L';
1119 break;
1120 case BuiltinType::ULongLong:
1121 encoding = 'Q';
1122 break;
1123 case BuiltinType::Char_S:
1124 case BuiltinType::SChar:
1125 encoding = 'c';
1126 break;
1127 case BuiltinType::Short:
1128 encoding = 's';
1129 break;
1130 case BuiltinType::Int:
1131 encoding = 'i';
1132 break;
1133 case BuiltinType::Long:
1134 encoding = 'l';
1135 break;
1136 case BuiltinType::LongLong:
1137 encoding = 'q';
1138 break;
1139 case BuiltinType::Float:
1140 encoding = 'f';
1141 break;
1142 case BuiltinType::Double:
1143 encoding = 'd';
1144 break;
1145 case BuiltinType::LongDouble:
1146 encoding = 'd';
1147 break;
1148 default:
1149 assert(0 && "Unhandled builtin type kind");
1150 }
1151
1152 S += encoding;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001153 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001154 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001155 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001156 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001157
1158 }
1159 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001160 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001161 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001162 S += '@';
1163 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001164 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001165 S += '#';
1166 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001167 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00001168 S += ':';
1169 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001170 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001171
1172 if (PointeeTy->isCharType()) {
1173 // char pointer types should be encoded as '*' unless it is a
1174 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001175 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001176 S += '*';
1177 return;
1178 }
1179 }
1180
1181 S += '^';
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001182 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone8c49532007-10-29 06:33:42 +00001183 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001184 S += '[';
1185
1186 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1187 S += llvm::utostr(CAT->getSize().getZExtValue());
1188 else
1189 assert(0 && "Unhandled array type!");
1190
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001191 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001192 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001193 } else if (T->getAsFunctionType()) {
1194 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001195 } else if (const RecordType *RTy = T->getAsRecordType()) {
1196 RecordDecl *RDecl= RTy->getDecl();
1197 S += '{';
1198 S += RDecl->getName();
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001199 bool found = false;
1200 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1201 if (ERType[i] == RTy) {
1202 found = true;
1203 break;
1204 }
1205 if (!found) {
1206 ERType.push_back(RTy);
1207 S += '=';
1208 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1209 FieldDecl *field = RDecl->getMember(i);
1210 getObjCEncodingForType(field->getType(), S, ERType);
1211 }
1212 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1213 ERType.pop_back();
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001214 }
1215 S += '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001216 } else if (T->isEnumeralType()) {
1217 S += 'i';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001218 } else
Steve Narofff69cc5d2008-01-30 19:17:43 +00001219 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001220}
1221
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001222void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001223 std::string& S) const {
1224 if (QT & Decl::OBJC_TQ_In)
1225 S += 'n';
1226 if (QT & Decl::OBJC_TQ_Inout)
1227 S += 'N';
1228 if (QT & Decl::OBJC_TQ_Out)
1229 S += 'o';
1230 if (QT & Decl::OBJC_TQ_Bycopy)
1231 S += 'O';
1232 if (QT & Decl::OBJC_TQ_Byref)
1233 S += 'R';
1234 if (QT & Decl::OBJC_TQ_Oneway)
1235 S += 'V';
1236}
1237
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001238void ASTContext::setBuiltinVaListType(QualType T)
1239{
1240 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1241
1242 BuiltinVaListType = T;
1243}
1244
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001245void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00001246{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001247 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff7e219e42007-10-15 14:41:52 +00001248
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001249 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00001250
1251 // typedef struct objc_object *id;
1252 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1253 assert(ptr && "'id' incorrectly typed");
1254 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1255 assert(rec && "'id' incorrectly typed");
1256 IdStructType = rec;
1257}
1258
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001259void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001260{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001261 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001262
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001263 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001264
1265 // typedef struct objc_selector *SEL;
1266 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1267 assert(ptr && "'SEL' incorrectly typed");
1268 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1269 assert(rec && "'SEL' incorrectly typed");
1270 SelStructType = rec;
1271}
1272
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001273void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001274{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001275 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1276 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001277}
1278
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001279void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00001280{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001281 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson8baaca52007-10-31 02:53:19 +00001282
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001283 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00001284
1285 // typedef struct objc_class *Class;
1286 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1287 assert(ptr && "'Class' incorrectly typed");
1288 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1289 assert(rec && "'Class' incorrectly typed");
1290 ClassStructType = rec;
1291}
1292
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001293void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1294 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00001295 "'NSConstantString' type already set!");
1296
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001297 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00001298}
1299
Steve Naroffec0550f2007-10-15 20:41:53 +00001300bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1301 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1302 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1303
1304 return lBuiltin->getKind() == rBuiltin->getKind();
1305}
1306
Fariborz Jahanianb145e7d2007-12-21 17:34:43 +00001307/// objcTypesAreCompatible - This routine is called when two types
1308/// are of different class; one is interface type or is
1309/// a qualified interface type and the other type is of a different class.
1310/// Example, II or II<P>.
Steve Naroffec0550f2007-10-15 20:41:53 +00001311bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001312 if (lhs->isObjCInterfaceType() && isObjCIdType(rhs))
Steve Naroffec0550f2007-10-15 20:41:53 +00001313 return true;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001314 else if (isObjCIdType(lhs) && rhs->isObjCInterfaceType())
Steve Naroffec0550f2007-10-15 20:41:53 +00001315 return true;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001316 if (ObjCInterfaceType *lhsIT =
1317 dyn_cast<ObjCInterfaceType>(lhs.getCanonicalType().getTypePtr())) {
1318 ObjCQualifiedInterfaceType *rhsQI =
1319 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanianb145e7d2007-12-21 17:34:43 +00001320 return rhsQI && (lhsIT->getDecl() == rhsQI->getDecl());
1321 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001322 else if (ObjCInterfaceType *rhsIT =
1323 dyn_cast<ObjCInterfaceType>(rhs.getCanonicalType().getTypePtr())) {
1324 ObjCQualifiedInterfaceType *lhsQI =
1325 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanianb145e7d2007-12-21 17:34:43 +00001326 return lhsQI && (rhsIT->getDecl() == lhsQI->getDecl());
1327 }
Steve Naroffec0550f2007-10-15 20:41:53 +00001328 return false;
1329}
1330
Fariborz Jahanianc5ae5cf2008-01-07 20:12:21 +00001331/// Check that 'lhs' and 'rhs' are compatible interface types. Both types
1332/// must be canonical types.
Steve Naroffec0550f2007-10-15 20:41:53 +00001333bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
Fariborz Jahanianc5ae5cf2008-01-07 20:12:21 +00001334 assert (lhs->isCanonical() &&
1335 "interfaceTypesAreCompatible strip typedefs of lhs");
1336 assert (rhs->isCanonical() &&
1337 "interfaceTypesAreCompatible strip typedefs of rhs");
Fariborz Jahanian0f01deb2007-12-20 22:37:58 +00001338 if (lhs == rhs)
1339 return true;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001340 ObjCInterfaceType *lhsIT = cast<ObjCInterfaceType>(lhs.getTypePtr());
1341 ObjCInterfaceType *rhsIT = cast<ObjCInterfaceType>(rhs.getTypePtr());
1342 ObjCInterfaceDecl *rhsIDecl = rhsIT->getDecl();
1343 ObjCInterfaceDecl *lhsIDecl = lhsIT->getDecl();
Fariborz Jahanian0f01deb2007-12-20 22:37:58 +00001344 // rhs is derived from lhs it is OK; else it is not OK.
1345 while (rhsIDecl != NULL) {
1346 if (rhsIDecl == lhsIDecl)
1347 return true;
1348 rhsIDecl = rhsIDecl->getSuperClass();
1349 }
1350 return false;
Steve Naroffec0550f2007-10-15 20:41:53 +00001351}
1352
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001353bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
1354 QualType rhs) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001355 ObjCQualifiedInterfaceType *lhsQI =
1356 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001357 assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001358 ObjCQualifiedInterfaceType *rhsQI =
1359 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001360 assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
Fariborz Jahanianc5ae5cf2008-01-07 20:12:21 +00001361 if (!interfaceTypesAreCompatible(
1362 getObjCInterfaceType(lhsQI->getDecl()).getCanonicalType(),
1363 getObjCInterfaceType(rhsQI->getDecl()).getCanonicalType()))
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001364 return false;
1365 /* All protocols in lhs must have a presense in rhs. */
1366 for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
1367 bool match = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001368 ObjCProtocolDecl *lhsProto = lhsQI->getProtocols(i);
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001369 for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001370 ObjCProtocolDecl *rhsProto = rhsQI->getProtocols(j);
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001371 if (lhsProto == rhsProto) {
1372 match = true;
1373 break;
1374 }
1375 }
1376 if (!match)
1377 return false;
1378 }
1379 return true;
1380}
1381
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001382/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
1383/// inheritance hierarchy of 'rProto'.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001384static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
1385 ObjCProtocolDecl *rProto) {
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001386 if (lProto == rProto)
1387 return true;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001388 ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001389 for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
1390 if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
1391 return true;
1392 return false;
1393}
1394
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001395/// ClassImplementsProtocol - Checks that 'lProto' protocol
1396/// has been implemented in IDecl class, its super class or categories (if
1397/// lookupCategory is true).
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001398static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1399 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001400 bool lookupCategory) {
1401
1402 // 1st, look up the class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001403 ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols();
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001404 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
1405 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1406 return true;
1407 }
1408
1409 // 2nd, look up the category.
1410 if (lookupCategory)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001411 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001412 CDecl = CDecl->getNextClassCategory()) {
1413 protoList = CDecl->getReferencedProtocols();
1414 for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
1415 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1416 return true;
1417 }
1418 }
1419
1420 // 3rd, look up the super class(s)
1421 if (IDecl->getSuperClass())
1422 return
1423 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory);
1424
1425 return false;
1426}
1427
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001428/// ObjCQualifiedIdTypesAreCompatible - Compares two types, at least
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001429/// one of which is a protocol qualified 'id' type. When 'compare'
1430/// is true it is for comparison; when false, for assignment/initialization.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001431bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs,
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001432 QualType rhs,
1433 bool compare) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001434 // match id<P..> with an 'id' type in all cases.
1435 if (const PointerType *PT = lhs->getAsPointerType()) {
1436 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001437 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001438 return true;
1439
1440 }
1441 else if (const PointerType *PT = rhs->getAsPointerType()) {
1442 QualType PointeeTy = PT->getPointeeType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001443 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001444 return true;
1445
1446 }
1447
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001448 ObjCQualifiedInterfaceType *lhsQI = 0;
1449 ObjCQualifiedInterfaceType *rhsQI = 0;
1450 ObjCInterfaceDecl *lhsID = 0;
1451 ObjCInterfaceDecl *rhsID = 0;
1452 ObjCQualifiedIdType *lhsQID = dyn_cast<ObjCQualifiedIdType>(lhs);
1453 ObjCQualifiedIdType *rhsQID = dyn_cast<ObjCQualifiedIdType>(rhs);
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001454
1455 if (lhsQID) {
1456 if (!rhsQID && rhs->getTypeClass() == Type::Pointer) {
1457 QualType rtype =
1458 cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1459 rhsQI =
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001460 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001461 rtype.getCanonicalType().getTypePtr());
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001462 if (!rhsQI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001463 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001464 rtype.getCanonicalType().getTypePtr());
1465 if (IT)
1466 rhsID = IT->getDecl();
1467 }
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001468 }
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001469 if (!rhsQI && !rhsQID && !rhsID)
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001470 return false;
1471
Fariborz Jahanianbca14a22008-01-03 20:01:35 +00001472 unsigned numRhsProtocols = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001473 ObjCProtocolDecl **rhsProtoList = 0;
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001474 if (rhsQI) {
1475 numRhsProtocols = rhsQI->getNumProtocols();
1476 rhsProtoList = rhsQI->getReferencedProtocols();
1477 }
1478 else if (rhsQID) {
1479 numRhsProtocols = rhsQID->getNumProtocols();
1480 rhsProtoList = rhsQID->getReferencedProtocols();
1481 }
1482
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001483 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001484 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001485 bool match = false;
1486
1487 // when comparing an id<P> on lhs with a static type on rhs,
1488 // see if static class implements all of id's protocols, directly or
1489 // through its super class and categories.
1490 if (rhsID) {
1491 if (ClassImplementsProtocol(lhsProto, rhsID, true))
1492 match = true;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001493 }
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001494 else for (unsigned j = 0; j < numRhsProtocols; j++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001495 ObjCProtocolDecl *rhsProto = rhsProtoList[j];
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001496 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1497 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001498 match = true;
1499 break;
1500 }
1501 }
1502 if (!match)
1503 return false;
1504 }
1505 }
1506 else if (rhsQID) {
1507 if (!lhsQID && lhs->getTypeClass() == Type::Pointer) {
1508 QualType ltype =
1509 cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1510 lhsQI =
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001511 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001512 ltype.getCanonicalType().getTypePtr());
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001513 if (!lhsQI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001514 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001515 ltype.getCanonicalType().getTypePtr());
1516 if (IT)
1517 lhsID = IT->getDecl();
1518 }
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001519 }
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001520 if (!lhsQI && !lhsQID && !lhsID)
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001521 return false;
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001522
Fariborz Jahanianbca14a22008-01-03 20:01:35 +00001523 unsigned numLhsProtocols = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001524 ObjCProtocolDecl **lhsProtoList = 0;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001525 if (lhsQI) {
1526 numLhsProtocols = lhsQI->getNumProtocols();
1527 lhsProtoList = lhsQI->getReferencedProtocols();
1528 }
Fariborz Jahanianc395bda2007-12-20 19:24:10 +00001529 else if (lhsQID) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001530 numLhsProtocols = lhsQID->getNumProtocols();
1531 lhsProtoList = lhsQID->getReferencedProtocols();
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001532 }
1533 bool match = false;
1534 // for static type vs. qualified 'id' type, check that class implements
1535 // one of 'id's protocols.
1536 if (lhsID) {
1537 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001538 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001539 if (ClassImplementsProtocol(rhsProto, lhsID, compare)) {
1540 match = true;
1541 break;
1542 }
1543 }
1544 }
1545 else for (unsigned i =0; i < numLhsProtocols; i++) {
1546 match = false;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001547 ObjCProtocolDecl *lhsProto = lhsProtoList[i];
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001548 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001549 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahaniand0c89c42007-12-21 00:33:59 +00001550 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1551 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001552 match = true;
1553 break;
1554 }
1555 }
Fariborz Jahanian4c71f1a2007-12-21 22:22:33 +00001556 }
1557 if (!match)
1558 return false;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001559 }
1560 return true;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001561}
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001562
Chris Lattner770951b2007-11-01 05:03:41 +00001563bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1564 const VectorType *lVector = lhs->getAsVectorType();
1565 const VectorType *rVector = rhs->getAsVectorType();
1566
1567 if ((lVector->getElementType().getCanonicalType() ==
1568 rVector->getElementType().getCanonicalType()) &&
1569 (lVector->getNumElements() == rVector->getNumElements()))
1570 return true;
1571 return false;
1572}
1573
Steve Naroffec0550f2007-10-15 20:41:53 +00001574// C99 6.2.7p1: If both are complete types, then the following additional
1575// requirements apply...FIXME (handle compatibility across source files).
1576bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
Steve Naroffab373092007-11-07 06:03:51 +00001577 // "Class" and "id" are compatible built-in structure types.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001578 if (isObjCIdType(lhs) && isObjCClassType(rhs) ||
1579 isObjCClassType(lhs) && isObjCIdType(rhs))
Steve Naroffab373092007-11-07 06:03:51 +00001580 return true;
Eli Friedmand5740522008-02-15 06:03:44 +00001581
1582 // Within a translation unit a tag type is
1583 // only compatible with itself.
1584 return lhs.getCanonicalType() == rhs.getCanonicalType();
Steve Naroffec0550f2007-10-15 20:41:53 +00001585}
1586
1587bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1588 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1589 // identically qualified and both shall be pointers to compatible types.
1590 if (lhs.getQualifiers() != rhs.getQualifiers())
1591 return false;
1592
1593 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1594 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1595
1596 return typesAreCompatible(ltype, rtype);
1597}
1598
Bill Wendling43d69752007-12-03 07:33:35 +00001599// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroffec0550f2007-10-15 20:41:53 +00001600// reference to T, the operation assigns to the object of type T denoted by the
1601// reference.
1602bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1603 QualType ltype = lhs;
1604
1605 if (lhs->isReferenceType())
1606 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1607
1608 QualType rtype = rhs;
1609
1610 if (rhs->isReferenceType())
1611 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1612
1613 return typesAreCompatible(ltype, rtype);
1614}
1615
1616bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1617 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1618 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1619 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1620 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1621
1622 // first check the return types (common between C99 and K&R).
1623 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1624 return false;
1625
1626 if (lproto && rproto) { // two C99 style function prototypes
1627 unsigned lproto_nargs = lproto->getNumArgs();
1628 unsigned rproto_nargs = rproto->getNumArgs();
1629
1630 if (lproto_nargs != rproto_nargs)
1631 return false;
1632
1633 // both prototypes have the same number of arguments.
1634 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1635 (rproto->isVariadic() && !lproto->isVariadic()))
1636 return false;
1637
1638 // The use of ellipsis agree...now check the argument types.
1639 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Narofff69cc5d2008-01-30 19:17:43 +00001640 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1641 // is taken as having the unqualified version of it's declared type.
Steve Naroffba03eda2008-01-29 00:15:50 +00001642 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Narofff69cc5d2008-01-30 19:17:43 +00001643 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroffec0550f2007-10-15 20:41:53 +00001644 return false;
1645 return true;
1646 }
1647 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1648 return true;
1649
1650 // we have a mixture of K&R style with C99 prototypes
1651 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1652
1653 if (proto->isVariadic())
1654 return false;
1655
1656 // FIXME: Each parameter type T in the prototype must be compatible with the
1657 // type resulting from applying the usual argument conversions to T.
1658 return true;
1659}
1660
1661bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
Eli Friedman4e92acf2008-02-06 04:53:22 +00001662 // Compatible arrays must have compatible element types
1663 QualType ltype = lhs->getAsArrayType()->getElementType();
1664 QualType rtype = rhs->getAsArrayType()->getElementType();
1665
Steve Naroffec0550f2007-10-15 20:41:53 +00001666 if (!typesAreCompatible(ltype, rtype))
1667 return false;
Eli Friedman4e92acf2008-02-06 04:53:22 +00001668
1669 // Compatible arrays must be the same size
1670 if (const ConstantArrayType* LCAT = lhs->getAsConstantArrayType())
1671 if (const ConstantArrayType* RCAT = rhs->getAsConstantArrayType())
1672 return RCAT->getSize() == LCAT->getSize();
1673
Steve Naroffec0550f2007-10-15 20:41:53 +00001674 return true;
1675}
1676
1677/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1678/// both shall have the identically qualified version of a compatible type.
1679/// C99 6.2.7p1: Two types have compatible types if their types are the
1680/// same. See 6.7.[2,3,5] for additional rules.
1681bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
Steve Naroff2565eef2008-01-29 18:58:14 +00001682 if (lhs.getQualifiers() != rhs.getQualifiers())
1683 return false;
1684
Steve Naroffec0550f2007-10-15 20:41:53 +00001685 QualType lcanon = lhs.getCanonicalType();
1686 QualType rcanon = rhs.getCanonicalType();
1687
1688 // If two types are identical, they are are compatible
1689 if (lcanon == rcanon)
1690 return true;
Bill Wendling43d69752007-12-03 07:33:35 +00001691
1692 // C++ [expr]: If an expression initially has the type "reference to T", the
1693 // type is adjusted to "T" prior to any further analysis, the expression
1694 // designates the object or function denoted by the reference, and the
1695 // expression is an lvalue.
Chris Lattner1adb8832008-01-14 05:45:46 +00001696 if (ReferenceType *RT = dyn_cast<ReferenceType>(lcanon))
1697 lcanon = RT->getReferenceeType();
1698 if (ReferenceType *RT = dyn_cast<ReferenceType>(rcanon))
1699 rcanon = RT->getReferenceeType();
1700
1701 Type::TypeClass LHSClass = lcanon->getTypeClass();
1702 Type::TypeClass RHSClass = rcanon->getTypeClass();
1703
1704 // We want to consider the two function types to be the same for these
1705 // comparisons, just force one to the other.
1706 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1707 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00001708
1709 // Same as above for arrays
1710 if (LHSClass == Type::VariableArray) LHSClass = Type::ConstantArray;
1711 if (RHSClass == Type::VariableArray) RHSClass = Type::ConstantArray;
Eli Friedmanc5773c42008-02-15 18:16:39 +00001712 if (LHSClass == Type::IncompleteArray) LHSClass = Type::ConstantArray;
1713 if (RHSClass == Type::IncompleteArray) RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00001714
Steve Naroff4a746782008-01-09 22:43:08 +00001715 // If the canonical type classes don't match...
Chris Lattner1adb8832008-01-14 05:45:46 +00001716 if (LHSClass != RHSClass) {
Steve Naroffec0550f2007-10-15 20:41:53 +00001717 // For Objective-C, it is possible for two types to be compatible
1718 // when their classes don't match (when dealing with "id"). If either type
1719 // is an interface, we defer to objcTypesAreCompatible().
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001720 if (lcanon->isObjCInterfaceType() || rcanon->isObjCInterfaceType())
Steve Naroffec0550f2007-10-15 20:41:53 +00001721 return objcTypesAreCompatible(lcanon, rcanon);
Steve Narofff69cc5d2008-01-30 19:17:43 +00001722
Chris Lattner1adb8832008-01-14 05:45:46 +00001723 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1724 // a signed integer type, or an unsigned integer type.
Eli Friedmanbab96962008-02-12 08:46:17 +00001725 if (lcanon->isEnumeralType() && rcanon->isIntegralType()) {
1726 EnumDecl* EDecl = cast<EnumDecl>(cast<TagType>(lcanon)->getDecl());
1727 return EDecl->getIntegerType() == rcanon;
1728 }
1729 if (rcanon->isEnumeralType() && lcanon->isIntegralType()) {
1730 EnumDecl* EDecl = cast<EnumDecl>(cast<TagType>(rcanon)->getDecl());
1731 return EDecl->getIntegerType() == lcanon;
1732 }
Chris Lattner1adb8832008-01-14 05:45:46 +00001733
Steve Naroffec0550f2007-10-15 20:41:53 +00001734 return false;
1735 }
Steve Naroff4a746782008-01-09 22:43:08 +00001736 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00001737 switch (LHSClass) {
1738 case Type::FunctionProto: assert(0 && "Canonicalized away above");
1739 case Type::Pointer:
1740 return pointerTypesAreCompatible(lcanon, rcanon);
1741 case Type::ConstantArray:
1742 case Type::VariableArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +00001743 case Type::IncompleteArray:
Chris Lattner1adb8832008-01-14 05:45:46 +00001744 return arrayTypesAreCompatible(lcanon, rcanon);
1745 case Type::FunctionNoProto:
1746 return functionTypesAreCompatible(lcanon, rcanon);
1747 case Type::Tagged: // handle structures, unions
1748 return tagTypesAreCompatible(lcanon, rcanon);
1749 case Type::Builtin:
1750 return builtinTypesAreCompatible(lcanon, rcanon);
1751 case Type::ObjCInterface:
1752 return interfaceTypesAreCompatible(lcanon, rcanon);
1753 case Type::Vector:
1754 case Type::OCUVector:
1755 return vectorTypesAreCompatible(lcanon, rcanon);
1756 case Type::ObjCQualifiedInterface:
1757 return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
1758 default:
1759 assert(0 && "unexpected type");
Steve Naroffec0550f2007-10-15 20:41:53 +00001760 }
1761 return true; // should never get here...
1762}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001763
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001764/// Emit - Serialize an ASTContext object to Bitcode.
1765void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek54513502007-10-31 20:00:03 +00001766 S.EmitRef(SourceMgr);
1767 S.EmitRef(Target);
1768 S.EmitRef(Idents);
1769 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001770
Ted Kremenekfee04522007-10-31 22:44:07 +00001771 // Emit the size of the type vector so that we can reserve that size
1772 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00001773 S.EmitInt(Types.size());
1774
Ted Kremenek03ed4402007-11-13 22:02:55 +00001775 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1776 I!=E;++I)
1777 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001778
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001779 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001780}
1781
Ted Kremenek0f84c002007-11-13 00:25:37 +00001782ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenekfee04522007-10-31 22:44:07 +00001783 SourceManager &SM = D.ReadRef<SourceManager>();
1784 TargetInfo &t = D.ReadRef<TargetInfo>();
1785 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1786 SelectorTable &sels = D.ReadRef<SelectorTable>();
1787
1788 unsigned size_reserve = D.ReadInt();
1789
1790 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1791
Ted Kremenek03ed4402007-11-13 22:02:55 +00001792 for (unsigned i = 0; i < size_reserve; ++i)
1793 Type::Create(*A,i,D);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001794
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001795 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00001796
1797 return A;
1798}