blob: 3342ca5bc9d08b513aac05f3e5631d34f1a2b11f [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
74 {
Chris Lattner23499252009-03-31 09:24:30 +000075 llvm::DenseMap<const ObjCInterfaceDecl*, RecordDecl*>::iterator
Nuno Lopesb74668e2008-12-17 22:30:25 +000076 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
77 while (I != E) {
Chris Lattner23499252009-03-31 09:24:30 +000078 RecordDecl *R = (I++)->second;
Nuno Lopesb74668e2008-12-17 22:30:25 +000079 R->Destroy(*this);
80 }
81 }
82
Douglas Gregorab452ba2009-03-26 23:50:42 +000083 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000084 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
85 NNS = NestedNameSpecifiers.begin(),
86 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregore7dcd782009-03-27 23:25:45 +000087 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000088 /* Increment in loop */)
89 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000090
91 if (GlobalNestedNameSpecifier)
92 GlobalNestedNameSpecifier->Destroy(*this);
93
Eli Friedmanb26153c2008-05-27 03:08:09 +000094 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000095}
96
Douglas Gregor2deaea32009-04-22 18:49:13 +000097void ASTContext::InitializeBuiltins(IdentifierTable &idents) {
Douglas Gregor2deaea32009-04-22 18:49:13 +000098 BuiltinInfo.InitializeBuiltins(idents, LangOpts.NoBuiltin);
99}
100
Douglas Gregor2cf26342009-04-09 22:27:44 +0000101void
102ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
103 ExternalSource.reset(Source.take());
104}
105
Reid Spencer5f016e22007-07-11 17:01:13 +0000106void ASTContext::PrintStats() const {
107 fprintf(stderr, "*** AST Context Stats:\n");
108 fprintf(stderr, " %d types total.\n", (int)Types.size());
109 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000110 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000111 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
112 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
113
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000115 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
116 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000117 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000118 unsigned NumExtQual = 0;
119
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
121 Type *T = Types[i];
122 if (isa<BuiltinType>(T))
123 ++NumBuiltin;
124 else if (isa<PointerType>(T))
125 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000126 else if (isa<BlockPointerType>(T))
127 ++NumBlockPointer;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000128 else if (isa<LValueReferenceType>(T))
129 ++NumLValueReference;
130 else if (isa<RValueReferenceType>(T))
131 ++NumRValueReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000132 else if (isa<MemberPointerType>(T))
133 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000134 else if (isa<ComplexType>(T))
135 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 else if (isa<ArrayType>(T))
137 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000138 else if (isa<VectorType>(T))
139 ++NumVector;
Douglas Gregor72564e72009-02-26 23:50:07 +0000140 else if (isa<FunctionNoProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 ++NumFunctionNP;
Douglas Gregor72564e72009-02-26 23:50:07 +0000142 else if (isa<FunctionProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 ++NumFunctionP;
144 else if (isa<TypedefType>(T))
145 ++NumTypeName;
146 else if (TagType *TT = dyn_cast<TagType>(T)) {
147 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000148 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000150 case TagDecl::TK_struct: ++NumTagStruct; break;
151 case TagDecl::TK_union: ++NumTagUnion; break;
152 case TagDecl::TK_class: ++NumTagClass; break;
153 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000155 } else if (isa<ObjCInterfaceType>(T))
156 ++NumObjCInterfaces;
157 else if (isa<ObjCQualifiedInterfaceType>(T))
158 ++NumObjCQualifiedInterfaces;
159 else if (isa<ObjCQualifiedIdType>(T))
160 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000161 else if (isa<TypeOfType>(T))
162 ++NumTypeOfTypes;
Douglas Gregor72564e72009-02-26 23:50:07 +0000163 else if (isa<TypeOfExprType>(T))
164 ++NumTypeOfExprTypes;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000165 else if (isa<ExtQualType>(T))
166 ++NumExtQual;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000167 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000168 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 assert(0 && "Unknown type!");
170 }
171 }
172
173 fprintf(stderr, " %d builtin types\n", NumBuiltin);
174 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000175 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000176 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
177 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000178 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000179 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000181 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
183 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
184 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
185 fprintf(stderr, " %d tagged types\n", NumTagged);
186 fprintf(stderr, " %d struct types\n", NumTagStruct);
187 fprintf(stderr, " %d union types\n", NumTagUnion);
188 fprintf(stderr, " %d class types\n", NumTagClass);
189 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000190 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000191 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000192 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000193 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000194 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000195 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor72564e72009-02-26 23:50:07 +0000196 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000197 fprintf(stderr, " %d attribute-qualified types\n", NumExtQual);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000198
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
200 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000201 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000202 NumLValueReference*sizeof(LValueReferenceType)+
203 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000204 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000205 NumFunctionP*sizeof(FunctionProtoType)+
206 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000207 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000208 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)+
209 NumExtQual*sizeof(ExtQualType)));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000210
211 if (ExternalSource.get()) {
212 fprintf(stderr, "\n");
213 ExternalSource->PrintStats();
214 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000215}
216
217
218void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000219 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000220}
221
Reid Spencer5f016e22007-07-11 17:01:13 +0000222void ASTContext::InitBuiltinTypes() {
223 assert(VoidTy.isNull() && "Context reinitialized?");
224
225 // C99 6.2.5p19.
226 InitBuiltinType(VoidTy, BuiltinType::Void);
227
228 // C99 6.2.5p2.
229 InitBuiltinType(BoolTy, BuiltinType::Bool);
230 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000231 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 InitBuiltinType(CharTy, BuiltinType::Char_S);
233 else
234 InitBuiltinType(CharTy, BuiltinType::Char_U);
235 // C99 6.2.5p4.
236 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
237 InitBuiltinType(ShortTy, BuiltinType::Short);
238 InitBuiltinType(IntTy, BuiltinType::Int);
239 InitBuiltinType(LongTy, BuiltinType::Long);
240 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
241
242 // C99 6.2.5p6.
243 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
244 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
245 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
246 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
247 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
248
249 // C99 6.2.5p10.
250 InitBuiltinType(FloatTy, BuiltinType::Float);
251 InitBuiltinType(DoubleTy, BuiltinType::Double);
252 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000253
Chris Lattner2df9ced2009-04-30 02:43:43 +0000254 // GNU extension, 128-bit integers.
255 InitBuiltinType(Int128Ty, BuiltinType::Int128);
256 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
257
Chris Lattner3a250322009-02-26 23:43:47 +0000258 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
259 InitBuiltinType(WCharTy, BuiltinType::WChar);
260 else // C99
261 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000262
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000263 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000264 InitBuiltinType(OverloadTy, BuiltinType::Overload);
265
266 // Placeholder type for type-dependent expressions whose type is
267 // completely unknown. No code should ever check a type against
268 // DependentTy and users should never see it; however, it is here to
269 // help diagnose failures to properly check for type-dependent
270 // expressions.
271 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000272
Reid Spencer5f016e22007-07-11 17:01:13 +0000273 // C99 6.2.5p11.
274 FloatComplexTy = getComplexType(FloatTy);
275 DoubleComplexTy = getComplexType(DoubleTy);
276 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000277
Steve Naroff7e219e42007-10-15 14:41:52 +0000278 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000279 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000280 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000281 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000282 ClassStructType = 0;
283
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000284 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000285
286 // void * type
287 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000288}
289
Chris Lattner464175b2007-07-18 17:52:12 +0000290//===----------------------------------------------------------------------===//
291// Type Sizing and Analysis
292//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000293
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000294/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
295/// scalar floating point type.
296const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
297 const BuiltinType *BT = T->getAsBuiltinType();
298 assert(BT && "Not a floating point type!");
299 switch (BT->getKind()) {
300 default: assert(0 && "Not a floating point type!");
301 case BuiltinType::Float: return Target.getFloatFormat();
302 case BuiltinType::Double: return Target.getDoubleFormat();
303 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
304 }
305}
306
Chris Lattneraf707ab2009-01-24 21:53:27 +0000307/// getDeclAlign - Return a conservative estimate of the alignment of the
308/// specified decl. Note that bitfields do not have a valid alignment, so
309/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000310unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000311 unsigned Align = Target.getCharWidth();
312
313 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
314 Align = std::max(Align, AA->getAlignment());
315
Chris Lattneraf707ab2009-01-24 21:53:27 +0000316 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
317 QualType T = VD->getType();
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000318 if (const ReferenceType* RT = T->getAsReferenceType()) {
319 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000320 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000321 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
322 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000323 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
324 T = cast<ArrayType>(T)->getElementType();
325
326 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
327 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000328 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000329
330 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000331}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000332
Chris Lattnera7674d82007-07-13 22:13:22 +0000333/// getTypeSize - Return the size of the specified type, in bits. This method
334/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000335std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000336ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000337 uint64_t Width=0;
338 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000339 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000340#define TYPE(Class, Base)
341#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000342#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000343#define DEPENDENT_TYPE(Class, Base) case Type::Class:
344#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000345 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000346 break;
347
Chris Lattner692233e2007-07-13 22:27:08 +0000348 case Type::FunctionNoProto:
349 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000350 // GCC extension: alignof(function) = 32 bits
351 Width = 0;
352 Align = 32;
353 break;
354
Douglas Gregor72564e72009-02-26 23:50:07 +0000355 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000356 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000357 Width = 0;
358 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
359 break;
360
Steve Narofffb22d962007-08-30 01:06:46 +0000361 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000362 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000363
Chris Lattner98be4942008-03-05 18:54:05 +0000364 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000365 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000366 Align = EltInfo.second;
367 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000368 }
Nate Begeman213541a2008-04-18 23:10:10 +0000369 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000370 case Type::Vector: {
371 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000372 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000373 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000374 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000375 // If the alignment is not a power of 2, round up to the next power of 2.
376 // This happens for non-power-of-2 length vectors.
377 // FIXME: this should probably be a target property.
378 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000379 break;
380 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000381
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000382 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000383 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000384 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000385 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000386 // GCC extension: alignof(void) = 8 bits.
387 Width = 0;
388 Align = 8;
389 break;
390
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000391 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000392 Width = Target.getBoolWidth();
393 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000394 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000395 case BuiltinType::Char_S:
396 case BuiltinType::Char_U:
397 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000398 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000399 Width = Target.getCharWidth();
400 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000401 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000402 case BuiltinType::WChar:
403 Width = Target.getWCharWidth();
404 Align = Target.getWCharAlign();
405 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000406 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000407 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000408 Width = Target.getShortWidth();
409 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000410 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000411 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000412 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000413 Width = Target.getIntWidth();
414 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000415 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000416 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000417 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000418 Width = Target.getLongWidth();
419 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000420 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000421 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000422 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000423 Width = Target.getLongLongWidth();
424 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000425 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000426 case BuiltinType::Int128:
427 case BuiltinType::UInt128:
428 Width = 128;
429 Align = 128; // int128_t is 128-bit aligned on all targets.
430 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000431 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000432 Width = Target.getFloatWidth();
433 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000434 break;
435 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000436 Width = Target.getDoubleWidth();
437 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000438 break;
439 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000440 Width = Target.getLongDoubleWidth();
441 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000442 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000443 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000444 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000445 case Type::FixedWidthInt:
446 // FIXME: This isn't precisely correct; the width/alignment should depend
447 // on the available types for the target
448 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000449 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000450 Align = Width;
451 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000452 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000453 // FIXME: Pointers into different addr spaces could have different sizes and
454 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000455 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000456 case Type::ObjCQualifiedId:
Douglas Gregor72564e72009-02-26 23:50:07 +0000457 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000458 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000459 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000460 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000461 case Type::BlockPointer: {
462 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
463 Width = Target.getPointerWidth(AS);
464 Align = Target.getPointerAlign(AS);
465 break;
466 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000467 case Type::Pointer: {
468 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000469 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000470 Align = Target.getPointerAlign(AS);
471 break;
472 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000473 case Type::LValueReference:
474 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000475 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000476 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000477 // FIXME: This is wrong for struct layout: a reference in a struct has
478 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000479 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000480 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000481 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000482 // the GCC ABI, where pointers to data are one pointer large, pointers to
483 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000484 // other compilers too, we need to delegate this completely to TargetInfo
485 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000486 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
487 unsigned AS = Pointee.getAddressSpace();
488 Width = Target.getPointerWidth(AS);
489 if (Pointee->isFunctionType())
490 Width *= 2;
491 Align = Target.getPointerAlign(AS);
492 // GCC aligns at single pointer width.
493 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000494 case Type::Complex: {
495 // Complex types have the same alignment as their elements, but twice the
496 // size.
497 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000498 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000499 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000500 Align = EltInfo.second;
501 break;
502 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000503 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000504 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000505 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
506 Width = Layout.getSize();
507 Align = Layout.getAlignment();
508 break;
509 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000510 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000511 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000512 const TagType *TT = cast<TagType>(T);
513
514 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000515 Width = 1;
516 Align = 1;
517 break;
518 }
519
Daniel Dunbar1d751182008-11-08 05:48:37 +0000520 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000521 return getTypeInfo(ET->getDecl()->getIntegerType());
522
Daniel Dunbar1d751182008-11-08 05:48:37 +0000523 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000524 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
525 Width = Layout.getSize();
526 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000527 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000528 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000529
Douglas Gregor18857642009-04-30 17:32:17 +0000530 case Type::Typedef: {
531 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
532 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
533 Align = Aligned->getAlignment();
534 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
535 } else
536 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000537 break;
Chris Lattner71763312008-04-06 22:05:18 +0000538 }
Douglas Gregor18857642009-04-30 17:32:17 +0000539
540 case Type::TypeOfExpr:
541 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
542 .getTypePtr());
543
544 case Type::TypeOf:
545 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
546
547 case Type::QualifiedName:
548 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
549
550 case Type::TemplateSpecialization:
551 assert(getCanonicalType(T) != T &&
552 "Cannot request the size of a dependent type");
553 // FIXME: this is likely to be wrong once we support template
554 // aliases, since a template alias could refer to a typedef that
555 // has an __aligned__ attribute on it.
556 return getTypeInfo(getCanonicalType(T));
557 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000558
Chris Lattner464175b2007-07-18 17:52:12 +0000559 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000560 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000561}
562
Chris Lattner34ebde42009-01-27 18:08:34 +0000563/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
564/// type for the current target in bits. This can be different than the ABI
565/// alignment in cases where it is beneficial for performance to overalign
566/// a data type.
567unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
568 unsigned ABIAlign = getTypeAlign(T);
569
570 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000571 if (T->isSpecificBuiltinType(BuiltinType::Double))
572 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000573
574 return ABIAlign;
575}
576
577
Devang Patel8b277042008-06-04 21:22:16 +0000578/// LayoutField - Field layout.
579void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000580 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000581 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000582 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000583 uint64_t FieldOffset = IsUnion ? 0 : Size;
584 uint64_t FieldSize;
585 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000586
587 // FIXME: Should this override struct packing? Probably we want to
588 // take the minimum?
589 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
590 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000591
592 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
593 // TODO: Need to check this algorithm on other targets!
594 // (tested on Linux-X86)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000595 FieldSize = BitWidthExpr->EvaluateAsInt(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000596
597 std::pair<uint64_t, unsigned> FieldInfo =
598 Context.getTypeInfo(FD->getType());
599 uint64_t TypeSize = FieldInfo.first;
600
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000601 // Determine the alignment of this bitfield. The packing
602 // attributes define a maximum and the alignment attribute defines
603 // a minimum.
604 // FIXME: What is the right behavior when the specified alignment
605 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000606 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000607 if (FieldPacking)
608 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000609 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
610 FieldAlign = std::max(FieldAlign, AA->getAlignment());
611
612 // Check if we need to add padding to give the field the correct
613 // alignment.
614 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
615 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
616
617 // Padding members don't affect overall alignment
618 if (!FD->getIdentifier())
619 FieldAlign = 1;
620 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000621 if (FD->getType()->isIncompleteArrayType()) {
622 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000623 // query getTypeInfo about these, so we figure it out here.
624 // Flexible array members don't have any size, but they
625 // have to be aligned appropriately for their element type.
626 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000627 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000628 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson2f1169f2009-04-10 05:31:15 +0000629 } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
630 unsigned AS = RT->getPointeeType().getAddressSpace();
631 FieldSize = Context.Target.getPointerWidth(AS);
632 FieldAlign = Context.Target.getPointerAlign(AS);
Devang Patel8b277042008-06-04 21:22:16 +0000633 } else {
634 std::pair<uint64_t, unsigned> FieldInfo =
635 Context.getTypeInfo(FD->getType());
636 FieldSize = FieldInfo.first;
637 FieldAlign = FieldInfo.second;
638 }
639
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000640 // Determine the alignment of this bitfield. The packing
641 // attributes define a maximum and the alignment attribute defines
642 // a minimum. Additionally, the packing alignment must be at least
643 // a byte for non-bitfields.
644 //
645 // FIXME: What is the right behavior when the specified alignment
646 // is smaller than the specified packing?
647 if (FieldPacking)
648 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000649 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
650 FieldAlign = std::max(FieldAlign, AA->getAlignment());
651
652 // Round up the current record size to the field's alignment boundary.
653 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
654 }
655
656 // Place this field at the current location.
657 FieldOffsets[FieldNo] = FieldOffset;
658
659 // Reserve space for this field.
660 if (IsUnion) {
661 Size = std::max(Size, FieldSize);
662 } else {
663 Size = FieldOffset + FieldSize;
664 }
665
666 // 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 }
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +0000679 // look into properties.
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000680 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*Ctx),
681 E = OI->prop_end(*Ctx); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000682 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +0000683 Fields.push_back(cast<FieldDecl>(IV));
684 }
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000685}
686
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000687void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
688 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
689 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
690 CollectObjCIvars(SuperClass, Fields);
691 CollectLocalObjCIvars(this, OI, Fields);
692}
693
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000694/// addRecordToClass - produces record info. for the class for its
695/// ivars and all those inherited.
696///
Chris Lattnerf1690852009-03-31 08:48:01 +0000697const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D) {
Daniel Dunbar75da6742009-04-22 10:56:29 +0000698 assert(!D->isForwardDecl() && "Invalid decl!");
699
Chris Lattner23499252009-03-31 09:24:30 +0000700 RecordDecl *&RD = ASTRecordForInterface[D];
Daniel Dunbar75da6742009-04-22 10:56:29 +0000701 if (RD)
702 return RD;
Chris Lattnerf1690852009-03-31 08:48:01 +0000703
704 llvm::SmallVector<FieldDecl*, 32> RecFields;
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000705 CollectLocalObjCIvars(this, D, RecFields);
Chris Lattner23499252009-03-31 09:24:30 +0000706
Daniel Dunbar75da6742009-04-22 10:56:29 +0000707 RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, D->getLocation(),
708 D->getIdentifier());
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000709 const RecordDecl *SRD;
710 if (const ObjCInterfaceDecl *SuperClass = D->getSuperClass()) {
711 SRD = addRecordToClass(SuperClass);
712 } else {
713 SRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, SourceLocation(), 0);
714 const_cast<RecordDecl*>(SRD)->completeDefinition(*this);
715 }
716
717 RD->addDecl(*this,
718 FieldDecl::Create(*this, RD,
719 SourceLocation(),
720 0,
721 getTagDeclType(const_cast<RecordDecl*>(SRD)),
722 0, false));
723
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000724 /// FIXME! Can do collection of ivars and adding to the record while
725 /// doing it.
Chris Lattner16ff7052009-03-31 08:58:42 +0000726 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000727 RD->addDecl(*this,
728 FieldDecl::Create(*this, RD,
Chris Lattner23499252009-03-31 09:24:30 +0000729 RecFields[i]->getLocation(),
730 RecFields[i]->getIdentifier(),
731 RecFields[i]->getType(),
732 RecFields[i]->getBitWidth(), false));
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000733 }
Chris Lattnerf1690852009-03-31 08:48:01 +0000734
Chris Lattner23499252009-03-31 09:24:30 +0000735 RD->completeDefinition(*this);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000736 return RD;
737}
Devang Patel44a3dde2008-06-04 21:54:36 +0000738
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000739/// getInterfaceLayoutImpl - Get or compute information about the
740/// layout of the given interface.
741///
742/// \param Impl - If given, also include the layout of the interface's
743/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000744const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000745ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
746 const ObjCImplementationDecl *Impl) {
Devang Patel44a3dde2008-06-04 21:54:36 +0000747 // Look up this layout, if already laid out, return what we have.
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000748 const ASTRecordLayout *&Entry = ObjCLayouts[D];
Devang Patel44a3dde2008-06-04 21:54:36 +0000749 if (Entry) return *Entry;
750
751 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
752 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000753 ASTRecordLayout *NewEntry = NULL;
Fariborz Jahanian0c7ce5b2009-04-08 21:54:52 +0000754 // FIXME. Add actual count of synthesized ivars, instead of count
755 // of properties which is the upper bound, but is safe.
Daniel Dunbar4af44122009-04-08 20:18:15 +0000756 unsigned FieldCount =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000757 D->ivar_size() + std::distance(D->prop_begin(*this), D->prop_end(*this));
Devang Patel6a5a34c2008-06-06 02:14:01 +0000758 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
759 FieldCount++;
760 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
761 unsigned Alignment = SL.getAlignment();
762 uint64_t Size = SL.getSize();
763 NewEntry = new ASTRecordLayout(Size, Alignment);
764 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000765 // Super class is at the beginning of the layout.
766 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000767 } else {
768 NewEntry = new ASTRecordLayout();
769 NewEntry->InitializeLayout(FieldCount);
770 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000771 Entry = NewEntry;
772
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000773 unsigned StructPacking = 0;
774 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
775 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000776
777 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
778 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
779 AA->getAlignment()));
780
781 // Layout each ivar sequentially.
782 unsigned i = 0;
783 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
784 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
785 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000786 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000787 }
Fariborz Jahanian18191882009-03-31 18:11:23 +0000788 // Also synthesized ivars
Douglas Gregor6ab35242009-04-09 21:40:53 +0000789 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
790 E = D->prop_end(*this); I != E; ++I) {
Fariborz Jahanian18191882009-03-31 18:11:23 +0000791 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
792 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
793 }
Fariborz Jahanian99eee362009-04-01 19:37:34 +0000794
Devang Patel44a3dde2008-06-04 21:54:36 +0000795 // Finally, round the size of the total struct up to the alignment of the
796 // struct itself.
797 NewEntry->FinalizeLayout();
798 return *NewEntry;
799}
800
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000801const ASTRecordLayout &
802ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
803 return getObjCLayout(D, 0);
804}
805
806const ASTRecordLayout &
807ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
808 return getObjCLayout(D->getClassInterface(), D);
809}
810
Devang Patel88a981b2007-11-01 19:11:01 +0000811/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000812/// specified record (struct/union/class), which indicates its size and field
813/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000814const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000815 D = D->getDefinition(*this);
816 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000817
Chris Lattner464175b2007-07-18 17:52:12 +0000818 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000819 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000820 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000821
Devang Patel88a981b2007-11-01 19:11:01 +0000822 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
823 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
824 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000825 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000826
Douglas Gregore267ff32008-12-11 20:41:00 +0000827 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000828 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
829 D->field_end(*this)));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000830 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000831
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000832 unsigned StructPacking = 0;
833 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
834 StructPacking = PA->getAlignment();
835
Eli Friedman4bd998b2008-05-30 09:31:38 +0000836 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000837 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
838 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000839
Eli Friedman4bd998b2008-05-30 09:31:38 +0000840 // Layout each field, for now, just sequentially, respecting alignment. In
841 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000842 unsigned FieldIdx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000843 for (RecordDecl::field_iterator Field = D->field_begin(*this),
844 FieldEnd = D->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000845 Field != FieldEnd; (void)++Field, ++FieldIdx)
846 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000847
848 // Finally, round the size of the total struct up to the alignment of the
849 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000850 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000851 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000852}
853
Chris Lattnera7674d82007-07-13 22:13:22 +0000854//===----------------------------------------------------------------------===//
855// Type creation/memoization methods
856//===----------------------------------------------------------------------===//
857
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000858QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000859 QualType CanT = getCanonicalType(T);
860 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000861 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000862
863 // If we are composing extended qualifiers together, merge together into one
864 // ExtQualType node.
865 unsigned CVRQuals = T.getCVRQualifiers();
866 QualType::GCAttrTypes GCAttr = QualType::GCNone;
867 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000868
Chris Lattnerb7d25532009-02-18 22:53:11 +0000869 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
870 // If this type already has an address space specified, it cannot get
871 // another one.
872 assert(EQT->getAddressSpace() == 0 &&
873 "Type cannot be in multiple addr spaces!");
874 GCAttr = EQT->getObjCGCAttr();
875 TypeNode = EQT->getBaseType();
876 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000877
Chris Lattnerb7d25532009-02-18 22:53:11 +0000878 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000879 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000880 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000881 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000882 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000883 return QualType(EXTQy, CVRQuals);
884
Christopher Lambebb97e92008-02-04 02:31:56 +0000885 // If the base type isn't canonical, this won't be a canonical type either,
886 // so fill in the canonical type field.
887 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000888 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000889 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000890
Chris Lattnerb7d25532009-02-18 22:53:11 +0000891 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000892 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000893 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000894 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000895 ExtQualType *New =
896 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000897 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000898 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000899 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000900}
901
Chris Lattnerb7d25532009-02-18 22:53:11 +0000902QualType ASTContext::getObjCGCQualType(QualType T,
903 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000904 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000905 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000906 return T;
907
Chris Lattnerb7d25532009-02-18 22:53:11 +0000908 // If we are composing extended qualifiers together, merge together into one
909 // ExtQualType node.
910 unsigned CVRQuals = T.getCVRQualifiers();
911 Type *TypeNode = T.getTypePtr();
912 unsigned AddressSpace = 0;
913
914 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
915 // If this type already has an address space specified, it cannot get
916 // another one.
917 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
918 "Type cannot be in multiple addr spaces!");
919 AddressSpace = EQT->getAddressSpace();
920 TypeNode = EQT->getBaseType();
921 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000922
923 // Check if we've already instantiated an gc qual'd type of this type.
924 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000925 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000926 void *InsertPos = 0;
927 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000928 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000929
930 // If the base type isn't canonical, this won't be a canonical type either,
931 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000932 // FIXME: Isn't this also not canonical if the base type is a array
933 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000934 QualType Canonical;
935 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000936 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000937
Chris Lattnerb7d25532009-02-18 22:53:11 +0000938 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000939 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
940 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
941 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000942 ExtQualType *New =
943 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000944 ExtQualTypes.InsertNode(New, InsertPos);
945 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000946 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000947}
Chris Lattnera7674d82007-07-13 22:13:22 +0000948
Reid Spencer5f016e22007-07-11 17:01:13 +0000949/// getComplexType - Return the uniqued reference to the type for a complex
950/// number with the specified element type.
951QualType ASTContext::getComplexType(QualType T) {
952 // Unique pointers, to guarantee there is only one pointer of a particular
953 // structure.
954 llvm::FoldingSetNodeID ID;
955 ComplexType::Profile(ID, T);
956
957 void *InsertPos = 0;
958 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
959 return QualType(CT, 0);
960
961 // If the pointee type isn't canonical, this won't be a canonical type either,
962 // so fill in the canonical type field.
963 QualType Canonical;
964 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000965 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000966
967 // Get the new insert position for the node we care about.
968 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000969 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000970 }
Steve Narofff83820b2009-01-27 22:08:43 +0000971 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000972 Types.push_back(New);
973 ComplexTypes.InsertNode(New, InsertPos);
974 return QualType(New, 0);
975}
976
Eli Friedmanf98aba32009-02-13 02:31:07 +0000977QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
978 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
979 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
980 FixedWidthIntType *&Entry = Map[Width];
981 if (!Entry)
982 Entry = new FixedWidthIntType(Width, Signed);
983 return QualType(Entry, 0);
984}
Reid Spencer5f016e22007-07-11 17:01:13 +0000985
986/// getPointerType - Return the uniqued reference to the type for a pointer to
987/// the specified type.
988QualType ASTContext::getPointerType(QualType T) {
989 // Unique pointers, to guarantee there is only one pointer of a particular
990 // structure.
991 llvm::FoldingSetNodeID ID;
992 PointerType::Profile(ID, T);
993
994 void *InsertPos = 0;
995 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
996 return QualType(PT, 0);
997
998 // If the pointee type isn't canonical, this won't be a canonical type either,
999 // so fill in the canonical type field.
1000 QualType Canonical;
1001 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001002 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +00001003
1004 // Get the new insert position for the node we care about.
1005 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001006 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001007 }
Steve Narofff83820b2009-01-27 22:08:43 +00001008 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 Types.push_back(New);
1010 PointerTypes.InsertNode(New, InsertPos);
1011 return QualType(New, 0);
1012}
1013
Steve Naroff5618bd42008-08-27 16:04:49 +00001014/// getBlockPointerType - Return the uniqued reference to the type for
1015/// a pointer to the specified block.
1016QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +00001017 assert(T->isFunctionType() && "block of function types only");
1018 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +00001019 // structure.
1020 llvm::FoldingSetNodeID ID;
1021 BlockPointerType::Profile(ID, T);
1022
1023 void *InsertPos = 0;
1024 if (BlockPointerType *PT =
1025 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1026 return QualType(PT, 0);
1027
Steve Naroff296e8d52008-08-28 19:20:44 +00001028 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +00001029 // type either so fill in the canonical type field.
1030 QualType Canonical;
1031 if (!T->isCanonical()) {
1032 Canonical = getBlockPointerType(getCanonicalType(T));
1033
1034 // Get the new insert position for the node we care about.
1035 BlockPointerType *NewIP =
1036 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001037 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001038 }
Steve Narofff83820b2009-01-27 22:08:43 +00001039 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001040 Types.push_back(New);
1041 BlockPointerTypes.InsertNode(New, InsertPos);
1042 return QualType(New, 0);
1043}
1044
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001045/// getLValueReferenceType - Return the uniqued reference to the type for an
1046/// lvalue reference to the specified type.
1047QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001048 // Unique pointers, to guarantee there is only one pointer of a particular
1049 // structure.
1050 llvm::FoldingSetNodeID ID;
1051 ReferenceType::Profile(ID, T);
1052
1053 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001054 if (LValueReferenceType *RT =
1055 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001057
Reid Spencer5f016e22007-07-11 17:01:13 +00001058 // If the referencee type isn't canonical, this won't be a canonical type
1059 // either, so fill in the canonical type field.
1060 QualType Canonical;
1061 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001062 Canonical = getLValueReferenceType(getCanonicalType(T));
1063
Reid Spencer5f016e22007-07-11 17:01:13 +00001064 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001065 LValueReferenceType *NewIP =
1066 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001067 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001068 }
1069
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001070 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001071 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001072 LValueReferenceTypes.InsertNode(New, InsertPos);
1073 return QualType(New, 0);
1074}
1075
1076/// getRValueReferenceType - Return the uniqued reference to the type for an
1077/// rvalue reference to the specified type.
1078QualType ASTContext::getRValueReferenceType(QualType T) {
1079 // Unique pointers, to guarantee there is only one pointer of a particular
1080 // structure.
1081 llvm::FoldingSetNodeID ID;
1082 ReferenceType::Profile(ID, T);
1083
1084 void *InsertPos = 0;
1085 if (RValueReferenceType *RT =
1086 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1087 return QualType(RT, 0);
1088
1089 // If the referencee type isn't canonical, this won't be a canonical type
1090 // either, so fill in the canonical type field.
1091 QualType Canonical;
1092 if (!T->isCanonical()) {
1093 Canonical = getRValueReferenceType(getCanonicalType(T));
1094
1095 // Get the new insert position for the node we care about.
1096 RValueReferenceType *NewIP =
1097 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1098 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1099 }
1100
1101 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1102 Types.push_back(New);
1103 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001104 return QualType(New, 0);
1105}
1106
Sebastian Redlf30208a2009-01-24 21:16:55 +00001107/// getMemberPointerType - Return the uniqued reference to the type for a
1108/// member pointer to the specified type, in the specified class.
1109QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1110{
1111 // Unique pointers, to guarantee there is only one pointer of a particular
1112 // structure.
1113 llvm::FoldingSetNodeID ID;
1114 MemberPointerType::Profile(ID, T, Cls);
1115
1116 void *InsertPos = 0;
1117 if (MemberPointerType *PT =
1118 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1119 return QualType(PT, 0);
1120
1121 // If the pointee or class type isn't canonical, this won't be a canonical
1122 // type either, so fill in the canonical type field.
1123 QualType Canonical;
1124 if (!T->isCanonical()) {
1125 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1126
1127 // Get the new insert position for the node we care about.
1128 MemberPointerType *NewIP =
1129 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1130 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1131 }
Steve Narofff83820b2009-01-27 22:08:43 +00001132 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001133 Types.push_back(New);
1134 MemberPointerTypes.InsertNode(New, InsertPos);
1135 return QualType(New, 0);
1136}
1137
Steve Narofffb22d962007-08-30 01:06:46 +00001138/// getConstantArrayType - Return the unique reference to the type for an
1139/// array of the specified element type.
1140QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +00001141 const llvm::APInt &ArySize,
1142 ArrayType::ArraySizeModifier ASM,
1143 unsigned EltTypeQuals) {
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 Redl7b9a2ee2009-04-30 19:20:55 +00001337 unsigned TypeQuals, bool hasExceptionSpec,
1338 bool hasAnyExceptionSpec, unsigned NumExs,
1339 const QualType *ExArray) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001340 // Unique functions, to guarantee there is only one function of a particular
1341 // structure.
1342 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001343 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl7b9a2ee2009-04-30 19:20:55 +00001344 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1345 NumExs, ExArray);
Reid Spencer5f016e22007-07-11 17:01:13 +00001346
1347 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001348 if (FunctionProtoType *FTP =
1349 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001350 return QualType(FTP, 0);
1351
1352 // Determine whether the type being created is already canonical or not.
1353 bool isCanonical = ResultTy->isCanonical();
1354 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1355 if (!ArgArray[i]->isCanonical())
1356 isCanonical = false;
1357
1358 // If this type isn't canonical, get the canonical version of it.
1359 QualType Canonical;
1360 if (!isCanonical) {
1361 llvm::SmallVector<QualType, 16> CanonicalArgs;
1362 CanonicalArgs.reserve(NumArgs);
1363 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001364 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redl7b9a2ee2009-04-30 19:20:55 +00001365 llvm::SmallVector<QualType, 2> CanonicalExs;
1366 CanonicalExs.reserve(NumExs);
1367 for (unsigned i = 0; i != NumExs; ++i)
1368 CanonicalExs.push_back(getCanonicalType(ExArray[i]));
1369
Chris Lattnerf52ab252008-04-06 22:59:24 +00001370 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001371 &CanonicalArgs[0], NumArgs,
Sebastian Redl7b9a2ee2009-04-30 19:20:55 +00001372 isVariadic, TypeQuals, hasExceptionSpec,
1373 hasAnyExceptionSpec, NumExs, &CanonicalExs[0]);
1374
Reid Spencer5f016e22007-07-11 17:01:13 +00001375 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001376 FunctionProtoType *NewIP =
1377 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001378 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001379 }
Sebastian Redl7b9a2ee2009-04-30 19:20:55 +00001380
Douglas Gregor72564e72009-02-26 23:50:07 +00001381 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl7b9a2ee2009-04-30 19:20:55 +00001382 // for two variable size arrays (for parameter and exception types) at the
1383 // end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001384 FunctionProtoType *FTP =
Sebastian Redl7b9a2ee2009-04-30 19:20:55 +00001385 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1386 NumArgs*sizeof(QualType) +
1387 NumExs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001388 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl7b9a2ee2009-04-30 19:20:55 +00001389 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1390 ExArray, NumExs, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001391 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001392 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001393 return QualType(FTP, 0);
1394}
1395
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001396/// getTypeDeclType - Return the unique reference to the type for the
1397/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001398QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001399 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001400 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1401
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001402 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001403 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001404 else if (isa<TemplateTypeParmDecl>(Decl)) {
1405 assert(false && "Template type parameter types are always available.");
1406 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001407 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001408
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001409 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001410 if (PrevDecl)
1411 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001412 else
1413 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001414 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001415 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1416 if (PrevDecl)
1417 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001418 else
1419 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001420 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001421 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001422 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001423
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001424 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001425 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001426}
1427
Reid Spencer5f016e22007-07-11 17:01:13 +00001428/// getTypedefType - Return the unique reference to the type for the
1429/// specified typename decl.
1430QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1431 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1432
Chris Lattnerf52ab252008-04-06 22:59:24 +00001433 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001434 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001435 Types.push_back(Decl->TypeForDecl);
1436 return QualType(Decl->TypeForDecl, 0);
1437}
1438
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001439/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001440/// specified ObjC interface decl.
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001441QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001442 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1443
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001444 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1445 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff3536b442007-09-06 21:24:23 +00001446 Types.push_back(Decl->TypeForDecl);
1447 return QualType(Decl->TypeForDecl, 0);
1448}
1449
Douglas Gregorfab9d672009-02-05 23:33:38 +00001450/// \brief Retrieve the template type parameter type for a template
1451/// parameter with the given depth, index, and (optionally) name.
1452QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1453 IdentifierInfo *Name) {
1454 llvm::FoldingSetNodeID ID;
1455 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1456 void *InsertPos = 0;
1457 TemplateTypeParmType *TypeParm
1458 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1459
1460 if (TypeParm)
1461 return QualType(TypeParm, 0);
1462
1463 if (Name)
1464 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1465 getTemplateTypeParmType(Depth, Index));
1466 else
1467 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1468
1469 Types.push_back(TypeParm);
1470 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1471
1472 return QualType(TypeParm, 0);
1473}
1474
Douglas Gregor55f6b142009-02-09 18:46:07 +00001475QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001476ASTContext::getTemplateSpecializationType(TemplateName Template,
1477 const TemplateArgument *Args,
1478 unsigned NumArgs,
1479 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001480 if (!Canon.isNull())
1481 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001482
Douglas Gregor55f6b142009-02-09 18:46:07 +00001483 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001484 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001485
Douglas Gregor55f6b142009-02-09 18:46:07 +00001486 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001487 TemplateSpecializationType *Spec
1488 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001489
1490 if (Spec)
1491 return QualType(Spec, 0);
1492
Douglas Gregor7532dc62009-03-30 22:58:21 +00001493 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001494 sizeof(TemplateArgument) * NumArgs),
1495 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001496 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001497 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001498 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001499
1500 return QualType(Spec, 0);
1501}
1502
Douglas Gregore4e5b052009-03-19 00:18:19 +00001503QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001504ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001505 QualType NamedType) {
1506 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001507 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001508
1509 void *InsertPos = 0;
1510 QualifiedNameType *T
1511 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1512 if (T)
1513 return QualType(T, 0);
1514
Douglas Gregorab452ba2009-03-26 23:50:42 +00001515 T = new (*this) QualifiedNameType(NNS, NamedType,
1516 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001517 Types.push_back(T);
1518 QualifiedNameTypes.InsertNode(T, InsertPos);
1519 return QualType(T, 0);
1520}
1521
Douglas Gregord57959a2009-03-27 23:10:48 +00001522QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1523 const IdentifierInfo *Name,
1524 QualType Canon) {
1525 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1526
1527 if (Canon.isNull()) {
1528 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1529 if (CanonNNS != NNS)
1530 Canon = getTypenameType(CanonNNS, Name);
1531 }
1532
1533 llvm::FoldingSetNodeID ID;
1534 TypenameType::Profile(ID, NNS, Name);
1535
1536 void *InsertPos = 0;
1537 TypenameType *T
1538 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1539 if (T)
1540 return QualType(T, 0);
1541
1542 T = new (*this) TypenameType(NNS, Name, Canon);
1543 Types.push_back(T);
1544 TypenameTypes.InsertNode(T, InsertPos);
1545 return QualType(T, 0);
1546}
1547
Douglas Gregor17343172009-04-01 00:28:59 +00001548QualType
1549ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1550 const TemplateSpecializationType *TemplateId,
1551 QualType Canon) {
1552 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1553
1554 if (Canon.isNull()) {
1555 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1556 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1557 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1558 const TemplateSpecializationType *CanonTemplateId
1559 = CanonType->getAsTemplateSpecializationType();
1560 assert(CanonTemplateId &&
1561 "Canonical type must also be a template specialization type");
1562 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1563 }
1564 }
1565
1566 llvm::FoldingSetNodeID ID;
1567 TypenameType::Profile(ID, NNS, TemplateId);
1568
1569 void *InsertPos = 0;
1570 TypenameType *T
1571 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1572 if (T)
1573 return QualType(T, 0);
1574
1575 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1576 Types.push_back(T);
1577 TypenameTypes.InsertNode(T, InsertPos);
1578 return QualType(T, 0);
1579}
1580
Chris Lattner88cb27a2008-04-07 04:56:42 +00001581/// CmpProtocolNames - Comparison predicate for sorting protocols
1582/// alphabetically.
1583static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1584 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001585 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001586}
1587
1588static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1589 unsigned &NumProtocols) {
1590 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1591
1592 // Sort protocols, keyed by name.
1593 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1594
1595 // Remove duplicates.
1596 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1597 NumProtocols = ProtocolsEnd-Protocols;
1598}
1599
1600
Chris Lattner065f0d72008-04-07 04:44:08 +00001601/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1602/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001603QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1604 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001605 // Sort the protocol list alphabetically to canonicalize it.
1606 SortAndUniqueProtocols(Protocols, NumProtocols);
1607
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001608 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001609 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001610
1611 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001612 if (ObjCQualifiedInterfaceType *QT =
1613 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001614 return QualType(QT, 0);
1615
1616 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001617 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001618 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001619
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001620 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001621 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001622 return QualType(QType, 0);
1623}
1624
Chris Lattner88cb27a2008-04-07 04:56:42 +00001625/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1626/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001627QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001628 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001629 // Sort the protocol list alphabetically to canonicalize it.
1630 SortAndUniqueProtocols(Protocols, NumProtocols);
1631
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001632 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001633 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001634
1635 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001636 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001637 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001638 return QualType(QT, 0);
1639
1640 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001641 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001642 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001643 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001644 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001645 return QualType(QType, 0);
1646}
1647
Douglas Gregor72564e72009-02-26 23:50:07 +00001648/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1649/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001650/// multiple declarations that refer to "typeof(x)" all contain different
1651/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1652/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001653QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001654 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001655 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001656 Types.push_back(toe);
1657 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001658}
1659
Steve Naroff9752f252007-08-01 18:02:17 +00001660/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1661/// TypeOfType AST's. The only motivation to unique these nodes would be
1662/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1663/// an issue. This doesn't effect the type checker, since it operates
1664/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001665QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001666 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001667 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001668 Types.push_back(tot);
1669 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001670}
1671
Reid Spencer5f016e22007-07-11 17:01:13 +00001672/// getTagDeclType - Return the unique reference to the type for the
1673/// specified TagDecl (struct/union/class/enum) decl.
1674QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001675 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001676 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001677}
1678
1679/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1680/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1681/// needs to agree with the definition in <stddef.h>.
1682QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001683 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001684}
1685
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001686/// getSignedWCharType - Return the type of "signed wchar_t".
1687/// Used when in C++, as a GCC extension.
1688QualType ASTContext::getSignedWCharType() const {
1689 // FIXME: derive from "Target" ?
1690 return WCharTy;
1691}
1692
1693/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1694/// Used when in C++, as a GCC extension.
1695QualType ASTContext::getUnsignedWCharType() const {
1696 // FIXME: derive from "Target" ?
1697 return UnsignedIntTy;
1698}
1699
Chris Lattner8b9023b2007-07-13 03:05:23 +00001700/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1701/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1702QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001703 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001704}
1705
Chris Lattnere6327742008-04-02 05:18:44 +00001706//===----------------------------------------------------------------------===//
1707// Type Operators
1708//===----------------------------------------------------------------------===//
1709
Chris Lattner77c96472008-04-06 22:41:35 +00001710/// getCanonicalType - Return the canonical (structural) type corresponding to
1711/// the specified potentially non-canonical type. The non-canonical version
1712/// of a type may have many "decorated" versions of types. Decorators can
1713/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1714/// to be free of any of these, allowing two canonical types to be compared
1715/// for exact equality with a simple pointer comparison.
1716QualType ASTContext::getCanonicalType(QualType T) {
1717 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001718
1719 // If the result has type qualifiers, make sure to canonicalize them as well.
1720 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1721 if (TypeQuals == 0) return CanType;
1722
1723 // If the type qualifiers are on an array type, get the canonical type of the
1724 // array with the qualifiers applied to the element type.
1725 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1726 if (!AT)
1727 return CanType.getQualifiedType(TypeQuals);
1728
1729 // Get the canonical version of the element with the extra qualifiers on it.
1730 // This can recursively sink qualifiers through multiple levels of arrays.
1731 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1732 NewEltTy = getCanonicalType(NewEltTy);
1733
1734 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1735 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1736 CAT->getIndexTypeQualifier());
1737 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1738 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1739 IAT->getIndexTypeQualifier());
1740
Douglas Gregor898574e2008-12-05 23:32:09 +00001741 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1742 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1743 DSAT->getSizeModifier(),
1744 DSAT->getIndexTypeQualifier());
1745
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001746 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1747 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1748 VAT->getSizeModifier(),
1749 VAT->getIndexTypeQualifier());
1750}
1751
Douglas Gregord57959a2009-03-27 23:10:48 +00001752NestedNameSpecifier *
1753ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1754 if (!NNS)
1755 return 0;
1756
1757 switch (NNS->getKind()) {
1758 case NestedNameSpecifier::Identifier:
1759 // Canonicalize the prefix but keep the identifier the same.
1760 return NestedNameSpecifier::Create(*this,
1761 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1762 NNS->getAsIdentifier());
1763
1764 case NestedNameSpecifier::Namespace:
1765 // A namespace is canonical; build a nested-name-specifier with
1766 // this namespace and no prefix.
1767 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1768
1769 case NestedNameSpecifier::TypeSpec:
1770 case NestedNameSpecifier::TypeSpecWithTemplate: {
1771 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1772 NestedNameSpecifier *Prefix = 0;
1773
1774 // FIXME: This isn't the right check!
1775 if (T->isDependentType())
1776 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1777
1778 return NestedNameSpecifier::Create(*this, Prefix,
1779 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1780 T.getTypePtr());
1781 }
1782
1783 case NestedNameSpecifier::Global:
1784 // The global specifier is canonical and unique.
1785 return NNS;
1786 }
1787
1788 // Required to silence a GCC warning
1789 return 0;
1790}
1791
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001792
1793const ArrayType *ASTContext::getAsArrayType(QualType T) {
1794 // Handle the non-qualified case efficiently.
1795 if (T.getCVRQualifiers() == 0) {
1796 // Handle the common positive case fast.
1797 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1798 return AT;
1799 }
1800
1801 // Handle the common negative case fast, ignoring CVR qualifiers.
1802 QualType CType = T->getCanonicalTypeInternal();
1803
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001804 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001805 // test.
1806 if (!isa<ArrayType>(CType) &&
1807 !isa<ArrayType>(CType.getUnqualifiedType()))
1808 return 0;
1809
1810 // Apply any CVR qualifiers from the array type to the element type. This
1811 // implements C99 6.7.3p8: "If the specification of an array type includes
1812 // any type qualifiers, the element type is so qualified, not the array type."
1813
1814 // If we get here, we either have type qualifiers on the type, or we have
1815 // sugar such as a typedef in the way. If we have type qualifiers on the type
1816 // we must propagate them down into the elemeng type.
1817 unsigned CVRQuals = T.getCVRQualifiers();
1818 unsigned AddrSpace = 0;
1819 Type *Ty = T.getTypePtr();
1820
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001821 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001822 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001823 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1824 AddrSpace = EXTQT->getAddressSpace();
1825 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001826 } else {
1827 T = Ty->getDesugaredType();
1828 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1829 break;
1830 CVRQuals |= T.getCVRQualifiers();
1831 Ty = T.getTypePtr();
1832 }
1833 }
1834
1835 // If we have a simple case, just return now.
1836 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1837 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1838 return ATy;
1839
1840 // Otherwise, we have an array and we have qualifiers on it. Push the
1841 // qualifiers into the array element type and return a new array type.
1842 // Get the canonical version of the element with the extra qualifiers on it.
1843 // This can recursively sink qualifiers through multiple levels of arrays.
1844 QualType NewEltTy = ATy->getElementType();
1845 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001846 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001847 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1848
1849 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1850 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1851 CAT->getSizeModifier(),
1852 CAT->getIndexTypeQualifier()));
1853 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1854 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1855 IAT->getSizeModifier(),
1856 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001857
Douglas Gregor898574e2008-12-05 23:32:09 +00001858 if (const DependentSizedArrayType *DSAT
1859 = dyn_cast<DependentSizedArrayType>(ATy))
1860 return cast<ArrayType>(
1861 getDependentSizedArrayType(NewEltTy,
1862 DSAT->getSizeExpr(),
1863 DSAT->getSizeModifier(),
1864 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001865
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001866 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1867 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1868 VAT->getSizeModifier(),
1869 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001870}
1871
1872
Chris Lattnere6327742008-04-02 05:18:44 +00001873/// getArrayDecayedType - Return the properly qualified result of decaying the
1874/// specified array type to a pointer. This operation is non-trivial when
1875/// handling typedefs etc. The canonical type of "T" must be an array type,
1876/// this returns a pointer to a properly qualified element of the array.
1877///
1878/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1879QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001880 // Get the element type with 'getAsArrayType' so that we don't lose any
1881 // typedefs in the element type of the array. This also handles propagation
1882 // of type qualifiers from the array type into the element type if present
1883 // (C99 6.7.3p8).
1884 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1885 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001886
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001887 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001888
1889 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001890 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001891}
1892
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001893QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001894 QualType ElemTy = VAT->getElementType();
1895
1896 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1897 return getBaseElementType(VAT);
1898
1899 return ElemTy;
1900}
1901
Reid Spencer5f016e22007-07-11 17:01:13 +00001902/// getFloatingRank - Return a relative rank for floating point types.
1903/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001904static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001905 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001906 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001907
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001908 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001909 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001910 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001911 case BuiltinType::Float: return FloatRank;
1912 case BuiltinType::Double: return DoubleRank;
1913 case BuiltinType::LongDouble: return LongDoubleRank;
1914 }
1915}
1916
Steve Naroff716c7302007-08-27 01:41:48 +00001917/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1918/// point or a complex type (based on typeDomain/typeSize).
1919/// 'typeDomain' is a real floating point or complex type.
1920/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001921QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1922 QualType Domain) const {
1923 FloatingRank EltRank = getFloatingRank(Size);
1924 if (Domain->isComplexType()) {
1925 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001926 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001927 case FloatRank: return FloatComplexTy;
1928 case DoubleRank: return DoubleComplexTy;
1929 case LongDoubleRank: return LongDoubleComplexTy;
1930 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001931 }
Chris Lattner1361b112008-04-06 23:58:54 +00001932
1933 assert(Domain->isRealFloatingType() && "Unknown domain!");
1934 switch (EltRank) {
1935 default: assert(0 && "getFloatingRank(): illegal value for rank");
1936 case FloatRank: return FloatTy;
1937 case DoubleRank: return DoubleTy;
1938 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001939 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001940}
1941
Chris Lattner7cfeb082008-04-06 23:55:33 +00001942/// getFloatingTypeOrder - Compare the rank of the two specified floating
1943/// point types, ignoring the domain of the type (i.e. 'double' ==
1944/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1945/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001946int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1947 FloatingRank LHSR = getFloatingRank(LHS);
1948 FloatingRank RHSR = getFloatingRank(RHS);
1949
1950 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001951 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001952 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001953 return 1;
1954 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001955}
1956
Chris Lattnerf52ab252008-04-06 22:59:24 +00001957/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1958/// routine will assert if passed a built-in type that isn't an integer or enum,
1959/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001960unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001961 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001962 if (EnumType* ET = dyn_cast<EnumType>(T))
1963 T = ET->getDecl()->getIntegerType().getTypePtr();
1964
1965 // There are two things which impact the integer rank: the width, and
1966 // the ordering of builtins. The builtin ordering is encoded in the
1967 // bottom three bits; the width is encoded in the bits above that.
1968 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1969 return FWIT->getWidth() << 3;
1970 }
1971
Chris Lattnerf52ab252008-04-06 22:59:24 +00001972 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001973 default: assert(0 && "getIntegerRank(): not a built-in integer");
1974 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001975 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001976 case BuiltinType::Char_S:
1977 case BuiltinType::Char_U:
1978 case BuiltinType::SChar:
1979 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001980 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001981 case BuiltinType::Short:
1982 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001983 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001984 case BuiltinType::Int:
1985 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001986 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001987 case BuiltinType::Long:
1988 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001989 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001990 case BuiltinType::LongLong:
1991 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001992 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00001993 case BuiltinType::Int128:
1994 case BuiltinType::UInt128:
1995 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001996 }
1997}
1998
Chris Lattner7cfeb082008-04-06 23:55:33 +00001999/// getIntegerTypeOrder - Returns the highest ranked integer type:
2000/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
2001/// LHS < RHS, return -1.
2002int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002003 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2004 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00002005 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002006
Chris Lattnerf52ab252008-04-06 22:59:24 +00002007 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2008 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00002009
Chris Lattner7cfeb082008-04-06 23:55:33 +00002010 unsigned LHSRank = getIntegerRank(LHSC);
2011 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00002012
Chris Lattner7cfeb082008-04-06 23:55:33 +00002013 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2014 if (LHSRank == RHSRank) return 0;
2015 return LHSRank > RHSRank ? 1 : -1;
2016 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002017
Chris Lattner7cfeb082008-04-06 23:55:33 +00002018 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2019 if (LHSUnsigned) {
2020 // If the unsigned [LHS] type is larger, return it.
2021 if (LHSRank >= RHSRank)
2022 return 1;
2023
2024 // If the signed type can represent all values of the unsigned type, it
2025 // wins. Because we are dealing with 2's complement and types that are
2026 // powers of two larger than each other, this is always safe.
2027 return -1;
2028 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002029
Chris Lattner7cfeb082008-04-06 23:55:33 +00002030 // If the unsigned [RHS] type is larger, return it.
2031 if (RHSRank >= LHSRank)
2032 return -1;
2033
2034 // If the signed type can represent all values of the unsigned type, it
2035 // wins. Because we are dealing with 2's complement and types that are
2036 // powers of two larger than each other, this is always safe.
2037 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002038}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002039
2040// getCFConstantStringType - Return the type used for constant CFStrings.
2041QualType ASTContext::getCFConstantStringType() {
2042 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00002043 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002044 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002045 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002046 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00002047
2048 // const int *isa;
2049 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002050 // int flags;
2051 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002052 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002053 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00002054 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002055 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00002056
Anders Carlsson71993dd2007-08-17 05:31:46 +00002057 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002058 for (unsigned i = 0; i < 4; ++i) {
2059 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2060 SourceLocation(), 0,
2061 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002062 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002063 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002064 }
2065
2066 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002067 }
2068
2069 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002070}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002071
Douglas Gregor319ac892009-04-23 22:29:11 +00002072void ASTContext::setCFConstantStringType(QualType T) {
2073 const RecordType *Rec = T->getAsRecordType();
2074 assert(Rec && "Invalid CFConstantStringType");
2075 CFConstantStringTypeDecl = Rec->getDecl();
2076}
2077
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002078QualType ASTContext::getObjCFastEnumerationStateType()
2079{
2080 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002081 ObjCFastEnumerationStateTypeDecl =
2082 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2083 &Idents.get("__objcFastEnumerationState"));
2084
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002085 QualType FieldTypes[] = {
2086 UnsignedLongTy,
2087 getPointerType(ObjCIdType),
2088 getPointerType(UnsignedLongTy),
2089 getConstantArrayType(UnsignedLongTy,
2090 llvm::APInt(32, 5), ArrayType::Normal, 0)
2091 };
2092
Douglas Gregor44b43212008-12-11 16:49:14 +00002093 for (size_t i = 0; i < 4; ++i) {
2094 FieldDecl *Field = FieldDecl::Create(*this,
2095 ObjCFastEnumerationStateTypeDecl,
2096 SourceLocation(), 0,
2097 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002098 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002099 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002100 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002101
Douglas Gregor44b43212008-12-11 16:49:14 +00002102 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002103 }
2104
2105 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2106}
2107
Douglas Gregor319ac892009-04-23 22:29:11 +00002108void ASTContext::setObjCFastEnumerationStateType(QualType T) {
2109 const RecordType *Rec = T->getAsRecordType();
2110 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2111 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2112}
2113
Anders Carlssone8c49532007-10-29 06:33:42 +00002114// This returns true if a type has been typedefed to BOOL:
2115// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002116static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002117 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002118 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2119 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002120
2121 return false;
2122}
2123
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002124/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002125/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002126int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002127 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002128
2129 // Make all integer and enum types at least as large as an int
2130 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002131 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002132 // Treat arrays as pointers, since that's how they're passed in.
2133 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002134 sz = getTypeSize(VoidPtrTy);
2135 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002136}
2137
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002138/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002139/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002140void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002141 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002142 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002143 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002144 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002145 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002146 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002147 // Compute size of all parameters.
2148 // Start with computing size of a pointer in number of bytes.
2149 // FIXME: There might(should) be a better way of doing this computation!
2150 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002151 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002152 // The first two arguments (self and _cmd) are pointers; account for
2153 // their size.
2154 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002155 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2156 E = Decl->param_end(); PI != E; ++PI) {
2157 QualType PType = (*PI)->getType();
2158 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002159 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002160 ParmOffset += sz;
2161 }
2162 S += llvm::utostr(ParmOffset);
2163 S += "@0:";
2164 S += llvm::utostr(PtrSize);
2165
2166 // Argument types.
2167 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002168 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2169 E = Decl->param_end(); PI != E; ++PI) {
2170 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002171 QualType PType = PVDecl->getOriginalType();
2172 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002173 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2174 // Use array's original type only if it has known number of
2175 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002176 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002177 PType = PVDecl->getType();
2178 } else if (PType->isFunctionType())
2179 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002180 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002181 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002182 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002183 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002184 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002185 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002186 }
2187}
2188
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002189/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002190/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002191/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2192/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002193/// Property attributes are stored as a comma-delimited C string. The simple
2194/// attributes readonly and bycopy are encoded as single characters. The
2195/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2196/// encoded as single characters, followed by an identifier. Property types
2197/// are also encoded as a parametrized attribute. The characters used to encode
2198/// these attributes are defined by the following enumeration:
2199/// @code
2200/// enum PropertyAttributes {
2201/// kPropertyReadOnly = 'R', // property is read-only.
2202/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2203/// kPropertyByref = '&', // property is a reference to the value last assigned
2204/// kPropertyDynamic = 'D', // property is dynamic
2205/// kPropertyGetter = 'G', // followed by getter selector name
2206/// kPropertySetter = 'S', // followed by setter selector name
2207/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2208/// kPropertyType = 't' // followed by old-style type encoding.
2209/// kPropertyWeak = 'W' // 'weak' property
2210/// kPropertyStrong = 'P' // property GC'able
2211/// kPropertyNonAtomic = 'N' // property non-atomic
2212/// };
2213/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002214void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2215 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002216 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002217 // Collect information from the property implementation decl(s).
2218 bool Dynamic = false;
2219 ObjCPropertyImplDecl *SynthesizePID = 0;
2220
2221 // FIXME: Duplicated code due to poor abstraction.
2222 if (Container) {
2223 if (const ObjCCategoryImplDecl *CID =
2224 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2225 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002226 i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2227 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002228 ObjCPropertyImplDecl *PID = *i;
2229 if (PID->getPropertyDecl() == PD) {
2230 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2231 Dynamic = true;
2232 } else {
2233 SynthesizePID = PID;
2234 }
2235 }
2236 }
2237 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002238 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002239 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002240 i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2241 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002242 ObjCPropertyImplDecl *PID = *i;
2243 if (PID->getPropertyDecl() == PD) {
2244 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2245 Dynamic = true;
2246 } else {
2247 SynthesizePID = PID;
2248 }
2249 }
2250 }
2251 }
2252 }
2253
2254 // FIXME: This is not very efficient.
2255 S = "T";
2256
2257 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002258 // GCC has some special rules regarding encoding of properties which
2259 // closely resembles encoding of ivars.
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002260 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002261 true /* outermost type */,
2262 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002263
2264 if (PD->isReadOnly()) {
2265 S += ",R";
2266 } else {
2267 switch (PD->getSetterKind()) {
2268 case ObjCPropertyDecl::Assign: break;
2269 case ObjCPropertyDecl::Copy: S += ",C"; break;
2270 case ObjCPropertyDecl::Retain: S += ",&"; break;
2271 }
2272 }
2273
2274 // It really isn't clear at all what this means, since properties
2275 // are "dynamic by default".
2276 if (Dynamic)
2277 S += ",D";
2278
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002279 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2280 S += ",N";
2281
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002282 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2283 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002284 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002285 }
2286
2287 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2288 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002289 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002290 }
2291
2292 if (SynthesizePID) {
2293 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2294 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002295 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002296 }
2297
2298 // FIXME: OBJCGC: weak & strong
2299}
2300
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002301/// getLegacyIntegralTypeEncoding -
2302/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002303/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002304/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2305///
2306void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2307 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2308 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002309 if (BT->getKind() == BuiltinType::ULong &&
2310 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002311 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002312 else
2313 if (BT->getKind() == BuiltinType::Long &&
2314 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002315 PointeeTy = IntTy;
2316 }
2317 }
2318}
2319
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002320void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002321 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002322 // We follow the behavior of gcc, expanding structures which are
2323 // directly pointed to, and expanding embedded structures. Note that
2324 // these rules are sufficient to prevent recursive encoding of the
2325 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002326 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2327 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002328}
2329
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002330static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002331 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002332 const Expr *E = FD->getBitWidth();
2333 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2334 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002335 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002336 S += 'b';
2337 S += llvm::utostr(N);
2338}
2339
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002340void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2341 bool ExpandPointedToStructures,
2342 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002343 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002344 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002345 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002346 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002347 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002348 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002349 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002350 else {
2351 char encoding;
2352 switch (BT->getKind()) {
2353 default: assert(0 && "Unhandled builtin type kind");
2354 case BuiltinType::Void: encoding = 'v'; break;
2355 case BuiltinType::Bool: encoding = 'B'; break;
2356 case BuiltinType::Char_U:
2357 case BuiltinType::UChar: encoding = 'C'; break;
2358 case BuiltinType::UShort: encoding = 'S'; break;
2359 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002360 case BuiltinType::ULong:
2361 encoding =
2362 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2363 break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002364 case BuiltinType::UInt128: encoding = 'T'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002365 case BuiltinType::ULongLong: encoding = 'Q'; break;
2366 case BuiltinType::Char_S:
2367 case BuiltinType::SChar: encoding = 'c'; break;
2368 case BuiltinType::Short: encoding = 's'; break;
2369 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002370 case BuiltinType::Long:
2371 encoding =
2372 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2373 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002374 case BuiltinType::LongLong: encoding = 'q'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002375 case BuiltinType::Int128: encoding = 't'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002376 case BuiltinType::Float: encoding = 'f'; break;
2377 case BuiltinType::Double: encoding = 'd'; break;
2378 case BuiltinType::LongDouble: encoding = 'd'; break;
2379 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002380
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002381 S += encoding;
2382 }
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002383 } else if (const ComplexType *CT = T->getAsComplexType()) {
2384 S += 'j';
2385 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2386 false);
2387 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002388 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2389 ExpandPointedToStructures,
2390 ExpandStructures, FD);
2391 if (FD || EncodingProperty) {
2392 // Note that we do extended encoding of protocol qualifer list
2393 // Only when doing ivar or property encoding.
2394 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2395 S += '"';
2396 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2397 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2398 S += '<';
2399 S += Proto->getNameAsString();
2400 S += '>';
2401 }
2402 S += '"';
2403 }
2404 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002405 }
2406 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002407 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002408 bool isReadOnly = false;
2409 // For historical/compatibility reasons, the read-only qualifier of the
2410 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2411 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2412 // Also, do not emit the 'r' for anything but the outermost type!
2413 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2414 if (OutermostType && T.isConstQualified()) {
2415 isReadOnly = true;
2416 S += 'r';
2417 }
2418 }
2419 else if (OutermostType) {
2420 QualType P = PointeeTy;
2421 while (P->getAsPointerType())
2422 P = P->getAsPointerType()->getPointeeType();
2423 if (P.isConstQualified()) {
2424 isReadOnly = true;
2425 S += 'r';
2426 }
2427 }
2428 if (isReadOnly) {
2429 // Another legacy compatibility encoding. Some ObjC qualifier and type
2430 // combinations need to be rearranged.
2431 // Rewrite "in const" from "nr" to "rn"
2432 const char * s = S.c_str();
2433 int len = S.length();
2434 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2435 std::string replace = "rn";
2436 S.replace(S.end()-2, S.end(), replace);
2437 }
2438 }
Steve Naroff389bf462009-02-12 17:52:19 +00002439 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002440 S += '@';
2441 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002442 }
2443 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002444 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002445 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002446 // Another historical/compatibility reason.
2447 // We encode the underlying type which comes out as
2448 // {...};
2449 S += '^';
2450 getObjCEncodingForTypeImpl(PointeeTy, S,
2451 false, ExpandPointedToStructures,
2452 NULL);
2453 return;
2454 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002455 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002456 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002457 const ObjCInterfaceType *OIT =
2458 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002459 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002460 S += '"';
2461 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002462 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2463 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2464 S += '<';
2465 S += Proto->getNameAsString();
2466 S += '>';
2467 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002468 S += '"';
2469 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002470 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002471 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002472 S += '#';
2473 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002474 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002475 S += ':';
2476 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002477 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002478
2479 if (PointeeTy->isCharType()) {
2480 // char pointer types should be encoded as '*' unless it is a
2481 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002482 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002483 S += '*';
2484 return;
2485 }
2486 }
2487
2488 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002489 getLegacyIntegralTypeEncoding(PointeeTy);
2490
2491 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002492 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002493 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002494 } else if (const ArrayType *AT =
2495 // Ignore type qualifiers etc.
2496 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002497 if (isa<IncompleteArrayType>(AT)) {
2498 // Incomplete arrays are encoded as a pointer to the array element.
2499 S += '^';
2500
2501 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2502 false, ExpandStructures, FD);
2503 } else {
2504 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002505
Anders Carlsson559a8332009-02-22 01:38:57 +00002506 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2507 S += llvm::utostr(CAT->getSize().getZExtValue());
2508 else {
2509 //Variable length arrays are encoded as a regular array with 0 elements.
2510 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2511 S += '0';
2512 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002513
Anders Carlsson559a8332009-02-22 01:38:57 +00002514 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2515 false, ExpandStructures, FD);
2516 S += ']';
2517 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002518 } else if (T->getAsFunctionType()) {
2519 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002520 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002521 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002522 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002523 // Anonymous structures print as '?'
2524 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2525 S += II->getName();
2526 } else {
2527 S += '?';
2528 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002529 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002530 S += '=';
Douglas Gregor6ab35242009-04-09 21:40:53 +00002531 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2532 FieldEnd = RDecl->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +00002533 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002534 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002535 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002536 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002537 S += '"';
2538 }
2539
2540 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002541 if (Field->isBitField()) {
2542 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2543 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002544 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002545 QualType qt = Field->getType();
2546 getLegacyIntegralTypeEncoding(qt);
2547 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002548 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002549 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002550 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002551 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002552 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002553 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002554 if (FD && FD->isBitField())
2555 EncodeBitField(this, S, FD);
2556 else
2557 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002558 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002559 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002560 } else if (T->isObjCInterfaceType()) {
2561 // @encode(class_name)
2562 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2563 S += '{';
2564 const IdentifierInfo *II = OI->getIdentifier();
2565 S += II->getName();
2566 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002567 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002568 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002569 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002570 if (RecFields[i]->isBitField())
2571 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2572 RecFields[i]);
2573 else
2574 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2575 FD);
2576 }
2577 S += '}';
2578 }
2579 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002580 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002581}
2582
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002583void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002584 std::string& S) const {
2585 if (QT & Decl::OBJC_TQ_In)
2586 S += 'n';
2587 if (QT & Decl::OBJC_TQ_Inout)
2588 S += 'N';
2589 if (QT & Decl::OBJC_TQ_Out)
2590 S += 'o';
2591 if (QT & Decl::OBJC_TQ_Bycopy)
2592 S += 'O';
2593 if (QT & Decl::OBJC_TQ_Byref)
2594 S += 'R';
2595 if (QT & Decl::OBJC_TQ_Oneway)
2596 S += 'V';
2597}
2598
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002599void ASTContext::setBuiltinVaListType(QualType T)
2600{
2601 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2602
2603 BuiltinVaListType = T;
2604}
2605
Douglas Gregor319ac892009-04-23 22:29:11 +00002606void ASTContext::setObjCIdType(QualType T)
Steve Naroff7e219e42007-10-15 14:41:52 +00002607{
Douglas Gregor319ac892009-04-23 22:29:11 +00002608 ObjCIdType = T;
2609
2610 const TypedefType *TT = T->getAsTypedefType();
2611 if (!TT)
2612 return;
2613
2614 TypedefDecl *TD = TT->getDecl();
Steve Naroff7e219e42007-10-15 14:41:52 +00002615
2616 // typedef struct objc_object *id;
2617 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002618 // User error - caller will issue diagnostics.
2619 if (!ptr)
2620 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002621 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002622 // User error - caller will issue diagnostics.
2623 if (!rec)
2624 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002625 IdStructType = rec;
2626}
2627
Douglas Gregor319ac892009-04-23 22:29:11 +00002628void ASTContext::setObjCSelType(QualType T)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002629{
Douglas Gregor319ac892009-04-23 22:29:11 +00002630 ObjCSelType = T;
2631
2632 const TypedefType *TT = T->getAsTypedefType();
2633 if (!TT)
2634 return;
2635 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002636
2637 // typedef struct objc_selector *SEL;
2638 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002639 if (!ptr)
2640 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002641 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002642 if (!rec)
2643 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002644 SelStructType = rec;
2645}
2646
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002647void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002648{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002649 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002650}
2651
Douglas Gregor319ac892009-04-23 22:29:11 +00002652void ASTContext::setObjCClassType(QualType T)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002653{
Douglas Gregor319ac892009-04-23 22:29:11 +00002654 ObjCClassType = T;
2655
2656 const TypedefType *TT = T->getAsTypedefType();
2657 if (!TT)
2658 return;
2659 TypedefDecl *TD = TT->getDecl();
Anders Carlsson8baaca52007-10-31 02:53:19 +00002660
2661 // typedef struct objc_class *Class;
2662 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2663 assert(ptr && "'Class' incorrectly typed");
2664 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2665 assert(rec && "'Class' incorrectly typed");
2666 ClassStructType = rec;
2667}
2668
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002669void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2670 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002671 "'NSConstantString' type already set!");
2672
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002673 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002674}
2675
Douglas Gregor7532dc62009-03-30 22:58:21 +00002676/// \brief Retrieve the template name that represents a qualified
2677/// template name such as \c std::vector.
2678TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2679 bool TemplateKeyword,
2680 TemplateDecl *Template) {
2681 llvm::FoldingSetNodeID ID;
2682 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2683
2684 void *InsertPos = 0;
2685 QualifiedTemplateName *QTN =
2686 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2687 if (!QTN) {
2688 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2689 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2690 }
2691
2692 return TemplateName(QTN);
2693}
2694
2695/// \brief Retrieve the template name that represents a dependent
2696/// template name such as \c MetaFun::template apply.
2697TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2698 const IdentifierInfo *Name) {
2699 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2700
2701 llvm::FoldingSetNodeID ID;
2702 DependentTemplateName::Profile(ID, NNS, Name);
2703
2704 void *InsertPos = 0;
2705 DependentTemplateName *QTN =
2706 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2707
2708 if (QTN)
2709 return TemplateName(QTN);
2710
2711 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2712 if (CanonNNS == NNS) {
2713 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2714 } else {
2715 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2716 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2717 }
2718
2719 DependentTemplateNames.InsertNode(QTN, InsertPos);
2720 return TemplateName(QTN);
2721}
2722
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002723/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002724/// TargetInfo, produce the corresponding type. The unsigned @p Type
2725/// is actually a value of type @c TargetInfo::IntType.
2726QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002727 switch (Type) {
2728 case TargetInfo::NoInt: return QualType();
2729 case TargetInfo::SignedShort: return ShortTy;
2730 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2731 case TargetInfo::SignedInt: return IntTy;
2732 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2733 case TargetInfo::SignedLong: return LongTy;
2734 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2735 case TargetInfo::SignedLongLong: return LongLongTy;
2736 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2737 }
2738
2739 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002740 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002741}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002742
2743//===----------------------------------------------------------------------===//
2744// Type Predicates.
2745//===----------------------------------------------------------------------===//
2746
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002747/// isObjCNSObjectType - Return true if this is an NSObject object using
2748/// NSObject attribute on a c-style pointer type.
2749/// FIXME - Make it work directly on types.
2750///
2751bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2752 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2753 if (TypedefDecl *TD = TDT->getDecl())
2754 if (TD->getAttr<ObjCNSObjectAttr>())
2755 return true;
2756 }
2757 return false;
2758}
2759
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002760/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2761/// to an object type. This includes "id" and "Class" (two 'special' pointers
2762/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2763/// ID type).
2764bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002765 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002766 return true;
2767
Steve Naroff6ae98502008-10-21 18:24:04 +00002768 // Blocks are objects.
2769 if (Ty->isBlockPointerType())
2770 return true;
2771
2772 // All other object types are pointers.
Chris Lattner16ede0e2009-04-12 23:51:02 +00002773 const PointerType *PT = Ty->getAsPointerType();
2774 if (PT == 0)
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002775 return false;
2776
Chris Lattner16ede0e2009-04-12 23:51:02 +00002777 // If this a pointer to an interface (e.g. NSString*), it is ok.
2778 if (PT->getPointeeType()->isObjCInterfaceType() ||
2779 // If is has NSObject attribute, OK as well.
2780 isObjCNSObjectType(Ty))
2781 return true;
2782
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002783 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2784 // pointer types. This looks for the typedef specifically, not for the
Chris Lattner16ede0e2009-04-12 23:51:02 +00002785 // underlying type. Iteratively strip off typedefs so that we can handle
2786 // typedefs of typedefs.
2787 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2788 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2789 Ty.getUnqualifiedType() == getObjCClassType())
2790 return true;
2791
2792 Ty = TDT->getDecl()->getUnderlyingType();
2793 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002794
Chris Lattner16ede0e2009-04-12 23:51:02 +00002795 return false;
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002796}
2797
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002798/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2799/// garbage collection attribute.
2800///
2801QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002802 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002803 if (getLangOptions().ObjC1 &&
2804 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002805 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002806 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002807 // (or pointers to them) be treated as though they were declared
2808 // as __strong.
2809 if (GCAttrs == QualType::GCNone) {
2810 if (isObjCObjectPointerType(Ty))
2811 GCAttrs = QualType::Strong;
2812 else if (Ty->isPointerType())
2813 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2814 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00002815 // Non-pointers have none gc'able attribute regardless of the attribute
2816 // set on them.
2817 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2818 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002819 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002820 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002821}
2822
Chris Lattner6ac46a42008-04-07 06:51:04 +00002823//===----------------------------------------------------------------------===//
2824// Type Compatibility Testing
2825//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002826
Steve Naroff1c7d0672008-09-04 15:10:53 +00002827/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002828/// block types. Types must be strictly compatible here. For example,
2829/// C unfortunately doesn't produce an error for the following:
2830///
2831/// int (*emptyArgFunc)();
2832/// int (*intArgList)(int) = emptyArgFunc;
2833///
2834/// For blocks, we will produce an error for the following (similar to C++):
2835///
2836/// int (^emptyArgBlock)();
2837/// int (^intArgBlock)(int) = emptyArgBlock;
2838///
2839/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2840///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002841bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002842 const FunctionType *lbase = lhs->getAsFunctionType();
2843 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002844 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2845 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stumpaab0f7a2009-04-01 01:17:39 +00002846 if (lproto && rproto == 0)
2847 return false;
2848 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff1c7d0672008-09-04 15:10:53 +00002849}
2850
Chris Lattner6ac46a42008-04-07 06:51:04 +00002851/// areCompatVectorTypes - Return true if the two specified vector types are
2852/// compatible.
2853static bool areCompatVectorTypes(const VectorType *LHS,
2854 const VectorType *RHS) {
2855 assert(LHS->isCanonical() && RHS->isCanonical());
2856 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002857 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002858}
2859
Eli Friedman3d815e72008-08-22 00:56:42 +00002860/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002861/// compatible for assignment from RHS to LHS. This handles validation of any
2862/// protocol qualifiers on the LHS or RHS.
2863///
Eli Friedman3d815e72008-08-22 00:56:42 +00002864bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2865 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002866 // Verify that the base decls are compatible: the RHS must be a subclass of
2867 // the LHS.
2868 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2869 return false;
2870
2871 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2872 // protocol qualified at all, then we are good.
2873 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2874 return true;
2875
2876 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2877 // isn't a superset.
2878 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2879 return true; // FIXME: should return false!
2880
2881 // Finally, we must have two protocol-qualified interfaces.
2882 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2883 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002884
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002885 // All LHS protocols must have a presence on the RHS.
2886 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002887
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002888 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2889 LHSPE = LHSP->qual_end();
2890 LHSPI != LHSPE; LHSPI++) {
2891 bool RHSImplementsProtocol = false;
2892
2893 // If the RHS doesn't implement the protocol on the left, the types
2894 // are incompatible.
2895 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2896 RHSPE = RHSP->qual_end();
2897 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2898 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2899 RHSImplementsProtocol = true;
2900 }
2901 // FIXME: For better diagnostics, consider passing back the protocol name.
2902 if (!RHSImplementsProtocol)
2903 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002904 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002905 // The RHS implements all protocols listed on the LHS.
2906 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002907}
2908
Steve Naroff389bf462009-02-12 17:52:19 +00002909bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2910 // get the "pointed to" types
2911 const PointerType *LHSPT = LHS->getAsPointerType();
2912 const PointerType *RHSPT = RHS->getAsPointerType();
2913
2914 if (!LHSPT || !RHSPT)
2915 return false;
2916
2917 QualType lhptee = LHSPT->getPointeeType();
2918 QualType rhptee = RHSPT->getPointeeType();
2919 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2920 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2921 // ID acts sort of like void* for ObjC interfaces
2922 if (LHSIface && isObjCIdStructType(rhptee))
2923 return true;
2924 if (RHSIface && isObjCIdStructType(lhptee))
2925 return true;
2926 if (!LHSIface || !RHSIface)
2927 return false;
2928 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2929 canAssignObjCInterfaces(RHSIface, LHSIface);
2930}
2931
Steve Naroffec0550f2007-10-15 20:41:53 +00002932/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2933/// both shall have the identically qualified version of a compatible type.
2934/// C99 6.2.7p1: Two types have compatible types if their types are the
2935/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002936bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2937 return !mergeTypes(LHS, RHS).isNull();
2938}
2939
2940QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2941 const FunctionType *lbase = lhs->getAsFunctionType();
2942 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002943 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2944 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002945 bool allLTypes = true;
2946 bool allRTypes = true;
2947
2948 // Check return type
2949 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2950 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002951 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2952 allLTypes = false;
2953 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2954 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002955
2956 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl7b9a2ee2009-04-30 19:20:55 +00002957 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
2958 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00002959 unsigned lproto_nargs = lproto->getNumArgs();
2960 unsigned rproto_nargs = rproto->getNumArgs();
2961
2962 // Compatible functions must have the same number of arguments
2963 if (lproto_nargs != rproto_nargs)
2964 return QualType();
2965
2966 // Variadic and non-variadic functions aren't compatible
2967 if (lproto->isVariadic() != rproto->isVariadic())
2968 return QualType();
2969
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002970 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2971 return QualType();
2972
Eli Friedman3d815e72008-08-22 00:56:42 +00002973 // Check argument compatibility
2974 llvm::SmallVector<QualType, 10> types;
2975 for (unsigned i = 0; i < lproto_nargs; i++) {
2976 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2977 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2978 QualType argtype = mergeTypes(largtype, rargtype);
2979 if (argtype.isNull()) return QualType();
2980 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002981 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2982 allLTypes = false;
2983 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2984 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002985 }
2986 if (allLTypes) return lhs;
2987 if (allRTypes) return rhs;
2988 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002989 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002990 }
2991
2992 if (lproto) allRTypes = false;
2993 if (rproto) allLTypes = false;
2994
Douglas Gregor72564e72009-02-26 23:50:07 +00002995 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002996 if (proto) {
Sebastian Redl7b9a2ee2009-04-30 19:20:55 +00002997 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00002998 if (proto->isVariadic()) return QualType();
2999 // Check that the types are compatible with the types that
3000 // would result from default argument promotions (C99 6.7.5.3p15).
3001 // The only types actually affected are promotable integer
3002 // types and floats, which would be passed as a different
3003 // type depending on whether the prototype is visible.
3004 unsigned proto_nargs = proto->getNumArgs();
3005 for (unsigned i = 0; i < proto_nargs; ++i) {
3006 QualType argTy = proto->getArgType(i);
3007 if (argTy->isPromotableIntegerType() ||
3008 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
3009 return QualType();
3010 }
3011
3012 if (allLTypes) return lhs;
3013 if (allRTypes) return rhs;
3014 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003015 proto->getNumArgs(), lproto->isVariadic(),
3016 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00003017 }
3018
3019 if (allLTypes) return lhs;
3020 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00003021 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00003022}
3023
3024QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00003025 // C++ [expr]: If an expression initially has the type "reference to T", the
3026 // type is adjusted to "T" prior to any further analysis, the expression
3027 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003028 // expression is an lvalue unless the reference is an rvalue reference and
3029 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00003030 // FIXME: C++ shouldn't be going through here! The rules are different
3031 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003032 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3033 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00003034 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003035 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00003036 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003037 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00003038
Eli Friedman3d815e72008-08-22 00:56:42 +00003039 QualType LHSCan = getCanonicalType(LHS),
3040 RHSCan = getCanonicalType(RHS);
3041
3042 // If two types are identical, they are compatible.
3043 if (LHSCan == RHSCan)
3044 return LHS;
3045
3046 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003047 // Note that we handle extended qualifiers later, in the
3048 // case for ExtQualType.
3049 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00003050 return QualType();
3051
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003052 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
3053 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003054
Chris Lattner1adb8832008-01-14 05:45:46 +00003055 // We want to consider the two function types to be the same for these
3056 // comparisons, just force one to the other.
3057 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3058 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003059
3060 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003061 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3062 LHSClass = Type::ConstantArray;
3063 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3064 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00003065
Nate Begeman213541a2008-04-18 23:10:10 +00003066 // Canonicalize ExtVector -> Vector.
3067 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3068 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00003069
Chris Lattnerb0489812008-04-07 06:38:24 +00003070 // Consider qualified interfaces and interfaces the same.
3071 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
3072 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00003073
Chris Lattnera36a61f2008-04-07 05:43:21 +00003074 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003075 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003076 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3077 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003078
Steve Naroffd824c9c2009-04-14 15:11:46 +00003079 // 'id' and 'Class' act sort of like void* for ObjC interfaces
3080 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003081 return LHS;
Steve Naroffd824c9c2009-04-14 15:11:46 +00003082 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003083 return RHS;
3084
Steve Naroffbc76dd02008-12-10 22:14:21 +00003085 // ID is compatible with all qualified id types.
3086 if (LHS->isObjCQualifiedIdType()) {
3087 if (const PointerType *PT = RHS->getAsPointerType()) {
3088 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003089 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003090 return LHS;
3091 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3092 // Unfortunately, this API is part of Sema (which we don't have access
3093 // to. Need to refactor. The following check is insufficient, since we
3094 // need to make sure the class implements the protocol.
3095 if (pType->isObjCInterfaceType())
3096 return LHS;
3097 }
3098 }
3099 if (RHS->isObjCQualifiedIdType()) {
3100 if (const PointerType *PT = LHS->getAsPointerType()) {
3101 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003102 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003103 return RHS;
3104 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3105 // Unfortunately, this API is part of Sema (which we don't have access
3106 // to. Need to refactor. The following check is insufficient, since we
3107 // need to make sure the class implements the protocol.
3108 if (pType->isObjCInterfaceType())
3109 return RHS;
3110 }
3111 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003112 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3113 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00003114 if (const EnumType* ETy = LHS->getAsEnumType()) {
3115 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3116 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003117 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003118 if (const EnumType* ETy = RHS->getAsEnumType()) {
3119 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3120 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003121 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003122
Eli Friedman3d815e72008-08-22 00:56:42 +00003123 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003124 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003125
Steve Naroff4a746782008-01-09 22:43:08 +00003126 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003127 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003128#define TYPE(Class, Base)
3129#define ABSTRACT_TYPE(Class, Base)
3130#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3131#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3132#include "clang/AST/TypeNodes.def"
3133 assert(false && "Non-canonical and dependent types shouldn't get here");
3134 return QualType();
3135
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003136 case Type::LValueReference:
3137 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003138 case Type::MemberPointer:
3139 assert(false && "C++ should never be in mergeTypes");
3140 return QualType();
3141
3142 case Type::IncompleteArray:
3143 case Type::VariableArray:
3144 case Type::FunctionProto:
3145 case Type::ExtVector:
3146 case Type::ObjCQualifiedInterface:
3147 assert(false && "Types are eliminated above");
3148 return QualType();
3149
Chris Lattner1adb8832008-01-14 05:45:46 +00003150 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003151 {
3152 // Merge two pointer types, while trying to preserve typedef info
3153 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3154 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3155 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3156 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003157 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3158 return LHS;
3159 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3160 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003161 return getPointerType(ResultType);
3162 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003163 case Type::BlockPointer:
3164 {
3165 // Merge two block pointer types, while trying to preserve typedef info
3166 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3167 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3168 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3169 if (ResultType.isNull()) return QualType();
3170 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3171 return LHS;
3172 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3173 return RHS;
3174 return getBlockPointerType(ResultType);
3175 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003176 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003177 {
3178 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3179 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3180 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3181 return QualType();
3182
3183 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3184 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3185 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3186 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003187 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3188 return LHS;
3189 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3190 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003191 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3192 ArrayType::ArraySizeModifier(), 0);
3193 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3194 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003195 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3196 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003197 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3198 return LHS;
3199 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3200 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003201 if (LVAT) {
3202 // FIXME: This isn't correct! But tricky to implement because
3203 // the array's size has to be the size of LHS, but the type
3204 // has to be different.
3205 return LHS;
3206 }
3207 if (RVAT) {
3208 // FIXME: This isn't correct! But tricky to implement because
3209 // the array's size has to be the size of RHS, but the type
3210 // has to be different.
3211 return RHS;
3212 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003213 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3214 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003215 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003216 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003217 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003218 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003219 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003220 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003221 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003222 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3223 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003224 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003225 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003226 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003227 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003228 case Type::Complex:
3229 // Distinct complex types are incompatible.
3230 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003231 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003232 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003233 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3234 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003235 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003236 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003237 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003238 // FIXME: This should be type compatibility, e.g. whether
3239 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003240 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3241 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3242 if (LHSIface && RHSIface &&
3243 canAssignObjCInterfaces(LHSIface, RHSIface))
3244 return LHS;
3245
Eli Friedman3d815e72008-08-22 00:56:42 +00003246 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003247 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003248 case Type::ObjCQualifiedId:
3249 // Distinct qualified id's are not compatible.
3250 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003251 case Type::FixedWidthInt:
3252 // Distinct fixed-width integers are not compatible.
3253 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003254 case Type::ExtQual:
3255 // FIXME: ExtQual types can be compatible even if they're not
3256 // identical!
3257 return QualType();
3258 // First attempt at an implementation, but I'm not really sure it's
3259 // right...
3260#if 0
3261 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3262 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3263 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3264 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3265 return QualType();
3266 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3267 LHSBase = QualType(LQual->getBaseType(), 0);
3268 RHSBase = QualType(RQual->getBaseType(), 0);
3269 ResultType = mergeTypes(LHSBase, RHSBase);
3270 if (ResultType.isNull()) return QualType();
3271 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3272 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3273 return LHS;
3274 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3275 return RHS;
3276 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3277 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3278 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3279 return ResultType;
3280#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003281
3282 case Type::TemplateSpecialization:
3283 assert(false && "Dependent types have no size");
3284 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003285 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003286
3287 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003288}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003289
Chris Lattner5426bf62008-04-07 07:01:58 +00003290//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003291// Integer Predicates
3292//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003293
Eli Friedmanad74a752008-06-28 06:23:08 +00003294unsigned ASTContext::getIntWidth(QualType T) {
3295 if (T == BoolTy)
3296 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003297 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3298 return FWIT->getWidth();
3299 }
3300 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003301 return (unsigned)getTypeSize(T);
3302}
3303
3304QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3305 assert(T->isSignedIntegerType() && "Unexpected type");
3306 if (const EnumType* ETy = T->getAsEnumType())
3307 T = ETy->getDecl()->getIntegerType();
3308 const BuiltinType* BTy = T->getAsBuiltinType();
3309 assert (BTy && "Unexpected signed integer type");
3310 switch (BTy->getKind()) {
3311 case BuiltinType::Char_S:
3312 case BuiltinType::SChar:
3313 return UnsignedCharTy;
3314 case BuiltinType::Short:
3315 return UnsignedShortTy;
3316 case BuiltinType::Int:
3317 return UnsignedIntTy;
3318 case BuiltinType::Long:
3319 return UnsignedLongTy;
3320 case BuiltinType::LongLong:
3321 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00003322 case BuiltinType::Int128:
3323 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00003324 default:
3325 assert(0 && "Unexpected signed integer type");
3326 return QualType();
3327 }
3328}
3329
Douglas Gregor2cf26342009-04-09 22:27:44 +00003330ExternalASTSource::~ExternalASTSource() { }
3331
3332void ExternalASTSource::PrintStats() { }