blob: 95c3c5fcaf33dc55616760b4c4801db1f006c4cb [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 {
Chris Lattner608c1e32009-03-31 09:24:30 +000072 llvm::DenseMap<const ObjCInterfaceDecl*, RecordDecl*>::iterator
Nuno Lopes355a8682008-12-17 22:30:25 +000073 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
74 while (I != E) {
Chris Lattner608c1e32009-03-31 09:24:30 +000075 RecordDecl *R = (I++)->second;
Nuno Lopes355a8682008-12-17 22:30:25 +000076 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,
Chris Lattner9329cf52009-03-31 08:48:01 +0000601 llvm::SmallVectorImpl<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) {
Chris Lattner9329cf52009-03-31 08:48:01 +0000607 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000608 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) {
Chris Lattner9329cf52009-03-31 08:48:01 +0000614 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
Fariborz Jahanianc625fa92009-03-31 00:06:29 +0000615 Fields.push_back(cast<FieldDecl>(IV));
616 }
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000617}
618
619/// addRecordToClass - produces record info. for the class for its
620/// ivars and all those inherited.
621///
Chris Lattner9329cf52009-03-31 08:48:01 +0000622const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D) {
Chris Lattner608c1e32009-03-31 09:24:30 +0000623 RecordDecl *&RD = ASTRecordForInterface[D];
624 if (RD) {
625 // If we have a record decl already and it is either a definition or if 'D'
626 // is still a forward declaration, return it.
627 if (RD->isDefinition() || D->isForwardDecl())
628 return RD;
629 }
630
631 // If D is a forward declaration, then just make a forward struct decl.
632 if (D->isForwardDecl())
633 return RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
634 D->getLocation(),
635 D->getIdentifier());
Chris Lattner9329cf52009-03-31 08:48:01 +0000636
637 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000638 CollectObjCIvars(D, RecFields);
Chris Lattner608c1e32009-03-31 09:24:30 +0000639
640 if (RD == 0)
641 RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, D->getLocation(),
642 D->getIdentifier());
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000643 /// FIXME! Can do collection of ivars and adding to the record while
644 /// doing it.
Chris Lattner41b780c2009-03-31 08:58:42 +0000645 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Chris Lattner608c1e32009-03-31 09:24:30 +0000646 RD->addDecl(FieldDecl::Create(*this, RD,
647 RecFields[i]->getLocation(),
648 RecFields[i]->getIdentifier(),
649 RecFields[i]->getType(),
650 RecFields[i]->getBitWidth(), false));
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000651 }
Chris Lattner9329cf52009-03-31 08:48:01 +0000652
Chris Lattner608c1e32009-03-31 09:24:30 +0000653 RD->completeDefinition(*this);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000654 return RD;
655}
Devang Patel4b6bf702008-06-04 21:54:36 +0000656
Fariborz Jahanianea944842008-12-18 17:29:46 +0000657/// setFieldDecl - maps a field for the given Ivar reference node.
658//
659void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
660 const ObjCIvarDecl *Ivar,
661 const ObjCIvarRefExpr *MRef) {
Chris Lattner04f4db72009-03-31 08:31:13 +0000662 ASTFieldForIvarRef[MRef] = OI->lookupFieldDeclForIvar(*this, Ivar);
Fariborz Jahanianea944842008-12-18 17:29:46 +0000663}
664
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000665/// getASTObjcInterfaceLayout - Get or compute information about the layout of
666/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000667/// position information.
668const ASTRecordLayout &
669ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
670 // Look up this layout, if already laid out, return what we have.
671 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
672 if (Entry) return *Entry;
673
674 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
675 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000676 ASTRecordLayout *NewEntry = NULL;
677 unsigned FieldCount = D->ivar_size();
678 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
679 FieldCount++;
680 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
681 unsigned Alignment = SL.getAlignment();
682 uint64_t Size = SL.getSize();
683 NewEntry = new ASTRecordLayout(Size, Alignment);
684 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000685 // Super class is at the beginning of the layout.
686 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000687 } else {
688 NewEntry = new ASTRecordLayout();
689 NewEntry->InitializeLayout(FieldCount);
690 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000691 Entry = NewEntry;
692
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000693 unsigned StructPacking = 0;
694 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
695 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000696
697 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
698 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
699 AA->getAlignment()));
700
701 // Layout each ivar sequentially.
702 unsigned i = 0;
703 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
704 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
705 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000706 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000707 }
708
709 // Finally, round the size of the total struct up to the alignment of the
710 // struct itself.
711 NewEntry->FinalizeLayout();
712 return *NewEntry;
713}
714
Devang Patel7a78e432007-11-01 19:11:01 +0000715/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000716/// specified record (struct/union/class), which indicates its size and field
717/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000718const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000719 D = D->getDefinition(*this);
720 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000721
Chris Lattner4b009652007-07-25 00:24:17 +0000722 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000723 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000724 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000725
Devang Patel7a78e432007-11-01 19:11:01 +0000726 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
727 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
728 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000729 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000730
Douglas Gregor39677622008-12-11 20:41:00 +0000731 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000732 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000733 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000734
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000735 unsigned StructPacking = 0;
736 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
737 StructPacking = PA->getAlignment();
738
Eli Friedman5949a022008-05-30 09:31:38 +0000739 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000740 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
741 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000742
Eli Friedman5949a022008-05-30 09:31:38 +0000743 // Layout each field, for now, just sequentially, respecting alignment. In
744 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000745 unsigned FieldIdx = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000746 for (RecordDecl::field_iterator Field = D->field_begin(),
747 FieldEnd = D->field_end();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000748 Field != FieldEnd; (void)++Field, ++FieldIdx)
749 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000750
751 // Finally, round the size of the total struct up to the alignment of the
752 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000753 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000754 return *NewEntry;
755}
756
Chris Lattner4b009652007-07-25 00:24:17 +0000757//===----------------------------------------------------------------------===//
758// Type creation/memoization methods
759//===----------------------------------------------------------------------===//
760
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000761QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000762 QualType CanT = getCanonicalType(T);
763 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000764 return T;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000765
766 // If we are composing extended qualifiers together, merge together into one
767 // ExtQualType node.
768 unsigned CVRQuals = T.getCVRQualifiers();
769 QualType::GCAttrTypes GCAttr = QualType::GCNone;
770 Type *TypeNode = T.getTypePtr();
Chris Lattner35fef522008-02-20 20:55:12 +0000771
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000772 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
773 // If this type already has an address space specified, it cannot get
774 // another one.
775 assert(EQT->getAddressSpace() == 0 &&
776 "Type cannot be in multiple addr spaces!");
777 GCAttr = EQT->getObjCGCAttr();
778 TypeNode = EQT->getBaseType();
779 }
Chris Lattner35fef522008-02-20 20:55:12 +0000780
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000781 // Check if we've already instantiated this type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000782 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000783 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000784 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000785 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000786 return QualType(EXTQy, CVRQuals);
787
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000788 // If the base type isn't canonical, this won't be a canonical type either,
789 // so fill in the canonical type field.
790 QualType Canonical;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000791 if (!TypeNode->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000792 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000793
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000794 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000795 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000796 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000797 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000798 ExtQualType *New =
799 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000800 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000801 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000802 return QualType(New, CVRQuals);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000803}
804
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000805QualType ASTContext::getObjCGCQualType(QualType T,
806 QualType::GCAttrTypes GCAttr) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000807 QualType CanT = getCanonicalType(T);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000808 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000809 return T;
810
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000811 // If we are composing extended qualifiers together, merge together into one
812 // ExtQualType node.
813 unsigned CVRQuals = T.getCVRQualifiers();
814 Type *TypeNode = T.getTypePtr();
815 unsigned AddressSpace = 0;
816
817 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
818 // If this type already has an address space specified, it cannot get
819 // another one.
820 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
821 "Type cannot be in multiple addr spaces!");
822 AddressSpace = EQT->getAddressSpace();
823 TypeNode = EQT->getBaseType();
824 }
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000825
826 // Check if we've already instantiated an gc qual'd type of this type.
827 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000828 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000829 void *InsertPos = 0;
830 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000831 return QualType(EXTQy, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000832
833 // If the base type isn't canonical, this won't be a canonical type either,
834 // so fill in the canonical type field.
Eli Friedman94fcc9a2009-02-27 23:04:43 +0000835 // FIXME: Isn't this also not canonical if the base type is a array
836 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000837 QualType Canonical;
838 if (!T->isCanonical()) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000839 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000840
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000841 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000842 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
843 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
844 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000845 ExtQualType *New =
846 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000847 ExtQualTypes.InsertNode(New, InsertPos);
848 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000849 return QualType(New, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000850}
Chris Lattner4b009652007-07-25 00:24:17 +0000851
852/// getComplexType - Return the uniqued reference to the type for a complex
853/// number with the specified element type.
854QualType ASTContext::getComplexType(QualType T) {
855 // Unique pointers, to guarantee there is only one pointer of a particular
856 // structure.
857 llvm::FoldingSetNodeID ID;
858 ComplexType::Profile(ID, T);
859
860 void *InsertPos = 0;
861 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
862 return QualType(CT, 0);
863
864 // If the pointee type isn't canonical, this won't be a canonical type either,
865 // so fill in the canonical type field.
866 QualType Canonical;
867 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000868 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000869
870 // Get the new insert position for the node we care about.
871 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000872 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000873 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000874 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000875 Types.push_back(New);
876 ComplexTypes.InsertNode(New, InsertPos);
877 return QualType(New, 0);
878}
879
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000880QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
881 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
882 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
883 FixedWidthIntType *&Entry = Map[Width];
884 if (!Entry)
885 Entry = new FixedWidthIntType(Width, Signed);
886 return QualType(Entry, 0);
887}
Chris Lattner4b009652007-07-25 00:24:17 +0000888
889/// getPointerType - Return the uniqued reference to the type for a pointer to
890/// the specified type.
891QualType ASTContext::getPointerType(QualType T) {
892 // Unique pointers, to guarantee there is only one pointer of a particular
893 // structure.
894 llvm::FoldingSetNodeID ID;
895 PointerType::Profile(ID, T);
896
897 void *InsertPos = 0;
898 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
899 return QualType(PT, 0);
900
901 // If the pointee type isn't canonical, this won't be a canonical type either,
902 // so fill in the canonical type field.
903 QualType Canonical;
904 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000905 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000906
907 // Get the new insert position for the node we care about.
908 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000909 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000910 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000911 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000912 Types.push_back(New);
913 PointerTypes.InsertNode(New, InsertPos);
914 return QualType(New, 0);
915}
916
Steve Naroff7aa54752008-08-27 16:04:49 +0000917/// getBlockPointerType - Return the uniqued reference to the type for
918/// a pointer to the specified block.
919QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000920 assert(T->isFunctionType() && "block of function types only");
921 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000922 // structure.
923 llvm::FoldingSetNodeID ID;
924 BlockPointerType::Profile(ID, T);
925
926 void *InsertPos = 0;
927 if (BlockPointerType *PT =
928 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
929 return QualType(PT, 0);
930
Steve Narofffd5b19d2008-08-28 19:20:44 +0000931 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000932 // type either so fill in the canonical type field.
933 QualType Canonical;
934 if (!T->isCanonical()) {
935 Canonical = getBlockPointerType(getCanonicalType(T));
936
937 // Get the new insert position for the node we care about.
938 BlockPointerType *NewIP =
939 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000940 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000941 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000942 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000943 Types.push_back(New);
944 BlockPointerTypes.InsertNode(New, InsertPos);
945 return QualType(New, 0);
946}
947
Sebastian Redlce6fff02009-03-16 23:22:08 +0000948/// getLValueReferenceType - Return the uniqued reference to the type for an
949/// lvalue reference to the specified type.
950QualType ASTContext::getLValueReferenceType(QualType T) {
Chris Lattner4b009652007-07-25 00:24:17 +0000951 // Unique pointers, to guarantee there is only one pointer of a particular
952 // structure.
953 llvm::FoldingSetNodeID ID;
954 ReferenceType::Profile(ID, T);
955
956 void *InsertPos = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000957 if (LValueReferenceType *RT =
958 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000959 return QualType(RT, 0);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000960
Chris Lattner4b009652007-07-25 00:24:17 +0000961 // If the referencee type isn't canonical, this won't be a canonical type
962 // either, so fill in the canonical type field.
963 QualType Canonical;
964 if (!T->isCanonical()) {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000965 Canonical = getLValueReferenceType(getCanonicalType(T));
966
Chris Lattner4b009652007-07-25 00:24:17 +0000967 // Get the new insert position for the node we care about.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000968 LValueReferenceType *NewIP =
969 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000970 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000971 }
972
Sebastian Redlce6fff02009-03-16 23:22:08 +0000973 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000974 Types.push_back(New);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000975 LValueReferenceTypes.InsertNode(New, InsertPos);
976 return QualType(New, 0);
977}
978
979/// getRValueReferenceType - Return the uniqued reference to the type for an
980/// rvalue reference to the specified type.
981QualType ASTContext::getRValueReferenceType(QualType T) {
982 // Unique pointers, to guarantee there is only one pointer of a particular
983 // structure.
984 llvm::FoldingSetNodeID ID;
985 ReferenceType::Profile(ID, T);
986
987 void *InsertPos = 0;
988 if (RValueReferenceType *RT =
989 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
990 return QualType(RT, 0);
991
992 // If the referencee type isn't canonical, this won't be a canonical type
993 // either, so fill in the canonical type field.
994 QualType Canonical;
995 if (!T->isCanonical()) {
996 Canonical = getRValueReferenceType(getCanonicalType(T));
997
998 // Get the new insert position for the node we care about.
999 RValueReferenceType *NewIP =
1000 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1001 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1002 }
1003
1004 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1005 Types.push_back(New);
1006 RValueReferenceTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001007 return QualType(New, 0);
1008}
1009
Sebastian Redl75555032009-01-24 21:16:55 +00001010/// getMemberPointerType - Return the uniqued reference to the type for a
1011/// member pointer to the specified type, in the specified class.
1012QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1013{
1014 // Unique pointers, to guarantee there is only one pointer of a particular
1015 // structure.
1016 llvm::FoldingSetNodeID ID;
1017 MemberPointerType::Profile(ID, T, Cls);
1018
1019 void *InsertPos = 0;
1020 if (MemberPointerType *PT =
1021 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1022 return QualType(PT, 0);
1023
1024 // If the pointee or class type isn't canonical, this won't be a canonical
1025 // type either, so fill in the canonical type field.
1026 QualType Canonical;
1027 if (!T->isCanonical()) {
1028 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1029
1030 // Get the new insert position for the node we care about.
1031 MemberPointerType *NewIP =
1032 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1033 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1034 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001035 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +00001036 Types.push_back(New);
1037 MemberPointerTypes.InsertNode(New, InsertPos);
1038 return QualType(New, 0);
1039}
1040
Steve Naroff83c13012007-08-30 01:06:46 +00001041/// getConstantArrayType - Return the unique reference to the type for an
1042/// array of the specified element type.
1043QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +00001044 const llvm::APInt &ArySize,
1045 ArrayType::ArraySizeModifier ASM,
1046 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001047 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001048 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001049
1050 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +00001051 if (ConstantArrayType *ATP =
1052 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001053 return QualType(ATP, 0);
1054
1055 // If the element type isn't canonical, this won't be a canonical type either,
1056 // so fill in the canonical type field.
1057 QualType Canonical;
1058 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001059 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +00001060 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001061 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +00001062 ConstantArrayType *NewIP =
1063 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001064 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001065 }
1066
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001067 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001068 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001069 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001070 Types.push_back(New);
1071 return QualType(New, 0);
1072}
1073
Steve Naroffe2579e32007-08-30 18:14:25 +00001074/// getVariableArrayType - Returns a non-unique reference to the type for a
1075/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +00001076QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1077 ArrayType::ArraySizeModifier ASM,
1078 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +00001079 // Since we don't unique expressions, it isn't possible to unique VLA's
1080 // that have an expression provided for their size.
1081
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001082 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001083 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001084
1085 VariableArrayTypes.push_back(New);
1086 Types.push_back(New);
1087 return QualType(New, 0);
1088}
1089
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001090/// getDependentSizedArrayType - Returns a non-unique reference to
1091/// the type for a dependently-sized array of the specified element
1092/// type. FIXME: We will need these to be uniqued, or at least
1093/// comparable, at some point.
1094QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1095 ArrayType::ArraySizeModifier ASM,
1096 unsigned EltTypeQuals) {
1097 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1098 "Size must be type- or value-dependent!");
1099
1100 // Since we don't unique expressions, it isn't possible to unique
1101 // dependently-sized array types.
1102
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001103 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001104 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1105 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001106
1107 DependentSizedArrayTypes.push_back(New);
1108 Types.push_back(New);
1109 return QualType(New, 0);
1110}
1111
Eli Friedman8ff07782008-02-15 18:16:39 +00001112QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1113 ArrayType::ArraySizeModifier ASM,
1114 unsigned EltTypeQuals) {
1115 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001116 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001117
1118 void *InsertPos = 0;
1119 if (IncompleteArrayType *ATP =
1120 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1121 return QualType(ATP, 0);
1122
1123 // If the element type isn't canonical, this won't be a canonical type
1124 // either, so fill in the canonical type field.
1125 QualType Canonical;
1126
1127 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001128 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001129 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001130
1131 // Get the new insert position for the node we care about.
1132 IncompleteArrayType *NewIP =
1133 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001134 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001135 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001136
Steve Naroff93fd2112009-01-27 22:08:43 +00001137 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001138 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001139
1140 IncompleteArrayTypes.InsertNode(New, InsertPos);
1141 Types.push_back(New);
1142 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001143}
1144
Chris Lattner4b009652007-07-25 00:24:17 +00001145/// getVectorType - Return the unique reference to a vector type of
1146/// the specified element type and size. VectorType must be a built-in type.
1147QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1148 BuiltinType *baseType;
1149
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001150 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001151 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1152
1153 // Check if we've already instantiated a vector of this type.
1154 llvm::FoldingSetNodeID ID;
1155 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1156 void *InsertPos = 0;
1157 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1158 return QualType(VTP, 0);
1159
1160 // If the element type isn't canonical, this won't be a canonical type either,
1161 // so fill in the canonical type field.
1162 QualType Canonical;
1163 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001164 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001165
1166 // Get the new insert position for the node we care about.
1167 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001168 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001169 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001170 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001171 VectorTypes.InsertNode(New, InsertPos);
1172 Types.push_back(New);
1173 return QualType(New, 0);
1174}
1175
Nate Begemanaf6ed502008-04-18 23:10:10 +00001176/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001177/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001178QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001179 BuiltinType *baseType;
1180
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001181 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001182 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001183
1184 // Check if we've already instantiated a vector of this type.
1185 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001186 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001187 void *InsertPos = 0;
1188 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1189 return QualType(VTP, 0);
1190
1191 // If the element type isn't canonical, this won't be a canonical type either,
1192 // so fill in the canonical type field.
1193 QualType Canonical;
1194 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001195 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001196
1197 // Get the new insert position for the node we care about.
1198 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001199 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001200 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001201 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001202 VectorTypes.InsertNode(New, InsertPos);
1203 Types.push_back(New);
1204 return QualType(New, 0);
1205}
1206
Douglas Gregor4fa58902009-02-26 23:50:07 +00001207/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001208///
Douglas Gregor4fa58902009-02-26 23:50:07 +00001209QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Chris Lattner4b009652007-07-25 00:24:17 +00001210 // Unique functions, to guarantee there is only one function of a particular
1211 // structure.
1212 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001213 FunctionNoProtoType::Profile(ID, ResultTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001214
1215 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001216 if (FunctionNoProtoType *FT =
1217 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001218 return QualType(FT, 0);
1219
1220 QualType Canonical;
1221 if (!ResultTy->isCanonical()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00001222 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001223
1224 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001225 FunctionNoProtoType *NewIP =
1226 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001227 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001228 }
1229
Douglas Gregor4fa58902009-02-26 23:50:07 +00001230 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001231 Types.push_back(New);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001232 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001233 return QualType(New, 0);
1234}
1235
1236/// getFunctionType - Return a normal function type with a typed argument
1237/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001238QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001239 unsigned NumArgs, bool isVariadic,
1240 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001241 // Unique functions, to guarantee there is only one function of a particular
1242 // structure.
1243 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001244 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001245 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001246
1247 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001248 if (FunctionProtoType *FTP =
1249 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001250 return QualType(FTP, 0);
1251
1252 // Determine whether the type being created is already canonical or not.
1253 bool isCanonical = ResultTy->isCanonical();
1254 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1255 if (!ArgArray[i]->isCanonical())
1256 isCanonical = false;
1257
1258 // If this type isn't canonical, get the canonical version of it.
1259 QualType Canonical;
1260 if (!isCanonical) {
1261 llvm::SmallVector<QualType, 16> CanonicalArgs;
1262 CanonicalArgs.reserve(NumArgs);
1263 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001264 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001265
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001266 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001267 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001268 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001269
1270 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001271 FunctionProtoType *NewIP =
1272 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001273 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001274 }
1275
Douglas Gregor4fa58902009-02-26 23:50:07 +00001276 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001277 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001278 FunctionProtoType *FTP =
1279 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001280 NumArgs*sizeof(QualType), 8);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001281 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001282 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001283 Types.push_back(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001284 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001285 return QualType(FTP, 0);
1286}
1287
Douglas Gregor1d661552008-04-13 21:07:44 +00001288/// getTypeDeclType - Return the unique reference to the type for the
1289/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001290QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001291 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001292 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1293
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001294 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001295 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001296 else if (isa<TemplateTypeParmDecl>(Decl)) {
1297 assert(false && "Template type parameter types are always available.");
1298 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001299 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001300
Douglas Gregor2e047592009-02-28 01:32:25 +00001301 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001302 if (PrevDecl)
1303 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001304 else
1305 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001306 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001307 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1308 if (PrevDecl)
1309 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001310 else
1311 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001312 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001313 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001314 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001315
Ted Kremenek46a837c2008-09-05 17:16:31 +00001316 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001317 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001318}
1319
Chris Lattner4b009652007-07-25 00:24:17 +00001320/// getTypedefType - Return the unique reference to the type for the
1321/// specified typename decl.
1322QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1323 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1324
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001325 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001326 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001327 Types.push_back(Decl->TypeForDecl);
1328 return QualType(Decl->TypeForDecl, 0);
1329}
1330
Ted Kremenek42730c52008-01-07 19:49:32 +00001331/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001332/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001333QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001334 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1335
Steve Naroff93fd2112009-01-27 22:08:43 +00001336 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001337 Types.push_back(Decl->TypeForDecl);
1338 return QualType(Decl->TypeForDecl, 0);
1339}
1340
Fariborz Jahanian27ecc672009-02-14 20:13:28 +00001341/// buildObjCInterfaceType - Returns a new type for the interface
1342/// declaration, regardless. It also removes any previously built
1343/// record declaration so caller can rebuild it.
1344QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Chris Lattner608c1e32009-03-31 09:24:30 +00001345 RecordDecl *&RD = ASTRecordForInterface[Decl];
Fariborz Jahanian27ecc672009-02-14 20:13:28 +00001346 if (RD)
1347 RD = 0;
1348 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
1349 Types.push_back(Decl->TypeForDecl);
1350 return QualType(Decl->TypeForDecl, 0);
1351}
1352
Douglas Gregora4918772009-02-05 23:33:38 +00001353/// \brief Retrieve the template type parameter type for a template
1354/// parameter with the given depth, index, and (optionally) name.
1355QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1356 IdentifierInfo *Name) {
1357 llvm::FoldingSetNodeID ID;
1358 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1359 void *InsertPos = 0;
1360 TemplateTypeParmType *TypeParm
1361 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1362
1363 if (TypeParm)
1364 return QualType(TypeParm, 0);
1365
1366 if (Name)
1367 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1368 getTemplateTypeParmType(Depth, Index));
1369 else
1370 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1371
1372 Types.push_back(TypeParm);
1373 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1374
1375 return QualType(TypeParm, 0);
1376}
1377
Douglas Gregor8e458f42009-02-09 18:46:07 +00001378QualType
Douglas Gregordd13e842009-03-30 22:58:21 +00001379ASTContext::getTemplateSpecializationType(TemplateName Template,
1380 const TemplateArgument *Args,
1381 unsigned NumArgs,
1382 QualType Canon) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001383 if (!Canon.isNull())
1384 Canon = getCanonicalType(Canon);
Douglas Gregor9c7825b2009-02-26 22:19:44 +00001385
Douglas Gregor8e458f42009-02-09 18:46:07 +00001386 llvm::FoldingSetNodeID ID;
Douglas Gregordd13e842009-03-30 22:58:21 +00001387 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001388
Douglas Gregor8e458f42009-02-09 18:46:07 +00001389 void *InsertPos = 0;
Douglas Gregordd13e842009-03-30 22:58:21 +00001390 TemplateSpecializationType *Spec
1391 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001392
1393 if (Spec)
1394 return QualType(Spec, 0);
1395
Douglas Gregordd13e842009-03-30 22:58:21 +00001396 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001397 sizeof(TemplateArgument) * NumArgs),
1398 8);
Douglas Gregordd13e842009-03-30 22:58:21 +00001399 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001400 Types.push_back(Spec);
Douglas Gregordd13e842009-03-30 22:58:21 +00001401 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001402
1403 return QualType(Spec, 0);
1404}
1405
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001406QualType
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001407ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001408 QualType NamedType) {
1409 llvm::FoldingSetNodeID ID;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001410 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001411
1412 void *InsertPos = 0;
1413 QualifiedNameType *T
1414 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1415 if (T)
1416 return QualType(T, 0);
1417
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001418 T = new (*this) QualifiedNameType(NNS, NamedType,
1419 getCanonicalType(NamedType));
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001420 Types.push_back(T);
1421 QualifiedNameTypes.InsertNode(T, InsertPos);
1422 return QualType(T, 0);
1423}
1424
Douglas Gregord3022602009-03-27 23:10:48 +00001425QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1426 const IdentifierInfo *Name,
1427 QualType Canon) {
1428 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1429
1430 if (Canon.isNull()) {
1431 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1432 if (CanonNNS != NNS)
1433 Canon = getTypenameType(CanonNNS, Name);
1434 }
1435
1436 llvm::FoldingSetNodeID ID;
1437 TypenameType::Profile(ID, NNS, Name);
1438
1439 void *InsertPos = 0;
1440 TypenameType *T
1441 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1442 if (T)
1443 return QualType(T, 0);
1444
1445 T = new (*this) TypenameType(NNS, Name, Canon);
1446 Types.push_back(T);
1447 TypenameTypes.InsertNode(T, InsertPos);
1448 return QualType(T, 0);
1449}
1450
Chris Lattnere1352302008-04-07 04:56:42 +00001451/// CmpProtocolNames - Comparison predicate for sorting protocols
1452/// alphabetically.
1453static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1454 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001455 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001456}
1457
1458static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1459 unsigned &NumProtocols) {
1460 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1461
1462 // Sort protocols, keyed by name.
1463 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1464
1465 // Remove duplicates.
1466 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1467 NumProtocols = ProtocolsEnd-Protocols;
1468}
1469
1470
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001471/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1472/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001473QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1474 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001475 // Sort the protocol list alphabetically to canonicalize it.
1476 SortAndUniqueProtocols(Protocols, NumProtocols);
1477
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001478 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001479 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001480
1481 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001482 if (ObjCQualifiedInterfaceType *QT =
1483 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001484 return QualType(QT, 0);
1485
1486 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001487 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001488 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001489
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001490 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001491 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001492 return QualType(QType, 0);
1493}
1494
Chris Lattnere1352302008-04-07 04:56:42 +00001495/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1496/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001497QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001498 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001499 // Sort the protocol list alphabetically to canonicalize it.
1500 SortAndUniqueProtocols(Protocols, NumProtocols);
1501
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001502 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001503 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001504
1505 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001506 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001507 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001508 return QualType(QT, 0);
1509
1510 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001511 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001512 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001513 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001514 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001515 return QualType(QType, 0);
1516}
1517
Douglas Gregor4fa58902009-02-26 23:50:07 +00001518/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1519/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff0604dd92007-08-01 18:02:17 +00001520/// multiple declarations that refer to "typeof(x)" all contain different
1521/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1522/// on canonical type's (which are always unique).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001523QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001524 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001525 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001526 Types.push_back(toe);
1527 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001528}
1529
Steve Naroff0604dd92007-08-01 18:02:17 +00001530/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1531/// TypeOfType AST's. The only motivation to unique these nodes would be
1532/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1533/// an issue. This doesn't effect the type checker, since it operates
1534/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001535QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001536 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001537 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001538 Types.push_back(tot);
1539 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001540}
1541
Chris Lattner4b009652007-07-25 00:24:17 +00001542/// getTagDeclType - Return the unique reference to the type for the
1543/// specified TagDecl (struct/union/class/enum) decl.
1544QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001545 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001546 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001547}
1548
1549/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1550/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1551/// needs to agree with the definition in <stddef.h>.
1552QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001553 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001554}
1555
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001556/// getSignedWCharType - Return the type of "signed wchar_t".
1557/// Used when in C++, as a GCC extension.
1558QualType ASTContext::getSignedWCharType() const {
1559 // FIXME: derive from "Target" ?
1560 return WCharTy;
1561}
1562
1563/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1564/// Used when in C++, as a GCC extension.
1565QualType ASTContext::getUnsignedWCharType() const {
1566 // FIXME: derive from "Target" ?
1567 return UnsignedIntTy;
1568}
1569
Chris Lattner4b009652007-07-25 00:24:17 +00001570/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1571/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1572QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001573 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001574}
1575
Chris Lattner19eb97e2008-04-02 05:18:44 +00001576//===----------------------------------------------------------------------===//
1577// Type Operators
1578//===----------------------------------------------------------------------===//
1579
Chris Lattner3dae6f42008-04-06 22:41:35 +00001580/// getCanonicalType - Return the canonical (structural) type corresponding to
1581/// the specified potentially non-canonical type. The non-canonical version
1582/// of a type may have many "decorated" versions of types. Decorators can
1583/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1584/// to be free of any of these, allowing two canonical types to be compared
1585/// for exact equality with a simple pointer comparison.
1586QualType ASTContext::getCanonicalType(QualType T) {
1587 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001588
1589 // If the result has type qualifiers, make sure to canonicalize them as well.
1590 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1591 if (TypeQuals == 0) return CanType;
1592
1593 // If the type qualifiers are on an array type, get the canonical type of the
1594 // array with the qualifiers applied to the element type.
1595 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1596 if (!AT)
1597 return CanType.getQualifiedType(TypeQuals);
1598
1599 // Get the canonical version of the element with the extra qualifiers on it.
1600 // This can recursively sink qualifiers through multiple levels of arrays.
1601 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1602 NewEltTy = getCanonicalType(NewEltTy);
1603
1604 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1605 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1606 CAT->getIndexTypeQualifier());
1607 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1608 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1609 IAT->getIndexTypeQualifier());
1610
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001611 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1612 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1613 DSAT->getSizeModifier(),
1614 DSAT->getIndexTypeQualifier());
1615
Chris Lattnera1923f62008-08-04 07:31:14 +00001616 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1617 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1618 VAT->getSizeModifier(),
1619 VAT->getIndexTypeQualifier());
1620}
1621
Douglas Gregord3022602009-03-27 23:10:48 +00001622NestedNameSpecifier *
1623ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1624 if (!NNS)
1625 return 0;
1626
1627 switch (NNS->getKind()) {
1628 case NestedNameSpecifier::Identifier:
1629 // Canonicalize the prefix but keep the identifier the same.
1630 return NestedNameSpecifier::Create(*this,
1631 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1632 NNS->getAsIdentifier());
1633
1634 case NestedNameSpecifier::Namespace:
1635 // A namespace is canonical; build a nested-name-specifier with
1636 // this namespace and no prefix.
1637 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1638
1639 case NestedNameSpecifier::TypeSpec:
1640 case NestedNameSpecifier::TypeSpecWithTemplate: {
1641 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1642 NestedNameSpecifier *Prefix = 0;
1643
1644 // FIXME: This isn't the right check!
1645 if (T->isDependentType())
1646 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1647
1648 return NestedNameSpecifier::Create(*this, Prefix,
1649 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1650 T.getTypePtr());
1651 }
1652
1653 case NestedNameSpecifier::Global:
1654 // The global specifier is canonical and unique.
1655 return NNS;
1656 }
1657
1658 // Required to silence a GCC warning
1659 return 0;
1660}
1661
Chris Lattnera1923f62008-08-04 07:31:14 +00001662
1663const ArrayType *ASTContext::getAsArrayType(QualType T) {
1664 // Handle the non-qualified case efficiently.
1665 if (T.getCVRQualifiers() == 0) {
1666 // Handle the common positive case fast.
1667 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1668 return AT;
1669 }
1670
1671 // Handle the common negative case fast, ignoring CVR qualifiers.
1672 QualType CType = T->getCanonicalTypeInternal();
1673
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001674 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001675 // test.
1676 if (!isa<ArrayType>(CType) &&
1677 !isa<ArrayType>(CType.getUnqualifiedType()))
1678 return 0;
1679
1680 // Apply any CVR qualifiers from the array type to the element type. This
1681 // implements C99 6.7.3p8: "If the specification of an array type includes
1682 // any type qualifiers, the element type is so qualified, not the array type."
1683
1684 // If we get here, we either have type qualifiers on the type, or we have
1685 // sugar such as a typedef in the way. If we have type qualifiers on the type
1686 // we must propagate them down into the elemeng type.
1687 unsigned CVRQuals = T.getCVRQualifiers();
1688 unsigned AddrSpace = 0;
1689 Type *Ty = T.getTypePtr();
1690
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001691 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001692 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001693 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1694 AddrSpace = EXTQT->getAddressSpace();
1695 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001696 } else {
1697 T = Ty->getDesugaredType();
1698 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1699 break;
1700 CVRQuals |= T.getCVRQualifiers();
1701 Ty = T.getTypePtr();
1702 }
1703 }
1704
1705 // If we have a simple case, just return now.
1706 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1707 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1708 return ATy;
1709
1710 // Otherwise, we have an array and we have qualifiers on it. Push the
1711 // qualifiers into the array element type and return a new array type.
1712 // Get the canonical version of the element with the extra qualifiers on it.
1713 // This can recursively sink qualifiers through multiple levels of arrays.
1714 QualType NewEltTy = ATy->getElementType();
1715 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001716 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001717 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1718
1719 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1720 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1721 CAT->getSizeModifier(),
1722 CAT->getIndexTypeQualifier()));
1723 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1724 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1725 IAT->getSizeModifier(),
1726 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001727
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001728 if (const DependentSizedArrayType *DSAT
1729 = dyn_cast<DependentSizedArrayType>(ATy))
1730 return cast<ArrayType>(
1731 getDependentSizedArrayType(NewEltTy,
1732 DSAT->getSizeExpr(),
1733 DSAT->getSizeModifier(),
1734 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001735
Chris Lattnera1923f62008-08-04 07:31:14 +00001736 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1737 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1738 VAT->getSizeModifier(),
1739 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001740}
1741
1742
Chris Lattner19eb97e2008-04-02 05:18:44 +00001743/// getArrayDecayedType - Return the properly qualified result of decaying the
1744/// specified array type to a pointer. This operation is non-trivial when
1745/// handling typedefs etc. The canonical type of "T" must be an array type,
1746/// this returns a pointer to a properly qualified element of the array.
1747///
1748/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1749QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001750 // Get the element type with 'getAsArrayType' so that we don't lose any
1751 // typedefs in the element type of the array. This also handles propagation
1752 // of type qualifiers from the array type into the element type if present
1753 // (C99 6.7.3p8).
1754 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1755 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001756
Chris Lattnera1923f62008-08-04 07:31:14 +00001757 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001758
1759 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001760 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001761}
1762
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001763QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001764 QualType ElemTy = VAT->getElementType();
1765
1766 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1767 return getBaseElementType(VAT);
1768
1769 return ElemTy;
1770}
1771
Chris Lattner4b009652007-07-25 00:24:17 +00001772/// getFloatingRank - Return a relative rank for floating point types.
1773/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001774static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001775 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001776 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001777
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001778 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001779 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001780 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001781 case BuiltinType::Float: return FloatRank;
1782 case BuiltinType::Double: return DoubleRank;
1783 case BuiltinType::LongDouble: return LongDoubleRank;
1784 }
1785}
1786
Steve Narofffa0c4532007-08-27 01:41:48 +00001787/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1788/// point or a complex type (based on typeDomain/typeSize).
1789/// 'typeDomain' is a real floating point or complex type.
1790/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001791QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1792 QualType Domain) const {
1793 FloatingRank EltRank = getFloatingRank(Size);
1794 if (Domain->isComplexType()) {
1795 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001796 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001797 case FloatRank: return FloatComplexTy;
1798 case DoubleRank: return DoubleComplexTy;
1799 case LongDoubleRank: return LongDoubleComplexTy;
1800 }
Chris Lattner4b009652007-07-25 00:24:17 +00001801 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001802
1803 assert(Domain->isRealFloatingType() && "Unknown domain!");
1804 switch (EltRank) {
1805 default: assert(0 && "getFloatingRank(): illegal value for rank");
1806 case FloatRank: return FloatTy;
1807 case DoubleRank: return DoubleTy;
1808 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001809 }
Chris Lattner4b009652007-07-25 00:24:17 +00001810}
1811
Chris Lattner51285d82008-04-06 23:55:33 +00001812/// getFloatingTypeOrder - Compare the rank of the two specified floating
1813/// point types, ignoring the domain of the type (i.e. 'double' ==
1814/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1815/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001816int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1817 FloatingRank LHSR = getFloatingRank(LHS);
1818 FloatingRank RHSR = getFloatingRank(RHS);
1819
1820 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001821 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001822 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001823 return 1;
1824 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001825}
1826
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001827/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1828/// routine will assert if passed a built-in type that isn't an integer or enum,
1829/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001830unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001831 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001832 if (EnumType* ET = dyn_cast<EnumType>(T))
1833 T = ET->getDecl()->getIntegerType().getTypePtr();
1834
1835 // There are two things which impact the integer rank: the width, and
1836 // the ordering of builtins. The builtin ordering is encoded in the
1837 // bottom three bits; the width is encoded in the bits above that.
1838 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1839 return FWIT->getWidth() << 3;
1840 }
1841
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001842 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001843 default: assert(0 && "getIntegerRank(): not a built-in integer");
1844 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001845 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001846 case BuiltinType::Char_S:
1847 case BuiltinType::Char_U:
1848 case BuiltinType::SChar:
1849 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001850 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001851 case BuiltinType::Short:
1852 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001853 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001854 case BuiltinType::Int:
1855 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001856 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001857 case BuiltinType::Long:
1858 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001859 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001860 case BuiltinType::LongLong:
1861 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001862 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001863 }
1864}
1865
Chris Lattner51285d82008-04-06 23:55:33 +00001866/// getIntegerTypeOrder - Returns the highest ranked integer type:
1867/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1868/// LHS < RHS, return -1.
1869int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001870 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1871 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001872 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001873
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001874 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1875 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001876
Chris Lattner51285d82008-04-06 23:55:33 +00001877 unsigned LHSRank = getIntegerRank(LHSC);
1878 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001879
Chris Lattner51285d82008-04-06 23:55:33 +00001880 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1881 if (LHSRank == RHSRank) return 0;
1882 return LHSRank > RHSRank ? 1 : -1;
1883 }
Chris Lattner4b009652007-07-25 00:24:17 +00001884
Chris Lattner51285d82008-04-06 23:55:33 +00001885 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1886 if (LHSUnsigned) {
1887 // If the unsigned [LHS] type is larger, return it.
1888 if (LHSRank >= RHSRank)
1889 return 1;
1890
1891 // If the signed type can represent all values of the unsigned type, it
1892 // wins. Because we are dealing with 2's complement and types that are
1893 // powers of two larger than each other, this is always safe.
1894 return -1;
1895 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001896
Chris Lattner51285d82008-04-06 23:55:33 +00001897 // If the unsigned [RHS] type is larger, return it.
1898 if (RHSRank >= LHSRank)
1899 return -1;
1900
1901 // If the signed type can represent all values of the unsigned type, it
1902 // wins. Because we are dealing with 2's complement and types that are
1903 // powers of two larger than each other, this is always safe.
1904 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001905}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001906
1907// getCFConstantStringType - Return the type used for constant CFStrings.
1908QualType ASTContext::getCFConstantStringType() {
1909 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001910 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001911 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001912 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001913 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001914
1915 // const int *isa;
1916 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001917 // int flags;
1918 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001919 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001920 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001921 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001922 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001923
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001924 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001925 for (unsigned i = 0; i < 4; ++i) {
1926 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1927 SourceLocation(), 0,
1928 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001929 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001930 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001931 }
1932
1933 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001934 }
1935
1936 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001937}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001938
Anders Carlssonf58cac72008-08-30 19:34:46 +00001939QualType ASTContext::getObjCFastEnumerationStateType()
1940{
1941 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001942 ObjCFastEnumerationStateTypeDecl =
1943 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1944 &Idents.get("__objcFastEnumerationState"));
1945
Anders Carlssonf58cac72008-08-30 19:34:46 +00001946 QualType FieldTypes[] = {
1947 UnsignedLongTy,
1948 getPointerType(ObjCIdType),
1949 getPointerType(UnsignedLongTy),
1950 getConstantArrayType(UnsignedLongTy,
1951 llvm::APInt(32, 5), ArrayType::Normal, 0)
1952 };
1953
Douglas Gregor8acb7272008-12-11 16:49:14 +00001954 for (size_t i = 0; i < 4; ++i) {
1955 FieldDecl *Field = FieldDecl::Create(*this,
1956 ObjCFastEnumerationStateTypeDecl,
1957 SourceLocation(), 0,
1958 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001959 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001960 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001961 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00001962
Douglas Gregor8acb7272008-12-11 16:49:14 +00001963 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001964 }
1965
1966 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1967}
1968
Anders Carlssone3f02572007-10-29 06:33:42 +00001969// This returns true if a type has been typedefed to BOOL:
1970// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001971static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001972 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00001973 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1974 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001975
1976 return false;
1977}
1978
Ted Kremenek42730c52008-01-07 19:49:32 +00001979/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001980/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001981int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001982 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001983
1984 // Make all integer and enum types at least as large as an int
1985 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001986 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001987 // Treat arrays as pointers, since that's how they're passed in.
1988 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001989 sz = getTypeSize(VoidPtrTy);
1990 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001991}
1992
Ted Kremenek42730c52008-01-07 19:49:32 +00001993/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001994/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001995void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00001996 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001997 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001998 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001999 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002000 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002001 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002002 // Compute size of all parameters.
2003 // Start with computing size of a pointer in number of bytes.
2004 // FIXME: There might(should) be a better way of doing this computation!
2005 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00002006 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002007 // The first two arguments (self and _cmd) are pointers; account for
2008 // their size.
2009 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002010 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2011 E = Decl->param_end(); PI != E; ++PI) {
2012 QualType PType = (*PI)->getType();
2013 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00002014 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002015 ParmOffset += sz;
2016 }
2017 S += llvm::utostr(ParmOffset);
2018 S += "@0:";
2019 S += llvm::utostr(PtrSize);
2020
2021 // Argument types.
2022 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002023 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2024 E = Decl->param_end(); PI != E; ++PI) {
2025 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002026 QualType PType = PVDecl->getOriginalType();
2027 if (const ArrayType *AT =
2028 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
2029 // Use array's original type only if it has known number of
2030 // elements.
2031 if (!dyn_cast<ConstantArrayType>(AT))
2032 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002033 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002034 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002035 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002036 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002037 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00002038 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002039 }
2040}
2041
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002042/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002043/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002044/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2045/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002046/// Property attributes are stored as a comma-delimited C string. The simple
2047/// attributes readonly and bycopy are encoded as single characters. The
2048/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2049/// encoded as single characters, followed by an identifier. Property types
2050/// are also encoded as a parametrized attribute. The characters used to encode
2051/// these attributes are defined by the following enumeration:
2052/// @code
2053/// enum PropertyAttributes {
2054/// kPropertyReadOnly = 'R', // property is read-only.
2055/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2056/// kPropertyByref = '&', // property is a reference to the value last assigned
2057/// kPropertyDynamic = 'D', // property is dynamic
2058/// kPropertyGetter = 'G', // followed by getter selector name
2059/// kPropertySetter = 'S', // followed by setter selector name
2060/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2061/// kPropertyType = 't' // followed by old-style type encoding.
2062/// kPropertyWeak = 'W' // 'weak' property
2063/// kPropertyStrong = 'P' // property GC'able
2064/// kPropertyNonAtomic = 'N' // property non-atomic
2065/// };
2066/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002067void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2068 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00002069 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002070 // Collect information from the property implementation decl(s).
2071 bool Dynamic = false;
2072 ObjCPropertyImplDecl *SynthesizePID = 0;
2073
2074 // FIXME: Duplicated code due to poor abstraction.
2075 if (Container) {
2076 if (const ObjCCategoryImplDecl *CID =
2077 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2078 for (ObjCCategoryImplDecl::propimpl_iterator
2079 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2080 ObjCPropertyImplDecl *PID = *i;
2081 if (PID->getPropertyDecl() == PD) {
2082 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2083 Dynamic = true;
2084 } else {
2085 SynthesizePID = PID;
2086 }
2087 }
2088 }
2089 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002090 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002091 for (ObjCCategoryImplDecl::propimpl_iterator
2092 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2093 ObjCPropertyImplDecl *PID = *i;
2094 if (PID->getPropertyDecl() == PD) {
2095 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2096 Dynamic = true;
2097 } else {
2098 SynthesizePID = PID;
2099 }
2100 }
2101 }
2102 }
2103 }
2104
2105 // FIXME: This is not very efficient.
2106 S = "T";
2107
2108 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002109 // GCC has some special rules regarding encoding of properties which
2110 // closely resembles encoding of ivars.
2111 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
2112 true /* outermost type */,
2113 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002114
2115 if (PD->isReadOnly()) {
2116 S += ",R";
2117 } else {
2118 switch (PD->getSetterKind()) {
2119 case ObjCPropertyDecl::Assign: break;
2120 case ObjCPropertyDecl::Copy: S += ",C"; break;
2121 case ObjCPropertyDecl::Retain: S += ",&"; break;
2122 }
2123 }
2124
2125 // It really isn't clear at all what this means, since properties
2126 // are "dynamic by default".
2127 if (Dynamic)
2128 S += ",D";
2129
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002130 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2131 S += ",N";
2132
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002133 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2134 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002135 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002136 }
2137
2138 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2139 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002140 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002141 }
2142
2143 if (SynthesizePID) {
2144 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2145 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002146 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002147 }
2148
2149 // FIXME: OBJCGC: weak & strong
2150}
2151
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002152/// getLegacyIntegralTypeEncoding -
2153/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002154/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002155/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2156///
2157void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2158 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2159 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002160 if (BT->getKind() == BuiltinType::ULong &&
2161 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002162 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002163 else
2164 if (BT->getKind() == BuiltinType::Long &&
2165 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002166 PointeeTy = IntTy;
2167 }
2168 }
2169}
2170
Fariborz Jahanian248db262008-01-22 22:44:46 +00002171void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002172 FieldDecl *Field) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002173 // We follow the behavior of gcc, expanding structures which are
2174 // directly pointed to, and expanding embedded structures. Note that
2175 // these rules are sufficient to prevent recursive encoding of the
2176 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002177 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2178 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002179}
2180
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002181static void EncodeBitField(const ASTContext *Context, std::string& S,
2182 FieldDecl *FD) {
2183 const Expr *E = FD->getBitWidth();
2184 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2185 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2186 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2187 S += 'b';
2188 S += llvm::utostr(N);
2189}
2190
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002191void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2192 bool ExpandPointedToStructures,
2193 bool ExpandStructures,
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002194 FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002195 bool OutermostType,
2196 bool EncodingProperty) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00002197 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002198 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002199 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002200 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002201 else {
2202 char encoding;
2203 switch (BT->getKind()) {
2204 default: assert(0 && "Unhandled builtin type kind");
2205 case BuiltinType::Void: encoding = 'v'; break;
2206 case BuiltinType::Bool: encoding = 'B'; break;
2207 case BuiltinType::Char_U:
2208 case BuiltinType::UChar: encoding = 'C'; break;
2209 case BuiltinType::UShort: encoding = 'S'; break;
2210 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002211 case BuiltinType::ULong:
2212 encoding =
2213 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2214 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002215 case BuiltinType::ULongLong: encoding = 'Q'; break;
2216 case BuiltinType::Char_S:
2217 case BuiltinType::SChar: encoding = 'c'; break;
2218 case BuiltinType::Short: encoding = 's'; break;
2219 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002220 case BuiltinType::Long:
2221 encoding =
2222 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2223 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002224 case BuiltinType::LongLong: encoding = 'q'; break;
2225 case BuiltinType::Float: encoding = 'f'; break;
2226 case BuiltinType::Double: encoding = 'd'; break;
2227 case BuiltinType::LongDouble: encoding = 'd'; break;
2228 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002229
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002230 S += encoding;
2231 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002232 }
Ted Kremenek42730c52008-01-07 19:49:32 +00002233 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002234 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2235 ExpandPointedToStructures,
2236 ExpandStructures, FD);
2237 if (FD || EncodingProperty) {
2238 // Note that we do extended encoding of protocol qualifer list
2239 // Only when doing ivar or property encoding.
2240 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2241 S += '"';
2242 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2243 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2244 S += '<';
2245 S += Proto->getNameAsString();
2246 S += '>';
2247 }
2248 S += '"';
2249 }
2250 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002251 }
2252 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002253 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002254 bool isReadOnly = false;
2255 // For historical/compatibility reasons, the read-only qualifier of the
2256 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2257 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2258 // Also, do not emit the 'r' for anything but the outermost type!
2259 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2260 if (OutermostType && T.isConstQualified()) {
2261 isReadOnly = true;
2262 S += 'r';
2263 }
2264 }
2265 else if (OutermostType) {
2266 QualType P = PointeeTy;
2267 while (P->getAsPointerType())
2268 P = P->getAsPointerType()->getPointeeType();
2269 if (P.isConstQualified()) {
2270 isReadOnly = true;
2271 S += 'r';
2272 }
2273 }
2274 if (isReadOnly) {
2275 // Another legacy compatibility encoding. Some ObjC qualifier and type
2276 // combinations need to be rearranged.
2277 // Rewrite "in const" from "nr" to "rn"
2278 const char * s = S.c_str();
2279 int len = S.length();
2280 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2281 std::string replace = "rn";
2282 S.replace(S.end()-2, S.end(), replace);
2283 }
2284 }
Steve Naroff17c03822009-02-12 17:52:19 +00002285 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002286 S += '@';
2287 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002288 }
2289 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002290 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002291 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002292 // Another historical/compatibility reason.
2293 // We encode the underlying type which comes out as
2294 // {...};
2295 S += '^';
2296 getObjCEncodingForTypeImpl(PointeeTy, S,
2297 false, ExpandPointedToStructures,
2298 NULL);
2299 return;
2300 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002301 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002302 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002303 const ObjCInterfaceType *OIT =
2304 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002305 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002306 S += '"';
2307 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002308 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2309 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2310 S += '<';
2311 S += Proto->getNameAsString();
2312 S += '>';
2313 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002314 S += '"';
2315 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002316 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002317 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002318 S += '#';
2319 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002320 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002321 S += ':';
2322 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002323 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002324
2325 if (PointeeTy->isCharType()) {
2326 // char pointer types should be encoded as '*' unless it is a
2327 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002328 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002329 S += '*';
2330 return;
2331 }
2332 }
2333
2334 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002335 getLegacyIntegralTypeEncoding(PointeeTy);
2336
2337 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002338 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002339 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002340 } else if (const ArrayType *AT =
2341 // Ignore type qualifiers etc.
2342 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002343 if (isa<IncompleteArrayType>(AT)) {
2344 // Incomplete arrays are encoded as a pointer to the array element.
2345 S += '^';
2346
2347 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2348 false, ExpandStructures, FD);
2349 } else {
2350 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002351
Anders Carlsson858c64d2009-02-22 01:38:57 +00002352 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2353 S += llvm::utostr(CAT->getSize().getZExtValue());
2354 else {
2355 //Variable length arrays are encoded as a regular array with 0 elements.
2356 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2357 S += '0';
2358 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002359
Anders Carlsson858c64d2009-02-22 01:38:57 +00002360 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2361 false, ExpandStructures, FD);
2362 S += ']';
2363 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002364 } else if (T->getAsFunctionType()) {
2365 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002366 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002367 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002368 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002369 // Anonymous structures print as '?'
2370 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2371 S += II->getName();
2372 } else {
2373 S += '?';
2374 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002375 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002376 S += '=';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002377 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2378 FieldEnd = RDecl->field_end();
2379 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002380 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002381 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002382 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002383 S += '"';
2384 }
2385
2386 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002387 if (Field->isBitField()) {
2388 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2389 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002390 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002391 QualType qt = Field->getType();
2392 getLegacyIntegralTypeEncoding(qt);
2393 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002394 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002395 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002396 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002397 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002398 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002399 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002400 if (FD && FD->isBitField())
2401 EncodeBitField(this, S, FD);
2402 else
2403 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002404 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002405 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002406 } else if (T->isObjCInterfaceType()) {
2407 // @encode(class_name)
2408 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2409 S += '{';
2410 const IdentifierInfo *II = OI->getIdentifier();
2411 S += II->getName();
2412 S += '=';
Chris Lattner9329cf52009-03-31 08:48:01 +00002413 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002414 CollectObjCIvars(OI, RecFields);
Chris Lattner9329cf52009-03-31 08:48:01 +00002415 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002416 if (RecFields[i]->isBitField())
2417 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2418 RecFields[i]);
2419 else
2420 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2421 FD);
2422 }
2423 S += '}';
2424 }
2425 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002426 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002427}
2428
Ted Kremenek42730c52008-01-07 19:49:32 +00002429void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002430 std::string& S) const {
2431 if (QT & Decl::OBJC_TQ_In)
2432 S += 'n';
2433 if (QT & Decl::OBJC_TQ_Inout)
2434 S += 'N';
2435 if (QT & Decl::OBJC_TQ_Out)
2436 S += 'o';
2437 if (QT & Decl::OBJC_TQ_Bycopy)
2438 S += 'O';
2439 if (QT & Decl::OBJC_TQ_Byref)
2440 S += 'R';
2441 if (QT & Decl::OBJC_TQ_Oneway)
2442 S += 'V';
2443}
2444
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002445void ASTContext::setBuiltinVaListType(QualType T)
2446{
2447 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2448
2449 BuiltinVaListType = T;
2450}
2451
Ted Kremenek42730c52008-01-07 19:49:32 +00002452void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002453{
Ted Kremenek42730c52008-01-07 19:49:32 +00002454 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002455
2456 // typedef struct objc_object *id;
2457 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002458 // User error - caller will issue diagnostics.
2459 if (!ptr)
2460 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002461 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002462 // User error - caller will issue diagnostics.
2463 if (!rec)
2464 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002465 IdStructType = rec;
2466}
2467
Ted Kremenek42730c52008-01-07 19:49:32 +00002468void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002469{
Ted Kremenek42730c52008-01-07 19:49:32 +00002470 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002471
2472 // typedef struct objc_selector *SEL;
2473 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002474 if (!ptr)
2475 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002476 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002477 if (!rec)
2478 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002479 SelStructType = rec;
2480}
2481
Ted Kremenek42730c52008-01-07 19:49:32 +00002482void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002483{
Ted Kremenek42730c52008-01-07 19:49:32 +00002484 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002485}
2486
Ted Kremenek42730c52008-01-07 19:49:32 +00002487void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002488{
Ted Kremenek42730c52008-01-07 19:49:32 +00002489 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002490
2491 // typedef struct objc_class *Class;
2492 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2493 assert(ptr && "'Class' incorrectly typed");
2494 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2495 assert(rec && "'Class' incorrectly typed");
2496 ClassStructType = rec;
2497}
2498
Ted Kremenek42730c52008-01-07 19:49:32 +00002499void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2500 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002501 "'NSConstantString' type already set!");
2502
Ted Kremenek42730c52008-01-07 19:49:32 +00002503 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002504}
2505
Douglas Gregordd13e842009-03-30 22:58:21 +00002506/// \brief Retrieve the template name that represents a qualified
2507/// template name such as \c std::vector.
2508TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2509 bool TemplateKeyword,
2510 TemplateDecl *Template) {
2511 llvm::FoldingSetNodeID ID;
2512 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2513
2514 void *InsertPos = 0;
2515 QualifiedTemplateName *QTN =
2516 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2517 if (!QTN) {
2518 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2519 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2520 }
2521
2522 return TemplateName(QTN);
2523}
2524
2525/// \brief Retrieve the template name that represents a dependent
2526/// template name such as \c MetaFun::template apply.
2527TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2528 const IdentifierInfo *Name) {
2529 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2530
2531 llvm::FoldingSetNodeID ID;
2532 DependentTemplateName::Profile(ID, NNS, Name);
2533
2534 void *InsertPos = 0;
2535 DependentTemplateName *QTN =
2536 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2537
2538 if (QTN)
2539 return TemplateName(QTN);
2540
2541 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2542 if (CanonNNS == NNS) {
2543 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2544 } else {
2545 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2546 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2547 }
2548
2549 DependentTemplateNames.InsertNode(QTN, InsertPos);
2550 return TemplateName(QTN);
2551}
2552
Douglas Gregorc6507e42008-11-03 14:12:49 +00002553/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002554/// TargetInfo, produce the corresponding type. The unsigned @p Type
2555/// is actually a value of type @c TargetInfo::IntType.
2556QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002557 switch (Type) {
2558 case TargetInfo::NoInt: return QualType();
2559 case TargetInfo::SignedShort: return ShortTy;
2560 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2561 case TargetInfo::SignedInt: return IntTy;
2562 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2563 case TargetInfo::SignedLong: return LongTy;
2564 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2565 case TargetInfo::SignedLongLong: return LongLongTy;
2566 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2567 }
2568
2569 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002570 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002571}
Ted Kremenek118930e2008-07-24 23:58:27 +00002572
2573//===----------------------------------------------------------------------===//
2574// Type Predicates.
2575//===----------------------------------------------------------------------===//
2576
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002577/// isObjCNSObjectType - Return true if this is an NSObject object using
2578/// NSObject attribute on a c-style pointer type.
2579/// FIXME - Make it work directly on types.
2580///
2581bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2582 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2583 if (TypedefDecl *TD = TDT->getDecl())
2584 if (TD->getAttr<ObjCNSObjectAttr>())
2585 return true;
2586 }
2587 return false;
2588}
2589
Ted Kremenek118930e2008-07-24 23:58:27 +00002590/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2591/// to an object type. This includes "id" and "Class" (two 'special' pointers
2592/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2593/// ID type).
2594bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroff6805fc42009-02-23 18:36:16 +00002595 if (Ty->isObjCQualifiedIdType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002596 return true;
2597
Steve Naroffd9e00802008-10-21 18:24:04 +00002598 // Blocks are objects.
2599 if (Ty->isBlockPointerType())
2600 return true;
2601
2602 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00002603 if (!Ty->isPointerType())
2604 return false;
2605
2606 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2607 // pointer types. This looks for the typedef specifically, not for the
2608 // underlying type.
Eli Friedman9c2b33f2009-03-22 23:00:19 +00002609 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2610 Ty.getUnqualifiedType() == getObjCClassType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002611 return true;
2612
2613 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002614 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2615 return true;
2616
2617 // If is has NSObject attribute, OK as well.
2618 return isObjCNSObjectType(Ty);
Ted Kremenek118930e2008-07-24 23:58:27 +00002619}
2620
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002621/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2622/// garbage collection attribute.
2623///
2624QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002625 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002626 if (getLangOptions().ObjC1 &&
2627 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002628 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002629 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002630 // (or pointers to them) be treated as though they were declared
2631 // as __strong.
2632 if (GCAttrs == QualType::GCNone) {
2633 if (isObjCObjectPointerType(Ty))
2634 GCAttrs = QualType::Strong;
2635 else if (Ty->isPointerType())
2636 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2637 }
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002638 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002639 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002640}
2641
Chris Lattner6ff358b2008-04-07 06:51:04 +00002642//===----------------------------------------------------------------------===//
2643// Type Compatibility Testing
2644//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002645
Steve Naroff3454b6c2008-09-04 15:10:53 +00002646/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002647/// block types. Types must be strictly compatible here. For example,
2648/// C unfortunately doesn't produce an error for the following:
2649///
2650/// int (*emptyArgFunc)();
2651/// int (*intArgList)(int) = emptyArgFunc;
2652///
2653/// For blocks, we will produce an error for the following (similar to C++):
2654///
2655/// int (^emptyArgBlock)();
2656/// int (^intArgBlock)(int) = emptyArgBlock;
2657///
2658/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2659///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002660bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002661 const FunctionType *lbase = lhs->getAsFunctionType();
2662 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002663 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2664 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002665 if (lproto && rproto)
2666 return !mergeTypes(lhs, rhs).isNull();
2667 return false;
Steve Naroff3454b6c2008-09-04 15:10:53 +00002668}
2669
Chris Lattner6ff358b2008-04-07 06:51:04 +00002670/// areCompatVectorTypes - Return true if the two specified vector types are
2671/// compatible.
2672static bool areCompatVectorTypes(const VectorType *LHS,
2673 const VectorType *RHS) {
2674 assert(LHS->isCanonical() && RHS->isCanonical());
2675 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002676 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002677}
2678
Eli Friedman0d9549b2008-08-22 00:56:42 +00002679/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002680/// compatible for assignment from RHS to LHS. This handles validation of any
2681/// protocol qualifiers on the LHS or RHS.
2682///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002683bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2684 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002685 // Verify that the base decls are compatible: the RHS must be a subclass of
2686 // the LHS.
2687 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2688 return false;
2689
2690 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2691 // protocol qualified at all, then we are good.
2692 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2693 return true;
2694
2695 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2696 // isn't a superset.
2697 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2698 return true; // FIXME: should return false!
2699
2700 // Finally, we must have two protocol-qualified interfaces.
2701 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2702 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ff358b2008-04-07 06:51:04 +00002703
Steve Naroff98e71b82009-03-01 16:12:44 +00002704 // All LHS protocols must have a presence on the RHS.
2705 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ff358b2008-04-07 06:51:04 +00002706
Steve Naroff98e71b82009-03-01 16:12:44 +00002707 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2708 LHSPE = LHSP->qual_end();
2709 LHSPI != LHSPE; LHSPI++) {
2710 bool RHSImplementsProtocol = false;
2711
2712 // If the RHS doesn't implement the protocol on the left, the types
2713 // are incompatible.
2714 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2715 RHSPE = RHSP->qual_end();
2716 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2717 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2718 RHSImplementsProtocol = true;
2719 }
2720 // FIXME: For better diagnostics, consider passing back the protocol name.
2721 if (!RHSImplementsProtocol)
2722 return false;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002723 }
Steve Naroff98e71b82009-03-01 16:12:44 +00002724 // The RHS implements all protocols listed on the LHS.
2725 return true;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002726}
2727
Steve Naroff17c03822009-02-12 17:52:19 +00002728bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2729 // get the "pointed to" types
2730 const PointerType *LHSPT = LHS->getAsPointerType();
2731 const PointerType *RHSPT = RHS->getAsPointerType();
2732
2733 if (!LHSPT || !RHSPT)
2734 return false;
2735
2736 QualType lhptee = LHSPT->getPointeeType();
2737 QualType rhptee = RHSPT->getPointeeType();
2738 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2739 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2740 // ID acts sort of like void* for ObjC interfaces
2741 if (LHSIface && isObjCIdStructType(rhptee))
2742 return true;
2743 if (RHSIface && isObjCIdStructType(lhptee))
2744 return true;
2745 if (!LHSIface || !RHSIface)
2746 return false;
2747 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2748 canAssignObjCInterfaces(RHSIface, LHSIface);
2749}
2750
Steve Naroff85f0dc52007-10-15 20:41:53 +00002751/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2752/// both shall have the identically qualified version of a compatible type.
2753/// C99 6.2.7p1: Two types have compatible types if their types are the
2754/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002755bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2756 return !mergeTypes(LHS, RHS).isNull();
2757}
2758
2759QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2760 const FunctionType *lbase = lhs->getAsFunctionType();
2761 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002762 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2763 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002764 bool allLTypes = true;
2765 bool allRTypes = true;
2766
2767 // Check return type
2768 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2769 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002770 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2771 allLTypes = false;
2772 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2773 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002774
2775 if (lproto && rproto) { // two C99 style function prototypes
2776 unsigned lproto_nargs = lproto->getNumArgs();
2777 unsigned rproto_nargs = rproto->getNumArgs();
2778
2779 // Compatible functions must have the same number of arguments
2780 if (lproto_nargs != rproto_nargs)
2781 return QualType();
2782
2783 // Variadic and non-variadic functions aren't compatible
2784 if (lproto->isVariadic() != rproto->isVariadic())
2785 return QualType();
2786
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002787 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2788 return QualType();
2789
Eli Friedman0d9549b2008-08-22 00:56:42 +00002790 // Check argument compatibility
2791 llvm::SmallVector<QualType, 10> types;
2792 for (unsigned i = 0; i < lproto_nargs; i++) {
2793 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2794 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2795 QualType argtype = mergeTypes(largtype, rargtype);
2796 if (argtype.isNull()) return QualType();
2797 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002798 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2799 allLTypes = false;
2800 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2801 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002802 }
2803 if (allLTypes) return lhs;
2804 if (allRTypes) return rhs;
2805 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002806 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002807 }
2808
2809 if (lproto) allRTypes = false;
2810 if (rproto) allLTypes = false;
2811
Douglas Gregor4fa58902009-02-26 23:50:07 +00002812 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002813 if (proto) {
2814 if (proto->isVariadic()) return QualType();
2815 // Check that the types are compatible with the types that
2816 // would result from default argument promotions (C99 6.7.5.3p15).
2817 // The only types actually affected are promotable integer
2818 // types and floats, which would be passed as a different
2819 // type depending on whether the prototype is visible.
2820 unsigned proto_nargs = proto->getNumArgs();
2821 for (unsigned i = 0; i < proto_nargs; ++i) {
2822 QualType argTy = proto->getArgType(i);
2823 if (argTy->isPromotableIntegerType() ||
2824 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2825 return QualType();
2826 }
2827
2828 if (allLTypes) return lhs;
2829 if (allRTypes) return rhs;
2830 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002831 proto->getNumArgs(), lproto->isVariadic(),
2832 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002833 }
2834
2835 if (allLTypes) return lhs;
2836 if (allRTypes) return rhs;
Douglas Gregor4fa58902009-02-26 23:50:07 +00002837 return getFunctionNoProtoType(retType);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002838}
2839
2840QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002841 // C++ [expr]: If an expression initially has the type "reference to T", the
2842 // type is adjusted to "T" prior to any further analysis, the expression
2843 // designates the object or function denoted by the reference, and the
Sebastian Redlce6fff02009-03-16 23:22:08 +00002844 // expression is an lvalue unless the reference is an rvalue reference and
2845 // the expression is a function call (possibly inside parentheses).
Eli Friedman0d9549b2008-08-22 00:56:42 +00002846 // FIXME: C++ shouldn't be going through here! The rules are different
2847 // enough that they should be handled separately.
Sebastian Redlce6fff02009-03-16 23:22:08 +00002848 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2849 // shouldn't be going through here!
Eli Friedman0d9549b2008-08-22 00:56:42 +00002850 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002851 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002852 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002853 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002854
Eli Friedman0d9549b2008-08-22 00:56:42 +00002855 QualType LHSCan = getCanonicalType(LHS),
2856 RHSCan = getCanonicalType(RHS);
2857
2858 // If two types are identical, they are compatible.
2859 if (LHSCan == RHSCan)
2860 return LHS;
2861
2862 // If the qualifiers are different, the types aren't compatible
Eli Friedman94fcc9a2009-02-27 23:04:43 +00002863 // Note that we handle extended qualifiers later, in the
2864 // case for ExtQualType.
2865 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman0d9549b2008-08-22 00:56:42 +00002866 return QualType();
2867
2868 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2869 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2870
Chris Lattnerc38d4522008-01-14 05:45:46 +00002871 // We want to consider the two function types to be the same for these
2872 // comparisons, just force one to the other.
2873 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2874 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002875
2876 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002877 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2878 LHSClass = Type::ConstantArray;
2879 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2880 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002881
Nate Begemanaf6ed502008-04-18 23:10:10 +00002882 // Canonicalize ExtVector -> Vector.
2883 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2884 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002885
Chris Lattner7cdcb252008-04-07 06:38:24 +00002886 // Consider qualified interfaces and interfaces the same.
2887 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2888 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002889
Chris Lattnerb5709e22008-04-07 05:43:21 +00002890 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002891 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00002892 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2893 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2894
2895 // ID acts sort of like void* for ObjC interfaces
2896 if (LHSIface && isObjCIdStructType(RHS))
2897 return LHS;
2898 if (RHSIface && isObjCIdStructType(LHS))
2899 return RHS;
2900
Steve Naroff28ceff72008-12-10 22:14:21 +00002901 // ID is compatible with all qualified id types.
2902 if (LHS->isObjCQualifiedIdType()) {
2903 if (const PointerType *PT = RHS->getAsPointerType()) {
2904 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002905 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002906 return LHS;
2907 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2908 // Unfortunately, this API is part of Sema (which we don't have access
2909 // to. Need to refactor. The following check is insufficient, since we
2910 // need to make sure the class implements the protocol.
2911 if (pType->isObjCInterfaceType())
2912 return LHS;
2913 }
2914 }
2915 if (RHS->isObjCQualifiedIdType()) {
2916 if (const PointerType *PT = LHS->getAsPointerType()) {
2917 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002918 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002919 return RHS;
2920 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2921 // Unfortunately, this API is part of Sema (which we don't have access
2922 // to. Need to refactor. The following check is insufficient, since we
2923 // need to make sure the class implements the protocol.
2924 if (pType->isObjCInterfaceType())
2925 return RHS;
2926 }
2927 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002928 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2929 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002930 if (const EnumType* ETy = LHS->getAsEnumType()) {
2931 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2932 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002933 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002934 if (const EnumType* ETy = RHS->getAsEnumType()) {
2935 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2936 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002937 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002938
Eli Friedman0d9549b2008-08-22 00:56:42 +00002939 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002940 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002941
Steve Naroffc88babe2008-01-09 22:43:08 +00002942 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002943 switch (LHSClass) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00002944#define TYPE(Class, Base)
2945#define ABSTRACT_TYPE(Class, Base)
2946#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2947#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2948#include "clang/AST/TypeNodes.def"
2949 assert(false && "Non-canonical and dependent types shouldn't get here");
2950 return QualType();
2951
Sebastian Redlce6fff02009-03-16 23:22:08 +00002952 case Type::LValueReference:
2953 case Type::RValueReference:
Douglas Gregor4fa58902009-02-26 23:50:07 +00002954 case Type::MemberPointer:
2955 assert(false && "C++ should never be in mergeTypes");
2956 return QualType();
2957
2958 case Type::IncompleteArray:
2959 case Type::VariableArray:
2960 case Type::FunctionProto:
2961 case Type::ExtVector:
2962 case Type::ObjCQualifiedInterface:
2963 assert(false && "Types are eliminated above");
2964 return QualType();
2965
Chris Lattnerc38d4522008-01-14 05:45:46 +00002966 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002967 {
2968 // Merge two pointer types, while trying to preserve typedef info
2969 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2970 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2971 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2972 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002973 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2974 return LHS;
2975 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2976 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002977 return getPointerType(ResultType);
2978 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002979 case Type::BlockPointer:
2980 {
2981 // Merge two block pointer types, while trying to preserve typedef info
2982 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2983 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2984 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2985 if (ResultType.isNull()) return QualType();
2986 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2987 return LHS;
2988 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2989 return RHS;
2990 return getBlockPointerType(ResultType);
2991 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002992 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002993 {
2994 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2995 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2996 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2997 return QualType();
2998
2999 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3000 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3001 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3002 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003003 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3004 return LHS;
3005 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3006 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003007 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3008 ArrayType::ArraySizeModifier(), 0);
3009 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3010 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003011 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3012 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003013 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3014 return LHS;
3015 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3016 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003017 if (LVAT) {
3018 // FIXME: This isn't correct! But tricky to implement because
3019 // the array's size has to be the size of LHS, but the type
3020 // has to be different.
3021 return LHS;
3022 }
3023 if (RVAT) {
3024 // FIXME: This isn't correct! But tricky to implement because
3025 // the array's size has to be the size of RHS, but the type
3026 // has to be different.
3027 return RHS;
3028 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003029 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3030 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003031 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003032 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003033 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003034 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor4fa58902009-02-26 23:50:07 +00003035 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003036 case Type::Enum:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003037 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00003038 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3039 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003040 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00003041 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003042 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003043 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00003044 case Type::Complex:
3045 // Distinct complex types are incompatible.
3046 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003047 case Type::Vector:
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003048 // FIXME: The merged type should be an ExtVector!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003049 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3050 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003051 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003052 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003053 // Check if the interfaces are assignment compatible.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003054 // FIXME: This should be type compatibility, e.g. whether
3055 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff0bbc1352009-02-21 16:18:07 +00003056 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3057 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3058 if (LHSIface && RHSIface &&
3059 canAssignObjCInterfaces(LHSIface, RHSIface))
3060 return LHS;
3061
Eli Friedman0d9549b2008-08-22 00:56:42 +00003062 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003063 }
Steve Naroff28ceff72008-12-10 22:14:21 +00003064 case Type::ObjCQualifiedId:
3065 // Distinct qualified id's are not compatible.
3066 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003067 case Type::FixedWidthInt:
3068 // Distinct fixed-width integers are not compatible.
3069 return QualType();
3070 case Type::ObjCQualifiedClass:
3071 // Distinct qualified classes are not compatible.
3072 return QualType();
3073 case Type::ExtQual:
3074 // FIXME: ExtQual types can be compatible even if they're not
3075 // identical!
3076 return QualType();
3077 // First attempt at an implementation, but I'm not really sure it's
3078 // right...
3079#if 0
3080 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3081 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3082 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3083 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3084 return QualType();
3085 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3086 LHSBase = QualType(LQual->getBaseType(), 0);
3087 RHSBase = QualType(RQual->getBaseType(), 0);
3088 ResultType = mergeTypes(LHSBase, RHSBase);
3089 if (ResultType.isNull()) return QualType();
3090 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3091 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3092 return LHS;
3093 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3094 return RHS;
3095 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3096 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3097 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3098 return ResultType;
3099#endif
Douglas Gregordd13e842009-03-30 22:58:21 +00003100
3101 case Type::TemplateSpecialization:
3102 assert(false && "Dependent types have no size");
3103 break;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003104 }
Douglas Gregor4fa58902009-02-26 23:50:07 +00003105
3106 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003107}
Ted Kremenek738e6c02007-10-31 17:10:13 +00003108
Chris Lattner1d78a862008-04-07 07:01:58 +00003109//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00003110// Integer Predicates
3111//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00003112
Eli Friedman0832dbc2008-06-28 06:23:08 +00003113unsigned ASTContext::getIntWidth(QualType T) {
3114 if (T == BoolTy)
3115 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00003116 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3117 return FWIT->getWidth();
3118 }
3119 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00003120 return (unsigned)getTypeSize(T);
3121}
3122
3123QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3124 assert(T->isSignedIntegerType() && "Unexpected type");
3125 if (const EnumType* ETy = T->getAsEnumType())
3126 T = ETy->getDecl()->getIntegerType();
3127 const BuiltinType* BTy = T->getAsBuiltinType();
3128 assert (BTy && "Unexpected signed integer type");
3129 switch (BTy->getKind()) {
3130 case BuiltinType::Char_S:
3131 case BuiltinType::SChar:
3132 return UnsignedCharTy;
3133 case BuiltinType::Short:
3134 return UnsignedShortTy;
3135 case BuiltinType::Int:
3136 return UnsignedIntTy;
3137 case BuiltinType::Long:
3138 return UnsignedLongTy;
3139 case BuiltinType::LongLong:
3140 return UnsignedLongLongTy;
3141 default:
3142 assert(0 && "Unexpected signed integer type");
3143 return QualType();
3144 }
3145}
3146
3147
3148//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00003149// Serialization Support
3150//===----------------------------------------------------------------------===//
3151
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003152enum {
3153 BasicMetadataBlock = 1,
3154 ASTContextBlock = 2,
3155 DeclsBlock = 3
3156};
3157
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003158void ASTContext::EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const{
3159 // Create bitstream.
3160 llvm::BitstreamWriter Stream(Buffer);
3161
3162 // Emit the preamble.
3163 Stream.Emit((unsigned)'B', 8);
3164 Stream.Emit((unsigned)'C', 8);
3165 Stream.Emit(0xC, 4);
3166 Stream.Emit(0xF, 4);
3167 Stream.Emit(0xE, 4);
3168 Stream.Emit(0x0, 4);
3169
3170 // Create serializer.
3171 llvm::Serializer S(Stream);
3172
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003173 // ===---------------------------------------------------===/
3174 // Serialize the "Translation Unit" metadata.
3175 // ===---------------------------------------------------===/
3176
3177 // Emit ASTContext.
3178 S.EnterBlock(ASTContextBlock);
3179 S.EmitOwnedPtr(this);
3180 S.ExitBlock(); // exit "ASTContextBlock"
3181
3182 S.EnterBlock(BasicMetadataBlock);
3183
3184 // Block for SourceManager and Target. Allows easy skipping
3185 // around to the block for the Selectors during deserialization.
3186 S.EnterBlock();
3187
3188 // Emit the SourceManager.
3189 S.Emit(getSourceManager());
3190
3191 // Emit the Target.
3192 S.EmitPtr(&Target);
3193 S.EmitCStr(Target.getTargetTriple());
3194
3195 S.ExitBlock(); // exit "SourceManager and Target Block"
3196
3197 // Emit the Selectors.
3198 S.Emit(Selectors);
3199
3200 // Emit the Identifier Table.
3201 S.Emit(Idents);
3202
3203 S.ExitBlock(); // exit "BasicMetadataBlock"
3204}
3205
3206
Ted Kremenek738e6c02007-10-31 17:10:13 +00003207/// Emit - Serialize an ASTContext object to Bitcode.
3208void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00003209 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00003210 S.EmitRef(SourceMgr);
3211 S.EmitRef(Target);
3212 S.EmitRef(Idents);
3213 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003214
Ted Kremenek68228a92007-10-31 22:44:07 +00003215 // Emit the size of the type vector so that we can reserve that size
3216 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003217 S.EmitInt(Types.size());
3218
Ted Kremenek034a78c2007-11-13 22:02:55 +00003219 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3220 I!=E;++I)
3221 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003222
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003223 S.EmitOwnedPtr(TUDecl);
3224
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003225 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003226}
3227
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003228
3229ASTContext *ASTContext::ReadASTBitcodeBuffer(llvm::MemoryBuffer &Buffer,
3230 FileManager &FMgr) {
3231 // Check if the file is of the proper length.
3232 if (Buffer.getBufferSize() & 0x3) {
3233 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
3234 return 0;
3235 }
3236
3237 // Create the bitstream reader.
3238 unsigned char *BufPtr = (unsigned char *)Buffer.getBufferStart();
3239 llvm::BitstreamReader Stream(BufPtr, BufPtr+Buffer.getBufferSize());
3240
3241 if (Stream.Read(8) != 'B' ||
3242 Stream.Read(8) != 'C' ||
3243 Stream.Read(4) != 0xC ||
3244 Stream.Read(4) != 0xF ||
3245 Stream.Read(4) != 0xE ||
3246 Stream.Read(4) != 0x0) {
3247 // FIXME: Provide diagnostic.
3248 return NULL;
3249 }
3250
3251 // Create the deserializer.
3252 llvm::Deserializer Dezr(Stream);
3253
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003254 // ===---------------------------------------------------===/
3255 // Deserialize the "Translation Unit" metadata.
3256 // ===---------------------------------------------------===/
3257
3258 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
3259 // (which will appear earlier) and record its location.
3260
3261 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
3262 assert (FoundBlock);
3263
3264 llvm::Deserializer::Location ASTContextBlockLoc =
3265 Dezr.getCurrentBlockLocation();
3266
3267 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
3268 assert (FoundBlock);
3269
3270 // Read the SourceManager.
3271 SourceManager::CreateAndRegister(Dezr, FMgr);
3272
3273 { // Read the TargetInfo.
3274 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
3275 char* triple = Dezr.ReadCStr(NULL,0,true);
3276 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
3277 delete [] triple;
3278 }
3279
3280 // For Selectors, we must read the identifier table first because the
3281 // SelectorTable depends on the identifiers being already deserialized.
3282 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
3283 Dezr.SkipBlock();
3284
3285 // Read the identifier table.
3286 IdentifierTable::CreateAndRegister(Dezr);
3287
3288 // Now jump back and read the selectors.
3289 Dezr.JumpTo(SelectorBlkLoc);
3290 SelectorTable::CreateAndRegister(Dezr);
3291
3292 // Now jump back to ASTContextBlock and read the ASTContext.
3293 Dezr.JumpTo(ASTContextBlockLoc);
3294 return Dezr.ReadOwnedPtr<ASTContext>();
3295}
3296
Ted Kremenekacba3612007-11-13 00:25:37 +00003297ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00003298
3299 // Read the language options.
3300 LangOptions LOpts;
3301 LOpts.Read(D);
3302
Ted Kremenek68228a92007-10-31 22:44:07 +00003303 SourceManager &SM = D.ReadRef<SourceManager>();
3304 TargetInfo &t = D.ReadRef<TargetInfo>();
3305 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3306 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00003307
Ted Kremenek68228a92007-10-31 22:44:07 +00003308 unsigned size_reserve = D.ReadInt();
3309
Douglas Gregor24afd4a2008-11-17 14:58:09 +00003310 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3311 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00003312
Ted Kremenek034a78c2007-11-13 22:02:55 +00003313 for (unsigned i = 0; i < size_reserve; ++i)
3314 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00003315
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003316 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3317
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003318 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00003319
3320 return A;
3321}