blob: ac70cef499b42092c790f7b479558af4cff16ce5 [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) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000747 assert(!D->isForwardDecl() && "Invalid interface decl!");
748
Devang Patel44a3dde2008-06-04 21:54:36 +0000749 // Look up this layout, if already laid out, return what we have.
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000750 ObjCContainerDecl *Key =
751 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
752 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
753 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000754
Daniel Dunbar453addb2009-05-03 11:16:44 +0000755 unsigned FieldCount = D->ivar_size();
756 // Add in synthesized ivar count if laying out an implementation.
757 if (Impl) {
758 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
759 E = D->prop_end(*this); I != E; ++I)
760 if ((*I)->getPropertyIvarDecl())
761 ++FieldCount;
762
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000763 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +0000764 // entry. Note we can't cache this because we simply free all
765 // entries later; however we shouldn't look up implementations
766 // frequently.
767 if (FieldCount == D->ivar_size())
768 return getObjCLayout(D, 0);
769 }
770
Devang Patel6a5a34c2008-06-06 02:14:01 +0000771 ASTRecordLayout *NewEntry = NULL;
Devang Patel6a5a34c2008-06-06 02:14:01 +0000772 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000773 // FIXME: This increment of FieldCount is wrong, we don't actually
774 // count the super class as a member (see the field index passed
775 // to LayoutField below).
Devang Patel6a5a34c2008-06-06 02:14:01 +0000776 FieldCount++;
777 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
778 unsigned Alignment = SL.getAlignment();
779 uint64_t Size = SL.getSize();
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000780
781 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout(Size, Alignment);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000782 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000783 // Super class is at the beginning of the layout.
784 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000785 } else {
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000786 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout();
Devang Patel6a5a34c2008-06-06 02:14:01 +0000787 NewEntry->InitializeLayout(FieldCount);
788 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000789
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000790 unsigned StructPacking = 0;
791 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
792 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000793
794 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
795 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
796 AA->getAlignment()));
797
798 // Layout each ivar sequentially.
799 unsigned i = 0;
800 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
801 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
802 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000803 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000804 }
Daniel Dunbar453addb2009-05-03 11:16:44 +0000805 // And synthesized ivars, if this is an implementation.
806 if (Impl) {
807 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
808 E = D->prop_end(*this); I != E; ++I) {
809 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
810 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
811 }
Fariborz Jahanian18191882009-03-31 18:11:23 +0000812 }
Fariborz Jahanian99eee362009-04-01 19:37:34 +0000813
Devang Patel44a3dde2008-06-04 21:54:36 +0000814 // Finally, round the size of the total struct up to the alignment of the
815 // struct itself.
816 NewEntry->FinalizeLayout();
817 return *NewEntry;
818}
819
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000820const ASTRecordLayout &
821ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
822 return getObjCLayout(D, 0);
823}
824
825const ASTRecordLayout &
826ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
827 return getObjCLayout(D->getClassInterface(), D);
828}
829
Devang Patel88a981b2007-11-01 19:11:01 +0000830/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000831/// specified record (struct/union/class), which indicates its size and field
832/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000833const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000834 D = D->getDefinition(*this);
835 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000836
Chris Lattner464175b2007-07-18 17:52:12 +0000837 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000838 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000839 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000840
Devang Patel88a981b2007-11-01 19:11:01 +0000841 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
842 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
843 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000844 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000845
Douglas Gregore267ff32008-12-11 20:41:00 +0000846 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000847 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
848 D->field_end(*this)));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000849 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000850
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000851 unsigned StructPacking = 0;
852 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
853 StructPacking = PA->getAlignment();
854
Eli Friedman4bd998b2008-05-30 09:31:38 +0000855 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000856 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
857 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000858
Eli Friedman4bd998b2008-05-30 09:31:38 +0000859 // Layout each field, for now, just sequentially, respecting alignment. In
860 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000861 unsigned FieldIdx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000862 for (RecordDecl::field_iterator Field = D->field_begin(*this),
863 FieldEnd = D->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000864 Field != FieldEnd; (void)++Field, ++FieldIdx)
865 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000866
867 // Finally, round the size of the total struct up to the alignment of the
868 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000869 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000870 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000871}
872
Chris Lattnera7674d82007-07-13 22:13:22 +0000873//===----------------------------------------------------------------------===//
874// Type creation/memoization methods
875//===----------------------------------------------------------------------===//
876
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000877QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000878 QualType CanT = getCanonicalType(T);
879 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000880 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000881
882 // If we are composing extended qualifiers together, merge together into one
883 // ExtQualType node.
884 unsigned CVRQuals = T.getCVRQualifiers();
885 QualType::GCAttrTypes GCAttr = QualType::GCNone;
886 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000887
Chris Lattnerb7d25532009-02-18 22:53:11 +0000888 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
889 // If this type already has an address space specified, it cannot get
890 // another one.
891 assert(EQT->getAddressSpace() == 0 &&
892 "Type cannot be in multiple addr spaces!");
893 GCAttr = EQT->getObjCGCAttr();
894 TypeNode = EQT->getBaseType();
895 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000896
Chris Lattnerb7d25532009-02-18 22:53:11 +0000897 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000898 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000899 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000900 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000901 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000902 return QualType(EXTQy, CVRQuals);
903
Christopher Lambebb97e92008-02-04 02:31:56 +0000904 // If the base type isn't canonical, this won't be a canonical type either,
905 // so fill in the canonical type field.
906 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000907 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000908 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000909
Chris Lattnerb7d25532009-02-18 22:53:11 +0000910 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000911 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000912 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000913 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000914 ExtQualType *New =
915 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000916 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000917 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000918 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000919}
920
Chris Lattnerb7d25532009-02-18 22:53:11 +0000921QualType ASTContext::getObjCGCQualType(QualType T,
922 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000923 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000924 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000925 return T;
926
Chris Lattnerb7d25532009-02-18 22:53:11 +0000927 // If we are composing extended qualifiers together, merge together into one
928 // ExtQualType node.
929 unsigned CVRQuals = T.getCVRQualifiers();
930 Type *TypeNode = T.getTypePtr();
931 unsigned AddressSpace = 0;
932
933 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
934 // If this type already has an address space specified, it cannot get
935 // another one.
936 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
937 "Type cannot be in multiple addr spaces!");
938 AddressSpace = EQT->getAddressSpace();
939 TypeNode = EQT->getBaseType();
940 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000941
942 // Check if we've already instantiated an gc qual'd type of this type.
943 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000944 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000945 void *InsertPos = 0;
946 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000947 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000948
949 // If the base type isn't canonical, this won't be a canonical type either,
950 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000951 // FIXME: Isn't this also not canonical if the base type is a array
952 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000953 QualType Canonical;
954 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000955 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000956
Chris Lattnerb7d25532009-02-18 22:53:11 +0000957 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000958 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
959 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
960 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000961 ExtQualType *New =
962 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000963 ExtQualTypes.InsertNode(New, InsertPos);
964 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000965 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000966}
Chris Lattnera7674d82007-07-13 22:13:22 +0000967
Reid Spencer5f016e22007-07-11 17:01:13 +0000968/// getComplexType - Return the uniqued reference to the type for a complex
969/// number with the specified element type.
970QualType ASTContext::getComplexType(QualType T) {
971 // Unique pointers, to guarantee there is only one pointer of a particular
972 // structure.
973 llvm::FoldingSetNodeID ID;
974 ComplexType::Profile(ID, T);
975
976 void *InsertPos = 0;
977 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
978 return QualType(CT, 0);
979
980 // If the pointee type isn't canonical, this won't be a canonical type either,
981 // so fill in the canonical type field.
982 QualType Canonical;
983 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000984 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000985
986 // Get the new insert position for the node we care about.
987 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000988 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000989 }
Steve Narofff83820b2009-01-27 22:08:43 +0000990 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000991 Types.push_back(New);
992 ComplexTypes.InsertNode(New, InsertPos);
993 return QualType(New, 0);
994}
995
Eli Friedmanf98aba32009-02-13 02:31:07 +0000996QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
997 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
998 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
999 FixedWidthIntType *&Entry = Map[Width];
1000 if (!Entry)
1001 Entry = new FixedWidthIntType(Width, Signed);
1002 return QualType(Entry, 0);
1003}
Reid Spencer5f016e22007-07-11 17:01:13 +00001004
1005/// getPointerType - Return the uniqued reference to the type for a pointer to
1006/// the specified type.
1007QualType ASTContext::getPointerType(QualType T) {
1008 // Unique pointers, to guarantee there is only one pointer of a particular
1009 // structure.
1010 llvm::FoldingSetNodeID ID;
1011 PointerType::Profile(ID, T);
1012
1013 void *InsertPos = 0;
1014 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1015 return QualType(PT, 0);
1016
1017 // If the pointee type isn't canonical, this won't be a canonical type either,
1018 // so fill in the canonical type field.
1019 QualType Canonical;
1020 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001021 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +00001022
1023 // Get the new insert position for the node we care about.
1024 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001025 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001026 }
Steve Narofff83820b2009-01-27 22:08:43 +00001027 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001028 Types.push_back(New);
1029 PointerTypes.InsertNode(New, InsertPos);
1030 return QualType(New, 0);
1031}
1032
Steve Naroff5618bd42008-08-27 16:04:49 +00001033/// getBlockPointerType - Return the uniqued reference to the type for
1034/// a pointer to the specified block.
1035QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +00001036 assert(T->isFunctionType() && "block of function types only");
1037 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +00001038 // structure.
1039 llvm::FoldingSetNodeID ID;
1040 BlockPointerType::Profile(ID, T);
1041
1042 void *InsertPos = 0;
1043 if (BlockPointerType *PT =
1044 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1045 return QualType(PT, 0);
1046
Steve Naroff296e8d52008-08-28 19:20:44 +00001047 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +00001048 // type either so fill in the canonical type field.
1049 QualType Canonical;
1050 if (!T->isCanonical()) {
1051 Canonical = getBlockPointerType(getCanonicalType(T));
1052
1053 // Get the new insert position for the node we care about.
1054 BlockPointerType *NewIP =
1055 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001056 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001057 }
Steve Narofff83820b2009-01-27 22:08:43 +00001058 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001059 Types.push_back(New);
1060 BlockPointerTypes.InsertNode(New, InsertPos);
1061 return QualType(New, 0);
1062}
1063
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001064/// getLValueReferenceType - Return the uniqued reference to the type for an
1065/// lvalue reference to the specified type.
1066QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001067 // Unique pointers, to guarantee there is only one pointer of a particular
1068 // structure.
1069 llvm::FoldingSetNodeID ID;
1070 ReferenceType::Profile(ID, T);
1071
1072 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001073 if (LValueReferenceType *RT =
1074 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001075 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001076
Reid Spencer5f016e22007-07-11 17:01:13 +00001077 // If the referencee type isn't canonical, this won't be a canonical type
1078 // either, so fill in the canonical type field.
1079 QualType Canonical;
1080 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001081 Canonical = getLValueReferenceType(getCanonicalType(T));
1082
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001084 LValueReferenceType *NewIP =
1085 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001086 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 }
1088
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001089 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001091 LValueReferenceTypes.InsertNode(New, InsertPos);
1092 return QualType(New, 0);
1093}
1094
1095/// getRValueReferenceType - Return the uniqued reference to the type for an
1096/// rvalue reference to the specified type.
1097QualType ASTContext::getRValueReferenceType(QualType T) {
1098 // Unique pointers, to guarantee there is only one pointer of a particular
1099 // structure.
1100 llvm::FoldingSetNodeID ID;
1101 ReferenceType::Profile(ID, T);
1102
1103 void *InsertPos = 0;
1104 if (RValueReferenceType *RT =
1105 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1106 return QualType(RT, 0);
1107
1108 // If the referencee type isn't canonical, this won't be a canonical type
1109 // either, so fill in the canonical type field.
1110 QualType Canonical;
1111 if (!T->isCanonical()) {
1112 Canonical = getRValueReferenceType(getCanonicalType(T));
1113
1114 // Get the new insert position for the node we care about.
1115 RValueReferenceType *NewIP =
1116 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1117 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1118 }
1119
1120 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1121 Types.push_back(New);
1122 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 return QualType(New, 0);
1124}
1125
Sebastian Redlf30208a2009-01-24 21:16:55 +00001126/// getMemberPointerType - Return the uniqued reference to the type for a
1127/// member pointer to the specified type, in the specified class.
1128QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1129{
1130 // Unique pointers, to guarantee there is only one pointer of a particular
1131 // structure.
1132 llvm::FoldingSetNodeID ID;
1133 MemberPointerType::Profile(ID, T, Cls);
1134
1135 void *InsertPos = 0;
1136 if (MemberPointerType *PT =
1137 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1138 return QualType(PT, 0);
1139
1140 // If the pointee or class type isn't canonical, this won't be a canonical
1141 // type either, so fill in the canonical type field.
1142 QualType Canonical;
1143 if (!T->isCanonical()) {
1144 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1145
1146 // Get the new insert position for the node we care about.
1147 MemberPointerType *NewIP =
1148 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1149 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1150 }
Steve Narofff83820b2009-01-27 22:08:43 +00001151 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001152 Types.push_back(New);
1153 MemberPointerTypes.InsertNode(New, InsertPos);
1154 return QualType(New, 0);
1155}
1156
Steve Narofffb22d962007-08-30 01:06:46 +00001157/// getConstantArrayType - Return the unique reference to the type for an
1158/// array of the specified element type.
1159QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +00001160 const llvm::APInt &ArySize,
1161 ArrayType::ArraySizeModifier ASM,
1162 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001163 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001164 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001165
1166 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001167 if (ConstantArrayType *ATP =
1168 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001169 return QualType(ATP, 0);
1170
1171 // If the element type isn't canonical, this won't be a canonical type either,
1172 // so fill in the canonical type field.
1173 QualType Canonical;
1174 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001175 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001176 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001177 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001178 ConstantArrayType *NewIP =
1179 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001180 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 }
1182
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001183 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001184 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001185 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 Types.push_back(New);
1187 return QualType(New, 0);
1188}
1189
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001190/// getVariableArrayType - Returns a non-unique reference to the type for a
1191/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001192QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1193 ArrayType::ArraySizeModifier ASM,
1194 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001195 // Since we don't unique expressions, it isn't possible to unique VLA's
1196 // that have an expression provided for their size.
1197
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001198 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001199 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001200
1201 VariableArrayTypes.push_back(New);
1202 Types.push_back(New);
1203 return QualType(New, 0);
1204}
1205
Douglas Gregor898574e2008-12-05 23:32:09 +00001206/// getDependentSizedArrayType - Returns a non-unique reference to
1207/// the type for a dependently-sized array of the specified element
1208/// type. FIXME: We will need these to be uniqued, or at least
1209/// comparable, at some point.
1210QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1211 ArrayType::ArraySizeModifier ASM,
1212 unsigned EltTypeQuals) {
1213 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1214 "Size must be type- or value-dependent!");
1215
1216 // Since we don't unique expressions, it isn't possible to unique
1217 // dependently-sized array types.
1218
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001219 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001220 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1221 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001222
1223 DependentSizedArrayTypes.push_back(New);
1224 Types.push_back(New);
1225 return QualType(New, 0);
1226}
1227
Eli Friedmanc5773c42008-02-15 18:16:39 +00001228QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1229 ArrayType::ArraySizeModifier ASM,
1230 unsigned EltTypeQuals) {
1231 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001232 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001233
1234 void *InsertPos = 0;
1235 if (IncompleteArrayType *ATP =
1236 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1237 return QualType(ATP, 0);
1238
1239 // If the element type isn't canonical, this won't be a canonical type
1240 // either, so fill in the canonical type field.
1241 QualType Canonical;
1242
1243 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001244 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001245 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001246
1247 // Get the new insert position for the node we care about.
1248 IncompleteArrayType *NewIP =
1249 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001250 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001251 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001252
Steve Narofff83820b2009-01-27 22:08:43 +00001253 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001254 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001255
1256 IncompleteArrayTypes.InsertNode(New, InsertPos);
1257 Types.push_back(New);
1258 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001259}
1260
Steve Naroff73322922007-07-18 18:00:27 +00001261/// getVectorType - Return the unique reference to a vector type of
1262/// the specified element type and size. VectorType must be a built-in type.
1263QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001264 BuiltinType *baseType;
1265
Chris Lattnerf52ab252008-04-06 22:59:24 +00001266 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001267 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001268
1269 // Check if we've already instantiated a vector of this type.
1270 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001271 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 void *InsertPos = 0;
1273 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1274 return QualType(VTP, 0);
1275
1276 // If the element type isn't canonical, this won't be a canonical type either,
1277 // so fill in the canonical type field.
1278 QualType Canonical;
1279 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001280 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001281
1282 // Get the new insert position for the node we care about.
1283 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001284 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001285 }
Steve Narofff83820b2009-01-27 22:08:43 +00001286 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001287 VectorTypes.InsertNode(New, InsertPos);
1288 Types.push_back(New);
1289 return QualType(New, 0);
1290}
1291
Nate Begeman213541a2008-04-18 23:10:10 +00001292/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001293/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001294QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001295 BuiltinType *baseType;
1296
Chris Lattnerf52ab252008-04-06 22:59:24 +00001297 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001298 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001299
1300 // Check if we've already instantiated a vector of this type.
1301 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001302 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001303 void *InsertPos = 0;
1304 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1305 return QualType(VTP, 0);
1306
1307 // If the element type isn't canonical, this won't be a canonical type either,
1308 // so fill in the canonical type field.
1309 QualType Canonical;
1310 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001311 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001312
1313 // Get the new insert position for the node we care about.
1314 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001315 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001316 }
Steve Narofff83820b2009-01-27 22:08:43 +00001317 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001318 VectorTypes.InsertNode(New, InsertPos);
1319 Types.push_back(New);
1320 return QualType(New, 0);
1321}
1322
Douglas Gregor72564e72009-02-26 23:50:07 +00001323/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001324///
Douglas Gregor72564e72009-02-26 23:50:07 +00001325QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001326 // Unique functions, to guarantee there is only one function of a particular
1327 // structure.
1328 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001329 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001330
1331 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001332 if (FunctionNoProtoType *FT =
1333 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001334 return QualType(FT, 0);
1335
1336 QualType Canonical;
1337 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001338 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001339
1340 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001341 FunctionNoProtoType *NewIP =
1342 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001343 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001344 }
1345
Douglas Gregor72564e72009-02-26 23:50:07 +00001346 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001347 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001348 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001349 return QualType(New, 0);
1350}
1351
1352/// getFunctionType - Return a normal function type with a typed argument
1353/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001354QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001355 unsigned NumArgs, bool isVariadic,
Sebastian Redl7b9a2ee2009-04-30 19:20:55 +00001356 unsigned TypeQuals, bool hasExceptionSpec,
1357 bool hasAnyExceptionSpec, unsigned NumExs,
1358 const QualType *ExArray) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001359 // Unique functions, to guarantee there is only one function of a particular
1360 // structure.
1361 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001362 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl7b9a2ee2009-04-30 19:20:55 +00001363 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1364 NumExs, ExArray);
Reid Spencer5f016e22007-07-11 17:01:13 +00001365
1366 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001367 if (FunctionProtoType *FTP =
1368 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001369 return QualType(FTP, 0);
1370
1371 // Determine whether the type being created is already canonical or not.
1372 bool isCanonical = ResultTy->isCanonical();
1373 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1374 if (!ArgArray[i]->isCanonical())
1375 isCanonical = false;
1376
1377 // If this type isn't canonical, get the canonical version of it.
1378 QualType Canonical;
1379 if (!isCanonical) {
1380 llvm::SmallVector<QualType, 16> CanonicalArgs;
1381 CanonicalArgs.reserve(NumArgs);
1382 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001383 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redl7b9a2ee2009-04-30 19:20:55 +00001384 llvm::SmallVector<QualType, 2> CanonicalExs;
1385 CanonicalExs.reserve(NumExs);
1386 for (unsigned i = 0; i != NumExs; ++i)
1387 CanonicalExs.push_back(getCanonicalType(ExArray[i]));
1388
Chris Lattnerf52ab252008-04-06 22:59:24 +00001389 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001390 &CanonicalArgs[0], NumArgs,
Sebastian Redl7b9a2ee2009-04-30 19:20:55 +00001391 isVariadic, TypeQuals, hasExceptionSpec,
1392 hasAnyExceptionSpec, NumExs, &CanonicalExs[0]);
1393
Reid Spencer5f016e22007-07-11 17:01:13 +00001394 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001395 FunctionProtoType *NewIP =
1396 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001397 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001398 }
Sebastian Redl7b9a2ee2009-04-30 19:20:55 +00001399
Douglas Gregor72564e72009-02-26 23:50:07 +00001400 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl7b9a2ee2009-04-30 19:20:55 +00001401 // for two variable size arrays (for parameter and exception types) at the
1402 // end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001403 FunctionProtoType *FTP =
Sebastian Redl7b9a2ee2009-04-30 19:20:55 +00001404 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1405 NumArgs*sizeof(QualType) +
1406 NumExs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001407 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl7b9a2ee2009-04-30 19:20:55 +00001408 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1409 ExArray, NumExs, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001410 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001411 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001412 return QualType(FTP, 0);
1413}
1414
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001415/// getTypeDeclType - Return the unique reference to the type for the
1416/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001417QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001418 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001419 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1420
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001421 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001422 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001423 else if (isa<TemplateTypeParmDecl>(Decl)) {
1424 assert(false && "Template type parameter types are always available.");
1425 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001426 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001427
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001428 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001429 if (PrevDecl)
1430 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001431 else
1432 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001433 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001434 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1435 if (PrevDecl)
1436 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001437 else
1438 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001439 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001440 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001441 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001442
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001443 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001444 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001445}
1446
Reid Spencer5f016e22007-07-11 17:01:13 +00001447/// getTypedefType - Return the unique reference to the type for the
1448/// specified typename decl.
1449QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1450 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1451
Chris Lattnerf52ab252008-04-06 22:59:24 +00001452 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001453 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001454 Types.push_back(Decl->TypeForDecl);
1455 return QualType(Decl->TypeForDecl, 0);
1456}
1457
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001458/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001459/// specified ObjC interface decl.
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001460QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001461 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1462
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001463 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1464 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff3536b442007-09-06 21:24:23 +00001465 Types.push_back(Decl->TypeForDecl);
1466 return QualType(Decl->TypeForDecl, 0);
1467}
1468
Douglas Gregorfab9d672009-02-05 23:33:38 +00001469/// \brief Retrieve the template type parameter type for a template
1470/// parameter with the given depth, index, and (optionally) name.
1471QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1472 IdentifierInfo *Name) {
1473 llvm::FoldingSetNodeID ID;
1474 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1475 void *InsertPos = 0;
1476 TemplateTypeParmType *TypeParm
1477 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1478
1479 if (TypeParm)
1480 return QualType(TypeParm, 0);
1481
1482 if (Name)
1483 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1484 getTemplateTypeParmType(Depth, Index));
1485 else
1486 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1487
1488 Types.push_back(TypeParm);
1489 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1490
1491 return QualType(TypeParm, 0);
1492}
1493
Douglas Gregor55f6b142009-02-09 18:46:07 +00001494QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001495ASTContext::getTemplateSpecializationType(TemplateName Template,
1496 const TemplateArgument *Args,
1497 unsigned NumArgs,
1498 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001499 if (!Canon.isNull())
1500 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001501
Douglas Gregor55f6b142009-02-09 18:46:07 +00001502 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001503 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001504
Douglas Gregor55f6b142009-02-09 18:46:07 +00001505 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001506 TemplateSpecializationType *Spec
1507 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001508
1509 if (Spec)
1510 return QualType(Spec, 0);
1511
Douglas Gregor7532dc62009-03-30 22:58:21 +00001512 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001513 sizeof(TemplateArgument) * NumArgs),
1514 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001515 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001516 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001517 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001518
1519 return QualType(Spec, 0);
1520}
1521
Douglas Gregore4e5b052009-03-19 00:18:19 +00001522QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001523ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001524 QualType NamedType) {
1525 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001526 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001527
1528 void *InsertPos = 0;
1529 QualifiedNameType *T
1530 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1531 if (T)
1532 return QualType(T, 0);
1533
Douglas Gregorab452ba2009-03-26 23:50:42 +00001534 T = new (*this) QualifiedNameType(NNS, NamedType,
1535 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001536 Types.push_back(T);
1537 QualifiedNameTypes.InsertNode(T, InsertPos);
1538 return QualType(T, 0);
1539}
1540
Douglas Gregord57959a2009-03-27 23:10:48 +00001541QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1542 const IdentifierInfo *Name,
1543 QualType Canon) {
1544 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1545
1546 if (Canon.isNull()) {
1547 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1548 if (CanonNNS != NNS)
1549 Canon = getTypenameType(CanonNNS, Name);
1550 }
1551
1552 llvm::FoldingSetNodeID ID;
1553 TypenameType::Profile(ID, NNS, Name);
1554
1555 void *InsertPos = 0;
1556 TypenameType *T
1557 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1558 if (T)
1559 return QualType(T, 0);
1560
1561 T = new (*this) TypenameType(NNS, Name, Canon);
1562 Types.push_back(T);
1563 TypenameTypes.InsertNode(T, InsertPos);
1564 return QualType(T, 0);
1565}
1566
Douglas Gregor17343172009-04-01 00:28:59 +00001567QualType
1568ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1569 const TemplateSpecializationType *TemplateId,
1570 QualType Canon) {
1571 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1572
1573 if (Canon.isNull()) {
1574 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1575 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1576 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1577 const TemplateSpecializationType *CanonTemplateId
1578 = CanonType->getAsTemplateSpecializationType();
1579 assert(CanonTemplateId &&
1580 "Canonical type must also be a template specialization type");
1581 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1582 }
1583 }
1584
1585 llvm::FoldingSetNodeID ID;
1586 TypenameType::Profile(ID, NNS, TemplateId);
1587
1588 void *InsertPos = 0;
1589 TypenameType *T
1590 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1591 if (T)
1592 return QualType(T, 0);
1593
1594 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1595 Types.push_back(T);
1596 TypenameTypes.InsertNode(T, InsertPos);
1597 return QualType(T, 0);
1598}
1599
Chris Lattner88cb27a2008-04-07 04:56:42 +00001600/// CmpProtocolNames - Comparison predicate for sorting protocols
1601/// alphabetically.
1602static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1603 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001604 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001605}
1606
1607static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1608 unsigned &NumProtocols) {
1609 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1610
1611 // Sort protocols, keyed by name.
1612 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1613
1614 // Remove duplicates.
1615 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1616 NumProtocols = ProtocolsEnd-Protocols;
1617}
1618
1619
Chris Lattner065f0d72008-04-07 04:44:08 +00001620/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1621/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001622QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1623 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001624 // Sort the protocol list alphabetically to canonicalize it.
1625 SortAndUniqueProtocols(Protocols, NumProtocols);
1626
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001627 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001628 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001629
1630 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001631 if (ObjCQualifiedInterfaceType *QT =
1632 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001633 return QualType(QT, 0);
1634
1635 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001636 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001637 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001638
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001639 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001640 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001641 return QualType(QType, 0);
1642}
1643
Chris Lattner88cb27a2008-04-07 04:56:42 +00001644/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1645/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001646QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001647 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001648 // Sort the protocol list alphabetically to canonicalize it.
1649 SortAndUniqueProtocols(Protocols, NumProtocols);
1650
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001651 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001652 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001653
1654 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001655 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001656 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001657 return QualType(QT, 0);
1658
1659 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001660 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001661 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001662 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001663 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001664 return QualType(QType, 0);
1665}
1666
Douglas Gregor72564e72009-02-26 23:50:07 +00001667/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1668/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001669/// multiple declarations that refer to "typeof(x)" all contain different
1670/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1671/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001672QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001673 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001674 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001675 Types.push_back(toe);
1676 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001677}
1678
Steve Naroff9752f252007-08-01 18:02:17 +00001679/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1680/// TypeOfType AST's. The only motivation to unique these nodes would be
1681/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1682/// an issue. This doesn't effect the type checker, since it operates
1683/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001684QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001685 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001686 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001687 Types.push_back(tot);
1688 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001689}
1690
Reid Spencer5f016e22007-07-11 17:01:13 +00001691/// getTagDeclType - Return the unique reference to the type for the
1692/// specified TagDecl (struct/union/class/enum) decl.
1693QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001694 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001695 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001696}
1697
1698/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1699/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1700/// needs to agree with the definition in <stddef.h>.
1701QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001702 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001703}
1704
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001705/// getSignedWCharType - Return the type of "signed wchar_t".
1706/// Used when in C++, as a GCC extension.
1707QualType ASTContext::getSignedWCharType() const {
1708 // FIXME: derive from "Target" ?
1709 return WCharTy;
1710}
1711
1712/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1713/// Used when in C++, as a GCC extension.
1714QualType ASTContext::getUnsignedWCharType() const {
1715 // FIXME: derive from "Target" ?
1716 return UnsignedIntTy;
1717}
1718
Chris Lattner8b9023b2007-07-13 03:05:23 +00001719/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1720/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1721QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001722 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001723}
1724
Chris Lattnere6327742008-04-02 05:18:44 +00001725//===----------------------------------------------------------------------===//
1726// Type Operators
1727//===----------------------------------------------------------------------===//
1728
Chris Lattner77c96472008-04-06 22:41:35 +00001729/// getCanonicalType - Return the canonical (structural) type corresponding to
1730/// the specified potentially non-canonical type. The non-canonical version
1731/// of a type may have many "decorated" versions of types. Decorators can
1732/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1733/// to be free of any of these, allowing two canonical types to be compared
1734/// for exact equality with a simple pointer comparison.
1735QualType ASTContext::getCanonicalType(QualType T) {
1736 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001737
1738 // If the result has type qualifiers, make sure to canonicalize them as well.
1739 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1740 if (TypeQuals == 0) return CanType;
1741
1742 // If the type qualifiers are on an array type, get the canonical type of the
1743 // array with the qualifiers applied to the element type.
1744 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1745 if (!AT)
1746 return CanType.getQualifiedType(TypeQuals);
1747
1748 // Get the canonical version of the element with the extra qualifiers on it.
1749 // This can recursively sink qualifiers through multiple levels of arrays.
1750 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1751 NewEltTy = getCanonicalType(NewEltTy);
1752
1753 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1754 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1755 CAT->getIndexTypeQualifier());
1756 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1757 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1758 IAT->getIndexTypeQualifier());
1759
Douglas Gregor898574e2008-12-05 23:32:09 +00001760 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1761 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1762 DSAT->getSizeModifier(),
1763 DSAT->getIndexTypeQualifier());
1764
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001765 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1766 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1767 VAT->getSizeModifier(),
1768 VAT->getIndexTypeQualifier());
1769}
1770
Douglas Gregord57959a2009-03-27 23:10:48 +00001771NestedNameSpecifier *
1772ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1773 if (!NNS)
1774 return 0;
1775
1776 switch (NNS->getKind()) {
1777 case NestedNameSpecifier::Identifier:
1778 // Canonicalize the prefix but keep the identifier the same.
1779 return NestedNameSpecifier::Create(*this,
1780 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1781 NNS->getAsIdentifier());
1782
1783 case NestedNameSpecifier::Namespace:
1784 // A namespace is canonical; build a nested-name-specifier with
1785 // this namespace and no prefix.
1786 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1787
1788 case NestedNameSpecifier::TypeSpec:
1789 case NestedNameSpecifier::TypeSpecWithTemplate: {
1790 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1791 NestedNameSpecifier *Prefix = 0;
1792
1793 // FIXME: This isn't the right check!
1794 if (T->isDependentType())
1795 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1796
1797 return NestedNameSpecifier::Create(*this, Prefix,
1798 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1799 T.getTypePtr());
1800 }
1801
1802 case NestedNameSpecifier::Global:
1803 // The global specifier is canonical and unique.
1804 return NNS;
1805 }
1806
1807 // Required to silence a GCC warning
1808 return 0;
1809}
1810
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001811
1812const ArrayType *ASTContext::getAsArrayType(QualType T) {
1813 // Handle the non-qualified case efficiently.
1814 if (T.getCVRQualifiers() == 0) {
1815 // Handle the common positive case fast.
1816 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1817 return AT;
1818 }
1819
1820 // Handle the common negative case fast, ignoring CVR qualifiers.
1821 QualType CType = T->getCanonicalTypeInternal();
1822
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001823 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001824 // test.
1825 if (!isa<ArrayType>(CType) &&
1826 !isa<ArrayType>(CType.getUnqualifiedType()))
1827 return 0;
1828
1829 // Apply any CVR qualifiers from the array type to the element type. This
1830 // implements C99 6.7.3p8: "If the specification of an array type includes
1831 // any type qualifiers, the element type is so qualified, not the array type."
1832
1833 // If we get here, we either have type qualifiers on the type, or we have
1834 // sugar such as a typedef in the way. If we have type qualifiers on the type
1835 // we must propagate them down into the elemeng type.
1836 unsigned CVRQuals = T.getCVRQualifiers();
1837 unsigned AddrSpace = 0;
1838 Type *Ty = T.getTypePtr();
1839
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001840 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001841 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001842 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1843 AddrSpace = EXTQT->getAddressSpace();
1844 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001845 } else {
1846 T = Ty->getDesugaredType();
1847 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1848 break;
1849 CVRQuals |= T.getCVRQualifiers();
1850 Ty = T.getTypePtr();
1851 }
1852 }
1853
1854 // If we have a simple case, just return now.
1855 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1856 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1857 return ATy;
1858
1859 // Otherwise, we have an array and we have qualifiers on it. Push the
1860 // qualifiers into the array element type and return a new array type.
1861 // Get the canonical version of the element with the extra qualifiers on it.
1862 // This can recursively sink qualifiers through multiple levels of arrays.
1863 QualType NewEltTy = ATy->getElementType();
1864 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001865 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001866 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1867
1868 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1869 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1870 CAT->getSizeModifier(),
1871 CAT->getIndexTypeQualifier()));
1872 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1873 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1874 IAT->getSizeModifier(),
1875 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001876
Douglas Gregor898574e2008-12-05 23:32:09 +00001877 if (const DependentSizedArrayType *DSAT
1878 = dyn_cast<DependentSizedArrayType>(ATy))
1879 return cast<ArrayType>(
1880 getDependentSizedArrayType(NewEltTy,
1881 DSAT->getSizeExpr(),
1882 DSAT->getSizeModifier(),
1883 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001884
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001885 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1886 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1887 VAT->getSizeModifier(),
1888 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001889}
1890
1891
Chris Lattnere6327742008-04-02 05:18:44 +00001892/// getArrayDecayedType - Return the properly qualified result of decaying the
1893/// specified array type to a pointer. This operation is non-trivial when
1894/// handling typedefs etc. The canonical type of "T" must be an array type,
1895/// this returns a pointer to a properly qualified element of the array.
1896///
1897/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1898QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001899 // Get the element type with 'getAsArrayType' so that we don't lose any
1900 // typedefs in the element type of the array. This also handles propagation
1901 // of type qualifiers from the array type into the element type if present
1902 // (C99 6.7.3p8).
1903 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1904 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001905
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001906 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001907
1908 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001909 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001910}
1911
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001912QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001913 QualType ElemTy = VAT->getElementType();
1914
1915 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1916 return getBaseElementType(VAT);
1917
1918 return ElemTy;
1919}
1920
Reid Spencer5f016e22007-07-11 17:01:13 +00001921/// getFloatingRank - Return a relative rank for floating point types.
1922/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001923static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001924 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001925 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001926
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001927 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001928 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001929 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001930 case BuiltinType::Float: return FloatRank;
1931 case BuiltinType::Double: return DoubleRank;
1932 case BuiltinType::LongDouble: return LongDoubleRank;
1933 }
1934}
1935
Steve Naroff716c7302007-08-27 01:41:48 +00001936/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1937/// point or a complex type (based on typeDomain/typeSize).
1938/// 'typeDomain' is a real floating point or complex type.
1939/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001940QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1941 QualType Domain) const {
1942 FloatingRank EltRank = getFloatingRank(Size);
1943 if (Domain->isComplexType()) {
1944 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001945 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001946 case FloatRank: return FloatComplexTy;
1947 case DoubleRank: return DoubleComplexTy;
1948 case LongDoubleRank: return LongDoubleComplexTy;
1949 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001950 }
Chris Lattner1361b112008-04-06 23:58:54 +00001951
1952 assert(Domain->isRealFloatingType() && "Unknown domain!");
1953 switch (EltRank) {
1954 default: assert(0 && "getFloatingRank(): illegal value for rank");
1955 case FloatRank: return FloatTy;
1956 case DoubleRank: return DoubleTy;
1957 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001958 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001959}
1960
Chris Lattner7cfeb082008-04-06 23:55:33 +00001961/// getFloatingTypeOrder - Compare the rank of the two specified floating
1962/// point types, ignoring the domain of the type (i.e. 'double' ==
1963/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1964/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001965int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1966 FloatingRank LHSR = getFloatingRank(LHS);
1967 FloatingRank RHSR = getFloatingRank(RHS);
1968
1969 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001970 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001971 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001972 return 1;
1973 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001974}
1975
Chris Lattnerf52ab252008-04-06 22:59:24 +00001976/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1977/// routine will assert if passed a built-in type that isn't an integer or enum,
1978/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001979unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001980 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001981 if (EnumType* ET = dyn_cast<EnumType>(T))
1982 T = ET->getDecl()->getIntegerType().getTypePtr();
1983
1984 // There are two things which impact the integer rank: the width, and
1985 // the ordering of builtins. The builtin ordering is encoded in the
1986 // bottom three bits; the width is encoded in the bits above that.
1987 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1988 return FWIT->getWidth() << 3;
1989 }
1990
Chris Lattnerf52ab252008-04-06 22:59:24 +00001991 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001992 default: assert(0 && "getIntegerRank(): not a built-in integer");
1993 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001994 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001995 case BuiltinType::Char_S:
1996 case BuiltinType::Char_U:
1997 case BuiltinType::SChar:
1998 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001999 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002000 case BuiltinType::Short:
2001 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002002 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002003 case BuiltinType::Int:
2004 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002005 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002006 case BuiltinType::Long:
2007 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002008 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002009 case BuiltinType::LongLong:
2010 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002011 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00002012 case BuiltinType::Int128:
2013 case BuiltinType::UInt128:
2014 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00002015 }
2016}
2017
Chris Lattner7cfeb082008-04-06 23:55:33 +00002018/// getIntegerTypeOrder - Returns the highest ranked integer type:
2019/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
2020/// LHS < RHS, return -1.
2021int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002022 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2023 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00002024 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002025
Chris Lattnerf52ab252008-04-06 22:59:24 +00002026 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2027 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00002028
Chris Lattner7cfeb082008-04-06 23:55:33 +00002029 unsigned LHSRank = getIntegerRank(LHSC);
2030 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00002031
Chris Lattner7cfeb082008-04-06 23:55:33 +00002032 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2033 if (LHSRank == RHSRank) return 0;
2034 return LHSRank > RHSRank ? 1 : -1;
2035 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002036
Chris Lattner7cfeb082008-04-06 23:55:33 +00002037 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2038 if (LHSUnsigned) {
2039 // If the unsigned [LHS] type is larger, return it.
2040 if (LHSRank >= RHSRank)
2041 return 1;
2042
2043 // If the signed type can represent all values of the unsigned type, it
2044 // wins. Because we are dealing with 2's complement and types that are
2045 // powers of two larger than each other, this is always safe.
2046 return -1;
2047 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002048
Chris Lattner7cfeb082008-04-06 23:55:33 +00002049 // If the unsigned [RHS] type is larger, return it.
2050 if (RHSRank >= LHSRank)
2051 return -1;
2052
2053 // If the signed type can represent all values of the unsigned type, it
2054 // wins. Because we are dealing with 2's complement and types that are
2055 // powers of two larger than each other, this is always safe.
2056 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002057}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002058
2059// getCFConstantStringType - Return the type used for constant CFStrings.
2060QualType ASTContext::getCFConstantStringType() {
2061 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00002062 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002063 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002064 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002065 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00002066
2067 // const int *isa;
2068 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002069 // int flags;
2070 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002071 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002072 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00002073 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002074 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00002075
Anders Carlsson71993dd2007-08-17 05:31:46 +00002076 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002077 for (unsigned i = 0; i < 4; ++i) {
2078 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2079 SourceLocation(), 0,
2080 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002081 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002082 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002083 }
2084
2085 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002086 }
2087
2088 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002089}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002090
Douglas Gregor319ac892009-04-23 22:29:11 +00002091void ASTContext::setCFConstantStringType(QualType T) {
2092 const RecordType *Rec = T->getAsRecordType();
2093 assert(Rec && "Invalid CFConstantStringType");
2094 CFConstantStringTypeDecl = Rec->getDecl();
2095}
2096
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002097QualType ASTContext::getObjCFastEnumerationStateType()
2098{
2099 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002100 ObjCFastEnumerationStateTypeDecl =
2101 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2102 &Idents.get("__objcFastEnumerationState"));
2103
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002104 QualType FieldTypes[] = {
2105 UnsignedLongTy,
2106 getPointerType(ObjCIdType),
2107 getPointerType(UnsignedLongTy),
2108 getConstantArrayType(UnsignedLongTy,
2109 llvm::APInt(32, 5), ArrayType::Normal, 0)
2110 };
2111
Douglas Gregor44b43212008-12-11 16:49:14 +00002112 for (size_t i = 0; i < 4; ++i) {
2113 FieldDecl *Field = FieldDecl::Create(*this,
2114 ObjCFastEnumerationStateTypeDecl,
2115 SourceLocation(), 0,
2116 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002117 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002118 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002119 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002120
Douglas Gregor44b43212008-12-11 16:49:14 +00002121 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002122 }
2123
2124 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2125}
2126
Douglas Gregor319ac892009-04-23 22:29:11 +00002127void ASTContext::setObjCFastEnumerationStateType(QualType T) {
2128 const RecordType *Rec = T->getAsRecordType();
2129 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2130 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2131}
2132
Anders Carlssone8c49532007-10-29 06:33:42 +00002133// This returns true if a type has been typedefed to BOOL:
2134// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002135static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002136 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002137 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2138 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002139
2140 return false;
2141}
2142
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002143/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002144/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002145int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002146 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002147
2148 // Make all integer and enum types at least as large as an int
2149 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002150 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002151 // Treat arrays as pointers, since that's how they're passed in.
2152 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002153 sz = getTypeSize(VoidPtrTy);
2154 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002155}
2156
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002157/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002158/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002159void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002160 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002161 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002162 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002163 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002164 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002165 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002166 // Compute size of all parameters.
2167 // Start with computing size of a pointer in number of bytes.
2168 // FIXME: There might(should) be a better way of doing this computation!
2169 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002170 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002171 // The first two arguments (self and _cmd) are pointers; account for
2172 // their size.
2173 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002174 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2175 E = Decl->param_end(); PI != E; ++PI) {
2176 QualType PType = (*PI)->getType();
2177 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002178 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002179 ParmOffset += sz;
2180 }
2181 S += llvm::utostr(ParmOffset);
2182 S += "@0:";
2183 S += llvm::utostr(PtrSize);
2184
2185 // Argument types.
2186 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002187 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2188 E = Decl->param_end(); PI != E; ++PI) {
2189 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002190 QualType PType = PVDecl->getOriginalType();
2191 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002192 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2193 // Use array's original type only if it has known number of
2194 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002195 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002196 PType = PVDecl->getType();
2197 } else if (PType->isFunctionType())
2198 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002199 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002200 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002201 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002202 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002203 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002204 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002205 }
2206}
2207
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002208/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002209/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002210/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2211/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002212/// Property attributes are stored as a comma-delimited C string. The simple
2213/// attributes readonly and bycopy are encoded as single characters. The
2214/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2215/// encoded as single characters, followed by an identifier. Property types
2216/// are also encoded as a parametrized attribute. The characters used to encode
2217/// these attributes are defined by the following enumeration:
2218/// @code
2219/// enum PropertyAttributes {
2220/// kPropertyReadOnly = 'R', // property is read-only.
2221/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2222/// kPropertyByref = '&', // property is a reference to the value last assigned
2223/// kPropertyDynamic = 'D', // property is dynamic
2224/// kPropertyGetter = 'G', // followed by getter selector name
2225/// kPropertySetter = 'S', // followed by setter selector name
2226/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2227/// kPropertyType = 't' // followed by old-style type encoding.
2228/// kPropertyWeak = 'W' // 'weak' property
2229/// kPropertyStrong = 'P' // property GC'able
2230/// kPropertyNonAtomic = 'N' // property non-atomic
2231/// };
2232/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002233void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2234 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002235 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002236 // Collect information from the property implementation decl(s).
2237 bool Dynamic = false;
2238 ObjCPropertyImplDecl *SynthesizePID = 0;
2239
2240 // FIXME: Duplicated code due to poor abstraction.
2241 if (Container) {
2242 if (const ObjCCategoryImplDecl *CID =
2243 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2244 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002245 i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2246 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002247 ObjCPropertyImplDecl *PID = *i;
2248 if (PID->getPropertyDecl() == PD) {
2249 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2250 Dynamic = true;
2251 } else {
2252 SynthesizePID = PID;
2253 }
2254 }
2255 }
2256 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002257 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002258 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002259 i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2260 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002261 ObjCPropertyImplDecl *PID = *i;
2262 if (PID->getPropertyDecl() == PD) {
2263 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2264 Dynamic = true;
2265 } else {
2266 SynthesizePID = PID;
2267 }
2268 }
2269 }
2270 }
2271 }
2272
2273 // FIXME: This is not very efficient.
2274 S = "T";
2275
2276 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002277 // GCC has some special rules regarding encoding of properties which
2278 // closely resembles encoding of ivars.
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002279 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002280 true /* outermost type */,
2281 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002282
2283 if (PD->isReadOnly()) {
2284 S += ",R";
2285 } else {
2286 switch (PD->getSetterKind()) {
2287 case ObjCPropertyDecl::Assign: break;
2288 case ObjCPropertyDecl::Copy: S += ",C"; break;
2289 case ObjCPropertyDecl::Retain: S += ",&"; break;
2290 }
2291 }
2292
2293 // It really isn't clear at all what this means, since properties
2294 // are "dynamic by default".
2295 if (Dynamic)
2296 S += ",D";
2297
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002298 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2299 S += ",N";
2300
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002301 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2302 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002303 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002304 }
2305
2306 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2307 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002308 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002309 }
2310
2311 if (SynthesizePID) {
2312 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2313 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002314 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002315 }
2316
2317 // FIXME: OBJCGC: weak & strong
2318}
2319
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002320/// getLegacyIntegralTypeEncoding -
2321/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002322/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002323/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2324///
2325void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2326 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2327 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002328 if (BT->getKind() == BuiltinType::ULong &&
2329 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002330 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002331 else
2332 if (BT->getKind() == BuiltinType::Long &&
2333 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002334 PointeeTy = IntTy;
2335 }
2336 }
2337}
2338
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002339void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002340 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002341 // We follow the behavior of gcc, expanding structures which are
2342 // directly pointed to, and expanding embedded structures. Note that
2343 // these rules are sufficient to prevent recursive encoding of the
2344 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002345 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2346 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002347}
2348
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002349static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002350 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002351 const Expr *E = FD->getBitWidth();
2352 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2353 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002354 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002355 S += 'b';
2356 S += llvm::utostr(N);
2357}
2358
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002359void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2360 bool ExpandPointedToStructures,
2361 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002362 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002363 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002364 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002365 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002366 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002367 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002368 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002369 else {
2370 char encoding;
2371 switch (BT->getKind()) {
2372 default: assert(0 && "Unhandled builtin type kind");
2373 case BuiltinType::Void: encoding = 'v'; break;
2374 case BuiltinType::Bool: encoding = 'B'; break;
2375 case BuiltinType::Char_U:
2376 case BuiltinType::UChar: encoding = 'C'; break;
2377 case BuiltinType::UShort: encoding = 'S'; break;
2378 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002379 case BuiltinType::ULong:
2380 encoding =
2381 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2382 break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002383 case BuiltinType::UInt128: encoding = 'T'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002384 case BuiltinType::ULongLong: encoding = 'Q'; break;
2385 case BuiltinType::Char_S:
2386 case BuiltinType::SChar: encoding = 'c'; break;
2387 case BuiltinType::Short: encoding = 's'; break;
2388 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002389 case BuiltinType::Long:
2390 encoding =
2391 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2392 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002393 case BuiltinType::LongLong: encoding = 'q'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002394 case BuiltinType::Int128: encoding = 't'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002395 case BuiltinType::Float: encoding = 'f'; break;
2396 case BuiltinType::Double: encoding = 'd'; break;
2397 case BuiltinType::LongDouble: encoding = 'd'; break;
2398 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002399
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002400 S += encoding;
2401 }
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002402 } else if (const ComplexType *CT = T->getAsComplexType()) {
2403 S += 'j';
2404 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2405 false);
2406 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002407 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2408 ExpandPointedToStructures,
2409 ExpandStructures, FD);
2410 if (FD || EncodingProperty) {
2411 // Note that we do extended encoding of protocol qualifer list
2412 // Only when doing ivar or property encoding.
2413 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2414 S += '"';
2415 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2416 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2417 S += '<';
2418 S += Proto->getNameAsString();
2419 S += '>';
2420 }
2421 S += '"';
2422 }
2423 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002424 }
2425 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002426 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002427 bool isReadOnly = false;
2428 // For historical/compatibility reasons, the read-only qualifier of the
2429 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2430 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2431 // Also, do not emit the 'r' for anything but the outermost type!
2432 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2433 if (OutermostType && T.isConstQualified()) {
2434 isReadOnly = true;
2435 S += 'r';
2436 }
2437 }
2438 else if (OutermostType) {
2439 QualType P = PointeeTy;
2440 while (P->getAsPointerType())
2441 P = P->getAsPointerType()->getPointeeType();
2442 if (P.isConstQualified()) {
2443 isReadOnly = true;
2444 S += 'r';
2445 }
2446 }
2447 if (isReadOnly) {
2448 // Another legacy compatibility encoding. Some ObjC qualifier and type
2449 // combinations need to be rearranged.
2450 // Rewrite "in const" from "nr" to "rn"
2451 const char * s = S.c_str();
2452 int len = S.length();
2453 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2454 std::string replace = "rn";
2455 S.replace(S.end()-2, S.end(), replace);
2456 }
2457 }
Steve Naroff389bf462009-02-12 17:52:19 +00002458 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002459 S += '@';
2460 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002461 }
2462 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002463 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002464 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002465 // Another historical/compatibility reason.
2466 // We encode the underlying type which comes out as
2467 // {...};
2468 S += '^';
2469 getObjCEncodingForTypeImpl(PointeeTy, S,
2470 false, ExpandPointedToStructures,
2471 NULL);
2472 return;
2473 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002474 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002475 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002476 const ObjCInterfaceType *OIT =
2477 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002478 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002479 S += '"';
2480 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002481 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2482 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2483 S += '<';
2484 S += Proto->getNameAsString();
2485 S += '>';
2486 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002487 S += '"';
2488 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002489 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002490 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002491 S += '#';
2492 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002493 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002494 S += ':';
2495 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002496 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002497
2498 if (PointeeTy->isCharType()) {
2499 // char pointer types should be encoded as '*' unless it is a
2500 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002501 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002502 S += '*';
2503 return;
2504 }
2505 }
2506
2507 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002508 getLegacyIntegralTypeEncoding(PointeeTy);
2509
2510 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002511 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002512 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002513 } else if (const ArrayType *AT =
2514 // Ignore type qualifiers etc.
2515 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002516 if (isa<IncompleteArrayType>(AT)) {
2517 // Incomplete arrays are encoded as a pointer to the array element.
2518 S += '^';
2519
2520 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2521 false, ExpandStructures, FD);
2522 } else {
2523 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002524
Anders Carlsson559a8332009-02-22 01:38:57 +00002525 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2526 S += llvm::utostr(CAT->getSize().getZExtValue());
2527 else {
2528 //Variable length arrays are encoded as a regular array with 0 elements.
2529 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2530 S += '0';
2531 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002532
Anders Carlsson559a8332009-02-22 01:38:57 +00002533 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2534 false, ExpandStructures, FD);
2535 S += ']';
2536 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002537 } else if (T->getAsFunctionType()) {
2538 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002539 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002540 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002541 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002542 // Anonymous structures print as '?'
2543 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2544 S += II->getName();
2545 } else {
2546 S += '?';
2547 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002548 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002549 S += '=';
Douglas Gregor6ab35242009-04-09 21:40:53 +00002550 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2551 FieldEnd = RDecl->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +00002552 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002553 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002554 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002555 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002556 S += '"';
2557 }
2558
2559 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002560 if (Field->isBitField()) {
2561 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2562 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002563 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002564 QualType qt = Field->getType();
2565 getLegacyIntegralTypeEncoding(qt);
2566 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002567 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002568 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002569 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002570 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002571 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002572 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002573 if (FD && FD->isBitField())
2574 EncodeBitField(this, S, FD);
2575 else
2576 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002577 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002578 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002579 } else if (T->isObjCInterfaceType()) {
2580 // @encode(class_name)
2581 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2582 S += '{';
2583 const IdentifierInfo *II = OI->getIdentifier();
2584 S += II->getName();
2585 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002586 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002587 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002588 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002589 if (RecFields[i]->isBitField())
2590 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2591 RecFields[i]);
2592 else
2593 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2594 FD);
2595 }
2596 S += '}';
2597 }
2598 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002599 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002600}
2601
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002602void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002603 std::string& S) const {
2604 if (QT & Decl::OBJC_TQ_In)
2605 S += 'n';
2606 if (QT & Decl::OBJC_TQ_Inout)
2607 S += 'N';
2608 if (QT & Decl::OBJC_TQ_Out)
2609 S += 'o';
2610 if (QT & Decl::OBJC_TQ_Bycopy)
2611 S += 'O';
2612 if (QT & Decl::OBJC_TQ_Byref)
2613 S += 'R';
2614 if (QT & Decl::OBJC_TQ_Oneway)
2615 S += 'V';
2616}
2617
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002618void ASTContext::setBuiltinVaListType(QualType T)
2619{
2620 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2621
2622 BuiltinVaListType = T;
2623}
2624
Douglas Gregor319ac892009-04-23 22:29:11 +00002625void ASTContext::setObjCIdType(QualType T)
Steve Naroff7e219e42007-10-15 14:41:52 +00002626{
Douglas Gregor319ac892009-04-23 22:29:11 +00002627 ObjCIdType = T;
2628
2629 const TypedefType *TT = T->getAsTypedefType();
2630 if (!TT)
2631 return;
2632
2633 TypedefDecl *TD = TT->getDecl();
Steve Naroff7e219e42007-10-15 14:41:52 +00002634
2635 // typedef struct objc_object *id;
2636 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002637 // User error - caller will issue diagnostics.
2638 if (!ptr)
2639 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002640 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002641 // User error - caller will issue diagnostics.
2642 if (!rec)
2643 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002644 IdStructType = rec;
2645}
2646
Douglas Gregor319ac892009-04-23 22:29:11 +00002647void ASTContext::setObjCSelType(QualType T)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002648{
Douglas Gregor319ac892009-04-23 22:29:11 +00002649 ObjCSelType = T;
2650
2651 const TypedefType *TT = T->getAsTypedefType();
2652 if (!TT)
2653 return;
2654 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002655
2656 // typedef struct objc_selector *SEL;
2657 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002658 if (!ptr)
2659 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002660 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002661 if (!rec)
2662 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002663 SelStructType = rec;
2664}
2665
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002666void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002667{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002668 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002669}
2670
Douglas Gregor319ac892009-04-23 22:29:11 +00002671void ASTContext::setObjCClassType(QualType T)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002672{
Douglas Gregor319ac892009-04-23 22:29:11 +00002673 ObjCClassType = T;
2674
2675 const TypedefType *TT = T->getAsTypedefType();
2676 if (!TT)
2677 return;
2678 TypedefDecl *TD = TT->getDecl();
Anders Carlsson8baaca52007-10-31 02:53:19 +00002679
2680 // typedef struct objc_class *Class;
2681 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2682 assert(ptr && "'Class' incorrectly typed");
2683 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2684 assert(rec && "'Class' incorrectly typed");
2685 ClassStructType = rec;
2686}
2687
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002688void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2689 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002690 "'NSConstantString' type already set!");
2691
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002692 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002693}
2694
Douglas Gregor7532dc62009-03-30 22:58:21 +00002695/// \brief Retrieve the template name that represents a qualified
2696/// template name such as \c std::vector.
2697TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2698 bool TemplateKeyword,
2699 TemplateDecl *Template) {
2700 llvm::FoldingSetNodeID ID;
2701 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2702
2703 void *InsertPos = 0;
2704 QualifiedTemplateName *QTN =
2705 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2706 if (!QTN) {
2707 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2708 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2709 }
2710
2711 return TemplateName(QTN);
2712}
2713
2714/// \brief Retrieve the template name that represents a dependent
2715/// template name such as \c MetaFun::template apply.
2716TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2717 const IdentifierInfo *Name) {
2718 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2719
2720 llvm::FoldingSetNodeID ID;
2721 DependentTemplateName::Profile(ID, NNS, Name);
2722
2723 void *InsertPos = 0;
2724 DependentTemplateName *QTN =
2725 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2726
2727 if (QTN)
2728 return TemplateName(QTN);
2729
2730 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2731 if (CanonNNS == NNS) {
2732 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2733 } else {
2734 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2735 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2736 }
2737
2738 DependentTemplateNames.InsertNode(QTN, InsertPos);
2739 return TemplateName(QTN);
2740}
2741
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002742/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002743/// TargetInfo, produce the corresponding type. The unsigned @p Type
2744/// is actually a value of type @c TargetInfo::IntType.
2745QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002746 switch (Type) {
2747 case TargetInfo::NoInt: return QualType();
2748 case TargetInfo::SignedShort: return ShortTy;
2749 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2750 case TargetInfo::SignedInt: return IntTy;
2751 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2752 case TargetInfo::SignedLong: return LongTy;
2753 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2754 case TargetInfo::SignedLongLong: return LongLongTy;
2755 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2756 }
2757
2758 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002759 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002760}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002761
2762//===----------------------------------------------------------------------===//
2763// Type Predicates.
2764//===----------------------------------------------------------------------===//
2765
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002766/// isObjCNSObjectType - Return true if this is an NSObject object using
2767/// NSObject attribute on a c-style pointer type.
2768/// FIXME - Make it work directly on types.
2769///
2770bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2771 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2772 if (TypedefDecl *TD = TDT->getDecl())
2773 if (TD->getAttr<ObjCNSObjectAttr>())
2774 return true;
2775 }
2776 return false;
2777}
2778
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002779/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2780/// to an object type. This includes "id" and "Class" (two 'special' pointers
2781/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2782/// ID type).
2783bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002784 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002785 return true;
2786
Steve Naroff6ae98502008-10-21 18:24:04 +00002787 // Blocks are objects.
2788 if (Ty->isBlockPointerType())
2789 return true;
2790
2791 // All other object types are pointers.
Chris Lattner16ede0e2009-04-12 23:51:02 +00002792 const PointerType *PT = Ty->getAsPointerType();
2793 if (PT == 0)
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002794 return false;
2795
Chris Lattner16ede0e2009-04-12 23:51:02 +00002796 // If this a pointer to an interface (e.g. NSString*), it is ok.
2797 if (PT->getPointeeType()->isObjCInterfaceType() ||
2798 // If is has NSObject attribute, OK as well.
2799 isObjCNSObjectType(Ty))
2800 return true;
2801
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002802 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2803 // pointer types. This looks for the typedef specifically, not for the
Chris Lattner16ede0e2009-04-12 23:51:02 +00002804 // underlying type. Iteratively strip off typedefs so that we can handle
2805 // typedefs of typedefs.
2806 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2807 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2808 Ty.getUnqualifiedType() == getObjCClassType())
2809 return true;
2810
2811 Ty = TDT->getDecl()->getUnderlyingType();
2812 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002813
Chris Lattner16ede0e2009-04-12 23:51:02 +00002814 return false;
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002815}
2816
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002817/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2818/// garbage collection attribute.
2819///
2820QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002821 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002822 if (getLangOptions().ObjC1 &&
2823 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002824 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002825 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002826 // (or pointers to them) be treated as though they were declared
2827 // as __strong.
2828 if (GCAttrs == QualType::GCNone) {
2829 if (isObjCObjectPointerType(Ty))
2830 GCAttrs = QualType::Strong;
2831 else if (Ty->isPointerType())
2832 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2833 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00002834 // Non-pointers have none gc'able attribute regardless of the attribute
2835 // set on them.
2836 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2837 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002838 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002839 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002840}
2841
Chris Lattner6ac46a42008-04-07 06:51:04 +00002842//===----------------------------------------------------------------------===//
2843// Type Compatibility Testing
2844//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002845
Steve Naroff1c7d0672008-09-04 15:10:53 +00002846/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002847/// block types. Types must be strictly compatible here. For example,
2848/// C unfortunately doesn't produce an error for the following:
2849///
2850/// int (*emptyArgFunc)();
2851/// int (*intArgList)(int) = emptyArgFunc;
2852///
2853/// For blocks, we will produce an error for the following (similar to C++):
2854///
2855/// int (^emptyArgBlock)();
2856/// int (^intArgBlock)(int) = emptyArgBlock;
2857///
2858/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2859///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002860bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002861 const FunctionType *lbase = lhs->getAsFunctionType();
2862 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002863 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2864 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stumpaab0f7a2009-04-01 01:17:39 +00002865 if (lproto && rproto == 0)
2866 return false;
2867 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff1c7d0672008-09-04 15:10:53 +00002868}
2869
Chris Lattner6ac46a42008-04-07 06:51:04 +00002870/// areCompatVectorTypes - Return true if the two specified vector types are
2871/// compatible.
2872static bool areCompatVectorTypes(const VectorType *LHS,
2873 const VectorType *RHS) {
2874 assert(LHS->isCanonical() && RHS->isCanonical());
2875 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002876 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002877}
2878
Eli Friedman3d815e72008-08-22 00:56:42 +00002879/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002880/// compatible for assignment from RHS to LHS. This handles validation of any
2881/// protocol qualifiers on the LHS or RHS.
2882///
Eli Friedman3d815e72008-08-22 00:56:42 +00002883bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2884 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002885 // Verify that the base decls are compatible: the RHS must be a subclass of
2886 // the LHS.
2887 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2888 return false;
2889
2890 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2891 // protocol qualified at all, then we are good.
2892 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2893 return true;
2894
2895 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2896 // isn't a superset.
2897 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2898 return true; // FIXME: should return false!
2899
2900 // Finally, we must have two protocol-qualified interfaces.
2901 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2902 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002903
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002904 // All LHS protocols must have a presence on the RHS.
2905 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002906
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002907 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2908 LHSPE = LHSP->qual_end();
2909 LHSPI != LHSPE; LHSPI++) {
2910 bool RHSImplementsProtocol = false;
2911
2912 // If the RHS doesn't implement the protocol on the left, the types
2913 // are incompatible.
2914 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2915 RHSPE = RHSP->qual_end();
2916 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2917 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2918 RHSImplementsProtocol = true;
2919 }
2920 // FIXME: For better diagnostics, consider passing back the protocol name.
2921 if (!RHSImplementsProtocol)
2922 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002923 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002924 // The RHS implements all protocols listed on the LHS.
2925 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002926}
2927
Steve Naroff389bf462009-02-12 17:52:19 +00002928bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2929 // get the "pointed to" types
2930 const PointerType *LHSPT = LHS->getAsPointerType();
2931 const PointerType *RHSPT = RHS->getAsPointerType();
2932
2933 if (!LHSPT || !RHSPT)
2934 return false;
2935
2936 QualType lhptee = LHSPT->getPointeeType();
2937 QualType rhptee = RHSPT->getPointeeType();
2938 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2939 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2940 // ID acts sort of like void* for ObjC interfaces
2941 if (LHSIface && isObjCIdStructType(rhptee))
2942 return true;
2943 if (RHSIface && isObjCIdStructType(lhptee))
2944 return true;
2945 if (!LHSIface || !RHSIface)
2946 return false;
2947 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2948 canAssignObjCInterfaces(RHSIface, LHSIface);
2949}
2950
Steve Naroffec0550f2007-10-15 20:41:53 +00002951/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2952/// both shall have the identically qualified version of a compatible type.
2953/// C99 6.2.7p1: Two types have compatible types if their types are the
2954/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002955bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2956 return !mergeTypes(LHS, RHS).isNull();
2957}
2958
2959QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2960 const FunctionType *lbase = lhs->getAsFunctionType();
2961 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002962 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2963 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002964 bool allLTypes = true;
2965 bool allRTypes = true;
2966
2967 // Check return type
2968 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2969 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002970 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2971 allLTypes = false;
2972 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2973 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002974
2975 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl7b9a2ee2009-04-30 19:20:55 +00002976 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
2977 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00002978 unsigned lproto_nargs = lproto->getNumArgs();
2979 unsigned rproto_nargs = rproto->getNumArgs();
2980
2981 // Compatible functions must have the same number of arguments
2982 if (lproto_nargs != rproto_nargs)
2983 return QualType();
2984
2985 // Variadic and non-variadic functions aren't compatible
2986 if (lproto->isVariadic() != rproto->isVariadic())
2987 return QualType();
2988
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002989 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2990 return QualType();
2991
Eli Friedman3d815e72008-08-22 00:56:42 +00002992 // Check argument compatibility
2993 llvm::SmallVector<QualType, 10> types;
2994 for (unsigned i = 0; i < lproto_nargs; i++) {
2995 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2996 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2997 QualType argtype = mergeTypes(largtype, rargtype);
2998 if (argtype.isNull()) return QualType();
2999 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00003000 if (getCanonicalType(argtype) != getCanonicalType(largtype))
3001 allLTypes = false;
3002 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
3003 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00003004 }
3005 if (allLTypes) return lhs;
3006 if (allRTypes) return rhs;
3007 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003008 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00003009 }
3010
3011 if (lproto) allRTypes = false;
3012 if (rproto) allLTypes = false;
3013
Douglas Gregor72564e72009-02-26 23:50:07 +00003014 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00003015 if (proto) {
Sebastian Redl7b9a2ee2009-04-30 19:20:55 +00003016 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003017 if (proto->isVariadic()) return QualType();
3018 // Check that the types are compatible with the types that
3019 // would result from default argument promotions (C99 6.7.5.3p15).
3020 // The only types actually affected are promotable integer
3021 // types and floats, which would be passed as a different
3022 // type depending on whether the prototype is visible.
3023 unsigned proto_nargs = proto->getNumArgs();
3024 for (unsigned i = 0; i < proto_nargs; ++i) {
3025 QualType argTy = proto->getArgType(i);
3026 if (argTy->isPromotableIntegerType() ||
3027 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
3028 return QualType();
3029 }
3030
3031 if (allLTypes) return lhs;
3032 if (allRTypes) return rhs;
3033 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003034 proto->getNumArgs(), lproto->isVariadic(),
3035 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00003036 }
3037
3038 if (allLTypes) return lhs;
3039 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00003040 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00003041}
3042
3043QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00003044 // C++ [expr]: If an expression initially has the type "reference to T", the
3045 // type is adjusted to "T" prior to any further analysis, the expression
3046 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003047 // expression is an lvalue unless the reference is an rvalue reference and
3048 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00003049 // FIXME: C++ shouldn't be going through here! The rules are different
3050 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003051 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3052 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00003053 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003054 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00003055 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003056 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00003057
Eli Friedman3d815e72008-08-22 00:56:42 +00003058 QualType LHSCan = getCanonicalType(LHS),
3059 RHSCan = getCanonicalType(RHS);
3060
3061 // If two types are identical, they are compatible.
3062 if (LHSCan == RHSCan)
3063 return LHS;
3064
3065 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003066 // Note that we handle extended qualifiers later, in the
3067 // case for ExtQualType.
3068 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00003069 return QualType();
3070
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003071 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
3072 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003073
Chris Lattner1adb8832008-01-14 05:45:46 +00003074 // We want to consider the two function types to be the same for these
3075 // comparisons, just force one to the other.
3076 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3077 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003078
3079 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003080 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3081 LHSClass = Type::ConstantArray;
3082 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3083 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00003084
Nate Begeman213541a2008-04-18 23:10:10 +00003085 // Canonicalize ExtVector -> Vector.
3086 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3087 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00003088
Chris Lattnerb0489812008-04-07 06:38:24 +00003089 // Consider qualified interfaces and interfaces the same.
3090 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
3091 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00003092
Chris Lattnera36a61f2008-04-07 05:43:21 +00003093 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003094 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003095 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3096 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003097
Steve Naroffd824c9c2009-04-14 15:11:46 +00003098 // 'id' and 'Class' act sort of like void* for ObjC interfaces
3099 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003100 return LHS;
Steve Naroffd824c9c2009-04-14 15:11:46 +00003101 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003102 return RHS;
3103
Steve Naroffbc76dd02008-12-10 22:14:21 +00003104 // ID is compatible with all qualified id types.
3105 if (LHS->isObjCQualifiedIdType()) {
3106 if (const PointerType *PT = RHS->getAsPointerType()) {
3107 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003108 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003109 return LHS;
3110 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3111 // Unfortunately, this API is part of Sema (which we don't have access
3112 // to. Need to refactor. The following check is insufficient, since we
3113 // need to make sure the class implements the protocol.
3114 if (pType->isObjCInterfaceType())
3115 return LHS;
3116 }
3117 }
3118 if (RHS->isObjCQualifiedIdType()) {
3119 if (const PointerType *PT = LHS->getAsPointerType()) {
3120 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003121 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003122 return RHS;
3123 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3124 // Unfortunately, this API is part of Sema (which we don't have access
3125 // to. Need to refactor. The following check is insufficient, since we
3126 // need to make sure the class implements the protocol.
3127 if (pType->isObjCInterfaceType())
3128 return RHS;
3129 }
3130 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003131 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3132 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00003133 if (const EnumType* ETy = LHS->getAsEnumType()) {
3134 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3135 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003136 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003137 if (const EnumType* ETy = RHS->getAsEnumType()) {
3138 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3139 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003140 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003141
Eli Friedman3d815e72008-08-22 00:56:42 +00003142 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003143 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003144
Steve Naroff4a746782008-01-09 22:43:08 +00003145 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003146 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003147#define TYPE(Class, Base)
3148#define ABSTRACT_TYPE(Class, Base)
3149#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3150#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3151#include "clang/AST/TypeNodes.def"
3152 assert(false && "Non-canonical and dependent types shouldn't get here");
3153 return QualType();
3154
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003155 case Type::LValueReference:
3156 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003157 case Type::MemberPointer:
3158 assert(false && "C++ should never be in mergeTypes");
3159 return QualType();
3160
3161 case Type::IncompleteArray:
3162 case Type::VariableArray:
3163 case Type::FunctionProto:
3164 case Type::ExtVector:
3165 case Type::ObjCQualifiedInterface:
3166 assert(false && "Types are eliminated above");
3167 return QualType();
3168
Chris Lattner1adb8832008-01-14 05:45:46 +00003169 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003170 {
3171 // Merge two pointer types, while trying to preserve typedef info
3172 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3173 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3174 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3175 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003176 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3177 return LHS;
3178 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3179 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003180 return getPointerType(ResultType);
3181 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003182 case Type::BlockPointer:
3183 {
3184 // Merge two block pointer types, while trying to preserve typedef info
3185 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3186 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3187 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3188 if (ResultType.isNull()) return QualType();
3189 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3190 return LHS;
3191 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3192 return RHS;
3193 return getBlockPointerType(ResultType);
3194 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003195 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003196 {
3197 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3198 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3199 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3200 return QualType();
3201
3202 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3203 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3204 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3205 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003206 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3207 return LHS;
3208 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3209 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003210 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3211 ArrayType::ArraySizeModifier(), 0);
3212 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3213 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003214 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3215 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003216 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3217 return LHS;
3218 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3219 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003220 if (LVAT) {
3221 // FIXME: This isn't correct! But tricky to implement because
3222 // the array's size has to be the size of LHS, but the type
3223 // has to be different.
3224 return LHS;
3225 }
3226 if (RVAT) {
3227 // FIXME: This isn't correct! But tricky to implement because
3228 // the array's size has to be the size of RHS, but the type
3229 // has to be different.
3230 return RHS;
3231 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003232 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3233 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003234 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003235 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003236 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003237 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003238 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003239 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003240 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003241 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3242 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003243 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003244 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003245 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003246 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003247 case Type::Complex:
3248 // Distinct complex types are incompatible.
3249 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003250 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003251 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003252 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3253 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003254 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003255 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003256 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003257 // FIXME: This should be type compatibility, e.g. whether
3258 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003259 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3260 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3261 if (LHSIface && RHSIface &&
3262 canAssignObjCInterfaces(LHSIface, RHSIface))
3263 return LHS;
3264
Eli Friedman3d815e72008-08-22 00:56:42 +00003265 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003266 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003267 case Type::ObjCQualifiedId:
3268 // Distinct qualified id's are not compatible.
3269 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003270 case Type::FixedWidthInt:
3271 // Distinct fixed-width integers are not compatible.
3272 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003273 case Type::ExtQual:
3274 // FIXME: ExtQual types can be compatible even if they're not
3275 // identical!
3276 return QualType();
3277 // First attempt at an implementation, but I'm not really sure it's
3278 // right...
3279#if 0
3280 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3281 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3282 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3283 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3284 return QualType();
3285 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3286 LHSBase = QualType(LQual->getBaseType(), 0);
3287 RHSBase = QualType(RQual->getBaseType(), 0);
3288 ResultType = mergeTypes(LHSBase, RHSBase);
3289 if (ResultType.isNull()) return QualType();
3290 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3291 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3292 return LHS;
3293 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3294 return RHS;
3295 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3296 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3297 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3298 return ResultType;
3299#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003300
3301 case Type::TemplateSpecialization:
3302 assert(false && "Dependent types have no size");
3303 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003304 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003305
3306 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003307}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003308
Chris Lattner5426bf62008-04-07 07:01:58 +00003309//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003310// Integer Predicates
3311//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003312
Eli Friedmanad74a752008-06-28 06:23:08 +00003313unsigned ASTContext::getIntWidth(QualType T) {
3314 if (T == BoolTy)
3315 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003316 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3317 return FWIT->getWidth();
3318 }
3319 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003320 return (unsigned)getTypeSize(T);
3321}
3322
3323QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3324 assert(T->isSignedIntegerType() && "Unexpected type");
3325 if (const EnumType* ETy = T->getAsEnumType())
3326 T = ETy->getDecl()->getIntegerType();
3327 const BuiltinType* BTy = T->getAsBuiltinType();
3328 assert (BTy && "Unexpected signed integer type");
3329 switch (BTy->getKind()) {
3330 case BuiltinType::Char_S:
3331 case BuiltinType::SChar:
3332 return UnsignedCharTy;
3333 case BuiltinType::Short:
3334 return UnsignedShortTy;
3335 case BuiltinType::Int:
3336 return UnsignedIntTy;
3337 case BuiltinType::Long:
3338 return UnsignedLongTy;
3339 case BuiltinType::LongLong:
3340 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00003341 case BuiltinType::Int128:
3342 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00003343 default:
3344 assert(0 && "Unexpected signed integer type");
3345 return QualType();
3346 }
3347}
3348
Douglas Gregor2cf26342009-04-09 22:27:44 +00003349ExternalASTSource::~ExternalASTSource() { }
3350
3351void ExternalASTSource::PrintStats() { }