blob: d716917b9ce4e98a1f161adec46d5550b94cc204 [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();
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000185 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000186 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());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000199 Width = 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 Lattnerfc18dcc2008-03-08 08:52:55 +0000207 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Chris Lattner4b009652007-07-25 00:24:17 +0000208 // FIXME: Vector alignment is not the alignment of its elements.
209 Align = EltInfo.second;
210 break;
211 }
212
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000213 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000214 // FIXME: need to use TargetInfo to derive the target specific sizes. This
215 // implementation will suffice for play with vector support.
216 switch (cast<BuiltinType>(T)->getKind()) {
217 default: assert(0 && "Unknown builtin type!");
218 case BuiltinType::Void:
219 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000220 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000221 Width = Target.getBoolWidth();
222 Align = Target.getBoolAlign();
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 Lattnerfc18dcc2008-03-08 08:52:55 +0000228 Width = Target.getCharWidth();
229 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000230 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000231 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000232 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000233 Width = Target.getShortWidth();
234 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000235 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000236 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000237 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000238 Width = Target.getIntWidth();
239 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000240 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000241 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000242 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000243 Width = Target.getLongWidth();
244 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000245 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000246 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000247 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000248 Width = Target.getLongLongWidth();
249 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000250 break;
251 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000252 Width = Target.getFloatWidth();
253 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000254 break;
255 case BuiltinType::Double:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000256 Width = Target.getDoubleWidth();
257 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000258 break;
259 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000260 Width = Target.getLongDoubleWidth();
261 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000262 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000263 }
264 break;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000265 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000266 // FIXME: Pointers into different addr spaces could have different sizes and
267 // alignment requirements: getPointerInfo should take an AddrSpace.
268 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000269 case Type::ObjCQualifiedId:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000270 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000271 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000272 break;
Chris Lattner461a6c52008-03-08 08:34:58 +0000273 case Type::Pointer: {
274 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000275 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000276 Align = Target.getPointerAlign(AS);
277 break;
278 }
Chris Lattner4b009652007-07-25 00:24:17 +0000279 case Type::Reference:
280 // "When applied to a reference or a reference type, the result is the size
281 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000282 // FIXME: This is wrong for struct layout: a reference in a struct has
283 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000284 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner4b009652007-07-25 00:24:17 +0000285
286 case Type::Complex: {
287 // Complex types have the same alignment as their elements, but twice the
288 // size.
289 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000290 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000291 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000292 Align = EltInfo.second;
293 break;
294 }
295 case Type::Tagged:
Chris Lattnereb56d292007-08-27 17:38:00 +0000296 TagType *TT = cast<TagType>(T);
297 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000298 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000299 Width = Layout.getSize();
Chris Lattnereb56d292007-08-27 17:38:00 +0000300 Align = Layout.getAlignment();
301 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000302 return getTypeInfo(ED->getIntegerType());
Chris Lattnereb56d292007-08-27 17:38:00 +0000303 } else {
Chris Lattner4b009652007-07-25 00:24:17 +0000304 assert(0 && "Unimplemented type sizes!");
Chris Lattnereb56d292007-08-27 17:38:00 +0000305 }
Chris Lattner4b009652007-07-25 00:24:17 +0000306 break;
307 }
308
309 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000310 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000311}
312
Devang Patel7a78e432007-11-01 19:11:01 +0000313/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000314/// specified record (struct/union/class), which indicates its size and field
315/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000316const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000317 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
318
319 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000320 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000321 if (Entry) return *Entry;
322
Devang Patel7a78e432007-11-01 19:11:01 +0000323 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
324 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
325 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000326 Entry = NewEntry;
327
328 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
329 uint64_t RecordSize = 0;
330 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
331
332 if (D->getKind() != Decl::Union) {
Anders Carlsson7dce0292008-02-16 19:51:27 +0000333 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
334 RecordAlign = std::max(RecordAlign, AA->getAlignment());
335
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000336 bool StructIsPacked = D->getAttr<PackedAttr>();
337
Chris Lattner4b009652007-07-25 00:24:17 +0000338 // Layout each field, for now, just sequentially, respecting alignment. In
339 // the future, this will need to be tweakable by targets.
340 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
341 const FieldDecl *FD = D->getMember(i);
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000342 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
Eli Friedman67571ac2008-02-06 05:33:51 +0000343 uint64_t FieldSize;
344 unsigned FieldAlign;
Anders Carlsson058237f2008-02-18 07:13:09 +0000345
346 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
347 llvm::APSInt I(32);
348 bool BitWidthIsICE =
349 BitWidthExpr->isIntegerConstantExpr(I, *this);
350 assert (BitWidthIsICE && "Invalid BitField size expression");
351 FieldSize = I.getZExtValue();
352
Chris Lattner8cd0e932008-03-05 18:54:05 +0000353 std::pair<uint64_t, unsigned> TypeInfo = getTypeInfo(FD->getType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000354 uint64_t TypeSize = TypeInfo.first;
Anders Carlsson7dce0292008-02-16 19:51:27 +0000355
356 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
357 FieldAlign = AA->getAlignment();
358 else if (FieldIsPacked)
359 FieldAlign = 8;
360 else {
Anders Carlsson058237f2008-02-18 07:13:09 +0000361 // FIXME: This is X86 specific, use 32-bit alignment for long long.
362 if (FD->getType()->isIntegerType() && TypeInfo.second > 32)
363 FieldAlign = 32;
364 else
365 FieldAlign = TypeInfo.second;
Anders Carlsson7dce0292008-02-16 19:51:27 +0000366 }
Eli Friedman67571ac2008-02-06 05:33:51 +0000367
Anders Carlsson058237f2008-02-18 07:13:09 +0000368 // Check if we need to add padding to give the field the correct
369 // alignment.
370 if (RecordSize % FieldAlign + FieldSize > TypeSize)
371 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
372
373 } else {
374 if (FD->getType()->isIncompleteType()) {
375 // This must be a flexible array member; we can't directly
376 // query getTypeInfo about these, so we figure it out here.
377 // Flexible array members don't have any size, but they
378 // have to be aligned appropriately for their element type.
379
380 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
381 FieldAlign = AA->getAlignment();
382 else if (FieldIsPacked)
383 FieldAlign = 8;
384 else {
385 const ArrayType* ATy = FD->getType()->getAsArrayType();
Chris Lattner8cd0e932008-03-05 18:54:05 +0000386 FieldAlign = getTypeAlign(ATy->getElementType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000387 }
388 FieldSize = 0;
389 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000390 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000391 FieldSize = FieldInfo.first;
392
393 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
394 FieldAlign = AA->getAlignment();
395 else if (FieldIsPacked)
396 FieldAlign = 8;
397 else
398 FieldAlign = FieldInfo.second;
399 }
400
401 // Round up the current record size to the field's alignment boundary.
402 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
403 }
Chris Lattner4b009652007-07-25 00:24:17 +0000404
405 // Place this field at the current location.
406 FieldOffsets[i] = RecordSize;
407
408 // Reserve space for this field.
409 RecordSize += FieldSize;
410
411 // Remember max struct/class alignment.
412 RecordAlign = std::max(RecordAlign, FieldAlign);
413 }
414
415 // Finally, round the size of the total struct up to the alignment of the
416 // struct itself.
417 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
418 } else {
419 // Union layout just puts each member at the start of the record.
420 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
421 const FieldDecl *FD = D->getMember(i);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000422 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Chris Lattner4b009652007-07-25 00:24:17 +0000423 uint64_t FieldSize = FieldInfo.first;
424 unsigned FieldAlign = FieldInfo.second;
425
Anders Carlsson058237f2008-02-18 07:13:09 +0000426 // FIXME: This is X86 specific, use 32-bit alignment for long long.
427 if (FD->getType()->isIntegerType() && FieldAlign > 32)
428 FieldAlign = 32;
429
Chris Lattner4b009652007-07-25 00:24:17 +0000430 // Round up the current record size to the field's alignment boundary.
431 RecordSize = std::max(RecordSize, FieldSize);
432
433 // Place this field at the start of the record.
434 FieldOffsets[i] = 0;
435
436 // Remember max struct/class alignment.
437 RecordAlign = std::max(RecordAlign, FieldAlign);
438 }
439 }
440
441 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
442 return *NewEntry;
443}
444
Chris Lattner4b009652007-07-25 00:24:17 +0000445//===----------------------------------------------------------------------===//
446// Type creation/memoization methods
447//===----------------------------------------------------------------------===//
448
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000449QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattner35fef522008-02-20 20:55:12 +0000450 if (T.getCanonicalType().getAddressSpace() == AddressSpace)
451 return T;
452
453 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
454 // with CVR qualifiers from here on out.
455 assert(T.getCanonicalType().getAddressSpace() == 0 &&
456 "Type is already address space qualified");
457
458 // Check if we've already instantiated an address space qual'd type of this
459 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000460 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000461 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000462 void *InsertPos = 0;
463 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
464 return QualType(ASQy, 0);
465
466 // If the base type isn't canonical, this won't be a canonical type either,
467 // so fill in the canonical type field.
468 QualType Canonical;
469 if (!T->isCanonical()) {
470 Canonical = getASQualType(T.getCanonicalType(), AddressSpace);
471
472 // Get the new insert position for the node we care about.
473 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
474 assert(NewIP == 0 && "Shouldn't be in the map!");
475 }
Chris Lattner35fef522008-02-20 20:55:12 +0000476 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000477 ASQualTypes.InsertNode(New, InsertPos);
478 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000479 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000480}
481
Chris Lattner4b009652007-07-25 00:24:17 +0000482
483/// getComplexType - Return the uniqued reference to the type for a complex
484/// number with the specified element type.
485QualType ASTContext::getComplexType(QualType T) {
486 // Unique pointers, to guarantee there is only one pointer of a particular
487 // structure.
488 llvm::FoldingSetNodeID ID;
489 ComplexType::Profile(ID, T);
490
491 void *InsertPos = 0;
492 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
493 return QualType(CT, 0);
494
495 // If the pointee type isn't canonical, this won't be a canonical type either,
496 // so fill in the canonical type field.
497 QualType Canonical;
498 if (!T->isCanonical()) {
499 Canonical = getComplexType(T.getCanonicalType());
500
501 // Get the new insert position for the node we care about.
502 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
503 assert(NewIP == 0 && "Shouldn't be in the map!");
504 }
505 ComplexType *New = new ComplexType(T, Canonical);
506 Types.push_back(New);
507 ComplexTypes.InsertNode(New, InsertPos);
508 return QualType(New, 0);
509}
510
511
512/// getPointerType - Return the uniqued reference to the type for a pointer to
513/// the specified type.
514QualType ASTContext::getPointerType(QualType T) {
515 // Unique pointers, to guarantee there is only one pointer of a particular
516 // structure.
517 llvm::FoldingSetNodeID ID;
518 PointerType::Profile(ID, T);
519
520 void *InsertPos = 0;
521 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
522 return QualType(PT, 0);
523
524 // If the pointee type isn't canonical, this won't be a canonical type either,
525 // so fill in the canonical type field.
526 QualType Canonical;
527 if (!T->isCanonical()) {
528 Canonical = getPointerType(T.getCanonicalType());
529
530 // Get the new insert position for the node we care about.
531 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
532 assert(NewIP == 0 && "Shouldn't be in the map!");
533 }
534 PointerType *New = new PointerType(T, Canonical);
535 Types.push_back(New);
536 PointerTypes.InsertNode(New, InsertPos);
537 return QualType(New, 0);
538}
539
540/// getReferenceType - Return the uniqued reference to the type for a reference
541/// to the specified type.
542QualType ASTContext::getReferenceType(QualType T) {
543 // Unique pointers, to guarantee there is only one pointer of a particular
544 // structure.
545 llvm::FoldingSetNodeID ID;
546 ReferenceType::Profile(ID, T);
547
548 void *InsertPos = 0;
549 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
550 return QualType(RT, 0);
551
552 // If the referencee type isn't canonical, this won't be a canonical type
553 // either, so fill in the canonical type field.
554 QualType Canonical;
555 if (!T->isCanonical()) {
556 Canonical = getReferenceType(T.getCanonicalType());
557
558 // Get the new insert position for the node we care about.
559 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
560 assert(NewIP == 0 && "Shouldn't be in the map!");
561 }
562
563 ReferenceType *New = new ReferenceType(T, Canonical);
564 Types.push_back(New);
565 ReferenceTypes.InsertNode(New, InsertPos);
566 return QualType(New, 0);
567}
568
Steve Naroff83c13012007-08-30 01:06:46 +0000569/// getConstantArrayType - Return the unique reference to the type for an
570/// array of the specified element type.
571QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000572 const llvm::APInt &ArySize,
573 ArrayType::ArraySizeModifier ASM,
574 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000575 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000576 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000577
578 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000579 if (ConstantArrayType *ATP =
580 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000581 return QualType(ATP, 0);
582
583 // If the element type isn't canonical, this won't be a canonical type either,
584 // so fill in the canonical type field.
585 QualType Canonical;
586 if (!EltTy->isCanonical()) {
Steve Naroff24c9b982007-08-30 18:10:14 +0000587 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
588 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000589 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000590 ConstantArrayType *NewIP =
591 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
592
Chris Lattner4b009652007-07-25 00:24:17 +0000593 assert(NewIP == 0 && "Shouldn't be in the map!");
594 }
595
Steve Naroff24c9b982007-08-30 18:10:14 +0000596 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
597 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000598 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000599 Types.push_back(New);
600 return QualType(New, 0);
601}
602
Steve Naroffe2579e32007-08-30 18:14:25 +0000603/// getVariableArrayType - Returns a non-unique reference to the type for a
604/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000605QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
606 ArrayType::ArraySizeModifier ASM,
607 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000608 // Since we don't unique expressions, it isn't possible to unique VLA's
609 // that have an expression provided for their size.
610
611 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
612 ASM, EltTypeQuals);
613
614 VariableArrayTypes.push_back(New);
615 Types.push_back(New);
616 return QualType(New, 0);
617}
618
619QualType ASTContext::getIncompleteArrayType(QualType EltTy,
620 ArrayType::ArraySizeModifier ASM,
621 unsigned EltTypeQuals) {
622 llvm::FoldingSetNodeID ID;
623 IncompleteArrayType::Profile(ID, EltTy);
624
625 void *InsertPos = 0;
626 if (IncompleteArrayType *ATP =
627 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
628 return QualType(ATP, 0);
629
630 // If the element type isn't canonical, this won't be a canonical type
631 // either, so fill in the canonical type field.
632 QualType Canonical;
633
634 if (!EltTy->isCanonical()) {
635 Canonical = getIncompleteArrayType(EltTy.getCanonicalType(),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000636 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000637
638 // Get the new insert position for the node we care about.
639 IncompleteArrayType *NewIP =
640 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
641
642 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000643 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000644
645 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
646 ASM, EltTypeQuals);
647
648 IncompleteArrayTypes.InsertNode(New, InsertPos);
649 Types.push_back(New);
650 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000651}
652
Chris Lattner4b009652007-07-25 00:24:17 +0000653/// getVectorType - Return the unique reference to a vector type of
654/// the specified element type and size. VectorType must be a built-in type.
655QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
656 BuiltinType *baseType;
657
658 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
659 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
660
661 // Check if we've already instantiated a vector of this type.
662 llvm::FoldingSetNodeID ID;
663 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
664 void *InsertPos = 0;
665 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
666 return QualType(VTP, 0);
667
668 // If the element type isn't canonical, this won't be a canonical type either,
669 // so fill in the canonical type field.
670 QualType Canonical;
671 if (!vecType->isCanonical()) {
672 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
673
674 // Get the new insert position for the node we care about.
675 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
676 assert(NewIP == 0 && "Shouldn't be in the map!");
677 }
678 VectorType *New = new VectorType(vecType, NumElts, Canonical);
679 VectorTypes.InsertNode(New, InsertPos);
680 Types.push_back(New);
681 return QualType(New, 0);
682}
683
684/// getOCUVectorType - Return the unique reference to an OCU vector type of
685/// the specified element type and size. VectorType must be a built-in type.
686QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
687 BuiltinType *baseType;
688
689 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
690 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
691
692 // Check if we've already instantiated a vector of this type.
693 llvm::FoldingSetNodeID ID;
694 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
695 void *InsertPos = 0;
696 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
697 return QualType(VTP, 0);
698
699 // If the element type isn't canonical, this won't be a canonical type either,
700 // so fill in the canonical type field.
701 QualType Canonical;
702 if (!vecType->isCanonical()) {
703 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
704
705 // Get the new insert position for the node we care about.
706 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
707 assert(NewIP == 0 && "Shouldn't be in the map!");
708 }
709 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
710 VectorTypes.InsertNode(New, InsertPos);
711 Types.push_back(New);
712 return QualType(New, 0);
713}
714
715/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
716///
717QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
718 // Unique functions, to guarantee there is only one function of a particular
719 // structure.
720 llvm::FoldingSetNodeID ID;
721 FunctionTypeNoProto::Profile(ID, ResultTy);
722
723 void *InsertPos = 0;
724 if (FunctionTypeNoProto *FT =
725 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
726 return QualType(FT, 0);
727
728 QualType Canonical;
729 if (!ResultTy->isCanonical()) {
730 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
731
732 // Get the new insert position for the node we care about.
733 FunctionTypeNoProto *NewIP =
734 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
735 assert(NewIP == 0 && "Shouldn't be in the map!");
736 }
737
738 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
739 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000740 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000741 return QualType(New, 0);
742}
743
744/// getFunctionType - Return a normal function type with a typed argument
745/// list. isVariadic indicates whether the argument list includes '...'.
746QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
747 unsigned NumArgs, bool isVariadic) {
748 // Unique functions, to guarantee there is only one function of a particular
749 // structure.
750 llvm::FoldingSetNodeID ID;
751 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
752
753 void *InsertPos = 0;
754 if (FunctionTypeProto *FTP =
755 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
756 return QualType(FTP, 0);
757
758 // Determine whether the type being created is already canonical or not.
759 bool isCanonical = ResultTy->isCanonical();
760 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
761 if (!ArgArray[i]->isCanonical())
762 isCanonical = false;
763
764 // If this type isn't canonical, get the canonical version of it.
765 QualType Canonical;
766 if (!isCanonical) {
767 llvm::SmallVector<QualType, 16> CanonicalArgs;
768 CanonicalArgs.reserve(NumArgs);
769 for (unsigned i = 0; i != NumArgs; ++i)
770 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
771
772 Canonical = getFunctionType(ResultTy.getCanonicalType(),
773 &CanonicalArgs[0], NumArgs,
774 isVariadic);
775
776 // Get the new insert position for the node we care about.
777 FunctionTypeProto *NewIP =
778 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
779 assert(NewIP == 0 && "Shouldn't be in the map!");
780 }
781
782 // FunctionTypeProto objects are not allocated with new because they have a
783 // variable size array (for parameter types) at the end of them.
784 FunctionTypeProto *FTP =
785 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
786 NumArgs*sizeof(QualType));
787 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
788 Canonical);
789 Types.push_back(FTP);
790 FunctionTypeProtos.InsertNode(FTP, InsertPos);
791 return QualType(FTP, 0);
792}
793
794/// getTypedefType - Return the unique reference to the type for the
795/// specified typename decl.
796QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
797 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
798
799 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000800 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000801 Types.push_back(Decl->TypeForDecl);
802 return QualType(Decl->TypeForDecl, 0);
803}
804
Ted Kremenek42730c52008-01-07 19:49:32 +0000805/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000806/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000807QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000808 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
809
Ted Kremenek42730c52008-01-07 19:49:32 +0000810 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000811 Types.push_back(Decl->TypeForDecl);
812 return QualType(Decl->TypeForDecl, 0);
813}
814
Ted Kremenek42730c52008-01-07 19:49:32 +0000815/// getObjCQualifiedInterfaceType - Return a
816/// ObjCQualifiedInterfaceType type for the given interface decl and
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000817/// the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000818QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
819 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000820 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000821 ObjCQualifiedInterfaceType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000822
823 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000824 if (ObjCQualifiedInterfaceType *QT =
825 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000826 return QualType(QT, 0);
827
828 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +0000829 ObjCQualifiedInterfaceType *QType =
830 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000831 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000832 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000833 return QualType(QType, 0);
834}
835
Ted Kremenek42730c52008-01-07 19:49:32 +0000836/// getObjCQualifiedIdType - Return a
837/// getObjCQualifiedIdType type for the 'id' decl and
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000838/// the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000839QualType ASTContext::getObjCQualifiedIdType(QualType idType,
840 ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000841 unsigned NumProtocols) {
842 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000843 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000844
845 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000846 if (ObjCQualifiedIdType *QT =
847 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000848 return QualType(QT, 0);
849
850 // No Match;
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000851 QualType Canonical;
852 if (!idType->isCanonical()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000853 Canonical = getObjCQualifiedIdType(idType.getCanonicalType(),
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000854 Protocols, NumProtocols);
Ted Kremenek42730c52008-01-07 19:49:32 +0000855 ObjCQualifiedIdType *NewQT =
856 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000857 assert(NewQT == 0 && "Shouldn't be in the map!");
858 }
859
Ted Kremenek42730c52008-01-07 19:49:32 +0000860 ObjCQualifiedIdType *QType =
861 new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000862 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000863 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000864 return QualType(QType, 0);
865}
866
Steve Naroff0604dd92007-08-01 18:02:17 +0000867/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
868/// TypeOfExpr AST's (since expression's are never shared). For example,
869/// multiple declarations that refer to "typeof(x)" all contain different
870/// DeclRefExpr's. This doesn't effect the type checker, since it operates
871/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000872QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroff7cbb1462007-07-31 12:34:36 +0000873 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000874 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
875 Types.push_back(toe);
876 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000877}
878
Steve Naroff0604dd92007-08-01 18:02:17 +0000879/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
880/// TypeOfType AST's. The only motivation to unique these nodes would be
881/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
882/// an issue. This doesn't effect the type checker, since it operates
883/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000884QualType ASTContext::getTypeOfType(QualType tofType) {
885 QualType Canonical = tofType.getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000886 TypeOfType *tot = new TypeOfType(tofType, Canonical);
887 Types.push_back(tot);
888 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000889}
890
Chris Lattner4b009652007-07-25 00:24:17 +0000891/// getTagDeclType - Return the unique reference to the type for the
892/// specified TagDecl (struct/union/class/enum) decl.
893QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +0000894 assert (Decl);
895
Ted Kremenekf05026d2007-11-14 00:03:20 +0000896 // The decl stores the type cache.
Ted Kremenekae8fa032007-11-26 21:16:01 +0000897 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Ted Kremenekf05026d2007-11-14 00:03:20 +0000898
899 TagType* T = new TagType(Decl, QualType());
Ted Kremenekae8fa032007-11-26 21:16:01 +0000900 Types.push_back(T);
901 Decl->TypeForDecl = T;
Ted Kremenekf05026d2007-11-14 00:03:20 +0000902
903 return QualType(T, 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000904}
905
906/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
907/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
908/// needs to agree with the definition in <stddef.h>.
909QualType ASTContext::getSizeType() const {
910 // On Darwin, size_t is defined as a "long unsigned int".
911 // FIXME: should derive from "Target".
912 return UnsignedLongTy;
913}
914
Eli Friedmanfdd35d72008-02-12 08:29:21 +0000915/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
916/// width of characters in wide strings, The value is target dependent and
917/// needs to agree with the definition in <stddef.h>.
918QualType ASTContext::getWcharType() const {
919 // On Darwin, wchar_t is defined as a "int".
920 // FIXME: should derive from "Target".
921 return IntTy;
922}
923
Chris Lattner4b009652007-07-25 00:24:17 +0000924/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
925/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
926QualType ASTContext::getPointerDiffType() const {
927 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
928 // FIXME: should derive from "Target".
929 return IntTy;
930}
931
Chris Lattner19eb97e2008-04-02 05:18:44 +0000932//===----------------------------------------------------------------------===//
933// Type Operators
934//===----------------------------------------------------------------------===//
935
936/// getArrayDecayedType - Return the properly qualified result of decaying the
937/// specified array type to a pointer. This operation is non-trivial when
938/// handling typedefs etc. The canonical type of "T" must be an array type,
939/// this returns a pointer to a properly qualified element of the array.
940///
941/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
942QualType ASTContext::getArrayDecayedType(QualType Ty) {
943 // Handle the common case where typedefs are not involved directly.
944 QualType EltTy;
945 unsigned ArrayQuals = 0;
946 unsigned PointerQuals = 0;
947 if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
948 // Since T "isa" an array type, it could not have had an address space
949 // qualifier, just CVR qualifiers. The properly qualified element pointer
950 // gets the union of the CVR qualifiers from the element and the array, and
951 // keeps any address space qualifier on the element type if present.
952 EltTy = AT->getElementType();
953 ArrayQuals = Ty.getCVRQualifiers();
954 PointerQuals = AT->getIndexTypeQualifier();
955 } else {
956 // Otherwise, we have an ASQualType or a typedef, etc. Make sure we don't
957 // lose qualifiers when dealing with typedefs. Example:
958 // typedef int arr[10];
959 // void test2() {
960 // const arr b;
961 // b[4] = 1;
962 // }
963 //
964 // The decayed type of b is "const int*" even though the element type of the
965 // array is "int".
966 QualType CanTy = Ty.getCanonicalType();
967 const ArrayType *PrettyArrayType = Ty->getAsArrayType();
968 assert(PrettyArrayType && "Not an array type!");
969
970 // Get the element type with 'getAsArrayType' so that we don't lose any
971 // typedefs in the element type of the array.
972 EltTy = PrettyArrayType->getElementType();
973
974 // If the array was address-space qualifier, make sure to ASQual the element
975 // type. We can just grab the address space from the canonical type.
976 if (unsigned AS = CanTy.getAddressSpace())
977 EltTy = getASQualType(EltTy, AS);
978
979 // To properly handle [multiple levels of] typedefs, typeof's etc, we take
980 // the CVR qualifiers directly from the canonical type, which is guaranteed
981 // to have the full set unioned together.
982 ArrayQuals = CanTy.getCVRQualifiers();
983 PointerQuals = PrettyArrayType->getIndexTypeQualifier();
984 }
985
Chris Lattnerda79b3f2008-04-02 06:06:35 +0000986 // Apply any CVR qualifiers from the array type to the element type. This
987 // implements C99 6.7.3p8: "If the specification of an array type includes
988 // any type qualifiers, the element type is so qualified, not the array type."
Chris Lattner19eb97e2008-04-02 05:18:44 +0000989 EltTy = EltTy.getQualifiedType(ArrayQuals | EltTy.getCVRQualifiers());
990
991 QualType PtrTy = getPointerType(EltTy);
992
993 // int x[restrict 4] -> int *restrict
994 PtrTy = PtrTy.getQualifiedType(PointerQuals);
995
996 return PtrTy;
997}
998
Chris Lattner4b009652007-07-25 00:24:17 +0000999/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1000/// routine will assert if passed a built-in type that isn't an integer or enum.
1001static int getIntegerRank(QualType t) {
1002 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
1003 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
1004 return 4;
1005 }
1006
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001007 const BuiltinType *BT = t.getCanonicalType()->getAsBuiltinType();
Chris Lattner4b009652007-07-25 00:24:17 +00001008 switch (BT->getKind()) {
1009 default:
1010 assert(0 && "getIntegerRank(): not a built-in integer");
1011 case BuiltinType::Bool:
1012 return 1;
1013 case BuiltinType::Char_S:
1014 case BuiltinType::Char_U:
1015 case BuiltinType::SChar:
1016 case BuiltinType::UChar:
1017 return 2;
1018 case BuiltinType::Short:
1019 case BuiltinType::UShort:
1020 return 3;
1021 case BuiltinType::Int:
1022 case BuiltinType::UInt:
1023 return 4;
1024 case BuiltinType::Long:
1025 case BuiltinType::ULong:
1026 return 5;
1027 case BuiltinType::LongLong:
1028 case BuiltinType::ULongLong:
1029 return 6;
1030 }
1031}
1032
1033/// getFloatingRank - Return a relative rank for floating point types.
1034/// This routine will assert if passed a built-in type that isn't a float.
1035static int getFloatingRank(QualType T) {
1036 T = T.getCanonicalType();
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001037 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001038 return getFloatingRank(CT->getElementType());
1039
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001040 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattner5003e8b2007-11-01 05:03:41 +00001041 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001042 case BuiltinType::Float: return FloatRank;
1043 case BuiltinType::Double: return DoubleRank;
1044 case BuiltinType::LongDouble: return LongDoubleRank;
1045 }
1046}
1047
Steve Narofffa0c4532007-08-27 01:41:48 +00001048/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1049/// point or a complex type (based on typeDomain/typeSize).
1050/// 'typeDomain' is a real floating point or complex type.
1051/// 'typeSize' is a real floating point or complex type.
Steve Naroff3cf497f2007-08-27 01:27:54 +00001052QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
1053 QualType typeSize, QualType typeDomain) const {
1054 if (typeDomain->isComplexType()) {
1055 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001056 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001057 case FloatRank: return FloatComplexTy;
1058 case DoubleRank: return DoubleComplexTy;
1059 case LongDoubleRank: return LongDoubleComplexTy;
1060 }
Chris Lattner4b009652007-07-25 00:24:17 +00001061 }
Steve Naroff3cf497f2007-08-27 01:27:54 +00001062 if (typeDomain->isRealFloatingType()) {
1063 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001064 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001065 case FloatRank: return FloatTy;
1066 case DoubleRank: return DoubleTy;
1067 case LongDoubleRank: return LongDoubleTy;
1068 }
1069 }
1070 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattner1d2b4612007-09-16 19:23:47 +00001071 //an invalid return value, but the assert
1072 //will ensure that this code is never reached.
1073 return VoidTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001074}
1075
Steve Naroff45fc9822007-08-27 15:30:22 +00001076/// compareFloatingType - Handles 3 different combos:
1077/// float/float, float/complex, complex/complex.
1078/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
1079int ASTContext::compareFloatingType(QualType lt, QualType rt) {
1080 if (getFloatingRank(lt) == getFloatingRank(rt))
1081 return 0;
1082 if (getFloatingRank(lt) > getFloatingRank(rt))
1083 return 1;
1084 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001085}
1086
1087// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
1088// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
1089QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
1090 if (lhs == rhs) return lhs;
1091
1092 bool t1Unsigned = lhs->isUnsignedIntegerType();
1093 bool t2Unsigned = rhs->isUnsignedIntegerType();
1094
1095 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
1096 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
1097
1098 // We have two integer types with differing signs
1099 QualType unsignedType = t1Unsigned ? lhs : rhs;
1100 QualType signedType = t1Unsigned ? rhs : lhs;
1101
1102 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
1103 return unsignedType;
1104 else {
1105 // FIXME: Need to check if the signed type can represent all values of the
1106 // unsigned type. If it can, then the result is the signed type.
1107 // If it can't, then the result is the unsigned version of the signed type.
1108 // Should probably add a helper that returns a signed integer type from
1109 // an unsigned (and vice versa). C99 6.3.1.8.
1110 return signedType;
1111 }
1112}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001113
1114// getCFConstantStringType - Return the type used for constant CFStrings.
1115QualType ASTContext::getCFConstantStringType() {
1116 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001117 CFConstantStringTypeDecl =
Chris Lattner58114f02008-03-15 21:32:50 +00001118 RecordDecl::Create(*this, Decl::Struct, SourceLocation(),
1119 &Idents.get("NSConstantString"), 0);
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001120 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001121
1122 // const int *isa;
1123 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001124 // int flags;
1125 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001126 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001127 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001128 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001129 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001130 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001131 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001132
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001133 for (unsigned i = 0; i < 4; ++i)
Chris Lattner81db64a2008-03-16 00:16:02 +00001134 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
1135 FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001136
1137 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1138 }
1139
1140 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001141}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001142
Anders Carlssone3f02572007-10-29 06:33:42 +00001143// This returns true if a type has been typedefed to BOOL:
1144// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001145static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001146 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001147 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001148
1149 return false;
1150}
1151
Ted Kremenek42730c52008-01-07 19:49:32 +00001152/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001153/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001154int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001155 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001156
1157 // Make all integer and enum types at least as large as an int
1158 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001159 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001160 // Treat arrays as pointers, since that's how they're passed in.
1161 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001162 sz = getTypeSize(VoidPtrTy);
1163 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001164}
1165
Ted Kremenek42730c52008-01-07 19:49:32 +00001166/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001167/// declaration.
Ted Kremenek42730c52008-01-07 19:49:32 +00001168void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001169 std::string& S)
1170{
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001171 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001172 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001173 // Encode result type.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001174 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001175 // Compute size of all parameters.
1176 // Start with computing size of a pointer in number of bytes.
1177 // FIXME: There might(should) be a better way of doing this computation!
1178 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001179 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001180 // The first two arguments (self and _cmd) are pointers; account for
1181 // their size.
1182 int ParmOffset = 2 * PtrSize;
1183 int NumOfParams = Decl->getNumParams();
1184 for (int i = 0; i < NumOfParams; i++) {
1185 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001186 int sz = getObjCEncodingTypeSize (PType);
1187 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001188 ParmOffset += sz;
1189 }
1190 S += llvm::utostr(ParmOffset);
1191 S += "@0:";
1192 S += llvm::utostr(PtrSize);
1193
1194 // Argument types.
1195 ParmOffset = 2 * PtrSize;
1196 for (int i = 0; i < NumOfParams; i++) {
1197 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001198 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001199 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001200 getObjCEncodingForTypeQualifier(
1201 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian248db262008-01-22 22:44:46 +00001202 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001203 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001204 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001205 }
1206}
1207
Fariborz Jahanian248db262008-01-22 22:44:46 +00001208void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1209 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson36f07d82007-10-29 05:01:08 +00001210{
Anders Carlssone3f02572007-10-29 06:33:42 +00001211 // FIXME: This currently doesn't encode:
1212 // @ An object (whether statically typed or typed id)
1213 // # A class object (Class)
1214 // : A method selector (SEL)
1215 // {name=type...} A structure
1216 // (name=type...) A union
1217 // bnum A bit field of num bits
1218
1219 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001220 char encoding;
1221 switch (BT->getKind()) {
1222 case BuiltinType::Void:
1223 encoding = 'v';
1224 break;
1225 case BuiltinType::Bool:
1226 encoding = 'B';
1227 break;
1228 case BuiltinType::Char_U:
1229 case BuiltinType::UChar:
1230 encoding = 'C';
1231 break;
1232 case BuiltinType::UShort:
1233 encoding = 'S';
1234 break;
1235 case BuiltinType::UInt:
1236 encoding = 'I';
1237 break;
1238 case BuiltinType::ULong:
1239 encoding = 'L';
1240 break;
1241 case BuiltinType::ULongLong:
1242 encoding = 'Q';
1243 break;
1244 case BuiltinType::Char_S:
1245 case BuiltinType::SChar:
1246 encoding = 'c';
1247 break;
1248 case BuiltinType::Short:
1249 encoding = 's';
1250 break;
1251 case BuiltinType::Int:
1252 encoding = 'i';
1253 break;
1254 case BuiltinType::Long:
1255 encoding = 'l';
1256 break;
1257 case BuiltinType::LongLong:
1258 encoding = 'q';
1259 break;
1260 case BuiltinType::Float:
1261 encoding = 'f';
1262 break;
1263 case BuiltinType::Double:
1264 encoding = 'd';
1265 break;
1266 case BuiltinType::LongDouble:
1267 encoding = 'd';
1268 break;
1269 default:
1270 assert(0 && "Unhandled builtin type kind");
1271 }
1272
1273 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001274 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001275 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001276 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001277 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001278
1279 }
1280 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001281 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001282 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001283 S += '@';
1284 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001285 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001286 S += '#';
1287 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001288 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001289 S += ':';
1290 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001291 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001292
1293 if (PointeeTy->isCharType()) {
1294 // char pointer types should be encoded as '*' unless it is a
1295 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001296 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001297 S += '*';
1298 return;
1299 }
1300 }
1301
1302 S += '^';
Fariborz Jahanian248db262008-01-22 22:44:46 +00001303 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone3f02572007-10-29 06:33:42 +00001304 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001305 S += '[';
1306
1307 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1308 S += llvm::utostr(CAT->getSize().getZExtValue());
1309 else
1310 assert(0 && "Unhandled array type!");
1311
Fariborz Jahanian248db262008-01-22 22:44:46 +00001312 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001313 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001314 } else if (T->getAsFunctionType()) {
1315 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001316 } else if (const RecordType *RTy = T->getAsRecordType()) {
1317 RecordDecl *RDecl= RTy->getDecl();
1318 S += '{';
1319 S += RDecl->getName();
Fariborz Jahanian248db262008-01-22 22:44:46 +00001320 bool found = false;
1321 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1322 if (ERType[i] == RTy) {
1323 found = true;
1324 break;
1325 }
1326 if (!found) {
1327 ERType.push_back(RTy);
1328 S += '=';
1329 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1330 FieldDecl *field = RDecl->getMember(i);
1331 getObjCEncodingForType(field->getType(), S, ERType);
1332 }
1333 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1334 ERType.pop_back();
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001335 }
1336 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001337 } else if (T->isEnumeralType()) {
1338 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001339 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001340 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001341}
1342
Ted Kremenek42730c52008-01-07 19:49:32 +00001343void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001344 std::string& S) const {
1345 if (QT & Decl::OBJC_TQ_In)
1346 S += 'n';
1347 if (QT & Decl::OBJC_TQ_Inout)
1348 S += 'N';
1349 if (QT & Decl::OBJC_TQ_Out)
1350 S += 'o';
1351 if (QT & Decl::OBJC_TQ_Bycopy)
1352 S += 'O';
1353 if (QT & Decl::OBJC_TQ_Byref)
1354 S += 'R';
1355 if (QT & Decl::OBJC_TQ_Oneway)
1356 S += 'V';
1357}
1358
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001359void ASTContext::setBuiltinVaListType(QualType T)
1360{
1361 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1362
1363 BuiltinVaListType = T;
1364}
1365
Ted Kremenek42730c52008-01-07 19:49:32 +00001366void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001367{
Ted Kremenek42730c52008-01-07 19:49:32 +00001368 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff9d12c902007-10-15 14:41:52 +00001369
Ted Kremenek42730c52008-01-07 19:49:32 +00001370 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001371
1372 // typedef struct objc_object *id;
1373 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1374 assert(ptr && "'id' incorrectly typed");
1375 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1376 assert(rec && "'id' incorrectly typed");
1377 IdStructType = rec;
1378}
1379
Ted Kremenek42730c52008-01-07 19:49:32 +00001380void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001381{
Ted Kremenek42730c52008-01-07 19:49:32 +00001382 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001383
Ted Kremenek42730c52008-01-07 19:49:32 +00001384 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001385
1386 // typedef struct objc_selector *SEL;
1387 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1388 assert(ptr && "'SEL' incorrectly typed");
1389 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1390 assert(rec && "'SEL' incorrectly typed");
1391 SelStructType = rec;
1392}
1393
Ted Kremenek42730c52008-01-07 19:49:32 +00001394void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001395{
Ted Kremenek42730c52008-01-07 19:49:32 +00001396 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1397 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001398}
1399
Ted Kremenek42730c52008-01-07 19:49:32 +00001400void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001401{
Ted Kremenek42730c52008-01-07 19:49:32 +00001402 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001403
Ted Kremenek42730c52008-01-07 19:49:32 +00001404 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001405
1406 // typedef struct objc_class *Class;
1407 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1408 assert(ptr && "'Class' incorrectly typed");
1409 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1410 assert(rec && "'Class' incorrectly typed");
1411 ClassStructType = rec;
1412}
1413
Ted Kremenek42730c52008-01-07 19:49:32 +00001414void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1415 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001416 "'NSConstantString' type already set!");
1417
Ted Kremenek42730c52008-01-07 19:49:32 +00001418 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001419}
1420
Steve Naroff85f0dc52007-10-15 20:41:53 +00001421bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1422 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1423 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1424
1425 return lBuiltin->getKind() == rBuiltin->getKind();
1426}
1427
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001428/// objcTypesAreCompatible - This routine is called when two types
1429/// are of different class; one is interface type or is
1430/// a qualified interface type and the other type is of a different class.
1431/// Example, II or II<P>.
Steve Naroff85f0dc52007-10-15 20:41:53 +00001432bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001433 if (lhs->isObjCInterfaceType() && isObjCIdType(rhs))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001434 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001435 else if (isObjCIdType(lhs) && rhs->isObjCInterfaceType())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001436 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001437 if (ObjCInterfaceType *lhsIT =
1438 dyn_cast<ObjCInterfaceType>(lhs.getCanonicalType().getTypePtr())) {
1439 ObjCQualifiedInterfaceType *rhsQI =
1440 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001441 return rhsQI && (lhsIT->getDecl() == rhsQI->getDecl());
1442 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001443 else if (ObjCInterfaceType *rhsIT =
1444 dyn_cast<ObjCInterfaceType>(rhs.getCanonicalType().getTypePtr())) {
1445 ObjCQualifiedInterfaceType *lhsQI =
1446 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001447 return lhsQI && (rhsIT->getDecl() == lhsQI->getDecl());
1448 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00001449 return false;
1450}
1451
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001452/// Check that 'lhs' and 'rhs' are compatible interface types. Both types
1453/// must be canonical types.
Steve Naroff85f0dc52007-10-15 20:41:53 +00001454bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001455 assert (lhs->isCanonical() &&
1456 "interfaceTypesAreCompatible strip typedefs of lhs");
1457 assert (rhs->isCanonical() &&
1458 "interfaceTypesAreCompatible strip typedefs of rhs");
Fariborz Jahaniance2de812007-12-20 22:37:58 +00001459 if (lhs == rhs)
1460 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001461 ObjCInterfaceType *lhsIT = cast<ObjCInterfaceType>(lhs.getTypePtr());
1462 ObjCInterfaceType *rhsIT = cast<ObjCInterfaceType>(rhs.getTypePtr());
1463 ObjCInterfaceDecl *rhsIDecl = rhsIT->getDecl();
1464 ObjCInterfaceDecl *lhsIDecl = lhsIT->getDecl();
Fariborz Jahaniance2de812007-12-20 22:37:58 +00001465 // rhs is derived from lhs it is OK; else it is not OK.
1466 while (rhsIDecl != NULL) {
1467 if (rhsIDecl == lhsIDecl)
1468 return true;
1469 rhsIDecl = rhsIDecl->getSuperClass();
1470 }
1471 return false;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001472}
1473
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001474bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
1475 QualType rhs) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001476 ObjCQualifiedInterfaceType *lhsQI =
1477 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001478 assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
Ted Kremenek42730c52008-01-07 19:49:32 +00001479 ObjCQualifiedInterfaceType *rhsQI =
1480 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001481 assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001482 if (!interfaceTypesAreCompatible(
1483 getObjCInterfaceType(lhsQI->getDecl()).getCanonicalType(),
1484 getObjCInterfaceType(rhsQI->getDecl()).getCanonicalType()))
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001485 return false;
1486 /* All protocols in lhs must have a presense in rhs. */
1487 for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
1488 bool match = false;
Ted Kremenek42730c52008-01-07 19:49:32 +00001489 ObjCProtocolDecl *lhsProto = lhsQI->getProtocols(i);
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001490 for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001491 ObjCProtocolDecl *rhsProto = rhsQI->getProtocols(j);
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001492 if (lhsProto == rhsProto) {
1493 match = true;
1494 break;
1495 }
1496 }
1497 if (!match)
1498 return false;
1499 }
1500 return true;
1501}
1502
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001503/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
1504/// inheritance hierarchy of 'rProto'.
Ted Kremenek42730c52008-01-07 19:49:32 +00001505static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
1506 ObjCProtocolDecl *rProto) {
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001507 if (lProto == rProto)
1508 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001509 ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001510 for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
1511 if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
1512 return true;
1513 return false;
1514}
1515
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001516/// ClassImplementsProtocol - Checks that 'lProto' protocol
1517/// has been implemented in IDecl class, its super class or categories (if
1518/// lookupCategory is true).
Ted Kremenek42730c52008-01-07 19:49:32 +00001519static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1520 ObjCInterfaceDecl *IDecl,
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001521 bool lookupCategory) {
1522
1523 // 1st, look up the class.
Ted Kremenek42730c52008-01-07 19:49:32 +00001524 ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols();
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001525 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
1526 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1527 return true;
1528 }
1529
1530 // 2nd, look up the category.
1531 if (lookupCategory)
Ted Kremenek42730c52008-01-07 19:49:32 +00001532 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001533 CDecl = CDecl->getNextClassCategory()) {
1534 protoList = CDecl->getReferencedProtocols();
1535 for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
1536 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1537 return true;
1538 }
1539 }
1540
1541 // 3rd, look up the super class(s)
1542 if (IDecl->getSuperClass())
1543 return
1544 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory);
1545
1546 return false;
1547}
1548
Ted Kremenek42730c52008-01-07 19:49:32 +00001549/// ObjCQualifiedIdTypesAreCompatible - Compares two types, at least
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001550/// one of which is a protocol qualified 'id' type. When 'compare'
1551/// is true it is for comparison; when false, for assignment/initialization.
Ted Kremenek42730c52008-01-07 19:49:32 +00001552bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs,
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001553 QualType rhs,
1554 bool compare) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001555 // match id<P..> with an 'id' type in all cases.
1556 if (const PointerType *PT = lhs->getAsPointerType()) {
1557 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001558 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001559 return true;
1560
1561 }
1562 else if (const PointerType *PT = rhs->getAsPointerType()) {
1563 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001564 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001565 return true;
1566
1567 }
1568
Ted Kremenek42730c52008-01-07 19:49:32 +00001569 ObjCQualifiedInterfaceType *lhsQI = 0;
1570 ObjCQualifiedInterfaceType *rhsQI = 0;
1571 ObjCInterfaceDecl *lhsID = 0;
1572 ObjCInterfaceDecl *rhsID = 0;
1573 ObjCQualifiedIdType *lhsQID = dyn_cast<ObjCQualifiedIdType>(lhs);
1574 ObjCQualifiedIdType *rhsQID = dyn_cast<ObjCQualifiedIdType>(rhs);
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001575
1576 if (lhsQID) {
1577 if (!rhsQID && rhs->getTypeClass() == Type::Pointer) {
1578 QualType rtype =
1579 cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1580 rhsQI =
Ted Kremenek42730c52008-01-07 19:49:32 +00001581 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001582 rtype.getCanonicalType().getTypePtr());
Fariborz Jahanian87829072007-12-20 19:24:10 +00001583 if (!rhsQI) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001584 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanian87829072007-12-20 19:24:10 +00001585 rtype.getCanonicalType().getTypePtr());
1586 if (IT)
1587 rhsID = IT->getDecl();
1588 }
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001589 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001590 if (!rhsQI && !rhsQID && !rhsID)
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001591 return false;
1592
Fariborz Jahaniance5528d2008-01-03 20:01:35 +00001593 unsigned numRhsProtocols = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001594 ObjCProtocolDecl **rhsProtoList = 0;
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001595 if (rhsQI) {
1596 numRhsProtocols = rhsQI->getNumProtocols();
1597 rhsProtoList = rhsQI->getReferencedProtocols();
1598 }
1599 else if (rhsQID) {
1600 numRhsProtocols = rhsQID->getNumProtocols();
1601 rhsProtoList = rhsQID->getReferencedProtocols();
1602 }
1603
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001604 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001605 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001606 bool match = false;
1607
1608 // when comparing an id<P> on lhs with a static type on rhs,
1609 // see if static class implements all of id's protocols, directly or
1610 // through its super class and categories.
1611 if (rhsID) {
1612 if (ClassImplementsProtocol(lhsProto, rhsID, true))
1613 match = true;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001614 }
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001615 else for (unsigned j = 0; j < numRhsProtocols; j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001616 ObjCProtocolDecl *rhsProto = rhsProtoList[j];
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001617 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1618 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001619 match = true;
1620 break;
1621 }
1622 }
1623 if (!match)
1624 return false;
1625 }
1626 }
1627 else if (rhsQID) {
1628 if (!lhsQID && lhs->getTypeClass() == Type::Pointer) {
1629 QualType ltype =
1630 cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1631 lhsQI =
Ted Kremenek42730c52008-01-07 19:49:32 +00001632 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001633 ltype.getCanonicalType().getTypePtr());
Fariborz Jahanian87829072007-12-20 19:24:10 +00001634 if (!lhsQI) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001635 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanian87829072007-12-20 19:24:10 +00001636 ltype.getCanonicalType().getTypePtr());
1637 if (IT)
1638 lhsID = IT->getDecl();
1639 }
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001640 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001641 if (!lhsQI && !lhsQID && !lhsID)
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001642 return false;
Fariborz Jahanian87829072007-12-20 19:24:10 +00001643
Fariborz Jahaniance5528d2008-01-03 20:01:35 +00001644 unsigned numLhsProtocols = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001645 ObjCProtocolDecl **lhsProtoList = 0;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001646 if (lhsQI) {
1647 numLhsProtocols = lhsQI->getNumProtocols();
1648 lhsProtoList = lhsQI->getReferencedProtocols();
1649 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001650 else if (lhsQID) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001651 numLhsProtocols = lhsQID->getNumProtocols();
1652 lhsProtoList = lhsQID->getReferencedProtocols();
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001653 }
1654 bool match = false;
1655 // for static type vs. qualified 'id' type, check that class implements
1656 // one of 'id's protocols.
1657 if (lhsID) {
1658 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001659 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001660 if (ClassImplementsProtocol(rhsProto, lhsID, compare)) {
1661 match = true;
1662 break;
1663 }
1664 }
1665 }
1666 else for (unsigned i =0; i < numLhsProtocols; i++) {
1667 match = false;
Ted Kremenek42730c52008-01-07 19:49:32 +00001668 ObjCProtocolDecl *lhsProto = lhsProtoList[i];
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001669 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001670 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001671 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1672 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001673 match = true;
1674 break;
1675 }
1676 }
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001677 }
1678 if (!match)
1679 return false;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001680 }
1681 return true;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001682}
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001683
Chris Lattner5003e8b2007-11-01 05:03:41 +00001684bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1685 const VectorType *lVector = lhs->getAsVectorType();
1686 const VectorType *rVector = rhs->getAsVectorType();
1687
1688 if ((lVector->getElementType().getCanonicalType() ==
1689 rVector->getElementType().getCanonicalType()) &&
1690 (lVector->getNumElements() == rVector->getNumElements()))
1691 return true;
1692 return false;
1693}
1694
Steve Naroff85f0dc52007-10-15 20:41:53 +00001695// C99 6.2.7p1: If both are complete types, then the following additional
1696// requirements apply...FIXME (handle compatibility across source files).
1697bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
Steve Naroff4a5e2072007-11-07 06:03:51 +00001698 // "Class" and "id" are compatible built-in structure types.
Ted Kremenek42730c52008-01-07 19:49:32 +00001699 if (isObjCIdType(lhs) && isObjCClassType(rhs) ||
1700 isObjCClassType(lhs) && isObjCIdType(rhs))
Steve Naroff4a5e2072007-11-07 06:03:51 +00001701 return true;
Eli Friedmane7fb03a2008-02-15 06:03:44 +00001702
1703 // Within a translation unit a tag type is
1704 // only compatible with itself.
1705 return lhs.getCanonicalType() == rhs.getCanonicalType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001706}
1707
1708bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1709 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1710 // identically qualified and both shall be pointers to compatible types.
Chris Lattner35fef522008-02-20 20:55:12 +00001711 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1712 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001713 return false;
1714
1715 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1716 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1717
1718 return typesAreCompatible(ltype, rtype);
1719}
1720
Bill Wendling6a9d8542007-12-03 07:33:35 +00001721// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroff85f0dc52007-10-15 20:41:53 +00001722// reference to T, the operation assigns to the object of type T denoted by the
1723// reference.
1724bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1725 QualType ltype = lhs;
1726
1727 if (lhs->isReferenceType())
Chris Lattnercfac88d2008-04-02 17:35:06 +00001728 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getPointeeType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001729
1730 QualType rtype = rhs;
1731
1732 if (rhs->isReferenceType())
Chris Lattnercfac88d2008-04-02 17:35:06 +00001733 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getPointeeType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001734
1735 return typesAreCompatible(ltype, rtype);
1736}
1737
1738bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1739 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1740 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1741 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1742 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1743
1744 // first check the return types (common between C99 and K&R).
1745 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1746 return false;
1747
1748 if (lproto && rproto) { // two C99 style function prototypes
1749 unsigned lproto_nargs = lproto->getNumArgs();
1750 unsigned rproto_nargs = rproto->getNumArgs();
1751
1752 if (lproto_nargs != rproto_nargs)
1753 return false;
1754
1755 // both prototypes have the same number of arguments.
1756 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1757 (rproto->isVariadic() && !lproto->isVariadic()))
1758 return false;
1759
1760 // The use of ellipsis agree...now check the argument types.
1761 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001762 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1763 // is taken as having the unqualified version of it's declared type.
Steve Naroffdec17fe2008-01-29 00:15:50 +00001764 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001765 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001766 return false;
1767 return true;
1768 }
1769 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1770 return true;
1771
1772 // we have a mixture of K&R style with C99 prototypes
1773 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1774
1775 if (proto->isVariadic())
1776 return false;
1777
1778 // FIXME: Each parameter type T in the prototype must be compatible with the
1779 // type resulting from applying the usual argument conversions to T.
1780 return true;
1781}
1782
1783bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
Eli Friedman1e7537832008-02-06 04:53:22 +00001784 // Compatible arrays must have compatible element types
1785 QualType ltype = lhs->getAsArrayType()->getElementType();
1786 QualType rtype = rhs->getAsArrayType()->getElementType();
1787
Steve Naroff85f0dc52007-10-15 20:41:53 +00001788 if (!typesAreCompatible(ltype, rtype))
1789 return false;
Eli Friedman1e7537832008-02-06 04:53:22 +00001790
1791 // Compatible arrays must be the same size
1792 if (const ConstantArrayType* LCAT = lhs->getAsConstantArrayType())
1793 if (const ConstantArrayType* RCAT = rhs->getAsConstantArrayType())
1794 return RCAT->getSize() == LCAT->getSize();
1795
Steve Naroff85f0dc52007-10-15 20:41:53 +00001796 return true;
1797}
1798
1799/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1800/// both shall have the identically qualified version of a compatible type.
1801/// C99 6.2.7p1: Two types have compatible types if their types are the
1802/// same. See 6.7.[2,3,5] for additional rules.
1803bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
Chris Lattner35fef522008-02-20 20:55:12 +00001804 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1805 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroff577f9722008-01-29 18:58:14 +00001806 return false;
1807
Steve Naroff85f0dc52007-10-15 20:41:53 +00001808 QualType lcanon = lhs.getCanonicalType();
1809 QualType rcanon = rhs.getCanonicalType();
1810
1811 // If two types are identical, they are are compatible
1812 if (lcanon == rcanon)
1813 return true;
Bill Wendling6a9d8542007-12-03 07:33:35 +00001814
1815 // C++ [expr]: If an expression initially has the type "reference to T", the
1816 // type is adjusted to "T" prior to any further analysis, the expression
1817 // designates the object or function denoted by the reference, and the
1818 // expression is an lvalue.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001819 if (ReferenceType *RT = dyn_cast<ReferenceType>(lcanon))
Chris Lattnercfac88d2008-04-02 17:35:06 +00001820 lcanon = RT->getPointeeType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001821 if (ReferenceType *RT = dyn_cast<ReferenceType>(rcanon))
Chris Lattnercfac88d2008-04-02 17:35:06 +00001822 rcanon = RT->getPointeeType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001823
1824 Type::TypeClass LHSClass = lcanon->getTypeClass();
1825 Type::TypeClass RHSClass = rcanon->getTypeClass();
1826
1827 // We want to consider the two function types to be the same for these
1828 // comparisons, just force one to the other.
1829 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1830 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00001831
1832 // Same as above for arrays
1833 if (LHSClass == Type::VariableArray) LHSClass = Type::ConstantArray;
1834 if (RHSClass == Type::VariableArray) RHSClass = Type::ConstantArray;
Eli Friedman8ff07782008-02-15 18:16:39 +00001835 if (LHSClass == Type::IncompleteArray) LHSClass = Type::ConstantArray;
1836 if (RHSClass == Type::IncompleteArray) RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001837
Steve Naroffc88babe2008-01-09 22:43:08 +00001838 // If the canonical type classes don't match...
Chris Lattnerc38d4522008-01-14 05:45:46 +00001839 if (LHSClass != RHSClass) {
Steve Naroff85f0dc52007-10-15 20:41:53 +00001840 // For Objective-C, it is possible for two types to be compatible
1841 // when their classes don't match (when dealing with "id"). If either type
1842 // is an interface, we defer to objcTypesAreCompatible().
Ted Kremenek42730c52008-01-07 19:49:32 +00001843 if (lcanon->isObjCInterfaceType() || rcanon->isObjCInterfaceType())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001844 return objcTypesAreCompatible(lcanon, rcanon);
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001845
Chris Lattnerc38d4522008-01-14 05:45:46 +00001846 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1847 // a signed integer type, or an unsigned integer type.
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001848 if (lcanon->isEnumeralType() && rcanon->isIntegralType()) {
1849 EnumDecl* EDecl = cast<EnumDecl>(cast<TagType>(lcanon)->getDecl());
1850 return EDecl->getIntegerType() == rcanon;
1851 }
1852 if (rcanon->isEnumeralType() && lcanon->isIntegralType()) {
1853 EnumDecl* EDecl = cast<EnumDecl>(cast<TagType>(rcanon)->getDecl());
1854 return EDecl->getIntegerType() == lcanon;
1855 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001856
Steve Naroff85f0dc52007-10-15 20:41:53 +00001857 return false;
1858 }
Steve Naroffc88babe2008-01-09 22:43:08 +00001859 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001860 switch (LHSClass) {
1861 case Type::FunctionProto: assert(0 && "Canonicalized away above");
1862 case Type::Pointer:
1863 return pointerTypesAreCompatible(lcanon, rcanon);
1864 case Type::ConstantArray:
1865 case Type::VariableArray:
Eli Friedman8ff07782008-02-15 18:16:39 +00001866 case Type::IncompleteArray:
Chris Lattnerc38d4522008-01-14 05:45:46 +00001867 return arrayTypesAreCompatible(lcanon, rcanon);
1868 case Type::FunctionNoProto:
1869 return functionTypesAreCompatible(lcanon, rcanon);
1870 case Type::Tagged: // handle structures, unions
1871 return tagTypesAreCompatible(lcanon, rcanon);
1872 case Type::Builtin:
1873 return builtinTypesAreCompatible(lcanon, rcanon);
1874 case Type::ObjCInterface:
1875 return interfaceTypesAreCompatible(lcanon, rcanon);
1876 case Type::Vector:
1877 case Type::OCUVector:
1878 return vectorTypesAreCompatible(lcanon, rcanon);
1879 case Type::ObjCQualifiedInterface:
1880 return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
1881 default:
1882 assert(0 && "unexpected type");
Steve Naroff85f0dc52007-10-15 20:41:53 +00001883 }
1884 return true; // should never get here...
1885}
Ted Kremenek738e6c02007-10-31 17:10:13 +00001886
Ted Kremenek738e6c02007-10-31 17:10:13 +00001887/// Emit - Serialize an ASTContext object to Bitcode.
1888void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00001889 S.EmitRef(SourceMgr);
1890 S.EmitRef(Target);
1891 S.EmitRef(Idents);
1892 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001893
Ted Kremenek68228a92007-10-31 22:44:07 +00001894 // Emit the size of the type vector so that we can reserve that size
1895 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001896 S.EmitInt(Types.size());
1897
Ted Kremenek034a78c2007-11-13 22:02:55 +00001898 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1899 I!=E;++I)
1900 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001901
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001902 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001903}
1904
Ted Kremenekacba3612007-11-13 00:25:37 +00001905ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek68228a92007-10-31 22:44:07 +00001906 SourceManager &SM = D.ReadRef<SourceManager>();
1907 TargetInfo &t = D.ReadRef<TargetInfo>();
1908 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1909 SelectorTable &sels = D.ReadRef<SelectorTable>();
1910
1911 unsigned size_reserve = D.ReadInt();
1912
1913 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1914
Ted Kremenek034a78c2007-11-13 22:02:55 +00001915 for (unsigned i = 0; i < size_reserve; ++i)
1916 Type::Create(*A,i,D);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001917
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001918 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00001919
1920 return A;
1921}