blob: b62438cd61a765d3ed1b9ef1f249084ce3d9459f [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"
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
Chris Lattnerb09b31d2009-03-28 03:45:20 +000020#include "clang/Basic/SourceManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021#include "clang/Basic/TargetInfo.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000022#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000023#include "llvm/Bitcode/Serialize.h"
24#include "llvm/Bitcode/Deserialize.h"
Nate Begeman7903d052009-01-18 06:42:49 +000025#include "llvm/Support/MathExtras.h"
Chris Lattnerf4fbc442009-03-28 04:27:18 +000026#include "llvm/Support/MemoryBuffer.h"
Chris Lattner4b009652007-07-25 00:24:17 +000027using namespace clang;
28
29enum FloatingRank {
30 FloatRank, DoubleRank, LongDoubleRank
31};
32
Chris Lattner2fda0ed2008-10-05 17:34:18 +000033ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
34 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000035 IdentifierTable &idents, SelectorTable &sels,
Steve Naroff207b9ec2009-01-27 23:20:32 +000036 bool FreeMem, unsigned size_reserve) :
Douglas Gregor1e589cc2009-03-26 23:50:42 +000037 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
38 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Chris Lattner9c18fde2009-03-28 01:44:40 +000039 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels) {
Daniel Dunbarde300732008-08-11 04:54:23 +000040 if (size_reserve > 0) Types.reserve(size_reserve);
41 InitBuiltinTypes();
Chris Lattner911b8672009-03-13 22:38:49 +000042 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.NoBuiltin);
Daniel Dunbarde300732008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
44}
45
Chris Lattner4b009652007-07-25 00:24:17 +000046ASTContext::~ASTContext() {
47 // Deallocate all the types.
48 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000049 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000050 Types.pop_back();
51 }
Eli Friedman65489b72008-05-27 03:08:09 +000052
Nuno Lopes355a8682008-12-17 22:30:25 +000053 {
54 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
55 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
56 while (I != E) {
57 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
58 delete R;
59 }
60 }
61
62 {
63 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
64 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
65 while (I != E) {
66 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
67 delete R;
68 }
69 }
70
71 {
72 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator
73 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
74 while (I != E) {
75 RecordDecl *R = const_cast<RecordDecl*>((I++)->second);
76 R->Destroy(*this);
77 }
78 }
79
Douglas Gregor1e589cc2009-03-26 23:50:42 +000080 // Destroy nested-name-specifiers.
Douglas Gregor3c4eae52009-03-27 23:54:10 +000081 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
82 NNS = NestedNameSpecifiers.begin(),
83 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregorbccd97c2009-03-27 23:25:45 +000084 NNS != NNSEnd;
Douglas Gregor3c4eae52009-03-27 23:54:10 +000085 /* Increment in loop */)
86 (*NNS++).Destroy(*this);
Douglas Gregor1e589cc2009-03-26 23:50:42 +000087
88 if (GlobalNestedNameSpecifier)
89 GlobalNestedNameSpecifier->Destroy(*this);
90
Eli Friedman65489b72008-05-27 03:08:09 +000091 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000092}
93
94void ASTContext::PrintStats() const {
95 fprintf(stderr, "*** AST Context Stats:\n");
96 fprintf(stderr, " %d types total.\n", (int)Types.size());
97 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar47677342008-09-26 03:23:00 +000098 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +000099 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
100 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
101
Chris Lattner4b009652007-07-25 00:24:17 +0000102 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000103 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
104 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000105 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000106
107 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
108 Type *T = Types[i];
109 if (isa<BuiltinType>(T))
110 ++NumBuiltin;
111 else if (isa<PointerType>(T))
112 ++NumPointer;
Daniel Dunbar47677342008-09-26 03:23:00 +0000113 else if (isa<BlockPointerType>(T))
114 ++NumBlockPointer;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000115 else if (isa<LValueReferenceType>(T))
116 ++NumLValueReference;
117 else if (isa<RValueReferenceType>(T))
118 ++NumRValueReference;
Sebastian Redl75555032009-01-24 21:16:55 +0000119 else if (isa<MemberPointerType>(T))
120 ++NumMemberPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000121 else if (isa<ComplexType>(T))
122 ++NumComplex;
123 else if (isa<ArrayType>(T))
124 ++NumArray;
125 else if (isa<VectorType>(T))
126 ++NumVector;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000127 else if (isa<FunctionNoProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000128 ++NumFunctionNP;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000129 else if (isa<FunctionProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000130 ++NumFunctionP;
131 else if (isa<TypedefType>(T))
132 ++NumTypeName;
133 else if (TagType *TT = dyn_cast<TagType>(T)) {
134 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000135 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000136 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000137 case TagDecl::TK_struct: ++NumTagStruct; break;
138 case TagDecl::TK_union: ++NumTagUnion; break;
139 case TagDecl::TK_class: ++NumTagClass; break;
140 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000141 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000142 } else if (isa<ObjCInterfaceType>(T))
143 ++NumObjCInterfaces;
144 else if (isa<ObjCQualifiedInterfaceType>(T))
145 ++NumObjCQualifiedInterfaces;
146 else if (isa<ObjCQualifiedIdType>(T))
147 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +0000148 else if (isa<TypeOfType>(T))
149 ++NumTypeOfTypes;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000150 else if (isa<TypeOfExprType>(T))
151 ++NumTypeOfExprTypes;
Steve Naroff948fd372007-09-17 14:16:13 +0000152 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000153 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000154 assert(0 && "Unknown type!");
155 }
156 }
157
158 fprintf(stderr, " %d builtin types\n", NumBuiltin);
159 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar47677342008-09-26 03:23:00 +0000160 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000161 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
162 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redl75555032009-01-24 21:16:55 +0000163 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000164 fprintf(stderr, " %d complex types\n", NumComplex);
165 fprintf(stderr, " %d array types\n", NumArray);
166 fprintf(stderr, " %d vector types\n", NumVector);
167 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
168 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
169 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
170 fprintf(stderr, " %d tagged types\n", NumTagged);
171 fprintf(stderr, " %d struct types\n", NumTagStruct);
172 fprintf(stderr, " %d union types\n", NumTagUnion);
173 fprintf(stderr, " %d class types\n", NumTagClass);
174 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000175 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000176 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000177 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000178 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000179 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000180 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000181 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000182
Chris Lattner4b009652007-07-25 00:24:17 +0000183 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
184 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
185 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redlce6fff02009-03-16 23:22:08 +0000186 NumLValueReference*sizeof(LValueReferenceType)+
187 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redl75555032009-01-24 21:16:55 +0000188 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor4fa58902009-02-26 23:50:07 +0000189 NumFunctionP*sizeof(FunctionProtoType)+
190 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroffe0430632008-05-21 15:59:22 +0000191 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregor4fa58902009-02-26 23:50:07 +0000192 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)));
Chris Lattner4b009652007-07-25 00:24:17 +0000193}
194
195
196void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroff93fd2112009-01-27 22:08:43 +0000197 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000198}
199
Chris Lattner4b009652007-07-25 00:24:17 +0000200void ASTContext::InitBuiltinTypes() {
201 assert(VoidTy.isNull() && "Context reinitialized?");
202
203 // C99 6.2.5p19.
204 InitBuiltinType(VoidTy, BuiltinType::Void);
205
206 // C99 6.2.5p2.
207 InitBuiltinType(BoolTy, BuiltinType::Bool);
208 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000209 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000210 InitBuiltinType(CharTy, BuiltinType::Char_S);
211 else
212 InitBuiltinType(CharTy, BuiltinType::Char_U);
213 // C99 6.2.5p4.
214 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
215 InitBuiltinType(ShortTy, BuiltinType::Short);
216 InitBuiltinType(IntTy, BuiltinType::Int);
217 InitBuiltinType(LongTy, BuiltinType::Long);
218 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
219
220 // C99 6.2.5p6.
221 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
222 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
223 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
224 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
225 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
226
227 // C99 6.2.5p10.
228 InitBuiltinType(FloatTy, BuiltinType::Float);
229 InitBuiltinType(DoubleTy, BuiltinType::Double);
230 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000231
Chris Lattnere1dafe72009-02-26 23:43:47 +0000232 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
233 InitBuiltinType(WCharTy, BuiltinType::WChar);
234 else // C99
235 WCharTy = getFromTargetType(Target.getWCharType());
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000236
Douglas Gregord2baafd2008-10-21 16:13:35 +0000237 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000238 InitBuiltinType(OverloadTy, BuiltinType::Overload);
239
240 // Placeholder type for type-dependent expressions whose type is
241 // completely unknown. No code should ever check a type against
242 // DependentTy and users should never see it; however, it is here to
243 // help diagnose failures to properly check for type-dependent
244 // expressions.
245 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000246
Chris Lattner4b009652007-07-25 00:24:17 +0000247 // C99 6.2.5p11.
248 FloatComplexTy = getComplexType(FloatTy);
249 DoubleComplexTy = getComplexType(DoubleTy);
250 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000251
Steve Naroff9d12c902007-10-15 14:41:52 +0000252 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000253 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000254 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000255 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000256 ClassStructType = 0;
257
Ted Kremenek42730c52008-01-07 19:49:32 +0000258 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000259
260 // void * type
261 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000262}
263
264//===----------------------------------------------------------------------===//
265// Type Sizing and Analysis
266//===----------------------------------------------------------------------===//
267
Chris Lattner2a674dc2008-06-30 18:32:54 +0000268/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
269/// scalar floating point type.
270const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
271 const BuiltinType *BT = T->getAsBuiltinType();
272 assert(BT && "Not a floating point type!");
273 switch (BT->getKind()) {
274 default: assert(0 && "Not a floating point type!");
275 case BuiltinType::Float: return Target.getFloatFormat();
276 case BuiltinType::Double: return Target.getDoubleFormat();
277 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
278 }
279}
280
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000281/// getDeclAlign - Return a conservative estimate of the alignment of the
282/// specified decl. Note that bitfields do not have a valid alignment, so
283/// this method will assert on them.
Daniel Dunbar96d1f1b2009-02-17 22:16:19 +0000284unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedman0ee57322009-02-22 02:56:25 +0000285 unsigned Align = Target.getCharWidth();
286
287 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
288 Align = std::max(Align, AA->getAlignment());
289
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000290 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
291 QualType T = VD->getType();
292 // Incomplete or function types default to 1.
Eli Friedman0ee57322009-02-22 02:56:25 +0000293 if (!T->isIncompleteType() && !T->isFunctionType()) {
294 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
295 T = cast<ArrayType>(T)->getElementType();
296
297 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
298 }
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000299 }
Eli Friedman0ee57322009-02-22 02:56:25 +0000300
301 return Align / Target.getCharWidth();
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000302}
Chris Lattner2a674dc2008-06-30 18:32:54 +0000303
Chris Lattner4b009652007-07-25 00:24:17 +0000304/// getTypeSize - Return the size of the specified type, in bits. This method
305/// does not work on incomplete types.
306std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000307ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000308 T = getCanonicalType(T);
Mike Stump44d1f402009-02-27 18:32:39 +0000309 uint64_t Width=0;
310 unsigned Align=8;
Chris Lattner4b009652007-07-25 00:24:17 +0000311 switch (T->getTypeClass()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000312#define TYPE(Class, Base)
313#define ABSTRACT_TYPE(Class, Base)
314#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
315#define DEPENDENT_TYPE(Class, Base) case Type::Class:
316#include "clang/AST/TypeNodes.def"
317 assert(false && "Should not see non-canonical or dependent types");
318 break;
319
Chris Lattner4b009652007-07-25 00:24:17 +0000320 case Type::FunctionNoProto:
321 case Type::FunctionProto:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000322 case Type::IncompleteArray:
Chris Lattner4b009652007-07-25 00:24:17 +0000323 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000324 case Type::VariableArray:
325 assert(0 && "VLAs not implemented yet!");
326 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000327 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000328
Chris Lattner8cd0e932008-03-05 18:54:05 +0000329 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000330 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000331 Align = EltInfo.second;
332 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000333 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000334 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000335 case Type::Vector: {
336 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000337 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000338 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000339 Align = Width;
Nate Begeman7903d052009-01-18 06:42:49 +0000340 // If the alignment is not a power of 2, round up to the next power of 2.
341 // This happens for non-power-of-2 length vectors.
342 // FIXME: this should probably be a target property.
343 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000344 break;
345 }
346
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000347 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000348 switch (cast<BuiltinType>(T)->getKind()) {
349 default: assert(0 && "Unknown builtin type!");
350 case BuiltinType::Void:
351 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000352 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000353 Width = Target.getBoolWidth();
354 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000355 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000356 case BuiltinType::Char_S:
357 case BuiltinType::Char_U:
358 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000359 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000360 Width = Target.getCharWidth();
361 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000362 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000363 case BuiltinType::WChar:
364 Width = Target.getWCharWidth();
365 Align = Target.getWCharAlign();
366 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000367 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000368 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000369 Width = Target.getShortWidth();
370 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000371 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000372 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000373 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000374 Width = Target.getIntWidth();
375 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000376 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000377 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000378 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000379 Width = Target.getLongWidth();
380 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000381 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000382 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000383 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000384 Width = Target.getLongLongWidth();
385 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000386 break;
387 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000388 Width = Target.getFloatWidth();
389 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000390 break;
391 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000392 Width = Target.getDoubleWidth();
393 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000394 break;
395 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000396 Width = Target.getLongDoubleWidth();
397 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000398 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000399 }
400 break;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000401 case Type::FixedWidthInt:
402 // FIXME: This isn't precisely correct; the width/alignment should depend
403 // on the available types for the target
404 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattnere9174982009-02-15 21:20:13 +0000405 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000406 Align = Width;
407 break;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000408 case Type::ExtQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000409 // FIXME: Pointers into different addr spaces could have different sizes and
410 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000411 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000412 case Type::ObjCQualifiedId:
Eli Friedman2f6d70d2009-02-22 04:02:33 +0000413 case Type::ObjCQualifiedClass:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000414 case Type::ObjCQualifiedInterface:
Chris Lattner1d78a862008-04-07 07:01:58 +0000415 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000416 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000417 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000418 case Type::BlockPointer: {
419 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
420 Width = Target.getPointerWidth(AS);
421 Align = Target.getPointerAlign(AS);
422 break;
423 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000424 case Type::Pointer: {
425 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000426 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000427 Align = Target.getPointerAlign(AS);
428 break;
429 }
Sebastian Redlce6fff02009-03-16 23:22:08 +0000430 case Type::LValueReference:
431 case Type::RValueReference:
Chris Lattner4b009652007-07-25 00:24:17 +0000432 // "When applied to a reference or a reference type, the result is the size
433 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000434 // FIXME: This is wrong for struct layout: a reference in a struct has
435 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000436 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl75555032009-01-24 21:16:55 +0000437 case Type::MemberPointer: {
Sebastian Redl18cffee2009-01-24 23:29:36 +0000438 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redl75555032009-01-24 21:16:55 +0000439 // the GCC ABI, where pointers to data are one pointer large, pointers to
440 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl18cffee2009-01-24 23:29:36 +0000441 // other compilers too, we need to delegate this completely to TargetInfo
442 // or some ABI abstraction layer.
Sebastian Redl75555032009-01-24 21:16:55 +0000443 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
444 unsigned AS = Pointee.getAddressSpace();
445 Width = Target.getPointerWidth(AS);
446 if (Pointee->isFunctionType())
447 Width *= 2;
448 Align = Target.getPointerAlign(AS);
449 // GCC aligns at single pointer width.
450 }
Chris Lattner4b009652007-07-25 00:24:17 +0000451 case Type::Complex: {
452 // Complex types have the same alignment as their elements, but twice the
453 // size.
454 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000455 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000456 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000457 Align = EltInfo.second;
458 break;
459 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000460 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000461 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000462 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
463 Width = Layout.getSize();
464 Align = Layout.getAlignment();
465 break;
466 }
Douglas Gregor4fa58902009-02-26 23:50:07 +0000467 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000468 case Type::Enum: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000469 const TagType *TT = cast<TagType>(T);
470
471 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000472 Width = 1;
473 Align = 1;
474 break;
475 }
476
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000477 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000478 return getTypeInfo(ET->getDecl()->getIntegerType());
479
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000480 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000481 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
482 Width = Layout.getSize();
483 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000484 break;
485 }
Douglas Gregordd13e842009-03-30 22:58:21 +0000486
487 case Type::TemplateSpecialization:
488 assert(false && "Dependent types have no size");
489 break;
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000490 }
Chris Lattner4b009652007-07-25 00:24:17 +0000491
492 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000493 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000494}
495
Chris Lattner83165b52009-01-27 18:08:34 +0000496/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
497/// type for the current target in bits. This can be different than the ABI
498/// alignment in cases where it is beneficial for performance to overalign
499/// a data type.
500unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
501 unsigned ABIAlign = getTypeAlign(T);
502
503 // Doubles should be naturally aligned if possible.
Daniel Dunbarc61a8002009-02-18 19:59:32 +0000504 if (T->isSpecificBuiltinType(BuiltinType::Double))
505 return std::max(ABIAlign, 64U);
Chris Lattner83165b52009-01-27 18:08:34 +0000506
507 return ABIAlign;
508}
509
510
Devang Patelbfe323c2008-06-04 21:22:16 +0000511/// LayoutField - Field layout.
512void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000513 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000514 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000515 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000516 uint64_t FieldOffset = IsUnion ? 0 : Size;
517 uint64_t FieldSize;
518 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000519
520 // FIXME: Should this override struct packing? Probably we want to
521 // take the minimum?
522 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
523 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000524
525 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
526 // TODO: Need to check this algorithm on other targets!
527 // (tested on Linux-X86)
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +0000528 FieldSize =
529 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000530
531 std::pair<uint64_t, unsigned> FieldInfo =
532 Context.getTypeInfo(FD->getType());
533 uint64_t TypeSize = FieldInfo.first;
534
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000535 // Determine the alignment of this bitfield. The packing
536 // attributes define a maximum and the alignment attribute defines
537 // a minimum.
538 // FIXME: What is the right behavior when the specified alignment
539 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000540 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000541 if (FieldPacking)
542 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000543 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
544 FieldAlign = std::max(FieldAlign, AA->getAlignment());
545
546 // Check if we need to add padding to give the field the correct
547 // alignment.
548 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
549 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
550
551 // Padding members don't affect overall alignment
552 if (!FD->getIdentifier())
553 FieldAlign = 1;
554 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000555 if (FD->getType()->isIncompleteArrayType()) {
556 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000557 // query getTypeInfo about these, so we figure it out here.
558 // Flexible array members don't have any size, but they
559 // have to be aligned appropriately for their element type.
560 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000561 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000562 FieldAlign = Context.getTypeAlign(ATy->getElementType());
563 } else {
564 std::pair<uint64_t, unsigned> FieldInfo =
565 Context.getTypeInfo(FD->getType());
566 FieldSize = FieldInfo.first;
567 FieldAlign = FieldInfo.second;
568 }
569
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000570 // Determine the alignment of this bitfield. The packing
571 // attributes define a maximum and the alignment attribute defines
572 // a minimum. Additionally, the packing alignment must be at least
573 // a byte for non-bitfields.
574 //
575 // FIXME: What is the right behavior when the specified alignment
576 // is smaller than the specified packing?
577 if (FieldPacking)
578 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000579 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
580 FieldAlign = std::max(FieldAlign, AA->getAlignment());
581
582 // Round up the current record size to the field's alignment boundary.
583 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
584 }
585
586 // Place this field at the current location.
587 FieldOffsets[FieldNo] = FieldOffset;
588
589 // Reserve space for this field.
590 if (IsUnion) {
591 Size = std::max(Size, FieldSize);
592 } else {
593 Size = FieldOffset + FieldSize;
594 }
595
596 // Remember max struct/class alignment.
597 Alignment = std::max(Alignment, FieldAlign);
598}
599
Fariborz Jahaniana2c97df2009-03-05 20:08:48 +0000600void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
601 std::vector<FieldDecl*> &Fields) const {
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000602 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
603 if (SuperClass)
604 CollectObjCIvars(SuperClass, Fields);
605 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
606 E = OI->ivar_end(); I != E; ++I) {
607 ObjCIvarDecl *IVDecl = (*I);
608 if (!IVDecl->isInvalidDecl())
609 Fields.push_back(cast<FieldDecl>(IVDecl));
610 }
Fariborz Jahanianc625fa92009-03-31 00:06:29 +0000611 // look into properties.
612 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
613 E = OI->prop_end(); I != E; ++I) {
614 ObjCPropertyDecl *PDecl = (*I);
615 if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl())
616 Fields.push_back(cast<FieldDecl>(IV));
617 }
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000618}
619
620/// addRecordToClass - produces record info. for the class for its
621/// ivars and all those inherited.
622///
623const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
624{
625 const RecordDecl *&RD = ASTRecordForInterface[D];
626 if (RD)
627 return RD;
628 std::vector<FieldDecl*> RecFields;
629 CollectObjCIvars(D, RecFields);
630 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
631 D->getLocation(),
632 D->getIdentifier());
633 /// FIXME! Can do collection of ivars and adding to the record while
634 /// doing it.
635 for (unsigned int i = 0; i != RecFields.size(); i++) {
636 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
637 RecFields[i]->getLocation(),
638 RecFields[i]->getIdentifier(),
639 RecFields[i]->getType(),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000640 RecFields[i]->getBitWidth(), false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000641 NewRD->addDecl(Field);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000642 }
643 NewRD->completeDefinition(*this);
644 RD = NewRD;
645 return RD;
646}
Devang Patel4b6bf702008-06-04 21:54:36 +0000647
Fariborz Jahanianea944842008-12-18 17:29:46 +0000648/// setFieldDecl - maps a field for the given Ivar reference node.
649//
650void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
651 const ObjCIvarDecl *Ivar,
652 const ObjCIvarRefExpr *MRef) {
653 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
654 lookupFieldDeclForIvar(*this, Ivar);
655 ASTFieldForIvarRef[MRef] = FD;
656}
657
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000658/// getASTObjcInterfaceLayout - Get or compute information about the layout of
659/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000660/// position information.
661const ASTRecordLayout &
662ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
663 // Look up this layout, if already laid out, return what we have.
664 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
665 if (Entry) return *Entry;
666
667 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
668 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000669 ASTRecordLayout *NewEntry = NULL;
670 unsigned FieldCount = D->ivar_size();
671 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
672 FieldCount++;
673 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
674 unsigned Alignment = SL.getAlignment();
675 uint64_t Size = SL.getSize();
676 NewEntry = new ASTRecordLayout(Size, Alignment);
677 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000678 // Super class is at the beginning of the layout.
679 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000680 } else {
681 NewEntry = new ASTRecordLayout();
682 NewEntry->InitializeLayout(FieldCount);
683 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000684 Entry = NewEntry;
685
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000686 unsigned StructPacking = 0;
687 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
688 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000689
690 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
691 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
692 AA->getAlignment()));
693
694 // Layout each ivar sequentially.
695 unsigned i = 0;
696 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
697 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
698 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000699 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000700 }
701
702 // Finally, round the size of the total struct up to the alignment of the
703 // struct itself.
704 NewEntry->FinalizeLayout();
705 return *NewEntry;
706}
707
Devang Patel7a78e432007-11-01 19:11:01 +0000708/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000709/// specified record (struct/union/class), which indicates its size and field
710/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000711const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000712 D = D->getDefinition(*this);
713 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000714
Chris Lattner4b009652007-07-25 00:24:17 +0000715 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000716 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000717 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000718
Devang Patel7a78e432007-11-01 19:11:01 +0000719 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
720 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
721 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000722 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000723
Douglas Gregor39677622008-12-11 20:41:00 +0000724 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000725 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000726 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000727
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000728 unsigned StructPacking = 0;
729 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
730 StructPacking = PA->getAlignment();
731
Eli Friedman5949a022008-05-30 09:31:38 +0000732 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000733 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
734 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000735
Eli Friedman5949a022008-05-30 09:31:38 +0000736 // Layout each field, for now, just sequentially, respecting alignment. In
737 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000738 unsigned FieldIdx = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000739 for (RecordDecl::field_iterator Field = D->field_begin(),
740 FieldEnd = D->field_end();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000741 Field != FieldEnd; (void)++Field, ++FieldIdx)
742 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000743
744 // Finally, round the size of the total struct up to the alignment of the
745 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000746 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000747 return *NewEntry;
748}
749
Chris Lattner4b009652007-07-25 00:24:17 +0000750//===----------------------------------------------------------------------===//
751// Type creation/memoization methods
752//===----------------------------------------------------------------------===//
753
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000754QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000755 QualType CanT = getCanonicalType(T);
756 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000757 return T;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000758
759 // If we are composing extended qualifiers together, merge together into one
760 // ExtQualType node.
761 unsigned CVRQuals = T.getCVRQualifiers();
762 QualType::GCAttrTypes GCAttr = QualType::GCNone;
763 Type *TypeNode = T.getTypePtr();
Chris Lattner35fef522008-02-20 20:55:12 +0000764
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000765 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
766 // If this type already has an address space specified, it cannot get
767 // another one.
768 assert(EQT->getAddressSpace() == 0 &&
769 "Type cannot be in multiple addr spaces!");
770 GCAttr = EQT->getObjCGCAttr();
771 TypeNode = EQT->getBaseType();
772 }
Chris Lattner35fef522008-02-20 20:55:12 +0000773
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000774 // Check if we've already instantiated this type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000775 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000776 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000777 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000778 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000779 return QualType(EXTQy, CVRQuals);
780
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000781 // If the base type isn't canonical, this won't be a canonical type either,
782 // so fill in the canonical type field.
783 QualType Canonical;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000784 if (!TypeNode->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000785 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000786
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000787 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000788 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000789 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000790 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000791 ExtQualType *New =
792 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000793 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000794 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000795 return QualType(New, CVRQuals);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000796}
797
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000798QualType ASTContext::getObjCGCQualType(QualType T,
799 QualType::GCAttrTypes GCAttr) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000800 QualType CanT = getCanonicalType(T);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000801 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000802 return T;
803
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000804 // If we are composing extended qualifiers together, merge together into one
805 // ExtQualType node.
806 unsigned CVRQuals = T.getCVRQualifiers();
807 Type *TypeNode = T.getTypePtr();
808 unsigned AddressSpace = 0;
809
810 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
811 // If this type already has an address space specified, it cannot get
812 // another one.
813 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
814 "Type cannot be in multiple addr spaces!");
815 AddressSpace = EQT->getAddressSpace();
816 TypeNode = EQT->getBaseType();
817 }
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000818
819 // Check if we've already instantiated an gc qual'd type of this type.
820 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000821 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000822 void *InsertPos = 0;
823 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000824 return QualType(EXTQy, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000825
826 // If the base type isn't canonical, this won't be a canonical type either,
827 // so fill in the canonical type field.
Eli Friedman94fcc9a2009-02-27 23:04:43 +0000828 // FIXME: Isn't this also not canonical if the base type is a array
829 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000830 QualType Canonical;
831 if (!T->isCanonical()) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000832 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000833
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000834 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000835 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
836 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
837 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000838 ExtQualType *New =
839 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000840 ExtQualTypes.InsertNode(New, InsertPos);
841 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000842 return QualType(New, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000843}
Chris Lattner4b009652007-07-25 00:24:17 +0000844
845/// getComplexType - Return the uniqued reference to the type for a complex
846/// number with the specified element type.
847QualType ASTContext::getComplexType(QualType T) {
848 // Unique pointers, to guarantee there is only one pointer of a particular
849 // structure.
850 llvm::FoldingSetNodeID ID;
851 ComplexType::Profile(ID, T);
852
853 void *InsertPos = 0;
854 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
855 return QualType(CT, 0);
856
857 // If the pointee type isn't canonical, this won't be a canonical type either,
858 // so fill in the canonical type field.
859 QualType Canonical;
860 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000861 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000862
863 // Get the new insert position for the node we care about.
864 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000865 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000866 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000867 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000868 Types.push_back(New);
869 ComplexTypes.InsertNode(New, InsertPos);
870 return QualType(New, 0);
871}
872
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000873QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
874 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
875 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
876 FixedWidthIntType *&Entry = Map[Width];
877 if (!Entry)
878 Entry = new FixedWidthIntType(Width, Signed);
879 return QualType(Entry, 0);
880}
Chris Lattner4b009652007-07-25 00:24:17 +0000881
882/// getPointerType - Return the uniqued reference to the type for a pointer to
883/// the specified type.
884QualType ASTContext::getPointerType(QualType T) {
885 // Unique pointers, to guarantee there is only one pointer of a particular
886 // structure.
887 llvm::FoldingSetNodeID ID;
888 PointerType::Profile(ID, T);
889
890 void *InsertPos = 0;
891 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
892 return QualType(PT, 0);
893
894 // If the pointee type isn't canonical, this won't be a canonical type either,
895 // so fill in the canonical type field.
896 QualType Canonical;
897 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000898 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000899
900 // Get the new insert position for the node we care about.
901 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000902 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000903 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000904 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000905 Types.push_back(New);
906 PointerTypes.InsertNode(New, InsertPos);
907 return QualType(New, 0);
908}
909
Steve Naroff7aa54752008-08-27 16:04:49 +0000910/// getBlockPointerType - Return the uniqued reference to the type for
911/// a pointer to the specified block.
912QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000913 assert(T->isFunctionType() && "block of function types only");
914 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000915 // structure.
916 llvm::FoldingSetNodeID ID;
917 BlockPointerType::Profile(ID, T);
918
919 void *InsertPos = 0;
920 if (BlockPointerType *PT =
921 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
922 return QualType(PT, 0);
923
Steve Narofffd5b19d2008-08-28 19:20:44 +0000924 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000925 // type either so fill in the canonical type field.
926 QualType Canonical;
927 if (!T->isCanonical()) {
928 Canonical = getBlockPointerType(getCanonicalType(T));
929
930 // Get the new insert position for the node we care about.
931 BlockPointerType *NewIP =
932 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000933 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000934 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000935 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000936 Types.push_back(New);
937 BlockPointerTypes.InsertNode(New, InsertPos);
938 return QualType(New, 0);
939}
940
Sebastian Redlce6fff02009-03-16 23:22:08 +0000941/// getLValueReferenceType - Return the uniqued reference to the type for an
942/// lvalue reference to the specified type.
943QualType ASTContext::getLValueReferenceType(QualType T) {
Chris Lattner4b009652007-07-25 00:24:17 +0000944 // Unique pointers, to guarantee there is only one pointer of a particular
945 // structure.
946 llvm::FoldingSetNodeID ID;
947 ReferenceType::Profile(ID, T);
948
949 void *InsertPos = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000950 if (LValueReferenceType *RT =
951 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000952 return QualType(RT, 0);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000953
Chris Lattner4b009652007-07-25 00:24:17 +0000954 // If the referencee type isn't canonical, this won't be a canonical type
955 // either, so fill in the canonical type field.
956 QualType Canonical;
957 if (!T->isCanonical()) {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000958 Canonical = getLValueReferenceType(getCanonicalType(T));
959
Chris Lattner4b009652007-07-25 00:24:17 +0000960 // Get the new insert position for the node we care about.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000961 LValueReferenceType *NewIP =
962 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000963 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000964 }
965
Sebastian Redlce6fff02009-03-16 23:22:08 +0000966 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000967 Types.push_back(New);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000968 LValueReferenceTypes.InsertNode(New, InsertPos);
969 return QualType(New, 0);
970}
971
972/// getRValueReferenceType - Return the uniqued reference to the type for an
973/// rvalue reference to the specified type.
974QualType ASTContext::getRValueReferenceType(QualType T) {
975 // Unique pointers, to guarantee there is only one pointer of a particular
976 // structure.
977 llvm::FoldingSetNodeID ID;
978 ReferenceType::Profile(ID, T);
979
980 void *InsertPos = 0;
981 if (RValueReferenceType *RT =
982 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
983 return QualType(RT, 0);
984
985 // If the referencee type isn't canonical, this won't be a canonical type
986 // either, so fill in the canonical type field.
987 QualType Canonical;
988 if (!T->isCanonical()) {
989 Canonical = getRValueReferenceType(getCanonicalType(T));
990
991 // Get the new insert position for the node we care about.
992 RValueReferenceType *NewIP =
993 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
994 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
995 }
996
997 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
998 Types.push_back(New);
999 RValueReferenceTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001000 return QualType(New, 0);
1001}
1002
Sebastian Redl75555032009-01-24 21:16:55 +00001003/// getMemberPointerType - Return the uniqued reference to the type for a
1004/// member pointer to the specified type, in the specified class.
1005QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1006{
1007 // Unique pointers, to guarantee there is only one pointer of a particular
1008 // structure.
1009 llvm::FoldingSetNodeID ID;
1010 MemberPointerType::Profile(ID, T, Cls);
1011
1012 void *InsertPos = 0;
1013 if (MemberPointerType *PT =
1014 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1015 return QualType(PT, 0);
1016
1017 // If the pointee or class type isn't canonical, this won't be a canonical
1018 // type either, so fill in the canonical type field.
1019 QualType Canonical;
1020 if (!T->isCanonical()) {
1021 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1022
1023 // Get the new insert position for the node we care about.
1024 MemberPointerType *NewIP =
1025 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1026 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1027 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001028 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +00001029 Types.push_back(New);
1030 MemberPointerTypes.InsertNode(New, InsertPos);
1031 return QualType(New, 0);
1032}
1033
Steve Naroff83c13012007-08-30 01:06:46 +00001034/// getConstantArrayType - Return the unique reference to the type for an
1035/// array of the specified element type.
1036QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +00001037 const llvm::APInt &ArySize,
1038 ArrayType::ArraySizeModifier ASM,
1039 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001040 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001041 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001042
1043 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +00001044 if (ConstantArrayType *ATP =
1045 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001046 return QualType(ATP, 0);
1047
1048 // If the element type isn't canonical, this won't be a canonical type either,
1049 // so fill in the canonical type field.
1050 QualType Canonical;
1051 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001052 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +00001053 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001054 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +00001055 ConstantArrayType *NewIP =
1056 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001057 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001058 }
1059
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001060 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001061 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001062 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001063 Types.push_back(New);
1064 return QualType(New, 0);
1065}
1066
Steve Naroffe2579e32007-08-30 18:14:25 +00001067/// getVariableArrayType - Returns a non-unique reference to the type for a
1068/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +00001069QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1070 ArrayType::ArraySizeModifier ASM,
1071 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +00001072 // Since we don't unique expressions, it isn't possible to unique VLA's
1073 // that have an expression provided for their size.
1074
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001075 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001076 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001077
1078 VariableArrayTypes.push_back(New);
1079 Types.push_back(New);
1080 return QualType(New, 0);
1081}
1082
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001083/// getDependentSizedArrayType - Returns a non-unique reference to
1084/// the type for a dependently-sized array of the specified element
1085/// type. FIXME: We will need these to be uniqued, or at least
1086/// comparable, at some point.
1087QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1088 ArrayType::ArraySizeModifier ASM,
1089 unsigned EltTypeQuals) {
1090 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1091 "Size must be type- or value-dependent!");
1092
1093 // Since we don't unique expressions, it isn't possible to unique
1094 // dependently-sized array types.
1095
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001096 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001097 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1098 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001099
1100 DependentSizedArrayTypes.push_back(New);
1101 Types.push_back(New);
1102 return QualType(New, 0);
1103}
1104
Eli Friedman8ff07782008-02-15 18:16:39 +00001105QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1106 ArrayType::ArraySizeModifier ASM,
1107 unsigned EltTypeQuals) {
1108 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001109 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001110
1111 void *InsertPos = 0;
1112 if (IncompleteArrayType *ATP =
1113 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1114 return QualType(ATP, 0);
1115
1116 // If the element type isn't canonical, this won't be a canonical type
1117 // either, so fill in the canonical type field.
1118 QualType Canonical;
1119
1120 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001121 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001122 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001123
1124 // Get the new insert position for the node we care about.
1125 IncompleteArrayType *NewIP =
1126 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001127 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001128 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001129
Steve Naroff93fd2112009-01-27 22:08:43 +00001130 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001131 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001132
1133 IncompleteArrayTypes.InsertNode(New, InsertPos);
1134 Types.push_back(New);
1135 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001136}
1137
Chris Lattner4b009652007-07-25 00:24:17 +00001138/// getVectorType - Return the unique reference to a vector type of
1139/// the specified element type and size. VectorType must be a built-in type.
1140QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1141 BuiltinType *baseType;
1142
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001143 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001144 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1145
1146 // Check if we've already instantiated a vector of this type.
1147 llvm::FoldingSetNodeID ID;
1148 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1149 void *InsertPos = 0;
1150 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1151 return QualType(VTP, 0);
1152
1153 // If the element type isn't canonical, this won't be a canonical type either,
1154 // so fill in the canonical type field.
1155 QualType Canonical;
1156 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001157 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001158
1159 // Get the new insert position for the node we care about.
1160 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001161 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001162 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001163 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001164 VectorTypes.InsertNode(New, InsertPos);
1165 Types.push_back(New);
1166 return QualType(New, 0);
1167}
1168
Nate Begemanaf6ed502008-04-18 23:10:10 +00001169/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001170/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001171QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001172 BuiltinType *baseType;
1173
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001174 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001175 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001176
1177 // Check if we've already instantiated a vector of this type.
1178 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001179 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001180 void *InsertPos = 0;
1181 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1182 return QualType(VTP, 0);
1183
1184 // If the element type isn't canonical, this won't be a canonical type either,
1185 // so fill in the canonical type field.
1186 QualType Canonical;
1187 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001188 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001189
1190 // Get the new insert position for the node we care about.
1191 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001192 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001193 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001194 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001195 VectorTypes.InsertNode(New, InsertPos);
1196 Types.push_back(New);
1197 return QualType(New, 0);
1198}
1199
Douglas Gregor4fa58902009-02-26 23:50:07 +00001200/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001201///
Douglas Gregor4fa58902009-02-26 23:50:07 +00001202QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Chris Lattner4b009652007-07-25 00:24:17 +00001203 // Unique functions, to guarantee there is only one function of a particular
1204 // structure.
1205 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001206 FunctionNoProtoType::Profile(ID, ResultTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001207
1208 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001209 if (FunctionNoProtoType *FT =
1210 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001211 return QualType(FT, 0);
1212
1213 QualType Canonical;
1214 if (!ResultTy->isCanonical()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00001215 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001216
1217 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001218 FunctionNoProtoType *NewIP =
1219 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001220 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001221 }
1222
Douglas Gregor4fa58902009-02-26 23:50:07 +00001223 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001224 Types.push_back(New);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001225 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001226 return QualType(New, 0);
1227}
1228
1229/// getFunctionType - Return a normal function type with a typed argument
1230/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001231QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001232 unsigned NumArgs, bool isVariadic,
1233 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001234 // Unique functions, to guarantee there is only one function of a particular
1235 // structure.
1236 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001237 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001238 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001239
1240 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001241 if (FunctionProtoType *FTP =
1242 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001243 return QualType(FTP, 0);
1244
1245 // Determine whether the type being created is already canonical or not.
1246 bool isCanonical = ResultTy->isCanonical();
1247 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1248 if (!ArgArray[i]->isCanonical())
1249 isCanonical = false;
1250
1251 // If this type isn't canonical, get the canonical version of it.
1252 QualType Canonical;
1253 if (!isCanonical) {
1254 llvm::SmallVector<QualType, 16> CanonicalArgs;
1255 CanonicalArgs.reserve(NumArgs);
1256 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001257 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001258
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001259 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001260 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001261 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001262
1263 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001264 FunctionProtoType *NewIP =
1265 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001266 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001267 }
1268
Douglas Gregor4fa58902009-02-26 23:50:07 +00001269 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001270 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001271 FunctionProtoType *FTP =
1272 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001273 NumArgs*sizeof(QualType), 8);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001274 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001275 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001276 Types.push_back(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001277 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001278 return QualType(FTP, 0);
1279}
1280
Douglas Gregor1d661552008-04-13 21:07:44 +00001281/// getTypeDeclType - Return the unique reference to the type for the
1282/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001283QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001284 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001285 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1286
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001287 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001288 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001289 else if (isa<TemplateTypeParmDecl>(Decl)) {
1290 assert(false && "Template type parameter types are always available.");
1291 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001292 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001293
Douglas Gregor2e047592009-02-28 01:32:25 +00001294 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001295 if (PrevDecl)
1296 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001297 else
1298 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001299 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001300 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1301 if (PrevDecl)
1302 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001303 else
1304 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001305 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001306 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001307 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001308
Ted Kremenek46a837c2008-09-05 17:16:31 +00001309 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001310 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001311}
1312
Chris Lattner4b009652007-07-25 00:24:17 +00001313/// getTypedefType - Return the unique reference to the type for the
1314/// specified typename decl.
1315QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1316 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1317
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001318 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001319 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001320 Types.push_back(Decl->TypeForDecl);
1321 return QualType(Decl->TypeForDecl, 0);
1322}
1323
Ted Kremenek42730c52008-01-07 19:49:32 +00001324/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001325/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001326QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001327 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1328
Steve Naroff93fd2112009-01-27 22:08:43 +00001329 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001330 Types.push_back(Decl->TypeForDecl);
1331 return QualType(Decl->TypeForDecl, 0);
1332}
1333
Fariborz Jahanian27ecc672009-02-14 20:13:28 +00001334/// buildObjCInterfaceType - Returns a new type for the interface
1335/// declaration, regardless. It also removes any previously built
1336/// record declaration so caller can rebuild it.
1337QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) {
1338 const RecordDecl *&RD = ASTRecordForInterface[Decl];
1339 if (RD)
1340 RD = 0;
1341 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
1342 Types.push_back(Decl->TypeForDecl);
1343 return QualType(Decl->TypeForDecl, 0);
1344}
1345
Douglas Gregora4918772009-02-05 23:33:38 +00001346/// \brief Retrieve the template type parameter type for a template
1347/// parameter with the given depth, index, and (optionally) name.
1348QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1349 IdentifierInfo *Name) {
1350 llvm::FoldingSetNodeID ID;
1351 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1352 void *InsertPos = 0;
1353 TemplateTypeParmType *TypeParm
1354 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1355
1356 if (TypeParm)
1357 return QualType(TypeParm, 0);
1358
1359 if (Name)
1360 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1361 getTemplateTypeParmType(Depth, Index));
1362 else
1363 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1364
1365 Types.push_back(TypeParm);
1366 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1367
1368 return QualType(TypeParm, 0);
1369}
1370
Douglas Gregor8e458f42009-02-09 18:46:07 +00001371QualType
Douglas Gregordd13e842009-03-30 22:58:21 +00001372ASTContext::getTemplateSpecializationType(TemplateName Template,
1373 const TemplateArgument *Args,
1374 unsigned NumArgs,
1375 QualType Canon) {
1376 // FIXME: If Template is dependent, canonicalize it!
1377
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001378 if (!Canon.isNull())
1379 Canon = getCanonicalType(Canon);
Douglas Gregor9c7825b2009-02-26 22:19:44 +00001380
Douglas Gregor8e458f42009-02-09 18:46:07 +00001381 llvm::FoldingSetNodeID ID;
Douglas Gregordd13e842009-03-30 22:58:21 +00001382 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001383
Douglas Gregor8e458f42009-02-09 18:46:07 +00001384 void *InsertPos = 0;
Douglas Gregordd13e842009-03-30 22:58:21 +00001385 TemplateSpecializationType *Spec
1386 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001387
1388 if (Spec)
1389 return QualType(Spec, 0);
1390
Douglas Gregordd13e842009-03-30 22:58:21 +00001391 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001392 sizeof(TemplateArgument) * NumArgs),
1393 8);
Douglas Gregordd13e842009-03-30 22:58:21 +00001394 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001395 Types.push_back(Spec);
Douglas Gregordd13e842009-03-30 22:58:21 +00001396 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001397
1398 return QualType(Spec, 0);
1399}
1400
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001401QualType
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001402ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001403 QualType NamedType) {
1404 llvm::FoldingSetNodeID ID;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001405 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001406
1407 void *InsertPos = 0;
1408 QualifiedNameType *T
1409 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1410 if (T)
1411 return QualType(T, 0);
1412
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001413 T = new (*this) QualifiedNameType(NNS, NamedType,
1414 getCanonicalType(NamedType));
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001415 Types.push_back(T);
1416 QualifiedNameTypes.InsertNode(T, InsertPos);
1417 return QualType(T, 0);
1418}
1419
Douglas Gregord3022602009-03-27 23:10:48 +00001420QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1421 const IdentifierInfo *Name,
1422 QualType Canon) {
1423 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1424
1425 if (Canon.isNull()) {
1426 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1427 if (CanonNNS != NNS)
1428 Canon = getTypenameType(CanonNNS, Name);
1429 }
1430
1431 llvm::FoldingSetNodeID ID;
1432 TypenameType::Profile(ID, NNS, Name);
1433
1434 void *InsertPos = 0;
1435 TypenameType *T
1436 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1437 if (T)
1438 return QualType(T, 0);
1439
1440 T = new (*this) TypenameType(NNS, Name, Canon);
1441 Types.push_back(T);
1442 TypenameTypes.InsertNode(T, InsertPos);
1443 return QualType(T, 0);
1444}
1445
Chris Lattnere1352302008-04-07 04:56:42 +00001446/// CmpProtocolNames - Comparison predicate for sorting protocols
1447/// alphabetically.
1448static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1449 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001450 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001451}
1452
1453static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1454 unsigned &NumProtocols) {
1455 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1456
1457 // Sort protocols, keyed by name.
1458 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1459
1460 // Remove duplicates.
1461 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1462 NumProtocols = ProtocolsEnd-Protocols;
1463}
1464
1465
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001466/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1467/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001468QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1469 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001470 // Sort the protocol list alphabetically to canonicalize it.
1471 SortAndUniqueProtocols(Protocols, NumProtocols);
1472
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001473 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001474 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001475
1476 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001477 if (ObjCQualifiedInterfaceType *QT =
1478 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001479 return QualType(QT, 0);
1480
1481 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001482 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001483 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001484
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001485 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001486 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001487 return QualType(QType, 0);
1488}
1489
Chris Lattnere1352302008-04-07 04:56:42 +00001490/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1491/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001492QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001493 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001494 // Sort the protocol list alphabetically to canonicalize it.
1495 SortAndUniqueProtocols(Protocols, NumProtocols);
1496
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001497 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001498 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001499
1500 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001501 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001502 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001503 return QualType(QT, 0);
1504
1505 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001506 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001507 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001508 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001509 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001510 return QualType(QType, 0);
1511}
1512
Douglas Gregor4fa58902009-02-26 23:50:07 +00001513/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1514/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff0604dd92007-08-01 18:02:17 +00001515/// multiple declarations that refer to "typeof(x)" all contain different
1516/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1517/// on canonical type's (which are always unique).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001518QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001519 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001520 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001521 Types.push_back(toe);
1522 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001523}
1524
Steve Naroff0604dd92007-08-01 18:02:17 +00001525/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1526/// TypeOfType AST's. The only motivation to unique these nodes would be
1527/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1528/// an issue. This doesn't effect the type checker, since it operates
1529/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001530QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001531 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001532 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001533 Types.push_back(tot);
1534 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001535}
1536
Chris Lattner4b009652007-07-25 00:24:17 +00001537/// getTagDeclType - Return the unique reference to the type for the
1538/// specified TagDecl (struct/union/class/enum) decl.
1539QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001540 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001541 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001542}
1543
1544/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1545/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1546/// needs to agree with the definition in <stddef.h>.
1547QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001548 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001549}
1550
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001551/// getSignedWCharType - Return the type of "signed wchar_t".
1552/// Used when in C++, as a GCC extension.
1553QualType ASTContext::getSignedWCharType() const {
1554 // FIXME: derive from "Target" ?
1555 return WCharTy;
1556}
1557
1558/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1559/// Used when in C++, as a GCC extension.
1560QualType ASTContext::getUnsignedWCharType() const {
1561 // FIXME: derive from "Target" ?
1562 return UnsignedIntTy;
1563}
1564
Chris Lattner4b009652007-07-25 00:24:17 +00001565/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1566/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1567QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001568 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001569}
1570
Chris Lattner19eb97e2008-04-02 05:18:44 +00001571//===----------------------------------------------------------------------===//
1572// Type Operators
1573//===----------------------------------------------------------------------===//
1574
Chris Lattner3dae6f42008-04-06 22:41:35 +00001575/// getCanonicalType - Return the canonical (structural) type corresponding to
1576/// the specified potentially non-canonical type. The non-canonical version
1577/// of a type may have many "decorated" versions of types. Decorators can
1578/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1579/// to be free of any of these, allowing two canonical types to be compared
1580/// for exact equality with a simple pointer comparison.
1581QualType ASTContext::getCanonicalType(QualType T) {
1582 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001583
1584 // If the result has type qualifiers, make sure to canonicalize them as well.
1585 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1586 if (TypeQuals == 0) return CanType;
1587
1588 // If the type qualifiers are on an array type, get the canonical type of the
1589 // array with the qualifiers applied to the element type.
1590 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1591 if (!AT)
1592 return CanType.getQualifiedType(TypeQuals);
1593
1594 // Get the canonical version of the element with the extra qualifiers on it.
1595 // This can recursively sink qualifiers through multiple levels of arrays.
1596 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1597 NewEltTy = getCanonicalType(NewEltTy);
1598
1599 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1600 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1601 CAT->getIndexTypeQualifier());
1602 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1603 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1604 IAT->getIndexTypeQualifier());
1605
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001606 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1607 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1608 DSAT->getSizeModifier(),
1609 DSAT->getIndexTypeQualifier());
1610
Chris Lattnera1923f62008-08-04 07:31:14 +00001611 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1612 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1613 VAT->getSizeModifier(),
1614 VAT->getIndexTypeQualifier());
1615}
1616
Douglas Gregord3022602009-03-27 23:10:48 +00001617NestedNameSpecifier *
1618ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1619 if (!NNS)
1620 return 0;
1621
1622 switch (NNS->getKind()) {
1623 case NestedNameSpecifier::Identifier:
1624 // Canonicalize the prefix but keep the identifier the same.
1625 return NestedNameSpecifier::Create(*this,
1626 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1627 NNS->getAsIdentifier());
1628
1629 case NestedNameSpecifier::Namespace:
1630 // A namespace is canonical; build a nested-name-specifier with
1631 // this namespace and no prefix.
1632 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1633
1634 case NestedNameSpecifier::TypeSpec:
1635 case NestedNameSpecifier::TypeSpecWithTemplate: {
1636 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1637 NestedNameSpecifier *Prefix = 0;
1638
1639 // FIXME: This isn't the right check!
1640 if (T->isDependentType())
1641 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1642
1643 return NestedNameSpecifier::Create(*this, Prefix,
1644 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1645 T.getTypePtr());
1646 }
1647
1648 case NestedNameSpecifier::Global:
1649 // The global specifier is canonical and unique.
1650 return NNS;
1651 }
1652
1653 // Required to silence a GCC warning
1654 return 0;
1655}
1656
Chris Lattnera1923f62008-08-04 07:31:14 +00001657
1658const ArrayType *ASTContext::getAsArrayType(QualType T) {
1659 // Handle the non-qualified case efficiently.
1660 if (T.getCVRQualifiers() == 0) {
1661 // Handle the common positive case fast.
1662 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1663 return AT;
1664 }
1665
1666 // Handle the common negative case fast, ignoring CVR qualifiers.
1667 QualType CType = T->getCanonicalTypeInternal();
1668
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001669 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001670 // test.
1671 if (!isa<ArrayType>(CType) &&
1672 !isa<ArrayType>(CType.getUnqualifiedType()))
1673 return 0;
1674
1675 // Apply any CVR qualifiers from the array type to the element type. This
1676 // implements C99 6.7.3p8: "If the specification of an array type includes
1677 // any type qualifiers, the element type is so qualified, not the array type."
1678
1679 // If we get here, we either have type qualifiers on the type, or we have
1680 // sugar such as a typedef in the way. If we have type qualifiers on the type
1681 // we must propagate them down into the elemeng type.
1682 unsigned CVRQuals = T.getCVRQualifiers();
1683 unsigned AddrSpace = 0;
1684 Type *Ty = T.getTypePtr();
1685
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001686 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001687 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001688 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1689 AddrSpace = EXTQT->getAddressSpace();
1690 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001691 } else {
1692 T = Ty->getDesugaredType();
1693 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1694 break;
1695 CVRQuals |= T.getCVRQualifiers();
1696 Ty = T.getTypePtr();
1697 }
1698 }
1699
1700 // If we have a simple case, just return now.
1701 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1702 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1703 return ATy;
1704
1705 // Otherwise, we have an array and we have qualifiers on it. Push the
1706 // qualifiers into the array element type and return a new array type.
1707 // Get the canonical version of the element with the extra qualifiers on it.
1708 // This can recursively sink qualifiers through multiple levels of arrays.
1709 QualType NewEltTy = ATy->getElementType();
1710 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001711 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001712 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1713
1714 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1715 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1716 CAT->getSizeModifier(),
1717 CAT->getIndexTypeQualifier()));
1718 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1719 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1720 IAT->getSizeModifier(),
1721 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001722
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001723 if (const DependentSizedArrayType *DSAT
1724 = dyn_cast<DependentSizedArrayType>(ATy))
1725 return cast<ArrayType>(
1726 getDependentSizedArrayType(NewEltTy,
1727 DSAT->getSizeExpr(),
1728 DSAT->getSizeModifier(),
1729 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001730
Chris Lattnera1923f62008-08-04 07:31:14 +00001731 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1732 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1733 VAT->getSizeModifier(),
1734 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001735}
1736
1737
Chris Lattner19eb97e2008-04-02 05:18:44 +00001738/// getArrayDecayedType - Return the properly qualified result of decaying the
1739/// specified array type to a pointer. This operation is non-trivial when
1740/// handling typedefs etc. The canonical type of "T" must be an array type,
1741/// this returns a pointer to a properly qualified element of the array.
1742///
1743/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1744QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001745 // Get the element type with 'getAsArrayType' so that we don't lose any
1746 // typedefs in the element type of the array. This also handles propagation
1747 // of type qualifiers from the array type into the element type if present
1748 // (C99 6.7.3p8).
1749 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1750 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001751
Chris Lattnera1923f62008-08-04 07:31:14 +00001752 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001753
1754 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001755 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001756}
1757
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001758QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001759 QualType ElemTy = VAT->getElementType();
1760
1761 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1762 return getBaseElementType(VAT);
1763
1764 return ElemTy;
1765}
1766
Chris Lattner4b009652007-07-25 00:24:17 +00001767/// getFloatingRank - Return a relative rank for floating point types.
1768/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001769static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001770 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001771 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001772
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001773 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001774 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001775 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001776 case BuiltinType::Float: return FloatRank;
1777 case BuiltinType::Double: return DoubleRank;
1778 case BuiltinType::LongDouble: return LongDoubleRank;
1779 }
1780}
1781
Steve Narofffa0c4532007-08-27 01:41:48 +00001782/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1783/// point or a complex type (based on typeDomain/typeSize).
1784/// 'typeDomain' is a real floating point or complex type.
1785/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001786QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1787 QualType Domain) const {
1788 FloatingRank EltRank = getFloatingRank(Size);
1789 if (Domain->isComplexType()) {
1790 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001791 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001792 case FloatRank: return FloatComplexTy;
1793 case DoubleRank: return DoubleComplexTy;
1794 case LongDoubleRank: return LongDoubleComplexTy;
1795 }
Chris Lattner4b009652007-07-25 00:24:17 +00001796 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001797
1798 assert(Domain->isRealFloatingType() && "Unknown domain!");
1799 switch (EltRank) {
1800 default: assert(0 && "getFloatingRank(): illegal value for rank");
1801 case FloatRank: return FloatTy;
1802 case DoubleRank: return DoubleTy;
1803 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001804 }
Chris Lattner4b009652007-07-25 00:24:17 +00001805}
1806
Chris Lattner51285d82008-04-06 23:55:33 +00001807/// getFloatingTypeOrder - Compare the rank of the two specified floating
1808/// point types, ignoring the domain of the type (i.e. 'double' ==
1809/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1810/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001811int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1812 FloatingRank LHSR = getFloatingRank(LHS);
1813 FloatingRank RHSR = getFloatingRank(RHS);
1814
1815 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001816 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001817 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001818 return 1;
1819 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001820}
1821
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001822/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1823/// routine will assert if passed a built-in type that isn't an integer or enum,
1824/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001825unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001826 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001827 if (EnumType* ET = dyn_cast<EnumType>(T))
1828 T = ET->getDecl()->getIntegerType().getTypePtr();
1829
1830 // There are two things which impact the integer rank: the width, and
1831 // the ordering of builtins. The builtin ordering is encoded in the
1832 // bottom three bits; the width is encoded in the bits above that.
1833 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1834 return FWIT->getWidth() << 3;
1835 }
1836
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001837 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001838 default: assert(0 && "getIntegerRank(): not a built-in integer");
1839 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001840 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001841 case BuiltinType::Char_S:
1842 case BuiltinType::Char_U:
1843 case BuiltinType::SChar:
1844 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001845 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001846 case BuiltinType::Short:
1847 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001848 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001849 case BuiltinType::Int:
1850 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001851 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001852 case BuiltinType::Long:
1853 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001854 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001855 case BuiltinType::LongLong:
1856 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001857 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001858 }
1859}
1860
Chris Lattner51285d82008-04-06 23:55:33 +00001861/// getIntegerTypeOrder - Returns the highest ranked integer type:
1862/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1863/// LHS < RHS, return -1.
1864int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001865 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1866 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001867 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001868
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001869 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1870 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001871
Chris Lattner51285d82008-04-06 23:55:33 +00001872 unsigned LHSRank = getIntegerRank(LHSC);
1873 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001874
Chris Lattner51285d82008-04-06 23:55:33 +00001875 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1876 if (LHSRank == RHSRank) return 0;
1877 return LHSRank > RHSRank ? 1 : -1;
1878 }
Chris Lattner4b009652007-07-25 00:24:17 +00001879
Chris Lattner51285d82008-04-06 23:55:33 +00001880 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1881 if (LHSUnsigned) {
1882 // If the unsigned [LHS] type is larger, return it.
1883 if (LHSRank >= RHSRank)
1884 return 1;
1885
1886 // If the signed type can represent all values of the unsigned type, it
1887 // wins. Because we are dealing with 2's complement and types that are
1888 // powers of two larger than each other, this is always safe.
1889 return -1;
1890 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001891
Chris Lattner51285d82008-04-06 23:55:33 +00001892 // If the unsigned [RHS] type is larger, return it.
1893 if (RHSRank >= LHSRank)
1894 return -1;
1895
1896 // If the signed type can represent all values of the unsigned type, it
1897 // wins. Because we are dealing with 2's complement and types that are
1898 // powers of two larger than each other, this is always safe.
1899 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001900}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001901
1902// getCFConstantStringType - Return the type used for constant CFStrings.
1903QualType ASTContext::getCFConstantStringType() {
1904 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001905 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001906 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001907 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001908 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001909
1910 // const int *isa;
1911 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001912 // int flags;
1913 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001914 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001915 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001916 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001917 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001918
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001919 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001920 for (unsigned i = 0; i < 4; ++i) {
1921 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1922 SourceLocation(), 0,
1923 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001924 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001925 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001926 }
1927
1928 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001929 }
1930
1931 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001932}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001933
Anders Carlssonf58cac72008-08-30 19:34:46 +00001934QualType ASTContext::getObjCFastEnumerationStateType()
1935{
1936 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001937 ObjCFastEnumerationStateTypeDecl =
1938 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1939 &Idents.get("__objcFastEnumerationState"));
1940
Anders Carlssonf58cac72008-08-30 19:34:46 +00001941 QualType FieldTypes[] = {
1942 UnsignedLongTy,
1943 getPointerType(ObjCIdType),
1944 getPointerType(UnsignedLongTy),
1945 getConstantArrayType(UnsignedLongTy,
1946 llvm::APInt(32, 5), ArrayType::Normal, 0)
1947 };
1948
Douglas Gregor8acb7272008-12-11 16:49:14 +00001949 for (size_t i = 0; i < 4; ++i) {
1950 FieldDecl *Field = FieldDecl::Create(*this,
1951 ObjCFastEnumerationStateTypeDecl,
1952 SourceLocation(), 0,
1953 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001954 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001955 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001956 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00001957
Douglas Gregor8acb7272008-12-11 16:49:14 +00001958 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001959 }
1960
1961 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1962}
1963
Anders Carlssone3f02572007-10-29 06:33:42 +00001964// This returns true if a type has been typedefed to BOOL:
1965// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001966static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001967 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00001968 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1969 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001970
1971 return false;
1972}
1973
Ted Kremenek42730c52008-01-07 19:49:32 +00001974/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001975/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001976int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001977 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001978
1979 // Make all integer and enum types at least as large as an int
1980 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001981 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001982 // Treat arrays as pointers, since that's how they're passed in.
1983 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001984 sz = getTypeSize(VoidPtrTy);
1985 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001986}
1987
Ted Kremenek42730c52008-01-07 19:49:32 +00001988/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001989/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001990void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00001991 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001992 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001993 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001994 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001995 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001996 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001997 // Compute size of all parameters.
1998 // Start with computing size of a pointer in number of bytes.
1999 // FIXME: There might(should) be a better way of doing this computation!
2000 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00002001 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002002 // The first two arguments (self and _cmd) are pointers; account for
2003 // their size.
2004 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002005 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2006 E = Decl->param_end(); PI != E; ++PI) {
2007 QualType PType = (*PI)->getType();
2008 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00002009 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002010 ParmOffset += sz;
2011 }
2012 S += llvm::utostr(ParmOffset);
2013 S += "@0:";
2014 S += llvm::utostr(PtrSize);
2015
2016 // Argument types.
2017 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002018 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2019 E = Decl->param_end(); PI != E; ++PI) {
2020 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002021 QualType PType = PVDecl->getOriginalType();
2022 if (const ArrayType *AT =
2023 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
2024 // Use array's original type only if it has known number of
2025 // elements.
2026 if (!dyn_cast<ConstantArrayType>(AT))
2027 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002028 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002029 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002030 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002031 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002032 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00002033 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002034 }
2035}
2036
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002037/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002038/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002039/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2040/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002041/// Property attributes are stored as a comma-delimited C string. The simple
2042/// attributes readonly and bycopy are encoded as single characters. The
2043/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2044/// encoded as single characters, followed by an identifier. Property types
2045/// are also encoded as a parametrized attribute. The characters used to encode
2046/// these attributes are defined by the following enumeration:
2047/// @code
2048/// enum PropertyAttributes {
2049/// kPropertyReadOnly = 'R', // property is read-only.
2050/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2051/// kPropertyByref = '&', // property is a reference to the value last assigned
2052/// kPropertyDynamic = 'D', // property is dynamic
2053/// kPropertyGetter = 'G', // followed by getter selector name
2054/// kPropertySetter = 'S', // followed by setter selector name
2055/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2056/// kPropertyType = 't' // followed by old-style type encoding.
2057/// kPropertyWeak = 'W' // 'weak' property
2058/// kPropertyStrong = 'P' // property GC'able
2059/// kPropertyNonAtomic = 'N' // property non-atomic
2060/// };
2061/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002062void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2063 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00002064 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002065 // Collect information from the property implementation decl(s).
2066 bool Dynamic = false;
2067 ObjCPropertyImplDecl *SynthesizePID = 0;
2068
2069 // FIXME: Duplicated code due to poor abstraction.
2070 if (Container) {
2071 if (const ObjCCategoryImplDecl *CID =
2072 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2073 for (ObjCCategoryImplDecl::propimpl_iterator
2074 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2075 ObjCPropertyImplDecl *PID = *i;
2076 if (PID->getPropertyDecl() == PD) {
2077 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2078 Dynamic = true;
2079 } else {
2080 SynthesizePID = PID;
2081 }
2082 }
2083 }
2084 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002085 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002086 for (ObjCCategoryImplDecl::propimpl_iterator
2087 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2088 ObjCPropertyImplDecl *PID = *i;
2089 if (PID->getPropertyDecl() == PD) {
2090 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2091 Dynamic = true;
2092 } else {
2093 SynthesizePID = PID;
2094 }
2095 }
2096 }
2097 }
2098 }
2099
2100 // FIXME: This is not very efficient.
2101 S = "T";
2102
2103 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002104 // GCC has some special rules regarding encoding of properties which
2105 // closely resembles encoding of ivars.
2106 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
2107 true /* outermost type */,
2108 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002109
2110 if (PD->isReadOnly()) {
2111 S += ",R";
2112 } else {
2113 switch (PD->getSetterKind()) {
2114 case ObjCPropertyDecl::Assign: break;
2115 case ObjCPropertyDecl::Copy: S += ",C"; break;
2116 case ObjCPropertyDecl::Retain: S += ",&"; break;
2117 }
2118 }
2119
2120 // It really isn't clear at all what this means, since properties
2121 // are "dynamic by default".
2122 if (Dynamic)
2123 S += ",D";
2124
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002125 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2126 S += ",N";
2127
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002128 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2129 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002130 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002131 }
2132
2133 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2134 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002135 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002136 }
2137
2138 if (SynthesizePID) {
2139 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2140 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002141 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002142 }
2143
2144 // FIXME: OBJCGC: weak & strong
2145}
2146
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002147/// getLegacyIntegralTypeEncoding -
2148/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002149/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002150/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2151///
2152void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2153 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2154 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002155 if (BT->getKind() == BuiltinType::ULong &&
2156 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002157 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002158 else
2159 if (BT->getKind() == BuiltinType::Long &&
2160 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002161 PointeeTy = IntTy;
2162 }
2163 }
2164}
2165
Fariborz Jahanian248db262008-01-22 22:44:46 +00002166void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002167 FieldDecl *Field) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002168 // We follow the behavior of gcc, expanding structures which are
2169 // directly pointed to, and expanding embedded structures. Note that
2170 // these rules are sufficient to prevent recursive encoding of the
2171 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002172 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2173 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002174}
2175
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002176static void EncodeBitField(const ASTContext *Context, std::string& S,
2177 FieldDecl *FD) {
2178 const Expr *E = FD->getBitWidth();
2179 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2180 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2181 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2182 S += 'b';
2183 S += llvm::utostr(N);
2184}
2185
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002186void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2187 bool ExpandPointedToStructures,
2188 bool ExpandStructures,
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002189 FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002190 bool OutermostType,
2191 bool EncodingProperty) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00002192 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002193 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002194 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002195 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002196 else {
2197 char encoding;
2198 switch (BT->getKind()) {
2199 default: assert(0 && "Unhandled builtin type kind");
2200 case BuiltinType::Void: encoding = 'v'; break;
2201 case BuiltinType::Bool: encoding = 'B'; break;
2202 case BuiltinType::Char_U:
2203 case BuiltinType::UChar: encoding = 'C'; break;
2204 case BuiltinType::UShort: encoding = 'S'; break;
2205 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002206 case BuiltinType::ULong:
2207 encoding =
2208 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2209 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002210 case BuiltinType::ULongLong: encoding = 'Q'; break;
2211 case BuiltinType::Char_S:
2212 case BuiltinType::SChar: encoding = 'c'; break;
2213 case BuiltinType::Short: encoding = 's'; break;
2214 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002215 case BuiltinType::Long:
2216 encoding =
2217 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2218 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002219 case BuiltinType::LongLong: encoding = 'q'; break;
2220 case BuiltinType::Float: encoding = 'f'; break;
2221 case BuiltinType::Double: encoding = 'd'; break;
2222 case BuiltinType::LongDouble: encoding = 'd'; break;
2223 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002224
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002225 S += encoding;
2226 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002227 }
Ted Kremenek42730c52008-01-07 19:49:32 +00002228 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002229 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2230 ExpandPointedToStructures,
2231 ExpandStructures, FD);
2232 if (FD || EncodingProperty) {
2233 // Note that we do extended encoding of protocol qualifer list
2234 // Only when doing ivar or property encoding.
2235 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2236 S += '"';
2237 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2238 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2239 S += '<';
2240 S += Proto->getNameAsString();
2241 S += '>';
2242 }
2243 S += '"';
2244 }
2245 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002246 }
2247 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002248 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002249 bool isReadOnly = false;
2250 // For historical/compatibility reasons, the read-only qualifier of the
2251 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2252 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2253 // Also, do not emit the 'r' for anything but the outermost type!
2254 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2255 if (OutermostType && T.isConstQualified()) {
2256 isReadOnly = true;
2257 S += 'r';
2258 }
2259 }
2260 else if (OutermostType) {
2261 QualType P = PointeeTy;
2262 while (P->getAsPointerType())
2263 P = P->getAsPointerType()->getPointeeType();
2264 if (P.isConstQualified()) {
2265 isReadOnly = true;
2266 S += 'r';
2267 }
2268 }
2269 if (isReadOnly) {
2270 // Another legacy compatibility encoding. Some ObjC qualifier and type
2271 // combinations need to be rearranged.
2272 // Rewrite "in const" from "nr" to "rn"
2273 const char * s = S.c_str();
2274 int len = S.length();
2275 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2276 std::string replace = "rn";
2277 S.replace(S.end()-2, S.end(), replace);
2278 }
2279 }
Steve Naroff17c03822009-02-12 17:52:19 +00002280 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002281 S += '@';
2282 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002283 }
2284 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002285 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002286 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002287 // Another historical/compatibility reason.
2288 // We encode the underlying type which comes out as
2289 // {...};
2290 S += '^';
2291 getObjCEncodingForTypeImpl(PointeeTy, S,
2292 false, ExpandPointedToStructures,
2293 NULL);
2294 return;
2295 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002296 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002297 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002298 const ObjCInterfaceType *OIT =
2299 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002300 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002301 S += '"';
2302 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002303 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2304 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2305 S += '<';
2306 S += Proto->getNameAsString();
2307 S += '>';
2308 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002309 S += '"';
2310 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002311 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002312 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002313 S += '#';
2314 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002315 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002316 S += ':';
2317 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002318 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002319
2320 if (PointeeTy->isCharType()) {
2321 // char pointer types should be encoded as '*' unless it is a
2322 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002323 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002324 S += '*';
2325 return;
2326 }
2327 }
2328
2329 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002330 getLegacyIntegralTypeEncoding(PointeeTy);
2331
2332 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002333 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002334 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002335 } else if (const ArrayType *AT =
2336 // Ignore type qualifiers etc.
2337 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002338 if (isa<IncompleteArrayType>(AT)) {
2339 // Incomplete arrays are encoded as a pointer to the array element.
2340 S += '^';
2341
2342 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2343 false, ExpandStructures, FD);
2344 } else {
2345 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002346
Anders Carlsson858c64d2009-02-22 01:38:57 +00002347 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2348 S += llvm::utostr(CAT->getSize().getZExtValue());
2349 else {
2350 //Variable length arrays are encoded as a regular array with 0 elements.
2351 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2352 S += '0';
2353 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002354
Anders Carlsson858c64d2009-02-22 01:38:57 +00002355 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2356 false, ExpandStructures, FD);
2357 S += ']';
2358 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002359 } else if (T->getAsFunctionType()) {
2360 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002361 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002362 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002363 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002364 // Anonymous structures print as '?'
2365 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2366 S += II->getName();
2367 } else {
2368 S += '?';
2369 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002370 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002371 S += '=';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002372 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2373 FieldEnd = RDecl->field_end();
2374 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002375 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002376 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002377 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002378 S += '"';
2379 }
2380
2381 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002382 if (Field->isBitField()) {
2383 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2384 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002385 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002386 QualType qt = Field->getType();
2387 getLegacyIntegralTypeEncoding(qt);
2388 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002389 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002390 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002391 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002392 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002393 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002394 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002395 if (FD && FD->isBitField())
2396 EncodeBitField(this, S, FD);
2397 else
2398 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002399 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002400 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002401 } else if (T->isObjCInterfaceType()) {
2402 // @encode(class_name)
2403 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2404 S += '{';
2405 const IdentifierInfo *II = OI->getIdentifier();
2406 S += II->getName();
2407 S += '=';
2408 std::vector<FieldDecl*> RecFields;
2409 CollectObjCIvars(OI, RecFields);
2410 for (unsigned int i = 0; i != RecFields.size(); i++) {
2411 if (RecFields[i]->isBitField())
2412 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2413 RecFields[i]);
2414 else
2415 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2416 FD);
2417 }
2418 S += '}';
2419 }
2420 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002421 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002422}
2423
Ted Kremenek42730c52008-01-07 19:49:32 +00002424void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002425 std::string& S) const {
2426 if (QT & Decl::OBJC_TQ_In)
2427 S += 'n';
2428 if (QT & Decl::OBJC_TQ_Inout)
2429 S += 'N';
2430 if (QT & Decl::OBJC_TQ_Out)
2431 S += 'o';
2432 if (QT & Decl::OBJC_TQ_Bycopy)
2433 S += 'O';
2434 if (QT & Decl::OBJC_TQ_Byref)
2435 S += 'R';
2436 if (QT & Decl::OBJC_TQ_Oneway)
2437 S += 'V';
2438}
2439
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002440void ASTContext::setBuiltinVaListType(QualType T)
2441{
2442 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2443
2444 BuiltinVaListType = T;
2445}
2446
Ted Kremenek42730c52008-01-07 19:49:32 +00002447void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002448{
Ted Kremenek42730c52008-01-07 19:49:32 +00002449 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002450
2451 // typedef struct objc_object *id;
2452 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002453 // User error - caller will issue diagnostics.
2454 if (!ptr)
2455 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002456 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002457 // User error - caller will issue diagnostics.
2458 if (!rec)
2459 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002460 IdStructType = rec;
2461}
2462
Ted Kremenek42730c52008-01-07 19:49:32 +00002463void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002464{
Ted Kremenek42730c52008-01-07 19:49:32 +00002465 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002466
2467 // typedef struct objc_selector *SEL;
2468 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002469 if (!ptr)
2470 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002471 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002472 if (!rec)
2473 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002474 SelStructType = rec;
2475}
2476
Ted Kremenek42730c52008-01-07 19:49:32 +00002477void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002478{
Ted Kremenek42730c52008-01-07 19:49:32 +00002479 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002480}
2481
Ted Kremenek42730c52008-01-07 19:49:32 +00002482void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002483{
Ted Kremenek42730c52008-01-07 19:49:32 +00002484 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002485
2486 // typedef struct objc_class *Class;
2487 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2488 assert(ptr && "'Class' incorrectly typed");
2489 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2490 assert(rec && "'Class' incorrectly typed");
2491 ClassStructType = rec;
2492}
2493
Ted Kremenek42730c52008-01-07 19:49:32 +00002494void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2495 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002496 "'NSConstantString' type already set!");
2497
Ted Kremenek42730c52008-01-07 19:49:32 +00002498 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002499}
2500
Douglas Gregordd13e842009-03-30 22:58:21 +00002501/// \brief Retrieve the template name that represents a qualified
2502/// template name such as \c std::vector.
2503TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2504 bool TemplateKeyword,
2505 TemplateDecl *Template) {
2506 llvm::FoldingSetNodeID ID;
2507 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2508
2509 void *InsertPos = 0;
2510 QualifiedTemplateName *QTN =
2511 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2512 if (!QTN) {
2513 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2514 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2515 }
2516
2517 return TemplateName(QTN);
2518}
2519
2520/// \brief Retrieve the template name that represents a dependent
2521/// template name such as \c MetaFun::template apply.
2522TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2523 const IdentifierInfo *Name) {
2524 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2525
2526 llvm::FoldingSetNodeID ID;
2527 DependentTemplateName::Profile(ID, NNS, Name);
2528
2529 void *InsertPos = 0;
2530 DependentTemplateName *QTN =
2531 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2532
2533 if (QTN)
2534 return TemplateName(QTN);
2535
2536 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2537 if (CanonNNS == NNS) {
2538 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2539 } else {
2540 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2541 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2542 }
2543
2544 DependentTemplateNames.InsertNode(QTN, InsertPos);
2545 return TemplateName(QTN);
2546}
2547
Douglas Gregorc6507e42008-11-03 14:12:49 +00002548/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002549/// TargetInfo, produce the corresponding type. The unsigned @p Type
2550/// is actually a value of type @c TargetInfo::IntType.
2551QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002552 switch (Type) {
2553 case TargetInfo::NoInt: return QualType();
2554 case TargetInfo::SignedShort: return ShortTy;
2555 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2556 case TargetInfo::SignedInt: return IntTy;
2557 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2558 case TargetInfo::SignedLong: return LongTy;
2559 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2560 case TargetInfo::SignedLongLong: return LongLongTy;
2561 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2562 }
2563
2564 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002565 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002566}
Ted Kremenek118930e2008-07-24 23:58:27 +00002567
2568//===----------------------------------------------------------------------===//
2569// Type Predicates.
2570//===----------------------------------------------------------------------===//
2571
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002572/// isObjCNSObjectType - Return true if this is an NSObject object using
2573/// NSObject attribute on a c-style pointer type.
2574/// FIXME - Make it work directly on types.
2575///
2576bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2577 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2578 if (TypedefDecl *TD = TDT->getDecl())
2579 if (TD->getAttr<ObjCNSObjectAttr>())
2580 return true;
2581 }
2582 return false;
2583}
2584
Ted Kremenek118930e2008-07-24 23:58:27 +00002585/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2586/// to an object type. This includes "id" and "Class" (two 'special' pointers
2587/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2588/// ID type).
2589bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroff6805fc42009-02-23 18:36:16 +00002590 if (Ty->isObjCQualifiedIdType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002591 return true;
2592
Steve Naroffd9e00802008-10-21 18:24:04 +00002593 // Blocks are objects.
2594 if (Ty->isBlockPointerType())
2595 return true;
2596
2597 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00002598 if (!Ty->isPointerType())
2599 return false;
2600
2601 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2602 // pointer types. This looks for the typedef specifically, not for the
2603 // underlying type.
Eli Friedman9c2b33f2009-03-22 23:00:19 +00002604 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2605 Ty.getUnqualifiedType() == getObjCClassType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002606 return true;
2607
2608 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002609 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2610 return true;
2611
2612 // If is has NSObject attribute, OK as well.
2613 return isObjCNSObjectType(Ty);
Ted Kremenek118930e2008-07-24 23:58:27 +00002614}
2615
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002616/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2617/// garbage collection attribute.
2618///
2619QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002620 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002621 if (getLangOptions().ObjC1 &&
2622 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002623 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002624 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002625 // (or pointers to them) be treated as though they were declared
2626 // as __strong.
2627 if (GCAttrs == QualType::GCNone) {
2628 if (isObjCObjectPointerType(Ty))
2629 GCAttrs = QualType::Strong;
2630 else if (Ty->isPointerType())
2631 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2632 }
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002633 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002634 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002635}
2636
Chris Lattner6ff358b2008-04-07 06:51:04 +00002637//===----------------------------------------------------------------------===//
2638// Type Compatibility Testing
2639//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002640
Steve Naroff3454b6c2008-09-04 15:10:53 +00002641/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002642/// block types. Types must be strictly compatible here. For example,
2643/// C unfortunately doesn't produce an error for the following:
2644///
2645/// int (*emptyArgFunc)();
2646/// int (*intArgList)(int) = emptyArgFunc;
2647///
2648/// For blocks, we will produce an error for the following (similar to C++):
2649///
2650/// int (^emptyArgBlock)();
2651/// int (^intArgBlock)(int) = emptyArgBlock;
2652///
2653/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2654///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002655bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002656 const FunctionType *lbase = lhs->getAsFunctionType();
2657 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002658 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2659 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002660 if (lproto && rproto)
2661 return !mergeTypes(lhs, rhs).isNull();
2662 return false;
Steve Naroff3454b6c2008-09-04 15:10:53 +00002663}
2664
Chris Lattner6ff358b2008-04-07 06:51:04 +00002665/// areCompatVectorTypes - Return true if the two specified vector types are
2666/// compatible.
2667static bool areCompatVectorTypes(const VectorType *LHS,
2668 const VectorType *RHS) {
2669 assert(LHS->isCanonical() && RHS->isCanonical());
2670 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002671 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002672}
2673
Eli Friedman0d9549b2008-08-22 00:56:42 +00002674/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002675/// compatible for assignment from RHS to LHS. This handles validation of any
2676/// protocol qualifiers on the LHS or RHS.
2677///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002678bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2679 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002680 // Verify that the base decls are compatible: the RHS must be a subclass of
2681 // the LHS.
2682 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2683 return false;
2684
2685 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2686 // protocol qualified at all, then we are good.
2687 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2688 return true;
2689
2690 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2691 // isn't a superset.
2692 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2693 return true; // FIXME: should return false!
2694
2695 // Finally, we must have two protocol-qualified interfaces.
2696 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2697 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ff358b2008-04-07 06:51:04 +00002698
Steve Naroff98e71b82009-03-01 16:12:44 +00002699 // All LHS protocols must have a presence on the RHS.
2700 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ff358b2008-04-07 06:51:04 +00002701
Steve Naroff98e71b82009-03-01 16:12:44 +00002702 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2703 LHSPE = LHSP->qual_end();
2704 LHSPI != LHSPE; LHSPI++) {
2705 bool RHSImplementsProtocol = false;
2706
2707 // If the RHS doesn't implement the protocol on the left, the types
2708 // are incompatible.
2709 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2710 RHSPE = RHSP->qual_end();
2711 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2712 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2713 RHSImplementsProtocol = true;
2714 }
2715 // FIXME: For better diagnostics, consider passing back the protocol name.
2716 if (!RHSImplementsProtocol)
2717 return false;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002718 }
Steve Naroff98e71b82009-03-01 16:12:44 +00002719 // The RHS implements all protocols listed on the LHS.
2720 return true;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002721}
2722
Steve Naroff17c03822009-02-12 17:52:19 +00002723bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2724 // get the "pointed to" types
2725 const PointerType *LHSPT = LHS->getAsPointerType();
2726 const PointerType *RHSPT = RHS->getAsPointerType();
2727
2728 if (!LHSPT || !RHSPT)
2729 return false;
2730
2731 QualType lhptee = LHSPT->getPointeeType();
2732 QualType rhptee = RHSPT->getPointeeType();
2733 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2734 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2735 // ID acts sort of like void* for ObjC interfaces
2736 if (LHSIface && isObjCIdStructType(rhptee))
2737 return true;
2738 if (RHSIface && isObjCIdStructType(lhptee))
2739 return true;
2740 if (!LHSIface || !RHSIface)
2741 return false;
2742 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2743 canAssignObjCInterfaces(RHSIface, LHSIface);
2744}
2745
Steve Naroff85f0dc52007-10-15 20:41:53 +00002746/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2747/// both shall have the identically qualified version of a compatible type.
2748/// C99 6.2.7p1: Two types have compatible types if their types are the
2749/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002750bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2751 return !mergeTypes(LHS, RHS).isNull();
2752}
2753
2754QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2755 const FunctionType *lbase = lhs->getAsFunctionType();
2756 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002757 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2758 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002759 bool allLTypes = true;
2760 bool allRTypes = true;
2761
2762 // Check return type
2763 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2764 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002765 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2766 allLTypes = false;
2767 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2768 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002769
2770 if (lproto && rproto) { // two C99 style function prototypes
2771 unsigned lproto_nargs = lproto->getNumArgs();
2772 unsigned rproto_nargs = rproto->getNumArgs();
2773
2774 // Compatible functions must have the same number of arguments
2775 if (lproto_nargs != rproto_nargs)
2776 return QualType();
2777
2778 // Variadic and non-variadic functions aren't compatible
2779 if (lproto->isVariadic() != rproto->isVariadic())
2780 return QualType();
2781
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002782 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2783 return QualType();
2784
Eli Friedman0d9549b2008-08-22 00:56:42 +00002785 // Check argument compatibility
2786 llvm::SmallVector<QualType, 10> types;
2787 for (unsigned i = 0; i < lproto_nargs; i++) {
2788 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2789 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2790 QualType argtype = mergeTypes(largtype, rargtype);
2791 if (argtype.isNull()) return QualType();
2792 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002793 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2794 allLTypes = false;
2795 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2796 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002797 }
2798 if (allLTypes) return lhs;
2799 if (allRTypes) return rhs;
2800 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002801 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002802 }
2803
2804 if (lproto) allRTypes = false;
2805 if (rproto) allLTypes = false;
2806
Douglas Gregor4fa58902009-02-26 23:50:07 +00002807 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002808 if (proto) {
2809 if (proto->isVariadic()) return QualType();
2810 // Check that the types are compatible with the types that
2811 // would result from default argument promotions (C99 6.7.5.3p15).
2812 // The only types actually affected are promotable integer
2813 // types and floats, which would be passed as a different
2814 // type depending on whether the prototype is visible.
2815 unsigned proto_nargs = proto->getNumArgs();
2816 for (unsigned i = 0; i < proto_nargs; ++i) {
2817 QualType argTy = proto->getArgType(i);
2818 if (argTy->isPromotableIntegerType() ||
2819 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2820 return QualType();
2821 }
2822
2823 if (allLTypes) return lhs;
2824 if (allRTypes) return rhs;
2825 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002826 proto->getNumArgs(), lproto->isVariadic(),
2827 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002828 }
2829
2830 if (allLTypes) return lhs;
2831 if (allRTypes) return rhs;
Douglas Gregor4fa58902009-02-26 23:50:07 +00002832 return getFunctionNoProtoType(retType);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002833}
2834
2835QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002836 // C++ [expr]: If an expression initially has the type "reference to T", the
2837 // type is adjusted to "T" prior to any further analysis, the expression
2838 // designates the object or function denoted by the reference, and the
Sebastian Redlce6fff02009-03-16 23:22:08 +00002839 // expression is an lvalue unless the reference is an rvalue reference and
2840 // the expression is a function call (possibly inside parentheses).
Eli Friedman0d9549b2008-08-22 00:56:42 +00002841 // FIXME: C++ shouldn't be going through here! The rules are different
2842 // enough that they should be handled separately.
Sebastian Redlce6fff02009-03-16 23:22:08 +00002843 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2844 // shouldn't be going through here!
Eli Friedman0d9549b2008-08-22 00:56:42 +00002845 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002846 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002847 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002848 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002849
Eli Friedman0d9549b2008-08-22 00:56:42 +00002850 QualType LHSCan = getCanonicalType(LHS),
2851 RHSCan = getCanonicalType(RHS);
2852
2853 // If two types are identical, they are compatible.
2854 if (LHSCan == RHSCan)
2855 return LHS;
2856
2857 // If the qualifiers are different, the types aren't compatible
Eli Friedman94fcc9a2009-02-27 23:04:43 +00002858 // Note that we handle extended qualifiers later, in the
2859 // case for ExtQualType.
2860 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman0d9549b2008-08-22 00:56:42 +00002861 return QualType();
2862
2863 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2864 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2865
Chris Lattnerc38d4522008-01-14 05:45:46 +00002866 // We want to consider the two function types to be the same for these
2867 // comparisons, just force one to the other.
2868 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2869 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002870
2871 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002872 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2873 LHSClass = Type::ConstantArray;
2874 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2875 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002876
Nate Begemanaf6ed502008-04-18 23:10:10 +00002877 // Canonicalize ExtVector -> Vector.
2878 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2879 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002880
Chris Lattner7cdcb252008-04-07 06:38:24 +00002881 // Consider qualified interfaces and interfaces the same.
2882 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2883 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002884
Chris Lattnerb5709e22008-04-07 05:43:21 +00002885 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002886 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00002887 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2888 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2889
2890 // ID acts sort of like void* for ObjC interfaces
2891 if (LHSIface && isObjCIdStructType(RHS))
2892 return LHS;
2893 if (RHSIface && isObjCIdStructType(LHS))
2894 return RHS;
2895
Steve Naroff28ceff72008-12-10 22:14:21 +00002896 // ID is compatible with all qualified id types.
2897 if (LHS->isObjCQualifiedIdType()) {
2898 if (const PointerType *PT = RHS->getAsPointerType()) {
2899 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002900 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002901 return LHS;
2902 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2903 // Unfortunately, this API is part of Sema (which we don't have access
2904 // to. Need to refactor. The following check is insufficient, since we
2905 // need to make sure the class implements the protocol.
2906 if (pType->isObjCInterfaceType())
2907 return LHS;
2908 }
2909 }
2910 if (RHS->isObjCQualifiedIdType()) {
2911 if (const PointerType *PT = LHS->getAsPointerType()) {
2912 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002913 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002914 return RHS;
2915 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2916 // Unfortunately, this API is part of Sema (which we don't have access
2917 // to. Need to refactor. The following check is insufficient, since we
2918 // need to make sure the class implements the protocol.
2919 if (pType->isObjCInterfaceType())
2920 return RHS;
2921 }
2922 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002923 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2924 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002925 if (const EnumType* ETy = LHS->getAsEnumType()) {
2926 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2927 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002928 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002929 if (const EnumType* ETy = RHS->getAsEnumType()) {
2930 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2931 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002932 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002933
Eli Friedman0d9549b2008-08-22 00:56:42 +00002934 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002935 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002936
Steve Naroffc88babe2008-01-09 22:43:08 +00002937 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002938 switch (LHSClass) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00002939#define TYPE(Class, Base)
2940#define ABSTRACT_TYPE(Class, Base)
2941#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2942#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2943#include "clang/AST/TypeNodes.def"
2944 assert(false && "Non-canonical and dependent types shouldn't get here");
2945 return QualType();
2946
Sebastian Redlce6fff02009-03-16 23:22:08 +00002947 case Type::LValueReference:
2948 case Type::RValueReference:
Douglas Gregor4fa58902009-02-26 23:50:07 +00002949 case Type::MemberPointer:
2950 assert(false && "C++ should never be in mergeTypes");
2951 return QualType();
2952
2953 case Type::IncompleteArray:
2954 case Type::VariableArray:
2955 case Type::FunctionProto:
2956 case Type::ExtVector:
2957 case Type::ObjCQualifiedInterface:
2958 assert(false && "Types are eliminated above");
2959 return QualType();
2960
Chris Lattnerc38d4522008-01-14 05:45:46 +00002961 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002962 {
2963 // Merge two pointer types, while trying to preserve typedef info
2964 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2965 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2966 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2967 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002968 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2969 return LHS;
2970 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2971 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002972 return getPointerType(ResultType);
2973 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002974 case Type::BlockPointer:
2975 {
2976 // Merge two block pointer types, while trying to preserve typedef info
2977 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2978 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2979 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2980 if (ResultType.isNull()) return QualType();
2981 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2982 return LHS;
2983 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2984 return RHS;
2985 return getBlockPointerType(ResultType);
2986 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002987 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002988 {
2989 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2990 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2991 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2992 return QualType();
2993
2994 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2995 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2996 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2997 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002998 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2999 return LHS;
3000 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3001 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003002 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3003 ArrayType::ArraySizeModifier(), 0);
3004 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3005 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003006 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3007 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003008 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3009 return LHS;
3010 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3011 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003012 if (LVAT) {
3013 // FIXME: This isn't correct! But tricky to implement because
3014 // the array's size has to be the size of LHS, but the type
3015 // has to be different.
3016 return LHS;
3017 }
3018 if (RVAT) {
3019 // FIXME: This isn't correct! But tricky to implement because
3020 // the array's size has to be the size of RHS, but the type
3021 // has to be different.
3022 return RHS;
3023 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003024 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3025 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003026 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003027 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003028 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003029 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor4fa58902009-02-26 23:50:07 +00003030 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003031 case Type::Enum:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003032 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00003033 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3034 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003035 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00003036 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003037 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003038 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00003039 case Type::Complex:
3040 // Distinct complex types are incompatible.
3041 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003042 case Type::Vector:
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003043 // FIXME: The merged type should be an ExtVector!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003044 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3045 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003046 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003047 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003048 // Check if the interfaces are assignment compatible.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003049 // FIXME: This should be type compatibility, e.g. whether
3050 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff0bbc1352009-02-21 16:18:07 +00003051 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3052 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3053 if (LHSIface && RHSIface &&
3054 canAssignObjCInterfaces(LHSIface, RHSIface))
3055 return LHS;
3056
Eli Friedman0d9549b2008-08-22 00:56:42 +00003057 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003058 }
Steve Naroff28ceff72008-12-10 22:14:21 +00003059 case Type::ObjCQualifiedId:
3060 // Distinct qualified id's are not compatible.
3061 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003062 case Type::FixedWidthInt:
3063 // Distinct fixed-width integers are not compatible.
3064 return QualType();
3065 case Type::ObjCQualifiedClass:
3066 // Distinct qualified classes are not compatible.
3067 return QualType();
3068 case Type::ExtQual:
3069 // FIXME: ExtQual types can be compatible even if they're not
3070 // identical!
3071 return QualType();
3072 // First attempt at an implementation, but I'm not really sure it's
3073 // right...
3074#if 0
3075 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3076 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3077 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3078 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3079 return QualType();
3080 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3081 LHSBase = QualType(LQual->getBaseType(), 0);
3082 RHSBase = QualType(RQual->getBaseType(), 0);
3083 ResultType = mergeTypes(LHSBase, RHSBase);
3084 if (ResultType.isNull()) return QualType();
3085 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3086 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3087 return LHS;
3088 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3089 return RHS;
3090 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3091 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3092 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3093 return ResultType;
3094#endif
Douglas Gregordd13e842009-03-30 22:58:21 +00003095
3096 case Type::TemplateSpecialization:
3097 assert(false && "Dependent types have no size");
3098 break;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003099 }
Douglas Gregor4fa58902009-02-26 23:50:07 +00003100
3101 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003102}
Ted Kremenek738e6c02007-10-31 17:10:13 +00003103
Chris Lattner1d78a862008-04-07 07:01:58 +00003104//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00003105// Integer Predicates
3106//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00003107
Eli Friedman0832dbc2008-06-28 06:23:08 +00003108unsigned ASTContext::getIntWidth(QualType T) {
3109 if (T == BoolTy)
3110 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00003111 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3112 return FWIT->getWidth();
3113 }
3114 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00003115 return (unsigned)getTypeSize(T);
3116}
3117
3118QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3119 assert(T->isSignedIntegerType() && "Unexpected type");
3120 if (const EnumType* ETy = T->getAsEnumType())
3121 T = ETy->getDecl()->getIntegerType();
3122 const BuiltinType* BTy = T->getAsBuiltinType();
3123 assert (BTy && "Unexpected signed integer type");
3124 switch (BTy->getKind()) {
3125 case BuiltinType::Char_S:
3126 case BuiltinType::SChar:
3127 return UnsignedCharTy;
3128 case BuiltinType::Short:
3129 return UnsignedShortTy;
3130 case BuiltinType::Int:
3131 return UnsignedIntTy;
3132 case BuiltinType::Long:
3133 return UnsignedLongTy;
3134 case BuiltinType::LongLong:
3135 return UnsignedLongLongTy;
3136 default:
3137 assert(0 && "Unexpected signed integer type");
3138 return QualType();
3139 }
3140}
3141
3142
3143//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00003144// Serialization Support
3145//===----------------------------------------------------------------------===//
3146
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003147enum {
3148 BasicMetadataBlock = 1,
3149 ASTContextBlock = 2,
3150 DeclsBlock = 3
3151};
3152
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003153void ASTContext::EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const{
3154 // Create bitstream.
3155 llvm::BitstreamWriter Stream(Buffer);
3156
3157 // Emit the preamble.
3158 Stream.Emit((unsigned)'B', 8);
3159 Stream.Emit((unsigned)'C', 8);
3160 Stream.Emit(0xC, 4);
3161 Stream.Emit(0xF, 4);
3162 Stream.Emit(0xE, 4);
3163 Stream.Emit(0x0, 4);
3164
3165 // Create serializer.
3166 llvm::Serializer S(Stream);
3167
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003168 // ===---------------------------------------------------===/
3169 // Serialize the "Translation Unit" metadata.
3170 // ===---------------------------------------------------===/
3171
3172 // Emit ASTContext.
3173 S.EnterBlock(ASTContextBlock);
3174 S.EmitOwnedPtr(this);
3175 S.ExitBlock(); // exit "ASTContextBlock"
3176
3177 S.EnterBlock(BasicMetadataBlock);
3178
3179 // Block for SourceManager and Target. Allows easy skipping
3180 // around to the block for the Selectors during deserialization.
3181 S.EnterBlock();
3182
3183 // Emit the SourceManager.
3184 S.Emit(getSourceManager());
3185
3186 // Emit the Target.
3187 S.EmitPtr(&Target);
3188 S.EmitCStr(Target.getTargetTriple());
3189
3190 S.ExitBlock(); // exit "SourceManager and Target Block"
3191
3192 // Emit the Selectors.
3193 S.Emit(Selectors);
3194
3195 // Emit the Identifier Table.
3196 S.Emit(Idents);
3197
3198 S.ExitBlock(); // exit "BasicMetadataBlock"
3199}
3200
3201
Ted Kremenek738e6c02007-10-31 17:10:13 +00003202/// Emit - Serialize an ASTContext object to Bitcode.
3203void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00003204 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00003205 S.EmitRef(SourceMgr);
3206 S.EmitRef(Target);
3207 S.EmitRef(Idents);
3208 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003209
Ted Kremenek68228a92007-10-31 22:44:07 +00003210 // Emit the size of the type vector so that we can reserve that size
3211 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003212 S.EmitInt(Types.size());
3213
Ted Kremenek034a78c2007-11-13 22:02:55 +00003214 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3215 I!=E;++I)
3216 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003217
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003218 S.EmitOwnedPtr(TUDecl);
3219
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003220 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003221}
3222
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003223
3224ASTContext *ASTContext::ReadASTBitcodeBuffer(llvm::MemoryBuffer &Buffer,
3225 FileManager &FMgr) {
3226 // Check if the file is of the proper length.
3227 if (Buffer.getBufferSize() & 0x3) {
3228 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
3229 return 0;
3230 }
3231
3232 // Create the bitstream reader.
3233 unsigned char *BufPtr = (unsigned char *)Buffer.getBufferStart();
3234 llvm::BitstreamReader Stream(BufPtr, BufPtr+Buffer.getBufferSize());
3235
3236 if (Stream.Read(8) != 'B' ||
3237 Stream.Read(8) != 'C' ||
3238 Stream.Read(4) != 0xC ||
3239 Stream.Read(4) != 0xF ||
3240 Stream.Read(4) != 0xE ||
3241 Stream.Read(4) != 0x0) {
3242 // FIXME: Provide diagnostic.
3243 return NULL;
3244 }
3245
3246 // Create the deserializer.
3247 llvm::Deserializer Dezr(Stream);
3248
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003249 // ===---------------------------------------------------===/
3250 // Deserialize the "Translation Unit" metadata.
3251 // ===---------------------------------------------------===/
3252
3253 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
3254 // (which will appear earlier) and record its location.
3255
3256 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
3257 assert (FoundBlock);
3258
3259 llvm::Deserializer::Location ASTContextBlockLoc =
3260 Dezr.getCurrentBlockLocation();
3261
3262 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
3263 assert (FoundBlock);
3264
3265 // Read the SourceManager.
3266 SourceManager::CreateAndRegister(Dezr, FMgr);
3267
3268 { // Read the TargetInfo.
3269 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
3270 char* triple = Dezr.ReadCStr(NULL,0,true);
3271 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
3272 delete [] triple;
3273 }
3274
3275 // For Selectors, we must read the identifier table first because the
3276 // SelectorTable depends on the identifiers being already deserialized.
3277 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
3278 Dezr.SkipBlock();
3279
3280 // Read the identifier table.
3281 IdentifierTable::CreateAndRegister(Dezr);
3282
3283 // Now jump back and read the selectors.
3284 Dezr.JumpTo(SelectorBlkLoc);
3285 SelectorTable::CreateAndRegister(Dezr);
3286
3287 // Now jump back to ASTContextBlock and read the ASTContext.
3288 Dezr.JumpTo(ASTContextBlockLoc);
3289 return Dezr.ReadOwnedPtr<ASTContext>();
3290}
3291
Ted Kremenekacba3612007-11-13 00:25:37 +00003292ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00003293
3294 // Read the language options.
3295 LangOptions LOpts;
3296 LOpts.Read(D);
3297
Ted Kremenek68228a92007-10-31 22:44:07 +00003298 SourceManager &SM = D.ReadRef<SourceManager>();
3299 TargetInfo &t = D.ReadRef<TargetInfo>();
3300 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3301 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00003302
Ted Kremenek68228a92007-10-31 22:44:07 +00003303 unsigned size_reserve = D.ReadInt();
3304
Douglas Gregor24afd4a2008-11-17 14:58:09 +00003305 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3306 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00003307
Ted Kremenek034a78c2007-11-13 22:02:55 +00003308 for (unsigned i = 0; i < size_reserve; ++i)
3309 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00003310
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003311 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3312
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003313 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00003314
3315 return A;
3316}