blob: 4a1fb39bb60afaaf452fe657e17682d445aeafc9 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Basic/TargetInfo.h"
18#include "llvm/ADT/SmallVector.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000019#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000020#include "llvm/Bitcode/Serialize.h"
21#include "llvm/Bitcode/Deserialize.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000022
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24
25enum FloatingRank {
26 FloatRank, DoubleRank, LongDoubleRank
27};
28
29ASTContext::~ASTContext() {
30 // Deallocate all the types.
31 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000032 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000033 Types.pop_back();
34 }
Eli Friedman65489b72008-05-27 03:08:09 +000035
36 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000037}
38
39void ASTContext::PrintStats() const {
40 fprintf(stderr, "*** AST Context Stats:\n");
41 fprintf(stderr, " %d types total.\n", (int)Types.size());
42 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
43 unsigned NumVector = 0, NumComplex = 0;
44 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
45
46 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000047 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
48 unsigned NumObjCQualifiedIds = 0;
Steve Naroffe0430632008-05-21 15:59:22 +000049 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000050
51 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
52 Type *T = Types[i];
53 if (isa<BuiltinType>(T))
54 ++NumBuiltin;
55 else if (isa<PointerType>(T))
56 ++NumPointer;
57 else if (isa<ReferenceType>(T))
58 ++NumReference;
59 else if (isa<ComplexType>(T))
60 ++NumComplex;
61 else if (isa<ArrayType>(T))
62 ++NumArray;
63 else if (isa<VectorType>(T))
64 ++NumVector;
65 else if (isa<FunctionTypeNoProto>(T))
66 ++NumFunctionNP;
67 else if (isa<FunctionTypeProto>(T))
68 ++NumFunctionP;
69 else if (isa<TypedefType>(T))
70 ++NumTypeName;
71 else if (TagType *TT = dyn_cast<TagType>(T)) {
72 ++NumTagged;
73 switch (TT->getDecl()->getKind()) {
74 default: assert(0 && "Unknown tagged type!");
75 case Decl::Struct: ++NumTagStruct; break;
76 case Decl::Union: ++NumTagUnion; break;
77 case Decl::Class: ++NumTagClass; break;
78 case Decl::Enum: ++NumTagEnum; break;
79 }
Ted Kremenek42730c52008-01-07 19:49:32 +000080 } else if (isa<ObjCInterfaceType>(T))
81 ++NumObjCInterfaces;
82 else if (isa<ObjCQualifiedInterfaceType>(T))
83 ++NumObjCQualifiedInterfaces;
84 else if (isa<ObjCQualifiedIdType>(T))
85 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +000086 else if (isa<TypeOfType>(T))
87 ++NumTypeOfTypes;
88 else if (isa<TypeOfExpr>(T))
89 ++NumTypeOfExprs;
Steve Naroff948fd372007-09-17 14:16:13 +000090 else {
Chris Lattner8a35b462007-12-12 06:43:05 +000091 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +000092 assert(0 && "Unknown type!");
93 }
94 }
95
96 fprintf(stderr, " %d builtin types\n", NumBuiltin);
97 fprintf(stderr, " %d pointer types\n", NumPointer);
98 fprintf(stderr, " %d reference types\n", NumReference);
99 fprintf(stderr, " %d complex types\n", NumComplex);
100 fprintf(stderr, " %d array types\n", NumArray);
101 fprintf(stderr, " %d vector types\n", NumVector);
102 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
103 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
104 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
105 fprintf(stderr, " %d tagged types\n", NumTagged);
106 fprintf(stderr, " %d struct types\n", NumTagStruct);
107 fprintf(stderr, " %d union types\n", NumTagUnion);
108 fprintf(stderr, " %d class types\n", NumTagClass);
109 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000110 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000111 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000112 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000113 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000114 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000115 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
116 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
117
Chris Lattner4b009652007-07-25 00:24:17 +0000118 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
119 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
120 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
121 NumFunctionP*sizeof(FunctionTypeProto)+
122 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroffe0430632008-05-21 15:59:22 +0000123 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
124 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Chris Lattner4b009652007-07-25 00:24:17 +0000125}
126
127
128void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
129 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
130}
131
Chris Lattner4b009652007-07-25 00:24:17 +0000132void ASTContext::InitBuiltinTypes() {
133 assert(VoidTy.isNull() && "Context reinitialized?");
134
135 // C99 6.2.5p19.
136 InitBuiltinType(VoidTy, BuiltinType::Void);
137
138 // C99 6.2.5p2.
139 InitBuiltinType(BoolTy, BuiltinType::Bool);
140 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000141 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000142 InitBuiltinType(CharTy, BuiltinType::Char_S);
143 else
144 InitBuiltinType(CharTy, BuiltinType::Char_U);
145 // C99 6.2.5p4.
146 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
147 InitBuiltinType(ShortTy, BuiltinType::Short);
148 InitBuiltinType(IntTy, BuiltinType::Int);
149 InitBuiltinType(LongTy, BuiltinType::Long);
150 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
151
152 // C99 6.2.5p6.
153 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
154 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
155 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
156 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
157 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
158
159 // C99 6.2.5p10.
160 InitBuiltinType(FloatTy, BuiltinType::Float);
161 InitBuiltinType(DoubleTy, BuiltinType::Double);
162 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
163
164 // C99 6.2.5p11.
165 FloatComplexTy = getComplexType(FloatTy);
166 DoubleComplexTy = getComplexType(DoubleTy);
167 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff9d12c902007-10-15 14:41:52 +0000168
169 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000170 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000171 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000172 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000173 ClassStructType = 0;
174
Ted Kremenek42730c52008-01-07 19:49:32 +0000175 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000176
177 // void * type
178 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000179}
180
181//===----------------------------------------------------------------------===//
182// Type Sizing and Analysis
183//===----------------------------------------------------------------------===//
184
185/// getTypeSize - Return the size of the specified type, in bits. This method
186/// does not work on incomplete types.
187std::pair<uint64_t, unsigned>
Chris Lattner8cd0e932008-03-05 18:54:05 +0000188ASTContext::getTypeInfo(QualType T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000189 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000190 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000191 unsigned Align;
192 switch (T->getTypeClass()) {
193 case Type::TypeName: assert(0 && "Not a canonical type!");
194 case Type::FunctionNoProto:
195 case Type::FunctionProto:
196 default:
197 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000198 case Type::VariableArray:
199 assert(0 && "VLAs not implemented yet!");
200 case Type::ConstantArray: {
201 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
202
Chris Lattner8cd0e932008-03-05 18:54:05 +0000203 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000204 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000205 Align = EltInfo.second;
206 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000207 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000208 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000209 case Type::Vector: {
210 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000211 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000212 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000213 // FIXME: This isn't right for unusual vectors
214 Align = Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000215 break;
216 }
217
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000218 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000219 switch (cast<BuiltinType>(T)->getKind()) {
220 default: assert(0 && "Unknown builtin type!");
221 case BuiltinType::Void:
222 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000223 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000224 Width = Target.getBoolWidth();
225 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000226 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000227 case BuiltinType::Char_S:
228 case BuiltinType::Char_U:
229 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000230 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000231 Width = Target.getCharWidth();
232 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000233 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000234 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000235 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000236 Width = Target.getShortWidth();
237 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000238 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000239 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000240 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000241 Width = Target.getIntWidth();
242 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000243 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000244 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000245 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000246 Width = Target.getLongWidth();
247 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000248 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000249 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000250 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000251 Width = Target.getLongLongWidth();
252 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000253 break;
254 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000255 Width = Target.getFloatWidth();
256 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000257 break;
258 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000259 Width = Target.getDoubleWidth();
260 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000261 break;
262 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000263 Width = Target.getLongDoubleWidth();
264 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000265 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000266 }
267 break;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000268 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000269 // FIXME: Pointers into different addr spaces could have different sizes and
270 // alignment requirements: getPointerInfo should take an AddrSpace.
271 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000272 case Type::ObjCQualifiedId:
Chris Lattner1d78a862008-04-07 07:01:58 +0000273 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000274 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000275 break;
Chris Lattner461a6c52008-03-08 08:34:58 +0000276 case Type::Pointer: {
277 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000278 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000279 Align = Target.getPointerAlign(AS);
280 break;
281 }
Chris Lattner4b009652007-07-25 00:24:17 +0000282 case Type::Reference:
283 // "When applied to a reference or a reference type, the result is the size
284 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000285 // FIXME: This is wrong for struct layout: a reference in a struct has
286 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000287 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner4b009652007-07-25 00:24:17 +0000288
289 case Type::Complex: {
290 // Complex types have the same alignment as their elements, but twice the
291 // size.
292 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000293 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000294 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000295 Align = EltInfo.second;
296 break;
297 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000298 case Type::ObjCInterface: {
299 ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
300 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
301 Width = Layout.getSize();
302 Align = Layout.getAlignment();
303 break;
304 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000305 case Type::Tagged: {
306 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
307 return getTypeInfo(ET->getDecl()->getIntegerType());
308
309 RecordType *RT = cast<RecordType>(T);
310 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
311 Width = Layout.getSize();
312 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000313 break;
314 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000315 }
Chris Lattner4b009652007-07-25 00:24:17 +0000316
317 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000318 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000319}
320
Devang Patelbfe323c2008-06-04 21:22:16 +0000321/// LayoutField - Field layout.
322void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
323 bool IsUnion, bool StructIsPacked,
324 ASTContext &Context) {
325 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
326 uint64_t FieldOffset = IsUnion ? 0 : Size;
327 uint64_t FieldSize;
328 unsigned FieldAlign;
329
330 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
331 // TODO: Need to check this algorithm on other targets!
332 // (tested on Linux-X86)
333 llvm::APSInt I(32);
334 bool BitWidthIsICE =
335 BitWidthExpr->isIntegerConstantExpr(I, Context);
336 assert (BitWidthIsICE && "Invalid BitField size expression");
337 FieldSize = I.getZExtValue();
338
339 std::pair<uint64_t, unsigned> FieldInfo =
340 Context.getTypeInfo(FD->getType());
341 uint64_t TypeSize = FieldInfo.first;
342
343 FieldAlign = FieldInfo.second;
344 if (FieldIsPacked)
345 FieldAlign = 1;
346 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
347 FieldAlign = std::max(FieldAlign, AA->getAlignment());
348
349 // Check if we need to add padding to give the field the correct
350 // alignment.
351 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
352 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
353
354 // Padding members don't affect overall alignment
355 if (!FD->getIdentifier())
356 FieldAlign = 1;
357 } else {
358 if (FD->getType()->isIncompleteType()) {
359 // This must be a flexible array member; we can't directly
360 // query getTypeInfo about these, so we figure it out here.
361 // Flexible array members don't have any size, but they
362 // have to be aligned appropriately for their element type.
363 FieldSize = 0;
364 const ArrayType* ATy = FD->getType()->getAsArrayType();
365 FieldAlign = Context.getTypeAlign(ATy->getElementType());
366 } else {
367 std::pair<uint64_t, unsigned> FieldInfo =
368 Context.getTypeInfo(FD->getType());
369 FieldSize = FieldInfo.first;
370 FieldAlign = FieldInfo.second;
371 }
372
373 if (FieldIsPacked)
374 FieldAlign = 8;
375 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
376 FieldAlign = std::max(FieldAlign, AA->getAlignment());
377
378 // Round up the current record size to the field's alignment boundary.
379 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
380 }
381
382 // Place this field at the current location.
383 FieldOffsets[FieldNo] = FieldOffset;
384
385 // Reserve space for this field.
386 if (IsUnion) {
387 Size = std::max(Size, FieldSize);
388 } else {
389 Size = FieldOffset + FieldSize;
390 }
391
392 // Remember max struct/class alignment.
393 Alignment = std::max(Alignment, FieldAlign);
394}
395
Devang Patel4b6bf702008-06-04 21:54:36 +0000396
397/// getASTObjcInterfaceLayout - Get or compute information about the layout of the
398/// specified Objective C, which indicates its size and ivar
399/// position information.
400const ASTRecordLayout &
401ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
402 // Look up this layout, if already laid out, return what we have.
403 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
404 if (Entry) return *Entry;
405
406 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
407 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
408 ASTRecordLayout *NewEntry = new ASTRecordLayout();
409 Entry = NewEntry;
410
411 NewEntry->InitializeLayout(D->ivar_size());
412 bool IsPacked = D->getAttr<PackedAttr>();
413
414 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
415 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
416 AA->getAlignment()));
417
418 // Layout each ivar sequentially.
419 unsigned i = 0;
420 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
421 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
422 const ObjCIvarDecl* Ivar = (*IVI);
423 NewEntry->LayoutField(Ivar, i++, false, IsPacked, *this);
424 }
425
426 // Finally, round the size of the total struct up to the alignment of the
427 // struct itself.
428 NewEntry->FinalizeLayout();
429 return *NewEntry;
430}
431
Devang Patel7a78e432007-11-01 19:11:01 +0000432/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000433/// specified record (struct/union/class), which indicates its size and field
434/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000435const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000436 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000437
Chris Lattner4b009652007-07-25 00:24:17 +0000438 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000439 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000440 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000441
Devang Patel7a78e432007-11-01 19:11:01 +0000442 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
443 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
444 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000445 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000446
Devang Patelbfe323c2008-06-04 21:22:16 +0000447 NewEntry->InitializeLayout(D->getNumMembers());
Eli Friedman5949a022008-05-30 09:31:38 +0000448 bool StructIsPacked = D->getAttr<PackedAttr>();
449 bool IsUnion = (D->getKind() == Decl::Union);
Chris Lattner4b009652007-07-25 00:24:17 +0000450
Eli Friedman5949a022008-05-30 09:31:38 +0000451 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000452 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
453 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000454
Eli Friedman5949a022008-05-30 09:31:38 +0000455 // Layout each field, for now, just sequentially, respecting alignment. In
456 // the future, this will need to be tweakable by targets.
457 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
458 const FieldDecl *FD = D->getMember(i);
Devang Patelbfe323c2008-06-04 21:22:16 +0000459 NewEntry->LayoutField(FD, i, IsUnion, StructIsPacked, *this);
Chris Lattner4b009652007-07-25 00:24:17 +0000460 }
Eli Friedman5949a022008-05-30 09:31:38 +0000461
462 // Finally, round the size of the total struct up to the alignment of the
463 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000464 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000465 return *NewEntry;
466}
467
Chris Lattner4b009652007-07-25 00:24:17 +0000468//===----------------------------------------------------------------------===//
469// Type creation/memoization methods
470//===----------------------------------------------------------------------===//
471
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000472QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000473 QualType CanT = getCanonicalType(T);
474 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000475 return T;
476
477 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
478 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000479 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000480 "Type is already address space qualified");
481
482 // Check if we've already instantiated an address space qual'd type of this
483 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000484 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000485 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000486 void *InsertPos = 0;
487 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
488 return QualType(ASQy, 0);
489
490 // If the base type isn't canonical, this won't be a canonical type either,
491 // so fill in the canonical type field.
492 QualType Canonical;
493 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000494 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000495
496 // Get the new insert position for the node we care about.
497 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
498 assert(NewIP == 0 && "Shouldn't be in the map!");
499 }
Chris Lattner35fef522008-02-20 20:55:12 +0000500 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000501 ASQualTypes.InsertNode(New, InsertPos);
502 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000503 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000504}
505
Chris Lattner4b009652007-07-25 00:24:17 +0000506
507/// getComplexType - Return the uniqued reference to the type for a complex
508/// number with the specified element type.
509QualType ASTContext::getComplexType(QualType T) {
510 // Unique pointers, to guarantee there is only one pointer of a particular
511 // structure.
512 llvm::FoldingSetNodeID ID;
513 ComplexType::Profile(ID, T);
514
515 void *InsertPos = 0;
516 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
517 return QualType(CT, 0);
518
519 // If the pointee type isn't canonical, this won't be a canonical type either,
520 // so fill in the canonical type field.
521 QualType Canonical;
522 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000523 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000524
525 // Get the new insert position for the node we care about.
526 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
527 assert(NewIP == 0 && "Shouldn't be in the map!");
528 }
529 ComplexType *New = new ComplexType(T, Canonical);
530 Types.push_back(New);
531 ComplexTypes.InsertNode(New, InsertPos);
532 return QualType(New, 0);
533}
534
535
536/// getPointerType - Return the uniqued reference to the type for a pointer to
537/// the specified type.
538QualType ASTContext::getPointerType(QualType T) {
539 // Unique pointers, to guarantee there is only one pointer of a particular
540 // structure.
541 llvm::FoldingSetNodeID ID;
542 PointerType::Profile(ID, T);
543
544 void *InsertPos = 0;
545 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
546 return QualType(PT, 0);
547
548 // If the pointee type isn't canonical, this won't be a canonical type either,
549 // so fill in the canonical type field.
550 QualType Canonical;
551 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000552 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000553
554 // Get the new insert position for the node we care about.
555 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
556 assert(NewIP == 0 && "Shouldn't be in the map!");
557 }
558 PointerType *New = new PointerType(T, Canonical);
559 Types.push_back(New);
560 PointerTypes.InsertNode(New, InsertPos);
561 return QualType(New, 0);
562}
563
564/// getReferenceType - Return the uniqued reference to the type for a reference
565/// to the specified type.
566QualType ASTContext::getReferenceType(QualType T) {
567 // Unique pointers, to guarantee there is only one pointer of a particular
568 // structure.
569 llvm::FoldingSetNodeID ID;
570 ReferenceType::Profile(ID, T);
571
572 void *InsertPos = 0;
573 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
574 return QualType(RT, 0);
575
576 // If the referencee type isn't canonical, this won't be a canonical type
577 // either, so fill in the canonical type field.
578 QualType Canonical;
579 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000580 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000581
582 // Get the new insert position for the node we care about.
583 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
584 assert(NewIP == 0 && "Shouldn't be in the map!");
585 }
586
587 ReferenceType *New = new ReferenceType(T, Canonical);
588 Types.push_back(New);
589 ReferenceTypes.InsertNode(New, InsertPos);
590 return QualType(New, 0);
591}
592
Steve Naroff83c13012007-08-30 01:06:46 +0000593/// getConstantArrayType - Return the unique reference to the type for an
594/// array of the specified element type.
595QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000596 const llvm::APInt &ArySize,
597 ArrayType::ArraySizeModifier ASM,
598 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000599 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000600 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000601
602 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000603 if (ConstantArrayType *ATP =
604 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000605 return QualType(ATP, 0);
606
607 // If the element type isn't canonical, this won't be a canonical type either,
608 // so fill in the canonical type field.
609 QualType Canonical;
610 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000611 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000612 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000613 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000614 ConstantArrayType *NewIP =
615 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
616
Chris Lattner4b009652007-07-25 00:24:17 +0000617 assert(NewIP == 0 && "Shouldn't be in the map!");
618 }
619
Steve Naroff24c9b982007-08-30 18:10:14 +0000620 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
621 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000622 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000623 Types.push_back(New);
624 return QualType(New, 0);
625}
626
Steve Naroffe2579e32007-08-30 18:14:25 +0000627/// getVariableArrayType - Returns a non-unique reference to the type for a
628/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000629QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
630 ArrayType::ArraySizeModifier ASM,
631 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000632 // Since we don't unique expressions, it isn't possible to unique VLA's
633 // that have an expression provided for their size.
634
635 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
636 ASM, EltTypeQuals);
637
638 VariableArrayTypes.push_back(New);
639 Types.push_back(New);
640 return QualType(New, 0);
641}
642
643QualType ASTContext::getIncompleteArrayType(QualType EltTy,
644 ArrayType::ArraySizeModifier ASM,
645 unsigned EltTypeQuals) {
646 llvm::FoldingSetNodeID ID;
647 IncompleteArrayType::Profile(ID, EltTy);
648
649 void *InsertPos = 0;
650 if (IncompleteArrayType *ATP =
651 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
652 return QualType(ATP, 0);
653
654 // If the element type isn't canonical, this won't be a canonical type
655 // either, so fill in the canonical type field.
656 QualType Canonical;
657
658 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000659 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000660 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000661
662 // Get the new insert position for the node we care about.
663 IncompleteArrayType *NewIP =
664 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
665
666 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000667 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000668
669 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
670 ASM, EltTypeQuals);
671
672 IncompleteArrayTypes.InsertNode(New, InsertPos);
673 Types.push_back(New);
674 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000675}
676
Chris Lattner4b009652007-07-25 00:24:17 +0000677/// getVectorType - Return the unique reference to a vector type of
678/// the specified element type and size. VectorType must be a built-in type.
679QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
680 BuiltinType *baseType;
681
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000682 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000683 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
684
685 // Check if we've already instantiated a vector of this type.
686 llvm::FoldingSetNodeID ID;
687 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
688 void *InsertPos = 0;
689 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
690 return QualType(VTP, 0);
691
692 // If the element type isn't canonical, this won't be a canonical type either,
693 // so fill in the canonical type field.
694 QualType Canonical;
695 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000696 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000697
698 // Get the new insert position for the node we care about.
699 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
700 assert(NewIP == 0 && "Shouldn't be in the map!");
701 }
702 VectorType *New = new VectorType(vecType, NumElts, Canonical);
703 VectorTypes.InsertNode(New, InsertPos);
704 Types.push_back(New);
705 return QualType(New, 0);
706}
707
Nate Begemanaf6ed502008-04-18 23:10:10 +0000708/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +0000709/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000710QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +0000711 BuiltinType *baseType;
712
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000713 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +0000714 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +0000715
716 // Check if we've already instantiated a vector of this type.
717 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000718 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +0000719 void *InsertPos = 0;
720 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
721 return QualType(VTP, 0);
722
723 // If the element type isn't canonical, this won't be a canonical type either,
724 // so fill in the canonical type field.
725 QualType Canonical;
726 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000727 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000728
729 // Get the new insert position for the node we care about.
730 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
731 assert(NewIP == 0 && "Shouldn't be in the map!");
732 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000733 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000734 VectorTypes.InsertNode(New, InsertPos);
735 Types.push_back(New);
736 return QualType(New, 0);
737}
738
739/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
740///
741QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
742 // Unique functions, to guarantee there is only one function of a particular
743 // structure.
744 llvm::FoldingSetNodeID ID;
745 FunctionTypeNoProto::Profile(ID, ResultTy);
746
747 void *InsertPos = 0;
748 if (FunctionTypeNoProto *FT =
749 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
750 return QualType(FT, 0);
751
752 QualType Canonical;
753 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000754 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000755
756 // Get the new insert position for the node we care about.
757 FunctionTypeNoProto *NewIP =
758 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
759 assert(NewIP == 0 && "Shouldn't be in the map!");
760 }
761
762 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
763 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000764 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000765 return QualType(New, 0);
766}
767
768/// getFunctionType - Return a normal function type with a typed argument
769/// list. isVariadic indicates whether the argument list includes '...'.
770QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
771 unsigned NumArgs, bool isVariadic) {
772 // Unique functions, to guarantee there is only one function of a particular
773 // structure.
774 llvm::FoldingSetNodeID ID;
775 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
776
777 void *InsertPos = 0;
778 if (FunctionTypeProto *FTP =
779 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
780 return QualType(FTP, 0);
781
782 // Determine whether the type being created is already canonical or not.
783 bool isCanonical = ResultTy->isCanonical();
784 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
785 if (!ArgArray[i]->isCanonical())
786 isCanonical = false;
787
788 // If this type isn't canonical, get the canonical version of it.
789 QualType Canonical;
790 if (!isCanonical) {
791 llvm::SmallVector<QualType, 16> CanonicalArgs;
792 CanonicalArgs.reserve(NumArgs);
793 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000794 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +0000795
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000796 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +0000797 &CanonicalArgs[0], NumArgs,
798 isVariadic);
799
800 // Get the new insert position for the node we care about.
801 FunctionTypeProto *NewIP =
802 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
803 assert(NewIP == 0 && "Shouldn't be in the map!");
804 }
805
806 // FunctionTypeProto objects are not allocated with new because they have a
807 // variable size array (for parameter types) at the end of them.
808 FunctionTypeProto *FTP =
809 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
810 NumArgs*sizeof(QualType));
811 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
812 Canonical);
813 Types.push_back(FTP);
814 FunctionTypeProtos.InsertNode(FTP, InsertPos);
815 return QualType(FTP, 0);
816}
817
Douglas Gregor1d661552008-04-13 21:07:44 +0000818/// getTypeDeclType - Return the unique reference to the type for the
819/// specified type declaration.
820QualType ASTContext::getTypeDeclType(TypeDecl *Decl) {
821 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
822
823 if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(Decl))
824 return getTypedefType(Typedef);
825 else if (ObjCInterfaceDecl *ObjCInterface
826 = dyn_cast_or_null<ObjCInterfaceDecl>(Decl))
827 return getObjCInterfaceType(ObjCInterface);
828 else if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Decl)) {
829 Decl->TypeForDecl = new RecordType(Record);
830 Types.push_back(Decl->TypeForDecl);
831 return QualType(Decl->TypeForDecl, 0);
832 } else if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Decl)) {
833 Decl->TypeForDecl = new EnumType(Enum);
834 Types.push_back(Decl->TypeForDecl);
835 return QualType(Decl->TypeForDecl, 0);
836 } else
837 assert(false && "TypeDecl without a type?");
838}
839
Chris Lattner4b009652007-07-25 00:24:17 +0000840/// getTypedefType - Return the unique reference to the type for the
841/// specified typename decl.
842QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
843 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
844
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000845 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000846 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000847 Types.push_back(Decl->TypeForDecl);
848 return QualType(Decl->TypeForDecl, 0);
849}
850
Ted Kremenek42730c52008-01-07 19:49:32 +0000851/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000852/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000853QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000854 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
855
Ted Kremenek42730c52008-01-07 19:49:32 +0000856 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000857 Types.push_back(Decl->TypeForDecl);
858 return QualType(Decl->TypeForDecl, 0);
859}
860
Chris Lattnere1352302008-04-07 04:56:42 +0000861/// CmpProtocolNames - Comparison predicate for sorting protocols
862/// alphabetically.
863static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
864 const ObjCProtocolDecl *RHS) {
865 return strcmp(LHS->getName(), RHS->getName()) < 0;
866}
867
868static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
869 unsigned &NumProtocols) {
870 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
871
872 // Sort protocols, keyed by name.
873 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
874
875 // Remove duplicates.
876 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
877 NumProtocols = ProtocolsEnd-Protocols;
878}
879
880
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +0000881/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
882/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000883QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
884 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000885 // Sort the protocol list alphabetically to canonicalize it.
886 SortAndUniqueProtocols(Protocols, NumProtocols);
887
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000888 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +0000889 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000890
891 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000892 if (ObjCQualifiedInterfaceType *QT =
893 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000894 return QualType(QT, 0);
895
896 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +0000897 ObjCQualifiedInterfaceType *QType =
898 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000899 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000900 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000901 return QualType(QType, 0);
902}
903
Chris Lattnere1352302008-04-07 04:56:42 +0000904/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
905/// and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000906QualType ASTContext::getObjCQualifiedIdType(QualType idType,
907 ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000908 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000909 // Sort the protocol list alphabetically to canonicalize it.
910 SortAndUniqueProtocols(Protocols, NumProtocols);
911
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000912 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000913 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000914
915 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000916 if (ObjCQualifiedIdType *QT =
917 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000918 return QualType(QT, 0);
919
920 // No Match;
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000921 QualType Canonical;
922 if (!idType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000923 Canonical = getObjCQualifiedIdType(getCanonicalType(idType),
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000924 Protocols, NumProtocols);
Ted Kremenek42730c52008-01-07 19:49:32 +0000925 ObjCQualifiedIdType *NewQT =
926 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000927 assert(NewQT == 0 && "Shouldn't be in the map!");
928 }
929
Ted Kremenek42730c52008-01-07 19:49:32 +0000930 ObjCQualifiedIdType *QType =
931 new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000932 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000933 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000934 return QualType(QType, 0);
935}
936
Steve Naroff0604dd92007-08-01 18:02:17 +0000937/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
938/// TypeOfExpr AST's (since expression's are never shared). For example,
939/// multiple declarations that refer to "typeof(x)" all contain different
940/// DeclRefExpr's. This doesn't effect the type checker, since it operates
941/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000942QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000943 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff0604dd92007-08-01 18:02:17 +0000944 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
945 Types.push_back(toe);
946 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000947}
948
Steve Naroff0604dd92007-08-01 18:02:17 +0000949/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
950/// TypeOfType AST's. The only motivation to unique these nodes would be
951/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
952/// an issue. This doesn't effect the type checker, since it operates
953/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000954QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000955 QualType Canonical = getCanonicalType(tofType);
Steve Naroff0604dd92007-08-01 18:02:17 +0000956 TypeOfType *tot = new TypeOfType(tofType, Canonical);
957 Types.push_back(tot);
958 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000959}
960
Chris Lattner4b009652007-07-25 00:24:17 +0000961/// getTagDeclType - Return the unique reference to the type for the
962/// specified TagDecl (struct/union/class/enum) decl.
963QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +0000964 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +0000965 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +0000966}
967
968/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
969/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
970/// needs to agree with the definition in <stddef.h>.
971QualType ASTContext::getSizeType() const {
972 // On Darwin, size_t is defined as a "long unsigned int".
973 // FIXME: should derive from "Target".
974 return UnsignedLongTy;
975}
976
Eli Friedmanfdd35d72008-02-12 08:29:21 +0000977/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
978/// width of characters in wide strings, The value is target dependent and
979/// needs to agree with the definition in <stddef.h>.
980QualType ASTContext::getWcharType() const {
981 // On Darwin, wchar_t is defined as a "int".
982 // FIXME: should derive from "Target".
983 return IntTy;
984}
985
Chris Lattner4b009652007-07-25 00:24:17 +0000986/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
987/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
988QualType ASTContext::getPointerDiffType() const {
989 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
990 // FIXME: should derive from "Target".
991 return IntTy;
992}
993
Chris Lattner19eb97e2008-04-02 05:18:44 +0000994//===----------------------------------------------------------------------===//
995// Type Operators
996//===----------------------------------------------------------------------===//
997
Chris Lattner3dae6f42008-04-06 22:41:35 +0000998/// getCanonicalType - Return the canonical (structural) type corresponding to
999/// the specified potentially non-canonical type. The non-canonical version
1000/// of a type may have many "decorated" versions of types. Decorators can
1001/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1002/// to be free of any of these, allowing two canonical types to be compared
1003/// for exact equality with a simple pointer comparison.
1004QualType ASTContext::getCanonicalType(QualType T) {
1005 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
1006 return QualType(CanType.getTypePtr(),
1007 T.getCVRQualifiers() | CanType.getCVRQualifiers());
1008}
1009
1010
Chris Lattner19eb97e2008-04-02 05:18:44 +00001011/// getArrayDecayedType - Return the properly qualified result of decaying the
1012/// specified array type to a pointer. This operation is non-trivial when
1013/// handling typedefs etc. The canonical type of "T" must be an array type,
1014/// this returns a pointer to a properly qualified element of the array.
1015///
1016/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1017QualType ASTContext::getArrayDecayedType(QualType Ty) {
1018 // Handle the common case where typedefs are not involved directly.
1019 QualType EltTy;
1020 unsigned ArrayQuals = 0;
1021 unsigned PointerQuals = 0;
1022 if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
1023 // Since T "isa" an array type, it could not have had an address space
1024 // qualifier, just CVR qualifiers. The properly qualified element pointer
1025 // gets the union of the CVR qualifiers from the element and the array, and
1026 // keeps any address space qualifier on the element type if present.
1027 EltTy = AT->getElementType();
1028 ArrayQuals = Ty.getCVRQualifiers();
1029 PointerQuals = AT->getIndexTypeQualifier();
1030 } else {
1031 // Otherwise, we have an ASQualType or a typedef, etc. Make sure we don't
1032 // lose qualifiers when dealing with typedefs. Example:
1033 // typedef int arr[10];
1034 // void test2() {
1035 // const arr b;
1036 // b[4] = 1;
1037 // }
1038 //
1039 // The decayed type of b is "const int*" even though the element type of the
1040 // array is "int".
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001041 QualType CanTy = getCanonicalType(Ty);
Chris Lattner19eb97e2008-04-02 05:18:44 +00001042 const ArrayType *PrettyArrayType = Ty->getAsArrayType();
1043 assert(PrettyArrayType && "Not an array type!");
1044
1045 // Get the element type with 'getAsArrayType' so that we don't lose any
1046 // typedefs in the element type of the array.
1047 EltTy = PrettyArrayType->getElementType();
1048
1049 // If the array was address-space qualifier, make sure to ASQual the element
1050 // type. We can just grab the address space from the canonical type.
1051 if (unsigned AS = CanTy.getAddressSpace())
1052 EltTy = getASQualType(EltTy, AS);
1053
1054 // To properly handle [multiple levels of] typedefs, typeof's etc, we take
1055 // the CVR qualifiers directly from the canonical type, which is guaranteed
1056 // to have the full set unioned together.
1057 ArrayQuals = CanTy.getCVRQualifiers();
1058 PointerQuals = PrettyArrayType->getIndexTypeQualifier();
1059 }
1060
Chris Lattnerda79b3f2008-04-02 06:06:35 +00001061 // Apply any CVR qualifiers from the array type to the element type. This
1062 // implements C99 6.7.3p8: "If the specification of an array type includes
1063 // any type qualifiers, the element type is so qualified, not the array type."
Chris Lattner19eb97e2008-04-02 05:18:44 +00001064 EltTy = EltTy.getQualifiedType(ArrayQuals | EltTy.getCVRQualifiers());
1065
1066 QualType PtrTy = getPointerType(EltTy);
1067
1068 // int x[restrict 4] -> int *restrict
1069 PtrTy = PtrTy.getQualifiedType(PointerQuals);
1070
1071 return PtrTy;
1072}
1073
Chris Lattner4b009652007-07-25 00:24:17 +00001074/// getFloatingRank - Return a relative rank for floating point types.
1075/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001076static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001077 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001078 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001079
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001080 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001081 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001082 case BuiltinType::Float: return FloatRank;
1083 case BuiltinType::Double: return DoubleRank;
1084 case BuiltinType::LongDouble: return LongDoubleRank;
1085 }
1086}
1087
Steve Narofffa0c4532007-08-27 01:41:48 +00001088/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1089/// point or a complex type (based on typeDomain/typeSize).
1090/// 'typeDomain' is a real floating point or complex type.
1091/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001092QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1093 QualType Domain) const {
1094 FloatingRank EltRank = getFloatingRank(Size);
1095 if (Domain->isComplexType()) {
1096 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001097 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001098 case FloatRank: return FloatComplexTy;
1099 case DoubleRank: return DoubleComplexTy;
1100 case LongDoubleRank: return LongDoubleComplexTy;
1101 }
Chris Lattner4b009652007-07-25 00:24:17 +00001102 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001103
1104 assert(Domain->isRealFloatingType() && "Unknown domain!");
1105 switch (EltRank) {
1106 default: assert(0 && "getFloatingRank(): illegal value for rank");
1107 case FloatRank: return FloatTy;
1108 case DoubleRank: return DoubleTy;
1109 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001110 }
Chris Lattner4b009652007-07-25 00:24:17 +00001111}
1112
Chris Lattner51285d82008-04-06 23:55:33 +00001113/// getFloatingTypeOrder - Compare the rank of the two specified floating
1114/// point types, ignoring the domain of the type (i.e. 'double' ==
1115/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1116/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001117int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1118 FloatingRank LHSR = getFloatingRank(LHS);
1119 FloatingRank RHSR = getFloatingRank(RHS);
1120
1121 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001122 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001123 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001124 return 1;
1125 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001126}
1127
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001128/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1129/// routine will assert if passed a built-in type that isn't an integer or enum,
1130/// or if it is not canonicalized.
1131static unsigned getIntegerRank(Type *T) {
1132 assert(T->isCanonical() && "T should be canonicalized");
1133 if (isa<EnumType>(T))
1134 return 4;
1135
1136 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001137 default: assert(0 && "getIntegerRank(): not a built-in integer");
1138 case BuiltinType::Bool:
1139 return 1;
1140 case BuiltinType::Char_S:
1141 case BuiltinType::Char_U:
1142 case BuiltinType::SChar:
1143 case BuiltinType::UChar:
1144 return 2;
1145 case BuiltinType::Short:
1146 case BuiltinType::UShort:
1147 return 3;
1148 case BuiltinType::Int:
1149 case BuiltinType::UInt:
1150 return 4;
1151 case BuiltinType::Long:
1152 case BuiltinType::ULong:
1153 return 5;
1154 case BuiltinType::LongLong:
1155 case BuiltinType::ULongLong:
1156 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001157 }
1158}
1159
Chris Lattner51285d82008-04-06 23:55:33 +00001160/// getIntegerTypeOrder - Returns the highest ranked integer type:
1161/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1162/// LHS < RHS, return -1.
1163int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001164 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1165 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001166 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001167
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001168 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1169 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001170
Chris Lattner51285d82008-04-06 23:55:33 +00001171 unsigned LHSRank = getIntegerRank(LHSC);
1172 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001173
Chris Lattner51285d82008-04-06 23:55:33 +00001174 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1175 if (LHSRank == RHSRank) return 0;
1176 return LHSRank > RHSRank ? 1 : -1;
1177 }
Chris Lattner4b009652007-07-25 00:24:17 +00001178
Chris Lattner51285d82008-04-06 23:55:33 +00001179 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1180 if (LHSUnsigned) {
1181 // If the unsigned [LHS] type is larger, return it.
1182 if (LHSRank >= RHSRank)
1183 return 1;
1184
1185 // If the signed type can represent all values of the unsigned type, it
1186 // wins. Because we are dealing with 2's complement and types that are
1187 // powers of two larger than each other, this is always safe.
1188 return -1;
1189 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001190
Chris Lattner51285d82008-04-06 23:55:33 +00001191 // If the unsigned [RHS] type is larger, return it.
1192 if (RHSRank >= LHSRank)
1193 return -1;
1194
1195 // If the signed type can represent all values of the unsigned type, it
1196 // wins. Because we are dealing with 2's complement and types that are
1197 // powers of two larger than each other, this is always safe.
1198 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001199}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001200
1201// getCFConstantStringType - Return the type used for constant CFStrings.
1202QualType ASTContext::getCFConstantStringType() {
1203 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001204 CFConstantStringTypeDecl =
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001205 RecordDecl::Create(*this, Decl::Struct, TUDecl, SourceLocation(),
Chris Lattner58114f02008-03-15 21:32:50 +00001206 &Idents.get("NSConstantString"), 0);
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001207 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001208
1209 // const int *isa;
1210 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001211 // int flags;
1212 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001213 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001214 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001215 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001216 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001217 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001218 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001219
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001220 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerf3874bc2008-04-06 04:47:34 +00001221 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner81db64a2008-03-16 00:16:02 +00001222 FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001223
1224 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1225 }
1226
1227 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001228}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001229
Anders Carlssone3f02572007-10-29 06:33:42 +00001230// This returns true if a type has been typedefed to BOOL:
1231// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001232static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001233 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001234 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001235
1236 return false;
1237}
1238
Ted Kremenek42730c52008-01-07 19:49:32 +00001239/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001240/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001241int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001242 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001243
1244 // Make all integer and enum types at least as large as an int
1245 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001246 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001247 // Treat arrays as pointers, since that's how they're passed in.
1248 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001249 sz = getTypeSize(VoidPtrTy);
1250 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001251}
1252
Ted Kremenek42730c52008-01-07 19:49:32 +00001253/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001254/// declaration.
Ted Kremenek42730c52008-01-07 19:49:32 +00001255void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001256 std::string& S)
1257{
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001258 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001259 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001260 // Encode result type.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001261 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001262 // Compute size of all parameters.
1263 // Start with computing size of a pointer in number of bytes.
1264 // FIXME: There might(should) be a better way of doing this computation!
1265 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001266 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001267 // The first two arguments (self and _cmd) are pointers; account for
1268 // their size.
1269 int ParmOffset = 2 * PtrSize;
1270 int NumOfParams = Decl->getNumParams();
1271 for (int i = 0; i < NumOfParams; i++) {
1272 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001273 int sz = getObjCEncodingTypeSize (PType);
1274 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001275 ParmOffset += sz;
1276 }
1277 S += llvm::utostr(ParmOffset);
1278 S += "@0:";
1279 S += llvm::utostr(PtrSize);
1280
1281 // Argument types.
1282 ParmOffset = 2 * PtrSize;
1283 for (int i = 0; i < NumOfParams; i++) {
1284 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001285 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001286 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001287 getObjCEncodingForTypeQualifier(
1288 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian248db262008-01-22 22:44:46 +00001289 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001290 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001291 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001292 }
1293}
1294
Fariborz Jahanian248db262008-01-22 22:44:46 +00001295void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1296 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson36f07d82007-10-29 05:01:08 +00001297{
Anders Carlssone3f02572007-10-29 06:33:42 +00001298 // FIXME: This currently doesn't encode:
1299 // @ An object (whether statically typed or typed id)
1300 // # A class object (Class)
1301 // : A method selector (SEL)
1302 // {name=type...} A structure
1303 // (name=type...) A union
1304 // bnum A bit field of num bits
1305
1306 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001307 char encoding;
1308 switch (BT->getKind()) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001309 default: assert(0 && "Unhandled builtin type kind");
1310 case BuiltinType::Void: encoding = 'v'; break;
1311 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001312 case BuiltinType::Char_U:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001313 case BuiltinType::UChar: encoding = 'C'; break;
1314 case BuiltinType::UShort: encoding = 'S'; break;
1315 case BuiltinType::UInt: encoding = 'I'; break;
1316 case BuiltinType::ULong: encoding = 'L'; break;
1317 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001318 case BuiltinType::Char_S:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001319 case BuiltinType::SChar: encoding = 'c'; break;
1320 case BuiltinType::Short: encoding = 's'; break;
1321 case BuiltinType::Int: encoding = 'i'; break;
1322 case BuiltinType::Long: encoding = 'l'; break;
1323 case BuiltinType::LongLong: encoding = 'q'; break;
1324 case BuiltinType::Float: encoding = 'f'; break;
1325 case BuiltinType::Double: encoding = 'd'; break;
1326 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001327 }
1328
1329 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001330 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001331 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001332 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001333 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001334
1335 }
1336 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001337 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001338 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001339 S += '@';
1340 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001341 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001342 S += '#';
1343 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001344 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001345 S += ':';
1346 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001347 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001348
1349 if (PointeeTy->isCharType()) {
1350 // char pointer types should be encoded as '*' unless it is a
1351 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001352 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001353 S += '*';
1354 return;
1355 }
1356 }
1357
1358 S += '^';
Fariborz Jahanian248db262008-01-22 22:44:46 +00001359 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone3f02572007-10-29 06:33:42 +00001360 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001361 S += '[';
1362
1363 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1364 S += llvm::utostr(CAT->getSize().getZExtValue());
1365 else
1366 assert(0 && "Unhandled array type!");
1367
Fariborz Jahanian248db262008-01-22 22:44:46 +00001368 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001369 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001370 } else if (T->getAsFunctionType()) {
1371 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001372 } else if (const RecordType *RTy = T->getAsRecordType()) {
1373 RecordDecl *RDecl= RTy->getDecl();
1374 S += '{';
1375 S += RDecl->getName();
Fariborz Jahanian248db262008-01-22 22:44:46 +00001376 bool found = false;
1377 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1378 if (ERType[i] == RTy) {
1379 found = true;
1380 break;
1381 }
1382 if (!found) {
1383 ERType.push_back(RTy);
1384 S += '=';
1385 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1386 FieldDecl *field = RDecl->getMember(i);
1387 getObjCEncodingForType(field->getType(), S, ERType);
1388 }
1389 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1390 ERType.pop_back();
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001391 }
1392 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001393 } else if (T->isEnumeralType()) {
1394 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001395 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001396 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001397}
1398
Ted Kremenek42730c52008-01-07 19:49:32 +00001399void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001400 std::string& S) const {
1401 if (QT & Decl::OBJC_TQ_In)
1402 S += 'n';
1403 if (QT & Decl::OBJC_TQ_Inout)
1404 S += 'N';
1405 if (QT & Decl::OBJC_TQ_Out)
1406 S += 'o';
1407 if (QT & Decl::OBJC_TQ_Bycopy)
1408 S += 'O';
1409 if (QT & Decl::OBJC_TQ_Byref)
1410 S += 'R';
1411 if (QT & Decl::OBJC_TQ_Oneway)
1412 S += 'V';
1413}
1414
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001415void ASTContext::setBuiltinVaListType(QualType T)
1416{
1417 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1418
1419 BuiltinVaListType = T;
1420}
1421
Ted Kremenek42730c52008-01-07 19:49:32 +00001422void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001423{
Ted Kremenek42730c52008-01-07 19:49:32 +00001424 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff9d12c902007-10-15 14:41:52 +00001425
Ted Kremenek42730c52008-01-07 19:49:32 +00001426 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001427
1428 // typedef struct objc_object *id;
1429 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1430 assert(ptr && "'id' incorrectly typed");
1431 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1432 assert(rec && "'id' incorrectly typed");
1433 IdStructType = rec;
1434}
1435
Ted Kremenek42730c52008-01-07 19:49:32 +00001436void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001437{
Ted Kremenek42730c52008-01-07 19:49:32 +00001438 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001439
Ted Kremenek42730c52008-01-07 19:49:32 +00001440 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001441
1442 // typedef struct objc_selector *SEL;
1443 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1444 assert(ptr && "'SEL' incorrectly typed");
1445 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1446 assert(rec && "'SEL' incorrectly typed");
1447 SelStructType = rec;
1448}
1449
Ted Kremenek42730c52008-01-07 19:49:32 +00001450void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001451{
Ted Kremenek42730c52008-01-07 19:49:32 +00001452 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1453 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001454}
1455
Ted Kremenek42730c52008-01-07 19:49:32 +00001456void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001457{
Ted Kremenek42730c52008-01-07 19:49:32 +00001458 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001459
Ted Kremenek42730c52008-01-07 19:49:32 +00001460 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001461
1462 // typedef struct objc_class *Class;
1463 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1464 assert(ptr && "'Class' incorrectly typed");
1465 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1466 assert(rec && "'Class' incorrectly typed");
1467 ClassStructType = rec;
1468}
1469
Ted Kremenek42730c52008-01-07 19:49:32 +00001470void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1471 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001472 "'NSConstantString' type already set!");
1473
Ted Kremenek42730c52008-01-07 19:49:32 +00001474 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001475}
1476
Chris Lattner6ff358b2008-04-07 06:51:04 +00001477//===----------------------------------------------------------------------===//
1478// Type Compatibility Testing
1479//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00001480
Chris Lattner390564e2008-04-07 06:49:41 +00001481/// C99 6.2.7p1: If both are complete types, then the following additional
1482/// requirements apply.
1483/// FIXME (handle compatibility across source files).
1484static bool areCompatTagTypes(TagType *LHS, TagType *RHS,
1485 const ASTContext &C) {
Steve Naroff4a5e2072007-11-07 06:03:51 +00001486 // "Class" and "id" are compatible built-in structure types.
Chris Lattner390564e2008-04-07 06:49:41 +00001487 if (C.isObjCIdType(QualType(LHS, 0)) && C.isObjCClassType(QualType(RHS, 0)) ||
1488 C.isObjCClassType(QualType(LHS, 0)) && C.isObjCIdType(QualType(RHS, 0)))
Steve Naroff4a5e2072007-11-07 06:03:51 +00001489 return true;
Eli Friedmane7fb03a2008-02-15 06:03:44 +00001490
Chris Lattner390564e2008-04-07 06:49:41 +00001491 // Within a translation unit a tag type is only compatible with itself. Self
1492 // equality is already handled by the time we get here.
1493 assert(LHS != RHS && "Self equality not handled!");
1494 return false;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001495}
1496
1497bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1498 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1499 // identically qualified and both shall be pointers to compatible types.
Chris Lattner35fef522008-02-20 20:55:12 +00001500 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1501 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001502 return false;
1503
1504 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1505 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1506
1507 return typesAreCompatible(ltype, rtype);
1508}
1509
Steve Naroff85f0dc52007-10-15 20:41:53 +00001510bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1511 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1512 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1513 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1514 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1515
1516 // first check the return types (common between C99 and K&R).
1517 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1518 return false;
1519
1520 if (lproto && rproto) { // two C99 style function prototypes
1521 unsigned lproto_nargs = lproto->getNumArgs();
1522 unsigned rproto_nargs = rproto->getNumArgs();
1523
1524 if (lproto_nargs != rproto_nargs)
1525 return false;
1526
1527 // both prototypes have the same number of arguments.
1528 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1529 (rproto->isVariadic() && !lproto->isVariadic()))
1530 return false;
1531
1532 // The use of ellipsis agree...now check the argument types.
1533 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001534 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1535 // is taken as having the unqualified version of it's declared type.
Steve Naroffdec17fe2008-01-29 00:15:50 +00001536 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001537 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001538 return false;
1539 return true;
1540 }
Chris Lattner1d78a862008-04-07 07:01:58 +00001541
Steve Naroff85f0dc52007-10-15 20:41:53 +00001542 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1543 return true;
1544
1545 // we have a mixture of K&R style with C99 prototypes
1546 const FunctionTypeProto *proto = lproto ? lproto : rproto;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001547 if (proto->isVariadic())
1548 return false;
1549
1550 // FIXME: Each parameter type T in the prototype must be compatible with the
1551 // type resulting from applying the usual argument conversions to T.
1552 return true;
1553}
1554
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001555// C99 6.7.5.2p6
1556static bool areCompatArrayTypes(ArrayType *LHS, ArrayType *RHS, ASTContext &C) {
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001557 // Constant arrays must be the same size to be compatible.
1558 if (const ConstantArrayType* LCAT = dyn_cast<ConstantArrayType>(LHS))
1559 if (const ConstantArrayType* RCAT = dyn_cast<ConstantArrayType>(RHS))
1560 if (RCAT->getSize() != LCAT->getSize())
1561 return false;
Eli Friedman1e7537832008-02-06 04:53:22 +00001562
Chris Lattnerc8971d72008-04-07 06:58:21 +00001563 // Compatible arrays must have compatible element types
1564 return C.typesAreCompatible(LHS->getElementType(), RHS->getElementType());
Steve Naroff85f0dc52007-10-15 20:41:53 +00001565}
1566
Chris Lattner6ff358b2008-04-07 06:51:04 +00001567/// areCompatVectorTypes - Return true if the two specified vector types are
1568/// compatible.
1569static bool areCompatVectorTypes(const VectorType *LHS,
1570 const VectorType *RHS) {
1571 assert(LHS->isCanonical() && RHS->isCanonical());
1572 return LHS->getElementType() == RHS->getElementType() &&
1573 LHS->getNumElements() == RHS->getNumElements();
1574}
1575
1576/// areCompatObjCInterfaces - Return true if the two interface types are
1577/// compatible for assignment from RHS to LHS. This handles validation of any
1578/// protocol qualifiers on the LHS or RHS.
1579///
Chris Lattner1d78a862008-04-07 07:01:58 +00001580static bool areCompatObjCInterfaces(const ObjCInterfaceType *LHS,
1581 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00001582 // Verify that the base decls are compatible: the RHS must be a subclass of
1583 // the LHS.
1584 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1585 return false;
1586
1587 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1588 // protocol qualified at all, then we are good.
1589 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1590 return true;
1591
1592 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1593 // isn't a superset.
1594 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1595 return true; // FIXME: should return false!
1596
1597 // Finally, we must have two protocol-qualified interfaces.
1598 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1599 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1600 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1601 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1602 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1603 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1604
1605 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1606 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1607 // LHS in a single parallel scan until we run out of elements in LHS.
1608 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1609 ObjCProtocolDecl *LHSProto = *LHSPI;
1610
1611 while (RHSPI != RHSPE) {
1612 ObjCProtocolDecl *RHSProto = *RHSPI++;
1613 // If the RHS has a protocol that the LHS doesn't, ignore it.
1614 if (RHSProto != LHSProto)
1615 continue;
1616
1617 // Otherwise, the RHS does have this element.
1618 ++LHSPI;
1619 if (LHSPI == LHSPE)
1620 return true; // All protocols in LHS exist in RHS.
1621
1622 LHSProto = *LHSPI;
1623 }
1624
1625 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1626 return false;
1627}
1628
1629
Steve Naroff85f0dc52007-10-15 20:41:53 +00001630/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1631/// both shall have the identically qualified version of a compatible type.
1632/// C99 6.2.7p1: Two types have compatible types if their types are the
1633/// same. See 6.7.[2,3,5] for additional rules.
Chris Lattner855fed42008-04-07 04:07:56 +00001634bool ASTContext::typesAreCompatible(QualType LHS_NC, QualType RHS_NC) {
1635 QualType LHS = LHS_NC.getCanonicalType();
1636 QualType RHS = RHS_NC.getCanonicalType();
Chris Lattner4d5670b2008-04-03 05:07:04 +00001637
Bill Wendling6a9d8542007-12-03 07:33:35 +00001638 // C++ [expr]: If an expression initially has the type "reference to T", the
1639 // type is adjusted to "T" prior to any further analysis, the expression
1640 // designates the object or function denoted by the reference, and the
1641 // expression is an lvalue.
Chris Lattner855fed42008-04-07 04:07:56 +00001642 if (ReferenceType *RT = dyn_cast<ReferenceType>(LHS))
1643 LHS = RT->getPointeeType();
1644 if (ReferenceType *RT = dyn_cast<ReferenceType>(RHS))
1645 RHS = RT->getPointeeType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001646
Chris Lattnerd47d6042008-04-07 05:37:56 +00001647 // If two types are identical, they are compatible.
1648 if (LHS == RHS)
1649 return true;
1650
1651 // If qualifiers differ, the types are different.
Chris Lattnerb5709e22008-04-07 05:43:21 +00001652 unsigned LHSAS = LHS.getAddressSpace(), RHSAS = RHS.getAddressSpace();
1653 if (LHS.getCVRQualifiers() != RHS.getCVRQualifiers() || LHSAS != RHSAS)
Chris Lattnerd47d6042008-04-07 05:37:56 +00001654 return false;
Chris Lattnerb5709e22008-04-07 05:43:21 +00001655
1656 // Strip off ASQual's if present.
1657 if (LHSAS) {
1658 LHS = LHS.getUnqualifiedType();
1659 RHS = RHS.getUnqualifiedType();
1660 }
Chris Lattnerd47d6042008-04-07 05:37:56 +00001661
Chris Lattner855fed42008-04-07 04:07:56 +00001662 Type::TypeClass LHSClass = LHS->getTypeClass();
1663 Type::TypeClass RHSClass = RHS->getTypeClass();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001664
1665 // We want to consider the two function types to be the same for these
1666 // comparisons, just force one to the other.
1667 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1668 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00001669
1670 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00001671 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
1672 LHSClass = Type::ConstantArray;
1673 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
1674 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001675
Nate Begemanaf6ed502008-04-18 23:10:10 +00001676 // Canonicalize ExtVector -> Vector.
1677 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
1678 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00001679
Chris Lattner7cdcb252008-04-07 06:38:24 +00001680 // Consider qualified interfaces and interfaces the same.
1681 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
1682 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
1683
Chris Lattnerb5709e22008-04-07 05:43:21 +00001684 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001685 if (LHSClass != RHSClass) {
Chris Lattner7cdcb252008-04-07 06:38:24 +00001686 // ID is compatible with all interface types.
1687 if (isa<ObjCInterfaceType>(LHS))
1688 return isObjCIdType(RHS);
1689 if (isa<ObjCInterfaceType>(RHS))
1690 return isObjCIdType(LHS);
Steve Naroff44549772008-06-04 15:07:33 +00001691
1692 // ID is compatible with all qualified id types.
1693 if (isa<ObjCQualifiedIdType>(LHS)) {
1694 if (const PointerType *PT = RHS->getAsPointerType())
1695 return isObjCIdType(PT->getPointeeType());
1696 }
1697 if (isa<ObjCQualifiedIdType>(RHS)) {
1698 if (const PointerType *PT = LHS->getAsPointerType())
1699 return isObjCIdType(PT->getPointeeType());
1700 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001701 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1702 // a signed integer type, or an unsigned integer type.
Chris Lattner855fed42008-04-07 04:07:56 +00001703 if (LHS->isEnumeralType() && RHS->isIntegralType()) {
1704 EnumDecl* EDecl = cast<EnumType>(LHS)->getDecl();
1705 return EDecl->getIntegerType() == RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001706 }
Chris Lattner855fed42008-04-07 04:07:56 +00001707 if (RHS->isEnumeralType() && LHS->isIntegralType()) {
1708 EnumDecl* EDecl = cast<EnumType>(RHS)->getDecl();
1709 return EDecl->getIntegerType() == LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001710 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001711
Steve Naroff85f0dc52007-10-15 20:41:53 +00001712 return false;
1713 }
Chris Lattnerb5709e22008-04-07 05:43:21 +00001714
Steve Naroffc88babe2008-01-09 22:43:08 +00001715 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001716 switch (LHSClass) {
Chris Lattnerb5709e22008-04-07 05:43:21 +00001717 case Type::ASQual:
1718 case Type::FunctionProto:
1719 case Type::VariableArray:
1720 case Type::IncompleteArray:
1721 case Type::Reference:
Chris Lattner7cdcb252008-04-07 06:38:24 +00001722 case Type::ObjCQualifiedInterface:
Chris Lattnerb5709e22008-04-07 05:43:21 +00001723 assert(0 && "Canonicalized away above");
Chris Lattnerc38d4522008-01-14 05:45:46 +00001724 case Type::Pointer:
Chris Lattner855fed42008-04-07 04:07:56 +00001725 return pointerTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001726 case Type::ConstantArray:
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001727 return areCompatArrayTypes(cast<ArrayType>(LHS), cast<ArrayType>(RHS),
1728 *this);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001729 case Type::FunctionNoProto:
Chris Lattner855fed42008-04-07 04:07:56 +00001730 return functionTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001731 case Type::Tagged: // handle structures, unions
Chris Lattner390564e2008-04-07 06:49:41 +00001732 return areCompatTagTypes(cast<TagType>(LHS), cast<TagType>(RHS), *this);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001733 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00001734 // Only exactly equal builtin types are compatible, which is tested above.
1735 return false;
1736 case Type::Vector:
1737 return areCompatVectorTypes(cast<VectorType>(LHS), cast<VectorType>(RHS));
Chris Lattnerc38d4522008-01-14 05:45:46 +00001738 case Type::ObjCInterface:
Chris Lattner7cdcb252008-04-07 06:38:24 +00001739 return areCompatObjCInterfaces(cast<ObjCInterfaceType>(LHS),
1740 cast<ObjCInterfaceType>(RHS));
Chris Lattnerc38d4522008-01-14 05:45:46 +00001741 default:
1742 assert(0 && "unexpected type");
Steve Naroff85f0dc52007-10-15 20:41:53 +00001743 }
1744 return true; // should never get here...
1745}
Ted Kremenek738e6c02007-10-31 17:10:13 +00001746
Chris Lattner1d78a862008-04-07 07:01:58 +00001747//===----------------------------------------------------------------------===//
1748// Serialization Support
1749//===----------------------------------------------------------------------===//
1750
Ted Kremenek738e6c02007-10-31 17:10:13 +00001751/// Emit - Serialize an ASTContext object to Bitcode.
1752void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00001753 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00001754 S.EmitRef(SourceMgr);
1755 S.EmitRef(Target);
1756 S.EmitRef(Idents);
1757 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001758
Ted Kremenek68228a92007-10-31 22:44:07 +00001759 // Emit the size of the type vector so that we can reserve that size
1760 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001761 S.EmitInt(Types.size());
1762
Ted Kremenek034a78c2007-11-13 22:02:55 +00001763 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1764 I!=E;++I)
1765 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001766
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001767 S.EmitOwnedPtr(TUDecl);
1768
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001769 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001770}
1771
Ted Kremenekacba3612007-11-13 00:25:37 +00001772ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00001773
1774 // Read the language options.
1775 LangOptions LOpts;
1776 LOpts.Read(D);
1777
Ted Kremenek68228a92007-10-31 22:44:07 +00001778 SourceManager &SM = D.ReadRef<SourceManager>();
1779 TargetInfo &t = D.ReadRef<TargetInfo>();
1780 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1781 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00001782
Ted Kremenek68228a92007-10-31 22:44:07 +00001783 unsigned size_reserve = D.ReadInt();
1784
Ted Kremenek842126e2008-06-04 15:55:15 +00001785 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00001786
Ted Kremenek034a78c2007-11-13 22:02:55 +00001787 for (unsigned i = 0; i < size_reserve; ++i)
1788 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00001789
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001790 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
1791
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001792 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00001793
1794 return A;
1795}