blob: ccbea263aa5182a5b586c8c9b1f1f1aa135be769 [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: {
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000478 // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
479 // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
480 // If we ever want to support other ABIs this needs to be abstracted.
481
Sebastian Redlf30208a2009-01-24 21:16:55 +0000482 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000483 std::pair<uint64_t, unsigned> PtrDiffInfo =
484 getTypeInfo(getPointerDiffType());
485 Width = PtrDiffInfo.first;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000486 if (Pointee->isFunctionType())
487 Width *= 2;
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000488 Align = PtrDiffInfo.second;
489 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000490 }
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,
Chris Lattner38aeec72009-05-13 04:12:56 +00001136 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001137 ArrayType::ArraySizeModifier ASM,
1138 unsigned EltTypeQuals) {
Chris Lattner38aeec72009-05-13 04:12:56 +00001139 // Convert the array size into a canonical width matching the pointer size for
1140 // the target.
1141 llvm::APInt ArySize(ArySizeIn);
1142 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1143
Reid Spencer5f016e22007-07-11 17:01:13 +00001144 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001145 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001146
1147 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001148 if (ConstantArrayType *ATP =
1149 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001150 return QualType(ATP, 0);
1151
1152 // If the element type isn't canonical, this won't be a canonical type either,
1153 // so fill in the canonical type field.
1154 QualType Canonical;
1155 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001156 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001157 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001158 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001159 ConstantArrayType *NewIP =
1160 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001161 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001162 }
1163
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001164 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001165 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001166 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001167 Types.push_back(New);
1168 return QualType(New, 0);
1169}
1170
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001171/// getVariableArrayType - Returns a non-unique reference to the type for a
1172/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001173QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1174 ArrayType::ArraySizeModifier ASM,
1175 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001176 // Since we don't unique expressions, it isn't possible to unique VLA's
1177 // that have an expression provided for their size.
1178
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001179 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001180 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001181
1182 VariableArrayTypes.push_back(New);
1183 Types.push_back(New);
1184 return QualType(New, 0);
1185}
1186
Douglas Gregor898574e2008-12-05 23:32:09 +00001187/// getDependentSizedArrayType - Returns a non-unique reference to
1188/// the type for a dependently-sized array of the specified element
1189/// type. FIXME: We will need these to be uniqued, or at least
1190/// comparable, at some point.
1191QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1192 ArrayType::ArraySizeModifier ASM,
1193 unsigned EltTypeQuals) {
1194 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1195 "Size must be type- or value-dependent!");
1196
1197 // Since we don't unique expressions, it isn't possible to unique
1198 // dependently-sized array types.
1199
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001200 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001201 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1202 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001203
1204 DependentSizedArrayTypes.push_back(New);
1205 Types.push_back(New);
1206 return QualType(New, 0);
1207}
1208
Eli Friedmanc5773c42008-02-15 18:16:39 +00001209QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1210 ArrayType::ArraySizeModifier ASM,
1211 unsigned EltTypeQuals) {
1212 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001213 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001214
1215 void *InsertPos = 0;
1216 if (IncompleteArrayType *ATP =
1217 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1218 return QualType(ATP, 0);
1219
1220 // If the element type isn't canonical, this won't be a canonical type
1221 // either, so fill in the canonical type field.
1222 QualType Canonical;
1223
1224 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001225 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001226 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001227
1228 // Get the new insert position for the node we care about.
1229 IncompleteArrayType *NewIP =
1230 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001231 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001232 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001233
Steve Narofff83820b2009-01-27 22:08:43 +00001234 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001235 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001236
1237 IncompleteArrayTypes.InsertNode(New, InsertPos);
1238 Types.push_back(New);
1239 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001240}
1241
Steve Naroff73322922007-07-18 18:00:27 +00001242/// getVectorType - Return the unique reference to a vector type of
1243/// the specified element type and size. VectorType must be a built-in type.
1244QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 BuiltinType *baseType;
1246
Chris Lattnerf52ab252008-04-06 22:59:24 +00001247 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001248 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001249
1250 // Check if we've already instantiated a vector of this type.
1251 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001252 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 void *InsertPos = 0;
1254 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1255 return QualType(VTP, 0);
1256
1257 // If the element type isn't canonical, this won't be a canonical type either,
1258 // so fill in the canonical type field.
1259 QualType Canonical;
1260 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001261 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001262
1263 // Get the new insert position for the node we care about.
1264 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001265 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001266 }
Steve Narofff83820b2009-01-27 22:08:43 +00001267 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001268 VectorTypes.InsertNode(New, InsertPos);
1269 Types.push_back(New);
1270 return QualType(New, 0);
1271}
1272
Nate Begeman213541a2008-04-18 23:10:10 +00001273/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001274/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001275QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001276 BuiltinType *baseType;
1277
Chris Lattnerf52ab252008-04-06 22:59:24 +00001278 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001279 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001280
1281 // Check if we've already instantiated a vector of this type.
1282 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001283 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001284 void *InsertPos = 0;
1285 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1286 return QualType(VTP, 0);
1287
1288 // If the element type isn't canonical, this won't be a canonical type either,
1289 // so fill in the canonical type field.
1290 QualType Canonical;
1291 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001292 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001293
1294 // Get the new insert position for the node we care about.
1295 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001296 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001297 }
Steve Narofff83820b2009-01-27 22:08:43 +00001298 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001299 VectorTypes.InsertNode(New, InsertPos);
1300 Types.push_back(New);
1301 return QualType(New, 0);
1302}
1303
Douglas Gregor72564e72009-02-26 23:50:07 +00001304/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001305///
Douglas Gregor72564e72009-02-26 23:50:07 +00001306QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001307 // Unique functions, to guarantee there is only one function of a particular
1308 // structure.
1309 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001310 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001311
1312 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001313 if (FunctionNoProtoType *FT =
1314 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001315 return QualType(FT, 0);
1316
1317 QualType Canonical;
1318 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001319 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001320
1321 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001322 FunctionNoProtoType *NewIP =
1323 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001324 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001325 }
1326
Douglas Gregor72564e72009-02-26 23:50:07 +00001327 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001328 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001329 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001330 return QualType(New, 0);
1331}
1332
1333/// getFunctionType - Return a normal function type with a typed argument
1334/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001335QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001336 unsigned NumArgs, bool isVariadic,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001337 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001338 // Unique functions, to guarantee there is only one function of a particular
1339 // structure.
1340 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001341 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001342 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001343
1344 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001345 if (FunctionProtoType *FTP =
1346 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001347 return QualType(FTP, 0);
1348
1349 // Determine whether the type being created is already canonical or not.
1350 bool isCanonical = ResultTy->isCanonical();
1351 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1352 if (!ArgArray[i]->isCanonical())
1353 isCanonical = false;
1354
1355 // If this type isn't canonical, get the canonical version of it.
1356 QualType Canonical;
1357 if (!isCanonical) {
1358 llvm::SmallVector<QualType, 16> CanonicalArgs;
1359 CanonicalArgs.reserve(NumArgs);
1360 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001361 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001362
Chris Lattnerf52ab252008-04-06 22:59:24 +00001363 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001364 CanonicalArgs.data(), NumArgs,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001365 isVariadic, TypeQuals);
1366
Reid Spencer5f016e22007-07-11 17:01:13 +00001367 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001368 FunctionProtoType *NewIP =
1369 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001370 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001371 }
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001372
Douglas Gregor72564e72009-02-26 23:50:07 +00001373 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001374 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001375 FunctionProtoType *FTP =
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001376 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1377 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001378 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001379 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001380 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001381 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001382 return QualType(FTP, 0);
1383}
1384
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001385/// getTypeDeclType - Return the unique reference to the type for the
1386/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001387QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001388 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001389 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1390
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001391 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001392 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001393 else if (isa<TemplateTypeParmDecl>(Decl)) {
1394 assert(false && "Template type parameter types are always available.");
1395 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001396 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001397
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001398 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001399 if (PrevDecl)
1400 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001401 else
1402 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001403 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001404 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1405 if (PrevDecl)
1406 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001407 else
1408 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001409 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001410 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001411 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001412
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001413 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001414 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001415}
1416
Reid Spencer5f016e22007-07-11 17:01:13 +00001417/// getTypedefType - Return the unique reference to the type for the
1418/// specified typename decl.
1419QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1420 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1421
Chris Lattnerf52ab252008-04-06 22:59:24 +00001422 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001423 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001424 Types.push_back(Decl->TypeForDecl);
1425 return QualType(Decl->TypeForDecl, 0);
1426}
1427
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001428/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001429/// specified ObjC interface decl.
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001430QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001431 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1432
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001433 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1434 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff3536b442007-09-06 21:24:23 +00001435 Types.push_back(Decl->TypeForDecl);
1436 return QualType(Decl->TypeForDecl, 0);
1437}
1438
Douglas Gregorfab9d672009-02-05 23:33:38 +00001439/// \brief Retrieve the template type parameter type for a template
1440/// parameter with the given depth, index, and (optionally) name.
1441QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1442 IdentifierInfo *Name) {
1443 llvm::FoldingSetNodeID ID;
1444 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1445 void *InsertPos = 0;
1446 TemplateTypeParmType *TypeParm
1447 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1448
1449 if (TypeParm)
1450 return QualType(TypeParm, 0);
1451
1452 if (Name)
1453 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1454 getTemplateTypeParmType(Depth, Index));
1455 else
1456 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1457
1458 Types.push_back(TypeParm);
1459 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1460
1461 return QualType(TypeParm, 0);
1462}
1463
Douglas Gregor55f6b142009-02-09 18:46:07 +00001464QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001465ASTContext::getTemplateSpecializationType(TemplateName Template,
1466 const TemplateArgument *Args,
1467 unsigned NumArgs,
1468 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001469 if (!Canon.isNull())
1470 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001471
Douglas Gregor55f6b142009-02-09 18:46:07 +00001472 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001473 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001474
Douglas Gregor55f6b142009-02-09 18:46:07 +00001475 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001476 TemplateSpecializationType *Spec
1477 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001478
1479 if (Spec)
1480 return QualType(Spec, 0);
1481
Douglas Gregor7532dc62009-03-30 22:58:21 +00001482 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001483 sizeof(TemplateArgument) * NumArgs),
1484 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001485 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001486 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001487 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001488
1489 return QualType(Spec, 0);
1490}
1491
Douglas Gregore4e5b052009-03-19 00:18:19 +00001492QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001493ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001494 QualType NamedType) {
1495 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001496 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001497
1498 void *InsertPos = 0;
1499 QualifiedNameType *T
1500 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1501 if (T)
1502 return QualType(T, 0);
1503
Douglas Gregorab452ba2009-03-26 23:50:42 +00001504 T = new (*this) QualifiedNameType(NNS, NamedType,
1505 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001506 Types.push_back(T);
1507 QualifiedNameTypes.InsertNode(T, InsertPos);
1508 return QualType(T, 0);
1509}
1510
Douglas Gregord57959a2009-03-27 23:10:48 +00001511QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1512 const IdentifierInfo *Name,
1513 QualType Canon) {
1514 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1515
1516 if (Canon.isNull()) {
1517 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1518 if (CanonNNS != NNS)
1519 Canon = getTypenameType(CanonNNS, Name);
1520 }
1521
1522 llvm::FoldingSetNodeID ID;
1523 TypenameType::Profile(ID, NNS, Name);
1524
1525 void *InsertPos = 0;
1526 TypenameType *T
1527 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1528 if (T)
1529 return QualType(T, 0);
1530
1531 T = new (*this) TypenameType(NNS, Name, Canon);
1532 Types.push_back(T);
1533 TypenameTypes.InsertNode(T, InsertPos);
1534 return QualType(T, 0);
1535}
1536
Douglas Gregor17343172009-04-01 00:28:59 +00001537QualType
1538ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1539 const TemplateSpecializationType *TemplateId,
1540 QualType Canon) {
1541 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1542
1543 if (Canon.isNull()) {
1544 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1545 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1546 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1547 const TemplateSpecializationType *CanonTemplateId
1548 = CanonType->getAsTemplateSpecializationType();
1549 assert(CanonTemplateId &&
1550 "Canonical type must also be a template specialization type");
1551 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1552 }
1553 }
1554
1555 llvm::FoldingSetNodeID ID;
1556 TypenameType::Profile(ID, NNS, TemplateId);
1557
1558 void *InsertPos = 0;
1559 TypenameType *T
1560 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1561 if (T)
1562 return QualType(T, 0);
1563
1564 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1565 Types.push_back(T);
1566 TypenameTypes.InsertNode(T, InsertPos);
1567 return QualType(T, 0);
1568}
1569
Chris Lattner88cb27a2008-04-07 04:56:42 +00001570/// CmpProtocolNames - Comparison predicate for sorting protocols
1571/// alphabetically.
1572static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1573 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001574 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001575}
1576
1577static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1578 unsigned &NumProtocols) {
1579 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1580
1581 // Sort protocols, keyed by name.
1582 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1583
1584 // Remove duplicates.
1585 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1586 NumProtocols = ProtocolsEnd-Protocols;
1587}
1588
1589
Chris Lattner065f0d72008-04-07 04:44:08 +00001590/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1591/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001592QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1593 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001594 // Sort the protocol list alphabetically to canonicalize it.
1595 SortAndUniqueProtocols(Protocols, NumProtocols);
1596
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001597 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001598 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001599
1600 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001601 if (ObjCQualifiedInterfaceType *QT =
1602 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001603 return QualType(QT, 0);
1604
1605 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001606 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001607 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001608
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001609 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001610 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001611 return QualType(QType, 0);
1612}
1613
Chris Lattner88cb27a2008-04-07 04:56:42 +00001614/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1615/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001616QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001617 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001618 // Sort the protocol list alphabetically to canonicalize it.
1619 SortAndUniqueProtocols(Protocols, NumProtocols);
1620
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001621 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001622 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001623
1624 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001625 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001626 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001627 return QualType(QT, 0);
1628
1629 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001630 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001631 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001632 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001633 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001634 return QualType(QType, 0);
1635}
1636
Douglas Gregor72564e72009-02-26 23:50:07 +00001637/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1638/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001639/// multiple declarations that refer to "typeof(x)" all contain different
1640/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1641/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001642QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001643 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001644 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001645 Types.push_back(toe);
1646 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001647}
1648
Steve Naroff9752f252007-08-01 18:02:17 +00001649/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1650/// TypeOfType AST's. The only motivation to unique these nodes would be
1651/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1652/// an issue. This doesn't effect the type checker, since it operates
1653/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001654QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001655 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001656 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001657 Types.push_back(tot);
1658 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001659}
1660
Reid Spencer5f016e22007-07-11 17:01:13 +00001661/// getTagDeclType - Return the unique reference to the type for the
1662/// specified TagDecl (struct/union/class/enum) decl.
1663QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001664 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001665 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001666}
1667
1668/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1669/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1670/// needs to agree with the definition in <stddef.h>.
1671QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001672 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001673}
1674
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001675/// getSignedWCharType - Return the type of "signed wchar_t".
1676/// Used when in C++, as a GCC extension.
1677QualType ASTContext::getSignedWCharType() const {
1678 // FIXME: derive from "Target" ?
1679 return WCharTy;
1680}
1681
1682/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1683/// Used when in C++, as a GCC extension.
1684QualType ASTContext::getUnsignedWCharType() const {
1685 // FIXME: derive from "Target" ?
1686 return UnsignedIntTy;
1687}
1688
Chris Lattner8b9023b2007-07-13 03:05:23 +00001689/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1690/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1691QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001692 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001693}
1694
Chris Lattnere6327742008-04-02 05:18:44 +00001695//===----------------------------------------------------------------------===//
1696// Type Operators
1697//===----------------------------------------------------------------------===//
1698
Chris Lattner77c96472008-04-06 22:41:35 +00001699/// getCanonicalType - Return the canonical (structural) type corresponding to
1700/// the specified potentially non-canonical type. The non-canonical version
1701/// of a type may have many "decorated" versions of types. Decorators can
1702/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1703/// to be free of any of these, allowing two canonical types to be compared
1704/// for exact equality with a simple pointer comparison.
1705QualType ASTContext::getCanonicalType(QualType T) {
1706 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001707
1708 // If the result has type qualifiers, make sure to canonicalize them as well.
1709 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1710 if (TypeQuals == 0) return CanType;
1711
1712 // If the type qualifiers are on an array type, get the canonical type of the
1713 // array with the qualifiers applied to the element type.
1714 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1715 if (!AT)
1716 return CanType.getQualifiedType(TypeQuals);
1717
1718 // Get the canonical version of the element with the extra qualifiers on it.
1719 // This can recursively sink qualifiers through multiple levels of arrays.
1720 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1721 NewEltTy = getCanonicalType(NewEltTy);
1722
1723 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1724 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1725 CAT->getIndexTypeQualifier());
1726 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1727 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1728 IAT->getIndexTypeQualifier());
1729
Douglas Gregor898574e2008-12-05 23:32:09 +00001730 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1731 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1732 DSAT->getSizeModifier(),
1733 DSAT->getIndexTypeQualifier());
1734
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001735 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1736 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1737 VAT->getSizeModifier(),
1738 VAT->getIndexTypeQualifier());
1739}
1740
Douglas Gregor7da97d02009-05-10 22:57:19 +00001741Decl *ASTContext::getCanonicalDecl(Decl *D) {
Douglas Gregorc4ccf012009-05-10 22:59:12 +00001742 if (!D)
1743 return 0;
1744
Douglas Gregor7da97d02009-05-10 22:57:19 +00001745 if (TagDecl *Tag = dyn_cast<TagDecl>(D)) {
1746 QualType T = getTagDeclType(Tag);
1747 return cast<TagDecl>(cast<TagType>(T.getTypePtr()->CanonicalType)
1748 ->getDecl());
1749 }
1750
1751 if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(D)) {
1752 while (Template->getPreviousDeclaration())
1753 Template = Template->getPreviousDeclaration();
1754 return Template;
1755 }
1756
1757 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
1758 while (Function->getPreviousDeclaration())
1759 Function = Function->getPreviousDeclaration();
1760 return const_cast<FunctionDecl *>(Function);
1761 }
1762
1763 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
1764 while (Var->getPreviousDeclaration())
1765 Var = Var->getPreviousDeclaration();
1766 return const_cast<VarDecl *>(Var);
1767 }
1768
1769 return D;
1770}
1771
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001772TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
1773 // If this template name refers to a template, the canonical
1774 // template name merely stores the template itself.
1775 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Douglas Gregor7da97d02009-05-10 22:57:19 +00001776 return TemplateName(cast<TemplateDecl>(getCanonicalDecl(Template)));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001777
1778 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
1779 assert(DTN && "Non-dependent template names must refer to template decls.");
1780 return DTN->CanonicalTemplateName;
1781}
1782
Douglas Gregord57959a2009-03-27 23:10:48 +00001783NestedNameSpecifier *
1784ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1785 if (!NNS)
1786 return 0;
1787
1788 switch (NNS->getKind()) {
1789 case NestedNameSpecifier::Identifier:
1790 // Canonicalize the prefix but keep the identifier the same.
1791 return NestedNameSpecifier::Create(*this,
1792 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1793 NNS->getAsIdentifier());
1794
1795 case NestedNameSpecifier::Namespace:
1796 // A namespace is canonical; build a nested-name-specifier with
1797 // this namespace and no prefix.
1798 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1799
1800 case NestedNameSpecifier::TypeSpec:
1801 case NestedNameSpecifier::TypeSpecWithTemplate: {
1802 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1803 NestedNameSpecifier *Prefix = 0;
1804
1805 // FIXME: This isn't the right check!
1806 if (T->isDependentType())
1807 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1808
1809 return NestedNameSpecifier::Create(*this, Prefix,
1810 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1811 T.getTypePtr());
1812 }
1813
1814 case NestedNameSpecifier::Global:
1815 // The global specifier is canonical and unique.
1816 return NNS;
1817 }
1818
1819 // Required to silence a GCC warning
1820 return 0;
1821}
1822
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001823
1824const ArrayType *ASTContext::getAsArrayType(QualType T) {
1825 // Handle the non-qualified case efficiently.
1826 if (T.getCVRQualifiers() == 0) {
1827 // Handle the common positive case fast.
1828 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1829 return AT;
1830 }
1831
1832 // Handle the common negative case fast, ignoring CVR qualifiers.
1833 QualType CType = T->getCanonicalTypeInternal();
1834
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001835 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001836 // test.
1837 if (!isa<ArrayType>(CType) &&
1838 !isa<ArrayType>(CType.getUnqualifiedType()))
1839 return 0;
1840
1841 // Apply any CVR qualifiers from the array type to the element type. This
1842 // implements C99 6.7.3p8: "If the specification of an array type includes
1843 // any type qualifiers, the element type is so qualified, not the array type."
1844
1845 // If we get here, we either have type qualifiers on the type, or we have
1846 // sugar such as a typedef in the way. If we have type qualifiers on the type
1847 // we must propagate them down into the elemeng type.
1848 unsigned CVRQuals = T.getCVRQualifiers();
1849 unsigned AddrSpace = 0;
1850 Type *Ty = T.getTypePtr();
1851
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001852 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001853 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001854 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1855 AddrSpace = EXTQT->getAddressSpace();
1856 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001857 } else {
1858 T = Ty->getDesugaredType();
1859 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1860 break;
1861 CVRQuals |= T.getCVRQualifiers();
1862 Ty = T.getTypePtr();
1863 }
1864 }
1865
1866 // If we have a simple case, just return now.
1867 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1868 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1869 return ATy;
1870
1871 // Otherwise, we have an array and we have qualifiers on it. Push the
1872 // qualifiers into the array element type and return a new array type.
1873 // Get the canonical version of the element with the extra qualifiers on it.
1874 // This can recursively sink qualifiers through multiple levels of arrays.
1875 QualType NewEltTy = ATy->getElementType();
1876 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001877 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001878 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1879
1880 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1881 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1882 CAT->getSizeModifier(),
1883 CAT->getIndexTypeQualifier()));
1884 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1885 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1886 IAT->getSizeModifier(),
1887 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001888
Douglas Gregor898574e2008-12-05 23:32:09 +00001889 if (const DependentSizedArrayType *DSAT
1890 = dyn_cast<DependentSizedArrayType>(ATy))
1891 return cast<ArrayType>(
1892 getDependentSizedArrayType(NewEltTy,
1893 DSAT->getSizeExpr(),
1894 DSAT->getSizeModifier(),
1895 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001896
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001897 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1898 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1899 VAT->getSizeModifier(),
1900 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001901}
1902
1903
Chris Lattnere6327742008-04-02 05:18:44 +00001904/// getArrayDecayedType - Return the properly qualified result of decaying the
1905/// specified array type to a pointer. This operation is non-trivial when
1906/// handling typedefs etc. The canonical type of "T" must be an array type,
1907/// this returns a pointer to a properly qualified element of the array.
1908///
1909/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1910QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001911 // Get the element type with 'getAsArrayType' so that we don't lose any
1912 // typedefs in the element type of the array. This also handles propagation
1913 // of type qualifiers from the array type into the element type if present
1914 // (C99 6.7.3p8).
1915 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1916 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001917
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001918 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001919
1920 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001921 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001922}
1923
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001924QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001925 QualType ElemTy = VAT->getElementType();
1926
1927 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1928 return getBaseElementType(VAT);
1929
1930 return ElemTy;
1931}
1932
Reid Spencer5f016e22007-07-11 17:01:13 +00001933/// getFloatingRank - Return a relative rank for floating point types.
1934/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001935static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001936 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001937 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001938
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001939 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001940 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001941 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001942 case BuiltinType::Float: return FloatRank;
1943 case BuiltinType::Double: return DoubleRank;
1944 case BuiltinType::LongDouble: return LongDoubleRank;
1945 }
1946}
1947
Steve Naroff716c7302007-08-27 01:41:48 +00001948/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1949/// point or a complex type (based on typeDomain/typeSize).
1950/// 'typeDomain' is a real floating point or complex type.
1951/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001952QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1953 QualType Domain) const {
1954 FloatingRank EltRank = getFloatingRank(Size);
1955 if (Domain->isComplexType()) {
1956 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001957 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001958 case FloatRank: return FloatComplexTy;
1959 case DoubleRank: return DoubleComplexTy;
1960 case LongDoubleRank: return LongDoubleComplexTy;
1961 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001962 }
Chris Lattner1361b112008-04-06 23:58:54 +00001963
1964 assert(Domain->isRealFloatingType() && "Unknown domain!");
1965 switch (EltRank) {
1966 default: assert(0 && "getFloatingRank(): illegal value for rank");
1967 case FloatRank: return FloatTy;
1968 case DoubleRank: return DoubleTy;
1969 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001970 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001971}
1972
Chris Lattner7cfeb082008-04-06 23:55:33 +00001973/// getFloatingTypeOrder - Compare the rank of the two specified floating
1974/// point types, ignoring the domain of the type (i.e. 'double' ==
1975/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1976/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001977int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1978 FloatingRank LHSR = getFloatingRank(LHS);
1979 FloatingRank RHSR = getFloatingRank(RHS);
1980
1981 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001982 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001983 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001984 return 1;
1985 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001986}
1987
Chris Lattnerf52ab252008-04-06 22:59:24 +00001988/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1989/// routine will assert if passed a built-in type that isn't an integer or enum,
1990/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001991unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001992 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001993 if (EnumType* ET = dyn_cast<EnumType>(T))
1994 T = ET->getDecl()->getIntegerType().getTypePtr();
1995
1996 // There are two things which impact the integer rank: the width, and
1997 // the ordering of builtins. The builtin ordering is encoded in the
1998 // bottom three bits; the width is encoded in the bits above that.
1999 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
2000 return FWIT->getWidth() << 3;
2001 }
2002
Chris Lattnerf52ab252008-04-06 22:59:24 +00002003 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00002004 default: assert(0 && "getIntegerRank(): not a built-in integer");
2005 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002006 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002007 case BuiltinType::Char_S:
2008 case BuiltinType::Char_U:
2009 case BuiltinType::SChar:
2010 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002011 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002012 case BuiltinType::Short:
2013 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002014 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002015 case BuiltinType::Int:
2016 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002017 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002018 case BuiltinType::Long:
2019 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002020 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002021 case BuiltinType::LongLong:
2022 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002023 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00002024 case BuiltinType::Int128:
2025 case BuiltinType::UInt128:
2026 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00002027 }
2028}
2029
Chris Lattner7cfeb082008-04-06 23:55:33 +00002030/// getIntegerTypeOrder - Returns the highest ranked integer type:
2031/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
2032/// LHS < RHS, return -1.
2033int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002034 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2035 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00002036 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002037
Chris Lattnerf52ab252008-04-06 22:59:24 +00002038 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2039 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00002040
Chris Lattner7cfeb082008-04-06 23:55:33 +00002041 unsigned LHSRank = getIntegerRank(LHSC);
2042 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00002043
Chris Lattner7cfeb082008-04-06 23:55:33 +00002044 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2045 if (LHSRank == RHSRank) return 0;
2046 return LHSRank > RHSRank ? 1 : -1;
2047 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002048
Chris Lattner7cfeb082008-04-06 23:55:33 +00002049 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2050 if (LHSUnsigned) {
2051 // If the unsigned [LHS] type is larger, return it.
2052 if (LHSRank >= RHSRank)
2053 return 1;
2054
2055 // If the signed type can represent all values of the unsigned type, it
2056 // wins. Because we are dealing with 2's complement and types that are
2057 // powers of two larger than each other, this is always safe.
2058 return -1;
2059 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002060
Chris Lattner7cfeb082008-04-06 23:55:33 +00002061 // If the unsigned [RHS] type is larger, return it.
2062 if (RHSRank >= LHSRank)
2063 return -1;
2064
2065 // If the signed type can represent all values of the unsigned type, it
2066 // wins. Because we are dealing with 2's complement and types that are
2067 // powers of two larger than each other, this is always safe.
2068 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002069}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002070
2071// getCFConstantStringType - Return the type used for constant CFStrings.
2072QualType ASTContext::getCFConstantStringType() {
2073 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00002074 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002075 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002076 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002077 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00002078
2079 // const int *isa;
2080 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002081 // int flags;
2082 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002083 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002084 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00002085 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002086 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00002087
Anders Carlsson71993dd2007-08-17 05:31:46 +00002088 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002089 for (unsigned i = 0; i < 4; ++i) {
2090 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2091 SourceLocation(), 0,
2092 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002093 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002094 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002095 }
2096
2097 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002098 }
2099
2100 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002101}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002102
Douglas Gregor319ac892009-04-23 22:29:11 +00002103void ASTContext::setCFConstantStringType(QualType T) {
2104 const RecordType *Rec = T->getAsRecordType();
2105 assert(Rec && "Invalid CFConstantStringType");
2106 CFConstantStringTypeDecl = Rec->getDecl();
2107}
2108
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002109QualType ASTContext::getObjCFastEnumerationStateType()
2110{
2111 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002112 ObjCFastEnumerationStateTypeDecl =
2113 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2114 &Idents.get("__objcFastEnumerationState"));
2115
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002116 QualType FieldTypes[] = {
2117 UnsignedLongTy,
2118 getPointerType(ObjCIdType),
2119 getPointerType(UnsignedLongTy),
2120 getConstantArrayType(UnsignedLongTy,
2121 llvm::APInt(32, 5), ArrayType::Normal, 0)
2122 };
2123
Douglas Gregor44b43212008-12-11 16:49:14 +00002124 for (size_t i = 0; i < 4; ++i) {
2125 FieldDecl *Field = FieldDecl::Create(*this,
2126 ObjCFastEnumerationStateTypeDecl,
2127 SourceLocation(), 0,
2128 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002129 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002130 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002131 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002132
Douglas Gregor44b43212008-12-11 16:49:14 +00002133 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002134 }
2135
2136 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2137}
2138
Douglas Gregor319ac892009-04-23 22:29:11 +00002139void ASTContext::setObjCFastEnumerationStateType(QualType T) {
2140 const RecordType *Rec = T->getAsRecordType();
2141 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2142 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2143}
2144
Anders Carlssone8c49532007-10-29 06:33:42 +00002145// This returns true if a type has been typedefed to BOOL:
2146// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002147static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002148 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002149 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2150 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002151
2152 return false;
2153}
2154
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002155/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002156/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002157int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002158 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002159
2160 // Make all integer and enum types at least as large as an int
2161 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002162 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002163 // Treat arrays as pointers, since that's how they're passed in.
2164 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002165 sz = getTypeSize(VoidPtrTy);
2166 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002167}
2168
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002169/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002170/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002171void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002172 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002173 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002174 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002175 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002176 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002177 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002178 // Compute size of all parameters.
2179 // Start with computing size of a pointer in number of bytes.
2180 // FIXME: There might(should) be a better way of doing this computation!
2181 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002182 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002183 // The first two arguments (self and _cmd) are pointers; account for
2184 // their size.
2185 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002186 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2187 E = Decl->param_end(); PI != E; ++PI) {
2188 QualType PType = (*PI)->getType();
2189 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002190 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002191 ParmOffset += sz;
2192 }
2193 S += llvm::utostr(ParmOffset);
2194 S += "@0:";
2195 S += llvm::utostr(PtrSize);
2196
2197 // Argument types.
2198 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002199 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2200 E = Decl->param_end(); PI != E; ++PI) {
2201 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002202 QualType PType = PVDecl->getOriginalType();
2203 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002204 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2205 // Use array's original type only if it has known number of
2206 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002207 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002208 PType = PVDecl->getType();
2209 } else if (PType->isFunctionType())
2210 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002211 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002212 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002213 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002214 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002215 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002216 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002217 }
2218}
2219
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002220/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002221/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002222/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2223/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002224/// Property attributes are stored as a comma-delimited C string. The simple
2225/// attributes readonly and bycopy are encoded as single characters. The
2226/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2227/// encoded as single characters, followed by an identifier. Property types
2228/// are also encoded as a parametrized attribute. The characters used to encode
2229/// these attributes are defined by the following enumeration:
2230/// @code
2231/// enum PropertyAttributes {
2232/// kPropertyReadOnly = 'R', // property is read-only.
2233/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2234/// kPropertyByref = '&', // property is a reference to the value last assigned
2235/// kPropertyDynamic = 'D', // property is dynamic
2236/// kPropertyGetter = 'G', // followed by getter selector name
2237/// kPropertySetter = 'S', // followed by setter selector name
2238/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2239/// kPropertyType = 't' // followed by old-style type encoding.
2240/// kPropertyWeak = 'W' // 'weak' property
2241/// kPropertyStrong = 'P' // property GC'able
2242/// kPropertyNonAtomic = 'N' // property non-atomic
2243/// };
2244/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002245void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2246 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002247 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002248 // Collect information from the property implementation decl(s).
2249 bool Dynamic = false;
2250 ObjCPropertyImplDecl *SynthesizePID = 0;
2251
2252 // FIXME: Duplicated code due to poor abstraction.
2253 if (Container) {
2254 if (const ObjCCategoryImplDecl *CID =
2255 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2256 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002257 i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2258 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002259 ObjCPropertyImplDecl *PID = *i;
2260 if (PID->getPropertyDecl() == PD) {
2261 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2262 Dynamic = true;
2263 } else {
2264 SynthesizePID = PID;
2265 }
2266 }
2267 }
2268 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002269 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002270 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002271 i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2272 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002273 ObjCPropertyImplDecl *PID = *i;
2274 if (PID->getPropertyDecl() == PD) {
2275 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2276 Dynamic = true;
2277 } else {
2278 SynthesizePID = PID;
2279 }
2280 }
2281 }
2282 }
2283 }
2284
2285 // FIXME: This is not very efficient.
2286 S = "T";
2287
2288 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002289 // GCC has some special rules regarding encoding of properties which
2290 // closely resembles encoding of ivars.
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002291 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002292 true /* outermost type */,
2293 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002294
2295 if (PD->isReadOnly()) {
2296 S += ",R";
2297 } else {
2298 switch (PD->getSetterKind()) {
2299 case ObjCPropertyDecl::Assign: break;
2300 case ObjCPropertyDecl::Copy: S += ",C"; break;
2301 case ObjCPropertyDecl::Retain: S += ",&"; break;
2302 }
2303 }
2304
2305 // It really isn't clear at all what this means, since properties
2306 // are "dynamic by default".
2307 if (Dynamic)
2308 S += ",D";
2309
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002310 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2311 S += ",N";
2312
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002313 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2314 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002315 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002316 }
2317
2318 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2319 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002320 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002321 }
2322
2323 if (SynthesizePID) {
2324 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2325 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002326 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002327 }
2328
2329 // FIXME: OBJCGC: weak & strong
2330}
2331
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002332/// getLegacyIntegralTypeEncoding -
2333/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002334/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002335/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2336///
2337void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2338 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2339 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002340 if (BT->getKind() == BuiltinType::ULong &&
2341 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002342 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002343 else
2344 if (BT->getKind() == BuiltinType::Long &&
2345 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002346 PointeeTy = IntTy;
2347 }
2348 }
2349}
2350
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002351void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002352 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002353 // We follow the behavior of gcc, expanding structures which are
2354 // directly pointed to, and expanding embedded structures. Note that
2355 // these rules are sufficient to prevent recursive encoding of the
2356 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002357 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2358 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002359}
2360
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002361static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002362 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002363 const Expr *E = FD->getBitWidth();
2364 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2365 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002366 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002367 S += 'b';
2368 S += llvm::utostr(N);
2369}
2370
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002371void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2372 bool ExpandPointedToStructures,
2373 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002374 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002375 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002376 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002377 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002378 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002379 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002380 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002381 else {
2382 char encoding;
2383 switch (BT->getKind()) {
2384 default: assert(0 && "Unhandled builtin type kind");
2385 case BuiltinType::Void: encoding = 'v'; break;
2386 case BuiltinType::Bool: encoding = 'B'; break;
2387 case BuiltinType::Char_U:
2388 case BuiltinType::UChar: encoding = 'C'; break;
2389 case BuiltinType::UShort: encoding = 'S'; break;
2390 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002391 case BuiltinType::ULong:
2392 encoding =
2393 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2394 break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002395 case BuiltinType::UInt128: encoding = 'T'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002396 case BuiltinType::ULongLong: encoding = 'Q'; break;
2397 case BuiltinType::Char_S:
2398 case BuiltinType::SChar: encoding = 'c'; break;
2399 case BuiltinType::Short: encoding = 's'; break;
2400 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002401 case BuiltinType::Long:
2402 encoding =
2403 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2404 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002405 case BuiltinType::LongLong: encoding = 'q'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002406 case BuiltinType::Int128: encoding = 't'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002407 case BuiltinType::Float: encoding = 'f'; break;
2408 case BuiltinType::Double: encoding = 'd'; break;
2409 case BuiltinType::LongDouble: encoding = 'd'; break;
2410 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002411
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002412 S += encoding;
2413 }
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002414 } else if (const ComplexType *CT = T->getAsComplexType()) {
2415 S += 'j';
2416 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2417 false);
2418 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002419 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2420 ExpandPointedToStructures,
2421 ExpandStructures, FD);
2422 if (FD || EncodingProperty) {
2423 // Note that we do extended encoding of protocol qualifer list
2424 // Only when doing ivar or property encoding.
2425 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2426 S += '"';
2427 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2428 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2429 S += '<';
2430 S += Proto->getNameAsString();
2431 S += '>';
2432 }
2433 S += '"';
2434 }
2435 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002436 }
2437 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002438 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002439 bool isReadOnly = false;
2440 // For historical/compatibility reasons, the read-only qualifier of the
2441 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2442 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2443 // Also, do not emit the 'r' for anything but the outermost type!
2444 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2445 if (OutermostType && T.isConstQualified()) {
2446 isReadOnly = true;
2447 S += 'r';
2448 }
2449 }
2450 else if (OutermostType) {
2451 QualType P = PointeeTy;
2452 while (P->getAsPointerType())
2453 P = P->getAsPointerType()->getPointeeType();
2454 if (P.isConstQualified()) {
2455 isReadOnly = true;
2456 S += 'r';
2457 }
2458 }
2459 if (isReadOnly) {
2460 // Another legacy compatibility encoding. Some ObjC qualifier and type
2461 // combinations need to be rearranged.
2462 // Rewrite "in const" from "nr" to "rn"
2463 const char * s = S.c_str();
2464 int len = S.length();
2465 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2466 std::string replace = "rn";
2467 S.replace(S.end()-2, S.end(), replace);
2468 }
2469 }
Steve Naroff389bf462009-02-12 17:52:19 +00002470 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002471 S += '@';
2472 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002473 }
2474 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002475 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002476 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002477 // Another historical/compatibility reason.
2478 // We encode the underlying type which comes out as
2479 // {...};
2480 S += '^';
2481 getObjCEncodingForTypeImpl(PointeeTy, S,
2482 false, ExpandPointedToStructures,
2483 NULL);
2484 return;
2485 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002486 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002487 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002488 const ObjCInterfaceType *OIT =
2489 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002490 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002491 S += '"';
2492 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002493 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2494 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2495 S += '<';
2496 S += Proto->getNameAsString();
2497 S += '>';
2498 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002499 S += '"';
2500 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002501 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002502 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002503 S += '#';
2504 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002505 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002506 S += ':';
2507 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002508 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002509
2510 if (PointeeTy->isCharType()) {
2511 // char pointer types should be encoded as '*' unless it is a
2512 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002513 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002514 S += '*';
2515 return;
2516 }
2517 }
2518
2519 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002520 getLegacyIntegralTypeEncoding(PointeeTy);
2521
2522 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002523 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002524 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002525 } else if (const ArrayType *AT =
2526 // Ignore type qualifiers etc.
2527 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002528 if (isa<IncompleteArrayType>(AT)) {
2529 // Incomplete arrays are encoded as a pointer to the array element.
2530 S += '^';
2531
2532 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2533 false, ExpandStructures, FD);
2534 } else {
2535 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002536
Anders Carlsson559a8332009-02-22 01:38:57 +00002537 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2538 S += llvm::utostr(CAT->getSize().getZExtValue());
2539 else {
2540 //Variable length arrays are encoded as a regular array with 0 elements.
2541 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2542 S += '0';
2543 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002544
Anders Carlsson559a8332009-02-22 01:38:57 +00002545 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2546 false, ExpandStructures, FD);
2547 S += ']';
2548 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002549 } else if (T->getAsFunctionType()) {
2550 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002551 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002552 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002553 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002554 // Anonymous structures print as '?'
2555 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2556 S += II->getName();
2557 } else {
2558 S += '?';
2559 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002560 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002561 S += '=';
Douglas Gregor6ab35242009-04-09 21:40:53 +00002562 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2563 FieldEnd = RDecl->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +00002564 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002565 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002566 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002567 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002568 S += '"';
2569 }
2570
2571 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002572 if (Field->isBitField()) {
2573 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2574 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002575 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002576 QualType qt = Field->getType();
2577 getLegacyIntegralTypeEncoding(qt);
2578 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002579 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002580 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002581 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002582 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002583 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002584 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002585 if (FD && FD->isBitField())
2586 EncodeBitField(this, S, FD);
2587 else
2588 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002589 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002590 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002591 } else if (T->isObjCInterfaceType()) {
2592 // @encode(class_name)
2593 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2594 S += '{';
2595 const IdentifierInfo *II = OI->getIdentifier();
2596 S += II->getName();
2597 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002598 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002599 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002600 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002601 if (RecFields[i]->isBitField())
2602 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2603 RecFields[i]);
2604 else
2605 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2606 FD);
2607 }
2608 S += '}';
2609 }
2610 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002611 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002612}
2613
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002614void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002615 std::string& S) const {
2616 if (QT & Decl::OBJC_TQ_In)
2617 S += 'n';
2618 if (QT & Decl::OBJC_TQ_Inout)
2619 S += 'N';
2620 if (QT & Decl::OBJC_TQ_Out)
2621 S += 'o';
2622 if (QT & Decl::OBJC_TQ_Bycopy)
2623 S += 'O';
2624 if (QT & Decl::OBJC_TQ_Byref)
2625 S += 'R';
2626 if (QT & Decl::OBJC_TQ_Oneway)
2627 S += 'V';
2628}
2629
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002630void ASTContext::setBuiltinVaListType(QualType T)
2631{
2632 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2633
2634 BuiltinVaListType = T;
2635}
2636
Douglas Gregor319ac892009-04-23 22:29:11 +00002637void ASTContext::setObjCIdType(QualType T)
Steve Naroff7e219e42007-10-15 14:41:52 +00002638{
Douglas Gregor319ac892009-04-23 22:29:11 +00002639 ObjCIdType = T;
2640
2641 const TypedefType *TT = T->getAsTypedefType();
2642 if (!TT)
2643 return;
2644
2645 TypedefDecl *TD = TT->getDecl();
Steve Naroff7e219e42007-10-15 14:41:52 +00002646
2647 // typedef struct objc_object *id;
2648 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002649 // User error - caller will issue diagnostics.
2650 if (!ptr)
2651 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002652 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002653 // User error - caller will issue diagnostics.
2654 if (!rec)
2655 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002656 IdStructType = rec;
2657}
2658
Douglas Gregor319ac892009-04-23 22:29:11 +00002659void ASTContext::setObjCSelType(QualType T)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002660{
Douglas Gregor319ac892009-04-23 22:29:11 +00002661 ObjCSelType = T;
2662
2663 const TypedefType *TT = T->getAsTypedefType();
2664 if (!TT)
2665 return;
2666 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002667
2668 // typedef struct objc_selector *SEL;
2669 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002670 if (!ptr)
2671 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002672 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002673 if (!rec)
2674 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002675 SelStructType = rec;
2676}
2677
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002678void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002679{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002680 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002681}
2682
Douglas Gregor319ac892009-04-23 22:29:11 +00002683void ASTContext::setObjCClassType(QualType T)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002684{
Douglas Gregor319ac892009-04-23 22:29:11 +00002685 ObjCClassType = T;
2686
2687 const TypedefType *TT = T->getAsTypedefType();
2688 if (!TT)
2689 return;
2690 TypedefDecl *TD = TT->getDecl();
Anders Carlsson8baaca52007-10-31 02:53:19 +00002691
2692 // typedef struct objc_class *Class;
2693 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2694 assert(ptr && "'Class' incorrectly typed");
2695 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2696 assert(rec && "'Class' incorrectly typed");
2697 ClassStructType = rec;
2698}
2699
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002700void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2701 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002702 "'NSConstantString' type already set!");
2703
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002704 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002705}
2706
Douglas Gregor7532dc62009-03-30 22:58:21 +00002707/// \brief Retrieve the template name that represents a qualified
2708/// template name such as \c std::vector.
2709TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2710 bool TemplateKeyword,
2711 TemplateDecl *Template) {
2712 llvm::FoldingSetNodeID ID;
2713 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2714
2715 void *InsertPos = 0;
2716 QualifiedTemplateName *QTN =
2717 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2718 if (!QTN) {
2719 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2720 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2721 }
2722
2723 return TemplateName(QTN);
2724}
2725
2726/// \brief Retrieve the template name that represents a dependent
2727/// template name such as \c MetaFun::template apply.
2728TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2729 const IdentifierInfo *Name) {
2730 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2731
2732 llvm::FoldingSetNodeID ID;
2733 DependentTemplateName::Profile(ID, NNS, Name);
2734
2735 void *InsertPos = 0;
2736 DependentTemplateName *QTN =
2737 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2738
2739 if (QTN)
2740 return TemplateName(QTN);
2741
2742 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2743 if (CanonNNS == NNS) {
2744 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2745 } else {
2746 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2747 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2748 }
2749
2750 DependentTemplateNames.InsertNode(QTN, InsertPos);
2751 return TemplateName(QTN);
2752}
2753
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002754/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002755/// TargetInfo, produce the corresponding type. The unsigned @p Type
2756/// is actually a value of type @c TargetInfo::IntType.
2757QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002758 switch (Type) {
2759 case TargetInfo::NoInt: return QualType();
2760 case TargetInfo::SignedShort: return ShortTy;
2761 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2762 case TargetInfo::SignedInt: return IntTy;
2763 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2764 case TargetInfo::SignedLong: return LongTy;
2765 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2766 case TargetInfo::SignedLongLong: return LongLongTy;
2767 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2768 }
2769
2770 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002771 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002772}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002773
2774//===----------------------------------------------------------------------===//
2775// Type Predicates.
2776//===----------------------------------------------------------------------===//
2777
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002778/// isObjCNSObjectType - Return true if this is an NSObject object using
2779/// NSObject attribute on a c-style pointer type.
2780/// FIXME - Make it work directly on types.
2781///
2782bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2783 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2784 if (TypedefDecl *TD = TDT->getDecl())
2785 if (TD->getAttr<ObjCNSObjectAttr>())
2786 return true;
2787 }
2788 return false;
2789}
2790
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002791/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2792/// to an object type. This includes "id" and "Class" (two 'special' pointers
2793/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2794/// ID type).
2795bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002796 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002797 return true;
2798
Steve Naroff6ae98502008-10-21 18:24:04 +00002799 // Blocks are objects.
2800 if (Ty->isBlockPointerType())
2801 return true;
2802
2803 // All other object types are pointers.
Chris Lattner16ede0e2009-04-12 23:51:02 +00002804 const PointerType *PT = Ty->getAsPointerType();
2805 if (PT == 0)
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002806 return false;
2807
Chris Lattner16ede0e2009-04-12 23:51:02 +00002808 // If this a pointer to an interface (e.g. NSString*), it is ok.
2809 if (PT->getPointeeType()->isObjCInterfaceType() ||
2810 // If is has NSObject attribute, OK as well.
2811 isObjCNSObjectType(Ty))
2812 return true;
2813
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002814 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2815 // pointer types. This looks for the typedef specifically, not for the
Chris Lattner16ede0e2009-04-12 23:51:02 +00002816 // underlying type. Iteratively strip off typedefs so that we can handle
2817 // typedefs of typedefs.
2818 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2819 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2820 Ty.getUnqualifiedType() == getObjCClassType())
2821 return true;
2822
2823 Ty = TDT->getDecl()->getUnderlyingType();
2824 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002825
Chris Lattner16ede0e2009-04-12 23:51:02 +00002826 return false;
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002827}
2828
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002829/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2830/// garbage collection attribute.
2831///
2832QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002833 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002834 if (getLangOptions().ObjC1 &&
2835 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002836 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002837 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002838 // (or pointers to them) be treated as though they were declared
2839 // as __strong.
2840 if (GCAttrs == QualType::GCNone) {
2841 if (isObjCObjectPointerType(Ty))
2842 GCAttrs = QualType::Strong;
2843 else if (Ty->isPointerType())
2844 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2845 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00002846 // Non-pointers have none gc'able attribute regardless of the attribute
2847 // set on them.
2848 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2849 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002850 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002851 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002852}
2853
Chris Lattner6ac46a42008-04-07 06:51:04 +00002854//===----------------------------------------------------------------------===//
2855// Type Compatibility Testing
2856//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002857
Steve Naroff1c7d0672008-09-04 15:10:53 +00002858/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002859/// block types. Types must be strictly compatible here. For example,
2860/// C unfortunately doesn't produce an error for the following:
2861///
2862/// int (*emptyArgFunc)();
2863/// int (*intArgList)(int) = emptyArgFunc;
2864///
2865/// For blocks, we will produce an error for the following (similar to C++):
2866///
2867/// int (^emptyArgBlock)();
2868/// int (^intArgBlock)(int) = emptyArgBlock;
2869///
2870/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2871///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002872bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002873 const FunctionType *lbase = lhs->getAsFunctionType();
2874 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002875 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2876 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stumpaab0f7a2009-04-01 01:17:39 +00002877 if (lproto && rproto == 0)
2878 return false;
2879 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff1c7d0672008-09-04 15:10:53 +00002880}
2881
Chris Lattner6ac46a42008-04-07 06:51:04 +00002882/// areCompatVectorTypes - Return true if the two specified vector types are
2883/// compatible.
2884static bool areCompatVectorTypes(const VectorType *LHS,
2885 const VectorType *RHS) {
2886 assert(LHS->isCanonical() && RHS->isCanonical());
2887 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002888 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002889}
2890
Eli Friedman3d815e72008-08-22 00:56:42 +00002891/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002892/// compatible for assignment from RHS to LHS. This handles validation of any
2893/// protocol qualifiers on the LHS or RHS.
2894///
Eli Friedman3d815e72008-08-22 00:56:42 +00002895bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2896 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002897 // Verify that the base decls are compatible: the RHS must be a subclass of
2898 // the LHS.
2899 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2900 return false;
2901
2902 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2903 // protocol qualified at all, then we are good.
2904 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2905 return true;
2906
2907 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2908 // isn't a superset.
2909 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2910 return true; // FIXME: should return false!
2911
2912 // Finally, we must have two protocol-qualified interfaces.
2913 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2914 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002915
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002916 // All LHS protocols must have a presence on the RHS.
2917 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002918
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002919 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2920 LHSPE = LHSP->qual_end();
2921 LHSPI != LHSPE; LHSPI++) {
2922 bool RHSImplementsProtocol = false;
2923
2924 // If the RHS doesn't implement the protocol on the left, the types
2925 // are incompatible.
2926 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2927 RHSPE = RHSP->qual_end();
2928 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2929 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2930 RHSImplementsProtocol = true;
2931 }
2932 // FIXME: For better diagnostics, consider passing back the protocol name.
2933 if (!RHSImplementsProtocol)
2934 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002935 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002936 // The RHS implements all protocols listed on the LHS.
2937 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002938}
2939
Steve Naroff389bf462009-02-12 17:52:19 +00002940bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2941 // get the "pointed to" types
2942 const PointerType *LHSPT = LHS->getAsPointerType();
2943 const PointerType *RHSPT = RHS->getAsPointerType();
2944
2945 if (!LHSPT || !RHSPT)
2946 return false;
2947
2948 QualType lhptee = LHSPT->getPointeeType();
2949 QualType rhptee = RHSPT->getPointeeType();
2950 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2951 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2952 // ID acts sort of like void* for ObjC interfaces
2953 if (LHSIface && isObjCIdStructType(rhptee))
2954 return true;
2955 if (RHSIface && isObjCIdStructType(lhptee))
2956 return true;
2957 if (!LHSIface || !RHSIface)
2958 return false;
2959 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2960 canAssignObjCInterfaces(RHSIface, LHSIface);
2961}
2962
Steve Naroffec0550f2007-10-15 20:41:53 +00002963/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2964/// both shall have the identically qualified version of a compatible type.
2965/// C99 6.2.7p1: Two types have compatible types if their types are the
2966/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002967bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2968 return !mergeTypes(LHS, RHS).isNull();
2969}
2970
2971QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2972 const FunctionType *lbase = lhs->getAsFunctionType();
2973 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002974 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2975 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002976 bool allLTypes = true;
2977 bool allRTypes = true;
2978
2979 // Check return type
2980 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2981 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002982 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2983 allLTypes = false;
2984 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2985 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002986
2987 if (lproto && rproto) { // two C99 style function prototypes
2988 unsigned lproto_nargs = lproto->getNumArgs();
2989 unsigned rproto_nargs = rproto->getNumArgs();
2990
2991 // Compatible functions must have the same number of arguments
2992 if (lproto_nargs != rproto_nargs)
2993 return QualType();
2994
2995 // Variadic and non-variadic functions aren't compatible
2996 if (lproto->isVariadic() != rproto->isVariadic())
2997 return QualType();
2998
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002999 if (lproto->getTypeQuals() != rproto->getTypeQuals())
3000 return QualType();
3001
Eli Friedman3d815e72008-08-22 00:56:42 +00003002 // Check argument compatibility
3003 llvm::SmallVector<QualType, 10> types;
3004 for (unsigned i = 0; i < lproto_nargs; i++) {
3005 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
3006 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
3007 QualType argtype = mergeTypes(largtype, rargtype);
3008 if (argtype.isNull()) return QualType();
3009 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00003010 if (getCanonicalType(argtype) != getCanonicalType(largtype))
3011 allLTypes = false;
3012 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
3013 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00003014 }
3015 if (allLTypes) return lhs;
3016 if (allRTypes) return rhs;
3017 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003018 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00003019 }
3020
3021 if (lproto) allRTypes = false;
3022 if (rproto) allLTypes = false;
3023
Douglas Gregor72564e72009-02-26 23:50:07 +00003024 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00003025 if (proto) {
3026 if (proto->isVariadic()) return QualType();
3027 // Check that the types are compatible with the types that
3028 // would result from default argument promotions (C99 6.7.5.3p15).
3029 // The only types actually affected are promotable integer
3030 // types and floats, which would be passed as a different
3031 // type depending on whether the prototype is visible.
3032 unsigned proto_nargs = proto->getNumArgs();
3033 for (unsigned i = 0; i < proto_nargs; ++i) {
3034 QualType argTy = proto->getArgType(i);
3035 if (argTy->isPromotableIntegerType() ||
3036 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
3037 return QualType();
3038 }
3039
3040 if (allLTypes) return lhs;
3041 if (allRTypes) return rhs;
3042 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003043 proto->getNumArgs(), lproto->isVariadic(),
3044 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00003045 }
3046
3047 if (allLTypes) return lhs;
3048 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00003049 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00003050}
3051
3052QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00003053 // C++ [expr]: If an expression initially has the type "reference to T", the
3054 // type is adjusted to "T" prior to any further analysis, the expression
3055 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003056 // expression is an lvalue unless the reference is an rvalue reference and
3057 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00003058 // FIXME: C++ shouldn't be going through here! The rules are different
3059 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003060 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3061 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00003062 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003063 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00003064 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003065 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00003066
Eli Friedman3d815e72008-08-22 00:56:42 +00003067 QualType LHSCan = getCanonicalType(LHS),
3068 RHSCan = getCanonicalType(RHS);
3069
3070 // If two types are identical, they are compatible.
3071 if (LHSCan == RHSCan)
3072 return LHS;
3073
3074 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003075 // Note that we handle extended qualifiers later, in the
3076 // case for ExtQualType.
3077 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00003078 return QualType();
3079
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003080 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
3081 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003082
Chris Lattner1adb8832008-01-14 05:45:46 +00003083 // We want to consider the two function types to be the same for these
3084 // comparisons, just force one to the other.
3085 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3086 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003087
3088 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003089 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3090 LHSClass = Type::ConstantArray;
3091 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3092 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00003093
Nate Begeman213541a2008-04-18 23:10:10 +00003094 // Canonicalize ExtVector -> Vector.
3095 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3096 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00003097
Chris Lattnerb0489812008-04-07 06:38:24 +00003098 // Consider qualified interfaces and interfaces the same.
3099 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
3100 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00003101
Chris Lattnera36a61f2008-04-07 05:43:21 +00003102 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003103 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003104 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3105 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003106
Steve Naroffd824c9c2009-04-14 15:11:46 +00003107 // 'id' and 'Class' act sort of like void* for ObjC interfaces
3108 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003109 return LHS;
Steve Naroffd824c9c2009-04-14 15:11:46 +00003110 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003111 return RHS;
3112
Steve Naroffbc76dd02008-12-10 22:14:21 +00003113 // ID is compatible with all qualified id types.
3114 if (LHS->isObjCQualifiedIdType()) {
3115 if (const PointerType *PT = RHS->getAsPointerType()) {
3116 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003117 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003118 return LHS;
3119 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3120 // Unfortunately, this API is part of Sema (which we don't have access
3121 // to. Need to refactor. The following check is insufficient, since we
3122 // need to make sure the class implements the protocol.
3123 if (pType->isObjCInterfaceType())
3124 return LHS;
3125 }
3126 }
3127 if (RHS->isObjCQualifiedIdType()) {
3128 if (const PointerType *PT = LHS->getAsPointerType()) {
3129 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003130 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003131 return RHS;
3132 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3133 // Unfortunately, this API is part of Sema (which we don't have access
3134 // to. Need to refactor. The following check is insufficient, since we
3135 // need to make sure the class implements the protocol.
3136 if (pType->isObjCInterfaceType())
3137 return RHS;
3138 }
3139 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003140 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3141 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00003142 if (const EnumType* ETy = LHS->getAsEnumType()) {
3143 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3144 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003145 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003146 if (const EnumType* ETy = RHS->getAsEnumType()) {
3147 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3148 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003149 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003150
Eli Friedman3d815e72008-08-22 00:56:42 +00003151 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003152 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003153
Steve Naroff4a746782008-01-09 22:43:08 +00003154 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003155 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003156#define TYPE(Class, Base)
3157#define ABSTRACT_TYPE(Class, Base)
3158#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3159#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3160#include "clang/AST/TypeNodes.def"
3161 assert(false && "Non-canonical and dependent types shouldn't get here");
3162 return QualType();
3163
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003164 case Type::LValueReference:
3165 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003166 case Type::MemberPointer:
3167 assert(false && "C++ should never be in mergeTypes");
3168 return QualType();
3169
3170 case Type::IncompleteArray:
3171 case Type::VariableArray:
3172 case Type::FunctionProto:
3173 case Type::ExtVector:
3174 case Type::ObjCQualifiedInterface:
3175 assert(false && "Types are eliminated above");
3176 return QualType();
3177
Chris Lattner1adb8832008-01-14 05:45:46 +00003178 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003179 {
3180 // Merge two pointer types, while trying to preserve typedef info
3181 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3182 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3183 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3184 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003185 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3186 return LHS;
3187 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3188 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003189 return getPointerType(ResultType);
3190 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003191 case Type::BlockPointer:
3192 {
3193 // Merge two block pointer types, while trying to preserve typedef info
3194 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3195 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3196 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3197 if (ResultType.isNull()) return QualType();
3198 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3199 return LHS;
3200 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3201 return RHS;
3202 return getBlockPointerType(ResultType);
3203 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003204 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003205 {
3206 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3207 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3208 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3209 return QualType();
3210
3211 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3212 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3213 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3214 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003215 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3216 return LHS;
3217 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3218 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003219 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3220 ArrayType::ArraySizeModifier(), 0);
3221 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3222 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003223 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3224 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003225 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3226 return LHS;
3227 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3228 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003229 if (LVAT) {
3230 // FIXME: This isn't correct! But tricky to implement because
3231 // the array's size has to be the size of LHS, but the type
3232 // has to be different.
3233 return LHS;
3234 }
3235 if (RVAT) {
3236 // FIXME: This isn't correct! But tricky to implement because
3237 // the array's size has to be the size of RHS, but the type
3238 // has to be different.
3239 return RHS;
3240 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003241 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3242 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003243 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003244 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003245 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003246 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003247 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003248 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003249 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003250 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3251 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003252 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003253 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003254 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003255 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003256 case Type::Complex:
3257 // Distinct complex types are incompatible.
3258 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003259 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003260 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003261 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3262 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003263 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003264 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003265 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003266 // FIXME: This should be type compatibility, e.g. whether
3267 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003268 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3269 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3270 if (LHSIface && RHSIface &&
3271 canAssignObjCInterfaces(LHSIface, RHSIface))
3272 return LHS;
3273
Eli Friedman3d815e72008-08-22 00:56:42 +00003274 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003275 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003276 case Type::ObjCQualifiedId:
3277 // Distinct qualified id's are not compatible.
3278 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003279 case Type::FixedWidthInt:
3280 // Distinct fixed-width integers are not compatible.
3281 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003282 case Type::ExtQual:
3283 // FIXME: ExtQual types can be compatible even if they're not
3284 // identical!
3285 return QualType();
3286 // First attempt at an implementation, but I'm not really sure it's
3287 // right...
3288#if 0
3289 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3290 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3291 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3292 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3293 return QualType();
3294 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3295 LHSBase = QualType(LQual->getBaseType(), 0);
3296 RHSBase = QualType(RQual->getBaseType(), 0);
3297 ResultType = mergeTypes(LHSBase, RHSBase);
3298 if (ResultType.isNull()) return QualType();
3299 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3300 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3301 return LHS;
3302 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3303 return RHS;
3304 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3305 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3306 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3307 return ResultType;
3308#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003309
3310 case Type::TemplateSpecialization:
3311 assert(false && "Dependent types have no size");
3312 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003313 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003314
3315 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003316}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003317
Chris Lattner5426bf62008-04-07 07:01:58 +00003318//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003319// Integer Predicates
3320//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003321
Eli Friedmanad74a752008-06-28 06:23:08 +00003322unsigned ASTContext::getIntWidth(QualType T) {
3323 if (T == BoolTy)
3324 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003325 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3326 return FWIT->getWidth();
3327 }
3328 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003329 return (unsigned)getTypeSize(T);
3330}
3331
3332QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3333 assert(T->isSignedIntegerType() && "Unexpected type");
3334 if (const EnumType* ETy = T->getAsEnumType())
3335 T = ETy->getDecl()->getIntegerType();
3336 const BuiltinType* BTy = T->getAsBuiltinType();
3337 assert (BTy && "Unexpected signed integer type");
3338 switch (BTy->getKind()) {
3339 case BuiltinType::Char_S:
3340 case BuiltinType::SChar:
3341 return UnsignedCharTy;
3342 case BuiltinType::Short:
3343 return UnsignedShortTy;
3344 case BuiltinType::Int:
3345 return UnsignedIntTy;
3346 case BuiltinType::Long:
3347 return UnsignedLongTy;
3348 case BuiltinType::LongLong:
3349 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00003350 case BuiltinType::Int128:
3351 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00003352 default:
3353 assert(0 && "Unexpected signed integer type");
3354 return QualType();
3355 }
3356}
3357
Douglas Gregor2cf26342009-04-09 22:27:44 +00003358ExternalASTSource::~ExternalASTSource() { }
3359
3360void ExternalASTSource::PrintStats() { }