blob: 5dd7d9aeebe0adc83b54e0bc79646eddd9d5d3c1 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +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 Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Basic/TargetInfo.h"
18#include "llvm/ADT/SmallVector.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000019#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000020#include "llvm/Bitcode/Serialize.h"
21#include "llvm/Bitcode/Deserialize.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000022
Chris Lattner4b009652007-07-25 00:24:17 +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;
47 unsigned NumVector = 0, NumComplex = 0;
48 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
49
50 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000051 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
52 unsigned NumObjCQualifiedIds = 0;
Chris Lattner4b009652007-07-25 00:24:17 +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;
62 else if (isa<ComplexType>(T))
63 ++NumComplex;
64 else if (isa<ArrayType>(T))
65 ++NumArray;
66 else if (isa<VectorType>(T))
67 ++NumVector;
68 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 Kremenek42730c52008-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 Naroff948fd372007-09-17 14:16:13 +000089 else {
Chris Lattner8a35b462007-12-12 06:43:05 +000090 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +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);
98 fprintf(stderr, " %d complex types\n", NumComplex);
99 fprintf(stderr, " %d array types\n", NumArray);
100 fprintf(stderr, " %d vector types\n", NumVector);
101 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 Kremenek42730c52008-01-07 19:49:32 +0000109 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000110 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000111 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000112 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000113 NumObjCQualifiedIds);
Chris Lattner4b009652007-07-25 00:24:17 +0000114 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
115 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
116 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
117 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
Chris Lattner4b009652007-07-25 00:24:17 +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.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000136 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +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 Naroff9d12c902007-10-15 14:41:52 +0000163
164 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000165 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000166 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000167 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000168 ClassStructType = 0;
169
Ted Kremenek42730c52008-01-07 19:49:32 +0000170 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000171
172 // void * type
173 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000174}
175
176//===----------------------------------------------------------------------===//
177// Type Sizing and Analysis
178//===----------------------------------------------------------------------===//
179
180/// getTypeSize - Return the size of the specified type, in bits. This method
181/// does not work on incomplete types.
182std::pair<uint64_t, unsigned>
Chris Lattner8cd0e932008-03-05 18:54:05 +0000183ASTContext::getTypeInfo(QualType T) {
Chris Lattner4b009652007-07-25 00:24:17 +0000184 T = T.getCanonicalType();
185 uint64_t Size;
186 unsigned Align;
187 switch (T->getTypeClass()) {
188 case Type::TypeName: assert(0 && "Not a canonical type!");
189 case Type::FunctionNoProto:
190 case Type::FunctionProto:
191 default:
192 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-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 Lattner8cd0e932008-03-05 18:54:05 +0000198 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Steve Naroff83c13012007-08-30 01:06:46 +0000199 Size = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000200 Align = EltInfo.second;
201 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000202 }
203 case Type::OCUVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000204 case Type::Vector: {
205 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000206 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner4b009652007-07-25 00:24:17 +0000207 Size = EltInfo.first*cast<VectorType>(T)->getNumElements();
208 // FIXME: Vector alignment is not the alignment of its elements.
209 Align = EltInfo.second;
210 break;
211 }
212
213 case Type::Builtin: {
214 // FIXME: need to use TargetInfo to derive the target specific sizes. This
215 // implementation will suffice for play with vector support.
Chris Lattner858eece2007-09-22 18:29:59 +0000216 const llvm::fltSemantics *F;
Chris Lattner4b009652007-07-25 00:24:17 +0000217 switch (cast<BuiltinType>(T)->getKind()) {
218 default: assert(0 && "Unknown builtin type!");
219 case BuiltinType::Void:
220 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000221 case BuiltinType::Bool:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000222 Target.getBoolInfo(Size, Align);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000223 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000224 case BuiltinType::Char_S:
225 case BuiltinType::Char_U:
226 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000227 case BuiltinType::SChar:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000228 Target.getCharInfo(Size, Align);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000229 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000230 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000231 case BuiltinType::Short:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000232 Target.getShortInfo(Size, Align);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000233 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000234 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000235 case BuiltinType::Int:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000236 Target.getIntInfo(Size, Align);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000237 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000238 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000239 case BuiltinType::Long:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000240 Target.getLongInfo(Size, Align);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000241 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000242 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000243 case BuiltinType::LongLong:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000244 Target.getLongLongInfo(Size, Align);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000245 break;
246 case BuiltinType::Float:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000247 Target.getFloatInfo(Size, Align, F);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000248 break;
249 case BuiltinType::Double:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000250 Target.getDoubleInfo(Size, Align, F);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000251 break;
252 case BuiltinType::LongDouble:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000253 Target.getLongDoubleInfo(Size, Align, F);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000254 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000255 }
256 break;
257 }
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000258 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000259 // FIXME: Pointers into different addr spaces could have different sizes and
260 // alignment requirements: getPointerInfo should take an AddrSpace.
261 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000262 case Type::ObjCQualifiedId:
Chris Lattner461a6c52008-03-08 08:34:58 +0000263 Size = Target.getPointerWidth(0);
264 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000265 break;
Chris Lattner461a6c52008-03-08 08:34:58 +0000266 case Type::Pointer: {
267 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
268 Size = Target.getPointerWidth(AS);
269 Align = Target.getPointerAlign(AS);
270 break;
271 }
Chris Lattner4b009652007-07-25 00:24:17 +0000272 case Type::Reference:
273 // "When applied to a reference or a reference type, the result is the size
274 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000275 // FIXME: This is wrong for struct layout: a reference in a struct has
276 // pointer size.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000277 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType());
Chris Lattner4b009652007-07-25 00:24:17 +0000278
279 case Type::Complex: {
280 // Complex types have the same alignment as their elements, but twice the
281 // size.
282 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000283 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner4b009652007-07-25 00:24:17 +0000284 Size = EltInfo.first*2;
285 Align = EltInfo.second;
286 break;
287 }
288 case Type::Tagged:
Chris Lattnereb56d292007-08-27 17:38:00 +0000289 TagType *TT = cast<TagType>(T);
290 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000291 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
Chris Lattnereb56d292007-08-27 17:38:00 +0000292 Size = Layout.getSize();
293 Align = Layout.getAlignment();
294 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000295 return getTypeInfo(ED->getIntegerType());
Chris Lattnereb56d292007-08-27 17:38:00 +0000296 } else {
Chris Lattner4b009652007-07-25 00:24:17 +0000297 assert(0 && "Unimplemented type sizes!");
Chris Lattnereb56d292007-08-27 17:38:00 +0000298 }
Chris Lattner4b009652007-07-25 00:24:17 +0000299 break;
300 }
301
302 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
303 return std::make_pair(Size, Align);
304}
305
Devang Patel7a78e432007-11-01 19:11:01 +0000306/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000307/// specified record (struct/union/class), which indicates its size and field
308/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000309const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000310 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
311
312 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000313 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000314 if (Entry) return *Entry;
315
Devang Patel7a78e432007-11-01 19:11:01 +0000316 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
317 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
318 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000319 Entry = NewEntry;
320
321 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
322 uint64_t RecordSize = 0;
323 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
324
325 if (D->getKind() != Decl::Union) {
Anders Carlsson7dce0292008-02-16 19:51:27 +0000326 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
327 RecordAlign = std::max(RecordAlign, AA->getAlignment());
328
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000329 bool StructIsPacked = D->getAttr<PackedAttr>();
330
Chris Lattner4b009652007-07-25 00:24:17 +0000331 // Layout each field, for now, just sequentially, respecting alignment. In
332 // the future, this will need to be tweakable by targets.
333 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
334 const FieldDecl *FD = D->getMember(i);
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000335 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
Eli Friedman67571ac2008-02-06 05:33:51 +0000336 uint64_t FieldSize;
337 unsigned FieldAlign;
Anders Carlsson058237f2008-02-18 07:13:09 +0000338
339 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
340 llvm::APSInt I(32);
341 bool BitWidthIsICE =
342 BitWidthExpr->isIntegerConstantExpr(I, *this);
343 assert (BitWidthIsICE && "Invalid BitField size expression");
344 FieldSize = I.getZExtValue();
345
Chris Lattner8cd0e932008-03-05 18:54:05 +0000346 std::pair<uint64_t, unsigned> TypeInfo = getTypeInfo(FD->getType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000347 uint64_t TypeSize = TypeInfo.first;
Anders Carlsson7dce0292008-02-16 19:51:27 +0000348
349 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
350 FieldAlign = AA->getAlignment();
351 else if (FieldIsPacked)
352 FieldAlign = 8;
353 else {
Anders Carlsson058237f2008-02-18 07:13:09 +0000354 // FIXME: This is X86 specific, use 32-bit alignment for long long.
355 if (FD->getType()->isIntegerType() && TypeInfo.second > 32)
356 FieldAlign = 32;
357 else
358 FieldAlign = TypeInfo.second;
Anders Carlsson7dce0292008-02-16 19:51:27 +0000359 }
Eli Friedman67571ac2008-02-06 05:33:51 +0000360
Anders Carlsson058237f2008-02-18 07:13:09 +0000361 // Check if we need to add padding to give the field the correct
362 // alignment.
363 if (RecordSize % FieldAlign + FieldSize > TypeSize)
364 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
365
366 } else {
367 if (FD->getType()->isIncompleteType()) {
368 // This must be a flexible array member; we can't directly
369 // query getTypeInfo about these, so we figure it out here.
370 // Flexible array members don't have any size, but they
371 // have to be aligned appropriately for their element type.
372
373 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
374 FieldAlign = AA->getAlignment();
375 else if (FieldIsPacked)
376 FieldAlign = 8;
377 else {
378 const ArrayType* ATy = FD->getType()->getAsArrayType();
Chris Lattner8cd0e932008-03-05 18:54:05 +0000379 FieldAlign = getTypeAlign(ATy->getElementType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000380 }
381 FieldSize = 0;
382 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000383 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000384 FieldSize = FieldInfo.first;
385
386 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
387 FieldAlign = AA->getAlignment();
388 else if (FieldIsPacked)
389 FieldAlign = 8;
390 else
391 FieldAlign = FieldInfo.second;
392 }
393
394 // Round up the current record size to the field's alignment boundary.
395 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
396 }
Chris Lattner4b009652007-07-25 00:24:17 +0000397
398 // Place this field at the current location.
399 FieldOffsets[i] = RecordSize;
400
401 // Reserve space for this field.
402 RecordSize += FieldSize;
403
404 // Remember max struct/class alignment.
405 RecordAlign = std::max(RecordAlign, FieldAlign);
406 }
407
408 // Finally, round the size of the total struct up to the alignment of the
409 // struct itself.
410 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
411 } else {
412 // Union layout just puts each member at the start of the record.
413 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
414 const FieldDecl *FD = D->getMember(i);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000415 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Chris Lattner4b009652007-07-25 00:24:17 +0000416 uint64_t FieldSize = FieldInfo.first;
417 unsigned FieldAlign = FieldInfo.second;
418
Anders Carlsson058237f2008-02-18 07:13:09 +0000419 // FIXME: This is X86 specific, use 32-bit alignment for long long.
420 if (FD->getType()->isIntegerType() && FieldAlign > 32)
421 FieldAlign = 32;
422
Chris Lattner4b009652007-07-25 00:24:17 +0000423 // Round up the current record size to the field's alignment boundary.
424 RecordSize = std::max(RecordSize, FieldSize);
425
426 // Place this field at the start of the record.
427 FieldOffsets[i] = 0;
428
429 // Remember max struct/class alignment.
430 RecordAlign = std::max(RecordAlign, FieldAlign);
431 }
432 }
433
434 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
435 return *NewEntry;
436}
437
Chris Lattner4b009652007-07-25 00:24:17 +0000438//===----------------------------------------------------------------------===//
439// Type creation/memoization methods
440//===----------------------------------------------------------------------===//
441
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000442QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattner35fef522008-02-20 20:55:12 +0000443 if (T.getCanonicalType().getAddressSpace() == AddressSpace)
444 return T;
445
446 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
447 // with CVR qualifiers from here on out.
448 assert(T.getCanonicalType().getAddressSpace() == 0 &&
449 "Type is already address space qualified");
450
451 // Check if we've already instantiated an address space qual'd type of this
452 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000453 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000454 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000455 void *InsertPos = 0;
456 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
457 return QualType(ASQy, 0);
458
459 // If the base type isn't canonical, this won't be a canonical type either,
460 // so fill in the canonical type field.
461 QualType Canonical;
462 if (!T->isCanonical()) {
463 Canonical = getASQualType(T.getCanonicalType(), AddressSpace);
464
465 // Get the new insert position for the node we care about.
466 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
467 assert(NewIP == 0 && "Shouldn't be in the map!");
468 }
Chris Lattner35fef522008-02-20 20:55:12 +0000469 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000470 ASQualTypes.InsertNode(New, InsertPos);
471 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000472 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000473}
474
Chris Lattner4b009652007-07-25 00:24:17 +0000475
476/// getComplexType - Return the uniqued reference to the type for a complex
477/// number with the specified element type.
478QualType ASTContext::getComplexType(QualType T) {
479 // Unique pointers, to guarantee there is only one pointer of a particular
480 // structure.
481 llvm::FoldingSetNodeID ID;
482 ComplexType::Profile(ID, T);
483
484 void *InsertPos = 0;
485 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
486 return QualType(CT, 0);
487
488 // If the pointee type isn't canonical, this won't be a canonical type either,
489 // so fill in the canonical type field.
490 QualType Canonical;
491 if (!T->isCanonical()) {
492 Canonical = getComplexType(T.getCanonicalType());
493
494 // Get the new insert position for the node we care about.
495 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
496 assert(NewIP == 0 && "Shouldn't be in the map!");
497 }
498 ComplexType *New = new ComplexType(T, Canonical);
499 Types.push_back(New);
500 ComplexTypes.InsertNode(New, InsertPos);
501 return QualType(New, 0);
502}
503
504
505/// getPointerType - Return the uniqued reference to the type for a pointer to
506/// the specified type.
507QualType ASTContext::getPointerType(QualType T) {
508 // Unique pointers, to guarantee there is only one pointer of a particular
509 // structure.
510 llvm::FoldingSetNodeID ID;
511 PointerType::Profile(ID, T);
512
513 void *InsertPos = 0;
514 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
515 return QualType(PT, 0);
516
517 // If the pointee type isn't canonical, this won't be a canonical type either,
518 // so fill in the canonical type field.
519 QualType Canonical;
520 if (!T->isCanonical()) {
521 Canonical = getPointerType(T.getCanonicalType());
522
523 // Get the new insert position for the node we care about.
524 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
525 assert(NewIP == 0 && "Shouldn't be in the map!");
526 }
527 PointerType *New = new PointerType(T, Canonical);
528 Types.push_back(New);
529 PointerTypes.InsertNode(New, InsertPos);
530 return QualType(New, 0);
531}
532
533/// getReferenceType - Return the uniqued reference to the type for a reference
534/// to the specified type.
535QualType ASTContext::getReferenceType(QualType T) {
536 // Unique pointers, to guarantee there is only one pointer of a particular
537 // structure.
538 llvm::FoldingSetNodeID ID;
539 ReferenceType::Profile(ID, T);
540
541 void *InsertPos = 0;
542 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
543 return QualType(RT, 0);
544
545 // If the referencee type isn't canonical, this won't be a canonical type
546 // either, so fill in the canonical type field.
547 QualType Canonical;
548 if (!T->isCanonical()) {
549 Canonical = getReferenceType(T.getCanonicalType());
550
551 // Get the new insert position for the node we care about.
552 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
553 assert(NewIP == 0 && "Shouldn't be in the map!");
554 }
555
556 ReferenceType *New = new ReferenceType(T, Canonical);
557 Types.push_back(New);
558 ReferenceTypes.InsertNode(New, InsertPos);
559 return QualType(New, 0);
560}
561
Steve Naroff83c13012007-08-30 01:06:46 +0000562/// getConstantArrayType - Return the unique reference to the type for an
563/// array of the specified element type.
564QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000565 const llvm::APInt &ArySize,
566 ArrayType::ArraySizeModifier ASM,
567 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000568 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000569 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000570
571 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000572 if (ConstantArrayType *ATP =
573 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000574 return QualType(ATP, 0);
575
576 // If the element type isn't canonical, this won't be a canonical type either,
577 // so fill in the canonical type field.
578 QualType Canonical;
579 if (!EltTy->isCanonical()) {
Steve Naroff24c9b982007-08-30 18:10:14 +0000580 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
581 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000582 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000583 ConstantArrayType *NewIP =
584 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
585
Chris Lattner4b009652007-07-25 00:24:17 +0000586 assert(NewIP == 0 && "Shouldn't be in the map!");
587 }
588
Steve Naroff24c9b982007-08-30 18:10:14 +0000589 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
590 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000591 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000592 Types.push_back(New);
593 return QualType(New, 0);
594}
595
Steve Naroffe2579e32007-08-30 18:14:25 +0000596/// getVariableArrayType - Returns a non-unique reference to the type for a
597/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000598QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
599 ArrayType::ArraySizeModifier ASM,
600 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000601 // Since we don't unique expressions, it isn't possible to unique VLA's
602 // that have an expression provided for their size.
603
604 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
605 ASM, EltTypeQuals);
606
607 VariableArrayTypes.push_back(New);
608 Types.push_back(New);
609 return QualType(New, 0);
610}
611
612QualType ASTContext::getIncompleteArrayType(QualType EltTy,
613 ArrayType::ArraySizeModifier ASM,
614 unsigned EltTypeQuals) {
615 llvm::FoldingSetNodeID ID;
616 IncompleteArrayType::Profile(ID, EltTy);
617
618 void *InsertPos = 0;
619 if (IncompleteArrayType *ATP =
620 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
621 return QualType(ATP, 0);
622
623 // If the element type isn't canonical, this won't be a canonical type
624 // either, so fill in the canonical type field.
625 QualType Canonical;
626
627 if (!EltTy->isCanonical()) {
628 Canonical = getIncompleteArrayType(EltTy.getCanonicalType(),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000629 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000630
631 // Get the new insert position for the node we care about.
632 IncompleteArrayType *NewIP =
633 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
634
635 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000636 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000637
638 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
639 ASM, EltTypeQuals);
640
641 IncompleteArrayTypes.InsertNode(New, InsertPos);
642 Types.push_back(New);
643 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000644}
645
Chris Lattner4b009652007-07-25 00:24:17 +0000646/// getVectorType - Return the unique reference to a vector type of
647/// the specified element type and size. VectorType must be a built-in type.
648QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
649 BuiltinType *baseType;
650
651 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
652 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
653
654 // Check if we've already instantiated a vector of this type.
655 llvm::FoldingSetNodeID ID;
656 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
657 void *InsertPos = 0;
658 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
659 return QualType(VTP, 0);
660
661 // If the element type isn't canonical, this won't be a canonical type either,
662 // so fill in the canonical type field.
663 QualType Canonical;
664 if (!vecType->isCanonical()) {
665 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
666
667 // Get the new insert position for the node we care about.
668 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
669 assert(NewIP == 0 && "Shouldn't be in the map!");
670 }
671 VectorType *New = new VectorType(vecType, NumElts, Canonical);
672 VectorTypes.InsertNode(New, InsertPos);
673 Types.push_back(New);
674 return QualType(New, 0);
675}
676
677/// getOCUVectorType - Return the unique reference to an OCU vector type of
678/// the specified element type and size. VectorType must be a built-in type.
679QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
680 BuiltinType *baseType;
681
682 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
683 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
684
685 // Check if we've already instantiated a vector of this type.
686 llvm::FoldingSetNodeID ID;
687 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
688 void *InsertPos = 0;
689 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
690 return QualType(VTP, 0);
691
692 // If the element type isn't canonical, this won't be a canonical type either,
693 // so fill in the canonical type field.
694 QualType Canonical;
695 if (!vecType->isCanonical()) {
696 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
697
698 // Get the new insert position for the node we care about.
699 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
700 assert(NewIP == 0 && "Shouldn't be in the map!");
701 }
702 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
703 VectorTypes.InsertNode(New, InsertPos);
704 Types.push_back(New);
705 return QualType(New, 0);
706}
707
708/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
709///
710QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
711 // Unique functions, to guarantee there is only one function of a particular
712 // structure.
713 llvm::FoldingSetNodeID ID;
714 FunctionTypeNoProto::Profile(ID, ResultTy);
715
716 void *InsertPos = 0;
717 if (FunctionTypeNoProto *FT =
718 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
719 return QualType(FT, 0);
720
721 QualType Canonical;
722 if (!ResultTy->isCanonical()) {
723 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
724
725 // Get the new insert position for the node we care about.
726 FunctionTypeNoProto *NewIP =
727 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
728 assert(NewIP == 0 && "Shouldn't be in the map!");
729 }
730
731 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
732 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000733 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000734 return QualType(New, 0);
735}
736
737/// getFunctionType - Return a normal function type with a typed argument
738/// list. isVariadic indicates whether the argument list includes '...'.
739QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
740 unsigned NumArgs, bool isVariadic) {
741 // Unique functions, to guarantee there is only one function of a particular
742 // structure.
743 llvm::FoldingSetNodeID ID;
744 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
745
746 void *InsertPos = 0;
747 if (FunctionTypeProto *FTP =
748 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
749 return QualType(FTP, 0);
750
751 // Determine whether the type being created is already canonical or not.
752 bool isCanonical = ResultTy->isCanonical();
753 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
754 if (!ArgArray[i]->isCanonical())
755 isCanonical = false;
756
757 // If this type isn't canonical, get the canonical version of it.
758 QualType Canonical;
759 if (!isCanonical) {
760 llvm::SmallVector<QualType, 16> CanonicalArgs;
761 CanonicalArgs.reserve(NumArgs);
762 for (unsigned i = 0; i != NumArgs; ++i)
763 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
764
765 Canonical = getFunctionType(ResultTy.getCanonicalType(),
766 &CanonicalArgs[0], NumArgs,
767 isVariadic);
768
769 // Get the new insert position for the node we care about.
770 FunctionTypeProto *NewIP =
771 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
772 assert(NewIP == 0 && "Shouldn't be in the map!");
773 }
774
775 // FunctionTypeProto objects are not allocated with new because they have a
776 // variable size array (for parameter types) at the end of them.
777 FunctionTypeProto *FTP =
778 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
779 NumArgs*sizeof(QualType));
780 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
781 Canonical);
782 Types.push_back(FTP);
783 FunctionTypeProtos.InsertNode(FTP, InsertPos);
784 return QualType(FTP, 0);
785}
786
787/// getTypedefType - Return the unique reference to the type for the
788/// specified typename decl.
789QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
790 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
791
792 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000793 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000794 Types.push_back(Decl->TypeForDecl);
795 return QualType(Decl->TypeForDecl, 0);
796}
797
Ted Kremenek42730c52008-01-07 19:49:32 +0000798/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000799/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000800QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000801 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
802
Ted Kremenek42730c52008-01-07 19:49:32 +0000803 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000804 Types.push_back(Decl->TypeForDecl);
805 return QualType(Decl->TypeForDecl, 0);
806}
807
Ted Kremenek42730c52008-01-07 19:49:32 +0000808/// getObjCQualifiedInterfaceType - Return a
809/// ObjCQualifiedInterfaceType type for the given interface decl and
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000810/// the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000811QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
812 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000813 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000814 ObjCQualifiedInterfaceType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000815
816 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000817 if (ObjCQualifiedInterfaceType *QT =
818 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000819 return QualType(QT, 0);
820
821 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +0000822 ObjCQualifiedInterfaceType *QType =
823 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000824 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000825 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000826 return QualType(QType, 0);
827}
828
Ted Kremenek42730c52008-01-07 19:49:32 +0000829/// getObjCQualifiedIdType - Return a
830/// getObjCQualifiedIdType type for the 'id' decl and
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000831/// the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000832QualType ASTContext::getObjCQualifiedIdType(QualType idType,
833 ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000834 unsigned NumProtocols) {
835 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000836 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000837
838 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000839 if (ObjCQualifiedIdType *QT =
840 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000841 return QualType(QT, 0);
842
843 // No Match;
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000844 QualType Canonical;
845 if (!idType->isCanonical()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000846 Canonical = getObjCQualifiedIdType(idType.getCanonicalType(),
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000847 Protocols, NumProtocols);
Ted Kremenek42730c52008-01-07 19:49:32 +0000848 ObjCQualifiedIdType *NewQT =
849 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000850 assert(NewQT == 0 && "Shouldn't be in the map!");
851 }
852
Ted Kremenek42730c52008-01-07 19:49:32 +0000853 ObjCQualifiedIdType *QType =
854 new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000855 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000856 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000857 return QualType(QType, 0);
858}
859
Steve Naroff0604dd92007-08-01 18:02:17 +0000860/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
861/// TypeOfExpr AST's (since expression's are never shared). For example,
862/// multiple declarations that refer to "typeof(x)" all contain different
863/// DeclRefExpr's. This doesn't effect the type checker, since it operates
864/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000865QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroff7cbb1462007-07-31 12:34:36 +0000866 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000867 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
868 Types.push_back(toe);
869 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000870}
871
Steve Naroff0604dd92007-08-01 18:02:17 +0000872/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
873/// TypeOfType AST's. The only motivation to unique these nodes would be
874/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
875/// an issue. This doesn't effect the type checker, since it operates
876/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000877QualType ASTContext::getTypeOfType(QualType tofType) {
878 QualType Canonical = tofType.getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000879 TypeOfType *tot = new TypeOfType(tofType, Canonical);
880 Types.push_back(tot);
881 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000882}
883
Chris Lattner4b009652007-07-25 00:24:17 +0000884/// getTagDeclType - Return the unique reference to the type for the
885/// specified TagDecl (struct/union/class/enum) decl.
886QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +0000887 assert (Decl);
888
Ted Kremenekf05026d2007-11-14 00:03:20 +0000889 // The decl stores the type cache.
Ted Kremenekae8fa032007-11-26 21:16:01 +0000890 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Ted Kremenekf05026d2007-11-14 00:03:20 +0000891
892 TagType* T = new TagType(Decl, QualType());
Ted Kremenekae8fa032007-11-26 21:16:01 +0000893 Types.push_back(T);
894 Decl->TypeForDecl = T;
Ted Kremenekf05026d2007-11-14 00:03:20 +0000895
896 return QualType(T, 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000897}
898
899/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
900/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
901/// needs to agree with the definition in <stddef.h>.
902QualType ASTContext::getSizeType() const {
903 // On Darwin, size_t is defined as a "long unsigned int".
904 // FIXME: should derive from "Target".
905 return UnsignedLongTy;
906}
907
Eli Friedmanfdd35d72008-02-12 08:29:21 +0000908/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
909/// width of characters in wide strings, The value is target dependent and
910/// needs to agree with the definition in <stddef.h>.
911QualType ASTContext::getWcharType() const {
912 // On Darwin, wchar_t is defined as a "int".
913 // FIXME: should derive from "Target".
914 return IntTy;
915}
916
Chris Lattner4b009652007-07-25 00:24:17 +0000917/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
918/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
919QualType ASTContext::getPointerDiffType() const {
920 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
921 // FIXME: should derive from "Target".
922 return IntTy;
923}
924
925/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
926/// routine will assert if passed a built-in type that isn't an integer or enum.
927static int getIntegerRank(QualType t) {
928 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
929 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
930 return 4;
931 }
932
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000933 const BuiltinType *BT = t.getCanonicalType()->getAsBuiltinType();
Chris Lattner4b009652007-07-25 00:24:17 +0000934 switch (BT->getKind()) {
935 default:
936 assert(0 && "getIntegerRank(): not a built-in integer");
937 case BuiltinType::Bool:
938 return 1;
939 case BuiltinType::Char_S:
940 case BuiltinType::Char_U:
941 case BuiltinType::SChar:
942 case BuiltinType::UChar:
943 return 2;
944 case BuiltinType::Short:
945 case BuiltinType::UShort:
946 return 3;
947 case BuiltinType::Int:
948 case BuiltinType::UInt:
949 return 4;
950 case BuiltinType::Long:
951 case BuiltinType::ULong:
952 return 5;
953 case BuiltinType::LongLong:
954 case BuiltinType::ULongLong:
955 return 6;
956 }
957}
958
959/// getFloatingRank - Return a relative rank for floating point types.
960/// This routine will assert if passed a built-in type that isn't a float.
961static int getFloatingRank(QualType T) {
962 T = T.getCanonicalType();
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000963 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +0000964 return getFloatingRank(CT->getElementType());
965
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000966 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattner5003e8b2007-11-01 05:03:41 +0000967 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +0000968 case BuiltinType::Float: return FloatRank;
969 case BuiltinType::Double: return DoubleRank;
970 case BuiltinType::LongDouble: return LongDoubleRank;
971 }
972}
973
Steve Narofffa0c4532007-08-27 01:41:48 +0000974/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
975/// point or a complex type (based on typeDomain/typeSize).
976/// 'typeDomain' is a real floating point or complex type.
977/// 'typeSize' is a real floating point or complex type.
Steve Naroff3cf497f2007-08-27 01:27:54 +0000978QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
979 QualType typeSize, QualType typeDomain) const {
980 if (typeDomain->isComplexType()) {
981 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000982 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000983 case FloatRank: return FloatComplexTy;
984 case DoubleRank: return DoubleComplexTy;
985 case LongDoubleRank: return LongDoubleComplexTy;
986 }
Chris Lattner4b009652007-07-25 00:24:17 +0000987 }
Steve Naroff3cf497f2007-08-27 01:27:54 +0000988 if (typeDomain->isRealFloatingType()) {
989 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000990 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000991 case FloatRank: return FloatTy;
992 case DoubleRank: return DoubleTy;
993 case LongDoubleRank: return LongDoubleTy;
994 }
995 }
996 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattner1d2b4612007-09-16 19:23:47 +0000997 //an invalid return value, but the assert
998 //will ensure that this code is never reached.
999 return VoidTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001000}
1001
Steve Naroff45fc9822007-08-27 15:30:22 +00001002/// compareFloatingType - Handles 3 different combos:
1003/// float/float, float/complex, complex/complex.
1004/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
1005int ASTContext::compareFloatingType(QualType lt, QualType rt) {
1006 if (getFloatingRank(lt) == getFloatingRank(rt))
1007 return 0;
1008 if (getFloatingRank(lt) > getFloatingRank(rt))
1009 return 1;
1010 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001011}
1012
1013// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
1014// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
1015QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
1016 if (lhs == rhs) return lhs;
1017
1018 bool t1Unsigned = lhs->isUnsignedIntegerType();
1019 bool t2Unsigned = rhs->isUnsignedIntegerType();
1020
1021 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
1022 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
1023
1024 // We have two integer types with differing signs
1025 QualType unsignedType = t1Unsigned ? lhs : rhs;
1026 QualType signedType = t1Unsigned ? rhs : lhs;
1027
1028 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
1029 return unsignedType;
1030 else {
1031 // FIXME: Need to check if the signed type can represent all values of the
1032 // unsigned type. If it can, then the result is the signed type.
1033 // If it can't, then the result is the unsigned version of the signed type.
1034 // Should probably add a helper that returns a signed integer type from
1035 // an unsigned (and vice versa). C99 6.3.1.8.
1036 return signedType;
1037 }
1038}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001039
1040// getCFConstantStringType - Return the type used for constant CFStrings.
1041QualType ASTContext::getCFConstantStringType() {
1042 if (!CFConstantStringTypeDecl) {
1043 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
Steve Naroff0add5d22007-11-03 11:27:19 +00001044 &Idents.get("NSConstantString"),
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001045 0);
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001046 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001047
1048 // const int *isa;
1049 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001050 // int flags;
1051 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001052 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001053 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001054 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001055 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001056 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001057 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001058
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001059 for (unsigned i = 0; i < 4; ++i)
Steve Naroffdc1ad762007-09-14 02:20:46 +00001060 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001061
1062 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1063 }
1064
1065 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001066}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001067
Anders Carlssone3f02572007-10-29 06:33:42 +00001068// This returns true if a type has been typedefed to BOOL:
1069// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001070static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001071 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001072 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001073
1074 return false;
1075}
1076
Ted Kremenek42730c52008-01-07 19:49:32 +00001077/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001078/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001079int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001080 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001081
1082 // Make all integer and enum types at least as large as an int
1083 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001084 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001085 // Treat arrays as pointers, since that's how they're passed in.
1086 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001087 sz = getTypeSize(VoidPtrTy);
1088 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001089}
1090
Ted Kremenek42730c52008-01-07 19:49:32 +00001091/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001092/// declaration.
Ted Kremenek42730c52008-01-07 19:49:32 +00001093void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001094 std::string& S)
1095{
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001096 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001097 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001098 // Encode result type.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001099 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001100 // Compute size of all parameters.
1101 // Start with computing size of a pointer in number of bytes.
1102 // FIXME: There might(should) be a better way of doing this computation!
1103 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001104 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001105 // The first two arguments (self and _cmd) are pointers; account for
1106 // their size.
1107 int ParmOffset = 2 * PtrSize;
1108 int NumOfParams = Decl->getNumParams();
1109 for (int i = 0; i < NumOfParams; i++) {
1110 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001111 int sz = getObjCEncodingTypeSize (PType);
1112 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001113 ParmOffset += sz;
1114 }
1115 S += llvm::utostr(ParmOffset);
1116 S += "@0:";
1117 S += llvm::utostr(PtrSize);
1118
1119 // Argument types.
1120 ParmOffset = 2 * PtrSize;
1121 for (int i = 0; i < NumOfParams; i++) {
1122 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001123 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001124 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001125 getObjCEncodingForTypeQualifier(
1126 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian248db262008-01-22 22:44:46 +00001127 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001128 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001129 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001130 }
1131}
1132
Fariborz Jahanian248db262008-01-22 22:44:46 +00001133void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1134 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson36f07d82007-10-29 05:01:08 +00001135{
Anders Carlssone3f02572007-10-29 06:33:42 +00001136 // FIXME: This currently doesn't encode:
1137 // @ An object (whether statically typed or typed id)
1138 // # A class object (Class)
1139 // : A method selector (SEL)
1140 // {name=type...} A structure
1141 // (name=type...) A union
1142 // bnum A bit field of num bits
1143
1144 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001145 char encoding;
1146 switch (BT->getKind()) {
1147 case BuiltinType::Void:
1148 encoding = 'v';
1149 break;
1150 case BuiltinType::Bool:
1151 encoding = 'B';
1152 break;
1153 case BuiltinType::Char_U:
1154 case BuiltinType::UChar:
1155 encoding = 'C';
1156 break;
1157 case BuiltinType::UShort:
1158 encoding = 'S';
1159 break;
1160 case BuiltinType::UInt:
1161 encoding = 'I';
1162 break;
1163 case BuiltinType::ULong:
1164 encoding = 'L';
1165 break;
1166 case BuiltinType::ULongLong:
1167 encoding = 'Q';
1168 break;
1169 case BuiltinType::Char_S:
1170 case BuiltinType::SChar:
1171 encoding = 'c';
1172 break;
1173 case BuiltinType::Short:
1174 encoding = 's';
1175 break;
1176 case BuiltinType::Int:
1177 encoding = 'i';
1178 break;
1179 case BuiltinType::Long:
1180 encoding = 'l';
1181 break;
1182 case BuiltinType::LongLong:
1183 encoding = 'q';
1184 break;
1185 case BuiltinType::Float:
1186 encoding = 'f';
1187 break;
1188 case BuiltinType::Double:
1189 encoding = 'd';
1190 break;
1191 case BuiltinType::LongDouble:
1192 encoding = 'd';
1193 break;
1194 default:
1195 assert(0 && "Unhandled builtin type kind");
1196 }
1197
1198 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001199 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001200 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001201 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001202 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001203
1204 }
1205 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001206 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001207 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001208 S += '@';
1209 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001210 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001211 S += '#';
1212 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001213 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001214 S += ':';
1215 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001216 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001217
1218 if (PointeeTy->isCharType()) {
1219 // char pointer types should be encoded as '*' unless it is a
1220 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001221 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001222 S += '*';
1223 return;
1224 }
1225 }
1226
1227 S += '^';
Fariborz Jahanian248db262008-01-22 22:44:46 +00001228 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone3f02572007-10-29 06:33:42 +00001229 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001230 S += '[';
1231
1232 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1233 S += llvm::utostr(CAT->getSize().getZExtValue());
1234 else
1235 assert(0 && "Unhandled array type!");
1236
Fariborz Jahanian248db262008-01-22 22:44:46 +00001237 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001238 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001239 } else if (T->getAsFunctionType()) {
1240 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001241 } else if (const RecordType *RTy = T->getAsRecordType()) {
1242 RecordDecl *RDecl= RTy->getDecl();
1243 S += '{';
1244 S += RDecl->getName();
Fariborz Jahanian248db262008-01-22 22:44:46 +00001245 bool found = false;
1246 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1247 if (ERType[i] == RTy) {
1248 found = true;
1249 break;
1250 }
1251 if (!found) {
1252 ERType.push_back(RTy);
1253 S += '=';
1254 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1255 FieldDecl *field = RDecl->getMember(i);
1256 getObjCEncodingForType(field->getType(), S, ERType);
1257 }
1258 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1259 ERType.pop_back();
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001260 }
1261 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001262 } else if (T->isEnumeralType()) {
1263 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001264 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001265 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001266}
1267
Ted Kremenek42730c52008-01-07 19:49:32 +00001268void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001269 std::string& S) const {
1270 if (QT & Decl::OBJC_TQ_In)
1271 S += 'n';
1272 if (QT & Decl::OBJC_TQ_Inout)
1273 S += 'N';
1274 if (QT & Decl::OBJC_TQ_Out)
1275 S += 'o';
1276 if (QT & Decl::OBJC_TQ_Bycopy)
1277 S += 'O';
1278 if (QT & Decl::OBJC_TQ_Byref)
1279 S += 'R';
1280 if (QT & Decl::OBJC_TQ_Oneway)
1281 S += 'V';
1282}
1283
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001284void ASTContext::setBuiltinVaListType(QualType T)
1285{
1286 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1287
1288 BuiltinVaListType = T;
1289}
1290
Ted Kremenek42730c52008-01-07 19:49:32 +00001291void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001292{
Ted Kremenek42730c52008-01-07 19:49:32 +00001293 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff9d12c902007-10-15 14:41:52 +00001294
Ted Kremenek42730c52008-01-07 19:49:32 +00001295 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001296
1297 // typedef struct objc_object *id;
1298 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1299 assert(ptr && "'id' incorrectly typed");
1300 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1301 assert(rec && "'id' incorrectly typed");
1302 IdStructType = rec;
1303}
1304
Ted Kremenek42730c52008-01-07 19:49:32 +00001305void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001306{
Ted Kremenek42730c52008-01-07 19:49:32 +00001307 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001308
Ted Kremenek42730c52008-01-07 19:49:32 +00001309 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001310
1311 // typedef struct objc_selector *SEL;
1312 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1313 assert(ptr && "'SEL' incorrectly typed");
1314 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1315 assert(rec && "'SEL' incorrectly typed");
1316 SelStructType = rec;
1317}
1318
Ted Kremenek42730c52008-01-07 19:49:32 +00001319void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001320{
Ted Kremenek42730c52008-01-07 19:49:32 +00001321 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1322 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001323}
1324
Ted Kremenek42730c52008-01-07 19:49:32 +00001325void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001326{
Ted Kremenek42730c52008-01-07 19:49:32 +00001327 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001328
Ted Kremenek42730c52008-01-07 19:49:32 +00001329 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001330
1331 // typedef struct objc_class *Class;
1332 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1333 assert(ptr && "'Class' incorrectly typed");
1334 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1335 assert(rec && "'Class' incorrectly typed");
1336 ClassStructType = rec;
1337}
1338
Ted Kremenek42730c52008-01-07 19:49:32 +00001339void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1340 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001341 "'NSConstantString' type already set!");
1342
Ted Kremenek42730c52008-01-07 19:49:32 +00001343 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001344}
1345
Steve Naroff85f0dc52007-10-15 20:41:53 +00001346bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1347 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1348 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1349
1350 return lBuiltin->getKind() == rBuiltin->getKind();
1351}
1352
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001353/// objcTypesAreCompatible - This routine is called when two types
1354/// are of different class; one is interface type or is
1355/// a qualified interface type and the other type is of a different class.
1356/// Example, II or II<P>.
Steve Naroff85f0dc52007-10-15 20:41:53 +00001357bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001358 if (lhs->isObjCInterfaceType() && isObjCIdType(rhs))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001359 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001360 else if (isObjCIdType(lhs) && rhs->isObjCInterfaceType())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001361 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001362 if (ObjCInterfaceType *lhsIT =
1363 dyn_cast<ObjCInterfaceType>(lhs.getCanonicalType().getTypePtr())) {
1364 ObjCQualifiedInterfaceType *rhsQI =
1365 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001366 return rhsQI && (lhsIT->getDecl() == rhsQI->getDecl());
1367 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001368 else if (ObjCInterfaceType *rhsIT =
1369 dyn_cast<ObjCInterfaceType>(rhs.getCanonicalType().getTypePtr())) {
1370 ObjCQualifiedInterfaceType *lhsQI =
1371 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001372 return lhsQI && (rhsIT->getDecl() == lhsQI->getDecl());
1373 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00001374 return false;
1375}
1376
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001377/// Check that 'lhs' and 'rhs' are compatible interface types. Both types
1378/// must be canonical types.
Steve Naroff85f0dc52007-10-15 20:41:53 +00001379bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001380 assert (lhs->isCanonical() &&
1381 "interfaceTypesAreCompatible strip typedefs of lhs");
1382 assert (rhs->isCanonical() &&
1383 "interfaceTypesAreCompatible strip typedefs of rhs");
Fariborz Jahaniance2de812007-12-20 22:37:58 +00001384 if (lhs == rhs)
1385 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001386 ObjCInterfaceType *lhsIT = cast<ObjCInterfaceType>(lhs.getTypePtr());
1387 ObjCInterfaceType *rhsIT = cast<ObjCInterfaceType>(rhs.getTypePtr());
1388 ObjCInterfaceDecl *rhsIDecl = rhsIT->getDecl();
1389 ObjCInterfaceDecl *lhsIDecl = lhsIT->getDecl();
Fariborz Jahaniance2de812007-12-20 22:37:58 +00001390 // rhs is derived from lhs it is OK; else it is not OK.
1391 while (rhsIDecl != NULL) {
1392 if (rhsIDecl == lhsIDecl)
1393 return true;
1394 rhsIDecl = rhsIDecl->getSuperClass();
1395 }
1396 return false;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001397}
1398
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001399bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
1400 QualType rhs) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001401 ObjCQualifiedInterfaceType *lhsQI =
1402 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001403 assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
Ted Kremenek42730c52008-01-07 19:49:32 +00001404 ObjCQualifiedInterfaceType *rhsQI =
1405 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001406 assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001407 if (!interfaceTypesAreCompatible(
1408 getObjCInterfaceType(lhsQI->getDecl()).getCanonicalType(),
1409 getObjCInterfaceType(rhsQI->getDecl()).getCanonicalType()))
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001410 return false;
1411 /* All protocols in lhs must have a presense in rhs. */
1412 for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
1413 bool match = false;
Ted Kremenek42730c52008-01-07 19:49:32 +00001414 ObjCProtocolDecl *lhsProto = lhsQI->getProtocols(i);
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001415 for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001416 ObjCProtocolDecl *rhsProto = rhsQI->getProtocols(j);
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001417 if (lhsProto == rhsProto) {
1418 match = true;
1419 break;
1420 }
1421 }
1422 if (!match)
1423 return false;
1424 }
1425 return true;
1426}
1427
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001428/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
1429/// inheritance hierarchy of 'rProto'.
Ted Kremenek42730c52008-01-07 19:49:32 +00001430static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
1431 ObjCProtocolDecl *rProto) {
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001432 if (lProto == rProto)
1433 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001434 ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001435 for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
1436 if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
1437 return true;
1438 return false;
1439}
1440
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001441/// ClassImplementsProtocol - Checks that 'lProto' protocol
1442/// has been implemented in IDecl class, its super class or categories (if
1443/// lookupCategory is true).
Ted Kremenek42730c52008-01-07 19:49:32 +00001444static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1445 ObjCInterfaceDecl *IDecl,
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001446 bool lookupCategory) {
1447
1448 // 1st, look up the class.
Ted Kremenek42730c52008-01-07 19:49:32 +00001449 ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols();
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001450 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
1451 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1452 return true;
1453 }
1454
1455 // 2nd, look up the category.
1456 if (lookupCategory)
Ted Kremenek42730c52008-01-07 19:49:32 +00001457 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001458 CDecl = CDecl->getNextClassCategory()) {
1459 protoList = CDecl->getReferencedProtocols();
1460 for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
1461 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1462 return true;
1463 }
1464 }
1465
1466 // 3rd, look up the super class(s)
1467 if (IDecl->getSuperClass())
1468 return
1469 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory);
1470
1471 return false;
1472}
1473
Ted Kremenek42730c52008-01-07 19:49:32 +00001474/// ObjCQualifiedIdTypesAreCompatible - Compares two types, at least
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001475/// one of which is a protocol qualified 'id' type. When 'compare'
1476/// is true it is for comparison; when false, for assignment/initialization.
Ted Kremenek42730c52008-01-07 19:49:32 +00001477bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs,
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001478 QualType rhs,
1479 bool compare) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001480 // match id<P..> with an 'id' type in all cases.
1481 if (const PointerType *PT = lhs->getAsPointerType()) {
1482 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001483 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001484 return true;
1485
1486 }
1487 else if (const PointerType *PT = rhs->getAsPointerType()) {
1488 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001489 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001490 return true;
1491
1492 }
1493
Ted Kremenek42730c52008-01-07 19:49:32 +00001494 ObjCQualifiedInterfaceType *lhsQI = 0;
1495 ObjCQualifiedInterfaceType *rhsQI = 0;
1496 ObjCInterfaceDecl *lhsID = 0;
1497 ObjCInterfaceDecl *rhsID = 0;
1498 ObjCQualifiedIdType *lhsQID = dyn_cast<ObjCQualifiedIdType>(lhs);
1499 ObjCQualifiedIdType *rhsQID = dyn_cast<ObjCQualifiedIdType>(rhs);
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001500
1501 if (lhsQID) {
1502 if (!rhsQID && rhs->getTypeClass() == Type::Pointer) {
1503 QualType rtype =
1504 cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1505 rhsQI =
Ted Kremenek42730c52008-01-07 19:49:32 +00001506 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001507 rtype.getCanonicalType().getTypePtr());
Fariborz Jahanian87829072007-12-20 19:24:10 +00001508 if (!rhsQI) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001509 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanian87829072007-12-20 19:24:10 +00001510 rtype.getCanonicalType().getTypePtr());
1511 if (IT)
1512 rhsID = IT->getDecl();
1513 }
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001514 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001515 if (!rhsQI && !rhsQID && !rhsID)
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001516 return false;
1517
Fariborz Jahaniance5528d2008-01-03 20:01:35 +00001518 unsigned numRhsProtocols = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001519 ObjCProtocolDecl **rhsProtoList = 0;
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001520 if (rhsQI) {
1521 numRhsProtocols = rhsQI->getNumProtocols();
1522 rhsProtoList = rhsQI->getReferencedProtocols();
1523 }
1524 else if (rhsQID) {
1525 numRhsProtocols = rhsQID->getNumProtocols();
1526 rhsProtoList = rhsQID->getReferencedProtocols();
1527 }
1528
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001529 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001530 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001531 bool match = false;
1532
1533 // when comparing an id<P> on lhs with a static type on rhs,
1534 // see if static class implements all of id's protocols, directly or
1535 // through its super class and categories.
1536 if (rhsID) {
1537 if (ClassImplementsProtocol(lhsProto, rhsID, true))
1538 match = true;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001539 }
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001540 else for (unsigned j = 0; j < numRhsProtocols; j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001541 ObjCProtocolDecl *rhsProto = rhsProtoList[j];
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001542 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1543 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001544 match = true;
1545 break;
1546 }
1547 }
1548 if (!match)
1549 return false;
1550 }
1551 }
1552 else if (rhsQID) {
1553 if (!lhsQID && lhs->getTypeClass() == Type::Pointer) {
1554 QualType ltype =
1555 cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1556 lhsQI =
Ted Kremenek42730c52008-01-07 19:49:32 +00001557 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001558 ltype.getCanonicalType().getTypePtr());
Fariborz Jahanian87829072007-12-20 19:24:10 +00001559 if (!lhsQI) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001560 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanian87829072007-12-20 19:24:10 +00001561 ltype.getCanonicalType().getTypePtr());
1562 if (IT)
1563 lhsID = IT->getDecl();
1564 }
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001565 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001566 if (!lhsQI && !lhsQID && !lhsID)
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001567 return false;
Fariborz Jahanian87829072007-12-20 19:24:10 +00001568
Fariborz Jahaniance5528d2008-01-03 20:01:35 +00001569 unsigned numLhsProtocols = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001570 ObjCProtocolDecl **lhsProtoList = 0;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001571 if (lhsQI) {
1572 numLhsProtocols = lhsQI->getNumProtocols();
1573 lhsProtoList = lhsQI->getReferencedProtocols();
1574 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001575 else if (lhsQID) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001576 numLhsProtocols = lhsQID->getNumProtocols();
1577 lhsProtoList = lhsQID->getReferencedProtocols();
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001578 }
1579 bool match = false;
1580 // for static type vs. qualified 'id' type, check that class implements
1581 // one of 'id's protocols.
1582 if (lhsID) {
1583 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001584 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001585 if (ClassImplementsProtocol(rhsProto, lhsID, compare)) {
1586 match = true;
1587 break;
1588 }
1589 }
1590 }
1591 else for (unsigned i =0; i < numLhsProtocols; i++) {
1592 match = false;
Ted Kremenek42730c52008-01-07 19:49:32 +00001593 ObjCProtocolDecl *lhsProto = lhsProtoList[i];
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001594 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001595 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001596 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1597 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001598 match = true;
1599 break;
1600 }
1601 }
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001602 }
1603 if (!match)
1604 return false;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001605 }
1606 return true;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001607}
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001608
Chris Lattner5003e8b2007-11-01 05:03:41 +00001609bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1610 const VectorType *lVector = lhs->getAsVectorType();
1611 const VectorType *rVector = rhs->getAsVectorType();
1612
1613 if ((lVector->getElementType().getCanonicalType() ==
1614 rVector->getElementType().getCanonicalType()) &&
1615 (lVector->getNumElements() == rVector->getNumElements()))
1616 return true;
1617 return false;
1618}
1619
Steve Naroff85f0dc52007-10-15 20:41:53 +00001620// C99 6.2.7p1: If both are complete types, then the following additional
1621// requirements apply...FIXME (handle compatibility across source files).
1622bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
Steve Naroff4a5e2072007-11-07 06:03:51 +00001623 // "Class" and "id" are compatible built-in structure types.
Ted Kremenek42730c52008-01-07 19:49:32 +00001624 if (isObjCIdType(lhs) && isObjCClassType(rhs) ||
1625 isObjCClassType(lhs) && isObjCIdType(rhs))
Steve Naroff4a5e2072007-11-07 06:03:51 +00001626 return true;
Eli Friedmane7fb03a2008-02-15 06:03:44 +00001627
1628 // Within a translation unit a tag type is
1629 // only compatible with itself.
1630 return lhs.getCanonicalType() == rhs.getCanonicalType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001631}
1632
1633bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1634 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1635 // identically qualified and both shall be pointers to compatible types.
Chris Lattner35fef522008-02-20 20:55:12 +00001636 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1637 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001638 return false;
1639
1640 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1641 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1642
1643 return typesAreCompatible(ltype, rtype);
1644}
1645
Bill Wendling6a9d8542007-12-03 07:33:35 +00001646// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroff85f0dc52007-10-15 20:41:53 +00001647// reference to T, the operation assigns to the object of type T denoted by the
1648// reference.
1649bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1650 QualType ltype = lhs;
1651
1652 if (lhs->isReferenceType())
1653 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1654
1655 QualType rtype = rhs;
1656
1657 if (rhs->isReferenceType())
1658 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1659
1660 return typesAreCompatible(ltype, rtype);
1661}
1662
1663bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1664 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1665 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1666 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1667 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1668
1669 // first check the return types (common between C99 and K&R).
1670 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1671 return false;
1672
1673 if (lproto && rproto) { // two C99 style function prototypes
1674 unsigned lproto_nargs = lproto->getNumArgs();
1675 unsigned rproto_nargs = rproto->getNumArgs();
1676
1677 if (lproto_nargs != rproto_nargs)
1678 return false;
1679
1680 // both prototypes have the same number of arguments.
1681 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1682 (rproto->isVariadic() && !lproto->isVariadic()))
1683 return false;
1684
1685 // The use of ellipsis agree...now check the argument types.
1686 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001687 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1688 // is taken as having the unqualified version of it's declared type.
Steve Naroffdec17fe2008-01-29 00:15:50 +00001689 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001690 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001691 return false;
1692 return true;
1693 }
1694 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1695 return true;
1696
1697 // we have a mixture of K&R style with C99 prototypes
1698 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1699
1700 if (proto->isVariadic())
1701 return false;
1702
1703 // FIXME: Each parameter type T in the prototype must be compatible with the
1704 // type resulting from applying the usual argument conversions to T.
1705 return true;
1706}
1707
1708bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
Eli Friedman1e7537832008-02-06 04:53:22 +00001709 // Compatible arrays must have compatible element types
1710 QualType ltype = lhs->getAsArrayType()->getElementType();
1711 QualType rtype = rhs->getAsArrayType()->getElementType();
1712
Steve Naroff85f0dc52007-10-15 20:41:53 +00001713 if (!typesAreCompatible(ltype, rtype))
1714 return false;
Eli Friedman1e7537832008-02-06 04:53:22 +00001715
1716 // Compatible arrays must be the same size
1717 if (const ConstantArrayType* LCAT = lhs->getAsConstantArrayType())
1718 if (const ConstantArrayType* RCAT = rhs->getAsConstantArrayType())
1719 return RCAT->getSize() == LCAT->getSize();
1720
Steve Naroff85f0dc52007-10-15 20:41:53 +00001721 return true;
1722}
1723
1724/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1725/// both shall have the identically qualified version of a compatible type.
1726/// C99 6.2.7p1: Two types have compatible types if their types are the
1727/// same. See 6.7.[2,3,5] for additional rules.
1728bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
Chris Lattner35fef522008-02-20 20:55:12 +00001729 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1730 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroff577f9722008-01-29 18:58:14 +00001731 return false;
1732
Steve Naroff85f0dc52007-10-15 20:41:53 +00001733 QualType lcanon = lhs.getCanonicalType();
1734 QualType rcanon = rhs.getCanonicalType();
1735
1736 // If two types are identical, they are are compatible
1737 if (lcanon == rcanon)
1738 return true;
Bill Wendling6a9d8542007-12-03 07:33:35 +00001739
1740 // C++ [expr]: If an expression initially has the type "reference to T", the
1741 // type is adjusted to "T" prior to any further analysis, the expression
1742 // designates the object or function denoted by the reference, and the
1743 // expression is an lvalue.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001744 if (ReferenceType *RT = dyn_cast<ReferenceType>(lcanon))
1745 lcanon = RT->getReferenceeType();
1746 if (ReferenceType *RT = dyn_cast<ReferenceType>(rcanon))
1747 rcanon = RT->getReferenceeType();
1748
1749 Type::TypeClass LHSClass = lcanon->getTypeClass();
1750 Type::TypeClass RHSClass = rcanon->getTypeClass();
1751
1752 // We want to consider the two function types to be the same for these
1753 // comparisons, just force one to the other.
1754 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1755 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00001756
1757 // Same as above for arrays
1758 if (LHSClass == Type::VariableArray) LHSClass = Type::ConstantArray;
1759 if (RHSClass == Type::VariableArray) RHSClass = Type::ConstantArray;
Eli Friedman8ff07782008-02-15 18:16:39 +00001760 if (LHSClass == Type::IncompleteArray) LHSClass = Type::ConstantArray;
1761 if (RHSClass == Type::IncompleteArray) RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001762
Steve Naroffc88babe2008-01-09 22:43:08 +00001763 // If the canonical type classes don't match...
Chris Lattnerc38d4522008-01-14 05:45:46 +00001764 if (LHSClass != RHSClass) {
Steve Naroff85f0dc52007-10-15 20:41:53 +00001765 // For Objective-C, it is possible for two types to be compatible
1766 // when their classes don't match (when dealing with "id"). If either type
1767 // is an interface, we defer to objcTypesAreCompatible().
Ted Kremenek42730c52008-01-07 19:49:32 +00001768 if (lcanon->isObjCInterfaceType() || rcanon->isObjCInterfaceType())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001769 return objcTypesAreCompatible(lcanon, rcanon);
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001770
Chris Lattnerc38d4522008-01-14 05:45:46 +00001771 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1772 // a signed integer type, or an unsigned integer type.
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001773 if (lcanon->isEnumeralType() && rcanon->isIntegralType()) {
1774 EnumDecl* EDecl = cast<EnumDecl>(cast<TagType>(lcanon)->getDecl());
1775 return EDecl->getIntegerType() == rcanon;
1776 }
1777 if (rcanon->isEnumeralType() && lcanon->isIntegralType()) {
1778 EnumDecl* EDecl = cast<EnumDecl>(cast<TagType>(rcanon)->getDecl());
1779 return EDecl->getIntegerType() == lcanon;
1780 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001781
Steve Naroff85f0dc52007-10-15 20:41:53 +00001782 return false;
1783 }
Steve Naroffc88babe2008-01-09 22:43:08 +00001784 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001785 switch (LHSClass) {
1786 case Type::FunctionProto: assert(0 && "Canonicalized away above");
1787 case Type::Pointer:
1788 return pointerTypesAreCompatible(lcanon, rcanon);
1789 case Type::ConstantArray:
1790 case Type::VariableArray:
Eli Friedman8ff07782008-02-15 18:16:39 +00001791 case Type::IncompleteArray:
Chris Lattnerc38d4522008-01-14 05:45:46 +00001792 return arrayTypesAreCompatible(lcanon, rcanon);
1793 case Type::FunctionNoProto:
1794 return functionTypesAreCompatible(lcanon, rcanon);
1795 case Type::Tagged: // handle structures, unions
1796 return tagTypesAreCompatible(lcanon, rcanon);
1797 case Type::Builtin:
1798 return builtinTypesAreCompatible(lcanon, rcanon);
1799 case Type::ObjCInterface:
1800 return interfaceTypesAreCompatible(lcanon, rcanon);
1801 case Type::Vector:
1802 case Type::OCUVector:
1803 return vectorTypesAreCompatible(lcanon, rcanon);
1804 case Type::ObjCQualifiedInterface:
1805 return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
1806 default:
1807 assert(0 && "unexpected type");
Steve Naroff85f0dc52007-10-15 20:41:53 +00001808 }
1809 return true; // should never get here...
1810}
Ted Kremenek738e6c02007-10-31 17:10:13 +00001811
Ted Kremenek738e6c02007-10-31 17:10:13 +00001812/// Emit - Serialize an ASTContext object to Bitcode.
1813void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00001814 S.EmitRef(SourceMgr);
1815 S.EmitRef(Target);
1816 S.EmitRef(Idents);
1817 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001818
Ted Kremenek68228a92007-10-31 22:44:07 +00001819 // Emit the size of the type vector so that we can reserve that size
1820 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001821 S.EmitInt(Types.size());
1822
Ted Kremenek034a78c2007-11-13 22:02:55 +00001823 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1824 I!=E;++I)
1825 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001826
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001827 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001828}
1829
Ted Kremenekacba3612007-11-13 00:25:37 +00001830ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek68228a92007-10-31 22:44:07 +00001831 SourceManager &SM = D.ReadRef<SourceManager>();
1832 TargetInfo &t = D.ReadRef<TargetInfo>();
1833 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1834 SelectorTable &sels = D.ReadRef<SelectorTable>();
1835
1836 unsigned size_reserve = D.ReadInt();
1837
1838 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1839
Ted Kremenek034a78c2007-11-13 22:02:55 +00001840 for (unsigned i = 0; i < size_reserve; ++i)
1841 Type::Create(*A,i,D);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001842
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001843 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00001844
1845 return A;
1846}