blob: 923aab291db2f8f1c65973e1e49af39621805e62 [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 }
35}
36
37void ASTContext::PrintStats() const {
38 fprintf(stderr, "*** AST Context Stats:\n");
39 fprintf(stderr, " %d types total.\n", (int)Types.size());
40 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
41 unsigned NumVector = 0, NumComplex = 0;
42 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
43
44 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000045 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
46 unsigned NumObjCQualifiedIds = 0;
Steve Naroffe0430632008-05-21 15:59:22 +000047 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000048
49 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
50 Type *T = Types[i];
51 if (isa<BuiltinType>(T))
52 ++NumBuiltin;
53 else if (isa<PointerType>(T))
54 ++NumPointer;
55 else if (isa<ReferenceType>(T))
56 ++NumReference;
57 else if (isa<ComplexType>(T))
58 ++NumComplex;
59 else if (isa<ArrayType>(T))
60 ++NumArray;
61 else if (isa<VectorType>(T))
62 ++NumVector;
63 else if (isa<FunctionTypeNoProto>(T))
64 ++NumFunctionNP;
65 else if (isa<FunctionTypeProto>(T))
66 ++NumFunctionP;
67 else if (isa<TypedefType>(T))
68 ++NumTypeName;
69 else if (TagType *TT = dyn_cast<TagType>(T)) {
70 ++NumTagged;
71 switch (TT->getDecl()->getKind()) {
72 default: assert(0 && "Unknown tagged type!");
73 case Decl::Struct: ++NumTagStruct; break;
74 case Decl::Union: ++NumTagUnion; break;
75 case Decl::Class: ++NumTagClass; break;
76 case Decl::Enum: ++NumTagEnum; break;
77 }
Ted Kremenek42730c52008-01-07 19:49:32 +000078 } else if (isa<ObjCInterfaceType>(T))
79 ++NumObjCInterfaces;
80 else if (isa<ObjCQualifiedInterfaceType>(T))
81 ++NumObjCQualifiedInterfaces;
82 else if (isa<ObjCQualifiedIdType>(T))
83 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +000084 else if (isa<TypeOfType>(T))
85 ++NumTypeOfTypes;
86 else if (isa<TypeOfExpr>(T))
87 ++NumTypeOfExprs;
Steve Naroff948fd372007-09-17 14:16:13 +000088 else {
Chris Lattner8a35b462007-12-12 06:43:05 +000089 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +000090 assert(0 && "Unknown type!");
91 }
92 }
93
94 fprintf(stderr, " %d builtin types\n", NumBuiltin);
95 fprintf(stderr, " %d pointer types\n", NumPointer);
96 fprintf(stderr, " %d reference types\n", NumReference);
97 fprintf(stderr, " %d complex types\n", NumComplex);
98 fprintf(stderr, " %d array types\n", NumArray);
99 fprintf(stderr, " %d vector types\n", NumVector);
100 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
101 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
102 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
103 fprintf(stderr, " %d tagged types\n", NumTagged);
104 fprintf(stderr, " %d struct types\n", NumTagStruct);
105 fprintf(stderr, " %d union types\n", NumTagUnion);
106 fprintf(stderr, " %d class types\n", NumTagClass);
107 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000108 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000109 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000110 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000111 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000112 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000113 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
114 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
115
Chris Lattner4b009652007-07-25 00:24:17 +0000116 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
117 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
118 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
119 NumFunctionP*sizeof(FunctionTypeProto)+
120 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroffe0430632008-05-21 15:59:22 +0000121 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
122 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Chris Lattner4b009652007-07-25 00:24:17 +0000123}
124
125
126void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
127 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
128}
129
Chris Lattner4b009652007-07-25 00:24:17 +0000130void ASTContext::InitBuiltinTypes() {
131 assert(VoidTy.isNull() && "Context reinitialized?");
132
133 // C99 6.2.5p19.
134 InitBuiltinType(VoidTy, BuiltinType::Void);
135
136 // C99 6.2.5p2.
137 InitBuiltinType(BoolTy, BuiltinType::Bool);
138 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000139 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000140 InitBuiltinType(CharTy, BuiltinType::Char_S);
141 else
142 InitBuiltinType(CharTy, BuiltinType::Char_U);
143 // C99 6.2.5p4.
144 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
145 InitBuiltinType(ShortTy, BuiltinType::Short);
146 InitBuiltinType(IntTy, BuiltinType::Int);
147 InitBuiltinType(LongTy, BuiltinType::Long);
148 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
149
150 // C99 6.2.5p6.
151 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
152 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
153 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
154 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
155 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
156
157 // C99 6.2.5p10.
158 InitBuiltinType(FloatTy, BuiltinType::Float);
159 InitBuiltinType(DoubleTy, BuiltinType::Double);
160 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
161
162 // C99 6.2.5p11.
163 FloatComplexTy = getComplexType(FloatTy);
164 DoubleComplexTy = getComplexType(DoubleTy);
165 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff9d12c902007-10-15 14:41:52 +0000166
167 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000168 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000169 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000170 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000171 ClassStructType = 0;
172
Ted Kremenek42730c52008-01-07 19:49:32 +0000173 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000174
175 // void * type
176 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000177}
178
179//===----------------------------------------------------------------------===//
180// Type Sizing and Analysis
181//===----------------------------------------------------------------------===//
182
183/// getTypeSize - Return the size of the specified type, in bits. This method
184/// does not work on incomplete types.
185std::pair<uint64_t, unsigned>
Chris Lattner8cd0e932008-03-05 18:54:05 +0000186ASTContext::getTypeInfo(QualType T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000187 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000188 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000189 unsigned Align;
190 switch (T->getTypeClass()) {
191 case Type::TypeName: assert(0 && "Not a canonical type!");
192 case Type::FunctionNoProto:
193 case Type::FunctionProto:
194 default:
195 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000196 case Type::VariableArray:
197 assert(0 && "VLAs not implemented yet!");
198 case Type::ConstantArray: {
199 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
200
Chris Lattner8cd0e932008-03-05 18:54:05 +0000201 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000202 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000203 Align = EltInfo.second;
204 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000205 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000206 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000207 case Type::Vector: {
208 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000209 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000210 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Chris Lattner4b009652007-07-25 00:24:17 +0000211 // FIXME: Vector alignment is not the alignment of its elements.
212 Align = EltInfo.second;
213 break;
214 }
215
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000216 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000217 switch (cast<BuiltinType>(T)->getKind()) {
218 default: assert(0 && "Unknown builtin type!");
219 case BuiltinType::Void:
220 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000221 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000222 Width = Target.getBoolWidth();
223 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000224 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000225 case BuiltinType::Char_S:
226 case BuiltinType::Char_U:
227 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000228 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000229 Width = Target.getCharWidth();
230 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000231 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000232 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000233 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000234 Width = Target.getShortWidth();
235 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000236 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000237 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000238 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000239 Width = Target.getIntWidth();
240 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000241 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000242 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000243 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000244 Width = Target.getLongWidth();
245 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000246 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000247 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000248 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000249 Width = Target.getLongLongWidth();
250 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000251 break;
252 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000253 Width = Target.getFloatWidth();
254 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000255 break;
256 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000257 Width = Target.getDoubleWidth();
258 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000259 break;
260 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000261 Width = Target.getLongDoubleWidth();
262 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000263 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000264 }
265 break;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000266 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000267 // FIXME: Pointers into different addr spaces could have different sizes and
268 // alignment requirements: getPointerInfo should take an AddrSpace.
269 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000270 case Type::ObjCQualifiedId:
Chris Lattner1d78a862008-04-07 07:01:58 +0000271 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000272 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000273 break;
Chris Lattner461a6c52008-03-08 08:34:58 +0000274 case Type::Pointer: {
275 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000276 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000277 Align = Target.getPointerAlign(AS);
278 break;
279 }
Chris Lattner4b009652007-07-25 00:24:17 +0000280 case Type::Reference:
281 // "When applied to a reference or a reference type, the result is the size
282 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000283 // FIXME: This is wrong for struct layout: a reference in a struct has
284 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000285 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner4b009652007-07-25 00:24:17 +0000286
287 case Type::Complex: {
288 // Complex types have the same alignment as their elements, but twice the
289 // size.
290 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000291 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000292 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000293 Align = EltInfo.second;
294 break;
295 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000296 case Type::Tagged: {
297 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
298 return getTypeInfo(ET->getDecl()->getIntegerType());
299
300 RecordType *RT = cast<RecordType>(T);
301 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
302 Width = Layout.getSize();
303 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000304 break;
305 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000306 }
Chris Lattner4b009652007-07-25 00:24:17 +0000307
308 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000309 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000310}
311
Devang Patel7a78e432007-11-01 19:11:01 +0000312/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000313/// specified record (struct/union/class), which indicates its size and field
314/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000315const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000316 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
317
318 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000319 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000320 if (Entry) return *Entry;
321
Devang Patel7a78e432007-11-01 19:11:01 +0000322 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
323 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
324 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000325 Entry = NewEntry;
326
327 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
328 uint64_t RecordSize = 0;
329 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
330
331 if (D->getKind() != Decl::Union) {
Anders Carlsson7dce0292008-02-16 19:51:27 +0000332 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
333 RecordAlign = std::max(RecordAlign, AA->getAlignment());
334
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000335 bool StructIsPacked = D->getAttr<PackedAttr>();
336
Chris Lattner4b009652007-07-25 00:24:17 +0000337 // Layout each field, for now, just sequentially, respecting alignment. In
338 // the future, this will need to be tweakable by targets.
339 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
340 const FieldDecl *FD = D->getMember(i);
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000341 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
Eli Friedman67571ac2008-02-06 05:33:51 +0000342 uint64_t FieldSize;
343 unsigned FieldAlign;
Anders Carlsson058237f2008-02-18 07:13:09 +0000344
345 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
346 llvm::APSInt I(32);
347 bool BitWidthIsICE =
348 BitWidthExpr->isIntegerConstantExpr(I, *this);
349 assert (BitWidthIsICE && "Invalid BitField size expression");
350 FieldSize = I.getZExtValue();
351
Chris Lattner8cd0e932008-03-05 18:54:05 +0000352 std::pair<uint64_t, unsigned> TypeInfo = getTypeInfo(FD->getType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000353 uint64_t TypeSize = TypeInfo.first;
Anders Carlsson7dce0292008-02-16 19:51:27 +0000354
355 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
356 FieldAlign = AA->getAlignment();
357 else if (FieldIsPacked)
358 FieldAlign = 8;
359 else {
Eli Friedmanf8382542008-05-20 15:17:39 +0000360 FieldAlign = TypeInfo.second;
Anders Carlsson7dce0292008-02-16 19:51:27 +0000361 }
Eli Friedman67571ac2008-02-06 05:33:51 +0000362
Anders Carlsson058237f2008-02-18 07:13:09 +0000363 // Check if we need to add padding to give the field the correct
364 // alignment.
365 if (RecordSize % FieldAlign + FieldSize > TypeSize)
366 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
367
368 } else {
369 if (FD->getType()->isIncompleteType()) {
370 // This must be a flexible array member; we can't directly
371 // query getTypeInfo about these, so we figure it out here.
372 // Flexible array members don't have any size, but they
373 // have to be aligned appropriately for their element type.
374
375 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
376 FieldAlign = AA->getAlignment();
377 else if (FieldIsPacked)
378 FieldAlign = 8;
379 else {
380 const ArrayType* ATy = FD->getType()->getAsArrayType();
Chris Lattner8cd0e932008-03-05 18:54:05 +0000381 FieldAlign = getTypeAlign(ATy->getElementType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000382 }
383 FieldSize = 0;
384 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000385 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000386 FieldSize = FieldInfo.first;
387
388 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
389 FieldAlign = AA->getAlignment();
390 else if (FieldIsPacked)
391 FieldAlign = 8;
392 else
393 FieldAlign = FieldInfo.second;
394 }
395
396 // Round up the current record size to the field's alignment boundary.
397 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
398 }
Chris Lattner4b009652007-07-25 00:24:17 +0000399
400 // Place this field at the current location.
401 FieldOffsets[i] = RecordSize;
402
403 // Reserve space for this field.
404 RecordSize += FieldSize;
405
406 // Remember max struct/class alignment.
407 RecordAlign = std::max(RecordAlign, FieldAlign);
408 }
409
410 // Finally, round the size of the total struct up to the alignment of the
411 // struct itself.
412 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
413 } else {
414 // Union layout just puts each member at the start of the record.
415 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
416 const FieldDecl *FD = D->getMember(i);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000417 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Chris Lattner4b009652007-07-25 00:24:17 +0000418 uint64_t FieldSize = FieldInfo.first;
419 unsigned FieldAlign = FieldInfo.second;
Anders Carlsson058237f2008-02-18 07:13:09 +0000420
Chris Lattner4b009652007-07-25 00:24:17 +0000421 // Round up the current record size to the field's alignment boundary.
422 RecordSize = std::max(RecordSize, FieldSize);
Eli Friedmanf8382542008-05-20 15:17:39 +0000423
Chris Lattner4b009652007-07-25 00:24:17 +0000424 // Place this field at the start of the record.
425 FieldOffsets[i] = 0;
Eli Friedmanf8382542008-05-20 15:17:39 +0000426
Chris Lattner4b009652007-07-25 00:24:17 +0000427 // Remember max struct/class alignment.
428 RecordAlign = std::max(RecordAlign, FieldAlign);
429 }
430 }
431
432 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
433 return *NewEntry;
434}
435
Chris Lattner4b009652007-07-25 00:24:17 +0000436//===----------------------------------------------------------------------===//
437// Type creation/memoization methods
438//===----------------------------------------------------------------------===//
439
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000440QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000441 QualType CanT = getCanonicalType(T);
442 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000443 return T;
444
445 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
446 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000447 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000448 "Type is already address space qualified");
449
450 // Check if we've already instantiated an address space qual'd type of this
451 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000452 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000453 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000454 void *InsertPos = 0;
455 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
456 return QualType(ASQy, 0);
457
458 // If the base type isn't canonical, this won't be a canonical type either,
459 // so fill in the canonical type field.
460 QualType Canonical;
461 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000462 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000463
464 // Get the new insert position for the node we care about.
465 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
466 assert(NewIP == 0 && "Shouldn't be in the map!");
467 }
Chris Lattner35fef522008-02-20 20:55:12 +0000468 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000469 ASQualTypes.InsertNode(New, InsertPos);
470 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000471 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000472}
473
Chris Lattner4b009652007-07-25 00:24:17 +0000474
475/// getComplexType - Return the uniqued reference to the type for a complex
476/// number with the specified element type.
477QualType ASTContext::getComplexType(QualType T) {
478 // Unique pointers, to guarantee there is only one pointer of a particular
479 // structure.
480 llvm::FoldingSetNodeID ID;
481 ComplexType::Profile(ID, T);
482
483 void *InsertPos = 0;
484 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
485 return QualType(CT, 0);
486
487 // If the pointee type isn't canonical, this won't be a canonical type either,
488 // so fill in the canonical type field.
489 QualType Canonical;
490 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000491 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000492
493 // Get the new insert position for the node we care about.
494 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
495 assert(NewIP == 0 && "Shouldn't be in the map!");
496 }
497 ComplexType *New = new ComplexType(T, Canonical);
498 Types.push_back(New);
499 ComplexTypes.InsertNode(New, InsertPos);
500 return QualType(New, 0);
501}
502
503
504/// getPointerType - Return the uniqued reference to the type for a pointer to
505/// the specified type.
506QualType ASTContext::getPointerType(QualType T) {
507 // Unique pointers, to guarantee there is only one pointer of a particular
508 // structure.
509 llvm::FoldingSetNodeID ID;
510 PointerType::Profile(ID, T);
511
512 void *InsertPos = 0;
513 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
514 return QualType(PT, 0);
515
516 // If the pointee type isn't canonical, this won't be a canonical type either,
517 // so fill in the canonical type field.
518 QualType Canonical;
519 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000520 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000521
522 // Get the new insert position for the node we care about.
523 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
524 assert(NewIP == 0 && "Shouldn't be in the map!");
525 }
526 PointerType *New = new PointerType(T, Canonical);
527 Types.push_back(New);
528 PointerTypes.InsertNode(New, InsertPos);
529 return QualType(New, 0);
530}
531
532/// getReferenceType - Return the uniqued reference to the type for a reference
533/// to the specified type.
534QualType ASTContext::getReferenceType(QualType T) {
535 // Unique pointers, to guarantee there is only one pointer of a particular
536 // structure.
537 llvm::FoldingSetNodeID ID;
538 ReferenceType::Profile(ID, T);
539
540 void *InsertPos = 0;
541 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
542 return QualType(RT, 0);
543
544 // If the referencee type isn't canonical, this won't be a canonical type
545 // either, so fill in the canonical type field.
546 QualType Canonical;
547 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000548 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000549
550 // Get the new insert position for the node we care about.
551 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
552 assert(NewIP == 0 && "Shouldn't be in the map!");
553 }
554
555 ReferenceType *New = new ReferenceType(T, Canonical);
556 Types.push_back(New);
557 ReferenceTypes.InsertNode(New, InsertPos);
558 return QualType(New, 0);
559}
560
Steve Naroff83c13012007-08-30 01:06:46 +0000561/// getConstantArrayType - Return the unique reference to the type for an
562/// array of the specified element type.
563QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000564 const llvm::APInt &ArySize,
565 ArrayType::ArraySizeModifier ASM,
566 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000567 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000568 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000569
570 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000571 if (ConstantArrayType *ATP =
572 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000573 return QualType(ATP, 0);
574
575 // If the element type isn't canonical, this won't be a canonical type either,
576 // so fill in the canonical type field.
577 QualType Canonical;
578 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000579 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000580 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000581 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000582 ConstantArrayType *NewIP =
583 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
584
Chris Lattner4b009652007-07-25 00:24:17 +0000585 assert(NewIP == 0 && "Shouldn't be in the map!");
586 }
587
Steve Naroff24c9b982007-08-30 18:10:14 +0000588 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
589 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000590 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000591 Types.push_back(New);
592 return QualType(New, 0);
593}
594
Steve Naroffe2579e32007-08-30 18:14:25 +0000595/// getVariableArrayType - Returns a non-unique reference to the type for a
596/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000597QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
598 ArrayType::ArraySizeModifier ASM,
599 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000600 // Since we don't unique expressions, it isn't possible to unique VLA's
601 // that have an expression provided for their size.
602
603 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
604 ASM, EltTypeQuals);
605
606 VariableArrayTypes.push_back(New);
607 Types.push_back(New);
608 return QualType(New, 0);
609}
610
611QualType ASTContext::getIncompleteArrayType(QualType EltTy,
612 ArrayType::ArraySizeModifier ASM,
613 unsigned EltTypeQuals) {
614 llvm::FoldingSetNodeID ID;
615 IncompleteArrayType::Profile(ID, EltTy);
616
617 void *InsertPos = 0;
618 if (IncompleteArrayType *ATP =
619 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
620 return QualType(ATP, 0);
621
622 // If the element type isn't canonical, this won't be a canonical type
623 // either, so fill in the canonical type field.
624 QualType Canonical;
625
626 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000627 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000628 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000629
630 // Get the new insert position for the node we care about.
631 IncompleteArrayType *NewIP =
632 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
633
634 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000635 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000636
637 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
638 ASM, EltTypeQuals);
639
640 IncompleteArrayTypes.InsertNode(New, InsertPos);
641 Types.push_back(New);
642 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000643}
644
Chris Lattner4b009652007-07-25 00:24:17 +0000645/// getVectorType - Return the unique reference to a vector type of
646/// the specified element type and size. VectorType must be a built-in type.
647QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
648 BuiltinType *baseType;
649
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000650 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000651 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
652
653 // Check if we've already instantiated a vector of this type.
654 llvm::FoldingSetNodeID ID;
655 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
656 void *InsertPos = 0;
657 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
658 return QualType(VTP, 0);
659
660 // If the element type isn't canonical, this won't be a canonical type either,
661 // so fill in the canonical type field.
662 QualType Canonical;
663 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000664 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000665
666 // Get the new insert position for the node we care about.
667 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
668 assert(NewIP == 0 && "Shouldn't be in the map!");
669 }
670 VectorType *New = new VectorType(vecType, NumElts, Canonical);
671 VectorTypes.InsertNode(New, InsertPos);
672 Types.push_back(New);
673 return QualType(New, 0);
674}
675
Nate Begemanaf6ed502008-04-18 23:10:10 +0000676/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +0000677/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000678QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +0000679 BuiltinType *baseType;
680
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000681 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +0000682 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +0000683
684 // Check if we've already instantiated a vector of this type.
685 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000686 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +0000687 void *InsertPos = 0;
688 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
689 return QualType(VTP, 0);
690
691 // If the element type isn't canonical, this won't be a canonical type either,
692 // so fill in the canonical type field.
693 QualType Canonical;
694 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000695 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000696
697 // Get the new insert position for the node we care about.
698 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
699 assert(NewIP == 0 && "Shouldn't be in the map!");
700 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000701 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000702 VectorTypes.InsertNode(New, InsertPos);
703 Types.push_back(New);
704 return QualType(New, 0);
705}
706
707/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
708///
709QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
710 // Unique functions, to guarantee there is only one function of a particular
711 // structure.
712 llvm::FoldingSetNodeID ID;
713 FunctionTypeNoProto::Profile(ID, ResultTy);
714
715 void *InsertPos = 0;
716 if (FunctionTypeNoProto *FT =
717 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
718 return QualType(FT, 0);
719
720 QualType Canonical;
721 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000722 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000723
724 // Get the new insert position for the node we care about.
725 FunctionTypeNoProto *NewIP =
726 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
727 assert(NewIP == 0 && "Shouldn't be in the map!");
728 }
729
730 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
731 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000732 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000733 return QualType(New, 0);
734}
735
736/// getFunctionType - Return a normal function type with a typed argument
737/// list. isVariadic indicates whether the argument list includes '...'.
738QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
739 unsigned NumArgs, bool isVariadic) {
740 // Unique functions, to guarantee there is only one function of a particular
741 // structure.
742 llvm::FoldingSetNodeID ID;
743 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
744
745 void *InsertPos = 0;
746 if (FunctionTypeProto *FTP =
747 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
748 return QualType(FTP, 0);
749
750 // Determine whether the type being created is already canonical or not.
751 bool isCanonical = ResultTy->isCanonical();
752 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
753 if (!ArgArray[i]->isCanonical())
754 isCanonical = false;
755
756 // If this type isn't canonical, get the canonical version of it.
757 QualType Canonical;
758 if (!isCanonical) {
759 llvm::SmallVector<QualType, 16> CanonicalArgs;
760 CanonicalArgs.reserve(NumArgs);
761 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000762 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +0000763
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000764 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +0000765 &CanonicalArgs[0], NumArgs,
766 isVariadic);
767
768 // Get the new insert position for the node we care about.
769 FunctionTypeProto *NewIP =
770 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
771 assert(NewIP == 0 && "Shouldn't be in the map!");
772 }
773
774 // FunctionTypeProto objects are not allocated with new because they have a
775 // variable size array (for parameter types) at the end of them.
776 FunctionTypeProto *FTP =
777 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
778 NumArgs*sizeof(QualType));
779 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
780 Canonical);
781 Types.push_back(FTP);
782 FunctionTypeProtos.InsertNode(FTP, InsertPos);
783 return QualType(FTP, 0);
784}
785
Douglas Gregor1d661552008-04-13 21:07:44 +0000786/// getTypeDeclType - Return the unique reference to the type for the
787/// specified type declaration.
788QualType ASTContext::getTypeDeclType(TypeDecl *Decl) {
789 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
790
791 if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(Decl))
792 return getTypedefType(Typedef);
793 else if (ObjCInterfaceDecl *ObjCInterface
794 = dyn_cast_or_null<ObjCInterfaceDecl>(Decl))
795 return getObjCInterfaceType(ObjCInterface);
796 else if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Decl)) {
797 Decl->TypeForDecl = new RecordType(Record);
798 Types.push_back(Decl->TypeForDecl);
799 return QualType(Decl->TypeForDecl, 0);
800 } else if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Decl)) {
801 Decl->TypeForDecl = new EnumType(Enum);
802 Types.push_back(Decl->TypeForDecl);
803 return QualType(Decl->TypeForDecl, 0);
804 } else
805 assert(false && "TypeDecl without a type?");
806}
807
Chris Lattner4b009652007-07-25 00:24:17 +0000808/// getTypedefType - Return the unique reference to the type for the
809/// specified typename decl.
810QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
811 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
812
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000813 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000814 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000815 Types.push_back(Decl->TypeForDecl);
816 return QualType(Decl->TypeForDecl, 0);
817}
818
Ted Kremenek42730c52008-01-07 19:49:32 +0000819/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000820/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000821QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000822 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
823
Ted Kremenek42730c52008-01-07 19:49:32 +0000824 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000825 Types.push_back(Decl->TypeForDecl);
826 return QualType(Decl->TypeForDecl, 0);
827}
828
Chris Lattnere1352302008-04-07 04:56:42 +0000829/// CmpProtocolNames - Comparison predicate for sorting protocols
830/// alphabetically.
831static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
832 const ObjCProtocolDecl *RHS) {
833 return strcmp(LHS->getName(), RHS->getName()) < 0;
834}
835
836static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
837 unsigned &NumProtocols) {
838 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
839
840 // Sort protocols, keyed by name.
841 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
842
843 // Remove duplicates.
844 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
845 NumProtocols = ProtocolsEnd-Protocols;
846}
847
848
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +0000849/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
850/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000851QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
852 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000853 // Sort the protocol list alphabetically to canonicalize it.
854 SortAndUniqueProtocols(Protocols, NumProtocols);
855
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000856 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +0000857 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000858
859 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000860 if (ObjCQualifiedInterfaceType *QT =
861 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000862 return QualType(QT, 0);
863
864 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +0000865 ObjCQualifiedInterfaceType *QType =
866 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000867 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000868 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000869 return QualType(QType, 0);
870}
871
Chris Lattnere1352302008-04-07 04:56:42 +0000872/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
873/// and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000874QualType ASTContext::getObjCQualifiedIdType(QualType idType,
875 ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000876 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000877 // Sort the protocol list alphabetically to canonicalize it.
878 SortAndUniqueProtocols(Protocols, NumProtocols);
879
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000880 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000881 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000882
883 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000884 if (ObjCQualifiedIdType *QT =
885 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000886 return QualType(QT, 0);
887
888 // No Match;
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000889 QualType Canonical;
890 if (!idType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000891 Canonical = getObjCQualifiedIdType(getCanonicalType(idType),
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000892 Protocols, NumProtocols);
Ted Kremenek42730c52008-01-07 19:49:32 +0000893 ObjCQualifiedIdType *NewQT =
894 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000895 assert(NewQT == 0 && "Shouldn't be in the map!");
896 }
897
Ted Kremenek42730c52008-01-07 19:49:32 +0000898 ObjCQualifiedIdType *QType =
899 new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000900 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000901 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000902 return QualType(QType, 0);
903}
904
Steve Naroff0604dd92007-08-01 18:02:17 +0000905/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
906/// TypeOfExpr AST's (since expression's are never shared). For example,
907/// multiple declarations that refer to "typeof(x)" all contain different
908/// DeclRefExpr's. This doesn't effect the type checker, since it operates
909/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000910QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000911 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff0604dd92007-08-01 18:02:17 +0000912 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
913 Types.push_back(toe);
914 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000915}
916
Steve Naroff0604dd92007-08-01 18:02:17 +0000917/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
918/// TypeOfType AST's. The only motivation to unique these nodes would be
919/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
920/// an issue. This doesn't effect the type checker, since it operates
921/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000922QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000923 QualType Canonical = getCanonicalType(tofType);
Steve Naroff0604dd92007-08-01 18:02:17 +0000924 TypeOfType *tot = new TypeOfType(tofType, Canonical);
925 Types.push_back(tot);
926 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000927}
928
Chris Lattner4b009652007-07-25 00:24:17 +0000929/// getTagDeclType - Return the unique reference to the type for the
930/// specified TagDecl (struct/union/class/enum) decl.
931QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +0000932 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +0000933 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +0000934}
935
936/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
937/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
938/// needs to agree with the definition in <stddef.h>.
939QualType ASTContext::getSizeType() const {
940 // On Darwin, size_t is defined as a "long unsigned int".
941 // FIXME: should derive from "Target".
942 return UnsignedLongTy;
943}
944
Eli Friedmanfdd35d72008-02-12 08:29:21 +0000945/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
946/// width of characters in wide strings, The value is target dependent and
947/// needs to agree with the definition in <stddef.h>.
948QualType ASTContext::getWcharType() const {
949 // On Darwin, wchar_t is defined as a "int".
950 // FIXME: should derive from "Target".
951 return IntTy;
952}
953
Chris Lattner4b009652007-07-25 00:24:17 +0000954/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
955/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
956QualType ASTContext::getPointerDiffType() const {
957 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
958 // FIXME: should derive from "Target".
959 return IntTy;
960}
961
Chris Lattner19eb97e2008-04-02 05:18:44 +0000962//===----------------------------------------------------------------------===//
963// Type Operators
964//===----------------------------------------------------------------------===//
965
Chris Lattner3dae6f42008-04-06 22:41:35 +0000966/// getCanonicalType - Return the canonical (structural) type corresponding to
967/// the specified potentially non-canonical type. The non-canonical version
968/// of a type may have many "decorated" versions of types. Decorators can
969/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
970/// to be free of any of these, allowing two canonical types to be compared
971/// for exact equality with a simple pointer comparison.
972QualType ASTContext::getCanonicalType(QualType T) {
973 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
974 return QualType(CanType.getTypePtr(),
975 T.getCVRQualifiers() | CanType.getCVRQualifiers());
976}
977
978
Chris Lattner19eb97e2008-04-02 05:18:44 +0000979/// getArrayDecayedType - Return the properly qualified result of decaying the
980/// specified array type to a pointer. This operation is non-trivial when
981/// handling typedefs etc. The canonical type of "T" must be an array type,
982/// this returns a pointer to a properly qualified element of the array.
983///
984/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
985QualType ASTContext::getArrayDecayedType(QualType Ty) {
986 // Handle the common case where typedefs are not involved directly.
987 QualType EltTy;
988 unsigned ArrayQuals = 0;
989 unsigned PointerQuals = 0;
990 if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
991 // Since T "isa" an array type, it could not have had an address space
992 // qualifier, just CVR qualifiers. The properly qualified element pointer
993 // gets the union of the CVR qualifiers from the element and the array, and
994 // keeps any address space qualifier on the element type if present.
995 EltTy = AT->getElementType();
996 ArrayQuals = Ty.getCVRQualifiers();
997 PointerQuals = AT->getIndexTypeQualifier();
998 } else {
999 // Otherwise, we have an ASQualType or a typedef, etc. Make sure we don't
1000 // lose qualifiers when dealing with typedefs. Example:
1001 // typedef int arr[10];
1002 // void test2() {
1003 // const arr b;
1004 // b[4] = 1;
1005 // }
1006 //
1007 // The decayed type of b is "const int*" even though the element type of the
1008 // array is "int".
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001009 QualType CanTy = getCanonicalType(Ty);
Chris Lattner19eb97e2008-04-02 05:18:44 +00001010 const ArrayType *PrettyArrayType = Ty->getAsArrayType();
1011 assert(PrettyArrayType && "Not an array type!");
1012
1013 // Get the element type with 'getAsArrayType' so that we don't lose any
1014 // typedefs in the element type of the array.
1015 EltTy = PrettyArrayType->getElementType();
1016
1017 // If the array was address-space qualifier, make sure to ASQual the element
1018 // type. We can just grab the address space from the canonical type.
1019 if (unsigned AS = CanTy.getAddressSpace())
1020 EltTy = getASQualType(EltTy, AS);
1021
1022 // To properly handle [multiple levels of] typedefs, typeof's etc, we take
1023 // the CVR qualifiers directly from the canonical type, which is guaranteed
1024 // to have the full set unioned together.
1025 ArrayQuals = CanTy.getCVRQualifiers();
1026 PointerQuals = PrettyArrayType->getIndexTypeQualifier();
1027 }
1028
Chris Lattnerda79b3f2008-04-02 06:06:35 +00001029 // Apply any CVR qualifiers from the array type to the element type. This
1030 // implements C99 6.7.3p8: "If the specification of an array type includes
1031 // any type qualifiers, the element type is so qualified, not the array type."
Chris Lattner19eb97e2008-04-02 05:18:44 +00001032 EltTy = EltTy.getQualifiedType(ArrayQuals | EltTy.getCVRQualifiers());
1033
1034 QualType PtrTy = getPointerType(EltTy);
1035
1036 // int x[restrict 4] -> int *restrict
1037 PtrTy = PtrTy.getQualifiedType(PointerQuals);
1038
1039 return PtrTy;
1040}
1041
Chris Lattner4b009652007-07-25 00:24:17 +00001042/// getFloatingRank - Return a relative rank for floating point types.
1043/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001044static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001045 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001046 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001047
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001048 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001049 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001050 case BuiltinType::Float: return FloatRank;
1051 case BuiltinType::Double: return DoubleRank;
1052 case BuiltinType::LongDouble: return LongDoubleRank;
1053 }
1054}
1055
Steve Narofffa0c4532007-08-27 01:41:48 +00001056/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1057/// point or a complex type (based on typeDomain/typeSize).
1058/// 'typeDomain' is a real floating point or complex type.
1059/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001060QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1061 QualType Domain) const {
1062 FloatingRank EltRank = getFloatingRank(Size);
1063 if (Domain->isComplexType()) {
1064 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001065 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001066 case FloatRank: return FloatComplexTy;
1067 case DoubleRank: return DoubleComplexTy;
1068 case LongDoubleRank: return LongDoubleComplexTy;
1069 }
Chris Lattner4b009652007-07-25 00:24:17 +00001070 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001071
1072 assert(Domain->isRealFloatingType() && "Unknown domain!");
1073 switch (EltRank) {
1074 default: assert(0 && "getFloatingRank(): illegal value for rank");
1075 case FloatRank: return FloatTy;
1076 case DoubleRank: return DoubleTy;
1077 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001078 }
Chris Lattner4b009652007-07-25 00:24:17 +00001079}
1080
Chris Lattner51285d82008-04-06 23:55:33 +00001081/// getFloatingTypeOrder - Compare the rank of the two specified floating
1082/// point types, ignoring the domain of the type (i.e. 'double' ==
1083/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1084/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001085int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1086 FloatingRank LHSR = getFloatingRank(LHS);
1087 FloatingRank RHSR = getFloatingRank(RHS);
1088
1089 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001090 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001091 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001092 return 1;
1093 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001094}
1095
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001096/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1097/// routine will assert if passed a built-in type that isn't an integer or enum,
1098/// or if it is not canonicalized.
1099static unsigned getIntegerRank(Type *T) {
1100 assert(T->isCanonical() && "T should be canonicalized");
1101 if (isa<EnumType>(T))
1102 return 4;
1103
1104 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001105 default: assert(0 && "getIntegerRank(): not a built-in integer");
1106 case BuiltinType::Bool:
1107 return 1;
1108 case BuiltinType::Char_S:
1109 case BuiltinType::Char_U:
1110 case BuiltinType::SChar:
1111 case BuiltinType::UChar:
1112 return 2;
1113 case BuiltinType::Short:
1114 case BuiltinType::UShort:
1115 return 3;
1116 case BuiltinType::Int:
1117 case BuiltinType::UInt:
1118 return 4;
1119 case BuiltinType::Long:
1120 case BuiltinType::ULong:
1121 return 5;
1122 case BuiltinType::LongLong:
1123 case BuiltinType::ULongLong:
1124 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001125 }
1126}
1127
Chris Lattner51285d82008-04-06 23:55:33 +00001128/// getIntegerTypeOrder - Returns the highest ranked integer type:
1129/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1130/// LHS < RHS, return -1.
1131int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001132 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1133 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001134 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001135
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001136 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1137 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001138
Chris Lattner51285d82008-04-06 23:55:33 +00001139 unsigned LHSRank = getIntegerRank(LHSC);
1140 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001141
Chris Lattner51285d82008-04-06 23:55:33 +00001142 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1143 if (LHSRank == RHSRank) return 0;
1144 return LHSRank > RHSRank ? 1 : -1;
1145 }
Chris Lattner4b009652007-07-25 00:24:17 +00001146
Chris Lattner51285d82008-04-06 23:55:33 +00001147 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1148 if (LHSUnsigned) {
1149 // If the unsigned [LHS] type is larger, return it.
1150 if (LHSRank >= RHSRank)
1151 return 1;
1152
1153 // If the signed type can represent all values of the unsigned type, it
1154 // wins. Because we are dealing with 2's complement and types that are
1155 // powers of two larger than each other, this is always safe.
1156 return -1;
1157 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001158
Chris Lattner51285d82008-04-06 23:55:33 +00001159 // If the unsigned [RHS] type is larger, return it.
1160 if (RHSRank >= LHSRank)
1161 return -1;
1162
1163 // If the signed type can represent all values of the unsigned type, it
1164 // wins. Because we are dealing with 2's complement and types that are
1165 // powers of two larger than each other, this is always safe.
1166 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001167}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001168
1169// getCFConstantStringType - Return the type used for constant CFStrings.
1170QualType ASTContext::getCFConstantStringType() {
1171 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001172 CFConstantStringTypeDecl =
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001173 RecordDecl::Create(*this, Decl::Struct, TUDecl, SourceLocation(),
Chris Lattner58114f02008-03-15 21:32:50 +00001174 &Idents.get("NSConstantString"), 0);
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001175 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001176
1177 // const int *isa;
1178 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001179 // int flags;
1180 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001181 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001182 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001183 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001184 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001185 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001186 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001187
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001188 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerf3874bc2008-04-06 04:47:34 +00001189 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner81db64a2008-03-16 00:16:02 +00001190 FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001191
1192 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1193 }
1194
1195 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001196}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001197
Anders Carlssone3f02572007-10-29 06:33:42 +00001198// This returns true if a type has been typedefed to BOOL:
1199// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001200static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001201 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001202 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001203
1204 return false;
1205}
1206
Ted Kremenek42730c52008-01-07 19:49:32 +00001207/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001208/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001209int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001210 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001211
1212 // Make all integer and enum types at least as large as an int
1213 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001214 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001215 // Treat arrays as pointers, since that's how they're passed in.
1216 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001217 sz = getTypeSize(VoidPtrTy);
1218 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001219}
1220
Ted Kremenek42730c52008-01-07 19:49:32 +00001221/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001222/// declaration.
Ted Kremenek42730c52008-01-07 19:49:32 +00001223void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001224 std::string& S)
1225{
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001226 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001227 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001228 // Encode result type.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001229 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001230 // Compute size of all parameters.
1231 // Start with computing size of a pointer in number of bytes.
1232 // FIXME: There might(should) be a better way of doing this computation!
1233 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001234 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001235 // The first two arguments (self and _cmd) are pointers; account for
1236 // their size.
1237 int ParmOffset = 2 * PtrSize;
1238 int NumOfParams = Decl->getNumParams();
1239 for (int i = 0; i < NumOfParams; i++) {
1240 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001241 int sz = getObjCEncodingTypeSize (PType);
1242 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001243 ParmOffset += sz;
1244 }
1245 S += llvm::utostr(ParmOffset);
1246 S += "@0:";
1247 S += llvm::utostr(PtrSize);
1248
1249 // Argument types.
1250 ParmOffset = 2 * PtrSize;
1251 for (int i = 0; i < NumOfParams; i++) {
1252 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001253 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001254 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001255 getObjCEncodingForTypeQualifier(
1256 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian248db262008-01-22 22:44:46 +00001257 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001258 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001259 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001260 }
1261}
1262
Fariborz Jahanian248db262008-01-22 22:44:46 +00001263void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1264 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson36f07d82007-10-29 05:01:08 +00001265{
Anders Carlssone3f02572007-10-29 06:33:42 +00001266 // FIXME: This currently doesn't encode:
1267 // @ An object (whether statically typed or typed id)
1268 // # A class object (Class)
1269 // : A method selector (SEL)
1270 // {name=type...} A structure
1271 // (name=type...) A union
1272 // bnum A bit field of num bits
1273
1274 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001275 char encoding;
1276 switch (BT->getKind()) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001277 default: assert(0 && "Unhandled builtin type kind");
1278 case BuiltinType::Void: encoding = 'v'; break;
1279 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001280 case BuiltinType::Char_U:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001281 case BuiltinType::UChar: encoding = 'C'; break;
1282 case BuiltinType::UShort: encoding = 'S'; break;
1283 case BuiltinType::UInt: encoding = 'I'; break;
1284 case BuiltinType::ULong: encoding = 'L'; break;
1285 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001286 case BuiltinType::Char_S:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001287 case BuiltinType::SChar: encoding = 'c'; break;
1288 case BuiltinType::Short: encoding = 's'; break;
1289 case BuiltinType::Int: encoding = 'i'; break;
1290 case BuiltinType::Long: encoding = 'l'; break;
1291 case BuiltinType::LongLong: encoding = 'q'; break;
1292 case BuiltinType::Float: encoding = 'f'; break;
1293 case BuiltinType::Double: encoding = 'd'; break;
1294 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001295 }
1296
1297 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001298 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001299 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001300 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001301 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001302
1303 }
1304 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001305 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001306 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001307 S += '@';
1308 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001309 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001310 S += '#';
1311 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001312 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001313 S += ':';
1314 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001315 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001316
1317 if (PointeeTy->isCharType()) {
1318 // char pointer types should be encoded as '*' unless it is a
1319 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001320 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001321 S += '*';
1322 return;
1323 }
1324 }
1325
1326 S += '^';
Fariborz Jahanian248db262008-01-22 22:44:46 +00001327 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone3f02572007-10-29 06:33:42 +00001328 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001329 S += '[';
1330
1331 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1332 S += llvm::utostr(CAT->getSize().getZExtValue());
1333 else
1334 assert(0 && "Unhandled array type!");
1335
Fariborz Jahanian248db262008-01-22 22:44:46 +00001336 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001337 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001338 } else if (T->getAsFunctionType()) {
1339 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001340 } else if (const RecordType *RTy = T->getAsRecordType()) {
1341 RecordDecl *RDecl= RTy->getDecl();
1342 S += '{';
1343 S += RDecl->getName();
Fariborz Jahanian248db262008-01-22 22:44:46 +00001344 bool found = false;
1345 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1346 if (ERType[i] == RTy) {
1347 found = true;
1348 break;
1349 }
1350 if (!found) {
1351 ERType.push_back(RTy);
1352 S += '=';
1353 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1354 FieldDecl *field = RDecl->getMember(i);
1355 getObjCEncodingForType(field->getType(), S, ERType);
1356 }
1357 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1358 ERType.pop_back();
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001359 }
1360 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001361 } else if (T->isEnumeralType()) {
1362 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001363 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001364 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001365}
1366
Ted Kremenek42730c52008-01-07 19:49:32 +00001367void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001368 std::string& S) const {
1369 if (QT & Decl::OBJC_TQ_In)
1370 S += 'n';
1371 if (QT & Decl::OBJC_TQ_Inout)
1372 S += 'N';
1373 if (QT & Decl::OBJC_TQ_Out)
1374 S += 'o';
1375 if (QT & Decl::OBJC_TQ_Bycopy)
1376 S += 'O';
1377 if (QT & Decl::OBJC_TQ_Byref)
1378 S += 'R';
1379 if (QT & Decl::OBJC_TQ_Oneway)
1380 S += 'V';
1381}
1382
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001383void ASTContext::setBuiltinVaListType(QualType T)
1384{
1385 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1386
1387 BuiltinVaListType = T;
1388}
1389
Ted Kremenek42730c52008-01-07 19:49:32 +00001390void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001391{
Ted Kremenek42730c52008-01-07 19:49:32 +00001392 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff9d12c902007-10-15 14:41:52 +00001393
Ted Kremenek42730c52008-01-07 19:49:32 +00001394 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001395
1396 // typedef struct objc_object *id;
1397 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1398 assert(ptr && "'id' incorrectly typed");
1399 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1400 assert(rec && "'id' incorrectly typed");
1401 IdStructType = rec;
1402}
1403
Ted Kremenek42730c52008-01-07 19:49:32 +00001404void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001405{
Ted Kremenek42730c52008-01-07 19:49:32 +00001406 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001407
Ted Kremenek42730c52008-01-07 19:49:32 +00001408 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001409
1410 // typedef struct objc_selector *SEL;
1411 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1412 assert(ptr && "'SEL' incorrectly typed");
1413 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1414 assert(rec && "'SEL' incorrectly typed");
1415 SelStructType = rec;
1416}
1417
Ted Kremenek42730c52008-01-07 19:49:32 +00001418void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001419{
Ted Kremenek42730c52008-01-07 19:49:32 +00001420 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1421 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001422}
1423
Ted Kremenek42730c52008-01-07 19:49:32 +00001424void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001425{
Ted Kremenek42730c52008-01-07 19:49:32 +00001426 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001427
Ted Kremenek42730c52008-01-07 19:49:32 +00001428 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001429
1430 // typedef struct objc_class *Class;
1431 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1432 assert(ptr && "'Class' incorrectly typed");
1433 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1434 assert(rec && "'Class' incorrectly typed");
1435 ClassStructType = rec;
1436}
1437
Ted Kremenek42730c52008-01-07 19:49:32 +00001438void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1439 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001440 "'NSConstantString' type already set!");
1441
Ted Kremenek42730c52008-01-07 19:49:32 +00001442 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001443}
1444
Chris Lattner6ff358b2008-04-07 06:51:04 +00001445//===----------------------------------------------------------------------===//
1446// Type Compatibility Testing
1447//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00001448
Chris Lattner390564e2008-04-07 06:49:41 +00001449/// C99 6.2.7p1: If both are complete types, then the following additional
1450/// requirements apply.
1451/// FIXME (handle compatibility across source files).
1452static bool areCompatTagTypes(TagType *LHS, TagType *RHS,
1453 const ASTContext &C) {
Steve Naroff4a5e2072007-11-07 06:03:51 +00001454 // "Class" and "id" are compatible built-in structure types.
Chris Lattner390564e2008-04-07 06:49:41 +00001455 if (C.isObjCIdType(QualType(LHS, 0)) && C.isObjCClassType(QualType(RHS, 0)) ||
1456 C.isObjCClassType(QualType(LHS, 0)) && C.isObjCIdType(QualType(RHS, 0)))
Steve Naroff4a5e2072007-11-07 06:03:51 +00001457 return true;
Eli Friedmane7fb03a2008-02-15 06:03:44 +00001458
Chris Lattner390564e2008-04-07 06:49:41 +00001459 // Within a translation unit a tag type is only compatible with itself. Self
1460 // equality is already handled by the time we get here.
1461 assert(LHS != RHS && "Self equality not handled!");
1462 return false;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001463}
1464
1465bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1466 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1467 // identically qualified and both shall be pointers to compatible types.
Chris Lattner35fef522008-02-20 20:55:12 +00001468 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1469 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001470 return false;
1471
1472 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1473 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1474
1475 return typesAreCompatible(ltype, rtype);
1476}
1477
Steve Naroff85f0dc52007-10-15 20:41:53 +00001478bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1479 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1480 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1481 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1482 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1483
1484 // first check the return types (common between C99 and K&R).
1485 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1486 return false;
1487
1488 if (lproto && rproto) { // two C99 style function prototypes
1489 unsigned lproto_nargs = lproto->getNumArgs();
1490 unsigned rproto_nargs = rproto->getNumArgs();
1491
1492 if (lproto_nargs != rproto_nargs)
1493 return false;
1494
1495 // both prototypes have the same number of arguments.
1496 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1497 (rproto->isVariadic() && !lproto->isVariadic()))
1498 return false;
1499
1500 // The use of ellipsis agree...now check the argument types.
1501 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001502 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1503 // is taken as having the unqualified version of it's declared type.
Steve Naroffdec17fe2008-01-29 00:15:50 +00001504 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001505 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001506 return false;
1507 return true;
1508 }
Chris Lattner1d78a862008-04-07 07:01:58 +00001509
Steve Naroff85f0dc52007-10-15 20:41:53 +00001510 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1511 return true;
1512
1513 // we have a mixture of K&R style with C99 prototypes
1514 const FunctionTypeProto *proto = lproto ? lproto : rproto;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001515 if (proto->isVariadic())
1516 return false;
1517
1518 // FIXME: Each parameter type T in the prototype must be compatible with the
1519 // type resulting from applying the usual argument conversions to T.
1520 return true;
1521}
1522
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001523// C99 6.7.5.2p6
1524static bool areCompatArrayTypes(ArrayType *LHS, ArrayType *RHS, ASTContext &C) {
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001525 // Constant arrays must be the same size to be compatible.
1526 if (const ConstantArrayType* LCAT = dyn_cast<ConstantArrayType>(LHS))
1527 if (const ConstantArrayType* RCAT = dyn_cast<ConstantArrayType>(RHS))
1528 if (RCAT->getSize() != LCAT->getSize())
1529 return false;
Eli Friedman1e7537832008-02-06 04:53:22 +00001530
Chris Lattnerc8971d72008-04-07 06:58:21 +00001531 // Compatible arrays must have compatible element types
1532 return C.typesAreCompatible(LHS->getElementType(), RHS->getElementType());
Steve Naroff85f0dc52007-10-15 20:41:53 +00001533}
1534
Chris Lattner6ff358b2008-04-07 06:51:04 +00001535/// areCompatVectorTypes - Return true if the two specified vector types are
1536/// compatible.
1537static bool areCompatVectorTypes(const VectorType *LHS,
1538 const VectorType *RHS) {
1539 assert(LHS->isCanonical() && RHS->isCanonical());
1540 return LHS->getElementType() == RHS->getElementType() &&
1541 LHS->getNumElements() == RHS->getNumElements();
1542}
1543
1544/// areCompatObjCInterfaces - Return true if the two interface types are
1545/// compatible for assignment from RHS to LHS. This handles validation of any
1546/// protocol qualifiers on the LHS or RHS.
1547///
Chris Lattner1d78a862008-04-07 07:01:58 +00001548static bool areCompatObjCInterfaces(const ObjCInterfaceType *LHS,
1549 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00001550 // Verify that the base decls are compatible: the RHS must be a subclass of
1551 // the LHS.
1552 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1553 return false;
1554
1555 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1556 // protocol qualified at all, then we are good.
1557 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1558 return true;
1559
1560 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1561 // isn't a superset.
1562 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1563 return true; // FIXME: should return false!
1564
1565 // Finally, we must have two protocol-qualified interfaces.
1566 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1567 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1568 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1569 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1570 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1571 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1572
1573 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1574 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1575 // LHS in a single parallel scan until we run out of elements in LHS.
1576 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1577 ObjCProtocolDecl *LHSProto = *LHSPI;
1578
1579 while (RHSPI != RHSPE) {
1580 ObjCProtocolDecl *RHSProto = *RHSPI++;
1581 // If the RHS has a protocol that the LHS doesn't, ignore it.
1582 if (RHSProto != LHSProto)
1583 continue;
1584
1585 // Otherwise, the RHS does have this element.
1586 ++LHSPI;
1587 if (LHSPI == LHSPE)
1588 return true; // All protocols in LHS exist in RHS.
1589
1590 LHSProto = *LHSPI;
1591 }
1592
1593 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1594 return false;
1595}
1596
1597
Steve Naroff85f0dc52007-10-15 20:41:53 +00001598/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1599/// both shall have the identically qualified version of a compatible type.
1600/// C99 6.2.7p1: Two types have compatible types if their types are the
1601/// same. See 6.7.[2,3,5] for additional rules.
Chris Lattner855fed42008-04-07 04:07:56 +00001602bool ASTContext::typesAreCompatible(QualType LHS_NC, QualType RHS_NC) {
1603 QualType LHS = LHS_NC.getCanonicalType();
1604 QualType RHS = RHS_NC.getCanonicalType();
Chris Lattner4d5670b2008-04-03 05:07:04 +00001605
Bill Wendling6a9d8542007-12-03 07:33:35 +00001606 // C++ [expr]: If an expression initially has the type "reference to T", the
1607 // type is adjusted to "T" prior to any further analysis, the expression
1608 // designates the object or function denoted by the reference, and the
1609 // expression is an lvalue.
Chris Lattner855fed42008-04-07 04:07:56 +00001610 if (ReferenceType *RT = dyn_cast<ReferenceType>(LHS))
1611 LHS = RT->getPointeeType();
1612 if (ReferenceType *RT = dyn_cast<ReferenceType>(RHS))
1613 RHS = RT->getPointeeType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001614
Chris Lattnerd47d6042008-04-07 05:37:56 +00001615 // If two types are identical, they are compatible.
1616 if (LHS == RHS)
1617 return true;
1618
1619 // If qualifiers differ, the types are different.
Chris Lattnerb5709e22008-04-07 05:43:21 +00001620 unsigned LHSAS = LHS.getAddressSpace(), RHSAS = RHS.getAddressSpace();
1621 if (LHS.getCVRQualifiers() != RHS.getCVRQualifiers() || LHSAS != RHSAS)
Chris Lattnerd47d6042008-04-07 05:37:56 +00001622 return false;
Chris Lattnerb5709e22008-04-07 05:43:21 +00001623
1624 // Strip off ASQual's if present.
1625 if (LHSAS) {
1626 LHS = LHS.getUnqualifiedType();
1627 RHS = RHS.getUnqualifiedType();
1628 }
Chris Lattnerd47d6042008-04-07 05:37:56 +00001629
Chris Lattner855fed42008-04-07 04:07:56 +00001630 Type::TypeClass LHSClass = LHS->getTypeClass();
1631 Type::TypeClass RHSClass = RHS->getTypeClass();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001632
1633 // We want to consider the two function types to be the same for these
1634 // comparisons, just force one to the other.
1635 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1636 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00001637
1638 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00001639 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
1640 LHSClass = Type::ConstantArray;
1641 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
1642 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001643
Nate Begemanaf6ed502008-04-18 23:10:10 +00001644 // Canonicalize ExtVector -> Vector.
1645 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
1646 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00001647
Chris Lattner7cdcb252008-04-07 06:38:24 +00001648 // Consider qualified interfaces and interfaces the same.
1649 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
1650 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
1651
Chris Lattnerb5709e22008-04-07 05:43:21 +00001652 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001653 if (LHSClass != RHSClass) {
Chris Lattner7cdcb252008-04-07 06:38:24 +00001654 // ID is compatible with all interface types.
1655 if (isa<ObjCInterfaceType>(LHS))
1656 return isObjCIdType(RHS);
1657 if (isa<ObjCInterfaceType>(RHS))
1658 return isObjCIdType(LHS);
Chris Lattner0d3e6452008-04-07 05:53:18 +00001659
Chris Lattnerc38d4522008-01-14 05:45:46 +00001660 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1661 // a signed integer type, or an unsigned integer type.
Chris Lattner855fed42008-04-07 04:07:56 +00001662 if (LHS->isEnumeralType() && RHS->isIntegralType()) {
1663 EnumDecl* EDecl = cast<EnumType>(LHS)->getDecl();
1664 return EDecl->getIntegerType() == RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001665 }
Chris Lattner855fed42008-04-07 04:07:56 +00001666 if (RHS->isEnumeralType() && LHS->isIntegralType()) {
1667 EnumDecl* EDecl = cast<EnumType>(RHS)->getDecl();
1668 return EDecl->getIntegerType() == LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001669 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001670
Steve Naroff85f0dc52007-10-15 20:41:53 +00001671 return false;
1672 }
Chris Lattnerb5709e22008-04-07 05:43:21 +00001673
Steve Naroffc88babe2008-01-09 22:43:08 +00001674 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001675 switch (LHSClass) {
Chris Lattnerb5709e22008-04-07 05:43:21 +00001676 case Type::ASQual:
1677 case Type::FunctionProto:
1678 case Type::VariableArray:
1679 case Type::IncompleteArray:
1680 case Type::Reference:
Chris Lattner7cdcb252008-04-07 06:38:24 +00001681 case Type::ObjCQualifiedInterface:
Chris Lattnerb5709e22008-04-07 05:43:21 +00001682 assert(0 && "Canonicalized away above");
Chris Lattnerc38d4522008-01-14 05:45:46 +00001683 case Type::Pointer:
Chris Lattner855fed42008-04-07 04:07:56 +00001684 return pointerTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001685 case Type::ConstantArray:
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001686 return areCompatArrayTypes(cast<ArrayType>(LHS), cast<ArrayType>(RHS),
1687 *this);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001688 case Type::FunctionNoProto:
Chris Lattner855fed42008-04-07 04:07:56 +00001689 return functionTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001690 case Type::Tagged: // handle structures, unions
Chris Lattner390564e2008-04-07 06:49:41 +00001691 return areCompatTagTypes(cast<TagType>(LHS), cast<TagType>(RHS), *this);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001692 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00001693 // Only exactly equal builtin types are compatible, which is tested above.
1694 return false;
1695 case Type::Vector:
1696 return areCompatVectorTypes(cast<VectorType>(LHS), cast<VectorType>(RHS));
Chris Lattnerc38d4522008-01-14 05:45:46 +00001697 case Type::ObjCInterface:
Chris Lattner7cdcb252008-04-07 06:38:24 +00001698 return areCompatObjCInterfaces(cast<ObjCInterfaceType>(LHS),
1699 cast<ObjCInterfaceType>(RHS));
Chris Lattnerc38d4522008-01-14 05:45:46 +00001700 default:
1701 assert(0 && "unexpected type");
Steve Naroff85f0dc52007-10-15 20:41:53 +00001702 }
1703 return true; // should never get here...
1704}
Ted Kremenek738e6c02007-10-31 17:10:13 +00001705
Chris Lattner1d78a862008-04-07 07:01:58 +00001706//===----------------------------------------------------------------------===//
1707// Serialization Support
1708//===----------------------------------------------------------------------===//
1709
Ted Kremenek738e6c02007-10-31 17:10:13 +00001710/// Emit - Serialize an ASTContext object to Bitcode.
1711void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00001712 S.EmitRef(SourceMgr);
1713 S.EmitRef(Target);
1714 S.EmitRef(Idents);
1715 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001716
Ted Kremenek68228a92007-10-31 22:44:07 +00001717 // Emit the size of the type vector so that we can reserve that size
1718 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001719 S.EmitInt(Types.size());
1720
Ted Kremenek034a78c2007-11-13 22:02:55 +00001721 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1722 I!=E;++I)
1723 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001724
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001725 S.EmitOwnedPtr(TUDecl);
1726
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001727 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001728}
1729
Ted Kremenekacba3612007-11-13 00:25:37 +00001730ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek68228a92007-10-31 22:44:07 +00001731 SourceManager &SM = D.ReadRef<SourceManager>();
1732 TargetInfo &t = D.ReadRef<TargetInfo>();
1733 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1734 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00001735
Ted Kremenek68228a92007-10-31 22:44:07 +00001736 unsigned size_reserve = D.ReadInt();
1737
1738 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1739
Ted Kremenek034a78c2007-11-13 22:02:55 +00001740 for (unsigned i = 0; i < size_reserve; ++i)
1741 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00001742
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001743 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
1744
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001745 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00001746
1747 return A;
1748}