blob: 52eadef75ab9e60e0f0e745fa7600c2335af4412 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000019#include "clang/AST/ExternalASTSource.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattnerb09b31d2009-03-28 03:45:20 +000021#include "clang/Basic/SourceManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000022#include "clang/Basic/TargetInfo.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000023#include "llvm/ADT/StringExtras.h"
Nate Begeman7903d052009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Chris Lattnerf4fbc442009-03-28 04:27:18 +000025#include "llvm/Support/MemoryBuffer.h"
Chris Lattner4b009652007-07-25 00:24:17 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner2fda0ed2008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Douglas Gregorda38c6c2009-04-22 18:49:13 +000035 bool FreeMem, unsigned size_reserve,
36 bool InitializeBuiltins) :
Douglas Gregor1e589cc2009-03-26 23:50:42 +000037 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
38 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Douglas Gregorc34897d2009-04-09 22:27:44 +000039 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels),
40 ExternalSource(0) {
Daniel Dunbarde300732008-08-11 04:54:23 +000041 if (size_reserve > 0) Types.reserve(size_reserve);
42 InitBuiltinTypes();
Daniel Dunbarde300732008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
Douglas Gregord37ce612009-04-26 03:57:37 +000044 BuiltinInfo.InitializeTargetBuiltins(Target);
Douglas Gregorda38c6c2009-04-22 18:49:13 +000045 if (InitializeBuiltins)
46 this->InitializeBuiltins(idents);
Daniel Dunbarde300732008-08-11 04:54:23 +000047}
48
Chris Lattner4b009652007-07-25 00:24:17 +000049ASTContext::~ASTContext() {
50 // Deallocate all the types.
51 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000052 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000053 Types.pop_back();
54 }
Eli Friedman65489b72008-05-27 03:08:09 +000055
Nuno Lopes355a8682008-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 Dunbar1fbaef12009-05-03 10:38:35 +000066 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
67 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopes355a8682008-12-17 22:30:25 +000068 while (I != E) {
69 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
70 delete R;
71 }
72 }
73
Douglas Gregor1e589cc2009-03-26 23:50:42 +000074 // Destroy nested-name-specifiers.
Douglas Gregor3c4eae52009-03-27 23:54:10 +000075 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
76 NNS = NestedNameSpecifiers.begin(),
77 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregorbccd97c2009-03-27 23:25:45 +000078 NNS != NNSEnd;
Douglas Gregor3c4eae52009-03-27 23:54:10 +000079 /* Increment in loop */)
80 (*NNS++).Destroy(*this);
Douglas Gregor1e589cc2009-03-26 23:50:42 +000081
82 if (GlobalNestedNameSpecifier)
83 GlobalNestedNameSpecifier->Destroy(*this);
84
Eli Friedman65489b72008-05-27 03:08:09 +000085 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000086}
87
Douglas Gregorda38c6c2009-04-22 18:49:13 +000088void ASTContext::InitializeBuiltins(IdentifierTable &idents) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +000089 BuiltinInfo.InitializeBuiltins(idents, LangOpts.NoBuiltin);
90}
91
Douglas Gregorc34897d2009-04-09 22:27:44 +000092void
93ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
94 ExternalSource.reset(Source.take());
95}
96
Chris Lattner4b009652007-07-25 00:24:17 +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 Dunbar47677342008-09-26 03:23:00 +0000101 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000102 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
103 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
104
Chris Lattner4b009652007-07-25 00:24:17 +0000105 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000106 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
107 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000108 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000109 unsigned NumExtQual = 0;
110
Chris Lattner4b009652007-07-25 00:24:17 +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 Dunbar47677342008-09-26 03:23:00 +0000117 else if (isa<BlockPointerType>(T))
118 ++NumBlockPointer;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000119 else if (isa<LValueReferenceType>(T))
120 ++NumLValueReference;
121 else if (isa<RValueReferenceType>(T))
122 ++NumRValueReference;
Sebastian Redl75555032009-01-24 21:16:55 +0000123 else if (isa<MemberPointerType>(T))
124 ++NumMemberPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000125 else if (isa<ComplexType>(T))
126 ++NumComplex;
127 else if (isa<ArrayType>(T))
128 ++NumArray;
129 else if (isa<VectorType>(T))
130 ++NumVector;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000131 else if (isa<FunctionNoProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000132 ++NumFunctionNP;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000133 else if (isa<FunctionProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000134 ++NumFunctionP;
135 else if (isa<TypedefType>(T))
136 ++NumTypeName;
137 else if (TagType *TT = dyn_cast<TagType>(T)) {
138 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000139 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000140 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-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;
Chris Lattner4b009652007-07-25 00:24:17 +0000145 }
Ted Kremenek42730c52008-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 Naroffe0430632008-05-21 15:59:22 +0000152 else if (isa<TypeOfType>(T))
153 ++NumTypeOfTypes;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000154 else if (isa<TypeOfExprType>(T))
155 ++NumTypeOfExprTypes;
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000156 else if (isa<ExtQualType>(T))
157 ++NumExtQual;
Steve Naroff948fd372007-09-17 14:16:13 +0000158 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000159 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +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 Dunbar47677342008-09-26 03:23:00 +0000166 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000167 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
168 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redl75555032009-01-24 21:16:55 +0000169 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000170 fprintf(stderr, " %d complex types\n", NumComplex);
171 fprintf(stderr, " %d array types\n", NumArray);
172 fprintf(stderr, " %d vector types\n", NumVector);
173 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 Kremenek42730c52008-01-07 19:49:32 +0000181 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000182 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000183 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000184 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000185 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000186 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000187 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000188 fprintf(stderr, " %d attribute-qualified types\n", NumExtQual);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000189
Chris Lattner4b009652007-07-25 00:24:17 +0000190 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
191 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
192 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redlce6fff02009-03-16 23:22:08 +0000193 NumLValueReference*sizeof(LValueReferenceType)+
194 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redl75555032009-01-24 21:16:55 +0000195 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor4fa58902009-02-26 23:50:07 +0000196 NumFunctionP*sizeof(FunctionProtoType)+
197 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroffe0430632008-05-21 15:59:22 +0000198 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000199 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)+
200 NumExtQual*sizeof(ExtQualType)));
Douglas Gregorc34897d2009-04-09 22:27:44 +0000201
202 if (ExternalSource.get()) {
203 fprintf(stderr, "\n");
204 ExternalSource->PrintStats();
205 }
Chris Lattner4b009652007-07-25 00:24:17 +0000206}
207
208
209void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroff93fd2112009-01-27 22:08:43 +0000210 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000211}
212
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner8cd0e932008-03-05 18:54:05 +0000222 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +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);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000244
Chris Lattner6cc7e412009-04-30 02:43:43 +0000245 // GNU extension, 128-bit integers.
246 InitBuiltinType(Int128Ty, BuiltinType::Int128);
247 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
248
Chris Lattnere1dafe72009-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());
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000253
Douglas Gregord2baafd2008-10-21 16:13:35 +0000254 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-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 Gregord2baafd2008-10-21 16:13:35 +0000263
Chris Lattner4b009652007-07-25 00:24:17 +0000264 // C99 6.2.5p11.
265 FloatComplexTy = getComplexType(FloatTy);
266 DoubleComplexTy = getComplexType(DoubleTy);
267 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000268
Steve Naroff9d12c902007-10-15 14:41:52 +0000269 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000270 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000271 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000272 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000273 ClassStructType = 0;
274
Ted Kremenek42730c52008-01-07 19:49:32 +0000275 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000276
277 // void * type
278 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl5d0ead72009-05-10 18:38:11 +0000279
280 // nullptr type (C++0x 2.14.7)
281 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Chris Lattner4b009652007-07-25 00:24:17 +0000282}
283
284//===----------------------------------------------------------------------===//
285// Type Sizing and Analysis
286//===----------------------------------------------------------------------===//
287
Chris Lattner2a674dc2008-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 Lattnerbd3153e2009-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 Dunbar96d1f1b2009-02-17 22:16:19 +0000304unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedman0ee57322009-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 Lattnerbd3153e2009-01-24 21:53:27 +0000310 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
311 QualType T = VD->getType();
Anders Carlssonaa0783b2009-04-10 04:47:03 +0000312 if (const ReferenceType* RT = T->getAsReferenceType()) {
313 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssoneeaeda32009-04-10 04:52:36 +0000314 Align = Target.getPointerAlign(AS);
Anders Carlssonaa0783b2009-04-10 04:47:03 +0000315 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
316 // Incomplete or function types default to 1.
Eli Friedman0ee57322009-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 Lattnerbd3153e2009-01-24 21:53:27 +0000322 }
Eli Friedman0ee57322009-02-22 02:56:25 +0000323
324 return Align / Target.getCharWidth();
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000325}
Chris Lattner2a674dc2008-06-30 18:32:54 +0000326
Chris Lattner4b009652007-07-25 00:24:17 +0000327/// getTypeSize - Return the size of the specified type, in bits. This method
328/// does not work on incomplete types.
329std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000330ASTContext::getTypeInfo(const Type *T) {
Mike Stump44d1f402009-02-27 18:32:39 +0000331 uint64_t Width=0;
332 unsigned Align=8;
Chris Lattner4b009652007-07-25 00:24:17 +0000333 switch (T->getTypeClass()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000334#define TYPE(Class, Base)
335#define ABSTRACT_TYPE(Class, Base)
Douglas Gregorab380272009-04-30 17:32:17 +0000336#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor4fa58902009-02-26 23:50:07 +0000337#define DEPENDENT_TYPE(Class, Base) case Type::Class:
338#include "clang/AST/TypeNodes.def"
Douglas Gregorab380272009-04-30 17:32:17 +0000339 assert(false && "Should not see dependent types");
Douglas Gregor4fa58902009-02-26 23:50:07 +0000340 break;
341
Chris Lattner4b009652007-07-25 00:24:17 +0000342 case Type::FunctionNoProto:
343 case Type::FunctionProto:
Douglas Gregorab380272009-04-30 17:32:17 +0000344 // GCC extension: alignof(function) = 32 bits
345 Width = 0;
346 Align = 32;
347 break;
348
Douglas Gregor4fa58902009-02-26 23:50:07 +0000349 case Type::IncompleteArray:
Steve Naroff83c13012007-08-30 01:06:46 +0000350 case Type::VariableArray:
Douglas Gregorab380272009-04-30 17:32:17 +0000351 Width = 0;
352 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
353 break;
354
Steve Naroff83c13012007-08-30 01:06:46 +0000355 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000356 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000357
Chris Lattner8cd0e932008-03-05 18:54:05 +0000358 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000359 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000360 Align = EltInfo.second;
361 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000362 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000363 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000364 case Type::Vector: {
365 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000366 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000367 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000368 Align = Width;
Nate Begeman7903d052009-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 Lattner4b009652007-07-25 00:24:17 +0000373 break;
374 }
375
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000376 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000377 switch (cast<BuiltinType>(T)->getKind()) {
378 default: assert(0 && "Unknown builtin type!");
379 case BuiltinType::Void:
Douglas Gregorab380272009-04-30 17:32:17 +0000380 // GCC extension: alignof(void) = 8 bits.
381 Width = 0;
382 Align = 8;
383 break;
384
Chris Lattnerb66237b2007-12-19 19:23:28 +0000385 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000386 Width = Target.getBoolWidth();
387 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000388 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000389 case BuiltinType::Char_S:
390 case BuiltinType::Char_U:
391 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000392 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000393 Width = Target.getCharWidth();
394 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000395 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000396 case BuiltinType::WChar:
397 Width = Target.getWCharWidth();
398 Align = Target.getWCharAlign();
399 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000400 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000401 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000402 Width = Target.getShortWidth();
403 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000404 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000405 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000406 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000407 Width = Target.getIntWidth();
408 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000409 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000410 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000411 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000412 Width = Target.getLongWidth();
413 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000414 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000415 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000416 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000417 Width = Target.getLongLongWidth();
418 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000419 break;
Chris Lattner4b11cc22009-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 Lattnerb66237b2007-12-19 19:23:28 +0000425 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000426 Width = Target.getFloatWidth();
427 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000428 break;
429 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000430 Width = Target.getDoubleWidth();
431 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000432 break;
433 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000434 Width = Target.getLongDoubleWidth();
435 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000436 break;
Sebastian Redl5d0ead72009-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 Lattner4b009652007-07-25 00:24:17 +0000440 }
441 break;
Eli Friedmanff3fcdf2009-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 Lattnere9174982009-02-15 21:20:13 +0000446 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000447 Align = Width;
448 break;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000449 case Type::ExtQual:
Chris Lattner8cd0e932008-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 Jahanianb60352a2009-02-17 18:27:45 +0000452 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000453 case Type::ObjCQualifiedId:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000454 case Type::ObjCQualifiedInterface:
Chris Lattner1d78a862008-04-07 07:01:58 +0000455 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000456 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000457 break;
Steve Naroff62f09f52008-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 Lattner461a6c52008-03-08 08:34:58 +0000464 case Type::Pointer: {
465 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000466 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000467 Align = Target.getPointerAlign(AS);
468 break;
469 }
Sebastian Redlce6fff02009-03-16 23:22:08 +0000470 case Type::LValueReference:
471 case Type::RValueReference:
Chris Lattner4b009652007-07-25 00:24:17 +0000472 // "When applied to a reference or a reference type, the result is the size
473 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000474 // FIXME: This is wrong for struct layout: a reference in a struct has
475 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000476 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl75555032009-01-24 21:16:55 +0000477 case Type::MemberPointer: {
Sebastian Redl18cffee2009-01-24 23:29:36 +0000478 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redl75555032009-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 Redl18cffee2009-01-24 23:29:36 +0000481 // other compilers too, we need to delegate this completely to TargetInfo
482 // or some ABI abstraction layer.
Sebastian Redl75555032009-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 Lattner4b009652007-07-25 00:24:17 +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 Lattner8cd0e932008-03-05 18:54:05 +0000495 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000496 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000497 Align = EltInfo.second;
498 break;
499 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000500 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000501 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000502 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
503 Width = Layout.getSize();
504 Align = Layout.getAlignment();
505 break;
506 }
Douglas Gregor4fa58902009-02-26 23:50:07 +0000507 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000508 case Type::Enum: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000509 const TagType *TT = cast<TagType>(T);
510
511 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000512 Width = 1;
513 Align = 1;
514 break;
515 }
516
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000517 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000518 return getTypeInfo(ET->getDecl()->getIntegerType());
519
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000520 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000521 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
522 Width = Layout.getSize();
523 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000524 break;
525 }
Douglas Gregordd13e842009-03-30 22:58:21 +0000526
Douglas Gregorab380272009-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 Gregordd13e842009-03-30 22:58:21 +0000534 break;
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000535 }
Douglas Gregorab380272009-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 Lattner4b009652007-07-25 00:24:17 +0000555
556 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000557 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000558}
559
Chris Lattner83165b52009-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 Dunbarc61a8002009-02-18 19:59:32 +0000568 if (T->isSpecificBuiltinType(BuiltinType::Double))
569 return std::max(ABIAlign, 64U);
Chris Lattner83165b52009-01-27 18:08:34 +0000570
571 return ABIAlign;
572}
573
574
Devang Patelbfe323c2008-06-04 21:22:16 +0000575/// LayoutField - Field layout.
576void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000577 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000578 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000579 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000580 uint64_t FieldOffset = IsUnion ? 0 : Size;
581 uint64_t FieldSize;
582 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-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 Patelbfe323c2008-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 Friedman5255e7a2009-04-26 19:19:15 +0000592 FieldSize = BitWidthExpr->EvaluateAsInt(Context).getZExtValue();
Devang Patelbfe323c2008-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 Dunbar2cb762f2008-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 Patelbfe323c2008-06-04 21:22:16 +0000603 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000604 if (FieldPacking)
605 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-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 Lattnerfd799692008-08-09 21:35:13 +0000618 if (FD->getType()->isIncompleteArrayType()) {
619 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-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 Lattnera1923f62008-08-04 07:31:14 +0000624 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000625 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson0843ea52009-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 Patelbfe323c2008-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 Dunbar2cb762f2008-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 Patelbfe323c2008-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 Dunbar5523e862009-05-04 05:16:21 +0000663 // Remember the next available offset.
664 NextOffset = Size;
665
Devang Patelbfe323c2008-06-04 21:22:16 +0000666 // Remember max struct/class alignment.
667 Alignment = std::max(Alignment, FieldAlign);
668}
669
Daniel Dunbar1af336e2009-04-22 17:43:55 +0000670static void CollectLocalObjCIvars(ASTContext *Ctx,
671 const ObjCInterfaceDecl *OI,
672 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000673 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
674 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner9329cf52009-03-31 08:48:01 +0000675 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000676 if (!IVDecl->isInvalidDecl())
677 Fields.push_back(cast<FieldDecl>(IVDecl));
678 }
679}
680
Daniel Dunbar1af336e2009-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 Dunbar1fbaef12009-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 Patel4b6bf702008-06-04 21:54:36 +0000693const ASTRecordLayout &
Daniel Dunbar1fbaef12009-05-03 10:38:35 +0000694ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
695 const ObjCImplementationDecl *Impl) {
Daniel Dunbar94d2ede2009-05-03 13:15:50 +0000696 assert(!D->isForwardDecl() && "Invalid interface decl!");
697
Devang Patel4b6bf702008-06-04 21:54:36 +0000698 // Look up this layout, if already laid out, return what we have.
Daniel Dunbarb3170af2009-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 Patel4b6bf702008-06-04 21:54:36 +0000703
Daniel Dunbar5b9332f2009-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 Dunbarb3170af2009-05-03 11:41:43 +0000712 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar5b9332f2009-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 Patel8682d882008-06-06 02:14:01 +0000720 ASTRecordLayout *NewEntry = NULL;
Devang Patel8682d882008-06-06 02:14:01 +0000721 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Devang Patel8682d882008-06-06 02:14:01 +0000722 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
723 unsigned Alignment = SL.getAlignment();
Daniel Dunbarb3170af2009-05-03 11:41:43 +0000724
Daniel Dunbarb5dc2942009-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 Dunbar5523e862009-05-04 05:16:21 +0000728
Daniel Dunbarb3170af2009-05-03 11:41:43 +0000729 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout(Size, Alignment);
Devang Patel8682d882008-06-06 02:14:01 +0000730 NewEntry->InitializeLayout(FieldCount);
Devang Patel8682d882008-06-06 02:14:01 +0000731 } else {
Daniel Dunbarb3170af2009-05-03 11:41:43 +0000732 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout();
Devang Patel8682d882008-06-06 02:14:01 +0000733 NewEntry->InitializeLayout(FieldCount);
734 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000735
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000736 unsigned StructPacking = 0;
737 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
738 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-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 Dunbar2cb762f2008-10-16 02:34:03 +0000749 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000750 }
Daniel Dunbar5b9332f2009-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 Jahanianfbf44642009-03-31 18:11:23 +0000758 }
Fariborz Jahanian84c45692009-04-01 19:37:34 +0000759
Devang Patel4b6bf702008-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 Dunbar1fbaef12009-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 Patel7a78e432007-11-01 19:11:01 +0000776/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000777/// specified record (struct/union/class), which indicates its size and field
778/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000779const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000780 D = D->getDefinition(*this);
781 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000782
Chris Lattner4b009652007-07-25 00:24:17 +0000783 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000784 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000785 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000786
Devang Patel7a78e432007-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 Lattner4b009652007-07-25 00:24:17 +0000790 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000791
Douglas Gregor39677622008-12-11 20:41:00 +0000792 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000793 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
794 D->field_end(*this)));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000795 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000796
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000797 unsigned StructPacking = 0;
798 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
799 StructPacking = PA->getAlignment();
800
Eli Friedman5949a022008-05-30 09:31:38 +0000801 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000802 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
803 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000804
Eli Friedman5949a022008-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 Gregor8acb7272008-12-11 16:49:14 +0000807 unsigned FieldIdx = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000808 for (RecordDecl::field_iterator Field = D->field_begin(*this),
809 FieldEnd = D->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000810 Field != FieldEnd; (void)++Field, ++FieldIdx)
811 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-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 Patelbfe323c2008-06-04 21:22:16 +0000815 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000816 return *NewEntry;
817}
818
Chris Lattner4b009652007-07-25 00:24:17 +0000819//===----------------------------------------------------------------------===//
820// Type creation/memoization methods
821//===----------------------------------------------------------------------===//
822
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000823QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000824 QualType CanT = getCanonicalType(T);
825 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000826 return T;
Chris Lattner18b5a9a2009-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 Lattner35fef522008-02-20 20:55:12 +0000833
Chris Lattner18b5a9a2009-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 Lattner35fef522008-02-20 20:55:12 +0000842
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000843 // Check if we've already instantiated this type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000844 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000845 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000846 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000847 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000848 return QualType(EXTQy, CVRQuals);
849
Christopher Lamb2a72bb32008-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 Lattner18b5a9a2009-02-18 22:53:11 +0000853 if (!TypeNode->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000854 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000855
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000856 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000857 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000858 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000859 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000860 ExtQualType *New =
861 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000862 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000863 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000864 return QualType(New, CVRQuals);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000865}
866
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000867QualType ASTContext::getObjCGCQualType(QualType T,
868 QualType::GCAttrTypes GCAttr) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000869 QualType CanT = getCanonicalType(T);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000870 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000871 return T;
872
Chris Lattner18b5a9a2009-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 Jahanianaf238092009-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 Lattner18b5a9a2009-02-18 22:53:11 +0000890 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000891 void *InsertPos = 0;
892 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000893 return QualType(EXTQy, CVRQuals);
Fariborz Jahanianaf238092009-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 Friedman94fcc9a2009-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 Jahanianaf238092009-02-18 05:09:49 +0000899 QualType Canonical;
900 if (!T->isCanonical()) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000901 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000902
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000903 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianaf238092009-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 Lattner18b5a9a2009-02-18 22:53:11 +0000907 ExtQualType *New =
908 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000909 ExtQualTypes.InsertNode(New, InsertPos);
910 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000911 return QualType(New, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000912}
Chris Lattner4b009652007-07-25 00:24:17 +0000913
914/// 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 Lattnerc1b68db2008-04-06 22:59:24 +0000930 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000931
932 // Get the new insert position for the node we care about.
933 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000934 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000935 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000936 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000937 Types.push_back(New);
938 ComplexTypes.InsertNode(New, InsertPos);
939 return QualType(New, 0);
940}
941
Eli Friedmanff3fcdf2009-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}
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerc1b68db2008-04-06 22:59:24 +0000967 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000968
969 // Get the new insert position for the node we care about.
970 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000971 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000972 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000973 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000974 Types.push_back(New);
975 PointerTypes.InsertNode(New, InsertPos);
976 return QualType(New, 0);
977}
978
Steve Naroff7aa54752008-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 Narofffd5b19d2008-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 Naroff7aa54752008-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 Narofffd5b19d2008-08-28 19:20:44 +0000993 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-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 Lattner578a37e2008-10-12 00:26:57 +00001002 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +00001003 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001004 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +00001005 Types.push_back(New);
1006 BlockPointerTypes.InsertNode(New, InsertPos);
1007 return QualType(New, 0);
1008}
1009
Sebastian Redlce6fff02009-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) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Redlce6fff02009-03-16 23:22:08 +00001019 if (LValueReferenceType *RT =
1020 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001021 return QualType(RT, 0);
Sebastian Redlce6fff02009-03-16 23:22:08 +00001022
Chris Lattner4b009652007-07-25 00:24:17 +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 Redlce6fff02009-03-16 23:22:08 +00001027 Canonical = getLValueReferenceType(getCanonicalType(T));
1028
Chris Lattner4b009652007-07-25 00:24:17 +00001029 // Get the new insert position for the node we care about.
Sebastian Redlce6fff02009-03-16 23:22:08 +00001030 LValueReferenceType *NewIP =
1031 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001032 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001033 }
1034
Sebastian Redlce6fff02009-03-16 23:22:08 +00001035 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001036 Types.push_back(New);
Sebastian Redlce6fff02009-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);
Chris Lattner4b009652007-07-25 00:24:17 +00001069 return QualType(New, 0);
1070}
1071
Sebastian Redl75555032009-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 Naroff93fd2112009-01-27 22:08:43 +00001097 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +00001098 Types.push_back(New);
1099 MemberPointerTypes.InsertNode(New, InsertPos);
1100 return QualType(New, 0);
1101}
1102
Steve Naroff83c13012007-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 Naroff24c9b982007-08-30 18:10:14 +00001106 const llvm::APInt &ArySize,
1107 ArrayType::ArraySizeModifier ASM,
1108 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001109 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001110 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001111
1112 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +00001113 if (ConstantArrayType *ATP =
1114 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerc1b68db2008-04-06 22:59:24 +00001121 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +00001122 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001123 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +00001124 ConstantArrayType *NewIP =
1125 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001126 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001127 }
1128
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001129 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001130 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001131 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001132 Types.push_back(New);
1133 return QualType(New, 0);
1134}
1135
Steve Naroffe2579e32007-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 Naroff24c9b982007-08-30 18:10:14 +00001138QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1139 ArrayType::ArraySizeModifier ASM,
1140 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-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 Kremenekc70e7d02009-01-19 21:31:22 +00001144 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001145 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001146
1147 VariableArrayTypes.push_back(New);
1148 Types.push_back(New);
1149 return QualType(New, 0);
1150}
1151
Douglas Gregor1b21c7f2008-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 Kremenekc70e7d02009-01-19 21:31:22 +00001165 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001166 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1167 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001168
1169 DependentSizedArrayTypes.push_back(New);
1170 Types.push_back(New);
1171 return QualType(New, 0);
1172}
1173
Eli Friedman8ff07782008-02-15 18:16:39 +00001174QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1175 ArrayType::ArraySizeModifier ASM,
1176 unsigned EltTypeQuals) {
1177 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001178 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedman8ff07782008-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 Lattnerc1b68db2008-04-06 22:59:24 +00001190 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001191 ASM, EltTypeQuals);
Eli Friedman8ff07782008-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 Lattner578a37e2008-10-12 00:26:57 +00001196 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001197 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001198
Steve Naroff93fd2112009-01-27 22:08:43 +00001199 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001200 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001201
1202 IncompleteArrayTypes.InsertNode(New, InsertPos);
1203 Types.push_back(New);
1204 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001205}
1206
Chris Lattner4b009652007-07-25 00:24:17 +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) {
1210 BuiltinType *baseType;
1211
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001212 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001213 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1214
1215 // Check if we've already instantiated a vector of this type.
1216 llvm::FoldingSetNodeID ID;
1217 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1218 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 Lattnerc1b68db2008-04-06 22:59:24 +00001226 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001227
1228 // Get the new insert position for the node we care about.
1229 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001230 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001231 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001232 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001233 VectorTypes.InsertNode(New, InsertPos);
1234 Types.push_back(New);
1235 return QualType(New, 0);
1236}
1237
Nate Begemanaf6ed502008-04-18 23:10:10 +00001238/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001239/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001240QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001241 BuiltinType *baseType;
1242
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001243 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001244 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001245
1246 // Check if we've already instantiated a vector of this type.
1247 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001248 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +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 Begemanaf6ed502008-04-18 23:10:10 +00001257 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001258
1259 // Get the new insert position for the node we care about.
1260 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001261 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001262 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001263 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001264 VectorTypes.InsertNode(New, InsertPos);
1265 Types.push_back(New);
1266 return QualType(New, 0);
1267}
1268
Douglas Gregor4fa58902009-02-26 23:50:07 +00001269/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001270///
Douglas Gregor4fa58902009-02-26 23:50:07 +00001271QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Chris Lattner4b009652007-07-25 00:24:17 +00001272 // Unique functions, to guarantee there is only one function of a particular
1273 // structure.
1274 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001275 FunctionNoProtoType::Profile(ID, ResultTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001276
1277 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001278 if (FunctionNoProtoType *FT =
1279 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001280 return QualType(FT, 0);
1281
1282 QualType Canonical;
1283 if (!ResultTy->isCanonical()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00001284 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001285
1286 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001287 FunctionNoProtoType *NewIP =
1288 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001289 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001290 }
1291
Douglas Gregor4fa58902009-02-26 23:50:07 +00001292 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001293 Types.push_back(New);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001294 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner2fda0ed2008-10-05 17:34:18 +00001300QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001301 unsigned NumArgs, bool isVariadic,
Sebastian Redlba9a3712009-05-06 23:27:55 +00001302 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001303 // Unique functions, to guarantee there is only one function of a particular
1304 // structure.
1305 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001306 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redlba9a3712009-05-06 23:27:55 +00001307 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001308
1309 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001310 if (FunctionProtoType *FTP =
1311 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerc1b68db2008-04-06 22:59:24 +00001326 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redlba9a3712009-05-06 23:27:55 +00001327
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001328 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001329 &CanonicalArgs[0], NumArgs,
Sebastian Redlba9a3712009-05-06 23:27:55 +00001330 isVariadic, TypeQuals);
1331
Chris Lattner4b009652007-07-25 00:24:17 +00001332 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001333 FunctionProtoType *NewIP =
1334 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001335 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001336 }
Sebastian Redlba9a3712009-05-06 23:27:55 +00001337
Douglas Gregor4fa58902009-02-26 23:50:07 +00001338 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redlba9a3712009-05-06 23:27:55 +00001339 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001340 FunctionProtoType *FTP =
Sebastian Redlba9a3712009-05-06 23:27:55 +00001341 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1342 NumArgs*sizeof(QualType), 8);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001343 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redlba9a3712009-05-06 23:27:55 +00001344 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001345 Types.push_back(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001346 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001347 return QualType(FTP, 0);
1348}
1349
Douglas Gregor1d661552008-04-13 21:07:44 +00001350/// getTypeDeclType - Return the unique reference to the type for the
1351/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001352QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001353 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001354 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1355
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001356 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001357 return getTypedefType(Typedef);
Douglas Gregora4918772009-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 Gregor1d661552008-04-13 21:07:44 +00001361 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001362
Douglas Gregor2e047592009-02-28 01:32:25 +00001363 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001364 if (PrevDecl)
1365 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001366 else
1367 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001368 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001369 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1370 if (PrevDecl)
1371 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001372 else
1373 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001374 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001375 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001376 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001377
Ted Kremenek46a837c2008-09-05 17:16:31 +00001378 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001379 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001380}
1381
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerc1b68db2008-04-06 22:59:24 +00001387 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001388 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001389 Types.push_back(Decl->TypeForDecl);
1390 return QualType(Decl->TypeForDecl, 0);
1391}
1392
Ted Kremenek42730c52008-01-07 19:49:32 +00001393/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001394/// specified ObjC interface decl.
Daniel Dunbarbe1ff272009-04-22 04:34:53 +00001395QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001396 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1397
Daniel Dunbarbe1ff272009-04-22 04:34:53 +00001398 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1399 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001400 Types.push_back(Decl->TypeForDecl);
1401 return QualType(Decl->TypeForDecl, 0);
1402}
1403
Douglas Gregora4918772009-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 Gregor8e458f42009-02-09 18:46:07 +00001429QualType
Douglas Gregordd13e842009-03-30 22:58:21 +00001430ASTContext::getTemplateSpecializationType(TemplateName Template,
1431 const TemplateArgument *Args,
1432 unsigned NumArgs,
1433 QualType Canon) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001434 if (!Canon.isNull())
1435 Canon = getCanonicalType(Canon);
Douglas Gregor9c7825b2009-02-26 22:19:44 +00001436
Douglas Gregor8e458f42009-02-09 18:46:07 +00001437 llvm::FoldingSetNodeID ID;
Douglas Gregordd13e842009-03-30 22:58:21 +00001438 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001439
Douglas Gregor8e458f42009-02-09 18:46:07 +00001440 void *InsertPos = 0;
Douglas Gregordd13e842009-03-30 22:58:21 +00001441 TemplateSpecializationType *Spec
1442 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001443
1444 if (Spec)
1445 return QualType(Spec, 0);
1446
Douglas Gregordd13e842009-03-30 22:58:21 +00001447 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001448 sizeof(TemplateArgument) * NumArgs),
1449 8);
Douglas Gregordd13e842009-03-30 22:58:21 +00001450 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001451 Types.push_back(Spec);
Douglas Gregordd13e842009-03-30 22:58:21 +00001452 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001453
1454 return QualType(Spec, 0);
1455}
1456
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001457QualType
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001458ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001459 QualType NamedType) {
1460 llvm::FoldingSetNodeID ID;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001461 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor734b4ba2009-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 Gregor1e589cc2009-03-26 23:50:42 +00001469 T = new (*this) QualifiedNameType(NNS, NamedType,
1470 getCanonicalType(NamedType));
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001471 Types.push_back(T);
1472 QualifiedNameTypes.InsertNode(T, InsertPos);
1473 return QualType(T, 0);
1474}
1475
Douglas Gregord3022602009-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 Gregor77da5802009-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 Lattnere1352302008-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 Gregor24afd4a2008-11-17 14:58:09 +00001539 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-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 Lattnerb0c6a1f2008-04-07 04:44:08 +00001555/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1556/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001557QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1558 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001559 // Sort the protocol list alphabetically to canonicalize it.
1560 SortAndUniqueProtocols(Protocols, NumProtocols);
1561
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001562 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001563 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001564
1565 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001566 if (ObjCQualifiedInterfaceType *QT =
1567 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001568 return QualType(QT, 0);
1569
1570 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001571 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001572 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001573
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001574 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001575 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001576 return QualType(QType, 0);
1577}
1578
Chris Lattnere1352302008-04-07 04:56:42 +00001579/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1580/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001581QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001582 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001583 // Sort the protocol list alphabetically to canonicalize it.
1584 SortAndUniqueProtocols(Protocols, NumProtocols);
1585
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001586 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001587 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001588
1589 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001590 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001591 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001592 return QualType(QT, 0);
1593
1594 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001595 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001596 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001597 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001598 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001599 return QualType(QType, 0);
1600}
1601
Douglas Gregor4fa58902009-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 Naroff0604dd92007-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 Gregor4fa58902009-02-26 23:50:07 +00001607QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001608 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001609 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001610 Types.push_back(toe);
1611 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001612}
1613
Steve Naroff0604dd92007-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 Naroff7cbb1462007-07-31 12:34:36 +00001619QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001620 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001621 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001622 Types.push_back(tot);
1623 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001624}
1625
Chris Lattner4b009652007-07-25 00:24:17 +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 Kremenekae8fa032007-11-26 21:16:01 +00001629 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001630 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +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 Gregorc6507e42008-11-03 14:12:49 +00001637 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001638}
1639
Argiris Kirtzidis1ed03e72008-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 Lattner4b009652007-07-25 00:24:17 +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 Gregorc6507e42008-11-03 14:12:49 +00001657 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001658}
1659
Chris Lattner19eb97e2008-04-02 05:18:44 +00001660//===----------------------------------------------------------------------===//
1661// Type Operators
1662//===----------------------------------------------------------------------===//
1663
Chris Lattner3dae6f42008-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 Lattnera1923f62008-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 Gregor1b21c7f2008-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 Lattnera1923f62008-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 Gregor9054f982009-05-10 22:57:19 +00001706Decl *ASTContext::getCanonicalDecl(Decl *D) {
Douglas Gregordf3e9572009-05-10 22:59:12 +00001707 if (!D)
1708 return 0;
1709
Douglas Gregor9054f982009-05-10 22:57:19 +00001710 if (TagDecl *Tag = dyn_cast<TagDecl>(D)) {
1711 QualType T = getTagDeclType(Tag);
1712 return cast<TagDecl>(cast<TagType>(T.getTypePtr()->CanonicalType)
1713 ->getDecl());
1714 }
1715
1716 if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(D)) {
1717 while (Template->getPreviousDeclaration())
1718 Template = Template->getPreviousDeclaration();
1719 return Template;
1720 }
1721
1722 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
1723 while (Function->getPreviousDeclaration())
1724 Function = Function->getPreviousDeclaration();
1725 return const_cast<FunctionDecl *>(Function);
1726 }
1727
1728 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
1729 while (Var->getPreviousDeclaration())
1730 Var = Var->getPreviousDeclaration();
1731 return const_cast<VarDecl *>(Var);
1732 }
1733
1734 return D;
1735}
1736
Douglas Gregorb88ba412009-05-07 06:41:52 +00001737TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
1738 // If this template name refers to a template, the canonical
1739 // template name merely stores the template itself.
1740 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Douglas Gregor9054f982009-05-10 22:57:19 +00001741 return TemplateName(cast<TemplateDecl>(getCanonicalDecl(Template)));
Douglas Gregorb88ba412009-05-07 06:41:52 +00001742
1743 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
1744 assert(DTN && "Non-dependent template names must refer to template decls.");
1745 return DTN->CanonicalTemplateName;
1746}
1747
Douglas Gregord3022602009-03-27 23:10:48 +00001748NestedNameSpecifier *
1749ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1750 if (!NNS)
1751 return 0;
1752
1753 switch (NNS->getKind()) {
1754 case NestedNameSpecifier::Identifier:
1755 // Canonicalize the prefix but keep the identifier the same.
1756 return NestedNameSpecifier::Create(*this,
1757 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1758 NNS->getAsIdentifier());
1759
1760 case NestedNameSpecifier::Namespace:
1761 // A namespace is canonical; build a nested-name-specifier with
1762 // this namespace and no prefix.
1763 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1764
1765 case NestedNameSpecifier::TypeSpec:
1766 case NestedNameSpecifier::TypeSpecWithTemplate: {
1767 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1768 NestedNameSpecifier *Prefix = 0;
1769
1770 // FIXME: This isn't the right check!
1771 if (T->isDependentType())
1772 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1773
1774 return NestedNameSpecifier::Create(*this, Prefix,
1775 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1776 T.getTypePtr());
1777 }
1778
1779 case NestedNameSpecifier::Global:
1780 // The global specifier is canonical and unique.
1781 return NNS;
1782 }
1783
1784 // Required to silence a GCC warning
1785 return 0;
1786}
1787
Chris Lattnera1923f62008-08-04 07:31:14 +00001788
1789const ArrayType *ASTContext::getAsArrayType(QualType T) {
1790 // Handle the non-qualified case efficiently.
1791 if (T.getCVRQualifiers() == 0) {
1792 // Handle the common positive case fast.
1793 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1794 return AT;
1795 }
1796
1797 // Handle the common negative case fast, ignoring CVR qualifiers.
1798 QualType CType = T->getCanonicalTypeInternal();
1799
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001800 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001801 // test.
1802 if (!isa<ArrayType>(CType) &&
1803 !isa<ArrayType>(CType.getUnqualifiedType()))
1804 return 0;
1805
1806 // Apply any CVR qualifiers from the array type to the element type. This
1807 // implements C99 6.7.3p8: "If the specification of an array type includes
1808 // any type qualifiers, the element type is so qualified, not the array type."
1809
1810 // If we get here, we either have type qualifiers on the type, or we have
1811 // sugar such as a typedef in the way. If we have type qualifiers on the type
1812 // we must propagate them down into the elemeng type.
1813 unsigned CVRQuals = T.getCVRQualifiers();
1814 unsigned AddrSpace = 0;
1815 Type *Ty = T.getTypePtr();
1816
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001817 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001818 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001819 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1820 AddrSpace = EXTQT->getAddressSpace();
1821 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001822 } else {
1823 T = Ty->getDesugaredType();
1824 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1825 break;
1826 CVRQuals |= T.getCVRQualifiers();
1827 Ty = T.getTypePtr();
1828 }
1829 }
1830
1831 // If we have a simple case, just return now.
1832 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1833 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1834 return ATy;
1835
1836 // Otherwise, we have an array and we have qualifiers on it. Push the
1837 // qualifiers into the array element type and return a new array type.
1838 // Get the canonical version of the element with the extra qualifiers on it.
1839 // This can recursively sink qualifiers through multiple levels of arrays.
1840 QualType NewEltTy = ATy->getElementType();
1841 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001842 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001843 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1844
1845 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1846 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1847 CAT->getSizeModifier(),
1848 CAT->getIndexTypeQualifier()));
1849 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1850 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1851 IAT->getSizeModifier(),
1852 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001853
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001854 if (const DependentSizedArrayType *DSAT
1855 = dyn_cast<DependentSizedArrayType>(ATy))
1856 return cast<ArrayType>(
1857 getDependentSizedArrayType(NewEltTy,
1858 DSAT->getSizeExpr(),
1859 DSAT->getSizeModifier(),
1860 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001861
Chris Lattnera1923f62008-08-04 07:31:14 +00001862 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1863 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1864 VAT->getSizeModifier(),
1865 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001866}
1867
1868
Chris Lattner19eb97e2008-04-02 05:18:44 +00001869/// getArrayDecayedType - Return the properly qualified result of decaying the
1870/// specified array type to a pointer. This operation is non-trivial when
1871/// handling typedefs etc. The canonical type of "T" must be an array type,
1872/// this returns a pointer to a properly qualified element of the array.
1873///
1874/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1875QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001876 // Get the element type with 'getAsArrayType' so that we don't lose any
1877 // typedefs in the element type of the array. This also handles propagation
1878 // of type qualifiers from the array type into the element type if present
1879 // (C99 6.7.3p8).
1880 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1881 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001882
Chris Lattnera1923f62008-08-04 07:31:14 +00001883 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001884
1885 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001886 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001887}
1888
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001889QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001890 QualType ElemTy = VAT->getElementType();
1891
1892 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1893 return getBaseElementType(VAT);
1894
1895 return ElemTy;
1896}
1897
Chris Lattner4b009652007-07-25 00:24:17 +00001898/// getFloatingRank - Return a relative rank for floating point types.
1899/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001900static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001901 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001902 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001903
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001904 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001905 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001906 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001907 case BuiltinType::Float: return FloatRank;
1908 case BuiltinType::Double: return DoubleRank;
1909 case BuiltinType::LongDouble: return LongDoubleRank;
1910 }
1911}
1912
Steve Narofffa0c4532007-08-27 01:41:48 +00001913/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1914/// point or a complex type (based on typeDomain/typeSize).
1915/// 'typeDomain' is a real floating point or complex type.
1916/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001917QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1918 QualType Domain) const {
1919 FloatingRank EltRank = getFloatingRank(Size);
1920 if (Domain->isComplexType()) {
1921 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001922 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001923 case FloatRank: return FloatComplexTy;
1924 case DoubleRank: return DoubleComplexTy;
1925 case LongDoubleRank: return LongDoubleComplexTy;
1926 }
Chris Lattner4b009652007-07-25 00:24:17 +00001927 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001928
1929 assert(Domain->isRealFloatingType() && "Unknown domain!");
1930 switch (EltRank) {
1931 default: assert(0 && "getFloatingRank(): illegal value for rank");
1932 case FloatRank: return FloatTy;
1933 case DoubleRank: return DoubleTy;
1934 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001935 }
Chris Lattner4b009652007-07-25 00:24:17 +00001936}
1937
Chris Lattner51285d82008-04-06 23:55:33 +00001938/// getFloatingTypeOrder - Compare the rank of the two specified floating
1939/// point types, ignoring the domain of the type (i.e. 'double' ==
1940/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1941/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001942int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1943 FloatingRank LHSR = getFloatingRank(LHS);
1944 FloatingRank RHSR = getFloatingRank(RHS);
1945
1946 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001947 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001948 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001949 return 1;
1950 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001951}
1952
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001953/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1954/// routine will assert if passed a built-in type that isn't an integer or enum,
1955/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001956unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001957 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001958 if (EnumType* ET = dyn_cast<EnumType>(T))
1959 T = ET->getDecl()->getIntegerType().getTypePtr();
1960
1961 // There are two things which impact the integer rank: the width, and
1962 // the ordering of builtins. The builtin ordering is encoded in the
1963 // bottom three bits; the width is encoded in the bits above that.
1964 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1965 return FWIT->getWidth() << 3;
1966 }
1967
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001968 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001969 default: assert(0 && "getIntegerRank(): not a built-in integer");
1970 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001971 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001972 case BuiltinType::Char_S:
1973 case BuiltinType::Char_U:
1974 case BuiltinType::SChar:
1975 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001976 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001977 case BuiltinType::Short:
1978 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001979 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001980 case BuiltinType::Int:
1981 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001982 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001983 case BuiltinType::Long:
1984 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001985 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001986 case BuiltinType::LongLong:
1987 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001988 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner6cc7e412009-04-30 02:43:43 +00001989 case BuiltinType::Int128:
1990 case BuiltinType::UInt128:
1991 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001992 }
1993}
1994
Chris Lattner51285d82008-04-06 23:55:33 +00001995/// getIntegerTypeOrder - Returns the highest ranked integer type:
1996/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1997/// LHS < RHS, return -1.
1998int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001999 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2000 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00002001 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00002002
Chris Lattnerc1b68db2008-04-06 22:59:24 +00002003 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2004 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00002005
Chris Lattner51285d82008-04-06 23:55:33 +00002006 unsigned LHSRank = getIntegerRank(LHSC);
2007 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00002008
Chris Lattner51285d82008-04-06 23:55:33 +00002009 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2010 if (LHSRank == RHSRank) return 0;
2011 return LHSRank > RHSRank ? 1 : -1;
2012 }
Chris Lattner4b009652007-07-25 00:24:17 +00002013
Chris Lattner51285d82008-04-06 23:55:33 +00002014 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2015 if (LHSUnsigned) {
2016 // If the unsigned [LHS] type is larger, return it.
2017 if (LHSRank >= RHSRank)
2018 return 1;
2019
2020 // If the signed type can represent all values of the unsigned type, it
2021 // wins. Because we are dealing with 2's complement and types that are
2022 // powers of two larger than each other, this is always safe.
2023 return -1;
2024 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00002025
Chris Lattner51285d82008-04-06 23:55:33 +00002026 // If the unsigned [RHS] type is larger, return it.
2027 if (RHSRank >= LHSRank)
2028 return -1;
2029
2030 // If the signed type can represent all values of the unsigned type, it
2031 // wins. Because we are dealing with 2's complement and types that are
2032 // powers of two larger than each other, this is always safe.
2033 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00002034}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002035
2036// getCFConstantStringType - Return the type used for constant CFStrings.
2037QualType ASTContext::getCFConstantStringType() {
2038 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00002039 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00002040 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00002041 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002042 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002043
2044 // const int *isa;
2045 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002046 // int flags;
2047 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002048 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002049 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002050 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002051 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00002052
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002053 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00002054 for (unsigned i = 0; i < 4; ++i) {
2055 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2056 SourceLocation(), 0,
2057 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002058 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002059 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002060 }
2061
2062 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002063 }
2064
2065 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00002066}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002067
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002068void ASTContext::setCFConstantStringType(QualType T) {
2069 const RecordType *Rec = T->getAsRecordType();
2070 assert(Rec && "Invalid CFConstantStringType");
2071 CFConstantStringTypeDecl = Rec->getDecl();
2072}
2073
Anders Carlssonf58cac72008-08-30 19:34:46 +00002074QualType ASTContext::getObjCFastEnumerationStateType()
2075{
2076 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00002077 ObjCFastEnumerationStateTypeDecl =
2078 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2079 &Idents.get("__objcFastEnumerationState"));
2080
Anders Carlssonf58cac72008-08-30 19:34:46 +00002081 QualType FieldTypes[] = {
2082 UnsignedLongTy,
2083 getPointerType(ObjCIdType),
2084 getPointerType(UnsignedLongTy),
2085 getConstantArrayType(UnsignedLongTy,
2086 llvm::APInt(32, 5), ArrayType::Normal, 0)
2087 };
2088
Douglas Gregor8acb7272008-12-11 16:49:14 +00002089 for (size_t i = 0; i < 4; ++i) {
2090 FieldDecl *Field = FieldDecl::Create(*this,
2091 ObjCFastEnumerationStateTypeDecl,
2092 SourceLocation(), 0,
2093 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002094 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002095 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002096 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00002097
Douglas Gregor8acb7272008-12-11 16:49:14 +00002098 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00002099 }
2100
2101 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2102}
2103
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002104void ASTContext::setObjCFastEnumerationStateType(QualType T) {
2105 const RecordType *Rec = T->getAsRecordType();
2106 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2107 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2108}
2109
Anders Carlssone3f02572007-10-29 06:33:42 +00002110// This returns true if a type has been typedefed to BOOL:
2111// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00002112static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002113 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00002114 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2115 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002116
2117 return false;
2118}
2119
Ted Kremenek42730c52008-01-07 19:49:32 +00002120/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002121/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00002122int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00002123 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002124
2125 // Make all integer and enum types at least as large as an int
2126 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002127 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002128 // Treat arrays as pointers, since that's how they're passed in.
2129 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002130 sz = getTypeSize(VoidPtrTy);
2131 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002132}
2133
Ted Kremenek42730c52008-01-07 19:49:32 +00002134/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002135/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002136void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00002137 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002138 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002139 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00002140 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002141 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002142 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002143 // Compute size of all parameters.
2144 // Start with computing size of a pointer in number of bytes.
2145 // FIXME: There might(should) be a better way of doing this computation!
2146 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00002147 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002148 // The first two arguments (self and _cmd) are pointers; account for
2149 // their size.
2150 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002151 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2152 E = Decl->param_end(); PI != E; ++PI) {
2153 QualType PType = (*PI)->getType();
2154 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00002155 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002156 ParmOffset += sz;
2157 }
2158 S += llvm::utostr(ParmOffset);
2159 S += "@0:";
2160 S += llvm::utostr(PtrSize);
2161
2162 // Argument types.
2163 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002164 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2165 E = Decl->param_end(); PI != E; ++PI) {
2166 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002167 QualType PType = PVDecl->getOriginalType();
2168 if (const ArrayType *AT =
Steve Naroff78380fb2009-04-14 00:03:58 +00002169 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2170 // Use array's original type only if it has known number of
2171 // elements.
Steve Naroff6777bf32009-04-14 00:40:09 +00002172 if (!isa<ConstantArrayType>(AT))
Steve Naroff78380fb2009-04-14 00:03:58 +00002173 PType = PVDecl->getType();
2174 } else if (PType->isFunctionType())
2175 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002176 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002177 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002178 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002179 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002180 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00002181 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002182 }
2183}
2184
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002185/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002186/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002187/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2188/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002189/// Property attributes are stored as a comma-delimited C string. The simple
2190/// attributes readonly and bycopy are encoded as single characters. The
2191/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2192/// encoded as single characters, followed by an identifier. Property types
2193/// are also encoded as a parametrized attribute. The characters used to encode
2194/// these attributes are defined by the following enumeration:
2195/// @code
2196/// enum PropertyAttributes {
2197/// kPropertyReadOnly = 'R', // property is read-only.
2198/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2199/// kPropertyByref = '&', // property is a reference to the value last assigned
2200/// kPropertyDynamic = 'D', // property is dynamic
2201/// kPropertyGetter = 'G', // followed by getter selector name
2202/// kPropertySetter = 'S', // followed by setter selector name
2203/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2204/// kPropertyType = 't' // followed by old-style type encoding.
2205/// kPropertyWeak = 'W' // 'weak' property
2206/// kPropertyStrong = 'P' // property GC'able
2207/// kPropertyNonAtomic = 'N' // property non-atomic
2208/// };
2209/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002210void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2211 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00002212 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002213 // Collect information from the property implementation decl(s).
2214 bool Dynamic = false;
2215 ObjCPropertyImplDecl *SynthesizePID = 0;
2216
2217 // FIXME: Duplicated code due to poor abstraction.
2218 if (Container) {
2219 if (const ObjCCategoryImplDecl *CID =
2220 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2221 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregorcd19b572009-04-23 01:02:12 +00002222 i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2223 i != e; ++i) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002224 ObjCPropertyImplDecl *PID = *i;
2225 if (PID->getPropertyDecl() == PD) {
2226 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2227 Dynamic = true;
2228 } else {
2229 SynthesizePID = PID;
2230 }
2231 }
2232 }
2233 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002234 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002235 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregorcd19b572009-04-23 01:02:12 +00002236 i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2237 i != e; ++i) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002238 ObjCPropertyImplDecl *PID = *i;
2239 if (PID->getPropertyDecl() == PD) {
2240 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2241 Dynamic = true;
2242 } else {
2243 SynthesizePID = PID;
2244 }
2245 }
2246 }
2247 }
2248 }
2249
2250 // FIXME: This is not very efficient.
2251 S = "T";
2252
2253 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002254 // GCC has some special rules regarding encoding of properties which
2255 // closely resembles encoding of ivars.
Daniel Dunbar701c8502009-04-20 06:37:24 +00002256 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002257 true /* outermost type */,
2258 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002259
2260 if (PD->isReadOnly()) {
2261 S += ",R";
2262 } else {
2263 switch (PD->getSetterKind()) {
2264 case ObjCPropertyDecl::Assign: break;
2265 case ObjCPropertyDecl::Copy: S += ",C"; break;
2266 case ObjCPropertyDecl::Retain: S += ",&"; break;
2267 }
2268 }
2269
2270 // It really isn't clear at all what this means, since properties
2271 // are "dynamic by default".
2272 if (Dynamic)
2273 S += ",D";
2274
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002275 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2276 S += ",N";
2277
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002278 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2279 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002280 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002281 }
2282
2283 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2284 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002285 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002286 }
2287
2288 if (SynthesizePID) {
2289 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2290 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002291 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002292 }
2293
2294 // FIXME: OBJCGC: weak & strong
2295}
2296
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002297/// getLegacyIntegralTypeEncoding -
2298/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002299/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002300/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2301///
2302void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2303 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2304 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002305 if (BT->getKind() == BuiltinType::ULong &&
2306 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002307 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002308 else
2309 if (BT->getKind() == BuiltinType::Long &&
2310 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002311 PointeeTy = IntTy;
2312 }
2313 }
2314}
2315
Fariborz Jahanian248db262008-01-22 22:44:46 +00002316void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002317 const FieldDecl *Field) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002318 // We follow the behavior of gcc, expanding structures which are
2319 // directly pointed to, and expanding embedded structures. Note that
2320 // these rules are sufficient to prevent recursive encoding of the
2321 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002322 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2323 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002324}
2325
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002326static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002327 const FieldDecl *FD) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002328 const Expr *E = FD->getBitWidth();
2329 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2330 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman5255e7a2009-04-26 19:19:15 +00002331 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002332 S += 'b';
2333 S += llvm::utostr(N);
2334}
2335
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002336void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2337 bool ExpandPointedToStructures,
2338 bool ExpandStructures,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002339 const FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002340 bool OutermostType,
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002341 bool EncodingProperty) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002342 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002343 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002344 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002345 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002346 else {
2347 char encoding;
2348 switch (BT->getKind()) {
2349 default: assert(0 && "Unhandled builtin type kind");
2350 case BuiltinType::Void: encoding = 'v'; break;
2351 case BuiltinType::Bool: encoding = 'B'; break;
2352 case BuiltinType::Char_U:
2353 case BuiltinType::UChar: encoding = 'C'; break;
2354 case BuiltinType::UShort: encoding = 'S'; break;
2355 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002356 case BuiltinType::ULong:
2357 encoding =
2358 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2359 break;
Chris Lattner6cc7e412009-04-30 02:43:43 +00002360 case BuiltinType::UInt128: encoding = 'T'; break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002361 case BuiltinType::ULongLong: encoding = 'Q'; break;
2362 case BuiltinType::Char_S:
2363 case BuiltinType::SChar: encoding = 'c'; break;
2364 case BuiltinType::Short: encoding = 's'; break;
2365 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002366 case BuiltinType::Long:
2367 encoding =
2368 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2369 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002370 case BuiltinType::LongLong: encoding = 'q'; break;
Chris Lattner6cc7e412009-04-30 02:43:43 +00002371 case BuiltinType::Int128: encoding = 't'; break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002372 case BuiltinType::Float: encoding = 'f'; break;
2373 case BuiltinType::Double: encoding = 'd'; break;
2374 case BuiltinType::LongDouble: encoding = 'd'; break;
2375 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002376
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002377 S += encoding;
2378 }
Anders Carlsson70e16dd2009-04-09 21:55:45 +00002379 } else if (const ComplexType *CT = T->getAsComplexType()) {
2380 S += 'j';
2381 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2382 false);
2383 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002384 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2385 ExpandPointedToStructures,
2386 ExpandStructures, FD);
2387 if (FD || EncodingProperty) {
2388 // Note that we do extended encoding of protocol qualifer list
2389 // Only when doing ivar or property encoding.
2390 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2391 S += '"';
2392 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2393 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2394 S += '<';
2395 S += Proto->getNameAsString();
2396 S += '>';
2397 }
2398 S += '"';
2399 }
2400 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002401 }
2402 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002403 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002404 bool isReadOnly = false;
2405 // For historical/compatibility reasons, the read-only qualifier of the
2406 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2407 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2408 // Also, do not emit the 'r' for anything but the outermost type!
2409 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2410 if (OutermostType && T.isConstQualified()) {
2411 isReadOnly = true;
2412 S += 'r';
2413 }
2414 }
2415 else if (OutermostType) {
2416 QualType P = PointeeTy;
2417 while (P->getAsPointerType())
2418 P = P->getAsPointerType()->getPointeeType();
2419 if (P.isConstQualified()) {
2420 isReadOnly = true;
2421 S += 'r';
2422 }
2423 }
2424 if (isReadOnly) {
2425 // Another legacy compatibility encoding. Some ObjC qualifier and type
2426 // combinations need to be rearranged.
2427 // Rewrite "in const" from "nr" to "rn"
2428 const char * s = S.c_str();
2429 int len = S.length();
2430 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2431 std::string replace = "rn";
2432 S.replace(S.end()-2, S.end(), replace);
2433 }
2434 }
Steve Naroff17c03822009-02-12 17:52:19 +00002435 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002436 S += '@';
2437 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002438 }
2439 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002440 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002441 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002442 // Another historical/compatibility reason.
2443 // We encode the underlying type which comes out as
2444 // {...};
2445 S += '^';
2446 getObjCEncodingForTypeImpl(PointeeTy, S,
2447 false, ExpandPointedToStructures,
2448 NULL);
2449 return;
2450 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002451 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002452 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002453 const ObjCInterfaceType *OIT =
2454 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002455 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002456 S += '"';
2457 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002458 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2459 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2460 S += '<';
2461 S += Proto->getNameAsString();
2462 S += '>';
2463 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002464 S += '"';
2465 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002466 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002467 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002468 S += '#';
2469 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002470 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002471 S += ':';
2472 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002473 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002474
2475 if (PointeeTy->isCharType()) {
2476 // char pointer types should be encoded as '*' unless it is a
2477 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002478 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002479 S += '*';
2480 return;
2481 }
2482 }
2483
2484 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002485 getLegacyIntegralTypeEncoding(PointeeTy);
2486
2487 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002488 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002489 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002490 } else if (const ArrayType *AT =
2491 // Ignore type qualifiers etc.
2492 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002493 if (isa<IncompleteArrayType>(AT)) {
2494 // Incomplete arrays are encoded as a pointer to the array element.
2495 S += '^';
2496
2497 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2498 false, ExpandStructures, FD);
2499 } else {
2500 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002501
Anders Carlsson858c64d2009-02-22 01:38:57 +00002502 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2503 S += llvm::utostr(CAT->getSize().getZExtValue());
2504 else {
2505 //Variable length arrays are encoded as a regular array with 0 elements.
2506 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2507 S += '0';
2508 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002509
Anders Carlsson858c64d2009-02-22 01:38:57 +00002510 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2511 false, ExpandStructures, FD);
2512 S += ']';
2513 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002514 } else if (T->getAsFunctionType()) {
2515 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002516 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002517 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002518 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002519 // Anonymous structures print as '?'
2520 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2521 S += II->getName();
2522 } else {
2523 S += '?';
2524 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002525 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002526 S += '=';
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002527 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2528 FieldEnd = RDecl->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002529 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002530 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002531 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002532 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002533 S += '"';
2534 }
2535
2536 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002537 if (Field->isBitField()) {
2538 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2539 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002540 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002541 QualType qt = Field->getType();
2542 getLegacyIntegralTypeEncoding(qt);
2543 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002544 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002545 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002546 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002547 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002548 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002549 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002550 if (FD && FD->isBitField())
2551 EncodeBitField(this, S, FD);
2552 else
2553 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002554 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002555 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002556 } else if (T->isObjCInterfaceType()) {
2557 // @encode(class_name)
2558 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2559 S += '{';
2560 const IdentifierInfo *II = OI->getIdentifier();
2561 S += II->getName();
2562 S += '=';
Chris Lattner9329cf52009-03-31 08:48:01 +00002563 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002564 CollectObjCIvars(OI, RecFields);
Chris Lattner9329cf52009-03-31 08:48:01 +00002565 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002566 if (RecFields[i]->isBitField())
2567 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2568 RecFields[i]);
2569 else
2570 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2571 FD);
2572 }
2573 S += '}';
2574 }
2575 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002576 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002577}
2578
Ted Kremenek42730c52008-01-07 19:49:32 +00002579void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002580 std::string& S) const {
2581 if (QT & Decl::OBJC_TQ_In)
2582 S += 'n';
2583 if (QT & Decl::OBJC_TQ_Inout)
2584 S += 'N';
2585 if (QT & Decl::OBJC_TQ_Out)
2586 S += 'o';
2587 if (QT & Decl::OBJC_TQ_Bycopy)
2588 S += 'O';
2589 if (QT & Decl::OBJC_TQ_Byref)
2590 S += 'R';
2591 if (QT & Decl::OBJC_TQ_Oneway)
2592 S += 'V';
2593}
2594
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002595void ASTContext::setBuiltinVaListType(QualType T)
2596{
2597 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2598
2599 BuiltinVaListType = T;
2600}
2601
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002602void ASTContext::setObjCIdType(QualType T)
Steve Naroff9d12c902007-10-15 14:41:52 +00002603{
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002604 ObjCIdType = T;
2605
2606 const TypedefType *TT = T->getAsTypedefType();
2607 if (!TT)
2608 return;
2609
2610 TypedefDecl *TD = TT->getDecl();
Steve Naroff9d12c902007-10-15 14:41:52 +00002611
2612 // typedef struct objc_object *id;
2613 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002614 // User error - caller will issue diagnostics.
2615 if (!ptr)
2616 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002617 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002618 // User error - caller will issue diagnostics.
2619 if (!rec)
2620 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002621 IdStructType = rec;
2622}
2623
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002624void ASTContext::setObjCSelType(QualType T)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002625{
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002626 ObjCSelType = T;
2627
2628 const TypedefType *TT = T->getAsTypedefType();
2629 if (!TT)
2630 return;
2631 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002632
2633 // typedef struct objc_selector *SEL;
2634 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002635 if (!ptr)
2636 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002637 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002638 if (!rec)
2639 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002640 SelStructType = rec;
2641}
2642
Ted Kremenek42730c52008-01-07 19:49:32 +00002643void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002644{
Ted Kremenek42730c52008-01-07 19:49:32 +00002645 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002646}
2647
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002648void ASTContext::setObjCClassType(QualType T)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002649{
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002650 ObjCClassType = T;
2651
2652 const TypedefType *TT = T->getAsTypedefType();
2653 if (!TT)
2654 return;
2655 TypedefDecl *TD = TT->getDecl();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002656
2657 // typedef struct objc_class *Class;
2658 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2659 assert(ptr && "'Class' incorrectly typed");
2660 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2661 assert(rec && "'Class' incorrectly typed");
2662 ClassStructType = rec;
2663}
2664
Ted Kremenek42730c52008-01-07 19:49:32 +00002665void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2666 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002667 "'NSConstantString' type already set!");
2668
Ted Kremenek42730c52008-01-07 19:49:32 +00002669 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002670}
2671
Douglas Gregordd13e842009-03-30 22:58:21 +00002672/// \brief Retrieve the template name that represents a qualified
2673/// template name such as \c std::vector.
2674TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2675 bool TemplateKeyword,
2676 TemplateDecl *Template) {
2677 llvm::FoldingSetNodeID ID;
2678 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2679
2680 void *InsertPos = 0;
2681 QualifiedTemplateName *QTN =
2682 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2683 if (!QTN) {
2684 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2685 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2686 }
2687
2688 return TemplateName(QTN);
2689}
2690
2691/// \brief Retrieve the template name that represents a dependent
2692/// template name such as \c MetaFun::template apply.
2693TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2694 const IdentifierInfo *Name) {
2695 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2696
2697 llvm::FoldingSetNodeID ID;
2698 DependentTemplateName::Profile(ID, NNS, Name);
2699
2700 void *InsertPos = 0;
2701 DependentTemplateName *QTN =
2702 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2703
2704 if (QTN)
2705 return TemplateName(QTN);
2706
2707 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2708 if (CanonNNS == NNS) {
2709 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2710 } else {
2711 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2712 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2713 }
2714
2715 DependentTemplateNames.InsertNode(QTN, InsertPos);
2716 return TemplateName(QTN);
2717}
2718
Douglas Gregorc6507e42008-11-03 14:12:49 +00002719/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002720/// TargetInfo, produce the corresponding type. The unsigned @p Type
2721/// is actually a value of type @c TargetInfo::IntType.
2722QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002723 switch (Type) {
2724 case TargetInfo::NoInt: return QualType();
2725 case TargetInfo::SignedShort: return ShortTy;
2726 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2727 case TargetInfo::SignedInt: return IntTy;
2728 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2729 case TargetInfo::SignedLong: return LongTy;
2730 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2731 case TargetInfo::SignedLongLong: return LongLongTy;
2732 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2733 }
2734
2735 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002736 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002737}
Ted Kremenek118930e2008-07-24 23:58:27 +00002738
2739//===----------------------------------------------------------------------===//
2740// Type Predicates.
2741//===----------------------------------------------------------------------===//
2742
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002743/// isObjCNSObjectType - Return true if this is an NSObject object using
2744/// NSObject attribute on a c-style pointer type.
2745/// FIXME - Make it work directly on types.
2746///
2747bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2748 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2749 if (TypedefDecl *TD = TDT->getDecl())
2750 if (TD->getAttr<ObjCNSObjectAttr>())
2751 return true;
2752 }
2753 return false;
2754}
2755
Ted Kremenek118930e2008-07-24 23:58:27 +00002756/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2757/// to an object type. This includes "id" and "Class" (two 'special' pointers
2758/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2759/// ID type).
2760bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroff6805fc42009-02-23 18:36:16 +00002761 if (Ty->isObjCQualifiedIdType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002762 return true;
2763
Steve Naroffd9e00802008-10-21 18:24:04 +00002764 // Blocks are objects.
2765 if (Ty->isBlockPointerType())
2766 return true;
2767
2768 // All other object types are pointers.
Chris Lattnera008d172009-04-12 23:51:02 +00002769 const PointerType *PT = Ty->getAsPointerType();
2770 if (PT == 0)
Ted Kremenek118930e2008-07-24 23:58:27 +00002771 return false;
2772
Chris Lattnera008d172009-04-12 23:51:02 +00002773 // If this a pointer to an interface (e.g. NSString*), it is ok.
2774 if (PT->getPointeeType()->isObjCInterfaceType() ||
2775 // If is has NSObject attribute, OK as well.
2776 isObjCNSObjectType(Ty))
2777 return true;
2778
Ted Kremenek118930e2008-07-24 23:58:27 +00002779 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2780 // pointer types. This looks for the typedef specifically, not for the
Chris Lattnera008d172009-04-12 23:51:02 +00002781 // underlying type. Iteratively strip off typedefs so that we can handle
2782 // typedefs of typedefs.
2783 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2784 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2785 Ty.getUnqualifiedType() == getObjCClassType())
2786 return true;
2787
2788 Ty = TDT->getDecl()->getUnderlyingType();
2789 }
Ted Kremenek118930e2008-07-24 23:58:27 +00002790
Chris Lattnera008d172009-04-12 23:51:02 +00002791 return false;
Ted Kremenek118930e2008-07-24 23:58:27 +00002792}
2793
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002794/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2795/// garbage collection attribute.
2796///
2797QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002798 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002799 if (getLangOptions().ObjC1 &&
2800 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002801 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002802 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002803 // (or pointers to them) be treated as though they were declared
2804 // as __strong.
2805 if (GCAttrs == QualType::GCNone) {
2806 if (isObjCObjectPointerType(Ty))
2807 GCAttrs = QualType::Strong;
2808 else if (Ty->isPointerType())
2809 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2810 }
Fariborz Jahaniand7b01972009-04-11 00:00:54 +00002811 // Non-pointers have none gc'able attribute regardless of the attribute
2812 // set on them.
2813 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2814 return QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002815 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002816 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002817}
2818
Chris Lattner6ff358b2008-04-07 06:51:04 +00002819//===----------------------------------------------------------------------===//
2820// Type Compatibility Testing
2821//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002822
Steve Naroff3454b6c2008-09-04 15:10:53 +00002823/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002824/// block types. Types must be strictly compatible here. For example,
2825/// C unfortunately doesn't produce an error for the following:
2826///
2827/// int (*emptyArgFunc)();
2828/// int (*intArgList)(int) = emptyArgFunc;
2829///
2830/// For blocks, we will produce an error for the following (similar to C++):
2831///
2832/// int (^emptyArgBlock)();
2833/// int (^intArgBlock)(int) = emptyArgBlock;
2834///
2835/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2836///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002837bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002838 const FunctionType *lbase = lhs->getAsFunctionType();
2839 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002840 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2841 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stump896ceed2009-04-01 01:17:39 +00002842 if (lproto && rproto == 0)
2843 return false;
2844 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff3454b6c2008-09-04 15:10:53 +00002845}
2846
Chris Lattner6ff358b2008-04-07 06:51:04 +00002847/// areCompatVectorTypes - Return true if the two specified vector types are
2848/// compatible.
2849static bool areCompatVectorTypes(const VectorType *LHS,
2850 const VectorType *RHS) {
2851 assert(LHS->isCanonical() && RHS->isCanonical());
2852 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002853 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002854}
2855
Eli Friedman0d9549b2008-08-22 00:56:42 +00002856/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002857/// compatible for assignment from RHS to LHS. This handles validation of any
2858/// protocol qualifiers on the LHS or RHS.
2859///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002860bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2861 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002862 // Verify that the base decls are compatible: the RHS must be a subclass of
2863 // the LHS.
2864 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2865 return false;
2866
2867 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2868 // protocol qualified at all, then we are good.
2869 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2870 return true;
2871
2872 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2873 // isn't a superset.
2874 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2875 return true; // FIXME: should return false!
2876
2877 // Finally, we must have two protocol-qualified interfaces.
2878 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2879 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ff358b2008-04-07 06:51:04 +00002880
Steve Naroff98e71b82009-03-01 16:12:44 +00002881 // All LHS protocols must have a presence on the RHS.
2882 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ff358b2008-04-07 06:51:04 +00002883
Steve Naroff98e71b82009-03-01 16:12:44 +00002884 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2885 LHSPE = LHSP->qual_end();
2886 LHSPI != LHSPE; LHSPI++) {
2887 bool RHSImplementsProtocol = false;
2888
2889 // If the RHS doesn't implement the protocol on the left, the types
2890 // are incompatible.
2891 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2892 RHSPE = RHSP->qual_end();
2893 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2894 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2895 RHSImplementsProtocol = true;
2896 }
2897 // FIXME: For better diagnostics, consider passing back the protocol name.
2898 if (!RHSImplementsProtocol)
2899 return false;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002900 }
Steve Naroff98e71b82009-03-01 16:12:44 +00002901 // The RHS implements all protocols listed on the LHS.
2902 return true;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002903}
2904
Steve Naroff17c03822009-02-12 17:52:19 +00002905bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2906 // get the "pointed to" types
2907 const PointerType *LHSPT = LHS->getAsPointerType();
2908 const PointerType *RHSPT = RHS->getAsPointerType();
2909
2910 if (!LHSPT || !RHSPT)
2911 return false;
2912
2913 QualType lhptee = LHSPT->getPointeeType();
2914 QualType rhptee = RHSPT->getPointeeType();
2915 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2916 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2917 // ID acts sort of like void* for ObjC interfaces
2918 if (LHSIface && isObjCIdStructType(rhptee))
2919 return true;
2920 if (RHSIface && isObjCIdStructType(lhptee))
2921 return true;
2922 if (!LHSIface || !RHSIface)
2923 return false;
2924 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2925 canAssignObjCInterfaces(RHSIface, LHSIface);
2926}
2927
Steve Naroff85f0dc52007-10-15 20:41:53 +00002928/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2929/// both shall have the identically qualified version of a compatible type.
2930/// C99 6.2.7p1: Two types have compatible types if their types are the
2931/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002932bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2933 return !mergeTypes(LHS, RHS).isNull();
2934}
2935
2936QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2937 const FunctionType *lbase = lhs->getAsFunctionType();
2938 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002939 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2940 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002941 bool allLTypes = true;
2942 bool allRTypes = true;
2943
2944 // Check return type
2945 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2946 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002947 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2948 allLTypes = false;
2949 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2950 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002951
2952 if (lproto && rproto) { // two C99 style function prototypes
2953 unsigned lproto_nargs = lproto->getNumArgs();
2954 unsigned rproto_nargs = rproto->getNumArgs();
2955
2956 // Compatible functions must have the same number of arguments
2957 if (lproto_nargs != rproto_nargs)
2958 return QualType();
2959
2960 // Variadic and non-variadic functions aren't compatible
2961 if (lproto->isVariadic() != rproto->isVariadic())
2962 return QualType();
2963
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002964 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2965 return QualType();
2966
Eli Friedman0d9549b2008-08-22 00:56:42 +00002967 // Check argument compatibility
2968 llvm::SmallVector<QualType, 10> types;
2969 for (unsigned i = 0; i < lproto_nargs; i++) {
2970 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2971 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2972 QualType argtype = mergeTypes(largtype, rargtype);
2973 if (argtype.isNull()) return QualType();
2974 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002975 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2976 allLTypes = false;
2977 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2978 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002979 }
2980 if (allLTypes) return lhs;
2981 if (allRTypes) return rhs;
2982 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002983 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002984 }
2985
2986 if (lproto) allRTypes = false;
2987 if (rproto) allLTypes = false;
2988
Douglas Gregor4fa58902009-02-26 23:50:07 +00002989 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002990 if (proto) {
2991 if (proto->isVariadic()) return QualType();
2992 // Check that the types are compatible with the types that
2993 // would result from default argument promotions (C99 6.7.5.3p15).
2994 // The only types actually affected are promotable integer
2995 // types and floats, which would be passed as a different
2996 // type depending on whether the prototype is visible.
2997 unsigned proto_nargs = proto->getNumArgs();
2998 for (unsigned i = 0; i < proto_nargs; ++i) {
2999 QualType argTy = proto->getArgType(i);
3000 if (argTy->isPromotableIntegerType() ||
3001 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
3002 return QualType();
3003 }
3004
3005 if (allLTypes) return lhs;
3006 if (allRTypes) return rhs;
3007 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00003008 proto->getNumArgs(), lproto->isVariadic(),
3009 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00003010 }
3011
3012 if (allLTypes) return lhs;
3013 if (allRTypes) return rhs;
Douglas Gregor4fa58902009-02-26 23:50:07 +00003014 return getFunctionNoProtoType(retType);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003015}
3016
3017QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00003018 // C++ [expr]: If an expression initially has the type "reference to T", the
3019 // type is adjusted to "T" prior to any further analysis, the expression
3020 // designates the object or function denoted by the reference, and the
Sebastian Redlce6fff02009-03-16 23:22:08 +00003021 // expression is an lvalue unless the reference is an rvalue reference and
3022 // the expression is a function call (possibly inside parentheses).
Eli Friedman0d9549b2008-08-22 00:56:42 +00003023 // FIXME: C++ shouldn't be going through here! The rules are different
3024 // enough that they should be handled separately.
Sebastian Redlce6fff02009-03-16 23:22:08 +00003025 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3026 // shouldn't be going through here!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003027 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00003028 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00003029 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00003030 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00003031
Eli Friedman0d9549b2008-08-22 00:56:42 +00003032 QualType LHSCan = getCanonicalType(LHS),
3033 RHSCan = getCanonicalType(RHS);
3034
3035 // If two types are identical, they are compatible.
3036 if (LHSCan == RHSCan)
3037 return LHS;
3038
3039 // If the qualifiers are different, the types aren't compatible
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003040 // Note that we handle extended qualifiers later, in the
3041 // case for ExtQualType.
3042 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman0d9549b2008-08-22 00:56:42 +00003043 return QualType();
3044
Fariborz Jahanian0dc684e2009-04-15 21:54:48 +00003045 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
3046 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman0d9549b2008-08-22 00:56:42 +00003047
Chris Lattnerc38d4522008-01-14 05:45:46 +00003048 // We want to consider the two function types to be the same for these
3049 // comparisons, just force one to the other.
3050 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3051 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00003052
3053 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00003054 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3055 LHSClass = Type::ConstantArray;
3056 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3057 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003058
Nate Begemanaf6ed502008-04-18 23:10:10 +00003059 // Canonicalize ExtVector -> Vector.
3060 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3061 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00003062
Chris Lattner7cdcb252008-04-07 06:38:24 +00003063 // Consider qualified interfaces and interfaces the same.
3064 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
3065 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003066
Chris Lattnerb5709e22008-04-07 05:43:21 +00003067 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00003068 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003069 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3070 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanian0dc684e2009-04-15 21:54:48 +00003071
Steve Naroff0773c582009-04-14 15:11:46 +00003072 // 'id' and 'Class' act sort of like void* for ObjC interfaces
3073 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00003074 return LHS;
Steve Naroff0773c582009-04-14 15:11:46 +00003075 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00003076 return RHS;
3077
Steve Naroff28ceff72008-12-10 22:14:21 +00003078 // ID is compatible with all qualified id types.
3079 if (LHS->isObjCQualifiedIdType()) {
3080 if (const PointerType *PT = RHS->getAsPointerType()) {
3081 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00003082 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00003083 return LHS;
3084 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3085 // Unfortunately, this API is part of Sema (which we don't have access
3086 // to. Need to refactor. The following check is insufficient, since we
3087 // need to make sure the class implements the protocol.
3088 if (pType->isObjCInterfaceType())
3089 return LHS;
3090 }
3091 }
3092 if (RHS->isObjCQualifiedIdType()) {
3093 if (const PointerType *PT = LHS->getAsPointerType()) {
3094 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00003095 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00003096 return RHS;
3097 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3098 // Unfortunately, this API is part of Sema (which we don't have access
3099 // to. Need to refactor. The following check is insufficient, since we
3100 // need to make sure the class implements the protocol.
3101 if (pType->isObjCInterfaceType())
3102 return RHS;
3103 }
3104 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003105 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3106 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003107 if (const EnumType* ETy = LHS->getAsEnumType()) {
3108 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3109 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00003110 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003111 if (const EnumType* ETy = RHS->getAsEnumType()) {
3112 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3113 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00003114 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003115
Eli Friedman0d9549b2008-08-22 00:56:42 +00003116 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003117 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003118
Steve Naroffc88babe2008-01-09 22:43:08 +00003119 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00003120 switch (LHSClass) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00003121#define TYPE(Class, Base)
3122#define ABSTRACT_TYPE(Class, Base)
3123#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3124#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3125#include "clang/AST/TypeNodes.def"
3126 assert(false && "Non-canonical and dependent types shouldn't get here");
3127 return QualType();
3128
Sebastian Redlce6fff02009-03-16 23:22:08 +00003129 case Type::LValueReference:
3130 case Type::RValueReference:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003131 case Type::MemberPointer:
3132 assert(false && "C++ should never be in mergeTypes");
3133 return QualType();
3134
3135 case Type::IncompleteArray:
3136 case Type::VariableArray:
3137 case Type::FunctionProto:
3138 case Type::ExtVector:
3139 case Type::ObjCQualifiedInterface:
3140 assert(false && "Types are eliminated above");
3141 return QualType();
3142
Chris Lattnerc38d4522008-01-14 05:45:46 +00003143 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003144 {
3145 // Merge two pointer types, while trying to preserve typedef info
3146 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3147 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3148 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3149 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003150 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3151 return LHS;
3152 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3153 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003154 return getPointerType(ResultType);
3155 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00003156 case Type::BlockPointer:
3157 {
3158 // Merge two block pointer types, while trying to preserve typedef info
3159 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3160 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3161 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3162 if (ResultType.isNull()) return QualType();
3163 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3164 return LHS;
3165 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3166 return RHS;
3167 return getBlockPointerType(ResultType);
3168 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003169 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003170 {
3171 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3172 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3173 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3174 return QualType();
3175
3176 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3177 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3178 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3179 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003180 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3181 return LHS;
3182 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3183 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003184 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3185 ArrayType::ArraySizeModifier(), 0);
3186 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3187 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003188 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3189 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003190 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3191 return LHS;
3192 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3193 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003194 if (LVAT) {
3195 // FIXME: This isn't correct! But tricky to implement because
3196 // the array's size has to be the size of LHS, but the type
3197 // has to be different.
3198 return LHS;
3199 }
3200 if (RVAT) {
3201 // FIXME: This isn't correct! But tricky to implement because
3202 // the array's size has to be the size of RHS, but the type
3203 // has to be different.
3204 return RHS;
3205 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003206 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3207 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003208 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003209 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003210 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003211 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor4fa58902009-02-26 23:50:07 +00003212 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003213 case Type::Enum:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003214 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00003215 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3216 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003217 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00003218 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003219 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003220 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00003221 case Type::Complex:
3222 // Distinct complex types are incompatible.
3223 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003224 case Type::Vector:
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003225 // FIXME: The merged type should be an ExtVector!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003226 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3227 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003228 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003229 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003230 // Check if the interfaces are assignment compatible.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003231 // FIXME: This should be type compatibility, e.g. whether
3232 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff0bbc1352009-02-21 16:18:07 +00003233 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3234 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3235 if (LHSIface && RHSIface &&
3236 canAssignObjCInterfaces(LHSIface, RHSIface))
3237 return LHS;
3238
Eli Friedman0d9549b2008-08-22 00:56:42 +00003239 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003240 }
Steve Naroff28ceff72008-12-10 22:14:21 +00003241 case Type::ObjCQualifiedId:
3242 // Distinct qualified id's are not compatible.
3243 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003244 case Type::FixedWidthInt:
3245 // Distinct fixed-width integers are not compatible.
3246 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003247 case Type::ExtQual:
3248 // FIXME: ExtQual types can be compatible even if they're not
3249 // identical!
3250 return QualType();
3251 // First attempt at an implementation, but I'm not really sure it's
3252 // right...
3253#if 0
3254 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3255 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3256 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3257 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3258 return QualType();
3259 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3260 LHSBase = QualType(LQual->getBaseType(), 0);
3261 RHSBase = QualType(RQual->getBaseType(), 0);
3262 ResultType = mergeTypes(LHSBase, RHSBase);
3263 if (ResultType.isNull()) return QualType();
3264 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3265 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3266 return LHS;
3267 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3268 return RHS;
3269 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3270 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3271 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3272 return ResultType;
3273#endif
Douglas Gregordd13e842009-03-30 22:58:21 +00003274
3275 case Type::TemplateSpecialization:
3276 assert(false && "Dependent types have no size");
3277 break;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003278 }
Douglas Gregor4fa58902009-02-26 23:50:07 +00003279
3280 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003281}
Ted Kremenek738e6c02007-10-31 17:10:13 +00003282
Chris Lattner1d78a862008-04-07 07:01:58 +00003283//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00003284// Integer Predicates
3285//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00003286
Eli Friedman0832dbc2008-06-28 06:23:08 +00003287unsigned ASTContext::getIntWidth(QualType T) {
3288 if (T == BoolTy)
3289 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00003290 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3291 return FWIT->getWidth();
3292 }
3293 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00003294 return (unsigned)getTypeSize(T);
3295}
3296
3297QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3298 assert(T->isSignedIntegerType() && "Unexpected type");
3299 if (const EnumType* ETy = T->getAsEnumType())
3300 T = ETy->getDecl()->getIntegerType();
3301 const BuiltinType* BTy = T->getAsBuiltinType();
3302 assert (BTy && "Unexpected signed integer type");
3303 switch (BTy->getKind()) {
3304 case BuiltinType::Char_S:
3305 case BuiltinType::SChar:
3306 return UnsignedCharTy;
3307 case BuiltinType::Short:
3308 return UnsignedShortTy;
3309 case BuiltinType::Int:
3310 return UnsignedIntTy;
3311 case BuiltinType::Long:
3312 return UnsignedLongTy;
3313 case BuiltinType::LongLong:
3314 return UnsignedLongLongTy;
Chris Lattner6cc7e412009-04-30 02:43:43 +00003315 case BuiltinType::Int128:
3316 return UnsignedInt128Ty;
Eli Friedman0832dbc2008-06-28 06:23:08 +00003317 default:
3318 assert(0 && "Unexpected signed integer type");
3319 return QualType();
3320 }
3321}
3322
Douglas Gregorc34897d2009-04-09 22:27:44 +00003323ExternalASTSource::~ExternalASTSource() { }
3324
3325void ExternalASTSource::PrintStats() { }