blob: ac46180b94d8f97c39e07e717b28dec1f781d60c [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000019#include "clang/AST/ExternalASTSource.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000021#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000023#include "llvm/ADT/StringExtras.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000025#include "llvm/Support/MemoryBuffer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner61710852008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Douglas Gregor2deaea32009-04-22 18:49:13 +000035 bool FreeMem, unsigned size_reserve,
36 bool InitializeBuiltins) :
Douglas Gregorab452ba2009-03-26 23:50:42 +000037 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
38 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Douglas Gregor2cf26342009-04-09 22:27:44 +000039 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels),
40 ExternalSource(0) {
Daniel Dunbare91593e2008-08-11 04:54:23 +000041 if (size_reserve > 0) Types.reserve(size_reserve);
42 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
Douglas Gregor7a9cbed2009-04-26 03:57:37 +000044 BuiltinInfo.InitializeTargetBuiltins(Target);
Douglas Gregor2deaea32009-04-22 18:49:13 +000045 if (InitializeBuiltins)
46 this->InitializeBuiltins(idents);
Daniel Dunbare91593e2008-08-11 04:54:23 +000047}
48
Reid Spencer5f016e22007-07-11 17:01:13 +000049ASTContext::~ASTContext() {
50 // Deallocate all the types.
51 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000052 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000053 Types.pop_back();
54 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000055
Nuno Lopesb74668e2008-12-17 22:30:25 +000056 {
57 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
58 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
59 while (I != E) {
60 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
61 delete R;
62 }
63 }
64
65 {
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +000066 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
67 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopesb74668e2008-12-17 22:30:25 +000068 while (I != E) {
69 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
70 delete R;
71 }
72 }
73
Douglas Gregorab452ba2009-03-26 23:50:42 +000074 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000075 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
76 NNS = NestedNameSpecifiers.begin(),
77 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregore7dcd782009-03-27 23:25:45 +000078 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000079 /* Increment in loop */)
80 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000081
82 if (GlobalNestedNameSpecifier)
83 GlobalNestedNameSpecifier->Destroy(*this);
84
Eli Friedmanb26153c2008-05-27 03:08:09 +000085 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000086}
87
Douglas Gregor2deaea32009-04-22 18:49:13 +000088void ASTContext::InitializeBuiltins(IdentifierTable &idents) {
Douglas Gregor2deaea32009-04-22 18:49:13 +000089 BuiltinInfo.InitializeBuiltins(idents, LangOpts.NoBuiltin);
90}
91
Douglas Gregor2cf26342009-04-09 22:27:44 +000092void
93ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
94 ExternalSource.reset(Source.take());
95}
96
Reid Spencer5f016e22007-07-11 17:01:13 +000097void ASTContext::PrintStats() const {
98 fprintf(stderr, "*** AST Context Stats:\n");
99 fprintf(stderr, " %d types total.\n", (int)Types.size());
100 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000101 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000102 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
103 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
104
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000106 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
107 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000108 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000109 unsigned NumExtQual = 0;
110
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
112 Type *T = Types[i];
113 if (isa<BuiltinType>(T))
114 ++NumBuiltin;
115 else if (isa<PointerType>(T))
116 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000117 else if (isa<BlockPointerType>(T))
118 ++NumBlockPointer;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000119 else if (isa<LValueReferenceType>(T))
120 ++NumLValueReference;
121 else if (isa<RValueReferenceType>(T))
122 ++NumRValueReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000123 else if (isa<MemberPointerType>(T))
124 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000125 else if (isa<ComplexType>(T))
126 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 else if (isa<ArrayType>(T))
128 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000129 else if (isa<VectorType>(T))
130 ++NumVector;
Douglas Gregor72564e72009-02-26 23:50:07 +0000131 else if (isa<FunctionNoProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 ++NumFunctionNP;
Douglas Gregor72564e72009-02-26 23:50:07 +0000133 else if (isa<FunctionProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 ++NumFunctionP;
135 else if (isa<TypedefType>(T))
136 ++NumTypeName;
137 else if (TagType *TT = dyn_cast<TagType>(T)) {
138 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000139 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000141 case TagDecl::TK_struct: ++NumTagStruct; break;
142 case TagDecl::TK_union: ++NumTagUnion; break;
143 case TagDecl::TK_class: ++NumTagClass; break;
144 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000146 } else if (isa<ObjCInterfaceType>(T))
147 ++NumObjCInterfaces;
148 else if (isa<ObjCQualifiedInterfaceType>(T))
149 ++NumObjCQualifiedInterfaces;
150 else if (isa<ObjCQualifiedIdType>(T))
151 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000152 else if (isa<TypeOfType>(T))
153 ++NumTypeOfTypes;
Douglas Gregor72564e72009-02-26 23:50:07 +0000154 else if (isa<TypeOfExprType>(T))
155 ++NumTypeOfExprTypes;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000156 else if (isa<ExtQualType>(T))
157 ++NumExtQual;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000158 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000159 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 assert(0 && "Unknown type!");
161 }
162 }
163
164 fprintf(stderr, " %d builtin types\n", NumBuiltin);
165 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000166 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000167 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
168 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000169 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000170 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000172 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
174 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
175 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
176 fprintf(stderr, " %d tagged types\n", NumTagged);
177 fprintf(stderr, " %d struct types\n", NumTagStruct);
178 fprintf(stderr, " %d union types\n", NumTagUnion);
179 fprintf(stderr, " %d class types\n", NumTagClass);
180 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000181 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000182 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000183 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000184 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000185 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000186 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor72564e72009-02-26 23:50:07 +0000187 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000188 fprintf(stderr, " %d attribute-qualified types\n", NumExtQual);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000189
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
191 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000192 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000193 NumLValueReference*sizeof(LValueReferenceType)+
194 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000195 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000196 NumFunctionP*sizeof(FunctionProtoType)+
197 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000198 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000199 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)+
200 NumExtQual*sizeof(ExtQualType)));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000201
202 if (ExternalSource.get()) {
203 fprintf(stderr, "\n");
204 ExternalSource->PrintStats();
205 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000206}
207
208
209void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000210 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000211}
212
Reid Spencer5f016e22007-07-11 17:01:13 +0000213void ASTContext::InitBuiltinTypes() {
214 assert(VoidTy.isNull() && "Context reinitialized?");
215
216 // C99 6.2.5p19.
217 InitBuiltinType(VoidTy, BuiltinType::Void);
218
219 // C99 6.2.5p2.
220 InitBuiltinType(BoolTy, BuiltinType::Bool);
221 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000222 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 InitBuiltinType(CharTy, BuiltinType::Char_S);
224 else
225 InitBuiltinType(CharTy, BuiltinType::Char_U);
226 // C99 6.2.5p4.
227 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
228 InitBuiltinType(ShortTy, BuiltinType::Short);
229 InitBuiltinType(IntTy, BuiltinType::Int);
230 InitBuiltinType(LongTy, BuiltinType::Long);
231 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
232
233 // C99 6.2.5p6.
234 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
235 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
236 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
237 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
238 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
239
240 // C99 6.2.5p10.
241 InitBuiltinType(FloatTy, BuiltinType::Float);
242 InitBuiltinType(DoubleTy, BuiltinType::Double);
243 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000244
Chris Lattner2df9ced2009-04-30 02:43:43 +0000245 // GNU extension, 128-bit integers.
246 InitBuiltinType(Int128Ty, BuiltinType::Int128);
247 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
248
Chris Lattner3a250322009-02-26 23:43:47 +0000249 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
250 InitBuiltinType(WCharTy, BuiltinType::WChar);
251 else // C99
252 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000253
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000254 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000255 InitBuiltinType(OverloadTy, BuiltinType::Overload);
256
257 // Placeholder type for type-dependent expressions whose type is
258 // completely unknown. No code should ever check a type against
259 // DependentTy and users should never see it; however, it is here to
260 // help diagnose failures to properly check for type-dependent
261 // expressions.
262 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000263
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 // C99 6.2.5p11.
265 FloatComplexTy = getComplexType(FloatTy);
266 DoubleComplexTy = getComplexType(DoubleTy);
267 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000268
Steve Naroff7e219e42007-10-15 14:41:52 +0000269 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000270 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000271 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000272 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000273 ClassStructType = 0;
274
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000275 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000276
277 // void * type
278 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000279
280 // nullptr type (C++0x 2.14.7)
281 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000282}
283
Chris Lattner464175b2007-07-18 17:52:12 +0000284//===----------------------------------------------------------------------===//
285// Type Sizing and Analysis
286//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000287
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000288/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
289/// scalar floating point type.
290const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
291 const BuiltinType *BT = T->getAsBuiltinType();
292 assert(BT && "Not a floating point type!");
293 switch (BT->getKind()) {
294 default: assert(0 && "Not a floating point type!");
295 case BuiltinType::Float: return Target.getFloatFormat();
296 case BuiltinType::Double: return Target.getDoubleFormat();
297 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
298 }
299}
300
Chris Lattneraf707ab2009-01-24 21:53:27 +0000301/// getDeclAlign - Return a conservative estimate of the alignment of the
302/// specified decl. Note that bitfields do not have a valid alignment, so
303/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000304unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000305 unsigned Align = Target.getCharWidth();
306
307 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
308 Align = std::max(Align, AA->getAlignment());
309
Chris Lattneraf707ab2009-01-24 21:53:27 +0000310 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
311 QualType T = VD->getType();
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000312 if (const ReferenceType* RT = T->getAsReferenceType()) {
313 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000314 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000315 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
316 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000317 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
318 T = cast<ArrayType>(T)->getElementType();
319
320 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
321 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000322 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000323
324 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000325}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000326
Chris Lattnera7674d82007-07-13 22:13:22 +0000327/// getTypeSize - Return the size of the specified type, in bits. This method
328/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000329std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000330ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000331 uint64_t Width=0;
332 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000333 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000334#define TYPE(Class, Base)
335#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000336#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000337#define DEPENDENT_TYPE(Class, Base) case Type::Class:
338#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000339 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000340 break;
341
Chris Lattner692233e2007-07-13 22:27:08 +0000342 case Type::FunctionNoProto:
343 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000344 // GCC extension: alignof(function) = 32 bits
345 Width = 0;
346 Align = 32;
347 break;
348
Douglas Gregor72564e72009-02-26 23:50:07 +0000349 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000350 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000351 Width = 0;
352 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
353 break;
354
Steve Narofffb22d962007-08-30 01:06:46 +0000355 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000356 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000357
Chris Lattner98be4942008-03-05 18:54:05 +0000358 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000359 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000360 Align = EltInfo.second;
361 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000362 }
Nate Begeman213541a2008-04-18 23:10:10 +0000363 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000364 case Type::Vector: {
365 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000366 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000367 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000368 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000369 // If the alignment is not a power of 2, round up to the next power of 2.
370 // This happens for non-power-of-2 length vectors.
371 // FIXME: this should probably be a target property.
372 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000373 break;
374 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000375
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000376 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000377 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000378 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000379 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000380 // GCC extension: alignof(void) = 8 bits.
381 Width = 0;
382 Align = 8;
383 break;
384
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000385 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000386 Width = Target.getBoolWidth();
387 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000388 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000389 case BuiltinType::Char_S:
390 case BuiltinType::Char_U:
391 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000392 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000393 Width = Target.getCharWidth();
394 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000395 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000396 case BuiltinType::WChar:
397 Width = Target.getWCharWidth();
398 Align = Target.getWCharAlign();
399 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000400 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000401 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000402 Width = Target.getShortWidth();
403 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000404 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000405 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000406 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000407 Width = Target.getIntWidth();
408 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000409 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000410 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000411 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000412 Width = Target.getLongWidth();
413 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000414 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000415 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000416 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000417 Width = Target.getLongLongWidth();
418 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000419 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000420 case BuiltinType::Int128:
421 case BuiltinType::UInt128:
422 Width = 128;
423 Align = 128; // int128_t is 128-bit aligned on all targets.
424 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000425 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000426 Width = Target.getFloatWidth();
427 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000428 break;
429 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000430 Width = Target.getDoubleWidth();
431 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000432 break;
433 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000434 Width = Target.getLongDoubleWidth();
435 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000436 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000437 case BuiltinType::NullPtr:
438 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
439 Align = Target.getPointerAlign(0); // == sizeof(void*)
Chris Lattnera7674d82007-07-13 22:13:22 +0000440 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000441 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000442 case Type::FixedWidthInt:
443 // FIXME: This isn't precisely correct; the width/alignment should depend
444 // on the available types for the target
445 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000446 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000447 Align = Width;
448 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000449 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000450 // FIXME: Pointers into different addr spaces could have different sizes and
451 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000452 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000453 case Type::ObjCQualifiedId:
Douglas Gregor72564e72009-02-26 23:50:07 +0000454 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000455 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000456 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000457 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000458 case Type::BlockPointer: {
459 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
460 Width = Target.getPointerWidth(AS);
461 Align = Target.getPointerAlign(AS);
462 break;
463 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000464 case Type::Pointer: {
465 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000466 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000467 Align = Target.getPointerAlign(AS);
468 break;
469 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000470 case Type::LValueReference:
471 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000472 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000473 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000474 // FIXME: This is wrong for struct layout: a reference in a struct has
475 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000476 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000477 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000478 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000479 // the GCC ABI, where pointers to data are one pointer large, pointers to
480 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000481 // other compilers too, we need to delegate this completely to TargetInfo
482 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000483 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
484 unsigned AS = Pointee.getAddressSpace();
485 Width = Target.getPointerWidth(AS);
486 if (Pointee->isFunctionType())
487 Width *= 2;
488 Align = Target.getPointerAlign(AS);
489 // GCC aligns at single pointer width.
490 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000491 case Type::Complex: {
492 // Complex types have the same alignment as their elements, but twice the
493 // size.
494 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000495 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000496 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000497 Align = EltInfo.second;
498 break;
499 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000500 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000501 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000502 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
503 Width = Layout.getSize();
504 Align = Layout.getAlignment();
505 break;
506 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000507 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000508 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000509 const TagType *TT = cast<TagType>(T);
510
511 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000512 Width = 1;
513 Align = 1;
514 break;
515 }
516
Daniel Dunbar1d751182008-11-08 05:48:37 +0000517 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000518 return getTypeInfo(ET->getDecl()->getIntegerType());
519
Daniel Dunbar1d751182008-11-08 05:48:37 +0000520 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000521 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
522 Width = Layout.getSize();
523 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000524 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000525 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000526
Douglas Gregor18857642009-04-30 17:32:17 +0000527 case Type::Typedef: {
528 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
529 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
530 Align = Aligned->getAlignment();
531 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
532 } else
533 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000534 break;
Chris Lattner71763312008-04-06 22:05:18 +0000535 }
Douglas Gregor18857642009-04-30 17:32:17 +0000536
537 case Type::TypeOfExpr:
538 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
539 .getTypePtr());
540
541 case Type::TypeOf:
542 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
543
544 case Type::QualifiedName:
545 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
546
547 case Type::TemplateSpecialization:
548 assert(getCanonicalType(T) != T &&
549 "Cannot request the size of a dependent type");
550 // FIXME: this is likely to be wrong once we support template
551 // aliases, since a template alias could refer to a typedef that
552 // has an __aligned__ attribute on it.
553 return getTypeInfo(getCanonicalType(T));
554 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000555
Chris Lattner464175b2007-07-18 17:52:12 +0000556 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000557 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000558}
559
Chris Lattner34ebde42009-01-27 18:08:34 +0000560/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
561/// type for the current target in bits. This can be different than the ABI
562/// alignment in cases where it is beneficial for performance to overalign
563/// a data type.
564unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
565 unsigned ABIAlign = getTypeAlign(T);
566
567 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000568 if (T->isSpecificBuiltinType(BuiltinType::Double))
569 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000570
571 return ABIAlign;
572}
573
574
Devang Patel8b277042008-06-04 21:22:16 +0000575/// LayoutField - Field layout.
576void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000577 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000578 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000579 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000580 uint64_t FieldOffset = IsUnion ? 0 : Size;
581 uint64_t FieldSize;
582 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000583
584 // FIXME: Should this override struct packing? Probably we want to
585 // take the minimum?
586 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
587 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000588
589 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
590 // TODO: Need to check this algorithm on other targets!
591 // (tested on Linux-X86)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000592 FieldSize = BitWidthExpr->EvaluateAsInt(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000593
594 std::pair<uint64_t, unsigned> FieldInfo =
595 Context.getTypeInfo(FD->getType());
596 uint64_t TypeSize = FieldInfo.first;
597
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000598 // Determine the alignment of this bitfield. The packing
599 // attributes define a maximum and the alignment attribute defines
600 // a minimum.
601 // FIXME: What is the right behavior when the specified alignment
602 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000603 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000604 if (FieldPacking)
605 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000606 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
607 FieldAlign = std::max(FieldAlign, AA->getAlignment());
608
609 // Check if we need to add padding to give the field the correct
610 // alignment.
611 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
612 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
613
614 // Padding members don't affect overall alignment
615 if (!FD->getIdentifier())
616 FieldAlign = 1;
617 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000618 if (FD->getType()->isIncompleteArrayType()) {
619 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000620 // query getTypeInfo about these, so we figure it out here.
621 // Flexible array members don't have any size, but they
622 // have to be aligned appropriately for their element type.
623 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000624 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000625 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson2f1169f2009-04-10 05:31:15 +0000626 } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
627 unsigned AS = RT->getPointeeType().getAddressSpace();
628 FieldSize = Context.Target.getPointerWidth(AS);
629 FieldAlign = Context.Target.getPointerAlign(AS);
Devang Patel8b277042008-06-04 21:22:16 +0000630 } else {
631 std::pair<uint64_t, unsigned> FieldInfo =
632 Context.getTypeInfo(FD->getType());
633 FieldSize = FieldInfo.first;
634 FieldAlign = FieldInfo.second;
635 }
636
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000637 // Determine the alignment of this bitfield. The packing
638 // attributes define a maximum and the alignment attribute defines
639 // a minimum. Additionally, the packing alignment must be at least
640 // a byte for non-bitfields.
641 //
642 // FIXME: What is the right behavior when the specified alignment
643 // is smaller than the specified packing?
644 if (FieldPacking)
645 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000646 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
647 FieldAlign = std::max(FieldAlign, AA->getAlignment());
648
649 // Round up the current record size to the field's alignment boundary.
650 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
651 }
652
653 // Place this field at the current location.
654 FieldOffsets[FieldNo] = FieldOffset;
655
656 // Reserve space for this field.
657 if (IsUnion) {
658 Size = std::max(Size, FieldSize);
659 } else {
660 Size = FieldOffset + FieldSize;
661 }
662
Daniel Dunbard6884a02009-05-04 05:16:21 +0000663 // Remember the next available offset.
664 NextOffset = Size;
665
Devang Patel8b277042008-06-04 21:22:16 +0000666 // Remember max struct/class alignment.
667 Alignment = std::max(Alignment, FieldAlign);
668}
669
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000670static void CollectLocalObjCIvars(ASTContext *Ctx,
671 const ObjCInterfaceDecl *OI,
672 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000673 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
674 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000675 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000676 if (!IVDecl->isInvalidDecl())
677 Fields.push_back(cast<FieldDecl>(IVDecl));
678 }
679}
680
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000681void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
682 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
683 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
684 CollectObjCIvars(SuperClass, Fields);
685 CollectLocalObjCIvars(this, OI, Fields);
686}
687
Fariborz Jahanian98200742009-05-12 18:14:29 +0000688void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
689 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
690 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(*this),
691 E = PD->prop_end(*this); I != E; ++I)
692 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
693 Ivars.push_back(Ivar);
694
695 // Also look into nested protocols.
696 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
697 E = PD->protocol_end(); P != E; ++P)
698 CollectProtocolSynthesizedIvars(*P, Ivars);
699}
700
701/// CollectSynthesizedIvars -
702/// This routine collect synthesized ivars for the designated class.
703///
704void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
705 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
706 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*this),
707 E = OI->prop_end(*this); I != E; ++I) {
708 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
709 Ivars.push_back(Ivar);
710 }
711 // Also look into interface's protocol list for properties declared
712 // in the protocol and whose ivars are synthesized.
713 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
714 PE = OI->protocol_end(); P != PE; ++P) {
715 ObjCProtocolDecl *PD = (*P);
716 CollectProtocolSynthesizedIvars(PD, Ivars);
717 }
718}
719
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000720/// getInterfaceLayoutImpl - Get or compute information about the
721/// layout of the given interface.
722///
723/// \param Impl - If given, also include the layout of the interface's
724/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000725const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000726ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
727 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000728 assert(!D->isForwardDecl() && "Invalid interface decl!");
729
Devang Patel44a3dde2008-06-04 21:54:36 +0000730 // Look up this layout, if already laid out, return what we have.
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000731 ObjCContainerDecl *Key =
732 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
733 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
734 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000735
Daniel Dunbar453addb2009-05-03 11:16:44 +0000736 unsigned FieldCount = D->ivar_size();
737 // Add in synthesized ivar count if laying out an implementation.
738 if (Impl) {
Fariborz Jahanian98200742009-05-12 18:14:29 +0000739 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
740 CollectSynthesizedIvars(D, Ivars);
741 FieldCount += Ivars.size();
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000742 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +0000743 // entry. Note we can't cache this because we simply free all
744 // entries later; however we shouldn't look up implementations
745 // frequently.
746 if (FieldCount == D->ivar_size())
747 return getObjCLayout(D, 0);
748 }
749
Devang Patel6a5a34c2008-06-06 02:14:01 +0000750 ASTRecordLayout *NewEntry = NULL;
Devang Patel6a5a34c2008-06-06 02:14:01 +0000751 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Devang Patel6a5a34c2008-06-06 02:14:01 +0000752 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
753 unsigned Alignment = SL.getAlignment();
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000754
Daniel Dunbar913af352009-05-07 21:58:26 +0000755 // We start laying out ivars not at the end of the superclass
756 // structure, but at the next byte following the last field.
757 uint64_t Size = llvm::RoundUpToAlignment(SL.NextOffset, 8);
Daniel Dunbard6884a02009-05-04 05:16:21 +0000758
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000759 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout(Size, Alignment);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000760 NewEntry->InitializeLayout(FieldCount);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000761 } else {
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000762 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout();
Devang Patel6a5a34c2008-06-06 02:14:01 +0000763 NewEntry->InitializeLayout(FieldCount);
764 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000765
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000766 unsigned StructPacking = 0;
767 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
768 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000769
770 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
771 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
772 AA->getAlignment()));
773
774 // Layout each ivar sequentially.
775 unsigned i = 0;
776 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
777 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
778 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000779 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000780 }
Daniel Dunbar453addb2009-05-03 11:16:44 +0000781 // And synthesized ivars, if this is an implementation.
782 if (Impl) {
Fariborz Jahanian98200742009-05-12 18:14:29 +0000783 // FIXME. Do we need to colltect twice?
784 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
785 CollectSynthesizedIvars(D, Ivars);
786 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
787 NewEntry->LayoutField(Ivars[k], i++, false, StructPacking, *this);
Fariborz Jahanian18191882009-03-31 18:11:23 +0000788 }
Fariborz Jahanian99eee362009-04-01 19:37:34 +0000789
Devang Patel44a3dde2008-06-04 21:54:36 +0000790 // Finally, round the size of the total struct up to the alignment of the
791 // struct itself.
792 NewEntry->FinalizeLayout();
793 return *NewEntry;
794}
795
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000796const ASTRecordLayout &
797ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
798 return getObjCLayout(D, 0);
799}
800
801const ASTRecordLayout &
802ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
803 return getObjCLayout(D->getClassInterface(), D);
804}
805
Devang Patel88a981b2007-11-01 19:11:01 +0000806/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000807/// specified record (struct/union/class), which indicates its size and field
808/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000809const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000810 D = D->getDefinition(*this);
811 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000812
Chris Lattner464175b2007-07-18 17:52:12 +0000813 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000814 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000815 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000816
Devang Patel88a981b2007-11-01 19:11:01 +0000817 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
818 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
819 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000820 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000821
Douglas Gregore267ff32008-12-11 20:41:00 +0000822 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000823 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
824 D->field_end(*this)));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000825 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000826
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000827 unsigned StructPacking = 0;
828 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
829 StructPacking = PA->getAlignment();
830
Eli Friedman4bd998b2008-05-30 09:31:38 +0000831 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000832 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
833 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000834
Eli Friedman4bd998b2008-05-30 09:31:38 +0000835 // Layout each field, for now, just sequentially, respecting alignment. In
836 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000837 unsigned FieldIdx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000838 for (RecordDecl::field_iterator Field = D->field_begin(*this),
839 FieldEnd = D->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000840 Field != FieldEnd; (void)++Field, ++FieldIdx)
841 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000842
843 // Finally, round the size of the total struct up to the alignment of the
844 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000845 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000846 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000847}
848
Chris Lattnera7674d82007-07-13 22:13:22 +0000849//===----------------------------------------------------------------------===//
850// Type creation/memoization methods
851//===----------------------------------------------------------------------===//
852
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000853QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000854 QualType CanT = getCanonicalType(T);
855 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000856 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000857
858 // If we are composing extended qualifiers together, merge together into one
859 // ExtQualType node.
860 unsigned CVRQuals = T.getCVRQualifiers();
861 QualType::GCAttrTypes GCAttr = QualType::GCNone;
862 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000863
Chris Lattnerb7d25532009-02-18 22:53:11 +0000864 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
865 // If this type already has an address space specified, it cannot get
866 // another one.
867 assert(EQT->getAddressSpace() == 0 &&
868 "Type cannot be in multiple addr spaces!");
869 GCAttr = EQT->getObjCGCAttr();
870 TypeNode = EQT->getBaseType();
871 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000872
Chris Lattnerb7d25532009-02-18 22:53:11 +0000873 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000874 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000875 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000876 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000877 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000878 return QualType(EXTQy, CVRQuals);
879
Christopher Lambebb97e92008-02-04 02:31:56 +0000880 // If the base type isn't canonical, this won't be a canonical type either,
881 // so fill in the canonical type field.
882 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000883 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000884 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000885
Chris Lattnerb7d25532009-02-18 22:53:11 +0000886 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000887 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000888 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000889 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000890 ExtQualType *New =
891 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000892 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000893 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000894 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000895}
896
Chris Lattnerb7d25532009-02-18 22:53:11 +0000897QualType ASTContext::getObjCGCQualType(QualType T,
898 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000899 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000900 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000901 return T;
902
Chris Lattnerb7d25532009-02-18 22:53:11 +0000903 // If we are composing extended qualifiers together, merge together into one
904 // ExtQualType node.
905 unsigned CVRQuals = T.getCVRQualifiers();
906 Type *TypeNode = T.getTypePtr();
907 unsigned AddressSpace = 0;
908
909 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
910 // If this type already has an address space specified, it cannot get
911 // another one.
912 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
913 "Type cannot be in multiple addr spaces!");
914 AddressSpace = EQT->getAddressSpace();
915 TypeNode = EQT->getBaseType();
916 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000917
918 // Check if we've already instantiated an gc qual'd type of this type.
919 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000920 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000921 void *InsertPos = 0;
922 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000923 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000924
925 // If the base type isn't canonical, this won't be a canonical type either,
926 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000927 // FIXME: Isn't this also not canonical if the base type is a array
928 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000929 QualType Canonical;
930 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000931 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000932
Chris Lattnerb7d25532009-02-18 22:53:11 +0000933 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000934 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
935 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
936 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000937 ExtQualType *New =
938 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000939 ExtQualTypes.InsertNode(New, InsertPos);
940 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000941 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000942}
Chris Lattnera7674d82007-07-13 22:13:22 +0000943
Reid Spencer5f016e22007-07-11 17:01:13 +0000944/// getComplexType - Return the uniqued reference to the type for a complex
945/// number with the specified element type.
946QualType ASTContext::getComplexType(QualType T) {
947 // Unique pointers, to guarantee there is only one pointer of a particular
948 // structure.
949 llvm::FoldingSetNodeID ID;
950 ComplexType::Profile(ID, T);
951
952 void *InsertPos = 0;
953 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
954 return QualType(CT, 0);
955
956 // If the pointee type isn't canonical, this won't be a canonical type either,
957 // so fill in the canonical type field.
958 QualType Canonical;
959 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000960 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000961
962 // Get the new insert position for the node we care about.
963 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000964 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000965 }
Steve Narofff83820b2009-01-27 22:08:43 +0000966 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000967 Types.push_back(New);
968 ComplexTypes.InsertNode(New, InsertPos);
969 return QualType(New, 0);
970}
971
Eli Friedmanf98aba32009-02-13 02:31:07 +0000972QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
973 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
974 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
975 FixedWidthIntType *&Entry = Map[Width];
976 if (!Entry)
977 Entry = new FixedWidthIntType(Width, Signed);
978 return QualType(Entry, 0);
979}
Reid Spencer5f016e22007-07-11 17:01:13 +0000980
981/// getPointerType - Return the uniqued reference to the type for a pointer to
982/// the specified type.
983QualType ASTContext::getPointerType(QualType T) {
984 // Unique pointers, to guarantee there is only one pointer of a particular
985 // structure.
986 llvm::FoldingSetNodeID ID;
987 PointerType::Profile(ID, T);
988
989 void *InsertPos = 0;
990 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
991 return QualType(PT, 0);
992
993 // If the pointee type isn't canonical, this won't be a canonical type either,
994 // so fill in the canonical type field.
995 QualType Canonical;
996 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000997 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000998
999 // Get the new insert position for the node we care about.
1000 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001001 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001002 }
Steve Narofff83820b2009-01-27 22:08:43 +00001003 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001004 Types.push_back(New);
1005 PointerTypes.InsertNode(New, InsertPos);
1006 return QualType(New, 0);
1007}
1008
Steve Naroff5618bd42008-08-27 16:04:49 +00001009/// getBlockPointerType - Return the uniqued reference to the type for
1010/// a pointer to the specified block.
1011QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +00001012 assert(T->isFunctionType() && "block of function types only");
1013 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +00001014 // structure.
1015 llvm::FoldingSetNodeID ID;
1016 BlockPointerType::Profile(ID, T);
1017
1018 void *InsertPos = 0;
1019 if (BlockPointerType *PT =
1020 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1021 return QualType(PT, 0);
1022
Steve Naroff296e8d52008-08-28 19:20:44 +00001023 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +00001024 // type either so fill in the canonical type field.
1025 QualType Canonical;
1026 if (!T->isCanonical()) {
1027 Canonical = getBlockPointerType(getCanonicalType(T));
1028
1029 // Get the new insert position for the node we care about.
1030 BlockPointerType *NewIP =
1031 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001032 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001033 }
Steve Narofff83820b2009-01-27 22:08:43 +00001034 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001035 Types.push_back(New);
1036 BlockPointerTypes.InsertNode(New, InsertPos);
1037 return QualType(New, 0);
1038}
1039
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001040/// getLValueReferenceType - Return the uniqued reference to the type for an
1041/// lvalue reference to the specified type.
1042QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001043 // Unique pointers, to guarantee there is only one pointer of a particular
1044 // structure.
1045 llvm::FoldingSetNodeID ID;
1046 ReferenceType::Profile(ID, T);
1047
1048 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001049 if (LValueReferenceType *RT =
1050 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001051 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001052
Reid Spencer5f016e22007-07-11 17:01:13 +00001053 // If the referencee type isn't canonical, this won't be a canonical type
1054 // either, so fill in the canonical type field.
1055 QualType Canonical;
1056 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001057 Canonical = getLValueReferenceType(getCanonicalType(T));
1058
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001060 LValueReferenceType *NewIP =
1061 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001062 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001063 }
1064
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001065 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001067 LValueReferenceTypes.InsertNode(New, InsertPos);
1068 return QualType(New, 0);
1069}
1070
1071/// getRValueReferenceType - Return the uniqued reference to the type for an
1072/// rvalue reference to the specified type.
1073QualType ASTContext::getRValueReferenceType(QualType T) {
1074 // Unique pointers, to guarantee there is only one pointer of a particular
1075 // structure.
1076 llvm::FoldingSetNodeID ID;
1077 ReferenceType::Profile(ID, T);
1078
1079 void *InsertPos = 0;
1080 if (RValueReferenceType *RT =
1081 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1082 return QualType(RT, 0);
1083
1084 // If the referencee type isn't canonical, this won't be a canonical type
1085 // either, so fill in the canonical type field.
1086 QualType Canonical;
1087 if (!T->isCanonical()) {
1088 Canonical = getRValueReferenceType(getCanonicalType(T));
1089
1090 // Get the new insert position for the node we care about.
1091 RValueReferenceType *NewIP =
1092 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1093 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1094 }
1095
1096 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1097 Types.push_back(New);
1098 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001099 return QualType(New, 0);
1100}
1101
Sebastian Redlf30208a2009-01-24 21:16:55 +00001102/// getMemberPointerType - Return the uniqued reference to the type for a
1103/// member pointer to the specified type, in the specified class.
1104QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1105{
1106 // Unique pointers, to guarantee there is only one pointer of a particular
1107 // structure.
1108 llvm::FoldingSetNodeID ID;
1109 MemberPointerType::Profile(ID, T, Cls);
1110
1111 void *InsertPos = 0;
1112 if (MemberPointerType *PT =
1113 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1114 return QualType(PT, 0);
1115
1116 // If the pointee or class type isn't canonical, this won't be a canonical
1117 // type either, so fill in the canonical type field.
1118 QualType Canonical;
1119 if (!T->isCanonical()) {
1120 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1121
1122 // Get the new insert position for the node we care about.
1123 MemberPointerType *NewIP =
1124 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1125 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1126 }
Steve Narofff83820b2009-01-27 22:08:43 +00001127 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001128 Types.push_back(New);
1129 MemberPointerTypes.InsertNode(New, InsertPos);
1130 return QualType(New, 0);
1131}
1132
Steve Narofffb22d962007-08-30 01:06:46 +00001133/// getConstantArrayType - Return the unique reference to the type for an
1134/// array of the specified element type.
1135QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +00001136 const llvm::APInt &ArySize,
1137 ArrayType::ArraySizeModifier ASM,
1138 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001139 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001140 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001141
1142 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001143 if (ConstantArrayType *ATP =
1144 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 return QualType(ATP, 0);
1146
1147 // If the element type isn't canonical, this won't be a canonical type either,
1148 // so fill in the canonical type field.
1149 QualType Canonical;
1150 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001151 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001152 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001154 ConstantArrayType *NewIP =
1155 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001156 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001157 }
1158
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001159 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001160 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001161 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001162 Types.push_back(New);
1163 return QualType(New, 0);
1164}
1165
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001166/// getVariableArrayType - Returns a non-unique reference to the type for a
1167/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001168QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1169 ArrayType::ArraySizeModifier ASM,
1170 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001171 // Since we don't unique expressions, it isn't possible to unique VLA's
1172 // that have an expression provided for their size.
1173
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001174 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001175 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001176
1177 VariableArrayTypes.push_back(New);
1178 Types.push_back(New);
1179 return QualType(New, 0);
1180}
1181
Douglas Gregor898574e2008-12-05 23:32:09 +00001182/// getDependentSizedArrayType - Returns a non-unique reference to
1183/// the type for a dependently-sized array of the specified element
1184/// type. FIXME: We will need these to be uniqued, or at least
1185/// comparable, at some point.
1186QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1187 ArrayType::ArraySizeModifier ASM,
1188 unsigned EltTypeQuals) {
1189 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1190 "Size must be type- or value-dependent!");
1191
1192 // Since we don't unique expressions, it isn't possible to unique
1193 // dependently-sized array types.
1194
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001195 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001196 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1197 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001198
1199 DependentSizedArrayTypes.push_back(New);
1200 Types.push_back(New);
1201 return QualType(New, 0);
1202}
1203
Eli Friedmanc5773c42008-02-15 18:16:39 +00001204QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1205 ArrayType::ArraySizeModifier ASM,
1206 unsigned EltTypeQuals) {
1207 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001208 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001209
1210 void *InsertPos = 0;
1211 if (IncompleteArrayType *ATP =
1212 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1213 return QualType(ATP, 0);
1214
1215 // If the element type isn't canonical, this won't be a canonical type
1216 // either, so fill in the canonical type field.
1217 QualType Canonical;
1218
1219 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001220 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001221 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001222
1223 // Get the new insert position for the node we care about.
1224 IncompleteArrayType *NewIP =
1225 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001226 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001227 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001228
Steve Narofff83820b2009-01-27 22:08:43 +00001229 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001230 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001231
1232 IncompleteArrayTypes.InsertNode(New, InsertPos);
1233 Types.push_back(New);
1234 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001235}
1236
Steve Naroff73322922007-07-18 18:00:27 +00001237/// getVectorType - Return the unique reference to a vector type of
1238/// the specified element type and size. VectorType must be a built-in type.
1239QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001240 BuiltinType *baseType;
1241
Chris Lattnerf52ab252008-04-06 22:59:24 +00001242 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001243 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001244
1245 // Check if we've already instantiated a vector of this type.
1246 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001247 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 void *InsertPos = 0;
1249 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1250 return QualType(VTP, 0);
1251
1252 // If the element type isn't canonical, this won't be a canonical type either,
1253 // so fill in the canonical type field.
1254 QualType Canonical;
1255 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001256 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001257
1258 // Get the new insert position for the node we care about.
1259 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001260 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 }
Steve Narofff83820b2009-01-27 22:08:43 +00001262 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 VectorTypes.InsertNode(New, InsertPos);
1264 Types.push_back(New);
1265 return QualType(New, 0);
1266}
1267
Nate Begeman213541a2008-04-18 23:10:10 +00001268/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001269/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001270QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001271 BuiltinType *baseType;
1272
Chris Lattnerf52ab252008-04-06 22:59:24 +00001273 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001274 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001275
1276 // Check if we've already instantiated a vector of this type.
1277 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001278 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001279 void *InsertPos = 0;
1280 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1281 return QualType(VTP, 0);
1282
1283 // If the element type isn't canonical, this won't be a canonical type either,
1284 // so fill in the canonical type field.
1285 QualType Canonical;
1286 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001287 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001288
1289 // Get the new insert position for the node we care about.
1290 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001291 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001292 }
Steve Narofff83820b2009-01-27 22:08:43 +00001293 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001294 VectorTypes.InsertNode(New, InsertPos);
1295 Types.push_back(New);
1296 return QualType(New, 0);
1297}
1298
Douglas Gregor72564e72009-02-26 23:50:07 +00001299/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001300///
Douglas Gregor72564e72009-02-26 23:50:07 +00001301QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001302 // Unique functions, to guarantee there is only one function of a particular
1303 // structure.
1304 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001305 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001306
1307 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001308 if (FunctionNoProtoType *FT =
1309 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001310 return QualType(FT, 0);
1311
1312 QualType Canonical;
1313 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001314 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001315
1316 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001317 FunctionNoProtoType *NewIP =
1318 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001319 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001320 }
1321
Douglas Gregor72564e72009-02-26 23:50:07 +00001322 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001323 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001324 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001325 return QualType(New, 0);
1326}
1327
1328/// getFunctionType - Return a normal function type with a typed argument
1329/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001330QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001331 unsigned NumArgs, bool isVariadic,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001332 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001333 // Unique functions, to guarantee there is only one function of a particular
1334 // structure.
1335 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001336 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001337 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001338
1339 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001340 if (FunctionProtoType *FTP =
1341 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001342 return QualType(FTP, 0);
1343
1344 // Determine whether the type being created is already canonical or not.
1345 bool isCanonical = ResultTy->isCanonical();
1346 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1347 if (!ArgArray[i]->isCanonical())
1348 isCanonical = false;
1349
1350 // If this type isn't canonical, get the canonical version of it.
1351 QualType Canonical;
1352 if (!isCanonical) {
1353 llvm::SmallVector<QualType, 16> CanonicalArgs;
1354 CanonicalArgs.reserve(NumArgs);
1355 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001356 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001357
Chris Lattnerf52ab252008-04-06 22:59:24 +00001358 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001359 &CanonicalArgs[0], NumArgs,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001360 isVariadic, TypeQuals);
1361
Reid Spencer5f016e22007-07-11 17:01:13 +00001362 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001363 FunctionProtoType *NewIP =
1364 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001365 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001366 }
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001367
Douglas Gregor72564e72009-02-26 23:50:07 +00001368 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001369 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001370 FunctionProtoType *FTP =
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001371 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1372 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001373 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001374 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001375 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001376 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001377 return QualType(FTP, 0);
1378}
1379
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001380/// getTypeDeclType - Return the unique reference to the type for the
1381/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001382QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001383 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001384 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1385
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001386 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001387 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001388 else if (isa<TemplateTypeParmDecl>(Decl)) {
1389 assert(false && "Template type parameter types are always available.");
1390 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001391 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001392
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001393 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001394 if (PrevDecl)
1395 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001396 else
1397 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001398 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001399 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1400 if (PrevDecl)
1401 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001402 else
1403 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001404 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001405 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001406 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001407
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001408 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001409 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001410}
1411
Reid Spencer5f016e22007-07-11 17:01:13 +00001412/// getTypedefType - Return the unique reference to the type for the
1413/// specified typename decl.
1414QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1415 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1416
Chris Lattnerf52ab252008-04-06 22:59:24 +00001417 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001418 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001419 Types.push_back(Decl->TypeForDecl);
1420 return QualType(Decl->TypeForDecl, 0);
1421}
1422
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001423/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001424/// specified ObjC interface decl.
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001425QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001426 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1427
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001428 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1429 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff3536b442007-09-06 21:24:23 +00001430 Types.push_back(Decl->TypeForDecl);
1431 return QualType(Decl->TypeForDecl, 0);
1432}
1433
Douglas Gregorfab9d672009-02-05 23:33:38 +00001434/// \brief Retrieve the template type parameter type for a template
1435/// parameter with the given depth, index, and (optionally) name.
1436QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1437 IdentifierInfo *Name) {
1438 llvm::FoldingSetNodeID ID;
1439 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1440 void *InsertPos = 0;
1441 TemplateTypeParmType *TypeParm
1442 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1443
1444 if (TypeParm)
1445 return QualType(TypeParm, 0);
1446
1447 if (Name)
1448 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1449 getTemplateTypeParmType(Depth, Index));
1450 else
1451 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1452
1453 Types.push_back(TypeParm);
1454 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1455
1456 return QualType(TypeParm, 0);
1457}
1458
Douglas Gregor55f6b142009-02-09 18:46:07 +00001459QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001460ASTContext::getTemplateSpecializationType(TemplateName Template,
1461 const TemplateArgument *Args,
1462 unsigned NumArgs,
1463 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001464 if (!Canon.isNull())
1465 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001466
Douglas Gregor55f6b142009-02-09 18:46:07 +00001467 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001468 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001469
Douglas Gregor55f6b142009-02-09 18:46:07 +00001470 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001471 TemplateSpecializationType *Spec
1472 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001473
1474 if (Spec)
1475 return QualType(Spec, 0);
1476
Douglas Gregor7532dc62009-03-30 22:58:21 +00001477 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001478 sizeof(TemplateArgument) * NumArgs),
1479 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001480 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001481 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001482 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001483
1484 return QualType(Spec, 0);
1485}
1486
Douglas Gregore4e5b052009-03-19 00:18:19 +00001487QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001488ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001489 QualType NamedType) {
1490 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001491 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001492
1493 void *InsertPos = 0;
1494 QualifiedNameType *T
1495 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1496 if (T)
1497 return QualType(T, 0);
1498
Douglas Gregorab452ba2009-03-26 23:50:42 +00001499 T = new (*this) QualifiedNameType(NNS, NamedType,
1500 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001501 Types.push_back(T);
1502 QualifiedNameTypes.InsertNode(T, InsertPos);
1503 return QualType(T, 0);
1504}
1505
Douglas Gregord57959a2009-03-27 23:10:48 +00001506QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1507 const IdentifierInfo *Name,
1508 QualType Canon) {
1509 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1510
1511 if (Canon.isNull()) {
1512 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1513 if (CanonNNS != NNS)
1514 Canon = getTypenameType(CanonNNS, Name);
1515 }
1516
1517 llvm::FoldingSetNodeID ID;
1518 TypenameType::Profile(ID, NNS, Name);
1519
1520 void *InsertPos = 0;
1521 TypenameType *T
1522 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1523 if (T)
1524 return QualType(T, 0);
1525
1526 T = new (*this) TypenameType(NNS, Name, Canon);
1527 Types.push_back(T);
1528 TypenameTypes.InsertNode(T, InsertPos);
1529 return QualType(T, 0);
1530}
1531
Douglas Gregor17343172009-04-01 00:28:59 +00001532QualType
1533ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1534 const TemplateSpecializationType *TemplateId,
1535 QualType Canon) {
1536 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1537
1538 if (Canon.isNull()) {
1539 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1540 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1541 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1542 const TemplateSpecializationType *CanonTemplateId
1543 = CanonType->getAsTemplateSpecializationType();
1544 assert(CanonTemplateId &&
1545 "Canonical type must also be a template specialization type");
1546 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1547 }
1548 }
1549
1550 llvm::FoldingSetNodeID ID;
1551 TypenameType::Profile(ID, NNS, TemplateId);
1552
1553 void *InsertPos = 0;
1554 TypenameType *T
1555 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1556 if (T)
1557 return QualType(T, 0);
1558
1559 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1560 Types.push_back(T);
1561 TypenameTypes.InsertNode(T, InsertPos);
1562 return QualType(T, 0);
1563}
1564
Chris Lattner88cb27a2008-04-07 04:56:42 +00001565/// CmpProtocolNames - Comparison predicate for sorting protocols
1566/// alphabetically.
1567static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1568 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001569 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001570}
1571
1572static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1573 unsigned &NumProtocols) {
1574 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1575
1576 // Sort protocols, keyed by name.
1577 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1578
1579 // Remove duplicates.
1580 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1581 NumProtocols = ProtocolsEnd-Protocols;
1582}
1583
1584
Chris Lattner065f0d72008-04-07 04:44:08 +00001585/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1586/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001587QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1588 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001589 // Sort the protocol list alphabetically to canonicalize it.
1590 SortAndUniqueProtocols(Protocols, NumProtocols);
1591
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001592 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001593 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001594
1595 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001596 if (ObjCQualifiedInterfaceType *QT =
1597 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001598 return QualType(QT, 0);
1599
1600 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001601 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001602 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001603
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001604 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001605 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001606 return QualType(QType, 0);
1607}
1608
Chris Lattner88cb27a2008-04-07 04:56:42 +00001609/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1610/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001611QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001612 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001613 // Sort the protocol list alphabetically to canonicalize it.
1614 SortAndUniqueProtocols(Protocols, NumProtocols);
1615
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001616 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001617 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001618
1619 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001620 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001621 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001622 return QualType(QT, 0);
1623
1624 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001625 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001626 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001627 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001628 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001629 return QualType(QType, 0);
1630}
1631
Douglas Gregor72564e72009-02-26 23:50:07 +00001632/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1633/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001634/// multiple declarations that refer to "typeof(x)" all contain different
1635/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1636/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001637QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001638 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001639 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001640 Types.push_back(toe);
1641 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001642}
1643
Steve Naroff9752f252007-08-01 18:02:17 +00001644/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1645/// TypeOfType AST's. The only motivation to unique these nodes would be
1646/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1647/// an issue. This doesn't effect the type checker, since it operates
1648/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001649QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001650 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001651 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001652 Types.push_back(tot);
1653 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001654}
1655
Reid Spencer5f016e22007-07-11 17:01:13 +00001656/// getTagDeclType - Return the unique reference to the type for the
1657/// specified TagDecl (struct/union/class/enum) decl.
1658QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001659 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001660 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001661}
1662
1663/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1664/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1665/// needs to agree with the definition in <stddef.h>.
1666QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001667 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001668}
1669
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001670/// getSignedWCharType - Return the type of "signed wchar_t".
1671/// Used when in C++, as a GCC extension.
1672QualType ASTContext::getSignedWCharType() const {
1673 // FIXME: derive from "Target" ?
1674 return WCharTy;
1675}
1676
1677/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1678/// Used when in C++, as a GCC extension.
1679QualType ASTContext::getUnsignedWCharType() const {
1680 // FIXME: derive from "Target" ?
1681 return UnsignedIntTy;
1682}
1683
Chris Lattner8b9023b2007-07-13 03:05:23 +00001684/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1685/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1686QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001687 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001688}
1689
Chris Lattnere6327742008-04-02 05:18:44 +00001690//===----------------------------------------------------------------------===//
1691// Type Operators
1692//===----------------------------------------------------------------------===//
1693
Chris Lattner77c96472008-04-06 22:41:35 +00001694/// getCanonicalType - Return the canonical (structural) type corresponding to
1695/// the specified potentially non-canonical type. The non-canonical version
1696/// of a type may have many "decorated" versions of types. Decorators can
1697/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1698/// to be free of any of these, allowing two canonical types to be compared
1699/// for exact equality with a simple pointer comparison.
1700QualType ASTContext::getCanonicalType(QualType T) {
1701 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001702
1703 // If the result has type qualifiers, make sure to canonicalize them as well.
1704 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1705 if (TypeQuals == 0) return CanType;
1706
1707 // If the type qualifiers are on an array type, get the canonical type of the
1708 // array with the qualifiers applied to the element type.
1709 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1710 if (!AT)
1711 return CanType.getQualifiedType(TypeQuals);
1712
1713 // Get the canonical version of the element with the extra qualifiers on it.
1714 // This can recursively sink qualifiers through multiple levels of arrays.
1715 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1716 NewEltTy = getCanonicalType(NewEltTy);
1717
1718 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1719 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1720 CAT->getIndexTypeQualifier());
1721 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1722 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1723 IAT->getIndexTypeQualifier());
1724
Douglas Gregor898574e2008-12-05 23:32:09 +00001725 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1726 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1727 DSAT->getSizeModifier(),
1728 DSAT->getIndexTypeQualifier());
1729
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001730 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1731 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1732 VAT->getSizeModifier(),
1733 VAT->getIndexTypeQualifier());
1734}
1735
Douglas Gregor7da97d02009-05-10 22:57:19 +00001736Decl *ASTContext::getCanonicalDecl(Decl *D) {
Douglas Gregorc4ccf012009-05-10 22:59:12 +00001737 if (!D)
1738 return 0;
1739
Douglas Gregor7da97d02009-05-10 22:57:19 +00001740 if (TagDecl *Tag = dyn_cast<TagDecl>(D)) {
1741 QualType T = getTagDeclType(Tag);
1742 return cast<TagDecl>(cast<TagType>(T.getTypePtr()->CanonicalType)
1743 ->getDecl());
1744 }
1745
1746 if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(D)) {
1747 while (Template->getPreviousDeclaration())
1748 Template = Template->getPreviousDeclaration();
1749 return Template;
1750 }
1751
1752 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
1753 while (Function->getPreviousDeclaration())
1754 Function = Function->getPreviousDeclaration();
1755 return const_cast<FunctionDecl *>(Function);
1756 }
1757
1758 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
1759 while (Var->getPreviousDeclaration())
1760 Var = Var->getPreviousDeclaration();
1761 return const_cast<VarDecl *>(Var);
1762 }
1763
1764 return D;
1765}
1766
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001767TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
1768 // If this template name refers to a template, the canonical
1769 // template name merely stores the template itself.
1770 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Douglas Gregor7da97d02009-05-10 22:57:19 +00001771 return TemplateName(cast<TemplateDecl>(getCanonicalDecl(Template)));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001772
1773 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
1774 assert(DTN && "Non-dependent template names must refer to template decls.");
1775 return DTN->CanonicalTemplateName;
1776}
1777
Douglas Gregord57959a2009-03-27 23:10:48 +00001778NestedNameSpecifier *
1779ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1780 if (!NNS)
1781 return 0;
1782
1783 switch (NNS->getKind()) {
1784 case NestedNameSpecifier::Identifier:
1785 // Canonicalize the prefix but keep the identifier the same.
1786 return NestedNameSpecifier::Create(*this,
1787 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1788 NNS->getAsIdentifier());
1789
1790 case NestedNameSpecifier::Namespace:
1791 // A namespace is canonical; build a nested-name-specifier with
1792 // this namespace and no prefix.
1793 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1794
1795 case NestedNameSpecifier::TypeSpec:
1796 case NestedNameSpecifier::TypeSpecWithTemplate: {
1797 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1798 NestedNameSpecifier *Prefix = 0;
1799
1800 // FIXME: This isn't the right check!
1801 if (T->isDependentType())
1802 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1803
1804 return NestedNameSpecifier::Create(*this, Prefix,
1805 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1806 T.getTypePtr());
1807 }
1808
1809 case NestedNameSpecifier::Global:
1810 // The global specifier is canonical and unique.
1811 return NNS;
1812 }
1813
1814 // Required to silence a GCC warning
1815 return 0;
1816}
1817
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001818
1819const ArrayType *ASTContext::getAsArrayType(QualType T) {
1820 // Handle the non-qualified case efficiently.
1821 if (T.getCVRQualifiers() == 0) {
1822 // Handle the common positive case fast.
1823 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1824 return AT;
1825 }
1826
1827 // Handle the common negative case fast, ignoring CVR qualifiers.
1828 QualType CType = T->getCanonicalTypeInternal();
1829
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001830 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001831 // test.
1832 if (!isa<ArrayType>(CType) &&
1833 !isa<ArrayType>(CType.getUnqualifiedType()))
1834 return 0;
1835
1836 // Apply any CVR qualifiers from the array type to the element type. This
1837 // implements C99 6.7.3p8: "If the specification of an array type includes
1838 // any type qualifiers, the element type is so qualified, not the array type."
1839
1840 // If we get here, we either have type qualifiers on the type, or we have
1841 // sugar such as a typedef in the way. If we have type qualifiers on the type
1842 // we must propagate them down into the elemeng type.
1843 unsigned CVRQuals = T.getCVRQualifiers();
1844 unsigned AddrSpace = 0;
1845 Type *Ty = T.getTypePtr();
1846
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001847 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001848 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001849 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1850 AddrSpace = EXTQT->getAddressSpace();
1851 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001852 } else {
1853 T = Ty->getDesugaredType();
1854 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1855 break;
1856 CVRQuals |= T.getCVRQualifiers();
1857 Ty = T.getTypePtr();
1858 }
1859 }
1860
1861 // If we have a simple case, just return now.
1862 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1863 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1864 return ATy;
1865
1866 // Otherwise, we have an array and we have qualifiers on it. Push the
1867 // qualifiers into the array element type and return a new array type.
1868 // Get the canonical version of the element with the extra qualifiers on it.
1869 // This can recursively sink qualifiers through multiple levels of arrays.
1870 QualType NewEltTy = ATy->getElementType();
1871 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001872 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001873 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1874
1875 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1876 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1877 CAT->getSizeModifier(),
1878 CAT->getIndexTypeQualifier()));
1879 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1880 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1881 IAT->getSizeModifier(),
1882 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001883
Douglas Gregor898574e2008-12-05 23:32:09 +00001884 if (const DependentSizedArrayType *DSAT
1885 = dyn_cast<DependentSizedArrayType>(ATy))
1886 return cast<ArrayType>(
1887 getDependentSizedArrayType(NewEltTy,
1888 DSAT->getSizeExpr(),
1889 DSAT->getSizeModifier(),
1890 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001891
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001892 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1893 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1894 VAT->getSizeModifier(),
1895 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001896}
1897
1898
Chris Lattnere6327742008-04-02 05:18:44 +00001899/// getArrayDecayedType - Return the properly qualified result of decaying the
1900/// specified array type to a pointer. This operation is non-trivial when
1901/// handling typedefs etc. The canonical type of "T" must be an array type,
1902/// this returns a pointer to a properly qualified element of the array.
1903///
1904/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1905QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001906 // Get the element type with 'getAsArrayType' so that we don't lose any
1907 // typedefs in the element type of the array. This also handles propagation
1908 // of type qualifiers from the array type into the element type if present
1909 // (C99 6.7.3p8).
1910 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1911 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001912
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001913 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001914
1915 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001916 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001917}
1918
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001919QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001920 QualType ElemTy = VAT->getElementType();
1921
1922 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1923 return getBaseElementType(VAT);
1924
1925 return ElemTy;
1926}
1927
Reid Spencer5f016e22007-07-11 17:01:13 +00001928/// getFloatingRank - Return a relative rank for floating point types.
1929/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001930static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001931 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001932 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001933
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001934 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001935 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001936 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001937 case BuiltinType::Float: return FloatRank;
1938 case BuiltinType::Double: return DoubleRank;
1939 case BuiltinType::LongDouble: return LongDoubleRank;
1940 }
1941}
1942
Steve Naroff716c7302007-08-27 01:41:48 +00001943/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1944/// point or a complex type (based on typeDomain/typeSize).
1945/// 'typeDomain' is a real floating point or complex type.
1946/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001947QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1948 QualType Domain) const {
1949 FloatingRank EltRank = getFloatingRank(Size);
1950 if (Domain->isComplexType()) {
1951 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001952 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001953 case FloatRank: return FloatComplexTy;
1954 case DoubleRank: return DoubleComplexTy;
1955 case LongDoubleRank: return LongDoubleComplexTy;
1956 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001957 }
Chris Lattner1361b112008-04-06 23:58:54 +00001958
1959 assert(Domain->isRealFloatingType() && "Unknown domain!");
1960 switch (EltRank) {
1961 default: assert(0 && "getFloatingRank(): illegal value for rank");
1962 case FloatRank: return FloatTy;
1963 case DoubleRank: return DoubleTy;
1964 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001965 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001966}
1967
Chris Lattner7cfeb082008-04-06 23:55:33 +00001968/// getFloatingTypeOrder - Compare the rank of the two specified floating
1969/// point types, ignoring the domain of the type (i.e. 'double' ==
1970/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1971/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001972int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1973 FloatingRank LHSR = getFloatingRank(LHS);
1974 FloatingRank RHSR = getFloatingRank(RHS);
1975
1976 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001977 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001978 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001979 return 1;
1980 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001981}
1982
Chris Lattnerf52ab252008-04-06 22:59:24 +00001983/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1984/// routine will assert if passed a built-in type that isn't an integer or enum,
1985/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001986unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001987 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001988 if (EnumType* ET = dyn_cast<EnumType>(T))
1989 T = ET->getDecl()->getIntegerType().getTypePtr();
1990
1991 // There are two things which impact the integer rank: the width, and
1992 // the ordering of builtins. The builtin ordering is encoded in the
1993 // bottom three bits; the width is encoded in the bits above that.
1994 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1995 return FWIT->getWidth() << 3;
1996 }
1997
Chris Lattnerf52ab252008-04-06 22:59:24 +00001998 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001999 default: assert(0 && "getIntegerRank(): not a built-in integer");
2000 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002001 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002002 case BuiltinType::Char_S:
2003 case BuiltinType::Char_U:
2004 case BuiltinType::SChar:
2005 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002006 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002007 case BuiltinType::Short:
2008 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002009 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002010 case BuiltinType::Int:
2011 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002012 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002013 case BuiltinType::Long:
2014 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002015 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002016 case BuiltinType::LongLong:
2017 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002018 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00002019 case BuiltinType::Int128:
2020 case BuiltinType::UInt128:
2021 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00002022 }
2023}
2024
Chris Lattner7cfeb082008-04-06 23:55:33 +00002025/// getIntegerTypeOrder - Returns the highest ranked integer type:
2026/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
2027/// LHS < RHS, return -1.
2028int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002029 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2030 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00002031 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002032
Chris Lattnerf52ab252008-04-06 22:59:24 +00002033 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2034 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00002035
Chris Lattner7cfeb082008-04-06 23:55:33 +00002036 unsigned LHSRank = getIntegerRank(LHSC);
2037 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00002038
Chris Lattner7cfeb082008-04-06 23:55:33 +00002039 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2040 if (LHSRank == RHSRank) return 0;
2041 return LHSRank > RHSRank ? 1 : -1;
2042 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002043
Chris Lattner7cfeb082008-04-06 23:55:33 +00002044 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2045 if (LHSUnsigned) {
2046 // If the unsigned [LHS] type is larger, return it.
2047 if (LHSRank >= RHSRank)
2048 return 1;
2049
2050 // If the signed type can represent all values of the unsigned type, it
2051 // wins. Because we are dealing with 2's complement and types that are
2052 // powers of two larger than each other, this is always safe.
2053 return -1;
2054 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002055
Chris Lattner7cfeb082008-04-06 23:55:33 +00002056 // If the unsigned [RHS] type is larger, return it.
2057 if (RHSRank >= LHSRank)
2058 return -1;
2059
2060 // If the signed type can represent all values of the unsigned type, it
2061 // wins. Because we are dealing with 2's complement and types that are
2062 // powers of two larger than each other, this is always safe.
2063 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002064}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002065
2066// getCFConstantStringType - Return the type used for constant CFStrings.
2067QualType ASTContext::getCFConstantStringType() {
2068 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00002069 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002070 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002071 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002072 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00002073
2074 // const int *isa;
2075 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002076 // int flags;
2077 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002078 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002079 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00002080 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002081 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00002082
Anders Carlsson71993dd2007-08-17 05:31:46 +00002083 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002084 for (unsigned i = 0; i < 4; ++i) {
2085 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2086 SourceLocation(), 0,
2087 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002088 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002089 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002090 }
2091
2092 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002093 }
2094
2095 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002096}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002097
Douglas Gregor319ac892009-04-23 22:29:11 +00002098void ASTContext::setCFConstantStringType(QualType T) {
2099 const RecordType *Rec = T->getAsRecordType();
2100 assert(Rec && "Invalid CFConstantStringType");
2101 CFConstantStringTypeDecl = Rec->getDecl();
2102}
2103
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002104QualType ASTContext::getObjCFastEnumerationStateType()
2105{
2106 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002107 ObjCFastEnumerationStateTypeDecl =
2108 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2109 &Idents.get("__objcFastEnumerationState"));
2110
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002111 QualType FieldTypes[] = {
2112 UnsignedLongTy,
2113 getPointerType(ObjCIdType),
2114 getPointerType(UnsignedLongTy),
2115 getConstantArrayType(UnsignedLongTy,
2116 llvm::APInt(32, 5), ArrayType::Normal, 0)
2117 };
2118
Douglas Gregor44b43212008-12-11 16:49:14 +00002119 for (size_t i = 0; i < 4; ++i) {
2120 FieldDecl *Field = FieldDecl::Create(*this,
2121 ObjCFastEnumerationStateTypeDecl,
2122 SourceLocation(), 0,
2123 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002124 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002125 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002126 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002127
Douglas Gregor44b43212008-12-11 16:49:14 +00002128 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002129 }
2130
2131 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2132}
2133
Douglas Gregor319ac892009-04-23 22:29:11 +00002134void ASTContext::setObjCFastEnumerationStateType(QualType T) {
2135 const RecordType *Rec = T->getAsRecordType();
2136 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2137 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2138}
2139
Anders Carlssone8c49532007-10-29 06:33:42 +00002140// This returns true if a type has been typedefed to BOOL:
2141// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002142static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002143 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002144 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2145 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002146
2147 return false;
2148}
2149
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002150/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002151/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002152int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002153 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002154
2155 // Make all integer and enum types at least as large as an int
2156 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002157 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002158 // Treat arrays as pointers, since that's how they're passed in.
2159 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002160 sz = getTypeSize(VoidPtrTy);
2161 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002162}
2163
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002164/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002165/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002166void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002167 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002168 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002169 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002170 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002171 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002172 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002173 // Compute size of all parameters.
2174 // Start with computing size of a pointer in number of bytes.
2175 // FIXME: There might(should) be a better way of doing this computation!
2176 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002177 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002178 // The first two arguments (self and _cmd) are pointers; account for
2179 // their size.
2180 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002181 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2182 E = Decl->param_end(); PI != E; ++PI) {
2183 QualType PType = (*PI)->getType();
2184 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002185 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002186 ParmOffset += sz;
2187 }
2188 S += llvm::utostr(ParmOffset);
2189 S += "@0:";
2190 S += llvm::utostr(PtrSize);
2191
2192 // Argument types.
2193 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002194 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2195 E = Decl->param_end(); PI != E; ++PI) {
2196 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002197 QualType PType = PVDecl->getOriginalType();
2198 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002199 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2200 // Use array's original type only if it has known number of
2201 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002202 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002203 PType = PVDecl->getType();
2204 } else if (PType->isFunctionType())
2205 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002206 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002207 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002208 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002209 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002210 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002211 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002212 }
2213}
2214
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002215/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002216/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002217/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2218/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002219/// Property attributes are stored as a comma-delimited C string. The simple
2220/// attributes readonly and bycopy are encoded as single characters. The
2221/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2222/// encoded as single characters, followed by an identifier. Property types
2223/// are also encoded as a parametrized attribute. The characters used to encode
2224/// these attributes are defined by the following enumeration:
2225/// @code
2226/// enum PropertyAttributes {
2227/// kPropertyReadOnly = 'R', // property is read-only.
2228/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2229/// kPropertyByref = '&', // property is a reference to the value last assigned
2230/// kPropertyDynamic = 'D', // property is dynamic
2231/// kPropertyGetter = 'G', // followed by getter selector name
2232/// kPropertySetter = 'S', // followed by setter selector name
2233/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2234/// kPropertyType = 't' // followed by old-style type encoding.
2235/// kPropertyWeak = 'W' // 'weak' property
2236/// kPropertyStrong = 'P' // property GC'able
2237/// kPropertyNonAtomic = 'N' // property non-atomic
2238/// };
2239/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002240void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2241 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002242 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002243 // Collect information from the property implementation decl(s).
2244 bool Dynamic = false;
2245 ObjCPropertyImplDecl *SynthesizePID = 0;
2246
2247 // FIXME: Duplicated code due to poor abstraction.
2248 if (Container) {
2249 if (const ObjCCategoryImplDecl *CID =
2250 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2251 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002252 i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2253 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002254 ObjCPropertyImplDecl *PID = *i;
2255 if (PID->getPropertyDecl() == PD) {
2256 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2257 Dynamic = true;
2258 } else {
2259 SynthesizePID = PID;
2260 }
2261 }
2262 }
2263 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002264 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002265 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002266 i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2267 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002268 ObjCPropertyImplDecl *PID = *i;
2269 if (PID->getPropertyDecl() == PD) {
2270 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2271 Dynamic = true;
2272 } else {
2273 SynthesizePID = PID;
2274 }
2275 }
2276 }
2277 }
2278 }
2279
2280 // FIXME: This is not very efficient.
2281 S = "T";
2282
2283 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002284 // GCC has some special rules regarding encoding of properties which
2285 // closely resembles encoding of ivars.
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002286 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002287 true /* outermost type */,
2288 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002289
2290 if (PD->isReadOnly()) {
2291 S += ",R";
2292 } else {
2293 switch (PD->getSetterKind()) {
2294 case ObjCPropertyDecl::Assign: break;
2295 case ObjCPropertyDecl::Copy: S += ",C"; break;
2296 case ObjCPropertyDecl::Retain: S += ",&"; break;
2297 }
2298 }
2299
2300 // It really isn't clear at all what this means, since properties
2301 // are "dynamic by default".
2302 if (Dynamic)
2303 S += ",D";
2304
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002305 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2306 S += ",N";
2307
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002308 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2309 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002310 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002311 }
2312
2313 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2314 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002315 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002316 }
2317
2318 if (SynthesizePID) {
2319 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2320 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002321 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002322 }
2323
2324 // FIXME: OBJCGC: weak & strong
2325}
2326
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002327/// getLegacyIntegralTypeEncoding -
2328/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002329/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002330/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2331///
2332void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2333 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2334 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002335 if (BT->getKind() == BuiltinType::ULong &&
2336 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002337 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002338 else
2339 if (BT->getKind() == BuiltinType::Long &&
2340 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002341 PointeeTy = IntTy;
2342 }
2343 }
2344}
2345
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002346void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002347 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002348 // We follow the behavior of gcc, expanding structures which are
2349 // directly pointed to, and expanding embedded structures. Note that
2350 // these rules are sufficient to prevent recursive encoding of the
2351 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002352 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2353 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002354}
2355
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002356static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002357 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002358 const Expr *E = FD->getBitWidth();
2359 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2360 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002361 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002362 S += 'b';
2363 S += llvm::utostr(N);
2364}
2365
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002366void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2367 bool ExpandPointedToStructures,
2368 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002369 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002370 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002371 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002372 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002373 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002374 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002375 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002376 else {
2377 char encoding;
2378 switch (BT->getKind()) {
2379 default: assert(0 && "Unhandled builtin type kind");
2380 case BuiltinType::Void: encoding = 'v'; break;
2381 case BuiltinType::Bool: encoding = 'B'; break;
2382 case BuiltinType::Char_U:
2383 case BuiltinType::UChar: encoding = 'C'; break;
2384 case BuiltinType::UShort: encoding = 'S'; break;
2385 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002386 case BuiltinType::ULong:
2387 encoding =
2388 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2389 break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002390 case BuiltinType::UInt128: encoding = 'T'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002391 case BuiltinType::ULongLong: encoding = 'Q'; break;
2392 case BuiltinType::Char_S:
2393 case BuiltinType::SChar: encoding = 'c'; break;
2394 case BuiltinType::Short: encoding = 's'; break;
2395 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002396 case BuiltinType::Long:
2397 encoding =
2398 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2399 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002400 case BuiltinType::LongLong: encoding = 'q'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002401 case BuiltinType::Int128: encoding = 't'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002402 case BuiltinType::Float: encoding = 'f'; break;
2403 case BuiltinType::Double: encoding = 'd'; break;
2404 case BuiltinType::LongDouble: encoding = 'd'; break;
2405 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002406
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002407 S += encoding;
2408 }
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002409 } else if (const ComplexType *CT = T->getAsComplexType()) {
2410 S += 'j';
2411 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2412 false);
2413 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002414 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2415 ExpandPointedToStructures,
2416 ExpandStructures, FD);
2417 if (FD || EncodingProperty) {
2418 // Note that we do extended encoding of protocol qualifer list
2419 // Only when doing ivar or property encoding.
2420 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2421 S += '"';
2422 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2423 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2424 S += '<';
2425 S += Proto->getNameAsString();
2426 S += '>';
2427 }
2428 S += '"';
2429 }
2430 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002431 }
2432 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002433 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002434 bool isReadOnly = false;
2435 // For historical/compatibility reasons, the read-only qualifier of the
2436 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2437 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2438 // Also, do not emit the 'r' for anything but the outermost type!
2439 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2440 if (OutermostType && T.isConstQualified()) {
2441 isReadOnly = true;
2442 S += 'r';
2443 }
2444 }
2445 else if (OutermostType) {
2446 QualType P = PointeeTy;
2447 while (P->getAsPointerType())
2448 P = P->getAsPointerType()->getPointeeType();
2449 if (P.isConstQualified()) {
2450 isReadOnly = true;
2451 S += 'r';
2452 }
2453 }
2454 if (isReadOnly) {
2455 // Another legacy compatibility encoding. Some ObjC qualifier and type
2456 // combinations need to be rearranged.
2457 // Rewrite "in const" from "nr" to "rn"
2458 const char * s = S.c_str();
2459 int len = S.length();
2460 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2461 std::string replace = "rn";
2462 S.replace(S.end()-2, S.end(), replace);
2463 }
2464 }
Steve Naroff389bf462009-02-12 17:52:19 +00002465 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002466 S += '@';
2467 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002468 }
2469 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002470 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002471 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002472 // Another historical/compatibility reason.
2473 // We encode the underlying type which comes out as
2474 // {...};
2475 S += '^';
2476 getObjCEncodingForTypeImpl(PointeeTy, S,
2477 false, ExpandPointedToStructures,
2478 NULL);
2479 return;
2480 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002481 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002482 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002483 const ObjCInterfaceType *OIT =
2484 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002485 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002486 S += '"';
2487 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002488 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2489 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2490 S += '<';
2491 S += Proto->getNameAsString();
2492 S += '>';
2493 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002494 S += '"';
2495 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002496 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002497 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002498 S += '#';
2499 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002500 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002501 S += ':';
2502 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002503 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002504
2505 if (PointeeTy->isCharType()) {
2506 // char pointer types should be encoded as '*' unless it is a
2507 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002508 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002509 S += '*';
2510 return;
2511 }
2512 }
2513
2514 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002515 getLegacyIntegralTypeEncoding(PointeeTy);
2516
2517 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002518 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002519 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002520 } else if (const ArrayType *AT =
2521 // Ignore type qualifiers etc.
2522 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002523 if (isa<IncompleteArrayType>(AT)) {
2524 // Incomplete arrays are encoded as a pointer to the array element.
2525 S += '^';
2526
2527 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2528 false, ExpandStructures, FD);
2529 } else {
2530 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002531
Anders Carlsson559a8332009-02-22 01:38:57 +00002532 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2533 S += llvm::utostr(CAT->getSize().getZExtValue());
2534 else {
2535 //Variable length arrays are encoded as a regular array with 0 elements.
2536 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2537 S += '0';
2538 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002539
Anders Carlsson559a8332009-02-22 01:38:57 +00002540 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2541 false, ExpandStructures, FD);
2542 S += ']';
2543 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002544 } else if (T->getAsFunctionType()) {
2545 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002546 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002547 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002548 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002549 // Anonymous structures print as '?'
2550 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2551 S += II->getName();
2552 } else {
2553 S += '?';
2554 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002555 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002556 S += '=';
Douglas Gregor6ab35242009-04-09 21:40:53 +00002557 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2558 FieldEnd = RDecl->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +00002559 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002560 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002561 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002562 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002563 S += '"';
2564 }
2565
2566 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002567 if (Field->isBitField()) {
2568 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2569 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002570 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002571 QualType qt = Field->getType();
2572 getLegacyIntegralTypeEncoding(qt);
2573 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002574 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002575 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002576 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002577 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002578 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002579 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002580 if (FD && FD->isBitField())
2581 EncodeBitField(this, S, FD);
2582 else
2583 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002584 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002585 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002586 } else if (T->isObjCInterfaceType()) {
2587 // @encode(class_name)
2588 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2589 S += '{';
2590 const IdentifierInfo *II = OI->getIdentifier();
2591 S += II->getName();
2592 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002593 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002594 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002595 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002596 if (RecFields[i]->isBitField())
2597 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2598 RecFields[i]);
2599 else
2600 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2601 FD);
2602 }
2603 S += '}';
2604 }
2605 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002606 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002607}
2608
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002609void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002610 std::string& S) const {
2611 if (QT & Decl::OBJC_TQ_In)
2612 S += 'n';
2613 if (QT & Decl::OBJC_TQ_Inout)
2614 S += 'N';
2615 if (QT & Decl::OBJC_TQ_Out)
2616 S += 'o';
2617 if (QT & Decl::OBJC_TQ_Bycopy)
2618 S += 'O';
2619 if (QT & Decl::OBJC_TQ_Byref)
2620 S += 'R';
2621 if (QT & Decl::OBJC_TQ_Oneway)
2622 S += 'V';
2623}
2624
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002625void ASTContext::setBuiltinVaListType(QualType T)
2626{
2627 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2628
2629 BuiltinVaListType = T;
2630}
2631
Douglas Gregor319ac892009-04-23 22:29:11 +00002632void ASTContext::setObjCIdType(QualType T)
Steve Naroff7e219e42007-10-15 14:41:52 +00002633{
Douglas Gregor319ac892009-04-23 22:29:11 +00002634 ObjCIdType = T;
2635
2636 const TypedefType *TT = T->getAsTypedefType();
2637 if (!TT)
2638 return;
2639
2640 TypedefDecl *TD = TT->getDecl();
Steve Naroff7e219e42007-10-15 14:41:52 +00002641
2642 // typedef struct objc_object *id;
2643 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002644 // User error - caller will issue diagnostics.
2645 if (!ptr)
2646 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002647 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002648 // User error - caller will issue diagnostics.
2649 if (!rec)
2650 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002651 IdStructType = rec;
2652}
2653
Douglas Gregor319ac892009-04-23 22:29:11 +00002654void ASTContext::setObjCSelType(QualType T)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002655{
Douglas Gregor319ac892009-04-23 22:29:11 +00002656 ObjCSelType = T;
2657
2658 const TypedefType *TT = T->getAsTypedefType();
2659 if (!TT)
2660 return;
2661 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002662
2663 // typedef struct objc_selector *SEL;
2664 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002665 if (!ptr)
2666 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002667 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002668 if (!rec)
2669 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002670 SelStructType = rec;
2671}
2672
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002673void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002674{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002675 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002676}
2677
Douglas Gregor319ac892009-04-23 22:29:11 +00002678void ASTContext::setObjCClassType(QualType T)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002679{
Douglas Gregor319ac892009-04-23 22:29:11 +00002680 ObjCClassType = T;
2681
2682 const TypedefType *TT = T->getAsTypedefType();
2683 if (!TT)
2684 return;
2685 TypedefDecl *TD = TT->getDecl();
Anders Carlsson8baaca52007-10-31 02:53:19 +00002686
2687 // typedef struct objc_class *Class;
2688 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2689 assert(ptr && "'Class' incorrectly typed");
2690 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2691 assert(rec && "'Class' incorrectly typed");
2692 ClassStructType = rec;
2693}
2694
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002695void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2696 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002697 "'NSConstantString' type already set!");
2698
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002699 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002700}
2701
Douglas Gregor7532dc62009-03-30 22:58:21 +00002702/// \brief Retrieve the template name that represents a qualified
2703/// template name such as \c std::vector.
2704TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2705 bool TemplateKeyword,
2706 TemplateDecl *Template) {
2707 llvm::FoldingSetNodeID ID;
2708 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2709
2710 void *InsertPos = 0;
2711 QualifiedTemplateName *QTN =
2712 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2713 if (!QTN) {
2714 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2715 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2716 }
2717
2718 return TemplateName(QTN);
2719}
2720
2721/// \brief Retrieve the template name that represents a dependent
2722/// template name such as \c MetaFun::template apply.
2723TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2724 const IdentifierInfo *Name) {
2725 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2726
2727 llvm::FoldingSetNodeID ID;
2728 DependentTemplateName::Profile(ID, NNS, Name);
2729
2730 void *InsertPos = 0;
2731 DependentTemplateName *QTN =
2732 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2733
2734 if (QTN)
2735 return TemplateName(QTN);
2736
2737 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2738 if (CanonNNS == NNS) {
2739 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2740 } else {
2741 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2742 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2743 }
2744
2745 DependentTemplateNames.InsertNode(QTN, InsertPos);
2746 return TemplateName(QTN);
2747}
2748
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002749/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002750/// TargetInfo, produce the corresponding type. The unsigned @p Type
2751/// is actually a value of type @c TargetInfo::IntType.
2752QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002753 switch (Type) {
2754 case TargetInfo::NoInt: return QualType();
2755 case TargetInfo::SignedShort: return ShortTy;
2756 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2757 case TargetInfo::SignedInt: return IntTy;
2758 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2759 case TargetInfo::SignedLong: return LongTy;
2760 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2761 case TargetInfo::SignedLongLong: return LongLongTy;
2762 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2763 }
2764
2765 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002766 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002767}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002768
2769//===----------------------------------------------------------------------===//
2770// Type Predicates.
2771//===----------------------------------------------------------------------===//
2772
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002773/// isObjCNSObjectType - Return true if this is an NSObject object using
2774/// NSObject attribute on a c-style pointer type.
2775/// FIXME - Make it work directly on types.
2776///
2777bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2778 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2779 if (TypedefDecl *TD = TDT->getDecl())
2780 if (TD->getAttr<ObjCNSObjectAttr>())
2781 return true;
2782 }
2783 return false;
2784}
2785
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002786/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2787/// to an object type. This includes "id" and "Class" (two 'special' pointers
2788/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2789/// ID type).
2790bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002791 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002792 return true;
2793
Steve Naroff6ae98502008-10-21 18:24:04 +00002794 // Blocks are objects.
2795 if (Ty->isBlockPointerType())
2796 return true;
2797
2798 // All other object types are pointers.
Chris Lattner16ede0e2009-04-12 23:51:02 +00002799 const PointerType *PT = Ty->getAsPointerType();
2800 if (PT == 0)
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002801 return false;
2802
Chris Lattner16ede0e2009-04-12 23:51:02 +00002803 // If this a pointer to an interface (e.g. NSString*), it is ok.
2804 if (PT->getPointeeType()->isObjCInterfaceType() ||
2805 // If is has NSObject attribute, OK as well.
2806 isObjCNSObjectType(Ty))
2807 return true;
2808
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002809 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2810 // pointer types. This looks for the typedef specifically, not for the
Chris Lattner16ede0e2009-04-12 23:51:02 +00002811 // underlying type. Iteratively strip off typedefs so that we can handle
2812 // typedefs of typedefs.
2813 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2814 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2815 Ty.getUnqualifiedType() == getObjCClassType())
2816 return true;
2817
2818 Ty = TDT->getDecl()->getUnderlyingType();
2819 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002820
Chris Lattner16ede0e2009-04-12 23:51:02 +00002821 return false;
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002822}
2823
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002824/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2825/// garbage collection attribute.
2826///
2827QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002828 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002829 if (getLangOptions().ObjC1 &&
2830 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002831 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002832 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002833 // (or pointers to them) be treated as though they were declared
2834 // as __strong.
2835 if (GCAttrs == QualType::GCNone) {
2836 if (isObjCObjectPointerType(Ty))
2837 GCAttrs = QualType::Strong;
2838 else if (Ty->isPointerType())
2839 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2840 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00002841 // Non-pointers have none gc'able attribute regardless of the attribute
2842 // set on them.
2843 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2844 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002845 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002846 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002847}
2848
Chris Lattner6ac46a42008-04-07 06:51:04 +00002849//===----------------------------------------------------------------------===//
2850// Type Compatibility Testing
2851//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002852
Steve Naroff1c7d0672008-09-04 15:10:53 +00002853/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002854/// block types. Types must be strictly compatible here. For example,
2855/// C unfortunately doesn't produce an error for the following:
2856///
2857/// int (*emptyArgFunc)();
2858/// int (*intArgList)(int) = emptyArgFunc;
2859///
2860/// For blocks, we will produce an error for the following (similar to C++):
2861///
2862/// int (^emptyArgBlock)();
2863/// int (^intArgBlock)(int) = emptyArgBlock;
2864///
2865/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2866///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002867bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002868 const FunctionType *lbase = lhs->getAsFunctionType();
2869 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002870 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2871 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stumpaab0f7a2009-04-01 01:17:39 +00002872 if (lproto && rproto == 0)
2873 return false;
2874 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff1c7d0672008-09-04 15:10:53 +00002875}
2876
Chris Lattner6ac46a42008-04-07 06:51:04 +00002877/// areCompatVectorTypes - Return true if the two specified vector types are
2878/// compatible.
2879static bool areCompatVectorTypes(const VectorType *LHS,
2880 const VectorType *RHS) {
2881 assert(LHS->isCanonical() && RHS->isCanonical());
2882 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002883 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002884}
2885
Eli Friedman3d815e72008-08-22 00:56:42 +00002886/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002887/// compatible for assignment from RHS to LHS. This handles validation of any
2888/// protocol qualifiers on the LHS or RHS.
2889///
Eli Friedman3d815e72008-08-22 00:56:42 +00002890bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2891 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002892 // Verify that the base decls are compatible: the RHS must be a subclass of
2893 // the LHS.
2894 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2895 return false;
2896
2897 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2898 // protocol qualified at all, then we are good.
2899 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2900 return true;
2901
2902 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2903 // isn't a superset.
2904 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2905 return true; // FIXME: should return false!
2906
2907 // Finally, we must have two protocol-qualified interfaces.
2908 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2909 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002910
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002911 // All LHS protocols must have a presence on the RHS.
2912 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002913
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002914 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2915 LHSPE = LHSP->qual_end();
2916 LHSPI != LHSPE; LHSPI++) {
2917 bool RHSImplementsProtocol = false;
2918
2919 // If the RHS doesn't implement the protocol on the left, the types
2920 // are incompatible.
2921 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2922 RHSPE = RHSP->qual_end();
2923 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2924 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2925 RHSImplementsProtocol = true;
2926 }
2927 // FIXME: For better diagnostics, consider passing back the protocol name.
2928 if (!RHSImplementsProtocol)
2929 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002930 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002931 // The RHS implements all protocols listed on the LHS.
2932 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002933}
2934
Steve Naroff389bf462009-02-12 17:52:19 +00002935bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2936 // get the "pointed to" types
2937 const PointerType *LHSPT = LHS->getAsPointerType();
2938 const PointerType *RHSPT = RHS->getAsPointerType();
2939
2940 if (!LHSPT || !RHSPT)
2941 return false;
2942
2943 QualType lhptee = LHSPT->getPointeeType();
2944 QualType rhptee = RHSPT->getPointeeType();
2945 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2946 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2947 // ID acts sort of like void* for ObjC interfaces
2948 if (LHSIface && isObjCIdStructType(rhptee))
2949 return true;
2950 if (RHSIface && isObjCIdStructType(lhptee))
2951 return true;
2952 if (!LHSIface || !RHSIface)
2953 return false;
2954 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2955 canAssignObjCInterfaces(RHSIface, LHSIface);
2956}
2957
Steve Naroffec0550f2007-10-15 20:41:53 +00002958/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2959/// both shall have the identically qualified version of a compatible type.
2960/// C99 6.2.7p1: Two types have compatible types if their types are the
2961/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002962bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2963 return !mergeTypes(LHS, RHS).isNull();
2964}
2965
2966QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2967 const FunctionType *lbase = lhs->getAsFunctionType();
2968 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002969 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2970 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002971 bool allLTypes = true;
2972 bool allRTypes = true;
2973
2974 // Check return type
2975 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2976 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002977 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2978 allLTypes = false;
2979 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2980 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002981
2982 if (lproto && rproto) { // two C99 style function prototypes
2983 unsigned lproto_nargs = lproto->getNumArgs();
2984 unsigned rproto_nargs = rproto->getNumArgs();
2985
2986 // Compatible functions must have the same number of arguments
2987 if (lproto_nargs != rproto_nargs)
2988 return QualType();
2989
2990 // Variadic and non-variadic functions aren't compatible
2991 if (lproto->isVariadic() != rproto->isVariadic())
2992 return QualType();
2993
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002994 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2995 return QualType();
2996
Eli Friedman3d815e72008-08-22 00:56:42 +00002997 // Check argument compatibility
2998 llvm::SmallVector<QualType, 10> types;
2999 for (unsigned i = 0; i < lproto_nargs; i++) {
3000 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
3001 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
3002 QualType argtype = mergeTypes(largtype, rargtype);
3003 if (argtype.isNull()) return QualType();
3004 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00003005 if (getCanonicalType(argtype) != getCanonicalType(largtype))
3006 allLTypes = false;
3007 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
3008 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00003009 }
3010 if (allLTypes) return lhs;
3011 if (allRTypes) return rhs;
3012 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003013 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00003014 }
3015
3016 if (lproto) allRTypes = false;
3017 if (rproto) allLTypes = false;
3018
Douglas Gregor72564e72009-02-26 23:50:07 +00003019 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00003020 if (proto) {
3021 if (proto->isVariadic()) return QualType();
3022 // Check that the types are compatible with the types that
3023 // would result from default argument promotions (C99 6.7.5.3p15).
3024 // The only types actually affected are promotable integer
3025 // types and floats, which would be passed as a different
3026 // type depending on whether the prototype is visible.
3027 unsigned proto_nargs = proto->getNumArgs();
3028 for (unsigned i = 0; i < proto_nargs; ++i) {
3029 QualType argTy = proto->getArgType(i);
3030 if (argTy->isPromotableIntegerType() ||
3031 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
3032 return QualType();
3033 }
3034
3035 if (allLTypes) return lhs;
3036 if (allRTypes) return rhs;
3037 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003038 proto->getNumArgs(), lproto->isVariadic(),
3039 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00003040 }
3041
3042 if (allLTypes) return lhs;
3043 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00003044 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00003045}
3046
3047QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00003048 // C++ [expr]: If an expression initially has the type "reference to T", the
3049 // type is adjusted to "T" prior to any further analysis, the expression
3050 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003051 // expression is an lvalue unless the reference is an rvalue reference and
3052 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00003053 // FIXME: C++ shouldn't be going through here! The rules are different
3054 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003055 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3056 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00003057 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003058 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00003059 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003060 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00003061
Eli Friedman3d815e72008-08-22 00:56:42 +00003062 QualType LHSCan = getCanonicalType(LHS),
3063 RHSCan = getCanonicalType(RHS);
3064
3065 // If two types are identical, they are compatible.
3066 if (LHSCan == RHSCan)
3067 return LHS;
3068
3069 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003070 // Note that we handle extended qualifiers later, in the
3071 // case for ExtQualType.
3072 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00003073 return QualType();
3074
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003075 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
3076 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003077
Chris Lattner1adb8832008-01-14 05:45:46 +00003078 // We want to consider the two function types to be the same for these
3079 // comparisons, just force one to the other.
3080 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3081 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003082
3083 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003084 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3085 LHSClass = Type::ConstantArray;
3086 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3087 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00003088
Nate Begeman213541a2008-04-18 23:10:10 +00003089 // Canonicalize ExtVector -> Vector.
3090 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3091 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00003092
Chris Lattnerb0489812008-04-07 06:38:24 +00003093 // Consider qualified interfaces and interfaces the same.
3094 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
3095 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00003096
Chris Lattnera36a61f2008-04-07 05:43:21 +00003097 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003098 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003099 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3100 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003101
Steve Naroffd824c9c2009-04-14 15:11:46 +00003102 // 'id' and 'Class' act sort of like void* for ObjC interfaces
3103 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003104 return LHS;
Steve Naroffd824c9c2009-04-14 15:11:46 +00003105 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003106 return RHS;
3107
Steve Naroffbc76dd02008-12-10 22:14:21 +00003108 // ID is compatible with all qualified id types.
3109 if (LHS->isObjCQualifiedIdType()) {
3110 if (const PointerType *PT = RHS->getAsPointerType()) {
3111 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003112 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003113 return LHS;
3114 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3115 // Unfortunately, this API is part of Sema (which we don't have access
3116 // to. Need to refactor. The following check is insufficient, since we
3117 // need to make sure the class implements the protocol.
3118 if (pType->isObjCInterfaceType())
3119 return LHS;
3120 }
3121 }
3122 if (RHS->isObjCQualifiedIdType()) {
3123 if (const PointerType *PT = LHS->getAsPointerType()) {
3124 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003125 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003126 return RHS;
3127 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3128 // Unfortunately, this API is part of Sema (which we don't have access
3129 // to. Need to refactor. The following check is insufficient, since we
3130 // need to make sure the class implements the protocol.
3131 if (pType->isObjCInterfaceType())
3132 return RHS;
3133 }
3134 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003135 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3136 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00003137 if (const EnumType* ETy = LHS->getAsEnumType()) {
3138 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3139 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003140 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003141 if (const EnumType* ETy = RHS->getAsEnumType()) {
3142 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3143 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003144 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003145
Eli Friedman3d815e72008-08-22 00:56:42 +00003146 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003147 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003148
Steve Naroff4a746782008-01-09 22:43:08 +00003149 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003150 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003151#define TYPE(Class, Base)
3152#define ABSTRACT_TYPE(Class, Base)
3153#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3154#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3155#include "clang/AST/TypeNodes.def"
3156 assert(false && "Non-canonical and dependent types shouldn't get here");
3157 return QualType();
3158
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003159 case Type::LValueReference:
3160 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003161 case Type::MemberPointer:
3162 assert(false && "C++ should never be in mergeTypes");
3163 return QualType();
3164
3165 case Type::IncompleteArray:
3166 case Type::VariableArray:
3167 case Type::FunctionProto:
3168 case Type::ExtVector:
3169 case Type::ObjCQualifiedInterface:
3170 assert(false && "Types are eliminated above");
3171 return QualType();
3172
Chris Lattner1adb8832008-01-14 05:45:46 +00003173 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003174 {
3175 // Merge two pointer types, while trying to preserve typedef info
3176 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3177 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3178 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3179 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003180 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3181 return LHS;
3182 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3183 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003184 return getPointerType(ResultType);
3185 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003186 case Type::BlockPointer:
3187 {
3188 // Merge two block pointer types, while trying to preserve typedef info
3189 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3190 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3191 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3192 if (ResultType.isNull()) return QualType();
3193 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3194 return LHS;
3195 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3196 return RHS;
3197 return getBlockPointerType(ResultType);
3198 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003199 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003200 {
3201 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3202 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3203 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3204 return QualType();
3205
3206 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3207 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3208 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3209 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003210 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3211 return LHS;
3212 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3213 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003214 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3215 ArrayType::ArraySizeModifier(), 0);
3216 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3217 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003218 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3219 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003220 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3221 return LHS;
3222 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3223 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003224 if (LVAT) {
3225 // FIXME: This isn't correct! But tricky to implement because
3226 // the array's size has to be the size of LHS, but the type
3227 // has to be different.
3228 return LHS;
3229 }
3230 if (RVAT) {
3231 // FIXME: This isn't correct! But tricky to implement because
3232 // the array's size has to be the size of RHS, but the type
3233 // has to be different.
3234 return RHS;
3235 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003236 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3237 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003238 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003239 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003240 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003241 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003242 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003243 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003244 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003245 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3246 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003247 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003248 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003249 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003250 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003251 case Type::Complex:
3252 // Distinct complex types are incompatible.
3253 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003254 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003255 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003256 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3257 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003258 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003259 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003260 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003261 // FIXME: This should be type compatibility, e.g. whether
3262 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003263 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3264 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3265 if (LHSIface && RHSIface &&
3266 canAssignObjCInterfaces(LHSIface, RHSIface))
3267 return LHS;
3268
Eli Friedman3d815e72008-08-22 00:56:42 +00003269 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003270 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003271 case Type::ObjCQualifiedId:
3272 // Distinct qualified id's are not compatible.
3273 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003274 case Type::FixedWidthInt:
3275 // Distinct fixed-width integers are not compatible.
3276 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003277 case Type::ExtQual:
3278 // FIXME: ExtQual types can be compatible even if they're not
3279 // identical!
3280 return QualType();
3281 // First attempt at an implementation, but I'm not really sure it's
3282 // right...
3283#if 0
3284 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3285 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3286 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3287 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3288 return QualType();
3289 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3290 LHSBase = QualType(LQual->getBaseType(), 0);
3291 RHSBase = QualType(RQual->getBaseType(), 0);
3292 ResultType = mergeTypes(LHSBase, RHSBase);
3293 if (ResultType.isNull()) return QualType();
3294 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3295 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3296 return LHS;
3297 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3298 return RHS;
3299 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3300 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3301 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3302 return ResultType;
3303#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003304
3305 case Type::TemplateSpecialization:
3306 assert(false && "Dependent types have no size");
3307 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003308 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003309
3310 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003311}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003312
Chris Lattner5426bf62008-04-07 07:01:58 +00003313//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003314// Integer Predicates
3315//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003316
Eli Friedmanad74a752008-06-28 06:23:08 +00003317unsigned ASTContext::getIntWidth(QualType T) {
3318 if (T == BoolTy)
3319 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003320 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3321 return FWIT->getWidth();
3322 }
3323 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003324 return (unsigned)getTypeSize(T);
3325}
3326
3327QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3328 assert(T->isSignedIntegerType() && "Unexpected type");
3329 if (const EnumType* ETy = T->getAsEnumType())
3330 T = ETy->getDecl()->getIntegerType();
3331 const BuiltinType* BTy = T->getAsBuiltinType();
3332 assert (BTy && "Unexpected signed integer type");
3333 switch (BTy->getKind()) {
3334 case BuiltinType::Char_S:
3335 case BuiltinType::SChar:
3336 return UnsignedCharTy;
3337 case BuiltinType::Short:
3338 return UnsignedShortTy;
3339 case BuiltinType::Int:
3340 return UnsignedIntTy;
3341 case BuiltinType::Long:
3342 return UnsignedLongTy;
3343 case BuiltinType::LongLong:
3344 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00003345 case BuiltinType::Int128:
3346 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00003347 default:
3348 assert(0 && "Unexpected signed integer type");
3349 return QualType();
3350 }
3351}
3352
Douglas Gregor2cf26342009-04-09 22:27:44 +00003353ExternalASTSource::~ExternalASTSource() { }
3354
3355void ExternalASTSource::PrintStats() { }