blob: 6b5eaa112ce0c352c6072019d38c6ffbeeb66a54 [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()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000032 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000033 Types.pop_back();
34 }
Eli Friedman65489b72008-05-27 03:08:09 +000035
36 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000037}
38
39void ASTContext::PrintStats() const {
40 fprintf(stderr, "*** AST Context Stats:\n");
41 fprintf(stderr, " %d types total.\n", (int)Types.size());
42 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
43 unsigned NumVector = 0, NumComplex = 0;
44 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
45
46 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000047 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
48 unsigned NumObjCQualifiedIds = 0;
Steve Naroffe0430632008-05-21 15:59:22 +000049 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000050
51 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
52 Type *T = Types[i];
53 if (isa<BuiltinType>(T))
54 ++NumBuiltin;
55 else if (isa<PointerType>(T))
56 ++NumPointer;
57 else if (isa<ReferenceType>(T))
58 ++NumReference;
59 else if (isa<ComplexType>(T))
60 ++NumComplex;
61 else if (isa<ArrayType>(T))
62 ++NumArray;
63 else if (isa<VectorType>(T))
64 ++NumVector;
65 else if (isa<FunctionTypeNoProto>(T))
66 ++NumFunctionNP;
67 else if (isa<FunctionTypeProto>(T))
68 ++NumFunctionP;
69 else if (isa<TypedefType>(T))
70 ++NumTypeName;
71 else if (TagType *TT = dyn_cast<TagType>(T)) {
72 ++NumTagged;
73 switch (TT->getDecl()->getKind()) {
74 default: assert(0 && "Unknown tagged type!");
75 case Decl::Struct: ++NumTagStruct; break;
76 case Decl::Union: ++NumTagUnion; break;
77 case Decl::Class: ++NumTagClass; break;
78 case Decl::Enum: ++NumTagEnum; break;
79 }
Ted Kremenek42730c52008-01-07 19:49:32 +000080 } else if (isa<ObjCInterfaceType>(T))
81 ++NumObjCInterfaces;
82 else if (isa<ObjCQualifiedInterfaceType>(T))
83 ++NumObjCQualifiedInterfaces;
84 else if (isa<ObjCQualifiedIdType>(T))
85 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +000086 else if (isa<TypeOfType>(T))
87 ++NumTypeOfTypes;
88 else if (isa<TypeOfExpr>(T))
89 ++NumTypeOfExprs;
Steve Naroff948fd372007-09-17 14:16:13 +000090 else {
Chris Lattner8a35b462007-12-12 06:43:05 +000091 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +000092 assert(0 && "Unknown type!");
93 }
94 }
95
96 fprintf(stderr, " %d builtin types\n", NumBuiltin);
97 fprintf(stderr, " %d pointer types\n", NumPointer);
98 fprintf(stderr, " %d reference types\n", NumReference);
99 fprintf(stderr, " %d complex types\n", NumComplex);
100 fprintf(stderr, " %d array types\n", NumArray);
101 fprintf(stderr, " %d vector types\n", NumVector);
102 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
103 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
104 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
105 fprintf(stderr, " %d tagged types\n", NumTagged);
106 fprintf(stderr, " %d struct types\n", NumTagStruct);
107 fprintf(stderr, " %d union types\n", NumTagUnion);
108 fprintf(stderr, " %d class types\n", NumTagClass);
109 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000110 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000111 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000112 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000113 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000114 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000115 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
116 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
117
Chris Lattner4b009652007-07-25 00:24:17 +0000118 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
119 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
120 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
121 NumFunctionP*sizeof(FunctionTypeProto)+
122 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroffe0430632008-05-21 15:59:22 +0000123 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
124 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Chris Lattner4b009652007-07-25 00:24:17 +0000125}
126
127
128void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
129 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
130}
131
Chris Lattner4b009652007-07-25 00:24:17 +0000132void ASTContext::InitBuiltinTypes() {
133 assert(VoidTy.isNull() && "Context reinitialized?");
134
135 // C99 6.2.5p19.
136 InitBuiltinType(VoidTy, BuiltinType::Void);
137
138 // C99 6.2.5p2.
139 InitBuiltinType(BoolTy, BuiltinType::Bool);
140 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000141 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000142 InitBuiltinType(CharTy, BuiltinType::Char_S);
143 else
144 InitBuiltinType(CharTy, BuiltinType::Char_U);
145 // C99 6.2.5p4.
146 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
147 InitBuiltinType(ShortTy, BuiltinType::Short);
148 InitBuiltinType(IntTy, BuiltinType::Int);
149 InitBuiltinType(LongTy, BuiltinType::Long);
150 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
151
152 // C99 6.2.5p6.
153 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
154 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
155 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
156 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
157 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
158
159 // C99 6.2.5p10.
160 InitBuiltinType(FloatTy, BuiltinType::Float);
161 InitBuiltinType(DoubleTy, BuiltinType::Double);
162 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
163
164 // C99 6.2.5p11.
165 FloatComplexTy = getComplexType(FloatTy);
166 DoubleComplexTy = getComplexType(DoubleTy);
167 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff9d12c902007-10-15 14:41:52 +0000168
169 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000170 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000171 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000172 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000173 ClassStructType = 0;
174
Ted Kremenek42730c52008-01-07 19:49:32 +0000175 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000176
177 // void * type
178 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000179}
180
181//===----------------------------------------------------------------------===//
182// Type Sizing and Analysis
183//===----------------------------------------------------------------------===//
184
185/// getTypeSize - Return the size of the specified type, in bits. This method
186/// does not work on incomplete types.
187std::pair<uint64_t, unsigned>
Chris Lattner8cd0e932008-03-05 18:54:05 +0000188ASTContext::getTypeInfo(QualType T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000189 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000190 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000191 unsigned Align;
192 switch (T->getTypeClass()) {
193 case Type::TypeName: assert(0 && "Not a canonical type!");
194 case Type::FunctionNoProto:
195 case Type::FunctionProto:
196 default:
197 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000198 case Type::VariableArray:
199 assert(0 && "VLAs not implemented yet!");
200 case Type::ConstantArray: {
201 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
202
Chris Lattner8cd0e932008-03-05 18:54:05 +0000203 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000204 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000205 Align = EltInfo.second;
206 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000207 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000208 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000209 case Type::Vector: {
210 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000211 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000212 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000213 // FIXME: This isn't right for unusual vectors
214 Align = Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000215 break;
216 }
217
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000218 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000219 switch (cast<BuiltinType>(T)->getKind()) {
220 default: assert(0 && "Unknown builtin type!");
221 case BuiltinType::Void:
222 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000223 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000224 Width = Target.getBoolWidth();
225 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000226 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000227 case BuiltinType::Char_S:
228 case BuiltinType::Char_U:
229 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000230 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000231 Width = Target.getCharWidth();
232 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000233 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000234 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000235 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000236 Width = Target.getShortWidth();
237 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000238 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000239 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000240 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000241 Width = Target.getIntWidth();
242 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000243 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000244 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000245 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000246 Width = Target.getLongWidth();
247 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000248 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000249 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000250 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000251 Width = Target.getLongLongWidth();
252 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000253 break;
254 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000255 Width = Target.getFloatWidth();
256 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000257 break;
258 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000259 Width = Target.getDoubleWidth();
260 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000261 break;
262 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000263 Width = Target.getLongDoubleWidth();
264 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000265 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000266 }
267 break;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000268 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000269 // FIXME: Pointers into different addr spaces could have different sizes and
270 // alignment requirements: getPointerInfo should take an AddrSpace.
271 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000272 case Type::ObjCQualifiedId:
Chris Lattner1d78a862008-04-07 07:01:58 +0000273 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000274 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000275 break;
Chris Lattner461a6c52008-03-08 08:34:58 +0000276 case Type::Pointer: {
277 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000278 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000279 Align = Target.getPointerAlign(AS);
280 break;
281 }
Chris Lattner4b009652007-07-25 00:24:17 +0000282 case Type::Reference:
283 // "When applied to a reference or a reference type, the result is the size
284 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000285 // FIXME: This is wrong for struct layout: a reference in a struct has
286 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000287 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner4b009652007-07-25 00:24:17 +0000288
289 case Type::Complex: {
290 // Complex types have the same alignment as their elements, but twice the
291 // size.
292 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000293 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000294 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000295 Align = EltInfo.second;
296 break;
297 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000298 case Type::Tagged: {
299 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
300 return getTypeInfo(ET->getDecl()->getIntegerType());
301
302 RecordType *RT = cast<RecordType>(T);
303 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
304 Width = Layout.getSize();
305 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000306 break;
307 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000308 }
Chris Lattner4b009652007-07-25 00:24:17 +0000309
310 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000311 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000312}
313
Devang Patelbfe323c2008-06-04 21:22:16 +0000314/// LayoutField - Field layout.
315void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
316 bool IsUnion, bool StructIsPacked,
317 ASTContext &Context) {
318 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
319 uint64_t FieldOffset = IsUnion ? 0 : Size;
320 uint64_t FieldSize;
321 unsigned FieldAlign;
322
323 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
324 // TODO: Need to check this algorithm on other targets!
325 // (tested on Linux-X86)
326 llvm::APSInt I(32);
327 bool BitWidthIsICE =
328 BitWidthExpr->isIntegerConstantExpr(I, Context);
329 assert (BitWidthIsICE && "Invalid BitField size expression");
330 FieldSize = I.getZExtValue();
331
332 std::pair<uint64_t, unsigned> FieldInfo =
333 Context.getTypeInfo(FD->getType());
334 uint64_t TypeSize = FieldInfo.first;
335
336 FieldAlign = FieldInfo.second;
337 if (FieldIsPacked)
338 FieldAlign = 1;
339 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
340 FieldAlign = std::max(FieldAlign, AA->getAlignment());
341
342 // Check if we need to add padding to give the field the correct
343 // alignment.
344 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
345 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
346
347 // Padding members don't affect overall alignment
348 if (!FD->getIdentifier())
349 FieldAlign = 1;
350 } else {
351 if (FD->getType()->isIncompleteType()) {
352 // This must be a flexible array member; we can't directly
353 // query getTypeInfo about these, so we figure it out here.
354 // Flexible array members don't have any size, but they
355 // have to be aligned appropriately for their element type.
356 FieldSize = 0;
357 const ArrayType* ATy = FD->getType()->getAsArrayType();
358 FieldAlign = Context.getTypeAlign(ATy->getElementType());
359 } else {
360 std::pair<uint64_t, unsigned> FieldInfo =
361 Context.getTypeInfo(FD->getType());
362 FieldSize = FieldInfo.first;
363 FieldAlign = FieldInfo.second;
364 }
365
366 if (FieldIsPacked)
367 FieldAlign = 8;
368 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
369 FieldAlign = std::max(FieldAlign, AA->getAlignment());
370
371 // Round up the current record size to the field's alignment boundary.
372 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
373 }
374
375 // Place this field at the current location.
376 FieldOffsets[FieldNo] = FieldOffset;
377
378 // Reserve space for this field.
379 if (IsUnion) {
380 Size = std::max(Size, FieldSize);
381 } else {
382 Size = FieldOffset + FieldSize;
383 }
384
385 // Remember max struct/class alignment.
386 Alignment = std::max(Alignment, FieldAlign);
387}
388
Devang Patel7a78e432007-11-01 19:11:01 +0000389/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000390/// specified record (struct/union/class), which indicates its size and field
391/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000392const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000393 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000394
Chris Lattner4b009652007-07-25 00:24:17 +0000395 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000396 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000397 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000398
Devang Patel7a78e432007-11-01 19:11:01 +0000399 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
400 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
401 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000402 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000403
Devang Patelbfe323c2008-06-04 21:22:16 +0000404 NewEntry->InitializeLayout(D->getNumMembers());
Eli Friedman5949a022008-05-30 09:31:38 +0000405 bool StructIsPacked = D->getAttr<PackedAttr>();
406 bool IsUnion = (D->getKind() == Decl::Union);
Chris Lattner4b009652007-07-25 00:24:17 +0000407
Eli Friedman5949a022008-05-30 09:31:38 +0000408 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000409 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
410 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000411
Eli Friedman5949a022008-05-30 09:31:38 +0000412 // Layout each field, for now, just sequentially, respecting alignment. In
413 // the future, this will need to be tweakable by targets.
414 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
415 const FieldDecl *FD = D->getMember(i);
Devang Patelbfe323c2008-06-04 21:22:16 +0000416 NewEntry->LayoutField(FD, i, IsUnion, StructIsPacked, *this);
Chris Lattner4b009652007-07-25 00:24:17 +0000417 }
Eli Friedman5949a022008-05-30 09:31:38 +0000418
419 // Finally, round the size of the total struct up to the alignment of the
420 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000421 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000422 return *NewEntry;
423}
424
Chris Lattner4b009652007-07-25 00:24:17 +0000425//===----------------------------------------------------------------------===//
426// Type creation/memoization methods
427//===----------------------------------------------------------------------===//
428
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000429QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000430 QualType CanT = getCanonicalType(T);
431 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000432 return T;
433
434 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
435 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000436 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000437 "Type is already address space qualified");
438
439 // Check if we've already instantiated an address space qual'd type of this
440 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000441 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000442 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000443 void *InsertPos = 0;
444 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
445 return QualType(ASQy, 0);
446
447 // If the base type isn't canonical, this won't be a canonical type either,
448 // so fill in the canonical type field.
449 QualType Canonical;
450 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000451 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000452
453 // Get the new insert position for the node we care about.
454 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
455 assert(NewIP == 0 && "Shouldn't be in the map!");
456 }
Chris Lattner35fef522008-02-20 20:55:12 +0000457 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000458 ASQualTypes.InsertNode(New, InsertPos);
459 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000460 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000461}
462
Chris Lattner4b009652007-07-25 00:24:17 +0000463
464/// getComplexType - Return the uniqued reference to the type for a complex
465/// number with the specified element type.
466QualType ASTContext::getComplexType(QualType T) {
467 // Unique pointers, to guarantee there is only one pointer of a particular
468 // structure.
469 llvm::FoldingSetNodeID ID;
470 ComplexType::Profile(ID, T);
471
472 void *InsertPos = 0;
473 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
474 return QualType(CT, 0);
475
476 // If the pointee type isn't canonical, this won't be a canonical type either,
477 // so fill in the canonical type field.
478 QualType Canonical;
479 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000480 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000481
482 // Get the new insert position for the node we care about.
483 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
484 assert(NewIP == 0 && "Shouldn't be in the map!");
485 }
486 ComplexType *New = new ComplexType(T, Canonical);
487 Types.push_back(New);
488 ComplexTypes.InsertNode(New, InsertPos);
489 return QualType(New, 0);
490}
491
492
493/// getPointerType - Return the uniqued reference to the type for a pointer to
494/// the specified type.
495QualType ASTContext::getPointerType(QualType T) {
496 // Unique pointers, to guarantee there is only one pointer of a particular
497 // structure.
498 llvm::FoldingSetNodeID ID;
499 PointerType::Profile(ID, T);
500
501 void *InsertPos = 0;
502 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
503 return QualType(PT, 0);
504
505 // If the pointee type isn't canonical, this won't be a canonical type either,
506 // so fill in the canonical type field.
507 QualType Canonical;
508 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000509 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000510
511 // Get the new insert position for the node we care about.
512 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
513 assert(NewIP == 0 && "Shouldn't be in the map!");
514 }
515 PointerType *New = new PointerType(T, Canonical);
516 Types.push_back(New);
517 PointerTypes.InsertNode(New, InsertPos);
518 return QualType(New, 0);
519}
520
521/// getReferenceType - Return the uniqued reference to the type for a reference
522/// to the specified type.
523QualType ASTContext::getReferenceType(QualType T) {
524 // Unique pointers, to guarantee there is only one pointer of a particular
525 // structure.
526 llvm::FoldingSetNodeID ID;
527 ReferenceType::Profile(ID, T);
528
529 void *InsertPos = 0;
530 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
531 return QualType(RT, 0);
532
533 // If the referencee type isn't canonical, this won't be a canonical type
534 // either, so fill in the canonical type field.
535 QualType Canonical;
536 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000537 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000538
539 // Get the new insert position for the node we care about.
540 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
541 assert(NewIP == 0 && "Shouldn't be in the map!");
542 }
543
544 ReferenceType *New = new ReferenceType(T, Canonical);
545 Types.push_back(New);
546 ReferenceTypes.InsertNode(New, InsertPos);
547 return QualType(New, 0);
548}
549
Steve Naroff83c13012007-08-30 01:06:46 +0000550/// getConstantArrayType - Return the unique reference to the type for an
551/// array of the specified element type.
552QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000553 const llvm::APInt &ArySize,
554 ArrayType::ArraySizeModifier ASM,
555 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000556 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000557 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000558
559 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000560 if (ConstantArrayType *ATP =
561 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000562 return QualType(ATP, 0);
563
564 // If the element type isn't canonical, this won't be a canonical type either,
565 // so fill in the canonical type field.
566 QualType Canonical;
567 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000568 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000569 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000570 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000571 ConstantArrayType *NewIP =
572 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
573
Chris Lattner4b009652007-07-25 00:24:17 +0000574 assert(NewIP == 0 && "Shouldn't be in the map!");
575 }
576
Steve Naroff24c9b982007-08-30 18:10:14 +0000577 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
578 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000579 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000580 Types.push_back(New);
581 return QualType(New, 0);
582}
583
Steve Naroffe2579e32007-08-30 18:14:25 +0000584/// getVariableArrayType - Returns a non-unique reference to the type for a
585/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000586QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
587 ArrayType::ArraySizeModifier ASM,
588 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000589 // Since we don't unique expressions, it isn't possible to unique VLA's
590 // that have an expression provided for their size.
591
592 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
593 ASM, EltTypeQuals);
594
595 VariableArrayTypes.push_back(New);
596 Types.push_back(New);
597 return QualType(New, 0);
598}
599
600QualType ASTContext::getIncompleteArrayType(QualType EltTy,
601 ArrayType::ArraySizeModifier ASM,
602 unsigned EltTypeQuals) {
603 llvm::FoldingSetNodeID ID;
604 IncompleteArrayType::Profile(ID, EltTy);
605
606 void *InsertPos = 0;
607 if (IncompleteArrayType *ATP =
608 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
609 return QualType(ATP, 0);
610
611 // If the element type isn't canonical, this won't be a canonical type
612 // either, so fill in the canonical type field.
613 QualType Canonical;
614
615 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000616 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000617 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000618
619 // Get the new insert position for the node we care about.
620 IncompleteArrayType *NewIP =
621 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
622
623 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000624 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000625
626 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
627 ASM, EltTypeQuals);
628
629 IncompleteArrayTypes.InsertNode(New, InsertPos);
630 Types.push_back(New);
631 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000632}
633
Chris Lattner4b009652007-07-25 00:24:17 +0000634/// getVectorType - Return the unique reference to a vector type of
635/// the specified element type and size. VectorType must be a built-in type.
636QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
637 BuiltinType *baseType;
638
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000639 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000640 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
641
642 // Check if we've already instantiated a vector of this type.
643 llvm::FoldingSetNodeID ID;
644 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
645 void *InsertPos = 0;
646 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
647 return QualType(VTP, 0);
648
649 // If the element type isn't canonical, this won't be a canonical type either,
650 // so fill in the canonical type field.
651 QualType Canonical;
652 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000653 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000654
655 // Get the new insert position for the node we care about.
656 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
657 assert(NewIP == 0 && "Shouldn't be in the map!");
658 }
659 VectorType *New = new VectorType(vecType, NumElts, Canonical);
660 VectorTypes.InsertNode(New, InsertPos);
661 Types.push_back(New);
662 return QualType(New, 0);
663}
664
Nate Begemanaf6ed502008-04-18 23:10:10 +0000665/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +0000666/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000667QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +0000668 BuiltinType *baseType;
669
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000670 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +0000671 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +0000672
673 // Check if we've already instantiated a vector of this type.
674 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000675 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +0000676 void *InsertPos = 0;
677 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
678 return QualType(VTP, 0);
679
680 // If the element type isn't canonical, this won't be a canonical type either,
681 // so fill in the canonical type field.
682 QualType Canonical;
683 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000684 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000685
686 // Get the new insert position for the node we care about.
687 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
688 assert(NewIP == 0 && "Shouldn't be in the map!");
689 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000690 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000691 VectorTypes.InsertNode(New, InsertPos);
692 Types.push_back(New);
693 return QualType(New, 0);
694}
695
696/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
697///
698QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
699 // Unique functions, to guarantee there is only one function of a particular
700 // structure.
701 llvm::FoldingSetNodeID ID;
702 FunctionTypeNoProto::Profile(ID, ResultTy);
703
704 void *InsertPos = 0;
705 if (FunctionTypeNoProto *FT =
706 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
707 return QualType(FT, 0);
708
709 QualType Canonical;
710 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000711 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000712
713 // Get the new insert position for the node we care about.
714 FunctionTypeNoProto *NewIP =
715 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
716 assert(NewIP == 0 && "Shouldn't be in the map!");
717 }
718
719 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
720 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000721 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000722 return QualType(New, 0);
723}
724
725/// getFunctionType - Return a normal function type with a typed argument
726/// list. isVariadic indicates whether the argument list includes '...'.
727QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
728 unsigned NumArgs, bool isVariadic) {
729 // Unique functions, to guarantee there is only one function of a particular
730 // structure.
731 llvm::FoldingSetNodeID ID;
732 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
733
734 void *InsertPos = 0;
735 if (FunctionTypeProto *FTP =
736 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
737 return QualType(FTP, 0);
738
739 // Determine whether the type being created is already canonical or not.
740 bool isCanonical = ResultTy->isCanonical();
741 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
742 if (!ArgArray[i]->isCanonical())
743 isCanonical = false;
744
745 // If this type isn't canonical, get the canonical version of it.
746 QualType Canonical;
747 if (!isCanonical) {
748 llvm::SmallVector<QualType, 16> CanonicalArgs;
749 CanonicalArgs.reserve(NumArgs);
750 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000751 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +0000752
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000753 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +0000754 &CanonicalArgs[0], NumArgs,
755 isVariadic);
756
757 // Get the new insert position for the node we care about.
758 FunctionTypeProto *NewIP =
759 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
760 assert(NewIP == 0 && "Shouldn't be in the map!");
761 }
762
763 // FunctionTypeProto objects are not allocated with new because they have a
764 // variable size array (for parameter types) at the end of them.
765 FunctionTypeProto *FTP =
766 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
767 NumArgs*sizeof(QualType));
768 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
769 Canonical);
770 Types.push_back(FTP);
771 FunctionTypeProtos.InsertNode(FTP, InsertPos);
772 return QualType(FTP, 0);
773}
774
Douglas Gregor1d661552008-04-13 21:07:44 +0000775/// getTypeDeclType - Return the unique reference to the type for the
776/// specified type declaration.
777QualType ASTContext::getTypeDeclType(TypeDecl *Decl) {
778 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
779
780 if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(Decl))
781 return getTypedefType(Typedef);
782 else if (ObjCInterfaceDecl *ObjCInterface
783 = dyn_cast_or_null<ObjCInterfaceDecl>(Decl))
784 return getObjCInterfaceType(ObjCInterface);
785 else if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Decl)) {
786 Decl->TypeForDecl = new RecordType(Record);
787 Types.push_back(Decl->TypeForDecl);
788 return QualType(Decl->TypeForDecl, 0);
789 } else if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Decl)) {
790 Decl->TypeForDecl = new EnumType(Enum);
791 Types.push_back(Decl->TypeForDecl);
792 return QualType(Decl->TypeForDecl, 0);
793 } else
794 assert(false && "TypeDecl without a type?");
795}
796
Chris Lattner4b009652007-07-25 00:24:17 +0000797/// getTypedefType - Return the unique reference to the type for the
798/// specified typename decl.
799QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
800 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
801
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000802 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000803 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000804 Types.push_back(Decl->TypeForDecl);
805 return QualType(Decl->TypeForDecl, 0);
806}
807
Ted Kremenek42730c52008-01-07 19:49:32 +0000808/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000809/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000810QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000811 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
812
Ted Kremenek42730c52008-01-07 19:49:32 +0000813 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000814 Types.push_back(Decl->TypeForDecl);
815 return QualType(Decl->TypeForDecl, 0);
816}
817
Chris Lattnere1352302008-04-07 04:56:42 +0000818/// CmpProtocolNames - Comparison predicate for sorting protocols
819/// alphabetically.
820static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
821 const ObjCProtocolDecl *RHS) {
822 return strcmp(LHS->getName(), RHS->getName()) < 0;
823}
824
825static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
826 unsigned &NumProtocols) {
827 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
828
829 // Sort protocols, keyed by name.
830 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
831
832 // Remove duplicates.
833 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
834 NumProtocols = ProtocolsEnd-Protocols;
835}
836
837
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +0000838/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
839/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000840QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
841 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000842 // Sort the protocol list alphabetically to canonicalize it.
843 SortAndUniqueProtocols(Protocols, NumProtocols);
844
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000845 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +0000846 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000847
848 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000849 if (ObjCQualifiedInterfaceType *QT =
850 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000851 return QualType(QT, 0);
852
853 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +0000854 ObjCQualifiedInterfaceType *QType =
855 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000856 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000857 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000858 return QualType(QType, 0);
859}
860
Chris Lattnere1352302008-04-07 04:56:42 +0000861/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
862/// and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000863QualType ASTContext::getObjCQualifiedIdType(QualType idType,
864 ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000865 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000866 // Sort the protocol list alphabetically to canonicalize it.
867 SortAndUniqueProtocols(Protocols, NumProtocols);
868
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000869 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000870 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000871
872 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000873 if (ObjCQualifiedIdType *QT =
874 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000875 return QualType(QT, 0);
876
877 // No Match;
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000878 QualType Canonical;
879 if (!idType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000880 Canonical = getObjCQualifiedIdType(getCanonicalType(idType),
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000881 Protocols, NumProtocols);
Ted Kremenek42730c52008-01-07 19:49:32 +0000882 ObjCQualifiedIdType *NewQT =
883 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000884 assert(NewQT == 0 && "Shouldn't be in the map!");
885 }
886
Ted Kremenek42730c52008-01-07 19:49:32 +0000887 ObjCQualifiedIdType *QType =
888 new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000889 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000890 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000891 return QualType(QType, 0);
892}
893
Steve Naroff0604dd92007-08-01 18:02:17 +0000894/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
895/// TypeOfExpr AST's (since expression's are never shared). For example,
896/// multiple declarations that refer to "typeof(x)" all contain different
897/// DeclRefExpr's. This doesn't effect the type checker, since it operates
898/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000899QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000900 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff0604dd92007-08-01 18:02:17 +0000901 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
902 Types.push_back(toe);
903 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000904}
905
Steve Naroff0604dd92007-08-01 18:02:17 +0000906/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
907/// TypeOfType AST's. The only motivation to unique these nodes would be
908/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
909/// an issue. This doesn't effect the type checker, since it operates
910/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000911QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000912 QualType Canonical = getCanonicalType(tofType);
Steve Naroff0604dd92007-08-01 18:02:17 +0000913 TypeOfType *tot = new TypeOfType(tofType, Canonical);
914 Types.push_back(tot);
915 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000916}
917
Chris Lattner4b009652007-07-25 00:24:17 +0000918/// getTagDeclType - Return the unique reference to the type for the
919/// specified TagDecl (struct/union/class/enum) decl.
920QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +0000921 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +0000922 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +0000923}
924
925/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
926/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
927/// needs to agree with the definition in <stddef.h>.
928QualType ASTContext::getSizeType() const {
929 // On Darwin, size_t is defined as a "long unsigned int".
930 // FIXME: should derive from "Target".
931 return UnsignedLongTy;
932}
933
Eli Friedmanfdd35d72008-02-12 08:29:21 +0000934/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
935/// width of characters in wide strings, The value is target dependent and
936/// needs to agree with the definition in <stddef.h>.
937QualType ASTContext::getWcharType() const {
938 // On Darwin, wchar_t is defined as a "int".
939 // FIXME: should derive from "Target".
940 return IntTy;
941}
942
Chris Lattner4b009652007-07-25 00:24:17 +0000943/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
944/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
945QualType ASTContext::getPointerDiffType() const {
946 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
947 // FIXME: should derive from "Target".
948 return IntTy;
949}
950
Chris Lattner19eb97e2008-04-02 05:18:44 +0000951//===----------------------------------------------------------------------===//
952// Type Operators
953//===----------------------------------------------------------------------===//
954
Chris Lattner3dae6f42008-04-06 22:41:35 +0000955/// getCanonicalType - Return the canonical (structural) type corresponding to
956/// the specified potentially non-canonical type. The non-canonical version
957/// of a type may have many "decorated" versions of types. Decorators can
958/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
959/// to be free of any of these, allowing two canonical types to be compared
960/// for exact equality with a simple pointer comparison.
961QualType ASTContext::getCanonicalType(QualType T) {
962 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
963 return QualType(CanType.getTypePtr(),
964 T.getCVRQualifiers() | CanType.getCVRQualifiers());
965}
966
967
Chris Lattner19eb97e2008-04-02 05:18:44 +0000968/// getArrayDecayedType - Return the properly qualified result of decaying the
969/// specified array type to a pointer. This operation is non-trivial when
970/// handling typedefs etc. The canonical type of "T" must be an array type,
971/// this returns a pointer to a properly qualified element of the array.
972///
973/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
974QualType ASTContext::getArrayDecayedType(QualType Ty) {
975 // Handle the common case where typedefs are not involved directly.
976 QualType EltTy;
977 unsigned ArrayQuals = 0;
978 unsigned PointerQuals = 0;
979 if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
980 // Since T "isa" an array type, it could not have had an address space
981 // qualifier, just CVR qualifiers. The properly qualified element pointer
982 // gets the union of the CVR qualifiers from the element and the array, and
983 // keeps any address space qualifier on the element type if present.
984 EltTy = AT->getElementType();
985 ArrayQuals = Ty.getCVRQualifiers();
986 PointerQuals = AT->getIndexTypeQualifier();
987 } else {
988 // Otherwise, we have an ASQualType or a typedef, etc. Make sure we don't
989 // lose qualifiers when dealing with typedefs. Example:
990 // typedef int arr[10];
991 // void test2() {
992 // const arr b;
993 // b[4] = 1;
994 // }
995 //
996 // The decayed type of b is "const int*" even though the element type of the
997 // array is "int".
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000998 QualType CanTy = getCanonicalType(Ty);
Chris Lattner19eb97e2008-04-02 05:18:44 +0000999 const ArrayType *PrettyArrayType = Ty->getAsArrayType();
1000 assert(PrettyArrayType && "Not an array type!");
1001
1002 // Get the element type with 'getAsArrayType' so that we don't lose any
1003 // typedefs in the element type of the array.
1004 EltTy = PrettyArrayType->getElementType();
1005
1006 // If the array was address-space qualifier, make sure to ASQual the element
1007 // type. We can just grab the address space from the canonical type.
1008 if (unsigned AS = CanTy.getAddressSpace())
1009 EltTy = getASQualType(EltTy, AS);
1010
1011 // To properly handle [multiple levels of] typedefs, typeof's etc, we take
1012 // the CVR qualifiers directly from the canonical type, which is guaranteed
1013 // to have the full set unioned together.
1014 ArrayQuals = CanTy.getCVRQualifiers();
1015 PointerQuals = PrettyArrayType->getIndexTypeQualifier();
1016 }
1017
Chris Lattnerda79b3f2008-04-02 06:06:35 +00001018 // Apply any CVR qualifiers from the array type to the element type. This
1019 // implements C99 6.7.3p8: "If the specification of an array type includes
1020 // any type qualifiers, the element type is so qualified, not the array type."
Chris Lattner19eb97e2008-04-02 05:18:44 +00001021 EltTy = EltTy.getQualifiedType(ArrayQuals | EltTy.getCVRQualifiers());
1022
1023 QualType PtrTy = getPointerType(EltTy);
1024
1025 // int x[restrict 4] -> int *restrict
1026 PtrTy = PtrTy.getQualifiedType(PointerQuals);
1027
1028 return PtrTy;
1029}
1030
Chris Lattner4b009652007-07-25 00:24:17 +00001031/// getFloatingRank - Return a relative rank for floating point types.
1032/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001033static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001034 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001035 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001036
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001037 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001038 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001039 case BuiltinType::Float: return FloatRank;
1040 case BuiltinType::Double: return DoubleRank;
1041 case BuiltinType::LongDouble: return LongDoubleRank;
1042 }
1043}
1044
Steve Narofffa0c4532007-08-27 01:41:48 +00001045/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1046/// point or a complex type (based on typeDomain/typeSize).
1047/// 'typeDomain' is a real floating point or complex type.
1048/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001049QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1050 QualType Domain) const {
1051 FloatingRank EltRank = getFloatingRank(Size);
1052 if (Domain->isComplexType()) {
1053 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001054 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001055 case FloatRank: return FloatComplexTy;
1056 case DoubleRank: return DoubleComplexTy;
1057 case LongDoubleRank: return LongDoubleComplexTy;
1058 }
Chris Lattner4b009652007-07-25 00:24:17 +00001059 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001060
1061 assert(Domain->isRealFloatingType() && "Unknown domain!");
1062 switch (EltRank) {
1063 default: assert(0 && "getFloatingRank(): illegal value for rank");
1064 case FloatRank: return FloatTy;
1065 case DoubleRank: return DoubleTy;
1066 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001067 }
Chris Lattner4b009652007-07-25 00:24:17 +00001068}
1069
Chris Lattner51285d82008-04-06 23:55:33 +00001070/// getFloatingTypeOrder - Compare the rank of the two specified floating
1071/// point types, ignoring the domain of the type (i.e. 'double' ==
1072/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1073/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001074int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1075 FloatingRank LHSR = getFloatingRank(LHS);
1076 FloatingRank RHSR = getFloatingRank(RHS);
1077
1078 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001079 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001080 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001081 return 1;
1082 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001083}
1084
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001085/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1086/// routine will assert if passed a built-in type that isn't an integer or enum,
1087/// or if it is not canonicalized.
1088static unsigned getIntegerRank(Type *T) {
1089 assert(T->isCanonical() && "T should be canonicalized");
1090 if (isa<EnumType>(T))
1091 return 4;
1092
1093 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001094 default: assert(0 && "getIntegerRank(): not a built-in integer");
1095 case BuiltinType::Bool:
1096 return 1;
1097 case BuiltinType::Char_S:
1098 case BuiltinType::Char_U:
1099 case BuiltinType::SChar:
1100 case BuiltinType::UChar:
1101 return 2;
1102 case BuiltinType::Short:
1103 case BuiltinType::UShort:
1104 return 3;
1105 case BuiltinType::Int:
1106 case BuiltinType::UInt:
1107 return 4;
1108 case BuiltinType::Long:
1109 case BuiltinType::ULong:
1110 return 5;
1111 case BuiltinType::LongLong:
1112 case BuiltinType::ULongLong:
1113 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001114 }
1115}
1116
Chris Lattner51285d82008-04-06 23:55:33 +00001117/// getIntegerTypeOrder - Returns the highest ranked integer type:
1118/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1119/// LHS < RHS, return -1.
1120int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001121 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1122 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001123 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001124
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001125 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1126 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001127
Chris Lattner51285d82008-04-06 23:55:33 +00001128 unsigned LHSRank = getIntegerRank(LHSC);
1129 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001130
Chris Lattner51285d82008-04-06 23:55:33 +00001131 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1132 if (LHSRank == RHSRank) return 0;
1133 return LHSRank > RHSRank ? 1 : -1;
1134 }
Chris Lattner4b009652007-07-25 00:24:17 +00001135
Chris Lattner51285d82008-04-06 23:55:33 +00001136 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1137 if (LHSUnsigned) {
1138 // If the unsigned [LHS] type is larger, return it.
1139 if (LHSRank >= RHSRank)
1140 return 1;
1141
1142 // If the signed type can represent all values of the unsigned type, it
1143 // wins. Because we are dealing with 2's complement and types that are
1144 // powers of two larger than each other, this is always safe.
1145 return -1;
1146 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001147
Chris Lattner51285d82008-04-06 23:55:33 +00001148 // If the unsigned [RHS] type is larger, return it.
1149 if (RHSRank >= LHSRank)
1150 return -1;
1151
1152 // If the signed type can represent all values of the unsigned type, it
1153 // wins. Because we are dealing with 2's complement and types that are
1154 // powers of two larger than each other, this is always safe.
1155 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001156}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001157
1158// getCFConstantStringType - Return the type used for constant CFStrings.
1159QualType ASTContext::getCFConstantStringType() {
1160 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001161 CFConstantStringTypeDecl =
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001162 RecordDecl::Create(*this, Decl::Struct, TUDecl, SourceLocation(),
Chris Lattner58114f02008-03-15 21:32:50 +00001163 &Idents.get("NSConstantString"), 0);
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001164 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001165
1166 // const int *isa;
1167 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001168 // int flags;
1169 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001170 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001171 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001172 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001173 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001174 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001175 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001176
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001177 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerf3874bc2008-04-06 04:47:34 +00001178 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner81db64a2008-03-16 00:16:02 +00001179 FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001180
1181 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1182 }
1183
1184 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001185}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001186
Anders Carlssone3f02572007-10-29 06:33:42 +00001187// This returns true if a type has been typedefed to BOOL:
1188// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001189static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001190 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001191 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001192
1193 return false;
1194}
1195
Ted Kremenek42730c52008-01-07 19:49:32 +00001196/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001197/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001198int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001199 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001200
1201 // Make all integer and enum types at least as large as an int
1202 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001203 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001204 // Treat arrays as pointers, since that's how they're passed in.
1205 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001206 sz = getTypeSize(VoidPtrTy);
1207 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001208}
1209
Ted Kremenek42730c52008-01-07 19:49:32 +00001210/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001211/// declaration.
Ted Kremenek42730c52008-01-07 19:49:32 +00001212void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001213 std::string& S)
1214{
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001215 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001216 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001217 // Encode result type.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001218 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001219 // Compute size of all parameters.
1220 // Start with computing size of a pointer in number of bytes.
1221 // FIXME: There might(should) be a better way of doing this computation!
1222 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001223 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001224 // The first two arguments (self and _cmd) are pointers; account for
1225 // their size.
1226 int ParmOffset = 2 * PtrSize;
1227 int NumOfParams = Decl->getNumParams();
1228 for (int i = 0; i < NumOfParams; i++) {
1229 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001230 int sz = getObjCEncodingTypeSize (PType);
1231 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001232 ParmOffset += sz;
1233 }
1234 S += llvm::utostr(ParmOffset);
1235 S += "@0:";
1236 S += llvm::utostr(PtrSize);
1237
1238 // Argument types.
1239 ParmOffset = 2 * PtrSize;
1240 for (int i = 0; i < NumOfParams; i++) {
1241 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001242 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001243 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001244 getObjCEncodingForTypeQualifier(
1245 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian248db262008-01-22 22:44:46 +00001246 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001247 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001248 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001249 }
1250}
1251
Fariborz Jahanian248db262008-01-22 22:44:46 +00001252void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1253 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson36f07d82007-10-29 05:01:08 +00001254{
Anders Carlssone3f02572007-10-29 06:33:42 +00001255 // FIXME: This currently doesn't encode:
1256 // @ An object (whether statically typed or typed id)
1257 // # A class object (Class)
1258 // : A method selector (SEL)
1259 // {name=type...} A structure
1260 // (name=type...) A union
1261 // bnum A bit field of num bits
1262
1263 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001264 char encoding;
1265 switch (BT->getKind()) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001266 default: assert(0 && "Unhandled builtin type kind");
1267 case BuiltinType::Void: encoding = 'v'; break;
1268 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001269 case BuiltinType::Char_U:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001270 case BuiltinType::UChar: encoding = 'C'; break;
1271 case BuiltinType::UShort: encoding = 'S'; break;
1272 case BuiltinType::UInt: encoding = 'I'; break;
1273 case BuiltinType::ULong: encoding = 'L'; break;
1274 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001275 case BuiltinType::Char_S:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001276 case BuiltinType::SChar: encoding = 'c'; break;
1277 case BuiltinType::Short: encoding = 's'; break;
1278 case BuiltinType::Int: encoding = 'i'; break;
1279 case BuiltinType::Long: encoding = 'l'; break;
1280 case BuiltinType::LongLong: encoding = 'q'; break;
1281 case BuiltinType::Float: encoding = 'f'; break;
1282 case BuiltinType::Double: encoding = 'd'; break;
1283 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001284 }
1285
1286 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001287 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001288 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001289 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001290 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001291
1292 }
1293 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001294 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001295 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001296 S += '@';
1297 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001298 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001299 S += '#';
1300 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001301 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001302 S += ':';
1303 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001304 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001305
1306 if (PointeeTy->isCharType()) {
1307 // char pointer types should be encoded as '*' unless it is a
1308 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001309 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001310 S += '*';
1311 return;
1312 }
1313 }
1314
1315 S += '^';
Fariborz Jahanian248db262008-01-22 22:44:46 +00001316 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone3f02572007-10-29 06:33:42 +00001317 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001318 S += '[';
1319
1320 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1321 S += llvm::utostr(CAT->getSize().getZExtValue());
1322 else
1323 assert(0 && "Unhandled array type!");
1324
Fariborz Jahanian248db262008-01-22 22:44:46 +00001325 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001326 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001327 } else if (T->getAsFunctionType()) {
1328 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001329 } else if (const RecordType *RTy = T->getAsRecordType()) {
1330 RecordDecl *RDecl= RTy->getDecl();
1331 S += '{';
1332 S += RDecl->getName();
Fariborz Jahanian248db262008-01-22 22:44:46 +00001333 bool found = false;
1334 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1335 if (ERType[i] == RTy) {
1336 found = true;
1337 break;
1338 }
1339 if (!found) {
1340 ERType.push_back(RTy);
1341 S += '=';
1342 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1343 FieldDecl *field = RDecl->getMember(i);
1344 getObjCEncodingForType(field->getType(), S, ERType);
1345 }
1346 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1347 ERType.pop_back();
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001348 }
1349 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001350 } else if (T->isEnumeralType()) {
1351 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001352 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001353 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001354}
1355
Ted Kremenek42730c52008-01-07 19:49:32 +00001356void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001357 std::string& S) const {
1358 if (QT & Decl::OBJC_TQ_In)
1359 S += 'n';
1360 if (QT & Decl::OBJC_TQ_Inout)
1361 S += 'N';
1362 if (QT & Decl::OBJC_TQ_Out)
1363 S += 'o';
1364 if (QT & Decl::OBJC_TQ_Bycopy)
1365 S += 'O';
1366 if (QT & Decl::OBJC_TQ_Byref)
1367 S += 'R';
1368 if (QT & Decl::OBJC_TQ_Oneway)
1369 S += 'V';
1370}
1371
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001372void ASTContext::setBuiltinVaListType(QualType T)
1373{
1374 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1375
1376 BuiltinVaListType = T;
1377}
1378
Ted Kremenek42730c52008-01-07 19:49:32 +00001379void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001380{
Ted Kremenek42730c52008-01-07 19:49:32 +00001381 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff9d12c902007-10-15 14:41:52 +00001382
Ted Kremenek42730c52008-01-07 19:49:32 +00001383 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001384
1385 // typedef struct objc_object *id;
1386 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1387 assert(ptr && "'id' incorrectly typed");
1388 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1389 assert(rec && "'id' incorrectly typed");
1390 IdStructType = rec;
1391}
1392
Ted Kremenek42730c52008-01-07 19:49:32 +00001393void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001394{
Ted Kremenek42730c52008-01-07 19:49:32 +00001395 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001396
Ted Kremenek42730c52008-01-07 19:49:32 +00001397 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001398
1399 // typedef struct objc_selector *SEL;
1400 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1401 assert(ptr && "'SEL' incorrectly typed");
1402 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1403 assert(rec && "'SEL' incorrectly typed");
1404 SelStructType = rec;
1405}
1406
Ted Kremenek42730c52008-01-07 19:49:32 +00001407void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001408{
Ted Kremenek42730c52008-01-07 19:49:32 +00001409 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1410 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001411}
1412
Ted Kremenek42730c52008-01-07 19:49:32 +00001413void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001414{
Ted Kremenek42730c52008-01-07 19:49:32 +00001415 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001416
Ted Kremenek42730c52008-01-07 19:49:32 +00001417 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001418
1419 // typedef struct objc_class *Class;
1420 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1421 assert(ptr && "'Class' incorrectly typed");
1422 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1423 assert(rec && "'Class' incorrectly typed");
1424 ClassStructType = rec;
1425}
1426
Ted Kremenek42730c52008-01-07 19:49:32 +00001427void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1428 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001429 "'NSConstantString' type already set!");
1430
Ted Kremenek42730c52008-01-07 19:49:32 +00001431 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001432}
1433
Chris Lattner6ff358b2008-04-07 06:51:04 +00001434//===----------------------------------------------------------------------===//
1435// Type Compatibility Testing
1436//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00001437
Chris Lattner390564e2008-04-07 06:49:41 +00001438/// C99 6.2.7p1: If both are complete types, then the following additional
1439/// requirements apply.
1440/// FIXME (handle compatibility across source files).
1441static bool areCompatTagTypes(TagType *LHS, TagType *RHS,
1442 const ASTContext &C) {
Steve Naroff4a5e2072007-11-07 06:03:51 +00001443 // "Class" and "id" are compatible built-in structure types.
Chris Lattner390564e2008-04-07 06:49:41 +00001444 if (C.isObjCIdType(QualType(LHS, 0)) && C.isObjCClassType(QualType(RHS, 0)) ||
1445 C.isObjCClassType(QualType(LHS, 0)) && C.isObjCIdType(QualType(RHS, 0)))
Steve Naroff4a5e2072007-11-07 06:03:51 +00001446 return true;
Eli Friedmane7fb03a2008-02-15 06:03:44 +00001447
Chris Lattner390564e2008-04-07 06:49:41 +00001448 // Within a translation unit a tag type is only compatible with itself. Self
1449 // equality is already handled by the time we get here.
1450 assert(LHS != RHS && "Self equality not handled!");
1451 return false;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001452}
1453
1454bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1455 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1456 // identically qualified and both shall be pointers to compatible types.
Chris Lattner35fef522008-02-20 20:55:12 +00001457 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1458 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001459 return false;
1460
1461 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1462 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1463
1464 return typesAreCompatible(ltype, rtype);
1465}
1466
Steve Naroff85f0dc52007-10-15 20:41:53 +00001467bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1468 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1469 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1470 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1471 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1472
1473 // first check the return types (common between C99 and K&R).
1474 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1475 return false;
1476
1477 if (lproto && rproto) { // two C99 style function prototypes
1478 unsigned lproto_nargs = lproto->getNumArgs();
1479 unsigned rproto_nargs = rproto->getNumArgs();
1480
1481 if (lproto_nargs != rproto_nargs)
1482 return false;
1483
1484 // both prototypes have the same number of arguments.
1485 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1486 (rproto->isVariadic() && !lproto->isVariadic()))
1487 return false;
1488
1489 // The use of ellipsis agree...now check the argument types.
1490 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001491 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1492 // is taken as having the unqualified version of it's declared type.
Steve Naroffdec17fe2008-01-29 00:15:50 +00001493 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001494 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001495 return false;
1496 return true;
1497 }
Chris Lattner1d78a862008-04-07 07:01:58 +00001498
Steve Naroff85f0dc52007-10-15 20:41:53 +00001499 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1500 return true;
1501
1502 // we have a mixture of K&R style with C99 prototypes
1503 const FunctionTypeProto *proto = lproto ? lproto : rproto;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001504 if (proto->isVariadic())
1505 return false;
1506
1507 // FIXME: Each parameter type T in the prototype must be compatible with the
1508 // type resulting from applying the usual argument conversions to T.
1509 return true;
1510}
1511
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001512// C99 6.7.5.2p6
1513static bool areCompatArrayTypes(ArrayType *LHS, ArrayType *RHS, ASTContext &C) {
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001514 // Constant arrays must be the same size to be compatible.
1515 if (const ConstantArrayType* LCAT = dyn_cast<ConstantArrayType>(LHS))
1516 if (const ConstantArrayType* RCAT = dyn_cast<ConstantArrayType>(RHS))
1517 if (RCAT->getSize() != LCAT->getSize())
1518 return false;
Eli Friedman1e7537832008-02-06 04:53:22 +00001519
Chris Lattnerc8971d72008-04-07 06:58:21 +00001520 // Compatible arrays must have compatible element types
1521 return C.typesAreCompatible(LHS->getElementType(), RHS->getElementType());
Steve Naroff85f0dc52007-10-15 20:41:53 +00001522}
1523
Chris Lattner6ff358b2008-04-07 06:51:04 +00001524/// areCompatVectorTypes - Return true if the two specified vector types are
1525/// compatible.
1526static bool areCompatVectorTypes(const VectorType *LHS,
1527 const VectorType *RHS) {
1528 assert(LHS->isCanonical() && RHS->isCanonical());
1529 return LHS->getElementType() == RHS->getElementType() &&
1530 LHS->getNumElements() == RHS->getNumElements();
1531}
1532
1533/// areCompatObjCInterfaces - Return true if the two interface types are
1534/// compatible for assignment from RHS to LHS. This handles validation of any
1535/// protocol qualifiers on the LHS or RHS.
1536///
Chris Lattner1d78a862008-04-07 07:01:58 +00001537static bool areCompatObjCInterfaces(const ObjCInterfaceType *LHS,
1538 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00001539 // Verify that the base decls are compatible: the RHS must be a subclass of
1540 // the LHS.
1541 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1542 return false;
1543
1544 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1545 // protocol qualified at all, then we are good.
1546 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1547 return true;
1548
1549 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1550 // isn't a superset.
1551 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1552 return true; // FIXME: should return false!
1553
1554 // Finally, we must have two protocol-qualified interfaces.
1555 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1556 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1557 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1558 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1559 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1560 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1561
1562 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1563 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1564 // LHS in a single parallel scan until we run out of elements in LHS.
1565 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1566 ObjCProtocolDecl *LHSProto = *LHSPI;
1567
1568 while (RHSPI != RHSPE) {
1569 ObjCProtocolDecl *RHSProto = *RHSPI++;
1570 // If the RHS has a protocol that the LHS doesn't, ignore it.
1571 if (RHSProto != LHSProto)
1572 continue;
1573
1574 // Otherwise, the RHS does have this element.
1575 ++LHSPI;
1576 if (LHSPI == LHSPE)
1577 return true; // All protocols in LHS exist in RHS.
1578
1579 LHSProto = *LHSPI;
1580 }
1581
1582 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1583 return false;
1584}
1585
1586
Steve Naroff85f0dc52007-10-15 20:41:53 +00001587/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1588/// both shall have the identically qualified version of a compatible type.
1589/// C99 6.2.7p1: Two types have compatible types if their types are the
1590/// same. See 6.7.[2,3,5] for additional rules.
Chris Lattner855fed42008-04-07 04:07:56 +00001591bool ASTContext::typesAreCompatible(QualType LHS_NC, QualType RHS_NC) {
1592 QualType LHS = LHS_NC.getCanonicalType();
1593 QualType RHS = RHS_NC.getCanonicalType();
Chris Lattner4d5670b2008-04-03 05:07:04 +00001594
Bill Wendling6a9d8542007-12-03 07:33:35 +00001595 // C++ [expr]: If an expression initially has the type "reference to T", the
1596 // type is adjusted to "T" prior to any further analysis, the expression
1597 // designates the object or function denoted by the reference, and the
1598 // expression is an lvalue.
Chris Lattner855fed42008-04-07 04:07:56 +00001599 if (ReferenceType *RT = dyn_cast<ReferenceType>(LHS))
1600 LHS = RT->getPointeeType();
1601 if (ReferenceType *RT = dyn_cast<ReferenceType>(RHS))
1602 RHS = RT->getPointeeType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001603
Chris Lattnerd47d6042008-04-07 05:37:56 +00001604 // If two types are identical, they are compatible.
1605 if (LHS == RHS)
1606 return true;
1607
1608 // If qualifiers differ, the types are different.
Chris Lattnerb5709e22008-04-07 05:43:21 +00001609 unsigned LHSAS = LHS.getAddressSpace(), RHSAS = RHS.getAddressSpace();
1610 if (LHS.getCVRQualifiers() != RHS.getCVRQualifiers() || LHSAS != RHSAS)
Chris Lattnerd47d6042008-04-07 05:37:56 +00001611 return false;
Chris Lattnerb5709e22008-04-07 05:43:21 +00001612
1613 // Strip off ASQual's if present.
1614 if (LHSAS) {
1615 LHS = LHS.getUnqualifiedType();
1616 RHS = RHS.getUnqualifiedType();
1617 }
Chris Lattnerd47d6042008-04-07 05:37:56 +00001618
Chris Lattner855fed42008-04-07 04:07:56 +00001619 Type::TypeClass LHSClass = LHS->getTypeClass();
1620 Type::TypeClass RHSClass = RHS->getTypeClass();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001621
1622 // We want to consider the two function types to be the same for these
1623 // comparisons, just force one to the other.
1624 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1625 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00001626
1627 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00001628 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
1629 LHSClass = Type::ConstantArray;
1630 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
1631 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001632
Nate Begemanaf6ed502008-04-18 23:10:10 +00001633 // Canonicalize ExtVector -> Vector.
1634 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
1635 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00001636
Chris Lattner7cdcb252008-04-07 06:38:24 +00001637 // Consider qualified interfaces and interfaces the same.
1638 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
1639 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
1640
Chris Lattnerb5709e22008-04-07 05:43:21 +00001641 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001642 if (LHSClass != RHSClass) {
Chris Lattner7cdcb252008-04-07 06:38:24 +00001643 // ID is compatible with all interface types.
1644 if (isa<ObjCInterfaceType>(LHS))
1645 return isObjCIdType(RHS);
1646 if (isa<ObjCInterfaceType>(RHS))
1647 return isObjCIdType(LHS);
Steve Naroff44549772008-06-04 15:07:33 +00001648
1649 // ID is compatible with all qualified id types.
1650 if (isa<ObjCQualifiedIdType>(LHS)) {
1651 if (const PointerType *PT = RHS->getAsPointerType())
1652 return isObjCIdType(PT->getPointeeType());
1653 }
1654 if (isa<ObjCQualifiedIdType>(RHS)) {
1655 if (const PointerType *PT = LHS->getAsPointerType())
1656 return isObjCIdType(PT->getPointeeType());
1657 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001658 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1659 // a signed integer type, or an unsigned integer type.
Chris Lattner855fed42008-04-07 04:07:56 +00001660 if (LHS->isEnumeralType() && RHS->isIntegralType()) {
1661 EnumDecl* EDecl = cast<EnumType>(LHS)->getDecl();
1662 return EDecl->getIntegerType() == RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001663 }
Chris Lattner855fed42008-04-07 04:07:56 +00001664 if (RHS->isEnumeralType() && LHS->isIntegralType()) {
1665 EnumDecl* EDecl = cast<EnumType>(RHS)->getDecl();
1666 return EDecl->getIntegerType() == LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001667 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001668
Steve Naroff85f0dc52007-10-15 20:41:53 +00001669 return false;
1670 }
Chris Lattnerb5709e22008-04-07 05:43:21 +00001671
Steve Naroffc88babe2008-01-09 22:43:08 +00001672 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001673 switch (LHSClass) {
Chris Lattnerb5709e22008-04-07 05:43:21 +00001674 case Type::ASQual:
1675 case Type::FunctionProto:
1676 case Type::VariableArray:
1677 case Type::IncompleteArray:
1678 case Type::Reference:
Chris Lattner7cdcb252008-04-07 06:38:24 +00001679 case Type::ObjCQualifiedInterface:
Chris Lattnerb5709e22008-04-07 05:43:21 +00001680 assert(0 && "Canonicalized away above");
Chris Lattnerc38d4522008-01-14 05:45:46 +00001681 case Type::Pointer:
Chris Lattner855fed42008-04-07 04:07:56 +00001682 return pointerTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001683 case Type::ConstantArray:
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001684 return areCompatArrayTypes(cast<ArrayType>(LHS), cast<ArrayType>(RHS),
1685 *this);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001686 case Type::FunctionNoProto:
Chris Lattner855fed42008-04-07 04:07:56 +00001687 return functionTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001688 case Type::Tagged: // handle structures, unions
Chris Lattner390564e2008-04-07 06:49:41 +00001689 return areCompatTagTypes(cast<TagType>(LHS), cast<TagType>(RHS), *this);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001690 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00001691 // Only exactly equal builtin types are compatible, which is tested above.
1692 return false;
1693 case Type::Vector:
1694 return areCompatVectorTypes(cast<VectorType>(LHS), cast<VectorType>(RHS));
Chris Lattnerc38d4522008-01-14 05:45:46 +00001695 case Type::ObjCInterface:
Chris Lattner7cdcb252008-04-07 06:38:24 +00001696 return areCompatObjCInterfaces(cast<ObjCInterfaceType>(LHS),
1697 cast<ObjCInterfaceType>(RHS));
Chris Lattnerc38d4522008-01-14 05:45:46 +00001698 default:
1699 assert(0 && "unexpected type");
Steve Naroff85f0dc52007-10-15 20:41:53 +00001700 }
1701 return true; // should never get here...
1702}
Ted Kremenek738e6c02007-10-31 17:10:13 +00001703
Chris Lattner1d78a862008-04-07 07:01:58 +00001704//===----------------------------------------------------------------------===//
1705// Serialization Support
1706//===----------------------------------------------------------------------===//
1707
Ted Kremenek738e6c02007-10-31 17:10:13 +00001708/// Emit - Serialize an ASTContext object to Bitcode.
1709void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00001710 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00001711 S.EmitRef(SourceMgr);
1712 S.EmitRef(Target);
1713 S.EmitRef(Idents);
1714 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001715
Ted Kremenek68228a92007-10-31 22:44:07 +00001716 // Emit the size of the type vector so that we can reserve that size
1717 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001718 S.EmitInt(Types.size());
1719
Ted Kremenek034a78c2007-11-13 22:02:55 +00001720 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1721 I!=E;++I)
1722 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001723
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001724 S.EmitOwnedPtr(TUDecl);
1725
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001726 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001727}
1728
Ted Kremenekacba3612007-11-13 00:25:37 +00001729ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00001730
1731 // Read the language options.
1732 LangOptions LOpts;
1733 LOpts.Read(D);
1734
Ted Kremenek68228a92007-10-31 22:44:07 +00001735 SourceManager &SM = D.ReadRef<SourceManager>();
1736 TargetInfo &t = D.ReadRef<TargetInfo>();
1737 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1738 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00001739
Ted Kremenek68228a92007-10-31 22:44:07 +00001740 unsigned size_reserve = D.ReadInt();
1741
Ted Kremenek842126e2008-06-04 15:55:15 +00001742 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00001743
Ted Kremenek034a78c2007-11-13 22:02:55 +00001744 for (unsigned i = 0; i < size_reserve; ++i)
1745 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00001746
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001747 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
1748
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001749 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00001750
1751 return A;
1752}