blob: 3165ad065b26b7007e32044854f8e06eed701a5a [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Basic/TargetInfo.h"
18#include "llvm/ADT/SmallVector.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000019#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000020#include "llvm/Bitcode/Serialize.h"
21#include "llvm/Bitcode/Deserialize.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000022
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24
25enum FloatingRank {
26 FloatRank, DoubleRank, LongDoubleRank
27};
28
29ASTContext::~ASTContext() {
30 // Deallocate all the types.
31 while (!Types.empty()) {
32 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) {
33 // Destroy the object, but don't call delete. These are malloc'd.
34 FT->~FunctionTypeProto();
35 free(FT);
36 } else {
37 delete Types.back();
38 }
39 Types.pop_back();
40 }
41}
42
43void ASTContext::PrintStats() const {
44 fprintf(stderr, "*** AST Context Stats:\n");
45 fprintf(stderr, " %d types total.\n", (int)Types.size());
46 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
47 unsigned NumVector = 0, NumComplex = 0;
48 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
49
50 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000051 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
52 unsigned NumObjCQualifiedIds = 0;
Steve Naroffe0430632008-05-21 15:59:22 +000053 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000054
55 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
56 Type *T = Types[i];
57 if (isa<BuiltinType>(T))
58 ++NumBuiltin;
59 else if (isa<PointerType>(T))
60 ++NumPointer;
61 else if (isa<ReferenceType>(T))
62 ++NumReference;
63 else if (isa<ComplexType>(T))
64 ++NumComplex;
65 else if (isa<ArrayType>(T))
66 ++NumArray;
67 else if (isa<VectorType>(T))
68 ++NumVector;
69 else if (isa<FunctionTypeNoProto>(T))
70 ++NumFunctionNP;
71 else if (isa<FunctionTypeProto>(T))
72 ++NumFunctionP;
73 else if (isa<TypedefType>(T))
74 ++NumTypeName;
75 else if (TagType *TT = dyn_cast<TagType>(T)) {
76 ++NumTagged;
77 switch (TT->getDecl()->getKind()) {
78 default: assert(0 && "Unknown tagged type!");
79 case Decl::Struct: ++NumTagStruct; break;
80 case Decl::Union: ++NumTagUnion; break;
81 case Decl::Class: ++NumTagClass; break;
82 case Decl::Enum: ++NumTagEnum; break;
83 }
Ted Kremenek42730c52008-01-07 19:49:32 +000084 } else if (isa<ObjCInterfaceType>(T))
85 ++NumObjCInterfaces;
86 else if (isa<ObjCQualifiedInterfaceType>(T))
87 ++NumObjCQualifiedInterfaces;
88 else if (isa<ObjCQualifiedIdType>(T))
89 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +000090 else if (isa<TypeOfType>(T))
91 ++NumTypeOfTypes;
92 else if (isa<TypeOfExpr>(T))
93 ++NumTypeOfExprs;
Steve Naroff948fd372007-09-17 14:16:13 +000094 else {
Chris Lattner8a35b462007-12-12 06:43:05 +000095 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +000096 assert(0 && "Unknown type!");
97 }
98 }
99
100 fprintf(stderr, " %d builtin types\n", NumBuiltin);
101 fprintf(stderr, " %d pointer types\n", NumPointer);
102 fprintf(stderr, " %d reference types\n", NumReference);
103 fprintf(stderr, " %d complex types\n", NumComplex);
104 fprintf(stderr, " %d array types\n", NumArray);
105 fprintf(stderr, " %d vector types\n", NumVector);
106 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
107 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
108 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
109 fprintf(stderr, " %d tagged types\n", NumTagged);
110 fprintf(stderr, " %d struct types\n", NumTagStruct);
111 fprintf(stderr, " %d union types\n", NumTagUnion);
112 fprintf(stderr, " %d class types\n", NumTagClass);
113 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000114 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000115 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000116 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000117 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000118 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000119 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
120 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
121
Chris Lattner4b009652007-07-25 00:24:17 +0000122 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
123 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
124 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
125 NumFunctionP*sizeof(FunctionTypeProto)+
126 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroffe0430632008-05-21 15:59:22 +0000127 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
128 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Chris Lattner4b009652007-07-25 00:24:17 +0000129}
130
131
132void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
133 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
134}
135
Chris Lattner4b009652007-07-25 00:24:17 +0000136void ASTContext::InitBuiltinTypes() {
137 assert(VoidTy.isNull() && "Context reinitialized?");
138
139 // C99 6.2.5p19.
140 InitBuiltinType(VoidTy, BuiltinType::Void);
141
142 // C99 6.2.5p2.
143 InitBuiltinType(BoolTy, BuiltinType::Bool);
144 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000145 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000146 InitBuiltinType(CharTy, BuiltinType::Char_S);
147 else
148 InitBuiltinType(CharTy, BuiltinType::Char_U);
149 // C99 6.2.5p4.
150 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
151 InitBuiltinType(ShortTy, BuiltinType::Short);
152 InitBuiltinType(IntTy, BuiltinType::Int);
153 InitBuiltinType(LongTy, BuiltinType::Long);
154 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
155
156 // C99 6.2.5p6.
157 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
158 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
159 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
160 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
161 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
162
163 // C99 6.2.5p10.
164 InitBuiltinType(FloatTy, BuiltinType::Float);
165 InitBuiltinType(DoubleTy, BuiltinType::Double);
166 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
167
168 // C99 6.2.5p11.
169 FloatComplexTy = getComplexType(FloatTy);
170 DoubleComplexTy = getComplexType(DoubleTy);
171 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff9d12c902007-10-15 14:41:52 +0000172
173 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000174 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000175 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000176 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000177 ClassStructType = 0;
178
Ted Kremenek42730c52008-01-07 19:49:32 +0000179 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000180
181 // void * type
182 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000183}
184
185//===----------------------------------------------------------------------===//
186// Type Sizing and Analysis
187//===----------------------------------------------------------------------===//
188
189/// getTypeSize - Return the size of the specified type, in bits. This method
190/// does not work on incomplete types.
191std::pair<uint64_t, unsigned>
Chris Lattner8cd0e932008-03-05 18:54:05 +0000192ASTContext::getTypeInfo(QualType T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000193 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000194 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000195 unsigned Align;
196 switch (T->getTypeClass()) {
197 case Type::TypeName: assert(0 && "Not a canonical type!");
198 case Type::FunctionNoProto:
199 case Type::FunctionProto:
200 default:
201 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000202 case Type::VariableArray:
203 assert(0 && "VLAs not implemented yet!");
204 case Type::ConstantArray: {
205 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
206
Chris Lattner8cd0e932008-03-05 18:54:05 +0000207 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000208 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000209 Align = EltInfo.second;
210 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000211 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000212 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000213 case Type::Vector: {
214 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000215 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000216 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Chris Lattner4b009652007-07-25 00:24:17 +0000217 // FIXME: Vector alignment is not the alignment of its elements.
218 Align = EltInfo.second;
219 break;
220 }
221
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000222 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000223 switch (cast<BuiltinType>(T)->getKind()) {
224 default: assert(0 && "Unknown builtin type!");
225 case BuiltinType::Void:
226 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000227 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000228 Width = Target.getBoolWidth();
229 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000230 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000231 case BuiltinType::Char_S:
232 case BuiltinType::Char_U:
233 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000234 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000235 Width = Target.getCharWidth();
236 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000237 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000238 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000239 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000240 Width = Target.getShortWidth();
241 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000242 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000243 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000244 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000245 Width = Target.getIntWidth();
246 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000247 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000248 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000249 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000250 Width = Target.getLongWidth();
251 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000252 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000253 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000254 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000255 Width = Target.getLongLongWidth();
256 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000257 break;
258 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000259 Width = Target.getFloatWidth();
260 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000261 break;
262 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000263 Width = Target.getDoubleWidth();
264 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000265 break;
266 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000267 Width = Target.getLongDoubleWidth();
268 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000269 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000270 }
271 break;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000272 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000273 // FIXME: Pointers into different addr spaces could have different sizes and
274 // alignment requirements: getPointerInfo should take an AddrSpace.
275 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000276 case Type::ObjCQualifiedId:
Chris Lattner1d78a862008-04-07 07:01:58 +0000277 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000278 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000279 break;
Chris Lattner461a6c52008-03-08 08:34:58 +0000280 case Type::Pointer: {
281 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000282 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000283 Align = Target.getPointerAlign(AS);
284 break;
285 }
Chris Lattner4b009652007-07-25 00:24:17 +0000286 case Type::Reference:
287 // "When applied to a reference or a reference type, the result is the size
288 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000289 // FIXME: This is wrong for struct layout: a reference in a struct has
290 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000291 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner4b009652007-07-25 00:24:17 +0000292
293 case Type::Complex: {
294 // Complex types have the same alignment as their elements, but twice the
295 // size.
296 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000297 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000298 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000299 Align = EltInfo.second;
300 break;
301 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000302 case Type::Tagged: {
303 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
304 return getTypeInfo(ET->getDecl()->getIntegerType());
305
306 RecordType *RT = cast<RecordType>(T);
307 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
308 Width = Layout.getSize();
309 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000310 break;
311 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000312 }
Chris Lattner4b009652007-07-25 00:24:17 +0000313
314 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000315 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000316}
317
Devang Patel7a78e432007-11-01 19:11:01 +0000318/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000319/// specified record (struct/union/class), which indicates its size and field
320/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000321const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000322 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
323
324 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000325 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000326 if (Entry) return *Entry;
327
Devang Patel7a78e432007-11-01 19:11:01 +0000328 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
329 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
330 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000331 Entry = NewEntry;
332
333 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
334 uint64_t RecordSize = 0;
335 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
336
337 if (D->getKind() != Decl::Union) {
Anders Carlsson7dce0292008-02-16 19:51:27 +0000338 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
339 RecordAlign = std::max(RecordAlign, AA->getAlignment());
340
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000341 bool StructIsPacked = D->getAttr<PackedAttr>();
342
Chris Lattner4b009652007-07-25 00:24:17 +0000343 // Layout each field, for now, just sequentially, respecting alignment. In
344 // the future, this will need to be tweakable by targets.
345 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
346 const FieldDecl *FD = D->getMember(i);
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000347 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
Eli Friedman67571ac2008-02-06 05:33:51 +0000348 uint64_t FieldSize;
349 unsigned FieldAlign;
Anders Carlsson058237f2008-02-18 07:13:09 +0000350
351 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
352 llvm::APSInt I(32);
353 bool BitWidthIsICE =
354 BitWidthExpr->isIntegerConstantExpr(I, *this);
355 assert (BitWidthIsICE && "Invalid BitField size expression");
356 FieldSize = I.getZExtValue();
357
Chris Lattner8cd0e932008-03-05 18:54:05 +0000358 std::pair<uint64_t, unsigned> TypeInfo = getTypeInfo(FD->getType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000359 uint64_t TypeSize = TypeInfo.first;
Anders Carlsson7dce0292008-02-16 19:51:27 +0000360
361 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
362 FieldAlign = AA->getAlignment();
363 else if (FieldIsPacked)
364 FieldAlign = 8;
365 else {
Eli Friedmanf8382542008-05-20 15:17:39 +0000366 FieldAlign = TypeInfo.second;
Anders Carlsson7dce0292008-02-16 19:51:27 +0000367 }
Eli Friedman67571ac2008-02-06 05:33:51 +0000368
Anders Carlsson058237f2008-02-18 07:13:09 +0000369 // Check if we need to add padding to give the field the correct
370 // alignment.
371 if (RecordSize % FieldAlign + FieldSize > TypeSize)
372 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
373
374 } else {
375 if (FD->getType()->isIncompleteType()) {
376 // This must be a flexible array member; we can't directly
377 // query getTypeInfo about these, so we figure it out here.
378 // Flexible array members don't have any size, but they
379 // have to be aligned appropriately for their element type.
380
381 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
382 FieldAlign = AA->getAlignment();
383 else if (FieldIsPacked)
384 FieldAlign = 8;
385 else {
386 const ArrayType* ATy = FD->getType()->getAsArrayType();
Chris Lattner8cd0e932008-03-05 18:54:05 +0000387 FieldAlign = getTypeAlign(ATy->getElementType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000388 }
389 FieldSize = 0;
390 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000391 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Anders Carlsson058237f2008-02-18 07:13:09 +0000392 FieldSize = FieldInfo.first;
393
394 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
395 FieldAlign = AA->getAlignment();
396 else if (FieldIsPacked)
397 FieldAlign = 8;
398 else
399 FieldAlign = FieldInfo.second;
400 }
401
402 // Round up the current record size to the field's alignment boundary.
403 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
404 }
Chris Lattner4b009652007-07-25 00:24:17 +0000405
406 // Place this field at the current location.
407 FieldOffsets[i] = RecordSize;
408
409 // Reserve space for this field.
410 RecordSize += FieldSize;
411
412 // Remember max struct/class alignment.
413 RecordAlign = std::max(RecordAlign, FieldAlign);
414 }
415
416 // Finally, round the size of the total struct up to the alignment of the
417 // struct itself.
418 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
419 } else {
420 // Union layout just puts each member at the start of the record.
421 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
422 const FieldDecl *FD = D->getMember(i);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000423 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
Chris Lattner4b009652007-07-25 00:24:17 +0000424 uint64_t FieldSize = FieldInfo.first;
425 unsigned FieldAlign = FieldInfo.second;
Anders Carlsson058237f2008-02-18 07:13:09 +0000426
Chris Lattner4b009652007-07-25 00:24:17 +0000427 // Round up the current record size to the field's alignment boundary.
428 RecordSize = std::max(RecordSize, FieldSize);
Eli Friedmanf8382542008-05-20 15:17:39 +0000429
Chris Lattner4b009652007-07-25 00:24:17 +0000430 // Place this field at the start of the record.
431 FieldOffsets[i] = 0;
Eli Friedmanf8382542008-05-20 15:17:39 +0000432
Chris Lattner4b009652007-07-25 00:24:17 +0000433 // Remember max struct/class alignment.
434 RecordAlign = std::max(RecordAlign, FieldAlign);
435 }
436 }
437
438 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
439 return *NewEntry;
440}
441
Chris Lattner4b009652007-07-25 00:24:17 +0000442//===----------------------------------------------------------------------===//
443// Type creation/memoization methods
444//===----------------------------------------------------------------------===//
445
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000446QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000447 QualType CanT = getCanonicalType(T);
448 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000449 return T;
450
451 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
452 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000453 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000454 "Type is already address space qualified");
455
456 // Check if we've already instantiated an address space qual'd type of this
457 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000458 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000459 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000460 void *InsertPos = 0;
461 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
462 return QualType(ASQy, 0);
463
464 // If the base type isn't canonical, this won't be a canonical type either,
465 // so fill in the canonical type field.
466 QualType Canonical;
467 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000468 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000469
470 // Get the new insert position for the node we care about.
471 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
472 assert(NewIP == 0 && "Shouldn't be in the map!");
473 }
Chris Lattner35fef522008-02-20 20:55:12 +0000474 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000475 ASQualTypes.InsertNode(New, InsertPos);
476 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000477 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000478}
479
Chris Lattner4b009652007-07-25 00:24:17 +0000480
481/// getComplexType - Return the uniqued reference to the type for a complex
482/// number with the specified element type.
483QualType ASTContext::getComplexType(QualType T) {
484 // Unique pointers, to guarantee there is only one pointer of a particular
485 // structure.
486 llvm::FoldingSetNodeID ID;
487 ComplexType::Profile(ID, T);
488
489 void *InsertPos = 0;
490 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
491 return QualType(CT, 0);
492
493 // If the pointee type isn't canonical, this won't be a canonical type either,
494 // so fill in the canonical type field.
495 QualType Canonical;
496 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000497 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000498
499 // Get the new insert position for the node we care about.
500 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
501 assert(NewIP == 0 && "Shouldn't be in the map!");
502 }
503 ComplexType *New = new ComplexType(T, Canonical);
504 Types.push_back(New);
505 ComplexTypes.InsertNode(New, InsertPos);
506 return QualType(New, 0);
507}
508
509
510/// getPointerType - Return the uniqued reference to the type for a pointer to
511/// the specified type.
512QualType ASTContext::getPointerType(QualType T) {
513 // Unique pointers, to guarantee there is only one pointer of a particular
514 // structure.
515 llvm::FoldingSetNodeID ID;
516 PointerType::Profile(ID, T);
517
518 void *InsertPos = 0;
519 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
520 return QualType(PT, 0);
521
522 // If the pointee type isn't canonical, this won't be a canonical type either,
523 // so fill in the canonical type field.
524 QualType Canonical;
525 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000526 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000527
528 // Get the new insert position for the node we care about.
529 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
530 assert(NewIP == 0 && "Shouldn't be in the map!");
531 }
532 PointerType *New = new PointerType(T, Canonical);
533 Types.push_back(New);
534 PointerTypes.InsertNode(New, InsertPos);
535 return QualType(New, 0);
536}
537
538/// getReferenceType - Return the uniqued reference to the type for a reference
539/// to the specified type.
540QualType ASTContext::getReferenceType(QualType T) {
541 // Unique pointers, to guarantee there is only one pointer of a particular
542 // structure.
543 llvm::FoldingSetNodeID ID;
544 ReferenceType::Profile(ID, T);
545
546 void *InsertPos = 0;
547 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
548 return QualType(RT, 0);
549
550 // If the referencee type isn't canonical, this won't be a canonical type
551 // either, so fill in the canonical type field.
552 QualType Canonical;
553 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000554 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000555
556 // Get the new insert position for the node we care about.
557 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
558 assert(NewIP == 0 && "Shouldn't be in the map!");
559 }
560
561 ReferenceType *New = new ReferenceType(T, Canonical);
562 Types.push_back(New);
563 ReferenceTypes.InsertNode(New, InsertPos);
564 return QualType(New, 0);
565}
566
Steve Naroff83c13012007-08-30 01:06:46 +0000567/// getConstantArrayType - Return the unique reference to the type for an
568/// array of the specified element type.
569QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000570 const llvm::APInt &ArySize,
571 ArrayType::ArraySizeModifier ASM,
572 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000573 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000574 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000575
576 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000577 if (ConstantArrayType *ATP =
578 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000579 return QualType(ATP, 0);
580
581 // If the element type isn't canonical, this won't be a canonical type either,
582 // so fill in the canonical type field.
583 QualType Canonical;
584 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000585 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000586 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000587 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000588 ConstantArrayType *NewIP =
589 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
590
Chris Lattner4b009652007-07-25 00:24:17 +0000591 assert(NewIP == 0 && "Shouldn't be in the map!");
592 }
593
Steve Naroff24c9b982007-08-30 18:10:14 +0000594 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
595 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000596 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000597 Types.push_back(New);
598 return QualType(New, 0);
599}
600
Steve Naroffe2579e32007-08-30 18:14:25 +0000601/// getVariableArrayType - Returns a non-unique reference to the type for a
602/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000603QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
604 ArrayType::ArraySizeModifier ASM,
605 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000606 // Since we don't unique expressions, it isn't possible to unique VLA's
607 // that have an expression provided for their size.
608
609 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
610 ASM, EltTypeQuals);
611
612 VariableArrayTypes.push_back(New);
613 Types.push_back(New);
614 return QualType(New, 0);
615}
616
617QualType ASTContext::getIncompleteArrayType(QualType EltTy,
618 ArrayType::ArraySizeModifier ASM,
619 unsigned EltTypeQuals) {
620 llvm::FoldingSetNodeID ID;
621 IncompleteArrayType::Profile(ID, EltTy);
622
623 void *InsertPos = 0;
624 if (IncompleteArrayType *ATP =
625 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
626 return QualType(ATP, 0);
627
628 // If the element type isn't canonical, this won't be a canonical type
629 // either, so fill in the canonical type field.
630 QualType Canonical;
631
632 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000633 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000634 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000635
636 // Get the new insert position for the node we care about.
637 IncompleteArrayType *NewIP =
638 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
639
640 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000641 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000642
643 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
644 ASM, EltTypeQuals);
645
646 IncompleteArrayTypes.InsertNode(New, InsertPos);
647 Types.push_back(New);
648 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000649}
650
Chris Lattner4b009652007-07-25 00:24:17 +0000651/// getVectorType - Return the unique reference to a vector type of
652/// the specified element type and size. VectorType must be a built-in type.
653QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
654 BuiltinType *baseType;
655
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000656 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000657 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
658
659 // Check if we've already instantiated a vector of this type.
660 llvm::FoldingSetNodeID ID;
661 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
662 void *InsertPos = 0;
663 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
664 return QualType(VTP, 0);
665
666 // If the element type isn't canonical, this won't be a canonical type either,
667 // so fill in the canonical type field.
668 QualType Canonical;
669 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000670 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000671
672 // Get the new insert position for the node we care about.
673 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
674 assert(NewIP == 0 && "Shouldn't be in the map!");
675 }
676 VectorType *New = new VectorType(vecType, NumElts, Canonical);
677 VectorTypes.InsertNode(New, InsertPos);
678 Types.push_back(New);
679 return QualType(New, 0);
680}
681
Nate Begemanaf6ed502008-04-18 23:10:10 +0000682/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +0000683/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000684QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +0000685 BuiltinType *baseType;
686
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000687 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +0000688 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +0000689
690 // Check if we've already instantiated a vector of this type.
691 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000692 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +0000693 void *InsertPos = 0;
694 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
695 return QualType(VTP, 0);
696
697 // If the element type isn't canonical, this won't be a canonical type either,
698 // so fill in the canonical type field.
699 QualType Canonical;
700 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000701 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000702
703 // Get the new insert position for the node we care about.
704 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
705 assert(NewIP == 0 && "Shouldn't be in the map!");
706 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000707 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000708 VectorTypes.InsertNode(New, InsertPos);
709 Types.push_back(New);
710 return QualType(New, 0);
711}
712
713/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
714///
715QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
716 // Unique functions, to guarantee there is only one function of a particular
717 // structure.
718 llvm::FoldingSetNodeID ID;
719 FunctionTypeNoProto::Profile(ID, ResultTy);
720
721 void *InsertPos = 0;
722 if (FunctionTypeNoProto *FT =
723 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
724 return QualType(FT, 0);
725
726 QualType Canonical;
727 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000728 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000729
730 // Get the new insert position for the node we care about.
731 FunctionTypeNoProto *NewIP =
732 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
733 assert(NewIP == 0 && "Shouldn't be in the map!");
734 }
735
736 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
737 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000738 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000739 return QualType(New, 0);
740}
741
742/// getFunctionType - Return a normal function type with a typed argument
743/// list. isVariadic indicates whether the argument list includes '...'.
744QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
745 unsigned NumArgs, bool isVariadic) {
746 // Unique functions, to guarantee there is only one function of a particular
747 // structure.
748 llvm::FoldingSetNodeID ID;
749 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
750
751 void *InsertPos = 0;
752 if (FunctionTypeProto *FTP =
753 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
754 return QualType(FTP, 0);
755
756 // Determine whether the type being created is already canonical or not.
757 bool isCanonical = ResultTy->isCanonical();
758 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
759 if (!ArgArray[i]->isCanonical())
760 isCanonical = false;
761
762 // If this type isn't canonical, get the canonical version of it.
763 QualType Canonical;
764 if (!isCanonical) {
765 llvm::SmallVector<QualType, 16> CanonicalArgs;
766 CanonicalArgs.reserve(NumArgs);
767 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000768 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +0000769
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000770 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +0000771 &CanonicalArgs[0], NumArgs,
772 isVariadic);
773
774 // Get the new insert position for the node we care about.
775 FunctionTypeProto *NewIP =
776 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
777 assert(NewIP == 0 && "Shouldn't be in the map!");
778 }
779
780 // FunctionTypeProto objects are not allocated with new because they have a
781 // variable size array (for parameter types) at the end of them.
782 FunctionTypeProto *FTP =
783 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
784 NumArgs*sizeof(QualType));
785 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
786 Canonical);
787 Types.push_back(FTP);
788 FunctionTypeProtos.InsertNode(FTP, InsertPos);
789 return QualType(FTP, 0);
790}
791
Douglas Gregor1d661552008-04-13 21:07:44 +0000792/// getTypeDeclType - Return the unique reference to the type for the
793/// specified type declaration.
794QualType ASTContext::getTypeDeclType(TypeDecl *Decl) {
795 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
796
797 if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(Decl))
798 return getTypedefType(Typedef);
799 else if (ObjCInterfaceDecl *ObjCInterface
800 = dyn_cast_or_null<ObjCInterfaceDecl>(Decl))
801 return getObjCInterfaceType(ObjCInterface);
802 else if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Decl)) {
803 Decl->TypeForDecl = new RecordType(Record);
804 Types.push_back(Decl->TypeForDecl);
805 return QualType(Decl->TypeForDecl, 0);
806 } else if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Decl)) {
807 Decl->TypeForDecl = new EnumType(Enum);
808 Types.push_back(Decl->TypeForDecl);
809 return QualType(Decl->TypeForDecl, 0);
810 } else
811 assert(false && "TypeDecl without a type?");
812}
813
Chris Lattner4b009652007-07-25 00:24:17 +0000814/// getTypedefType - Return the unique reference to the type for the
815/// specified typename decl.
816QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
817 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
818
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000819 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000820 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000821 Types.push_back(Decl->TypeForDecl);
822 return QualType(Decl->TypeForDecl, 0);
823}
824
Ted Kremenek42730c52008-01-07 19:49:32 +0000825/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000826/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000827QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000828 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
829
Ted Kremenek42730c52008-01-07 19:49:32 +0000830 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000831 Types.push_back(Decl->TypeForDecl);
832 return QualType(Decl->TypeForDecl, 0);
833}
834
Chris Lattnere1352302008-04-07 04:56:42 +0000835/// CmpProtocolNames - Comparison predicate for sorting protocols
836/// alphabetically.
837static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
838 const ObjCProtocolDecl *RHS) {
839 return strcmp(LHS->getName(), RHS->getName()) < 0;
840}
841
842static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
843 unsigned &NumProtocols) {
844 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
845
846 // Sort protocols, keyed by name.
847 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
848
849 // Remove duplicates.
850 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
851 NumProtocols = ProtocolsEnd-Protocols;
852}
853
854
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +0000855/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
856/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000857QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
858 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000859 // Sort the protocol list alphabetically to canonicalize it.
860 SortAndUniqueProtocols(Protocols, NumProtocols);
861
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000862 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +0000863 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000864
865 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000866 if (ObjCQualifiedInterfaceType *QT =
867 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000868 return QualType(QT, 0);
869
870 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +0000871 ObjCQualifiedInterfaceType *QType =
872 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000873 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000874 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000875 return QualType(QType, 0);
876}
877
Chris Lattnere1352302008-04-07 04:56:42 +0000878/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
879/// and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000880QualType ASTContext::getObjCQualifiedIdType(QualType idType,
881 ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000882 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000883 // Sort the protocol list alphabetically to canonicalize it.
884 SortAndUniqueProtocols(Protocols, NumProtocols);
885
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000886 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000887 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000888
889 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000890 if (ObjCQualifiedIdType *QT =
891 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000892 return QualType(QT, 0);
893
894 // No Match;
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000895 QualType Canonical;
896 if (!idType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000897 Canonical = getObjCQualifiedIdType(getCanonicalType(idType),
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000898 Protocols, NumProtocols);
Ted Kremenek42730c52008-01-07 19:49:32 +0000899 ObjCQualifiedIdType *NewQT =
900 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000901 assert(NewQT == 0 && "Shouldn't be in the map!");
902 }
903
Ted Kremenek42730c52008-01-07 19:49:32 +0000904 ObjCQualifiedIdType *QType =
905 new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000906 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000907 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000908 return QualType(QType, 0);
909}
910
Steve Naroff0604dd92007-08-01 18:02:17 +0000911/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
912/// TypeOfExpr AST's (since expression's are never shared). For example,
913/// multiple declarations that refer to "typeof(x)" all contain different
914/// DeclRefExpr's. This doesn't effect the type checker, since it operates
915/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000916QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000917 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff0604dd92007-08-01 18:02:17 +0000918 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
919 Types.push_back(toe);
920 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000921}
922
Steve Naroff0604dd92007-08-01 18:02:17 +0000923/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
924/// TypeOfType AST's. The only motivation to unique these nodes would be
925/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
926/// an issue. This doesn't effect the type checker, since it operates
927/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000928QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000929 QualType Canonical = getCanonicalType(tofType);
Steve Naroff0604dd92007-08-01 18:02:17 +0000930 TypeOfType *tot = new TypeOfType(tofType, Canonical);
931 Types.push_back(tot);
932 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000933}
934
Chris Lattner4b009652007-07-25 00:24:17 +0000935/// getTagDeclType - Return the unique reference to the type for the
936/// specified TagDecl (struct/union/class/enum) decl.
937QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +0000938 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +0000939 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +0000940}
941
942/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
943/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
944/// needs to agree with the definition in <stddef.h>.
945QualType ASTContext::getSizeType() const {
946 // On Darwin, size_t is defined as a "long unsigned int".
947 // FIXME: should derive from "Target".
948 return UnsignedLongTy;
949}
950
Eli Friedmanfdd35d72008-02-12 08:29:21 +0000951/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
952/// width of characters in wide strings, The value is target dependent and
953/// needs to agree with the definition in <stddef.h>.
954QualType ASTContext::getWcharType() const {
955 // On Darwin, wchar_t is defined as a "int".
956 // FIXME: should derive from "Target".
957 return IntTy;
958}
959
Chris Lattner4b009652007-07-25 00:24:17 +0000960/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
961/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
962QualType ASTContext::getPointerDiffType() const {
963 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
964 // FIXME: should derive from "Target".
965 return IntTy;
966}
967
Chris Lattner19eb97e2008-04-02 05:18:44 +0000968//===----------------------------------------------------------------------===//
969// Type Operators
970//===----------------------------------------------------------------------===//
971
Chris Lattner3dae6f42008-04-06 22:41:35 +0000972/// getCanonicalType - Return the canonical (structural) type corresponding to
973/// the specified potentially non-canonical type. The non-canonical version
974/// of a type may have many "decorated" versions of types. Decorators can
975/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
976/// to be free of any of these, allowing two canonical types to be compared
977/// for exact equality with a simple pointer comparison.
978QualType ASTContext::getCanonicalType(QualType T) {
979 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
980 return QualType(CanType.getTypePtr(),
981 T.getCVRQualifiers() | CanType.getCVRQualifiers());
982}
983
984
Chris Lattner19eb97e2008-04-02 05:18:44 +0000985/// getArrayDecayedType - Return the properly qualified result of decaying the
986/// specified array type to a pointer. This operation is non-trivial when
987/// handling typedefs etc. The canonical type of "T" must be an array type,
988/// this returns a pointer to a properly qualified element of the array.
989///
990/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
991QualType ASTContext::getArrayDecayedType(QualType Ty) {
992 // Handle the common case where typedefs are not involved directly.
993 QualType EltTy;
994 unsigned ArrayQuals = 0;
995 unsigned PointerQuals = 0;
996 if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
997 // Since T "isa" an array type, it could not have had an address space
998 // qualifier, just CVR qualifiers. The properly qualified element pointer
999 // gets the union of the CVR qualifiers from the element and the array, and
1000 // keeps any address space qualifier on the element type if present.
1001 EltTy = AT->getElementType();
1002 ArrayQuals = Ty.getCVRQualifiers();
1003 PointerQuals = AT->getIndexTypeQualifier();
1004 } else {
1005 // Otherwise, we have an ASQualType or a typedef, etc. Make sure we don't
1006 // lose qualifiers when dealing with typedefs. Example:
1007 // typedef int arr[10];
1008 // void test2() {
1009 // const arr b;
1010 // b[4] = 1;
1011 // }
1012 //
1013 // The decayed type of b is "const int*" even though the element type of the
1014 // array is "int".
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001015 QualType CanTy = getCanonicalType(Ty);
Chris Lattner19eb97e2008-04-02 05:18:44 +00001016 const ArrayType *PrettyArrayType = Ty->getAsArrayType();
1017 assert(PrettyArrayType && "Not an array type!");
1018
1019 // Get the element type with 'getAsArrayType' so that we don't lose any
1020 // typedefs in the element type of the array.
1021 EltTy = PrettyArrayType->getElementType();
1022
1023 // If the array was address-space qualifier, make sure to ASQual the element
1024 // type. We can just grab the address space from the canonical type.
1025 if (unsigned AS = CanTy.getAddressSpace())
1026 EltTy = getASQualType(EltTy, AS);
1027
1028 // To properly handle [multiple levels of] typedefs, typeof's etc, we take
1029 // the CVR qualifiers directly from the canonical type, which is guaranteed
1030 // to have the full set unioned together.
1031 ArrayQuals = CanTy.getCVRQualifiers();
1032 PointerQuals = PrettyArrayType->getIndexTypeQualifier();
1033 }
1034
Chris Lattnerda79b3f2008-04-02 06:06:35 +00001035 // Apply any CVR qualifiers from the array type to the element type. This
1036 // implements C99 6.7.3p8: "If the specification of an array type includes
1037 // any type qualifiers, the element type is so qualified, not the array type."
Chris Lattner19eb97e2008-04-02 05:18:44 +00001038 EltTy = EltTy.getQualifiedType(ArrayQuals | EltTy.getCVRQualifiers());
1039
1040 QualType PtrTy = getPointerType(EltTy);
1041
1042 // int x[restrict 4] -> int *restrict
1043 PtrTy = PtrTy.getQualifiedType(PointerQuals);
1044
1045 return PtrTy;
1046}
1047
Chris Lattner4b009652007-07-25 00:24:17 +00001048/// getFloatingRank - Return a relative rank for floating point types.
1049/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001050static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001051 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001052 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001053
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001054 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001055 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001056 case BuiltinType::Float: return FloatRank;
1057 case BuiltinType::Double: return DoubleRank;
1058 case BuiltinType::LongDouble: return LongDoubleRank;
1059 }
1060}
1061
Steve Narofffa0c4532007-08-27 01:41:48 +00001062/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1063/// point or a complex type (based on typeDomain/typeSize).
1064/// 'typeDomain' is a real floating point or complex type.
1065/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001066QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1067 QualType Domain) const {
1068 FloatingRank EltRank = getFloatingRank(Size);
1069 if (Domain->isComplexType()) {
1070 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001071 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001072 case FloatRank: return FloatComplexTy;
1073 case DoubleRank: return DoubleComplexTy;
1074 case LongDoubleRank: return LongDoubleComplexTy;
1075 }
Chris Lattner4b009652007-07-25 00:24:17 +00001076 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001077
1078 assert(Domain->isRealFloatingType() && "Unknown domain!");
1079 switch (EltRank) {
1080 default: assert(0 && "getFloatingRank(): illegal value for rank");
1081 case FloatRank: return FloatTy;
1082 case DoubleRank: return DoubleTy;
1083 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001084 }
Chris Lattner4b009652007-07-25 00:24:17 +00001085}
1086
Chris Lattner51285d82008-04-06 23:55:33 +00001087/// getFloatingTypeOrder - Compare the rank of the two specified floating
1088/// point types, ignoring the domain of the type (i.e. 'double' ==
1089/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1090/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001091int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1092 FloatingRank LHSR = getFloatingRank(LHS);
1093 FloatingRank RHSR = getFloatingRank(RHS);
1094
1095 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001096 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001097 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001098 return 1;
1099 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001100}
1101
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001102/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1103/// routine will assert if passed a built-in type that isn't an integer or enum,
1104/// or if it is not canonicalized.
1105static unsigned getIntegerRank(Type *T) {
1106 assert(T->isCanonical() && "T should be canonicalized");
1107 if (isa<EnumType>(T))
1108 return 4;
1109
1110 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001111 default: assert(0 && "getIntegerRank(): not a built-in integer");
1112 case BuiltinType::Bool:
1113 return 1;
1114 case BuiltinType::Char_S:
1115 case BuiltinType::Char_U:
1116 case BuiltinType::SChar:
1117 case BuiltinType::UChar:
1118 return 2;
1119 case BuiltinType::Short:
1120 case BuiltinType::UShort:
1121 return 3;
1122 case BuiltinType::Int:
1123 case BuiltinType::UInt:
1124 return 4;
1125 case BuiltinType::Long:
1126 case BuiltinType::ULong:
1127 return 5;
1128 case BuiltinType::LongLong:
1129 case BuiltinType::ULongLong:
1130 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001131 }
1132}
1133
Chris Lattner51285d82008-04-06 23:55:33 +00001134/// getIntegerTypeOrder - Returns the highest ranked integer type:
1135/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1136/// LHS < RHS, return -1.
1137int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001138 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1139 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001140 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001141
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001142 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1143 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001144
Chris Lattner51285d82008-04-06 23:55:33 +00001145 unsigned LHSRank = getIntegerRank(LHSC);
1146 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001147
Chris Lattner51285d82008-04-06 23:55:33 +00001148 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1149 if (LHSRank == RHSRank) return 0;
1150 return LHSRank > RHSRank ? 1 : -1;
1151 }
Chris Lattner4b009652007-07-25 00:24:17 +00001152
Chris Lattner51285d82008-04-06 23:55:33 +00001153 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1154 if (LHSUnsigned) {
1155 // If the unsigned [LHS] type is larger, return it.
1156 if (LHSRank >= RHSRank)
1157 return 1;
1158
1159 // If the signed type can represent all values of the unsigned type, it
1160 // wins. Because we are dealing with 2's complement and types that are
1161 // powers of two larger than each other, this is always safe.
1162 return -1;
1163 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001164
Chris Lattner51285d82008-04-06 23:55:33 +00001165 // If the unsigned [RHS] type is larger, return it.
1166 if (RHSRank >= LHSRank)
1167 return -1;
1168
1169 // If the signed type can represent all values of the unsigned type, it
1170 // wins. Because we are dealing with 2's complement and types that are
1171 // powers of two larger than each other, this is always safe.
1172 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001173}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001174
1175// getCFConstantStringType - Return the type used for constant CFStrings.
1176QualType ASTContext::getCFConstantStringType() {
1177 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001178 CFConstantStringTypeDecl =
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001179 RecordDecl::Create(*this, Decl::Struct, TUDecl, SourceLocation(),
Chris Lattner58114f02008-03-15 21:32:50 +00001180 &Idents.get("NSConstantString"), 0);
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001181 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001182
1183 // const int *isa;
1184 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001185 // int flags;
1186 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001187 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001188 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001189 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001190 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001191 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001192 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001193
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001194 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerf3874bc2008-04-06 04:47:34 +00001195 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner81db64a2008-03-16 00:16:02 +00001196 FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001197
1198 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1199 }
1200
1201 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001202}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001203
Anders Carlssone3f02572007-10-29 06:33:42 +00001204// This returns true if a type has been typedefed to BOOL:
1205// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001206static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001207 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001208 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001209
1210 return false;
1211}
1212
Ted Kremenek42730c52008-01-07 19:49:32 +00001213/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001214/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001215int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001216 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001217
1218 // Make all integer and enum types at least as large as an int
1219 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001220 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001221 // Treat arrays as pointers, since that's how they're passed in.
1222 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001223 sz = getTypeSize(VoidPtrTy);
1224 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001225}
1226
Ted Kremenek42730c52008-01-07 19:49:32 +00001227/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001228/// declaration.
Ted Kremenek42730c52008-01-07 19:49:32 +00001229void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001230 std::string& S)
1231{
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001232 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001233 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001234 // Encode result type.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001235 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001236 // Compute size of all parameters.
1237 // Start with computing size of a pointer in number of bytes.
1238 // FIXME: There might(should) be a better way of doing this computation!
1239 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001240 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001241 // The first two arguments (self and _cmd) are pointers; account for
1242 // their size.
1243 int ParmOffset = 2 * PtrSize;
1244 int NumOfParams = Decl->getNumParams();
1245 for (int i = 0; i < NumOfParams; i++) {
1246 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001247 int sz = getObjCEncodingTypeSize (PType);
1248 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001249 ParmOffset += sz;
1250 }
1251 S += llvm::utostr(ParmOffset);
1252 S += "@0:";
1253 S += llvm::utostr(PtrSize);
1254
1255 // Argument types.
1256 ParmOffset = 2 * PtrSize;
1257 for (int i = 0; i < NumOfParams; i++) {
1258 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001259 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001260 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001261 getObjCEncodingForTypeQualifier(
1262 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian248db262008-01-22 22:44:46 +00001263 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001264 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001265 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001266 }
1267}
1268
Fariborz Jahanian248db262008-01-22 22:44:46 +00001269void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1270 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson36f07d82007-10-29 05:01:08 +00001271{
Anders Carlssone3f02572007-10-29 06:33:42 +00001272 // FIXME: This currently doesn't encode:
1273 // @ An object (whether statically typed or typed id)
1274 // # A class object (Class)
1275 // : A method selector (SEL)
1276 // {name=type...} A structure
1277 // (name=type...) A union
1278 // bnum A bit field of num bits
1279
1280 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001281 char encoding;
1282 switch (BT->getKind()) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001283 default: assert(0 && "Unhandled builtin type kind");
1284 case BuiltinType::Void: encoding = 'v'; break;
1285 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001286 case BuiltinType::Char_U:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001287 case BuiltinType::UChar: encoding = 'C'; break;
1288 case BuiltinType::UShort: encoding = 'S'; break;
1289 case BuiltinType::UInt: encoding = 'I'; break;
1290 case BuiltinType::ULong: encoding = 'L'; break;
1291 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001292 case BuiltinType::Char_S:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001293 case BuiltinType::SChar: encoding = 'c'; break;
1294 case BuiltinType::Short: encoding = 's'; break;
1295 case BuiltinType::Int: encoding = 'i'; break;
1296 case BuiltinType::Long: encoding = 'l'; break;
1297 case BuiltinType::LongLong: encoding = 'q'; break;
1298 case BuiltinType::Float: encoding = 'f'; break;
1299 case BuiltinType::Double: encoding = 'd'; break;
1300 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001301 }
1302
1303 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001304 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001305 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001306 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001307 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001308
1309 }
1310 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001311 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001312 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001313 S += '@';
1314 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001315 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001316 S += '#';
1317 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001318 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001319 S += ':';
1320 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001321 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001322
1323 if (PointeeTy->isCharType()) {
1324 // char pointer types should be encoded as '*' unless it is a
1325 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001326 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001327 S += '*';
1328 return;
1329 }
1330 }
1331
1332 S += '^';
Fariborz Jahanian248db262008-01-22 22:44:46 +00001333 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone3f02572007-10-29 06:33:42 +00001334 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001335 S += '[';
1336
1337 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1338 S += llvm::utostr(CAT->getSize().getZExtValue());
1339 else
1340 assert(0 && "Unhandled array type!");
1341
Fariborz Jahanian248db262008-01-22 22:44:46 +00001342 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001343 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001344 } else if (T->getAsFunctionType()) {
1345 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001346 } else if (const RecordType *RTy = T->getAsRecordType()) {
1347 RecordDecl *RDecl= RTy->getDecl();
1348 S += '{';
1349 S += RDecl->getName();
Fariborz Jahanian248db262008-01-22 22:44:46 +00001350 bool found = false;
1351 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1352 if (ERType[i] == RTy) {
1353 found = true;
1354 break;
1355 }
1356 if (!found) {
1357 ERType.push_back(RTy);
1358 S += '=';
1359 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1360 FieldDecl *field = RDecl->getMember(i);
1361 getObjCEncodingForType(field->getType(), S, ERType);
1362 }
1363 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1364 ERType.pop_back();
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001365 }
1366 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001367 } else if (T->isEnumeralType()) {
1368 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001369 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001370 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001371}
1372
Ted Kremenek42730c52008-01-07 19:49:32 +00001373void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001374 std::string& S) const {
1375 if (QT & Decl::OBJC_TQ_In)
1376 S += 'n';
1377 if (QT & Decl::OBJC_TQ_Inout)
1378 S += 'N';
1379 if (QT & Decl::OBJC_TQ_Out)
1380 S += 'o';
1381 if (QT & Decl::OBJC_TQ_Bycopy)
1382 S += 'O';
1383 if (QT & Decl::OBJC_TQ_Byref)
1384 S += 'R';
1385 if (QT & Decl::OBJC_TQ_Oneway)
1386 S += 'V';
1387}
1388
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001389void ASTContext::setBuiltinVaListType(QualType T)
1390{
1391 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1392
1393 BuiltinVaListType = T;
1394}
1395
Ted Kremenek42730c52008-01-07 19:49:32 +00001396void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001397{
Ted Kremenek42730c52008-01-07 19:49:32 +00001398 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff9d12c902007-10-15 14:41:52 +00001399
Ted Kremenek42730c52008-01-07 19:49:32 +00001400 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001401
1402 // typedef struct objc_object *id;
1403 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1404 assert(ptr && "'id' incorrectly typed");
1405 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1406 assert(rec && "'id' incorrectly typed");
1407 IdStructType = rec;
1408}
1409
Ted Kremenek42730c52008-01-07 19:49:32 +00001410void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001411{
Ted Kremenek42730c52008-01-07 19:49:32 +00001412 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001413
Ted Kremenek42730c52008-01-07 19:49:32 +00001414 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001415
1416 // typedef struct objc_selector *SEL;
1417 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1418 assert(ptr && "'SEL' incorrectly typed");
1419 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1420 assert(rec && "'SEL' incorrectly typed");
1421 SelStructType = rec;
1422}
1423
Ted Kremenek42730c52008-01-07 19:49:32 +00001424void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001425{
Ted Kremenek42730c52008-01-07 19:49:32 +00001426 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1427 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001428}
1429
Ted Kremenek42730c52008-01-07 19:49:32 +00001430void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001431{
Ted Kremenek42730c52008-01-07 19:49:32 +00001432 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001433
Ted Kremenek42730c52008-01-07 19:49:32 +00001434 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001435
1436 // typedef struct objc_class *Class;
1437 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1438 assert(ptr && "'Class' incorrectly typed");
1439 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1440 assert(rec && "'Class' incorrectly typed");
1441 ClassStructType = rec;
1442}
1443
Ted Kremenek42730c52008-01-07 19:49:32 +00001444void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1445 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001446 "'NSConstantString' type already set!");
1447
Ted Kremenek42730c52008-01-07 19:49:32 +00001448 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001449}
1450
Chris Lattner6ff358b2008-04-07 06:51:04 +00001451//===----------------------------------------------------------------------===//
1452// Type Compatibility Testing
1453//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00001454
Chris Lattner390564e2008-04-07 06:49:41 +00001455/// C99 6.2.7p1: If both are complete types, then the following additional
1456/// requirements apply.
1457/// FIXME (handle compatibility across source files).
1458static bool areCompatTagTypes(TagType *LHS, TagType *RHS,
1459 const ASTContext &C) {
Steve Naroff4a5e2072007-11-07 06:03:51 +00001460 // "Class" and "id" are compatible built-in structure types.
Chris Lattner390564e2008-04-07 06:49:41 +00001461 if (C.isObjCIdType(QualType(LHS, 0)) && C.isObjCClassType(QualType(RHS, 0)) ||
1462 C.isObjCClassType(QualType(LHS, 0)) && C.isObjCIdType(QualType(RHS, 0)))
Steve Naroff4a5e2072007-11-07 06:03:51 +00001463 return true;
Eli Friedmane7fb03a2008-02-15 06:03:44 +00001464
Chris Lattner390564e2008-04-07 06:49:41 +00001465 // Within a translation unit a tag type is only compatible with itself. Self
1466 // equality is already handled by the time we get here.
1467 assert(LHS != RHS && "Self equality not handled!");
1468 return false;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001469}
1470
1471bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1472 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1473 // identically qualified and both shall be pointers to compatible types.
Chris Lattner35fef522008-02-20 20:55:12 +00001474 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1475 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001476 return false;
1477
1478 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1479 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1480
1481 return typesAreCompatible(ltype, rtype);
1482}
1483
Steve Naroff85f0dc52007-10-15 20:41:53 +00001484bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1485 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1486 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1487 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1488 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1489
1490 // first check the return types (common between C99 and K&R).
1491 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1492 return false;
1493
1494 if (lproto && rproto) { // two C99 style function prototypes
1495 unsigned lproto_nargs = lproto->getNumArgs();
1496 unsigned rproto_nargs = rproto->getNumArgs();
1497
1498 if (lproto_nargs != rproto_nargs)
1499 return false;
1500
1501 // both prototypes have the same number of arguments.
1502 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1503 (rproto->isVariadic() && !lproto->isVariadic()))
1504 return false;
1505
1506 // The use of ellipsis agree...now check the argument types.
1507 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001508 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1509 // is taken as having the unqualified version of it's declared type.
Steve Naroffdec17fe2008-01-29 00:15:50 +00001510 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001511 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001512 return false;
1513 return true;
1514 }
Chris Lattner1d78a862008-04-07 07:01:58 +00001515
Steve Naroff85f0dc52007-10-15 20:41:53 +00001516 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1517 return true;
1518
1519 // we have a mixture of K&R style with C99 prototypes
1520 const FunctionTypeProto *proto = lproto ? lproto : rproto;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001521 if (proto->isVariadic())
1522 return false;
1523
1524 // FIXME: Each parameter type T in the prototype must be compatible with the
1525 // type resulting from applying the usual argument conversions to T.
1526 return true;
1527}
1528
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001529// C99 6.7.5.2p6
1530static bool areCompatArrayTypes(ArrayType *LHS, ArrayType *RHS, ASTContext &C) {
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001531 // Constant arrays must be the same size to be compatible.
1532 if (const ConstantArrayType* LCAT = dyn_cast<ConstantArrayType>(LHS))
1533 if (const ConstantArrayType* RCAT = dyn_cast<ConstantArrayType>(RHS))
1534 if (RCAT->getSize() != LCAT->getSize())
1535 return false;
Eli Friedman1e7537832008-02-06 04:53:22 +00001536
Chris Lattnerc8971d72008-04-07 06:58:21 +00001537 // Compatible arrays must have compatible element types
1538 return C.typesAreCompatible(LHS->getElementType(), RHS->getElementType());
Steve Naroff85f0dc52007-10-15 20:41:53 +00001539}
1540
Chris Lattner6ff358b2008-04-07 06:51:04 +00001541/// areCompatVectorTypes - Return true if the two specified vector types are
1542/// compatible.
1543static bool areCompatVectorTypes(const VectorType *LHS,
1544 const VectorType *RHS) {
1545 assert(LHS->isCanonical() && RHS->isCanonical());
1546 return LHS->getElementType() == RHS->getElementType() &&
1547 LHS->getNumElements() == RHS->getNumElements();
1548}
1549
1550/// areCompatObjCInterfaces - Return true if the two interface types are
1551/// compatible for assignment from RHS to LHS. This handles validation of any
1552/// protocol qualifiers on the LHS or RHS.
1553///
Chris Lattner1d78a862008-04-07 07:01:58 +00001554static bool areCompatObjCInterfaces(const ObjCInterfaceType *LHS,
1555 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00001556 // Verify that the base decls are compatible: the RHS must be a subclass of
1557 // the LHS.
1558 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1559 return false;
1560
1561 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1562 // protocol qualified at all, then we are good.
1563 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1564 return true;
1565
1566 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1567 // isn't a superset.
1568 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1569 return true; // FIXME: should return false!
1570
1571 // Finally, we must have two protocol-qualified interfaces.
1572 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1573 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1574 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1575 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1576 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1577 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1578
1579 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1580 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1581 // LHS in a single parallel scan until we run out of elements in LHS.
1582 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1583 ObjCProtocolDecl *LHSProto = *LHSPI;
1584
1585 while (RHSPI != RHSPE) {
1586 ObjCProtocolDecl *RHSProto = *RHSPI++;
1587 // If the RHS has a protocol that the LHS doesn't, ignore it.
1588 if (RHSProto != LHSProto)
1589 continue;
1590
1591 // Otherwise, the RHS does have this element.
1592 ++LHSPI;
1593 if (LHSPI == LHSPE)
1594 return true; // All protocols in LHS exist in RHS.
1595
1596 LHSProto = *LHSPI;
1597 }
1598
1599 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1600 return false;
1601}
1602
1603
Steve Naroff85f0dc52007-10-15 20:41:53 +00001604/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1605/// both shall have the identically qualified version of a compatible type.
1606/// C99 6.2.7p1: Two types have compatible types if their types are the
1607/// same. See 6.7.[2,3,5] for additional rules.
Chris Lattner855fed42008-04-07 04:07:56 +00001608bool ASTContext::typesAreCompatible(QualType LHS_NC, QualType RHS_NC) {
1609 QualType LHS = LHS_NC.getCanonicalType();
1610 QualType RHS = RHS_NC.getCanonicalType();
Chris Lattner4d5670b2008-04-03 05:07:04 +00001611
Bill Wendling6a9d8542007-12-03 07:33:35 +00001612 // C++ [expr]: If an expression initially has the type "reference to T", the
1613 // type is adjusted to "T" prior to any further analysis, the expression
1614 // designates the object or function denoted by the reference, and the
1615 // expression is an lvalue.
Chris Lattner855fed42008-04-07 04:07:56 +00001616 if (ReferenceType *RT = dyn_cast<ReferenceType>(LHS))
1617 LHS = RT->getPointeeType();
1618 if (ReferenceType *RT = dyn_cast<ReferenceType>(RHS))
1619 RHS = RT->getPointeeType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001620
Chris Lattnerd47d6042008-04-07 05:37:56 +00001621 // If two types are identical, they are compatible.
1622 if (LHS == RHS)
1623 return true;
1624
1625 // If qualifiers differ, the types are different.
Chris Lattnerb5709e22008-04-07 05:43:21 +00001626 unsigned LHSAS = LHS.getAddressSpace(), RHSAS = RHS.getAddressSpace();
1627 if (LHS.getCVRQualifiers() != RHS.getCVRQualifiers() || LHSAS != RHSAS)
Chris Lattnerd47d6042008-04-07 05:37:56 +00001628 return false;
Chris Lattnerb5709e22008-04-07 05:43:21 +00001629
1630 // Strip off ASQual's if present.
1631 if (LHSAS) {
1632 LHS = LHS.getUnqualifiedType();
1633 RHS = RHS.getUnqualifiedType();
1634 }
Chris Lattnerd47d6042008-04-07 05:37:56 +00001635
Chris Lattner855fed42008-04-07 04:07:56 +00001636 Type::TypeClass LHSClass = LHS->getTypeClass();
1637 Type::TypeClass RHSClass = RHS->getTypeClass();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001638
1639 // We want to consider the two function types to be the same for these
1640 // comparisons, just force one to the other.
1641 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1642 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00001643
1644 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00001645 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
1646 LHSClass = Type::ConstantArray;
1647 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
1648 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001649
Nate Begemanaf6ed502008-04-18 23:10:10 +00001650 // Canonicalize ExtVector -> Vector.
1651 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
1652 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00001653
Chris Lattner7cdcb252008-04-07 06:38:24 +00001654 // Consider qualified interfaces and interfaces the same.
1655 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
1656 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
1657
Chris Lattnerb5709e22008-04-07 05:43:21 +00001658 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001659 if (LHSClass != RHSClass) {
Chris Lattner7cdcb252008-04-07 06:38:24 +00001660 // ID is compatible with all interface types.
1661 if (isa<ObjCInterfaceType>(LHS))
1662 return isObjCIdType(RHS);
1663 if (isa<ObjCInterfaceType>(RHS))
1664 return isObjCIdType(LHS);
Chris Lattner0d3e6452008-04-07 05:53:18 +00001665
Chris Lattnerc38d4522008-01-14 05:45:46 +00001666 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1667 // a signed integer type, or an unsigned integer type.
Chris Lattner855fed42008-04-07 04:07:56 +00001668 if (LHS->isEnumeralType() && RHS->isIntegralType()) {
1669 EnumDecl* EDecl = cast<EnumType>(LHS)->getDecl();
1670 return EDecl->getIntegerType() == RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001671 }
Chris Lattner855fed42008-04-07 04:07:56 +00001672 if (RHS->isEnumeralType() && LHS->isIntegralType()) {
1673 EnumDecl* EDecl = cast<EnumType>(RHS)->getDecl();
1674 return EDecl->getIntegerType() == LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001675 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001676
Steve Naroff85f0dc52007-10-15 20:41:53 +00001677 return false;
1678 }
Chris Lattnerb5709e22008-04-07 05:43:21 +00001679
Steve Naroffc88babe2008-01-09 22:43:08 +00001680 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001681 switch (LHSClass) {
Chris Lattnerb5709e22008-04-07 05:43:21 +00001682 case Type::ASQual:
1683 case Type::FunctionProto:
1684 case Type::VariableArray:
1685 case Type::IncompleteArray:
1686 case Type::Reference:
Chris Lattner7cdcb252008-04-07 06:38:24 +00001687 case Type::ObjCQualifiedInterface:
Chris Lattnerb5709e22008-04-07 05:43:21 +00001688 assert(0 && "Canonicalized away above");
Chris Lattnerc38d4522008-01-14 05:45:46 +00001689 case Type::Pointer:
Chris Lattner855fed42008-04-07 04:07:56 +00001690 return pointerTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001691 case Type::ConstantArray:
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001692 return areCompatArrayTypes(cast<ArrayType>(LHS), cast<ArrayType>(RHS),
1693 *this);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001694 case Type::FunctionNoProto:
Chris Lattner855fed42008-04-07 04:07:56 +00001695 return functionTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001696 case Type::Tagged: // handle structures, unions
Chris Lattner390564e2008-04-07 06:49:41 +00001697 return areCompatTagTypes(cast<TagType>(LHS), cast<TagType>(RHS), *this);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001698 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00001699 // Only exactly equal builtin types are compatible, which is tested above.
1700 return false;
1701 case Type::Vector:
1702 return areCompatVectorTypes(cast<VectorType>(LHS), cast<VectorType>(RHS));
Chris Lattnerc38d4522008-01-14 05:45:46 +00001703 case Type::ObjCInterface:
Chris Lattner7cdcb252008-04-07 06:38:24 +00001704 return areCompatObjCInterfaces(cast<ObjCInterfaceType>(LHS),
1705 cast<ObjCInterfaceType>(RHS));
Chris Lattnerc38d4522008-01-14 05:45:46 +00001706 default:
1707 assert(0 && "unexpected type");
Steve Naroff85f0dc52007-10-15 20:41:53 +00001708 }
1709 return true; // should never get here...
1710}
Ted Kremenek738e6c02007-10-31 17:10:13 +00001711
Chris Lattner1d78a862008-04-07 07:01:58 +00001712//===----------------------------------------------------------------------===//
1713// Serialization Support
1714//===----------------------------------------------------------------------===//
1715
Ted Kremenek738e6c02007-10-31 17:10:13 +00001716/// Emit - Serialize an ASTContext object to Bitcode.
1717void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00001718 S.EmitRef(SourceMgr);
1719 S.EmitRef(Target);
1720 S.EmitRef(Idents);
1721 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001722
Ted Kremenek68228a92007-10-31 22:44:07 +00001723 // Emit the size of the type vector so that we can reserve that size
1724 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001725 S.EmitInt(Types.size());
1726
Ted Kremenek034a78c2007-11-13 22:02:55 +00001727 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1728 I!=E;++I)
1729 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001730
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001731 S.EmitOwnedPtr(TUDecl);
1732
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001733 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001734}
1735
Ted Kremenekacba3612007-11-13 00:25:37 +00001736ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek68228a92007-10-31 22:44:07 +00001737 SourceManager &SM = D.ReadRef<SourceManager>();
1738 TargetInfo &t = D.ReadRef<TargetInfo>();
1739 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1740 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00001741
Ted Kremenek68228a92007-10-31 22:44:07 +00001742 unsigned size_reserve = D.ReadInt();
1743
1744 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1745
Ted Kremenek034a78c2007-11-13 22:02:55 +00001746 for (unsigned i = 0; i < size_reserve; ++i)
1747 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00001748
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001749 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
1750
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001751 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00001752
1753 return A;
1754}