blob: f9d648b816b392f6328a49f07c3245887bfb4b0c [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000019#include "clang/AST/ExternalASTSource.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000021#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000023#include "llvm/ADT/StringExtras.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000025#include "llvm/Support/MemoryBuffer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner61710852008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Douglas Gregor2deaea32009-04-22 18:49:13 +000035 bool FreeMem, unsigned size_reserve,
36 bool InitializeBuiltins) :
Douglas Gregorab452ba2009-03-26 23:50:42 +000037 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
38 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Douglas Gregor2cf26342009-04-09 22:27:44 +000039 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels),
40 ExternalSource(0) {
Daniel Dunbare91593e2008-08-11 04:54:23 +000041 if (size_reserve > 0) Types.reserve(size_reserve);
42 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
Douglas Gregor7a9cbed2009-04-26 03:57:37 +000044 BuiltinInfo.InitializeTargetBuiltins(Target);
Douglas Gregor2deaea32009-04-22 18:49:13 +000045 if (InitializeBuiltins)
46 this->InitializeBuiltins(idents);
Daniel Dunbare91593e2008-08-11 04:54:23 +000047}
48
Reid Spencer5f016e22007-07-11 17:01:13 +000049ASTContext::~ASTContext() {
50 // Deallocate all the types.
51 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000052 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000053 Types.pop_back();
54 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000055
Nuno Lopesb74668e2008-12-17 22:30:25 +000056 {
57 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
58 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
59 while (I != E) {
60 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
61 delete R;
62 }
63 }
64
65 {
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +000066 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
67 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopesb74668e2008-12-17 22:30:25 +000068 while (I != E) {
69 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
70 delete R;
71 }
72 }
73
Douglas Gregorab452ba2009-03-26 23:50:42 +000074 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000075 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
76 NNS = NestedNameSpecifiers.begin(),
77 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregore7dcd782009-03-27 23:25:45 +000078 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000079 /* Increment in loop */)
80 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000081
82 if (GlobalNestedNameSpecifier)
83 GlobalNestedNameSpecifier->Destroy(*this);
84
Eli Friedmanb26153c2008-05-27 03:08:09 +000085 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000086}
87
Douglas Gregor2deaea32009-04-22 18:49:13 +000088void ASTContext::InitializeBuiltins(IdentifierTable &idents) {
Douglas Gregor2deaea32009-04-22 18:49:13 +000089 BuiltinInfo.InitializeBuiltins(idents, LangOpts.NoBuiltin);
90}
91
Douglas Gregor2cf26342009-04-09 22:27:44 +000092void
93ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
94 ExternalSource.reset(Source.take());
95}
96
Reid Spencer5f016e22007-07-11 17:01:13 +000097void ASTContext::PrintStats() const {
98 fprintf(stderr, "*** AST Context Stats:\n");
99 fprintf(stderr, " %d types total.\n", (int)Types.size());
100 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000101 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000102 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
103 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
104
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000106 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
107 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000108 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000109 unsigned NumExtQual = 0;
110
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
112 Type *T = Types[i];
113 if (isa<BuiltinType>(T))
114 ++NumBuiltin;
115 else if (isa<PointerType>(T))
116 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000117 else if (isa<BlockPointerType>(T))
118 ++NumBlockPointer;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000119 else if (isa<LValueReferenceType>(T))
120 ++NumLValueReference;
121 else if (isa<RValueReferenceType>(T))
122 ++NumRValueReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000123 else if (isa<MemberPointerType>(T))
124 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000125 else if (isa<ComplexType>(T))
126 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 else if (isa<ArrayType>(T))
128 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000129 else if (isa<VectorType>(T))
130 ++NumVector;
Douglas Gregor72564e72009-02-26 23:50:07 +0000131 else if (isa<FunctionNoProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 ++NumFunctionNP;
Douglas Gregor72564e72009-02-26 23:50:07 +0000133 else if (isa<FunctionProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 ++NumFunctionP;
135 else if (isa<TypedefType>(T))
136 ++NumTypeName;
137 else if (TagType *TT = dyn_cast<TagType>(T)) {
138 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000139 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000141 case TagDecl::TK_struct: ++NumTagStruct; break;
142 case TagDecl::TK_union: ++NumTagUnion; break;
143 case TagDecl::TK_class: ++NumTagClass; break;
144 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000146 } else if (isa<ObjCInterfaceType>(T))
147 ++NumObjCInterfaces;
148 else if (isa<ObjCQualifiedInterfaceType>(T))
149 ++NumObjCQualifiedInterfaces;
150 else if (isa<ObjCQualifiedIdType>(T))
151 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000152 else if (isa<TypeOfType>(T))
153 ++NumTypeOfTypes;
Douglas Gregor72564e72009-02-26 23:50:07 +0000154 else if (isa<TypeOfExprType>(T))
155 ++NumTypeOfExprTypes;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000156 else if (isa<ExtQualType>(T))
157 ++NumExtQual;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000158 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000159 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 assert(0 && "Unknown type!");
161 }
162 }
163
164 fprintf(stderr, " %d builtin types\n", NumBuiltin);
165 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000166 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000167 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
168 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000169 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000170 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000172 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
174 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
175 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
176 fprintf(stderr, " %d tagged types\n", NumTagged);
177 fprintf(stderr, " %d struct types\n", NumTagStruct);
178 fprintf(stderr, " %d union types\n", NumTagUnion);
179 fprintf(stderr, " %d class types\n", NumTagClass);
180 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000181 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000182 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000183 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000184 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000185 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000186 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor72564e72009-02-26 23:50:07 +0000187 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000188 fprintf(stderr, " %d attribute-qualified types\n", NumExtQual);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000189
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
191 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000192 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000193 NumLValueReference*sizeof(LValueReferenceType)+
194 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000195 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000196 NumFunctionP*sizeof(FunctionProtoType)+
197 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000198 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000199 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)+
200 NumExtQual*sizeof(ExtQualType)));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000201
202 if (ExternalSource.get()) {
203 fprintf(stderr, "\n");
204 ExternalSource->PrintStats();
205 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000206}
207
208
209void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000210 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000211}
212
Reid Spencer5f016e22007-07-11 17:01:13 +0000213void ASTContext::InitBuiltinTypes() {
214 assert(VoidTy.isNull() && "Context reinitialized?");
215
216 // C99 6.2.5p19.
217 InitBuiltinType(VoidTy, BuiltinType::Void);
218
219 // C99 6.2.5p2.
220 InitBuiltinType(BoolTy, BuiltinType::Bool);
221 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000222 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 InitBuiltinType(CharTy, BuiltinType::Char_S);
224 else
225 InitBuiltinType(CharTy, BuiltinType::Char_U);
226 // C99 6.2.5p4.
227 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
228 InitBuiltinType(ShortTy, BuiltinType::Short);
229 InitBuiltinType(IntTy, BuiltinType::Int);
230 InitBuiltinType(LongTy, BuiltinType::Long);
231 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
232
233 // C99 6.2.5p6.
234 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
235 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
236 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
237 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
238 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
239
240 // C99 6.2.5p10.
241 InitBuiltinType(FloatTy, BuiltinType::Float);
242 InitBuiltinType(DoubleTy, BuiltinType::Double);
243 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000244
Chris Lattner2df9ced2009-04-30 02:43:43 +0000245 // GNU extension, 128-bit integers.
246 InitBuiltinType(Int128Ty, BuiltinType::Int128);
247 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
248
Chris Lattner3a250322009-02-26 23:43:47 +0000249 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
250 InitBuiltinType(WCharTy, BuiltinType::WChar);
251 else // C99
252 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000253
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000254 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000255 InitBuiltinType(OverloadTy, BuiltinType::Overload);
256
257 // Placeholder type for type-dependent expressions whose type is
258 // completely unknown. No code should ever check a type against
259 // DependentTy and users should never see it; however, it is here to
260 // help diagnose failures to properly check for type-dependent
261 // expressions.
262 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000263
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 // C99 6.2.5p11.
265 FloatComplexTy = getComplexType(FloatTy);
266 DoubleComplexTy = getComplexType(DoubleTy);
267 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000268
Steve Naroff7e219e42007-10-15 14:41:52 +0000269 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000270 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000271 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000272 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000273 ClassStructType = 0;
274
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000275 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000276
277 // void * type
278 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000279
280 // nullptr type (C++0x 2.14.7)
281 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000282}
283
Chris Lattner464175b2007-07-18 17:52:12 +0000284//===----------------------------------------------------------------------===//
285// Type Sizing and Analysis
286//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000287
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000288/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
289/// scalar floating point type.
290const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
291 const BuiltinType *BT = T->getAsBuiltinType();
292 assert(BT && "Not a floating point type!");
293 switch (BT->getKind()) {
294 default: assert(0 && "Not a floating point type!");
295 case BuiltinType::Float: return Target.getFloatFormat();
296 case BuiltinType::Double: return Target.getDoubleFormat();
297 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
298 }
299}
300
Chris Lattneraf707ab2009-01-24 21:53:27 +0000301/// getDeclAlign - Return a conservative estimate of the alignment of the
302/// specified decl. Note that bitfields do not have a valid alignment, so
303/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000304unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000305 unsigned Align = Target.getCharWidth();
306
307 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
308 Align = std::max(Align, AA->getAlignment());
309
Chris Lattneraf707ab2009-01-24 21:53:27 +0000310 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
311 QualType T = VD->getType();
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000312 if (const ReferenceType* RT = T->getAsReferenceType()) {
313 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000314 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000315 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
316 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000317 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
318 T = cast<ArrayType>(T)->getElementType();
319
320 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
321 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000322 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000323
324 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000325}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000326
Chris Lattnera7674d82007-07-13 22:13:22 +0000327/// getTypeSize - Return the size of the specified type, in bits. This method
328/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000329std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000330ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000331 uint64_t Width=0;
332 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000333 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000334#define TYPE(Class, Base)
335#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000336#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000337#define DEPENDENT_TYPE(Class, Base) case Type::Class:
338#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000339 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000340 break;
341
Chris Lattner692233e2007-07-13 22:27:08 +0000342 case Type::FunctionNoProto:
343 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000344 // GCC extension: alignof(function) = 32 bits
345 Width = 0;
346 Align = 32;
347 break;
348
Douglas Gregor72564e72009-02-26 23:50:07 +0000349 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000350 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000351 Width = 0;
352 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
353 break;
354
Steve Narofffb22d962007-08-30 01:06:46 +0000355 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000356 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000357
Chris Lattner98be4942008-03-05 18:54:05 +0000358 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000359 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000360 Align = EltInfo.second;
361 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000362 }
Nate Begeman213541a2008-04-18 23:10:10 +0000363 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000364 case Type::Vector: {
365 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000366 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000367 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000368 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000369 // If the alignment is not a power of 2, round up to the next power of 2.
370 // This happens for non-power-of-2 length vectors.
371 // FIXME: this should probably be a target property.
372 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000373 break;
374 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000375
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000376 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000377 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000378 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000379 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000380 // GCC extension: alignof(void) = 8 bits.
381 Width = 0;
382 Align = 8;
383 break;
384
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000385 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000386 Width = Target.getBoolWidth();
387 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000388 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000389 case BuiltinType::Char_S:
390 case BuiltinType::Char_U:
391 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000392 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000393 Width = Target.getCharWidth();
394 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000395 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000396 case BuiltinType::WChar:
397 Width = Target.getWCharWidth();
398 Align = Target.getWCharAlign();
399 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000400 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000401 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000402 Width = Target.getShortWidth();
403 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000404 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000405 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000406 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000407 Width = Target.getIntWidth();
408 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000409 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000410 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000411 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000412 Width = Target.getLongWidth();
413 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000414 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000415 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000416 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000417 Width = Target.getLongLongWidth();
418 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000419 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000420 case BuiltinType::Int128:
421 case BuiltinType::UInt128:
422 Width = 128;
423 Align = 128; // int128_t is 128-bit aligned on all targets.
424 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000425 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000426 Width = Target.getFloatWidth();
427 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000428 break;
429 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000430 Width = Target.getDoubleWidth();
431 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000432 break;
433 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000434 Width = Target.getLongDoubleWidth();
435 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000436 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000437 case BuiltinType::NullPtr:
438 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
439 Align = Target.getPointerAlign(0); // == sizeof(void*)
Chris Lattnera7674d82007-07-13 22:13:22 +0000440 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000441 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000442 case Type::FixedWidthInt:
443 // FIXME: This isn't precisely correct; the width/alignment should depend
444 // on the available types for the target
445 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000446 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000447 Align = Width;
448 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000449 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000450 // FIXME: Pointers into different addr spaces could have different sizes and
451 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000452 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000453 case Type::ObjCQualifiedId:
Douglas Gregor72564e72009-02-26 23:50:07 +0000454 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000455 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000456 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000457 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000458 case Type::BlockPointer: {
459 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
460 Width = Target.getPointerWidth(AS);
461 Align = Target.getPointerAlign(AS);
462 break;
463 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000464 case Type::Pointer: {
465 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000466 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000467 Align = Target.getPointerAlign(AS);
468 break;
469 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000470 case Type::LValueReference:
471 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000472 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000473 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000474 // FIXME: This is wrong for struct layout: a reference in a struct has
475 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000476 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000477 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000478 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000479 // the GCC ABI, where pointers to data are one pointer large, pointers to
480 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000481 // other compilers too, we need to delegate this completely to TargetInfo
482 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000483 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
484 unsigned AS = Pointee.getAddressSpace();
485 Width = Target.getPointerWidth(AS);
486 if (Pointee->isFunctionType())
487 Width *= 2;
488 Align = Target.getPointerAlign(AS);
489 // GCC aligns at single pointer width.
490 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000491 case Type::Complex: {
492 // Complex types have the same alignment as their elements, but twice the
493 // size.
494 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000495 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000496 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000497 Align = EltInfo.second;
498 break;
499 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000500 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000501 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000502 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
503 Width = Layout.getSize();
504 Align = Layout.getAlignment();
505 break;
506 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000507 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000508 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000509 const TagType *TT = cast<TagType>(T);
510
511 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000512 Width = 1;
513 Align = 1;
514 break;
515 }
516
Daniel Dunbar1d751182008-11-08 05:48:37 +0000517 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000518 return getTypeInfo(ET->getDecl()->getIntegerType());
519
Daniel Dunbar1d751182008-11-08 05:48:37 +0000520 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000521 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
522 Width = Layout.getSize();
523 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000524 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000525 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000526
Douglas Gregor18857642009-04-30 17:32:17 +0000527 case Type::Typedef: {
528 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
529 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
530 Align = Aligned->getAlignment();
531 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
532 } else
533 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000534 break;
Chris Lattner71763312008-04-06 22:05:18 +0000535 }
Douglas Gregor18857642009-04-30 17:32:17 +0000536
537 case Type::TypeOfExpr:
538 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
539 .getTypePtr());
540
541 case Type::TypeOf:
542 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
543
544 case Type::QualifiedName:
545 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
546
547 case Type::TemplateSpecialization:
548 assert(getCanonicalType(T) != T &&
549 "Cannot request the size of a dependent type");
550 // FIXME: this is likely to be wrong once we support template
551 // aliases, since a template alias could refer to a typedef that
552 // has an __aligned__ attribute on it.
553 return getTypeInfo(getCanonicalType(T));
554 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000555
Chris Lattner464175b2007-07-18 17:52:12 +0000556 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000557 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000558}
559
Chris Lattner34ebde42009-01-27 18:08:34 +0000560/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
561/// type for the current target in bits. This can be different than the ABI
562/// alignment in cases where it is beneficial for performance to overalign
563/// a data type.
564unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
565 unsigned ABIAlign = getTypeAlign(T);
566
567 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000568 if (T->isSpecificBuiltinType(BuiltinType::Double))
569 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000570
571 return ABIAlign;
572}
573
574
Devang Patel8b277042008-06-04 21:22:16 +0000575/// LayoutField - Field layout.
576void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000577 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000578 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000579 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000580 uint64_t FieldOffset = IsUnion ? 0 : Size;
581 uint64_t FieldSize;
582 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000583
584 // FIXME: Should this override struct packing? Probably we want to
585 // take the minimum?
586 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
587 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000588
589 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
590 // TODO: Need to check this algorithm on other targets!
591 // (tested on Linux-X86)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000592 FieldSize = BitWidthExpr->EvaluateAsInt(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000593
594 std::pair<uint64_t, unsigned> FieldInfo =
595 Context.getTypeInfo(FD->getType());
596 uint64_t TypeSize = FieldInfo.first;
597
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000598 // Determine the alignment of this bitfield. The packing
599 // attributes define a maximum and the alignment attribute defines
600 // a minimum.
601 // FIXME: What is the right behavior when the specified alignment
602 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000603 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000604 if (FieldPacking)
605 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000606 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
607 FieldAlign = std::max(FieldAlign, AA->getAlignment());
608
609 // Check if we need to add padding to give the field the correct
610 // alignment.
611 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
612 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
613
614 // Padding members don't affect overall alignment
615 if (!FD->getIdentifier())
616 FieldAlign = 1;
617 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000618 if (FD->getType()->isIncompleteArrayType()) {
619 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000620 // query getTypeInfo about these, so we figure it out here.
621 // Flexible array members don't have any size, but they
622 // have to be aligned appropriately for their element type.
623 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000624 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000625 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson2f1169f2009-04-10 05:31:15 +0000626 } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
627 unsigned AS = RT->getPointeeType().getAddressSpace();
628 FieldSize = Context.Target.getPointerWidth(AS);
629 FieldAlign = Context.Target.getPointerAlign(AS);
Devang Patel8b277042008-06-04 21:22:16 +0000630 } else {
631 std::pair<uint64_t, unsigned> FieldInfo =
632 Context.getTypeInfo(FD->getType());
633 FieldSize = FieldInfo.first;
634 FieldAlign = FieldInfo.second;
635 }
636
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000637 // Determine the alignment of this bitfield. The packing
638 // attributes define a maximum and the alignment attribute defines
639 // a minimum. Additionally, the packing alignment must be at least
640 // a byte for non-bitfields.
641 //
642 // FIXME: What is the right behavior when the specified alignment
643 // is smaller than the specified packing?
644 if (FieldPacking)
645 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000646 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
647 FieldAlign = std::max(FieldAlign, AA->getAlignment());
648
649 // Round up the current record size to the field's alignment boundary.
650 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
651 }
652
653 // Place this field at the current location.
654 FieldOffsets[FieldNo] = FieldOffset;
655
656 // Reserve space for this field.
657 if (IsUnion) {
658 Size = std::max(Size, FieldSize);
659 } else {
660 Size = FieldOffset + FieldSize;
661 }
662
Daniel Dunbard6884a02009-05-04 05:16:21 +0000663 // Remember the next available offset.
664 NextOffset = Size;
665
Devang Patel8b277042008-06-04 21:22:16 +0000666 // Remember max struct/class alignment.
667 Alignment = std::max(Alignment, FieldAlign);
668}
669
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000670static void CollectLocalObjCIvars(ASTContext *Ctx,
671 const ObjCInterfaceDecl *OI,
672 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000673 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
674 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000675 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000676 if (!IVDecl->isInvalidDecl())
677 Fields.push_back(cast<FieldDecl>(IVDecl));
678 }
679}
680
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000681void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
682 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
683 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
684 CollectObjCIvars(SuperClass, Fields);
685 CollectLocalObjCIvars(this, OI, Fields);
686}
687
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000688/// getInterfaceLayoutImpl - Get or compute information about the
689/// layout of the given interface.
690///
691/// \param Impl - If given, also include the layout of the interface's
692/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000693const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000694ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
695 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000696 assert(!D->isForwardDecl() && "Invalid interface decl!");
697
Devang Patel44a3dde2008-06-04 21:54:36 +0000698 // Look up this layout, if already laid out, return what we have.
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000699 ObjCContainerDecl *Key =
700 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
701 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
702 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000703
Daniel Dunbar453addb2009-05-03 11:16:44 +0000704 unsigned FieldCount = D->ivar_size();
705 // Add in synthesized ivar count if laying out an implementation.
706 if (Impl) {
707 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
708 E = D->prop_end(*this); I != E; ++I)
709 if ((*I)->getPropertyIvarDecl())
710 ++FieldCount;
711
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000712 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +0000713 // entry. Note we can't cache this because we simply free all
714 // entries later; however we shouldn't look up implementations
715 // frequently.
716 if (FieldCount == D->ivar_size())
717 return getObjCLayout(D, 0);
718 }
719
Devang Patel6a5a34c2008-06-06 02:14:01 +0000720 ASTRecordLayout *NewEntry = NULL;
Devang Patel6a5a34c2008-06-06 02:14:01 +0000721 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Devang Patel6a5a34c2008-06-06 02:14:01 +0000722 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
723 unsigned Alignment = SL.getAlignment();
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000724
Daniel Dunbar913af352009-05-07 21:58:26 +0000725 // We start laying out ivars not at the end of the superclass
726 // structure, but at the next byte following the last field.
727 uint64_t Size = llvm::RoundUpToAlignment(SL.NextOffset, 8);
Daniel Dunbard6884a02009-05-04 05:16:21 +0000728
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000729 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout(Size, Alignment);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000730 NewEntry->InitializeLayout(FieldCount);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000731 } else {
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000732 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout();
Devang Patel6a5a34c2008-06-06 02:14:01 +0000733 NewEntry->InitializeLayout(FieldCount);
734 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000735
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000736 unsigned StructPacking = 0;
737 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
738 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000739
740 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
741 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
742 AA->getAlignment()));
743
744 // Layout each ivar sequentially.
745 unsigned i = 0;
746 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
747 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
748 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000749 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000750 }
Daniel Dunbar453addb2009-05-03 11:16:44 +0000751 // And synthesized ivars, if this is an implementation.
752 if (Impl) {
753 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
754 E = D->prop_end(*this); I != E; ++I) {
755 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
756 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
757 }
Fariborz Jahanian18191882009-03-31 18:11:23 +0000758 }
Fariborz Jahanian99eee362009-04-01 19:37:34 +0000759
Devang Patel44a3dde2008-06-04 21:54:36 +0000760 // Finally, round the size of the total struct up to the alignment of the
761 // struct itself.
762 NewEntry->FinalizeLayout();
763 return *NewEntry;
764}
765
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000766const ASTRecordLayout &
767ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
768 return getObjCLayout(D, 0);
769}
770
771const ASTRecordLayout &
772ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
773 return getObjCLayout(D->getClassInterface(), D);
774}
775
Devang Patel88a981b2007-11-01 19:11:01 +0000776/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000777/// specified record (struct/union/class), which indicates its size and field
778/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000779const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000780 D = D->getDefinition(*this);
781 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000782
Chris Lattner464175b2007-07-18 17:52:12 +0000783 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000784 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000785 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000786
Devang Patel88a981b2007-11-01 19:11:01 +0000787 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
788 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
789 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000790 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000791
Douglas Gregore267ff32008-12-11 20:41:00 +0000792 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000793 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
794 D->field_end(*this)));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000795 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000796
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000797 unsigned StructPacking = 0;
798 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
799 StructPacking = PA->getAlignment();
800
Eli Friedman4bd998b2008-05-30 09:31:38 +0000801 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000802 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
803 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000804
Eli Friedman4bd998b2008-05-30 09:31:38 +0000805 // Layout each field, for now, just sequentially, respecting alignment. In
806 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000807 unsigned FieldIdx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000808 for (RecordDecl::field_iterator Field = D->field_begin(*this),
809 FieldEnd = D->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000810 Field != FieldEnd; (void)++Field, ++FieldIdx)
811 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000812
813 // Finally, round the size of the total struct up to the alignment of the
814 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000815 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000816 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000817}
818
Chris Lattnera7674d82007-07-13 22:13:22 +0000819//===----------------------------------------------------------------------===//
820// Type creation/memoization methods
821//===----------------------------------------------------------------------===//
822
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000823QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000824 QualType CanT = getCanonicalType(T);
825 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000826 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000827
828 // If we are composing extended qualifiers together, merge together into one
829 // ExtQualType node.
830 unsigned CVRQuals = T.getCVRQualifiers();
831 QualType::GCAttrTypes GCAttr = QualType::GCNone;
832 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000833
Chris Lattnerb7d25532009-02-18 22:53:11 +0000834 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
835 // If this type already has an address space specified, it cannot get
836 // another one.
837 assert(EQT->getAddressSpace() == 0 &&
838 "Type cannot be in multiple addr spaces!");
839 GCAttr = EQT->getObjCGCAttr();
840 TypeNode = EQT->getBaseType();
841 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000842
Chris Lattnerb7d25532009-02-18 22:53:11 +0000843 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000844 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000845 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000846 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000847 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000848 return QualType(EXTQy, CVRQuals);
849
Christopher Lambebb97e92008-02-04 02:31:56 +0000850 // If the base type isn't canonical, this won't be a canonical type either,
851 // so fill in the canonical type field.
852 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000853 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000854 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000855
Chris Lattnerb7d25532009-02-18 22:53:11 +0000856 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000857 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000858 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000859 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000860 ExtQualType *New =
861 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000862 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000863 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000864 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000865}
866
Chris Lattnerb7d25532009-02-18 22:53:11 +0000867QualType ASTContext::getObjCGCQualType(QualType T,
868 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000869 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000870 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000871 return T;
872
Chris Lattnerb7d25532009-02-18 22:53:11 +0000873 // If we are composing extended qualifiers together, merge together into one
874 // ExtQualType node.
875 unsigned CVRQuals = T.getCVRQualifiers();
876 Type *TypeNode = T.getTypePtr();
877 unsigned AddressSpace = 0;
878
879 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
880 // If this type already has an address space specified, it cannot get
881 // another one.
882 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
883 "Type cannot be in multiple addr spaces!");
884 AddressSpace = EQT->getAddressSpace();
885 TypeNode = EQT->getBaseType();
886 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000887
888 // Check if we've already instantiated an gc qual'd type of this type.
889 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000890 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000891 void *InsertPos = 0;
892 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000893 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000894
895 // If the base type isn't canonical, this won't be a canonical type either,
896 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000897 // FIXME: Isn't this also not canonical if the base type is a array
898 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000899 QualType Canonical;
900 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000901 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000902
Chris Lattnerb7d25532009-02-18 22:53:11 +0000903 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000904 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
905 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
906 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000907 ExtQualType *New =
908 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000909 ExtQualTypes.InsertNode(New, InsertPos);
910 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000911 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000912}
Chris Lattnera7674d82007-07-13 22:13:22 +0000913
Reid Spencer5f016e22007-07-11 17:01:13 +0000914/// getComplexType - Return the uniqued reference to the type for a complex
915/// number with the specified element type.
916QualType ASTContext::getComplexType(QualType T) {
917 // Unique pointers, to guarantee there is only one pointer of a particular
918 // structure.
919 llvm::FoldingSetNodeID ID;
920 ComplexType::Profile(ID, T);
921
922 void *InsertPos = 0;
923 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
924 return QualType(CT, 0);
925
926 // If the pointee type isn't canonical, this won't be a canonical type either,
927 // so fill in the canonical type field.
928 QualType Canonical;
929 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000930 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000931
932 // Get the new insert position for the node we care about.
933 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000934 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000935 }
Steve Narofff83820b2009-01-27 22:08:43 +0000936 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000937 Types.push_back(New);
938 ComplexTypes.InsertNode(New, InsertPos);
939 return QualType(New, 0);
940}
941
Eli Friedmanf98aba32009-02-13 02:31:07 +0000942QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
943 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
944 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
945 FixedWidthIntType *&Entry = Map[Width];
946 if (!Entry)
947 Entry = new FixedWidthIntType(Width, Signed);
948 return QualType(Entry, 0);
949}
Reid Spencer5f016e22007-07-11 17:01:13 +0000950
951/// getPointerType - Return the uniqued reference to the type for a pointer to
952/// the specified type.
953QualType ASTContext::getPointerType(QualType T) {
954 // Unique pointers, to guarantee there is only one pointer of a particular
955 // structure.
956 llvm::FoldingSetNodeID ID;
957 PointerType::Profile(ID, T);
958
959 void *InsertPos = 0;
960 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
961 return QualType(PT, 0);
962
963 // If the pointee type isn't canonical, this won't be a canonical type either,
964 // so fill in the canonical type field.
965 QualType Canonical;
966 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000967 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000968
969 // Get the new insert position for the node we care about.
970 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000971 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000972 }
Steve Narofff83820b2009-01-27 22:08:43 +0000973 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 Types.push_back(New);
975 PointerTypes.InsertNode(New, InsertPos);
976 return QualType(New, 0);
977}
978
Steve Naroff5618bd42008-08-27 16:04:49 +0000979/// getBlockPointerType - Return the uniqued reference to the type for
980/// a pointer to the specified block.
981QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000982 assert(T->isFunctionType() && "block of function types only");
983 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000984 // structure.
985 llvm::FoldingSetNodeID ID;
986 BlockPointerType::Profile(ID, T);
987
988 void *InsertPos = 0;
989 if (BlockPointerType *PT =
990 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
991 return QualType(PT, 0);
992
Steve Naroff296e8d52008-08-28 19:20:44 +0000993 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000994 // type either so fill in the canonical type field.
995 QualType Canonical;
996 if (!T->isCanonical()) {
997 Canonical = getBlockPointerType(getCanonicalType(T));
998
999 // Get the new insert position for the node we care about.
1000 BlockPointerType *NewIP =
1001 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001002 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001003 }
Steve Narofff83820b2009-01-27 22:08:43 +00001004 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001005 Types.push_back(New);
1006 BlockPointerTypes.InsertNode(New, InsertPos);
1007 return QualType(New, 0);
1008}
1009
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001010/// getLValueReferenceType - Return the uniqued reference to the type for an
1011/// lvalue reference to the specified type.
1012QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 // Unique pointers, to guarantee there is only one pointer of a particular
1014 // structure.
1015 llvm::FoldingSetNodeID ID;
1016 ReferenceType::Profile(ID, T);
1017
1018 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001019 if (LValueReferenceType *RT =
1020 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001021 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001022
Reid Spencer5f016e22007-07-11 17:01:13 +00001023 // If the referencee type isn't canonical, this won't be a canonical type
1024 // either, so fill in the canonical type field.
1025 QualType Canonical;
1026 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001027 Canonical = getLValueReferenceType(getCanonicalType(T));
1028
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001030 LValueReferenceType *NewIP =
1031 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001032 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001033 }
1034
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001035 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001036 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001037 LValueReferenceTypes.InsertNode(New, InsertPos);
1038 return QualType(New, 0);
1039}
1040
1041/// getRValueReferenceType - Return the uniqued reference to the type for an
1042/// rvalue reference to the specified type.
1043QualType ASTContext::getRValueReferenceType(QualType T) {
1044 // Unique pointers, to guarantee there is only one pointer of a particular
1045 // structure.
1046 llvm::FoldingSetNodeID ID;
1047 ReferenceType::Profile(ID, T);
1048
1049 void *InsertPos = 0;
1050 if (RValueReferenceType *RT =
1051 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1052 return QualType(RT, 0);
1053
1054 // If the referencee type isn't canonical, this won't be a canonical type
1055 // either, so fill in the canonical type field.
1056 QualType Canonical;
1057 if (!T->isCanonical()) {
1058 Canonical = getRValueReferenceType(getCanonicalType(T));
1059
1060 // Get the new insert position for the node we care about.
1061 RValueReferenceType *NewIP =
1062 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1063 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1064 }
1065
1066 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1067 Types.push_back(New);
1068 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 return QualType(New, 0);
1070}
1071
Sebastian Redlf30208a2009-01-24 21:16:55 +00001072/// getMemberPointerType - Return the uniqued reference to the type for a
1073/// member pointer to the specified type, in the specified class.
1074QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1075{
1076 // Unique pointers, to guarantee there is only one pointer of a particular
1077 // structure.
1078 llvm::FoldingSetNodeID ID;
1079 MemberPointerType::Profile(ID, T, Cls);
1080
1081 void *InsertPos = 0;
1082 if (MemberPointerType *PT =
1083 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1084 return QualType(PT, 0);
1085
1086 // If the pointee or class type isn't canonical, this won't be a canonical
1087 // type either, so fill in the canonical type field.
1088 QualType Canonical;
1089 if (!T->isCanonical()) {
1090 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1091
1092 // Get the new insert position for the node we care about.
1093 MemberPointerType *NewIP =
1094 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1095 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1096 }
Steve Narofff83820b2009-01-27 22:08:43 +00001097 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001098 Types.push_back(New);
1099 MemberPointerTypes.InsertNode(New, InsertPos);
1100 return QualType(New, 0);
1101}
1102
Steve Narofffb22d962007-08-30 01:06:46 +00001103/// getConstantArrayType - Return the unique reference to the type for an
1104/// array of the specified element type.
1105QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +00001106 const llvm::APInt &ArySize,
1107 ArrayType::ArraySizeModifier ASM,
1108 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001109 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001110 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001111
1112 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001113 if (ConstantArrayType *ATP =
1114 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001115 return QualType(ATP, 0);
1116
1117 // If the element type isn't canonical, this won't be a canonical type either,
1118 // so fill in the canonical type field.
1119 QualType Canonical;
1120 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001121 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001122 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001124 ConstantArrayType *NewIP =
1125 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001126 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001127 }
1128
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001129 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001130 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001131 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001132 Types.push_back(New);
1133 return QualType(New, 0);
1134}
1135
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001136/// getVariableArrayType - Returns a non-unique reference to the type for a
1137/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001138QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1139 ArrayType::ArraySizeModifier ASM,
1140 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001141 // Since we don't unique expressions, it isn't possible to unique VLA's
1142 // that have an expression provided for their size.
1143
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001144 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001145 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001146
1147 VariableArrayTypes.push_back(New);
1148 Types.push_back(New);
1149 return QualType(New, 0);
1150}
1151
Douglas Gregor898574e2008-12-05 23:32:09 +00001152/// getDependentSizedArrayType - Returns a non-unique reference to
1153/// the type for a dependently-sized array of the specified element
1154/// type. FIXME: We will need these to be uniqued, or at least
1155/// comparable, at some point.
1156QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1157 ArrayType::ArraySizeModifier ASM,
1158 unsigned EltTypeQuals) {
1159 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1160 "Size must be type- or value-dependent!");
1161
1162 // Since we don't unique expressions, it isn't possible to unique
1163 // dependently-sized array types.
1164
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001165 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001166 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1167 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001168
1169 DependentSizedArrayTypes.push_back(New);
1170 Types.push_back(New);
1171 return QualType(New, 0);
1172}
1173
Eli Friedmanc5773c42008-02-15 18:16:39 +00001174QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1175 ArrayType::ArraySizeModifier ASM,
1176 unsigned EltTypeQuals) {
1177 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001178 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001179
1180 void *InsertPos = 0;
1181 if (IncompleteArrayType *ATP =
1182 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1183 return QualType(ATP, 0);
1184
1185 // If the element type isn't canonical, this won't be a canonical type
1186 // either, so fill in the canonical type field.
1187 QualType Canonical;
1188
1189 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001190 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001191 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001192
1193 // Get the new insert position for the node we care about.
1194 IncompleteArrayType *NewIP =
1195 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001196 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001197 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001198
Steve Narofff83820b2009-01-27 22:08:43 +00001199 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001200 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001201
1202 IncompleteArrayTypes.InsertNode(New, InsertPos);
1203 Types.push_back(New);
1204 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001205}
1206
Steve Naroff73322922007-07-18 18:00:27 +00001207/// getVectorType - Return the unique reference to a vector type of
1208/// the specified element type and size. VectorType must be a built-in type.
1209QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 BuiltinType *baseType;
1211
Chris Lattnerf52ab252008-04-06 22:59:24 +00001212 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001213 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001214
1215 // Check if we've already instantiated a vector of this type.
1216 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001217 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001218 void *InsertPos = 0;
1219 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1220 return QualType(VTP, 0);
1221
1222 // If the element type isn't canonical, this won't be a canonical type either,
1223 // so fill in the canonical type field.
1224 QualType Canonical;
1225 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001226 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001227
1228 // Get the new insert position for the node we care about.
1229 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001230 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001231 }
Steve Narofff83820b2009-01-27 22:08:43 +00001232 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001233 VectorTypes.InsertNode(New, InsertPos);
1234 Types.push_back(New);
1235 return QualType(New, 0);
1236}
1237
Nate Begeman213541a2008-04-18 23:10:10 +00001238/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001239/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001240QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001241 BuiltinType *baseType;
1242
Chris Lattnerf52ab252008-04-06 22:59:24 +00001243 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001244 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001245
1246 // Check if we've already instantiated a vector of this type.
1247 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001248 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001249 void *InsertPos = 0;
1250 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1251 return QualType(VTP, 0);
1252
1253 // If the element type isn't canonical, this won't be a canonical type either,
1254 // so fill in the canonical type field.
1255 QualType Canonical;
1256 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001257 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001258
1259 // Get the new insert position for the node we care about.
1260 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001261 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001262 }
Steve Narofff83820b2009-01-27 22:08:43 +00001263 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001264 VectorTypes.InsertNode(New, InsertPos);
1265 Types.push_back(New);
1266 return QualType(New, 0);
1267}
1268
Douglas Gregor72564e72009-02-26 23:50:07 +00001269/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001270///
Douglas Gregor72564e72009-02-26 23:50:07 +00001271QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 // Unique functions, to guarantee there is only one function of a particular
1273 // structure.
1274 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001275 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001276
1277 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001278 if (FunctionNoProtoType *FT =
1279 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 return QualType(FT, 0);
1281
1282 QualType Canonical;
1283 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001284 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001285
1286 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001287 FunctionNoProtoType *NewIP =
1288 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001289 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001290 }
1291
Douglas Gregor72564e72009-02-26 23:50:07 +00001292 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001293 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001294 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001295 return QualType(New, 0);
1296}
1297
1298/// getFunctionType - Return a normal function type with a typed argument
1299/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001300QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001301 unsigned NumArgs, bool isVariadic,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001302 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001303 // Unique functions, to guarantee there is only one function of a particular
1304 // structure.
1305 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001306 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001307 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001308
1309 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001310 if (FunctionProtoType *FTP =
1311 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 return QualType(FTP, 0);
1313
1314 // Determine whether the type being created is already canonical or not.
1315 bool isCanonical = ResultTy->isCanonical();
1316 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1317 if (!ArgArray[i]->isCanonical())
1318 isCanonical = false;
1319
1320 // If this type isn't canonical, get the canonical version of it.
1321 QualType Canonical;
1322 if (!isCanonical) {
1323 llvm::SmallVector<QualType, 16> CanonicalArgs;
1324 CanonicalArgs.reserve(NumArgs);
1325 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001326 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001327
Chris Lattnerf52ab252008-04-06 22:59:24 +00001328 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001329 &CanonicalArgs[0], NumArgs,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001330 isVariadic, TypeQuals);
1331
Reid Spencer5f016e22007-07-11 17:01:13 +00001332 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001333 FunctionProtoType *NewIP =
1334 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001335 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001336 }
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001337
Douglas Gregor72564e72009-02-26 23:50:07 +00001338 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001339 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001340 FunctionProtoType *FTP =
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001341 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1342 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001343 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001344 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001345 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001346 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001347 return QualType(FTP, 0);
1348}
1349
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001350/// getTypeDeclType - Return the unique reference to the type for the
1351/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001352QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001353 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001354 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1355
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001356 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001357 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001358 else if (isa<TemplateTypeParmDecl>(Decl)) {
1359 assert(false && "Template type parameter types are always available.");
1360 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001361 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001362
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001363 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001364 if (PrevDecl)
1365 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001366 else
1367 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001368 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001369 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1370 if (PrevDecl)
1371 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001372 else
1373 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001374 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001375 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001376 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001377
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001378 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001379 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001380}
1381
Reid Spencer5f016e22007-07-11 17:01:13 +00001382/// getTypedefType - Return the unique reference to the type for the
1383/// specified typename decl.
1384QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1385 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1386
Chris Lattnerf52ab252008-04-06 22:59:24 +00001387 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001388 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001389 Types.push_back(Decl->TypeForDecl);
1390 return QualType(Decl->TypeForDecl, 0);
1391}
1392
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001393/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001394/// specified ObjC interface decl.
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001395QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001396 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1397
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001398 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1399 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff3536b442007-09-06 21:24:23 +00001400 Types.push_back(Decl->TypeForDecl);
1401 return QualType(Decl->TypeForDecl, 0);
1402}
1403
Douglas Gregorfab9d672009-02-05 23:33:38 +00001404/// \brief Retrieve the template type parameter type for a template
1405/// parameter with the given depth, index, and (optionally) name.
1406QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1407 IdentifierInfo *Name) {
1408 llvm::FoldingSetNodeID ID;
1409 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1410 void *InsertPos = 0;
1411 TemplateTypeParmType *TypeParm
1412 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1413
1414 if (TypeParm)
1415 return QualType(TypeParm, 0);
1416
1417 if (Name)
1418 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1419 getTemplateTypeParmType(Depth, Index));
1420 else
1421 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1422
1423 Types.push_back(TypeParm);
1424 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1425
1426 return QualType(TypeParm, 0);
1427}
1428
Douglas Gregor55f6b142009-02-09 18:46:07 +00001429QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001430ASTContext::getTemplateSpecializationType(TemplateName Template,
1431 const TemplateArgument *Args,
1432 unsigned NumArgs,
1433 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001434 if (!Canon.isNull())
1435 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001436
Douglas Gregor55f6b142009-02-09 18:46:07 +00001437 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001438 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001439
Douglas Gregor55f6b142009-02-09 18:46:07 +00001440 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001441 TemplateSpecializationType *Spec
1442 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001443
1444 if (Spec)
1445 return QualType(Spec, 0);
1446
Douglas Gregor7532dc62009-03-30 22:58:21 +00001447 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001448 sizeof(TemplateArgument) * NumArgs),
1449 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001450 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001451 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001452 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001453
1454 return QualType(Spec, 0);
1455}
1456
Douglas Gregore4e5b052009-03-19 00:18:19 +00001457QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001458ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001459 QualType NamedType) {
1460 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001461 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001462
1463 void *InsertPos = 0;
1464 QualifiedNameType *T
1465 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1466 if (T)
1467 return QualType(T, 0);
1468
Douglas Gregorab452ba2009-03-26 23:50:42 +00001469 T = new (*this) QualifiedNameType(NNS, NamedType,
1470 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001471 Types.push_back(T);
1472 QualifiedNameTypes.InsertNode(T, InsertPos);
1473 return QualType(T, 0);
1474}
1475
Douglas Gregord57959a2009-03-27 23:10:48 +00001476QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1477 const IdentifierInfo *Name,
1478 QualType Canon) {
1479 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1480
1481 if (Canon.isNull()) {
1482 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1483 if (CanonNNS != NNS)
1484 Canon = getTypenameType(CanonNNS, Name);
1485 }
1486
1487 llvm::FoldingSetNodeID ID;
1488 TypenameType::Profile(ID, NNS, Name);
1489
1490 void *InsertPos = 0;
1491 TypenameType *T
1492 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1493 if (T)
1494 return QualType(T, 0);
1495
1496 T = new (*this) TypenameType(NNS, Name, Canon);
1497 Types.push_back(T);
1498 TypenameTypes.InsertNode(T, InsertPos);
1499 return QualType(T, 0);
1500}
1501
Douglas Gregor17343172009-04-01 00:28:59 +00001502QualType
1503ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1504 const TemplateSpecializationType *TemplateId,
1505 QualType Canon) {
1506 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1507
1508 if (Canon.isNull()) {
1509 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1510 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1511 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1512 const TemplateSpecializationType *CanonTemplateId
1513 = CanonType->getAsTemplateSpecializationType();
1514 assert(CanonTemplateId &&
1515 "Canonical type must also be a template specialization type");
1516 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1517 }
1518 }
1519
1520 llvm::FoldingSetNodeID ID;
1521 TypenameType::Profile(ID, NNS, TemplateId);
1522
1523 void *InsertPos = 0;
1524 TypenameType *T
1525 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1526 if (T)
1527 return QualType(T, 0);
1528
1529 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1530 Types.push_back(T);
1531 TypenameTypes.InsertNode(T, InsertPos);
1532 return QualType(T, 0);
1533}
1534
Chris Lattner88cb27a2008-04-07 04:56:42 +00001535/// CmpProtocolNames - Comparison predicate for sorting protocols
1536/// alphabetically.
1537static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1538 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001539 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001540}
1541
1542static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1543 unsigned &NumProtocols) {
1544 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1545
1546 // Sort protocols, keyed by name.
1547 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1548
1549 // Remove duplicates.
1550 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1551 NumProtocols = ProtocolsEnd-Protocols;
1552}
1553
1554
Chris Lattner065f0d72008-04-07 04:44:08 +00001555/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1556/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001557QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1558 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001559 // Sort the protocol list alphabetically to canonicalize it.
1560 SortAndUniqueProtocols(Protocols, NumProtocols);
1561
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001562 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001563 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001564
1565 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001566 if (ObjCQualifiedInterfaceType *QT =
1567 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001568 return QualType(QT, 0);
1569
1570 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001571 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001572 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001573
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001574 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001575 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001576 return QualType(QType, 0);
1577}
1578
Chris Lattner88cb27a2008-04-07 04:56:42 +00001579/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1580/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001581QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001582 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001583 // Sort the protocol list alphabetically to canonicalize it.
1584 SortAndUniqueProtocols(Protocols, NumProtocols);
1585
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001586 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001587 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001588
1589 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001590 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001591 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001592 return QualType(QT, 0);
1593
1594 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001595 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001596 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001597 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001598 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001599 return QualType(QType, 0);
1600}
1601
Douglas Gregor72564e72009-02-26 23:50:07 +00001602/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1603/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001604/// multiple declarations that refer to "typeof(x)" all contain different
1605/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1606/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001607QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001608 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001609 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001610 Types.push_back(toe);
1611 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001612}
1613
Steve Naroff9752f252007-08-01 18:02:17 +00001614/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1615/// TypeOfType AST's. The only motivation to unique these nodes would be
1616/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1617/// an issue. This doesn't effect the type checker, since it operates
1618/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001619QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001620 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001621 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001622 Types.push_back(tot);
1623 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001624}
1625
Reid Spencer5f016e22007-07-11 17:01:13 +00001626/// getTagDeclType - Return the unique reference to the type for the
1627/// specified TagDecl (struct/union/class/enum) decl.
1628QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001629 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001630 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001631}
1632
1633/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1634/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1635/// needs to agree with the definition in <stddef.h>.
1636QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001637 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001638}
1639
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001640/// getSignedWCharType - Return the type of "signed wchar_t".
1641/// Used when in C++, as a GCC extension.
1642QualType ASTContext::getSignedWCharType() const {
1643 // FIXME: derive from "Target" ?
1644 return WCharTy;
1645}
1646
1647/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1648/// Used when in C++, as a GCC extension.
1649QualType ASTContext::getUnsignedWCharType() const {
1650 // FIXME: derive from "Target" ?
1651 return UnsignedIntTy;
1652}
1653
Chris Lattner8b9023b2007-07-13 03:05:23 +00001654/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1655/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1656QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001657 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001658}
1659
Chris Lattnere6327742008-04-02 05:18:44 +00001660//===----------------------------------------------------------------------===//
1661// Type Operators
1662//===----------------------------------------------------------------------===//
1663
Chris Lattner77c96472008-04-06 22:41:35 +00001664/// getCanonicalType - Return the canonical (structural) type corresponding to
1665/// the specified potentially non-canonical type. The non-canonical version
1666/// of a type may have many "decorated" versions of types. Decorators can
1667/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1668/// to be free of any of these, allowing two canonical types to be compared
1669/// for exact equality with a simple pointer comparison.
1670QualType ASTContext::getCanonicalType(QualType T) {
1671 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001672
1673 // If the result has type qualifiers, make sure to canonicalize them as well.
1674 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1675 if (TypeQuals == 0) return CanType;
1676
1677 // If the type qualifiers are on an array type, get the canonical type of the
1678 // array with the qualifiers applied to the element type.
1679 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1680 if (!AT)
1681 return CanType.getQualifiedType(TypeQuals);
1682
1683 // Get the canonical version of the element with the extra qualifiers on it.
1684 // This can recursively sink qualifiers through multiple levels of arrays.
1685 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1686 NewEltTy = getCanonicalType(NewEltTy);
1687
1688 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1689 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1690 CAT->getIndexTypeQualifier());
1691 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1692 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1693 IAT->getIndexTypeQualifier());
1694
Douglas Gregor898574e2008-12-05 23:32:09 +00001695 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1696 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1697 DSAT->getSizeModifier(),
1698 DSAT->getIndexTypeQualifier());
1699
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001700 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1701 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1702 VAT->getSizeModifier(),
1703 VAT->getIndexTypeQualifier());
1704}
1705
Douglas Gregor7da97d02009-05-10 22:57:19 +00001706Decl *ASTContext::getCanonicalDecl(Decl *D) {
1707 if (TagDecl *Tag = dyn_cast<TagDecl>(D)) {
1708 QualType T = getTagDeclType(Tag);
1709 return cast<TagDecl>(cast<TagType>(T.getTypePtr()->CanonicalType)
1710 ->getDecl());
1711 }
1712
1713 if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(D)) {
1714 while (Template->getPreviousDeclaration())
1715 Template = Template->getPreviousDeclaration();
1716 return Template;
1717 }
1718
1719 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
1720 while (Function->getPreviousDeclaration())
1721 Function = Function->getPreviousDeclaration();
1722 return const_cast<FunctionDecl *>(Function);
1723 }
1724
1725 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
1726 while (Var->getPreviousDeclaration())
1727 Var = Var->getPreviousDeclaration();
1728 return const_cast<VarDecl *>(Var);
1729 }
1730
1731 return D;
1732}
1733
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001734TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
1735 // If this template name refers to a template, the canonical
1736 // template name merely stores the template itself.
1737 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Douglas Gregor7da97d02009-05-10 22:57:19 +00001738 return TemplateName(cast<TemplateDecl>(getCanonicalDecl(Template)));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001739
1740 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
1741 assert(DTN && "Non-dependent template names must refer to template decls.");
1742 return DTN->CanonicalTemplateName;
1743}
1744
Douglas Gregord57959a2009-03-27 23:10:48 +00001745NestedNameSpecifier *
1746ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1747 if (!NNS)
1748 return 0;
1749
1750 switch (NNS->getKind()) {
1751 case NestedNameSpecifier::Identifier:
1752 // Canonicalize the prefix but keep the identifier the same.
1753 return NestedNameSpecifier::Create(*this,
1754 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1755 NNS->getAsIdentifier());
1756
1757 case NestedNameSpecifier::Namespace:
1758 // A namespace is canonical; build a nested-name-specifier with
1759 // this namespace and no prefix.
1760 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1761
1762 case NestedNameSpecifier::TypeSpec:
1763 case NestedNameSpecifier::TypeSpecWithTemplate: {
1764 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1765 NestedNameSpecifier *Prefix = 0;
1766
1767 // FIXME: This isn't the right check!
1768 if (T->isDependentType())
1769 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1770
1771 return NestedNameSpecifier::Create(*this, Prefix,
1772 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1773 T.getTypePtr());
1774 }
1775
1776 case NestedNameSpecifier::Global:
1777 // The global specifier is canonical and unique.
1778 return NNS;
1779 }
1780
1781 // Required to silence a GCC warning
1782 return 0;
1783}
1784
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001785
1786const ArrayType *ASTContext::getAsArrayType(QualType T) {
1787 // Handle the non-qualified case efficiently.
1788 if (T.getCVRQualifiers() == 0) {
1789 // Handle the common positive case fast.
1790 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1791 return AT;
1792 }
1793
1794 // Handle the common negative case fast, ignoring CVR qualifiers.
1795 QualType CType = T->getCanonicalTypeInternal();
1796
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001797 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001798 // test.
1799 if (!isa<ArrayType>(CType) &&
1800 !isa<ArrayType>(CType.getUnqualifiedType()))
1801 return 0;
1802
1803 // Apply any CVR qualifiers from the array type to the element type. This
1804 // implements C99 6.7.3p8: "If the specification of an array type includes
1805 // any type qualifiers, the element type is so qualified, not the array type."
1806
1807 // If we get here, we either have type qualifiers on the type, or we have
1808 // sugar such as a typedef in the way. If we have type qualifiers on the type
1809 // we must propagate them down into the elemeng type.
1810 unsigned CVRQuals = T.getCVRQualifiers();
1811 unsigned AddrSpace = 0;
1812 Type *Ty = T.getTypePtr();
1813
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001814 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001815 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001816 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1817 AddrSpace = EXTQT->getAddressSpace();
1818 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001819 } else {
1820 T = Ty->getDesugaredType();
1821 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1822 break;
1823 CVRQuals |= T.getCVRQualifiers();
1824 Ty = T.getTypePtr();
1825 }
1826 }
1827
1828 // If we have a simple case, just return now.
1829 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1830 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1831 return ATy;
1832
1833 // Otherwise, we have an array and we have qualifiers on it. Push the
1834 // qualifiers into the array element type and return a new array type.
1835 // Get the canonical version of the element with the extra qualifiers on it.
1836 // This can recursively sink qualifiers through multiple levels of arrays.
1837 QualType NewEltTy = ATy->getElementType();
1838 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001839 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001840 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1841
1842 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1843 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1844 CAT->getSizeModifier(),
1845 CAT->getIndexTypeQualifier()));
1846 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1847 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1848 IAT->getSizeModifier(),
1849 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001850
Douglas Gregor898574e2008-12-05 23:32:09 +00001851 if (const DependentSizedArrayType *DSAT
1852 = dyn_cast<DependentSizedArrayType>(ATy))
1853 return cast<ArrayType>(
1854 getDependentSizedArrayType(NewEltTy,
1855 DSAT->getSizeExpr(),
1856 DSAT->getSizeModifier(),
1857 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001858
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001859 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1860 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1861 VAT->getSizeModifier(),
1862 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001863}
1864
1865
Chris Lattnere6327742008-04-02 05:18:44 +00001866/// getArrayDecayedType - Return the properly qualified result of decaying the
1867/// specified array type to a pointer. This operation is non-trivial when
1868/// handling typedefs etc. The canonical type of "T" must be an array type,
1869/// this returns a pointer to a properly qualified element of the array.
1870///
1871/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1872QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001873 // Get the element type with 'getAsArrayType' so that we don't lose any
1874 // typedefs in the element type of the array. This also handles propagation
1875 // of type qualifiers from the array type into the element type if present
1876 // (C99 6.7.3p8).
1877 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1878 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001879
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001880 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001881
1882 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001883 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001884}
1885
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001886QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001887 QualType ElemTy = VAT->getElementType();
1888
1889 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1890 return getBaseElementType(VAT);
1891
1892 return ElemTy;
1893}
1894
Reid Spencer5f016e22007-07-11 17:01:13 +00001895/// getFloatingRank - Return a relative rank for floating point types.
1896/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001897static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001898 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001899 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001900
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001901 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001902 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001903 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001904 case BuiltinType::Float: return FloatRank;
1905 case BuiltinType::Double: return DoubleRank;
1906 case BuiltinType::LongDouble: return LongDoubleRank;
1907 }
1908}
1909
Steve Naroff716c7302007-08-27 01:41:48 +00001910/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1911/// point or a complex type (based on typeDomain/typeSize).
1912/// 'typeDomain' is a real floating point or complex type.
1913/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001914QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1915 QualType Domain) const {
1916 FloatingRank EltRank = getFloatingRank(Size);
1917 if (Domain->isComplexType()) {
1918 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001919 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001920 case FloatRank: return FloatComplexTy;
1921 case DoubleRank: return DoubleComplexTy;
1922 case LongDoubleRank: return LongDoubleComplexTy;
1923 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001924 }
Chris Lattner1361b112008-04-06 23:58:54 +00001925
1926 assert(Domain->isRealFloatingType() && "Unknown domain!");
1927 switch (EltRank) {
1928 default: assert(0 && "getFloatingRank(): illegal value for rank");
1929 case FloatRank: return FloatTy;
1930 case DoubleRank: return DoubleTy;
1931 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001932 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001933}
1934
Chris Lattner7cfeb082008-04-06 23:55:33 +00001935/// getFloatingTypeOrder - Compare the rank of the two specified floating
1936/// point types, ignoring the domain of the type (i.e. 'double' ==
1937/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1938/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001939int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1940 FloatingRank LHSR = getFloatingRank(LHS);
1941 FloatingRank RHSR = getFloatingRank(RHS);
1942
1943 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001944 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001945 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001946 return 1;
1947 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001948}
1949
Chris Lattnerf52ab252008-04-06 22:59:24 +00001950/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1951/// routine will assert if passed a built-in type that isn't an integer or enum,
1952/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001953unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001954 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001955 if (EnumType* ET = dyn_cast<EnumType>(T))
1956 T = ET->getDecl()->getIntegerType().getTypePtr();
1957
1958 // There are two things which impact the integer rank: the width, and
1959 // the ordering of builtins. The builtin ordering is encoded in the
1960 // bottom three bits; the width is encoded in the bits above that.
1961 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1962 return FWIT->getWidth() << 3;
1963 }
1964
Chris Lattnerf52ab252008-04-06 22:59:24 +00001965 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001966 default: assert(0 && "getIntegerRank(): not a built-in integer");
1967 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001968 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001969 case BuiltinType::Char_S:
1970 case BuiltinType::Char_U:
1971 case BuiltinType::SChar:
1972 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001973 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001974 case BuiltinType::Short:
1975 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001976 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001977 case BuiltinType::Int:
1978 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001979 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001980 case BuiltinType::Long:
1981 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001982 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001983 case BuiltinType::LongLong:
1984 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001985 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00001986 case BuiltinType::Int128:
1987 case BuiltinType::UInt128:
1988 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001989 }
1990}
1991
Chris Lattner7cfeb082008-04-06 23:55:33 +00001992/// getIntegerTypeOrder - Returns the highest ranked integer type:
1993/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1994/// LHS < RHS, return -1.
1995int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001996 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1997 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001998 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001999
Chris Lattnerf52ab252008-04-06 22:59:24 +00002000 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2001 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00002002
Chris Lattner7cfeb082008-04-06 23:55:33 +00002003 unsigned LHSRank = getIntegerRank(LHSC);
2004 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00002005
Chris Lattner7cfeb082008-04-06 23:55:33 +00002006 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2007 if (LHSRank == RHSRank) return 0;
2008 return LHSRank > RHSRank ? 1 : -1;
2009 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002010
Chris Lattner7cfeb082008-04-06 23:55:33 +00002011 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2012 if (LHSUnsigned) {
2013 // If the unsigned [LHS] type is larger, return it.
2014 if (LHSRank >= RHSRank)
2015 return 1;
2016
2017 // If the signed type can represent all values of the unsigned type, it
2018 // wins. Because we are dealing with 2's complement and types that are
2019 // powers of two larger than each other, this is always safe.
2020 return -1;
2021 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002022
Chris Lattner7cfeb082008-04-06 23:55:33 +00002023 // If the unsigned [RHS] type is larger, return it.
2024 if (RHSRank >= LHSRank)
2025 return -1;
2026
2027 // If the signed type can represent all values of the unsigned type, it
2028 // wins. Because we are dealing with 2's complement and types that are
2029 // powers of two larger than each other, this is always safe.
2030 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002031}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002032
2033// getCFConstantStringType - Return the type used for constant CFStrings.
2034QualType ASTContext::getCFConstantStringType() {
2035 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00002036 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002037 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002038 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002039 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00002040
2041 // const int *isa;
2042 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002043 // int flags;
2044 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002045 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002046 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00002047 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002048 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00002049
Anders Carlsson71993dd2007-08-17 05:31:46 +00002050 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002051 for (unsigned i = 0; i < 4; ++i) {
2052 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2053 SourceLocation(), 0,
2054 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002055 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002056 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002057 }
2058
2059 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002060 }
2061
2062 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002063}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002064
Douglas Gregor319ac892009-04-23 22:29:11 +00002065void ASTContext::setCFConstantStringType(QualType T) {
2066 const RecordType *Rec = T->getAsRecordType();
2067 assert(Rec && "Invalid CFConstantStringType");
2068 CFConstantStringTypeDecl = Rec->getDecl();
2069}
2070
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002071QualType ASTContext::getObjCFastEnumerationStateType()
2072{
2073 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002074 ObjCFastEnumerationStateTypeDecl =
2075 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2076 &Idents.get("__objcFastEnumerationState"));
2077
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002078 QualType FieldTypes[] = {
2079 UnsignedLongTy,
2080 getPointerType(ObjCIdType),
2081 getPointerType(UnsignedLongTy),
2082 getConstantArrayType(UnsignedLongTy,
2083 llvm::APInt(32, 5), ArrayType::Normal, 0)
2084 };
2085
Douglas Gregor44b43212008-12-11 16:49:14 +00002086 for (size_t i = 0; i < 4; ++i) {
2087 FieldDecl *Field = FieldDecl::Create(*this,
2088 ObjCFastEnumerationStateTypeDecl,
2089 SourceLocation(), 0,
2090 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002091 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002092 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002093 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002094
Douglas Gregor44b43212008-12-11 16:49:14 +00002095 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002096 }
2097
2098 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2099}
2100
Douglas Gregor319ac892009-04-23 22:29:11 +00002101void ASTContext::setObjCFastEnumerationStateType(QualType T) {
2102 const RecordType *Rec = T->getAsRecordType();
2103 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2104 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2105}
2106
Anders Carlssone8c49532007-10-29 06:33:42 +00002107// This returns true if a type has been typedefed to BOOL:
2108// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002109static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002110 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002111 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2112 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002113
2114 return false;
2115}
2116
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002117/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002118/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002119int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002120 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002121
2122 // Make all integer and enum types at least as large as an int
2123 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002124 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002125 // Treat arrays as pointers, since that's how they're passed in.
2126 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002127 sz = getTypeSize(VoidPtrTy);
2128 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002129}
2130
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002131/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002132/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002133void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002134 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002135 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002136 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002137 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002138 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002139 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002140 // Compute size of all parameters.
2141 // Start with computing size of a pointer in number of bytes.
2142 // FIXME: There might(should) be a better way of doing this computation!
2143 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002144 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002145 // The first two arguments (self and _cmd) are pointers; account for
2146 // their size.
2147 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002148 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2149 E = Decl->param_end(); PI != E; ++PI) {
2150 QualType PType = (*PI)->getType();
2151 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002152 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002153 ParmOffset += sz;
2154 }
2155 S += llvm::utostr(ParmOffset);
2156 S += "@0:";
2157 S += llvm::utostr(PtrSize);
2158
2159 // Argument types.
2160 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002161 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2162 E = Decl->param_end(); PI != E; ++PI) {
2163 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002164 QualType PType = PVDecl->getOriginalType();
2165 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002166 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2167 // Use array's original type only if it has known number of
2168 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002169 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002170 PType = PVDecl->getType();
2171 } else if (PType->isFunctionType())
2172 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002173 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002174 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002175 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002176 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002177 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002178 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002179 }
2180}
2181
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002182/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002183/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002184/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2185/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002186/// Property attributes are stored as a comma-delimited C string. The simple
2187/// attributes readonly and bycopy are encoded as single characters. The
2188/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2189/// encoded as single characters, followed by an identifier. Property types
2190/// are also encoded as a parametrized attribute. The characters used to encode
2191/// these attributes are defined by the following enumeration:
2192/// @code
2193/// enum PropertyAttributes {
2194/// kPropertyReadOnly = 'R', // property is read-only.
2195/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2196/// kPropertyByref = '&', // property is a reference to the value last assigned
2197/// kPropertyDynamic = 'D', // property is dynamic
2198/// kPropertyGetter = 'G', // followed by getter selector name
2199/// kPropertySetter = 'S', // followed by setter selector name
2200/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2201/// kPropertyType = 't' // followed by old-style type encoding.
2202/// kPropertyWeak = 'W' // 'weak' property
2203/// kPropertyStrong = 'P' // property GC'able
2204/// kPropertyNonAtomic = 'N' // property non-atomic
2205/// };
2206/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002207void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2208 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002209 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002210 // Collect information from the property implementation decl(s).
2211 bool Dynamic = false;
2212 ObjCPropertyImplDecl *SynthesizePID = 0;
2213
2214 // FIXME: Duplicated code due to poor abstraction.
2215 if (Container) {
2216 if (const ObjCCategoryImplDecl *CID =
2217 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2218 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002219 i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2220 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002221 ObjCPropertyImplDecl *PID = *i;
2222 if (PID->getPropertyDecl() == PD) {
2223 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2224 Dynamic = true;
2225 } else {
2226 SynthesizePID = PID;
2227 }
2228 }
2229 }
2230 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002231 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002232 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002233 i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2234 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002235 ObjCPropertyImplDecl *PID = *i;
2236 if (PID->getPropertyDecl() == PD) {
2237 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2238 Dynamic = true;
2239 } else {
2240 SynthesizePID = PID;
2241 }
2242 }
2243 }
2244 }
2245 }
2246
2247 // FIXME: This is not very efficient.
2248 S = "T";
2249
2250 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002251 // GCC has some special rules regarding encoding of properties which
2252 // closely resembles encoding of ivars.
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002253 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002254 true /* outermost type */,
2255 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002256
2257 if (PD->isReadOnly()) {
2258 S += ",R";
2259 } else {
2260 switch (PD->getSetterKind()) {
2261 case ObjCPropertyDecl::Assign: break;
2262 case ObjCPropertyDecl::Copy: S += ",C"; break;
2263 case ObjCPropertyDecl::Retain: S += ",&"; break;
2264 }
2265 }
2266
2267 // It really isn't clear at all what this means, since properties
2268 // are "dynamic by default".
2269 if (Dynamic)
2270 S += ",D";
2271
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002272 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2273 S += ",N";
2274
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002275 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2276 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002277 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002278 }
2279
2280 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2281 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002282 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002283 }
2284
2285 if (SynthesizePID) {
2286 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2287 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002288 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002289 }
2290
2291 // FIXME: OBJCGC: weak & strong
2292}
2293
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002294/// getLegacyIntegralTypeEncoding -
2295/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002296/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002297/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2298///
2299void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2300 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2301 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002302 if (BT->getKind() == BuiltinType::ULong &&
2303 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002304 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002305 else
2306 if (BT->getKind() == BuiltinType::Long &&
2307 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002308 PointeeTy = IntTy;
2309 }
2310 }
2311}
2312
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002313void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002314 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002315 // We follow the behavior of gcc, expanding structures which are
2316 // directly pointed to, and expanding embedded structures. Note that
2317 // these rules are sufficient to prevent recursive encoding of the
2318 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002319 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2320 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002321}
2322
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002323static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002324 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002325 const Expr *E = FD->getBitWidth();
2326 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2327 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002328 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002329 S += 'b';
2330 S += llvm::utostr(N);
2331}
2332
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002333void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2334 bool ExpandPointedToStructures,
2335 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002336 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002337 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002338 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002339 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002340 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002341 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002342 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002343 else {
2344 char encoding;
2345 switch (BT->getKind()) {
2346 default: assert(0 && "Unhandled builtin type kind");
2347 case BuiltinType::Void: encoding = 'v'; break;
2348 case BuiltinType::Bool: encoding = 'B'; break;
2349 case BuiltinType::Char_U:
2350 case BuiltinType::UChar: encoding = 'C'; break;
2351 case BuiltinType::UShort: encoding = 'S'; break;
2352 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002353 case BuiltinType::ULong:
2354 encoding =
2355 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2356 break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002357 case BuiltinType::UInt128: encoding = 'T'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002358 case BuiltinType::ULongLong: encoding = 'Q'; break;
2359 case BuiltinType::Char_S:
2360 case BuiltinType::SChar: encoding = 'c'; break;
2361 case BuiltinType::Short: encoding = 's'; break;
2362 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002363 case BuiltinType::Long:
2364 encoding =
2365 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2366 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002367 case BuiltinType::LongLong: encoding = 'q'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002368 case BuiltinType::Int128: encoding = 't'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002369 case BuiltinType::Float: encoding = 'f'; break;
2370 case BuiltinType::Double: encoding = 'd'; break;
2371 case BuiltinType::LongDouble: encoding = 'd'; break;
2372 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002373
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002374 S += encoding;
2375 }
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002376 } else if (const ComplexType *CT = T->getAsComplexType()) {
2377 S += 'j';
2378 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2379 false);
2380 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002381 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2382 ExpandPointedToStructures,
2383 ExpandStructures, FD);
2384 if (FD || EncodingProperty) {
2385 // Note that we do extended encoding of protocol qualifer list
2386 // Only when doing ivar or property encoding.
2387 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2388 S += '"';
2389 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2390 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2391 S += '<';
2392 S += Proto->getNameAsString();
2393 S += '>';
2394 }
2395 S += '"';
2396 }
2397 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002398 }
2399 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002400 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002401 bool isReadOnly = false;
2402 // For historical/compatibility reasons, the read-only qualifier of the
2403 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2404 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2405 // Also, do not emit the 'r' for anything but the outermost type!
2406 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2407 if (OutermostType && T.isConstQualified()) {
2408 isReadOnly = true;
2409 S += 'r';
2410 }
2411 }
2412 else if (OutermostType) {
2413 QualType P = PointeeTy;
2414 while (P->getAsPointerType())
2415 P = P->getAsPointerType()->getPointeeType();
2416 if (P.isConstQualified()) {
2417 isReadOnly = true;
2418 S += 'r';
2419 }
2420 }
2421 if (isReadOnly) {
2422 // Another legacy compatibility encoding. Some ObjC qualifier and type
2423 // combinations need to be rearranged.
2424 // Rewrite "in const" from "nr" to "rn"
2425 const char * s = S.c_str();
2426 int len = S.length();
2427 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2428 std::string replace = "rn";
2429 S.replace(S.end()-2, S.end(), replace);
2430 }
2431 }
Steve Naroff389bf462009-02-12 17:52:19 +00002432 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002433 S += '@';
2434 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002435 }
2436 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002437 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002438 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002439 // Another historical/compatibility reason.
2440 // We encode the underlying type which comes out as
2441 // {...};
2442 S += '^';
2443 getObjCEncodingForTypeImpl(PointeeTy, S,
2444 false, ExpandPointedToStructures,
2445 NULL);
2446 return;
2447 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002448 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002449 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002450 const ObjCInterfaceType *OIT =
2451 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002452 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002453 S += '"';
2454 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002455 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2456 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2457 S += '<';
2458 S += Proto->getNameAsString();
2459 S += '>';
2460 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002461 S += '"';
2462 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002463 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002464 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002465 S += '#';
2466 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002467 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002468 S += ':';
2469 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002470 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002471
2472 if (PointeeTy->isCharType()) {
2473 // char pointer types should be encoded as '*' unless it is a
2474 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002475 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002476 S += '*';
2477 return;
2478 }
2479 }
2480
2481 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002482 getLegacyIntegralTypeEncoding(PointeeTy);
2483
2484 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002485 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002486 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002487 } else if (const ArrayType *AT =
2488 // Ignore type qualifiers etc.
2489 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002490 if (isa<IncompleteArrayType>(AT)) {
2491 // Incomplete arrays are encoded as a pointer to the array element.
2492 S += '^';
2493
2494 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2495 false, ExpandStructures, FD);
2496 } else {
2497 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002498
Anders Carlsson559a8332009-02-22 01:38:57 +00002499 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2500 S += llvm::utostr(CAT->getSize().getZExtValue());
2501 else {
2502 //Variable length arrays are encoded as a regular array with 0 elements.
2503 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2504 S += '0';
2505 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002506
Anders Carlsson559a8332009-02-22 01:38:57 +00002507 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2508 false, ExpandStructures, FD);
2509 S += ']';
2510 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002511 } else if (T->getAsFunctionType()) {
2512 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002513 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002514 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002515 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002516 // Anonymous structures print as '?'
2517 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2518 S += II->getName();
2519 } else {
2520 S += '?';
2521 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002522 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002523 S += '=';
Douglas Gregor6ab35242009-04-09 21:40:53 +00002524 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2525 FieldEnd = RDecl->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +00002526 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002527 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002528 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002529 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002530 S += '"';
2531 }
2532
2533 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002534 if (Field->isBitField()) {
2535 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2536 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002537 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002538 QualType qt = Field->getType();
2539 getLegacyIntegralTypeEncoding(qt);
2540 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002541 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002542 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002543 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002544 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002545 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002546 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002547 if (FD && FD->isBitField())
2548 EncodeBitField(this, S, FD);
2549 else
2550 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002551 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002552 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002553 } else if (T->isObjCInterfaceType()) {
2554 // @encode(class_name)
2555 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2556 S += '{';
2557 const IdentifierInfo *II = OI->getIdentifier();
2558 S += II->getName();
2559 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002560 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002561 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002562 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002563 if (RecFields[i]->isBitField())
2564 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2565 RecFields[i]);
2566 else
2567 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2568 FD);
2569 }
2570 S += '}';
2571 }
2572 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002573 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002574}
2575
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002576void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002577 std::string& S) const {
2578 if (QT & Decl::OBJC_TQ_In)
2579 S += 'n';
2580 if (QT & Decl::OBJC_TQ_Inout)
2581 S += 'N';
2582 if (QT & Decl::OBJC_TQ_Out)
2583 S += 'o';
2584 if (QT & Decl::OBJC_TQ_Bycopy)
2585 S += 'O';
2586 if (QT & Decl::OBJC_TQ_Byref)
2587 S += 'R';
2588 if (QT & Decl::OBJC_TQ_Oneway)
2589 S += 'V';
2590}
2591
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002592void ASTContext::setBuiltinVaListType(QualType T)
2593{
2594 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2595
2596 BuiltinVaListType = T;
2597}
2598
Douglas Gregor319ac892009-04-23 22:29:11 +00002599void ASTContext::setObjCIdType(QualType T)
Steve Naroff7e219e42007-10-15 14:41:52 +00002600{
Douglas Gregor319ac892009-04-23 22:29:11 +00002601 ObjCIdType = T;
2602
2603 const TypedefType *TT = T->getAsTypedefType();
2604 if (!TT)
2605 return;
2606
2607 TypedefDecl *TD = TT->getDecl();
Steve Naroff7e219e42007-10-15 14:41:52 +00002608
2609 // typedef struct objc_object *id;
2610 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002611 // User error - caller will issue diagnostics.
2612 if (!ptr)
2613 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002614 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002615 // User error - caller will issue diagnostics.
2616 if (!rec)
2617 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002618 IdStructType = rec;
2619}
2620
Douglas Gregor319ac892009-04-23 22:29:11 +00002621void ASTContext::setObjCSelType(QualType T)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002622{
Douglas Gregor319ac892009-04-23 22:29:11 +00002623 ObjCSelType = T;
2624
2625 const TypedefType *TT = T->getAsTypedefType();
2626 if (!TT)
2627 return;
2628 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002629
2630 // typedef struct objc_selector *SEL;
2631 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002632 if (!ptr)
2633 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002634 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002635 if (!rec)
2636 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002637 SelStructType = rec;
2638}
2639
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002640void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002641{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002642 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002643}
2644
Douglas Gregor319ac892009-04-23 22:29:11 +00002645void ASTContext::setObjCClassType(QualType T)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002646{
Douglas Gregor319ac892009-04-23 22:29:11 +00002647 ObjCClassType = T;
2648
2649 const TypedefType *TT = T->getAsTypedefType();
2650 if (!TT)
2651 return;
2652 TypedefDecl *TD = TT->getDecl();
Anders Carlsson8baaca52007-10-31 02:53:19 +00002653
2654 // typedef struct objc_class *Class;
2655 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2656 assert(ptr && "'Class' incorrectly typed");
2657 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2658 assert(rec && "'Class' incorrectly typed");
2659 ClassStructType = rec;
2660}
2661
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002662void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2663 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002664 "'NSConstantString' type already set!");
2665
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002666 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002667}
2668
Douglas Gregor7532dc62009-03-30 22:58:21 +00002669/// \brief Retrieve the template name that represents a qualified
2670/// template name such as \c std::vector.
2671TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2672 bool TemplateKeyword,
2673 TemplateDecl *Template) {
2674 llvm::FoldingSetNodeID ID;
2675 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2676
2677 void *InsertPos = 0;
2678 QualifiedTemplateName *QTN =
2679 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2680 if (!QTN) {
2681 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2682 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2683 }
2684
2685 return TemplateName(QTN);
2686}
2687
2688/// \brief Retrieve the template name that represents a dependent
2689/// template name such as \c MetaFun::template apply.
2690TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2691 const IdentifierInfo *Name) {
2692 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2693
2694 llvm::FoldingSetNodeID ID;
2695 DependentTemplateName::Profile(ID, NNS, Name);
2696
2697 void *InsertPos = 0;
2698 DependentTemplateName *QTN =
2699 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2700
2701 if (QTN)
2702 return TemplateName(QTN);
2703
2704 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2705 if (CanonNNS == NNS) {
2706 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2707 } else {
2708 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2709 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2710 }
2711
2712 DependentTemplateNames.InsertNode(QTN, InsertPos);
2713 return TemplateName(QTN);
2714}
2715
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002716/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002717/// TargetInfo, produce the corresponding type. The unsigned @p Type
2718/// is actually a value of type @c TargetInfo::IntType.
2719QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002720 switch (Type) {
2721 case TargetInfo::NoInt: return QualType();
2722 case TargetInfo::SignedShort: return ShortTy;
2723 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2724 case TargetInfo::SignedInt: return IntTy;
2725 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2726 case TargetInfo::SignedLong: return LongTy;
2727 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2728 case TargetInfo::SignedLongLong: return LongLongTy;
2729 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2730 }
2731
2732 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002733 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002734}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002735
2736//===----------------------------------------------------------------------===//
2737// Type Predicates.
2738//===----------------------------------------------------------------------===//
2739
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002740/// isObjCNSObjectType - Return true if this is an NSObject object using
2741/// NSObject attribute on a c-style pointer type.
2742/// FIXME - Make it work directly on types.
2743///
2744bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2745 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2746 if (TypedefDecl *TD = TDT->getDecl())
2747 if (TD->getAttr<ObjCNSObjectAttr>())
2748 return true;
2749 }
2750 return false;
2751}
2752
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002753/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2754/// to an object type. This includes "id" and "Class" (two 'special' pointers
2755/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2756/// ID type).
2757bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002758 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002759 return true;
2760
Steve Naroff6ae98502008-10-21 18:24:04 +00002761 // Blocks are objects.
2762 if (Ty->isBlockPointerType())
2763 return true;
2764
2765 // All other object types are pointers.
Chris Lattner16ede0e2009-04-12 23:51:02 +00002766 const PointerType *PT = Ty->getAsPointerType();
2767 if (PT == 0)
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002768 return false;
2769
Chris Lattner16ede0e2009-04-12 23:51:02 +00002770 // If this a pointer to an interface (e.g. NSString*), it is ok.
2771 if (PT->getPointeeType()->isObjCInterfaceType() ||
2772 // If is has NSObject attribute, OK as well.
2773 isObjCNSObjectType(Ty))
2774 return true;
2775
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002776 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2777 // pointer types. This looks for the typedef specifically, not for the
Chris Lattner16ede0e2009-04-12 23:51:02 +00002778 // underlying type. Iteratively strip off typedefs so that we can handle
2779 // typedefs of typedefs.
2780 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2781 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2782 Ty.getUnqualifiedType() == getObjCClassType())
2783 return true;
2784
2785 Ty = TDT->getDecl()->getUnderlyingType();
2786 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002787
Chris Lattner16ede0e2009-04-12 23:51:02 +00002788 return false;
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002789}
2790
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002791/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2792/// garbage collection attribute.
2793///
2794QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002795 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002796 if (getLangOptions().ObjC1 &&
2797 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002798 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002799 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002800 // (or pointers to them) be treated as though they were declared
2801 // as __strong.
2802 if (GCAttrs == QualType::GCNone) {
2803 if (isObjCObjectPointerType(Ty))
2804 GCAttrs = QualType::Strong;
2805 else if (Ty->isPointerType())
2806 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2807 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00002808 // Non-pointers have none gc'able attribute regardless of the attribute
2809 // set on them.
2810 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2811 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002812 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002813 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002814}
2815
Chris Lattner6ac46a42008-04-07 06:51:04 +00002816//===----------------------------------------------------------------------===//
2817// Type Compatibility Testing
2818//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002819
Steve Naroff1c7d0672008-09-04 15:10:53 +00002820/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002821/// block types. Types must be strictly compatible here. For example,
2822/// C unfortunately doesn't produce an error for the following:
2823///
2824/// int (*emptyArgFunc)();
2825/// int (*intArgList)(int) = emptyArgFunc;
2826///
2827/// For blocks, we will produce an error for the following (similar to C++):
2828///
2829/// int (^emptyArgBlock)();
2830/// int (^intArgBlock)(int) = emptyArgBlock;
2831///
2832/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2833///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002834bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002835 const FunctionType *lbase = lhs->getAsFunctionType();
2836 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002837 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2838 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stumpaab0f7a2009-04-01 01:17:39 +00002839 if (lproto && rproto == 0)
2840 return false;
2841 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff1c7d0672008-09-04 15:10:53 +00002842}
2843
Chris Lattner6ac46a42008-04-07 06:51:04 +00002844/// areCompatVectorTypes - Return true if the two specified vector types are
2845/// compatible.
2846static bool areCompatVectorTypes(const VectorType *LHS,
2847 const VectorType *RHS) {
2848 assert(LHS->isCanonical() && RHS->isCanonical());
2849 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002850 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002851}
2852
Eli Friedman3d815e72008-08-22 00:56:42 +00002853/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002854/// compatible for assignment from RHS to LHS. This handles validation of any
2855/// protocol qualifiers on the LHS or RHS.
2856///
Eli Friedman3d815e72008-08-22 00:56:42 +00002857bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2858 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002859 // Verify that the base decls are compatible: the RHS must be a subclass of
2860 // the LHS.
2861 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2862 return false;
2863
2864 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2865 // protocol qualified at all, then we are good.
2866 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2867 return true;
2868
2869 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2870 // isn't a superset.
2871 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2872 return true; // FIXME: should return false!
2873
2874 // Finally, we must have two protocol-qualified interfaces.
2875 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2876 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002877
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002878 // All LHS protocols must have a presence on the RHS.
2879 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002880
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002881 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2882 LHSPE = LHSP->qual_end();
2883 LHSPI != LHSPE; LHSPI++) {
2884 bool RHSImplementsProtocol = false;
2885
2886 // If the RHS doesn't implement the protocol on the left, the types
2887 // are incompatible.
2888 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2889 RHSPE = RHSP->qual_end();
2890 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2891 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2892 RHSImplementsProtocol = true;
2893 }
2894 // FIXME: For better diagnostics, consider passing back the protocol name.
2895 if (!RHSImplementsProtocol)
2896 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002897 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002898 // The RHS implements all protocols listed on the LHS.
2899 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002900}
2901
Steve Naroff389bf462009-02-12 17:52:19 +00002902bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2903 // get the "pointed to" types
2904 const PointerType *LHSPT = LHS->getAsPointerType();
2905 const PointerType *RHSPT = RHS->getAsPointerType();
2906
2907 if (!LHSPT || !RHSPT)
2908 return false;
2909
2910 QualType lhptee = LHSPT->getPointeeType();
2911 QualType rhptee = RHSPT->getPointeeType();
2912 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2913 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2914 // ID acts sort of like void* for ObjC interfaces
2915 if (LHSIface && isObjCIdStructType(rhptee))
2916 return true;
2917 if (RHSIface && isObjCIdStructType(lhptee))
2918 return true;
2919 if (!LHSIface || !RHSIface)
2920 return false;
2921 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2922 canAssignObjCInterfaces(RHSIface, LHSIface);
2923}
2924
Steve Naroffec0550f2007-10-15 20:41:53 +00002925/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2926/// both shall have the identically qualified version of a compatible type.
2927/// C99 6.2.7p1: Two types have compatible types if their types are the
2928/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002929bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2930 return !mergeTypes(LHS, RHS).isNull();
2931}
2932
2933QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2934 const FunctionType *lbase = lhs->getAsFunctionType();
2935 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002936 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2937 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002938 bool allLTypes = true;
2939 bool allRTypes = true;
2940
2941 // Check return type
2942 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2943 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002944 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2945 allLTypes = false;
2946 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2947 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002948
2949 if (lproto && rproto) { // two C99 style function prototypes
2950 unsigned lproto_nargs = lproto->getNumArgs();
2951 unsigned rproto_nargs = rproto->getNumArgs();
2952
2953 // Compatible functions must have the same number of arguments
2954 if (lproto_nargs != rproto_nargs)
2955 return QualType();
2956
2957 // Variadic and non-variadic functions aren't compatible
2958 if (lproto->isVariadic() != rproto->isVariadic())
2959 return QualType();
2960
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002961 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2962 return QualType();
2963
Eli Friedman3d815e72008-08-22 00:56:42 +00002964 // Check argument compatibility
2965 llvm::SmallVector<QualType, 10> types;
2966 for (unsigned i = 0; i < lproto_nargs; i++) {
2967 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2968 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2969 QualType argtype = mergeTypes(largtype, rargtype);
2970 if (argtype.isNull()) return QualType();
2971 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002972 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2973 allLTypes = false;
2974 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2975 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002976 }
2977 if (allLTypes) return lhs;
2978 if (allRTypes) return rhs;
2979 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002980 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002981 }
2982
2983 if (lproto) allRTypes = false;
2984 if (rproto) allLTypes = false;
2985
Douglas Gregor72564e72009-02-26 23:50:07 +00002986 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002987 if (proto) {
2988 if (proto->isVariadic()) return QualType();
2989 // Check that the types are compatible with the types that
2990 // would result from default argument promotions (C99 6.7.5.3p15).
2991 // The only types actually affected are promotable integer
2992 // types and floats, which would be passed as a different
2993 // type depending on whether the prototype is visible.
2994 unsigned proto_nargs = proto->getNumArgs();
2995 for (unsigned i = 0; i < proto_nargs; ++i) {
2996 QualType argTy = proto->getArgType(i);
2997 if (argTy->isPromotableIntegerType() ||
2998 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2999 return QualType();
3000 }
3001
3002 if (allLTypes) return lhs;
3003 if (allRTypes) return rhs;
3004 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003005 proto->getNumArgs(), lproto->isVariadic(),
3006 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00003007 }
3008
3009 if (allLTypes) return lhs;
3010 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00003011 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00003012}
3013
3014QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00003015 // C++ [expr]: If an expression initially has the type "reference to T", the
3016 // type is adjusted to "T" prior to any further analysis, the expression
3017 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003018 // expression is an lvalue unless the reference is an rvalue reference and
3019 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00003020 // FIXME: C++ shouldn't be going through here! The rules are different
3021 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003022 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3023 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00003024 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003025 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00003026 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003027 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00003028
Eli Friedman3d815e72008-08-22 00:56:42 +00003029 QualType LHSCan = getCanonicalType(LHS),
3030 RHSCan = getCanonicalType(RHS);
3031
3032 // If two types are identical, they are compatible.
3033 if (LHSCan == RHSCan)
3034 return LHS;
3035
3036 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003037 // Note that we handle extended qualifiers later, in the
3038 // case for ExtQualType.
3039 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00003040 return QualType();
3041
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003042 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
3043 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003044
Chris Lattner1adb8832008-01-14 05:45:46 +00003045 // We want to consider the two function types to be the same for these
3046 // comparisons, just force one to the other.
3047 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3048 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003049
3050 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003051 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3052 LHSClass = Type::ConstantArray;
3053 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3054 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00003055
Nate Begeman213541a2008-04-18 23:10:10 +00003056 // Canonicalize ExtVector -> Vector.
3057 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3058 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00003059
Chris Lattnerb0489812008-04-07 06:38:24 +00003060 // Consider qualified interfaces and interfaces the same.
3061 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
3062 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00003063
Chris Lattnera36a61f2008-04-07 05:43:21 +00003064 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003065 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003066 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3067 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003068
Steve Naroffd824c9c2009-04-14 15:11:46 +00003069 // 'id' and 'Class' act sort of like void* for ObjC interfaces
3070 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003071 return LHS;
Steve Naroffd824c9c2009-04-14 15:11:46 +00003072 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003073 return RHS;
3074
Steve Naroffbc76dd02008-12-10 22:14:21 +00003075 // ID is compatible with all qualified id types.
3076 if (LHS->isObjCQualifiedIdType()) {
3077 if (const PointerType *PT = RHS->getAsPointerType()) {
3078 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003079 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003080 return LHS;
3081 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3082 // Unfortunately, this API is part of Sema (which we don't have access
3083 // to. Need to refactor. The following check is insufficient, since we
3084 // need to make sure the class implements the protocol.
3085 if (pType->isObjCInterfaceType())
3086 return LHS;
3087 }
3088 }
3089 if (RHS->isObjCQualifiedIdType()) {
3090 if (const PointerType *PT = LHS->getAsPointerType()) {
3091 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003092 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003093 return RHS;
3094 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3095 // Unfortunately, this API is part of Sema (which we don't have access
3096 // to. Need to refactor. The following check is insufficient, since we
3097 // need to make sure the class implements the protocol.
3098 if (pType->isObjCInterfaceType())
3099 return RHS;
3100 }
3101 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003102 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3103 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00003104 if (const EnumType* ETy = LHS->getAsEnumType()) {
3105 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3106 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003107 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003108 if (const EnumType* ETy = RHS->getAsEnumType()) {
3109 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3110 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003111 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003112
Eli Friedman3d815e72008-08-22 00:56:42 +00003113 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003114 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003115
Steve Naroff4a746782008-01-09 22:43:08 +00003116 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003117 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003118#define TYPE(Class, Base)
3119#define ABSTRACT_TYPE(Class, Base)
3120#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3121#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3122#include "clang/AST/TypeNodes.def"
3123 assert(false && "Non-canonical and dependent types shouldn't get here");
3124 return QualType();
3125
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003126 case Type::LValueReference:
3127 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003128 case Type::MemberPointer:
3129 assert(false && "C++ should never be in mergeTypes");
3130 return QualType();
3131
3132 case Type::IncompleteArray:
3133 case Type::VariableArray:
3134 case Type::FunctionProto:
3135 case Type::ExtVector:
3136 case Type::ObjCQualifiedInterface:
3137 assert(false && "Types are eliminated above");
3138 return QualType();
3139
Chris Lattner1adb8832008-01-14 05:45:46 +00003140 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003141 {
3142 // Merge two pointer types, while trying to preserve typedef info
3143 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3144 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3145 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3146 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003147 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3148 return LHS;
3149 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3150 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003151 return getPointerType(ResultType);
3152 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003153 case Type::BlockPointer:
3154 {
3155 // Merge two block pointer types, while trying to preserve typedef info
3156 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3157 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3158 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3159 if (ResultType.isNull()) return QualType();
3160 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3161 return LHS;
3162 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3163 return RHS;
3164 return getBlockPointerType(ResultType);
3165 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003166 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003167 {
3168 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3169 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3170 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3171 return QualType();
3172
3173 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3174 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3175 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3176 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003177 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3178 return LHS;
3179 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3180 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003181 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3182 ArrayType::ArraySizeModifier(), 0);
3183 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3184 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003185 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3186 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003187 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3188 return LHS;
3189 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3190 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003191 if (LVAT) {
3192 // FIXME: This isn't correct! But tricky to implement because
3193 // the array's size has to be the size of LHS, but the type
3194 // has to be different.
3195 return LHS;
3196 }
3197 if (RVAT) {
3198 // FIXME: This isn't correct! But tricky to implement because
3199 // the array's size has to be the size of RHS, but the type
3200 // has to be different.
3201 return RHS;
3202 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003203 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3204 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003205 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003206 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003207 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003208 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003209 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003210 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003211 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003212 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3213 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003214 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003215 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003216 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003217 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003218 case Type::Complex:
3219 // Distinct complex types are incompatible.
3220 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003221 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003222 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003223 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3224 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003225 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003226 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003227 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003228 // FIXME: This should be type compatibility, e.g. whether
3229 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003230 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3231 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3232 if (LHSIface && RHSIface &&
3233 canAssignObjCInterfaces(LHSIface, RHSIface))
3234 return LHS;
3235
Eli Friedman3d815e72008-08-22 00:56:42 +00003236 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003237 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003238 case Type::ObjCQualifiedId:
3239 // Distinct qualified id's are not compatible.
3240 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003241 case Type::FixedWidthInt:
3242 // Distinct fixed-width integers are not compatible.
3243 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003244 case Type::ExtQual:
3245 // FIXME: ExtQual types can be compatible even if they're not
3246 // identical!
3247 return QualType();
3248 // First attempt at an implementation, but I'm not really sure it's
3249 // right...
3250#if 0
3251 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3252 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3253 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3254 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3255 return QualType();
3256 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3257 LHSBase = QualType(LQual->getBaseType(), 0);
3258 RHSBase = QualType(RQual->getBaseType(), 0);
3259 ResultType = mergeTypes(LHSBase, RHSBase);
3260 if (ResultType.isNull()) return QualType();
3261 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3262 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3263 return LHS;
3264 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3265 return RHS;
3266 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3267 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3268 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3269 return ResultType;
3270#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003271
3272 case Type::TemplateSpecialization:
3273 assert(false && "Dependent types have no size");
3274 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003275 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003276
3277 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003278}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003279
Chris Lattner5426bf62008-04-07 07:01:58 +00003280//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003281// Integer Predicates
3282//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003283
Eli Friedmanad74a752008-06-28 06:23:08 +00003284unsigned ASTContext::getIntWidth(QualType T) {
3285 if (T == BoolTy)
3286 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003287 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3288 return FWIT->getWidth();
3289 }
3290 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003291 return (unsigned)getTypeSize(T);
3292}
3293
3294QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3295 assert(T->isSignedIntegerType() && "Unexpected type");
3296 if (const EnumType* ETy = T->getAsEnumType())
3297 T = ETy->getDecl()->getIntegerType();
3298 const BuiltinType* BTy = T->getAsBuiltinType();
3299 assert (BTy && "Unexpected signed integer type");
3300 switch (BTy->getKind()) {
3301 case BuiltinType::Char_S:
3302 case BuiltinType::SChar:
3303 return UnsignedCharTy;
3304 case BuiltinType::Short:
3305 return UnsignedShortTy;
3306 case BuiltinType::Int:
3307 return UnsignedIntTy;
3308 case BuiltinType::Long:
3309 return UnsignedLongTy;
3310 case BuiltinType::LongLong:
3311 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00003312 case BuiltinType::Int128:
3313 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00003314 default:
3315 assert(0 && "Unexpected signed integer type");
3316 return QualType();
3317 }
3318}
3319
Douglas Gregor2cf26342009-04-09 22:27:44 +00003320ExternalASTSource::~ExternalASTSource() { }
3321
3322void ExternalASTSource::PrintStats() { }