blob: 0494de199f9e176185c7673c62144169f907994e [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 {
66 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
67 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
68 while (I != E) {
69 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
70 delete R;
71 }
72 }
73
74 {
Chris Lattner608c1e32009-03-31 09:24:30 +000075 llvm::DenseMap<const ObjCInterfaceDecl*, RecordDecl*>::iterator
Nuno Lopes355a8682008-12-17 22:30:25 +000076 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
77 while (I != E) {
Chris Lattner608c1e32009-03-31 09:24:30 +000078 RecordDecl *R = (I++)->second;
Nuno Lopes355a8682008-12-17 22:30:25 +000079 R->Destroy(*this);
80 }
81 }
82
Douglas Gregor1e589cc2009-03-26 23:50:42 +000083 // Destroy nested-name-specifiers.
Douglas Gregor3c4eae52009-03-27 23:54:10 +000084 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
85 NNS = NestedNameSpecifiers.begin(),
86 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregorbccd97c2009-03-27 23:25:45 +000087 NNS != NNSEnd;
Douglas Gregor3c4eae52009-03-27 23:54:10 +000088 /* Increment in loop */)
89 (*NNS++).Destroy(*this);
Douglas Gregor1e589cc2009-03-26 23:50:42 +000090
91 if (GlobalNestedNameSpecifier)
92 GlobalNestedNameSpecifier->Destroy(*this);
93
Eli Friedman65489b72008-05-27 03:08:09 +000094 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000095}
96
Douglas Gregorda38c6c2009-04-22 18:49:13 +000097void ASTContext::InitializeBuiltins(IdentifierTable &idents) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +000098 BuiltinInfo.InitializeBuiltins(idents, LangOpts.NoBuiltin);
99}
100
Douglas Gregorc34897d2009-04-09 22:27:44 +0000101void
102ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
103 ExternalSource.reset(Source.take());
104}
105
Chris Lattner4b009652007-07-25 00:24:17 +0000106void ASTContext::PrintStats() const {
107 fprintf(stderr, "*** AST Context Stats:\n");
108 fprintf(stderr, " %d types total.\n", (int)Types.size());
109 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar47677342008-09-26 03:23:00 +0000110 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000111 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
112 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
113
Chris Lattner4b009652007-07-25 00:24:17 +0000114 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000115 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
116 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000117 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000118 unsigned NumExtQual = 0;
119
Chris Lattner4b009652007-07-25 00:24:17 +0000120 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
121 Type *T = Types[i];
122 if (isa<BuiltinType>(T))
123 ++NumBuiltin;
124 else if (isa<PointerType>(T))
125 ++NumPointer;
Daniel Dunbar47677342008-09-26 03:23:00 +0000126 else if (isa<BlockPointerType>(T))
127 ++NumBlockPointer;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000128 else if (isa<LValueReferenceType>(T))
129 ++NumLValueReference;
130 else if (isa<RValueReferenceType>(T))
131 ++NumRValueReference;
Sebastian Redl75555032009-01-24 21:16:55 +0000132 else if (isa<MemberPointerType>(T))
133 ++NumMemberPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000134 else if (isa<ComplexType>(T))
135 ++NumComplex;
136 else if (isa<ArrayType>(T))
137 ++NumArray;
138 else if (isa<VectorType>(T))
139 ++NumVector;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000140 else if (isa<FunctionNoProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000141 ++NumFunctionNP;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000142 else if (isa<FunctionProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000143 ++NumFunctionP;
144 else if (isa<TypedefType>(T))
145 ++NumTypeName;
146 else if (TagType *TT = dyn_cast<TagType>(T)) {
147 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000148 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000149 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000150 case TagDecl::TK_struct: ++NumTagStruct; break;
151 case TagDecl::TK_union: ++NumTagUnion; break;
152 case TagDecl::TK_class: ++NumTagClass; break;
153 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000154 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000155 } else if (isa<ObjCInterfaceType>(T))
156 ++NumObjCInterfaces;
157 else if (isa<ObjCQualifiedInterfaceType>(T))
158 ++NumObjCQualifiedInterfaces;
159 else if (isa<ObjCQualifiedIdType>(T))
160 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +0000161 else if (isa<TypeOfType>(T))
162 ++NumTypeOfTypes;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000163 else if (isa<TypeOfExprType>(T))
164 ++NumTypeOfExprTypes;
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000165 else if (isa<ExtQualType>(T))
166 ++NumExtQual;
Steve Naroff948fd372007-09-17 14:16:13 +0000167 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000168 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000169 assert(0 && "Unknown type!");
170 }
171 }
172
173 fprintf(stderr, " %d builtin types\n", NumBuiltin);
174 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar47677342008-09-26 03:23:00 +0000175 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000176 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
177 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redl75555032009-01-24 21:16:55 +0000178 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000179 fprintf(stderr, " %d complex types\n", NumComplex);
180 fprintf(stderr, " %d array types\n", NumArray);
181 fprintf(stderr, " %d vector types\n", NumVector);
182 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
183 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
184 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
185 fprintf(stderr, " %d tagged types\n", NumTagged);
186 fprintf(stderr, " %d struct types\n", NumTagStruct);
187 fprintf(stderr, " %d union types\n", NumTagUnion);
188 fprintf(stderr, " %d class types\n", NumTagClass);
189 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000190 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000191 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000192 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000193 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000194 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000195 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000196 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000197 fprintf(stderr, " %d attribute-qualified types\n", NumExtQual);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000198
Chris Lattner4b009652007-07-25 00:24:17 +0000199 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
200 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
201 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redlce6fff02009-03-16 23:22:08 +0000202 NumLValueReference*sizeof(LValueReferenceType)+
203 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redl75555032009-01-24 21:16:55 +0000204 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor4fa58902009-02-26 23:50:07 +0000205 NumFunctionP*sizeof(FunctionProtoType)+
206 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroffe0430632008-05-21 15:59:22 +0000207 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000208 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)+
209 NumExtQual*sizeof(ExtQualType)));
Douglas Gregorc34897d2009-04-09 22:27:44 +0000210
211 if (ExternalSource.get()) {
212 fprintf(stderr, "\n");
213 ExternalSource->PrintStats();
214 }
Chris Lattner4b009652007-07-25 00:24:17 +0000215}
216
217
218void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroff93fd2112009-01-27 22:08:43 +0000219 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000220}
221
Chris Lattner4b009652007-07-25 00:24:17 +0000222void ASTContext::InitBuiltinTypes() {
223 assert(VoidTy.isNull() && "Context reinitialized?");
224
225 // C99 6.2.5p19.
226 InitBuiltinType(VoidTy, BuiltinType::Void);
227
228 // C99 6.2.5p2.
229 InitBuiltinType(BoolTy, BuiltinType::Bool);
230 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000231 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000232 InitBuiltinType(CharTy, BuiltinType::Char_S);
233 else
234 InitBuiltinType(CharTy, BuiltinType::Char_U);
235 // C99 6.2.5p4.
236 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
237 InitBuiltinType(ShortTy, BuiltinType::Short);
238 InitBuiltinType(IntTy, BuiltinType::Int);
239 InitBuiltinType(LongTy, BuiltinType::Long);
240 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
241
242 // C99 6.2.5p6.
243 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
244 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
245 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
246 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
247 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
248
249 // C99 6.2.5p10.
250 InitBuiltinType(FloatTy, BuiltinType::Float);
251 InitBuiltinType(DoubleTy, BuiltinType::Double);
252 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000253
Chris Lattner6cc7e412009-04-30 02:43:43 +0000254 // GNU extension, 128-bit integers.
255 InitBuiltinType(Int128Ty, BuiltinType::Int128);
256 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
257
Chris Lattnere1dafe72009-02-26 23:43:47 +0000258 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
259 InitBuiltinType(WCharTy, BuiltinType::WChar);
260 else // C99
261 WCharTy = getFromTargetType(Target.getWCharType());
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000262
Douglas Gregord2baafd2008-10-21 16:13:35 +0000263 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000264 InitBuiltinType(OverloadTy, BuiltinType::Overload);
265
266 // Placeholder type for type-dependent expressions whose type is
267 // completely unknown. No code should ever check a type against
268 // DependentTy and users should never see it; however, it is here to
269 // help diagnose failures to properly check for type-dependent
270 // expressions.
271 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000272
Chris Lattner4b009652007-07-25 00:24:17 +0000273 // C99 6.2.5p11.
274 FloatComplexTy = getComplexType(FloatTy);
275 DoubleComplexTy = getComplexType(DoubleTy);
276 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000277
Steve Naroff9d12c902007-10-15 14:41:52 +0000278 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000279 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000280 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000281 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000282 ClassStructType = 0;
283
Ted Kremenek42730c52008-01-07 19:49:32 +0000284 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000285
286 // void * type
287 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000288}
289
290//===----------------------------------------------------------------------===//
291// Type Sizing and Analysis
292//===----------------------------------------------------------------------===//
293
Chris Lattner2a674dc2008-06-30 18:32:54 +0000294/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
295/// scalar floating point type.
296const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
297 const BuiltinType *BT = T->getAsBuiltinType();
298 assert(BT && "Not a floating point type!");
299 switch (BT->getKind()) {
300 default: assert(0 && "Not a floating point type!");
301 case BuiltinType::Float: return Target.getFloatFormat();
302 case BuiltinType::Double: return Target.getDoubleFormat();
303 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
304 }
305}
306
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000307/// getDeclAlign - Return a conservative estimate of the alignment of the
308/// specified decl. Note that bitfields do not have a valid alignment, so
309/// this method will assert on them.
Daniel Dunbar96d1f1b2009-02-17 22:16:19 +0000310unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedman0ee57322009-02-22 02:56:25 +0000311 unsigned Align = Target.getCharWidth();
312
313 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
314 Align = std::max(Align, AA->getAlignment());
315
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000316 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
317 QualType T = VD->getType();
Anders Carlssonaa0783b2009-04-10 04:47:03 +0000318 if (const ReferenceType* RT = T->getAsReferenceType()) {
319 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssoneeaeda32009-04-10 04:52:36 +0000320 Align = Target.getPointerAlign(AS);
Anders Carlssonaa0783b2009-04-10 04:47:03 +0000321 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
322 // Incomplete or function types default to 1.
Eli Friedman0ee57322009-02-22 02:56:25 +0000323 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
324 T = cast<ArrayType>(T)->getElementType();
325
326 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
327 }
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000328 }
Eli Friedman0ee57322009-02-22 02:56:25 +0000329
330 return Align / Target.getCharWidth();
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000331}
Chris Lattner2a674dc2008-06-30 18:32:54 +0000332
Chris Lattner4b009652007-07-25 00:24:17 +0000333/// getTypeSize - Return the size of the specified type, in bits. This method
334/// does not work on incomplete types.
335std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000336ASTContext::getTypeInfo(const Type *T) {
Mike Stump44d1f402009-02-27 18:32:39 +0000337 uint64_t Width=0;
338 unsigned Align=8;
Chris Lattner4b009652007-07-25 00:24:17 +0000339 switch (T->getTypeClass()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000340#define TYPE(Class, Base)
341#define ABSTRACT_TYPE(Class, Base)
Douglas Gregorab380272009-04-30 17:32:17 +0000342#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor4fa58902009-02-26 23:50:07 +0000343#define DEPENDENT_TYPE(Class, Base) case Type::Class:
344#include "clang/AST/TypeNodes.def"
Douglas Gregorab380272009-04-30 17:32:17 +0000345 assert(false && "Should not see dependent types");
Douglas Gregor4fa58902009-02-26 23:50:07 +0000346 break;
347
Chris Lattner4b009652007-07-25 00:24:17 +0000348 case Type::FunctionNoProto:
349 case Type::FunctionProto:
Douglas Gregorab380272009-04-30 17:32:17 +0000350 // GCC extension: alignof(function) = 32 bits
351 Width = 0;
352 Align = 32;
353 break;
354
Douglas Gregor4fa58902009-02-26 23:50:07 +0000355 case Type::IncompleteArray:
Steve Naroff83c13012007-08-30 01:06:46 +0000356 case Type::VariableArray:
Douglas Gregorab380272009-04-30 17:32:17 +0000357 Width = 0;
358 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
359 break;
360
Steve Naroff83c13012007-08-30 01:06:46 +0000361 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000362 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000363
Chris Lattner8cd0e932008-03-05 18:54:05 +0000364 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000365 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000366 Align = EltInfo.second;
367 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000368 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000369 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000370 case Type::Vector: {
371 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000372 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000373 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000374 Align = Width;
Nate Begeman7903d052009-01-18 06:42:49 +0000375 // If the alignment is not a power of 2, round up to the next power of 2.
376 // This happens for non-power-of-2 length vectors.
377 // FIXME: this should probably be a target property.
378 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000379 break;
380 }
381
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000382 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000383 switch (cast<BuiltinType>(T)->getKind()) {
384 default: assert(0 && "Unknown builtin type!");
385 case BuiltinType::Void:
Douglas Gregorab380272009-04-30 17:32:17 +0000386 // GCC extension: alignof(void) = 8 bits.
387 Width = 0;
388 Align = 8;
389 break;
390
Chris Lattnerb66237b2007-12-19 19:23:28 +0000391 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000392 Width = Target.getBoolWidth();
393 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000394 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000395 case BuiltinType::Char_S:
396 case BuiltinType::Char_U:
397 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000398 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000399 Width = Target.getCharWidth();
400 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000401 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000402 case BuiltinType::WChar:
403 Width = Target.getWCharWidth();
404 Align = Target.getWCharAlign();
405 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000406 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000407 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000408 Width = Target.getShortWidth();
409 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000410 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000411 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000412 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000413 Width = Target.getIntWidth();
414 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000415 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000416 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000417 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000418 Width = Target.getLongWidth();
419 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000420 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000421 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000422 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000423 Width = Target.getLongLongWidth();
424 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000425 break;
Chris Lattner4b11cc22009-04-30 02:55:13 +0000426 case BuiltinType::Int128:
427 case BuiltinType::UInt128:
428 Width = 128;
429 Align = 128; // int128_t is 128-bit aligned on all targets.
430 break;
Chris Lattnerb66237b2007-12-19 19:23:28 +0000431 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000432 Width = Target.getFloatWidth();
433 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000434 break;
435 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000436 Width = Target.getDoubleWidth();
437 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000438 break;
439 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000440 Width = Target.getLongDoubleWidth();
441 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000442 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000443 }
444 break;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000445 case Type::FixedWidthInt:
446 // FIXME: This isn't precisely correct; the width/alignment should depend
447 // on the available types for the target
448 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattnere9174982009-02-15 21:20:13 +0000449 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000450 Align = Width;
451 break;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000452 case Type::ExtQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000453 // FIXME: Pointers into different addr spaces could have different sizes and
454 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000455 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000456 case Type::ObjCQualifiedId:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000457 case Type::ObjCQualifiedInterface:
Chris Lattner1d78a862008-04-07 07:01:58 +0000458 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000459 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000460 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000461 case Type::BlockPointer: {
462 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
463 Width = Target.getPointerWidth(AS);
464 Align = Target.getPointerAlign(AS);
465 break;
466 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000467 case Type::Pointer: {
468 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000469 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000470 Align = Target.getPointerAlign(AS);
471 break;
472 }
Sebastian Redlce6fff02009-03-16 23:22:08 +0000473 case Type::LValueReference:
474 case Type::RValueReference:
Chris Lattner4b009652007-07-25 00:24:17 +0000475 // "When applied to a reference or a reference type, the result is the size
476 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000477 // FIXME: This is wrong for struct layout: a reference in a struct has
478 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000479 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl75555032009-01-24 21:16:55 +0000480 case Type::MemberPointer: {
Sebastian Redl18cffee2009-01-24 23:29:36 +0000481 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redl75555032009-01-24 21:16:55 +0000482 // the GCC ABI, where pointers to data are one pointer large, pointers to
483 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl18cffee2009-01-24 23:29:36 +0000484 // other compilers too, we need to delegate this completely to TargetInfo
485 // or some ABI abstraction layer.
Sebastian Redl75555032009-01-24 21:16:55 +0000486 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
487 unsigned AS = Pointee.getAddressSpace();
488 Width = Target.getPointerWidth(AS);
489 if (Pointee->isFunctionType())
490 Width *= 2;
491 Align = Target.getPointerAlign(AS);
492 // GCC aligns at single pointer width.
493 }
Chris Lattner4b009652007-07-25 00:24:17 +0000494 case Type::Complex: {
495 // Complex types have the same alignment as their elements, but twice the
496 // size.
497 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000498 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000499 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000500 Align = EltInfo.second;
501 break;
502 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000503 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000504 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000505 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
506 Width = Layout.getSize();
507 Align = Layout.getAlignment();
508 break;
509 }
Douglas Gregor4fa58902009-02-26 23:50:07 +0000510 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000511 case Type::Enum: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000512 const TagType *TT = cast<TagType>(T);
513
514 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000515 Width = 1;
516 Align = 1;
517 break;
518 }
519
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000520 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000521 return getTypeInfo(ET->getDecl()->getIntegerType());
522
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000523 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000524 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
525 Width = Layout.getSize();
526 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000527 break;
528 }
Douglas Gregordd13e842009-03-30 22:58:21 +0000529
Douglas Gregorab380272009-04-30 17:32:17 +0000530 case Type::Typedef: {
531 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
532 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
533 Align = Aligned->getAlignment();
534 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
535 } else
536 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregordd13e842009-03-30 22:58:21 +0000537 break;
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000538 }
Douglas Gregorab380272009-04-30 17:32:17 +0000539
540 case Type::TypeOfExpr:
541 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
542 .getTypePtr());
543
544 case Type::TypeOf:
545 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
546
547 case Type::QualifiedName:
548 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
549
550 case Type::TemplateSpecialization:
551 assert(getCanonicalType(T) != T &&
552 "Cannot request the size of a dependent type");
553 // FIXME: this is likely to be wrong once we support template
554 // aliases, since a template alias could refer to a typedef that
555 // has an __aligned__ attribute on it.
556 return getTypeInfo(getCanonicalType(T));
557 }
Chris Lattner4b009652007-07-25 00:24:17 +0000558
559 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000560 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000561}
562
Chris Lattner83165b52009-01-27 18:08:34 +0000563/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
564/// type for the current target in bits. This can be different than the ABI
565/// alignment in cases where it is beneficial for performance to overalign
566/// a data type.
567unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
568 unsigned ABIAlign = getTypeAlign(T);
569
570 // Doubles should be naturally aligned if possible.
Daniel Dunbarc61a8002009-02-18 19:59:32 +0000571 if (T->isSpecificBuiltinType(BuiltinType::Double))
572 return std::max(ABIAlign, 64U);
Chris Lattner83165b52009-01-27 18:08:34 +0000573
574 return ABIAlign;
575}
576
577
Devang Patelbfe323c2008-06-04 21:22:16 +0000578/// LayoutField - Field layout.
579void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000580 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000581 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000582 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000583 uint64_t FieldOffset = IsUnion ? 0 : Size;
584 uint64_t FieldSize;
585 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000586
587 // FIXME: Should this override struct packing? Probably we want to
588 // take the minimum?
589 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
590 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000591
592 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
593 // TODO: Need to check this algorithm on other targets!
594 // (tested on Linux-X86)
Eli Friedman5255e7a2009-04-26 19:19:15 +0000595 FieldSize = BitWidthExpr->EvaluateAsInt(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000596
597 std::pair<uint64_t, unsigned> FieldInfo =
598 Context.getTypeInfo(FD->getType());
599 uint64_t TypeSize = FieldInfo.first;
600
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000601 // Determine the alignment of this bitfield. The packing
602 // attributes define a maximum and the alignment attribute defines
603 // a minimum.
604 // FIXME: What is the right behavior when the specified alignment
605 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000606 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000607 if (FieldPacking)
608 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000609 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
610 FieldAlign = std::max(FieldAlign, AA->getAlignment());
611
612 // Check if we need to add padding to give the field the correct
613 // alignment.
614 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
615 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
616
617 // Padding members don't affect overall alignment
618 if (!FD->getIdentifier())
619 FieldAlign = 1;
620 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000621 if (FD->getType()->isIncompleteArrayType()) {
622 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000623 // query getTypeInfo about these, so we figure it out here.
624 // Flexible array members don't have any size, but they
625 // have to be aligned appropriately for their element type.
626 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000627 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000628 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson0843ea52009-04-10 05:31:15 +0000629 } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
630 unsigned AS = RT->getPointeeType().getAddressSpace();
631 FieldSize = Context.Target.getPointerWidth(AS);
632 FieldAlign = Context.Target.getPointerAlign(AS);
Devang Patelbfe323c2008-06-04 21:22:16 +0000633 } else {
634 std::pair<uint64_t, unsigned> FieldInfo =
635 Context.getTypeInfo(FD->getType());
636 FieldSize = FieldInfo.first;
637 FieldAlign = FieldInfo.second;
638 }
639
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000640 // Determine the alignment of this bitfield. The packing
641 // attributes define a maximum and the alignment attribute defines
642 // a minimum. Additionally, the packing alignment must be at least
643 // a byte for non-bitfields.
644 //
645 // FIXME: What is the right behavior when the specified alignment
646 // is smaller than the specified packing?
647 if (FieldPacking)
648 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000649 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
650 FieldAlign = std::max(FieldAlign, AA->getAlignment());
651
652 // Round up the current record size to the field's alignment boundary.
653 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
654 }
655
656 // Place this field at the current location.
657 FieldOffsets[FieldNo] = FieldOffset;
658
659 // Reserve space for this field.
660 if (IsUnion) {
661 Size = std::max(Size, FieldSize);
662 } else {
663 Size = FieldOffset + FieldSize;
664 }
665
666 // Remember max struct/class alignment.
667 Alignment = std::max(Alignment, FieldAlign);
668}
669
Daniel 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 }
Fariborz Jahanianc625fa92009-03-31 00:06:29 +0000679 // look into properties.
Daniel Dunbar1af336e2009-04-22 17:43:55 +0000680 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*Ctx),
681 E = OI->prop_end(*Ctx); I != E; ++I) {
Chris Lattner9329cf52009-03-31 08:48:01 +0000682 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
Fariborz Jahanianc625fa92009-03-31 00:06:29 +0000683 Fields.push_back(cast<FieldDecl>(IV));
684 }
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000685}
686
Daniel Dunbar1af336e2009-04-22 17:43:55 +0000687void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
688 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
689 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
690 CollectObjCIvars(SuperClass, Fields);
691 CollectLocalObjCIvars(this, OI, Fields);
692}
693
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000694/// addRecordToClass - produces record info. for the class for its
695/// ivars and all those inherited.
696///
Chris Lattner9329cf52009-03-31 08:48:01 +0000697const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D) {
Daniel Dunbaracdae262009-04-22 10:56:29 +0000698 assert(!D->isForwardDecl() && "Invalid decl!");
699
Chris Lattner608c1e32009-03-31 09:24:30 +0000700 RecordDecl *&RD = ASTRecordForInterface[D];
Daniel Dunbaracdae262009-04-22 10:56:29 +0000701 if (RD)
702 return RD;
Chris Lattner9329cf52009-03-31 08:48:01 +0000703
704 llvm::SmallVector<FieldDecl*, 32> RecFields;
Daniel Dunbar1af336e2009-04-22 17:43:55 +0000705 CollectLocalObjCIvars(this, D, RecFields);
Chris Lattner608c1e32009-03-31 09:24:30 +0000706
Daniel Dunbaracdae262009-04-22 10:56:29 +0000707 RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, D->getLocation(),
708 D->getIdentifier());
Daniel Dunbar1af336e2009-04-22 17:43:55 +0000709 const RecordDecl *SRD;
710 if (const ObjCInterfaceDecl *SuperClass = D->getSuperClass()) {
711 SRD = addRecordToClass(SuperClass);
712 } else {
713 SRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, SourceLocation(), 0);
714 const_cast<RecordDecl*>(SRD)->completeDefinition(*this);
715 }
716
717 RD->addDecl(*this,
718 FieldDecl::Create(*this, RD,
719 SourceLocation(),
720 0,
721 getTagDeclType(const_cast<RecordDecl*>(SRD)),
722 0, false));
723
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000724 /// FIXME! Can do collection of ivars and adding to the record while
725 /// doing it.
Chris Lattner41b780c2009-03-31 08:58:42 +0000726 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000727 RD->addDecl(*this,
728 FieldDecl::Create(*this, RD,
Chris Lattner608c1e32009-03-31 09:24:30 +0000729 RecFields[i]->getLocation(),
730 RecFields[i]->getIdentifier(),
731 RecFields[i]->getType(),
732 RecFields[i]->getBitWidth(), false));
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000733 }
Chris Lattner9329cf52009-03-31 08:48:01 +0000734
Chris Lattner608c1e32009-03-31 09:24:30 +0000735 RD->completeDefinition(*this);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000736 return RD;
737}
Devang Patel4b6bf702008-06-04 21:54:36 +0000738
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000739/// getASTObjcInterfaceLayout - Get or compute information about the layout of
740/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000741/// position information.
742const ASTRecordLayout &
743ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
744 // Look up this layout, if already laid out, return what we have.
745 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
746 if (Entry) return *Entry;
747
748 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
749 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000750 ASTRecordLayout *NewEntry = NULL;
Fariborz Jahanianba50c332009-04-08 21:54:52 +0000751 // FIXME. Add actual count of synthesized ivars, instead of count
752 // of properties which is the upper bound, but is safe.
Daniel Dunbar998510f2009-04-08 20:18:15 +0000753 unsigned FieldCount =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000754 D->ivar_size() + std::distance(D->prop_begin(*this), D->prop_end(*this));
Devang Patel8682d882008-06-06 02:14:01 +0000755 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
756 FieldCount++;
757 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
758 unsigned Alignment = SL.getAlignment();
759 uint64_t Size = SL.getSize();
760 NewEntry = new ASTRecordLayout(Size, Alignment);
761 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000762 // Super class is at the beginning of the layout.
763 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000764 } else {
765 NewEntry = new ASTRecordLayout();
766 NewEntry->InitializeLayout(FieldCount);
767 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000768 Entry = NewEntry;
769
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000770 unsigned StructPacking = 0;
771 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
772 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000773
774 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
775 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
776 AA->getAlignment()));
777
778 // Layout each ivar sequentially.
779 unsigned i = 0;
780 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
781 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
782 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000783 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000784 }
Fariborz Jahanianfbf44642009-03-31 18:11:23 +0000785 // Also synthesized ivars
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000786 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
787 E = D->prop_end(*this); I != E; ++I) {
Fariborz Jahanianfbf44642009-03-31 18:11:23 +0000788 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
789 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
790 }
Fariborz Jahanian84c45692009-04-01 19:37:34 +0000791
Devang Patel4b6bf702008-06-04 21:54:36 +0000792 // Finally, round the size of the total struct up to the alignment of the
793 // struct itself.
794 NewEntry->FinalizeLayout();
795 return *NewEntry;
796}
797
Devang Patel7a78e432007-11-01 19:11:01 +0000798/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000799/// specified record (struct/union/class), which indicates its size and field
800/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000801const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000802 D = D->getDefinition(*this);
803 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000804
Chris Lattner4b009652007-07-25 00:24:17 +0000805 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000806 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000807 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000808
Devang Patel7a78e432007-11-01 19:11:01 +0000809 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
810 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
811 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000812 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000813
Douglas Gregor39677622008-12-11 20:41:00 +0000814 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000815 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
816 D->field_end(*this)));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000817 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000818
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000819 unsigned StructPacking = 0;
820 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
821 StructPacking = PA->getAlignment();
822
Eli Friedman5949a022008-05-30 09:31:38 +0000823 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000824 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
825 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000826
Eli Friedman5949a022008-05-30 09:31:38 +0000827 // Layout each field, for now, just sequentially, respecting alignment. In
828 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000829 unsigned FieldIdx = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000830 for (RecordDecl::field_iterator Field = D->field_begin(*this),
831 FieldEnd = D->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000832 Field != FieldEnd; (void)++Field, ++FieldIdx)
833 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000834
835 // Finally, round the size of the total struct up to the alignment of the
836 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000837 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000838 return *NewEntry;
839}
840
Chris Lattner4b009652007-07-25 00:24:17 +0000841//===----------------------------------------------------------------------===//
842// Type creation/memoization methods
843//===----------------------------------------------------------------------===//
844
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000845QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000846 QualType CanT = getCanonicalType(T);
847 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000848 return T;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000849
850 // If we are composing extended qualifiers together, merge together into one
851 // ExtQualType node.
852 unsigned CVRQuals = T.getCVRQualifiers();
853 QualType::GCAttrTypes GCAttr = QualType::GCNone;
854 Type *TypeNode = T.getTypePtr();
Chris Lattner35fef522008-02-20 20:55:12 +0000855
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000856 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
857 // If this type already has an address space specified, it cannot get
858 // another one.
859 assert(EQT->getAddressSpace() == 0 &&
860 "Type cannot be in multiple addr spaces!");
861 GCAttr = EQT->getObjCGCAttr();
862 TypeNode = EQT->getBaseType();
863 }
Chris Lattner35fef522008-02-20 20:55:12 +0000864
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000865 // Check if we've already instantiated this type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000866 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000867 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000868 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000869 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000870 return QualType(EXTQy, CVRQuals);
871
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000872 // If the base type isn't canonical, this won't be a canonical type either,
873 // so fill in the canonical type field.
874 QualType Canonical;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000875 if (!TypeNode->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000876 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000877
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000878 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000879 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000880 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000881 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000882 ExtQualType *New =
883 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000884 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000885 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000886 return QualType(New, CVRQuals);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000887}
888
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000889QualType ASTContext::getObjCGCQualType(QualType T,
890 QualType::GCAttrTypes GCAttr) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000891 QualType CanT = getCanonicalType(T);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000892 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000893 return T;
894
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000895 // If we are composing extended qualifiers together, merge together into one
896 // ExtQualType node.
897 unsigned CVRQuals = T.getCVRQualifiers();
898 Type *TypeNode = T.getTypePtr();
899 unsigned AddressSpace = 0;
900
901 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
902 // If this type already has an address space specified, it cannot get
903 // another one.
904 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
905 "Type cannot be in multiple addr spaces!");
906 AddressSpace = EQT->getAddressSpace();
907 TypeNode = EQT->getBaseType();
908 }
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000909
910 // Check if we've already instantiated an gc qual'd type of this type.
911 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000912 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000913 void *InsertPos = 0;
914 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000915 return QualType(EXTQy, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000916
917 // If the base type isn't canonical, this won't be a canonical type either,
918 // so fill in the canonical type field.
Eli Friedman94fcc9a2009-02-27 23:04:43 +0000919 // FIXME: Isn't this also not canonical if the base type is a array
920 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000921 QualType Canonical;
922 if (!T->isCanonical()) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000923 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000924
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000925 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000926 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
927 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
928 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000929 ExtQualType *New =
930 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000931 ExtQualTypes.InsertNode(New, InsertPos);
932 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000933 return QualType(New, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000934}
Chris Lattner4b009652007-07-25 00:24:17 +0000935
936/// getComplexType - Return the uniqued reference to the type for a complex
937/// number with the specified element type.
938QualType ASTContext::getComplexType(QualType T) {
939 // Unique pointers, to guarantee there is only one pointer of a particular
940 // structure.
941 llvm::FoldingSetNodeID ID;
942 ComplexType::Profile(ID, T);
943
944 void *InsertPos = 0;
945 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
946 return QualType(CT, 0);
947
948 // If the pointee type isn't canonical, this won't be a canonical type either,
949 // so fill in the canonical type field.
950 QualType Canonical;
951 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000952 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000953
954 // Get the new insert position for the node we care about.
955 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000956 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000957 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000958 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000959 Types.push_back(New);
960 ComplexTypes.InsertNode(New, InsertPos);
961 return QualType(New, 0);
962}
963
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000964QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
965 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
966 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
967 FixedWidthIntType *&Entry = Map[Width];
968 if (!Entry)
969 Entry = new FixedWidthIntType(Width, Signed);
970 return QualType(Entry, 0);
971}
Chris Lattner4b009652007-07-25 00:24:17 +0000972
973/// getPointerType - Return the uniqued reference to the type for a pointer to
974/// the specified type.
975QualType ASTContext::getPointerType(QualType T) {
976 // Unique pointers, to guarantee there is only one pointer of a particular
977 // structure.
978 llvm::FoldingSetNodeID ID;
979 PointerType::Profile(ID, T);
980
981 void *InsertPos = 0;
982 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
983 return QualType(PT, 0);
984
985 // If the pointee type isn't canonical, this won't be a canonical type either,
986 // so fill in the canonical type field.
987 QualType Canonical;
988 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000989 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000990
991 // Get the new insert position for the node we care about.
992 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000993 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000994 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000995 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000996 Types.push_back(New);
997 PointerTypes.InsertNode(New, InsertPos);
998 return QualType(New, 0);
999}
1000
Steve Naroff7aa54752008-08-27 16:04:49 +00001001/// getBlockPointerType - Return the uniqued reference to the type for
1002/// a pointer to the specified block.
1003QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +00001004 assert(T->isFunctionType() && "block of function types only");
1005 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +00001006 // structure.
1007 llvm::FoldingSetNodeID ID;
1008 BlockPointerType::Profile(ID, T);
1009
1010 void *InsertPos = 0;
1011 if (BlockPointerType *PT =
1012 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1013 return QualType(PT, 0);
1014
Steve Narofffd5b19d2008-08-28 19:20:44 +00001015 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +00001016 // type either so fill in the canonical type field.
1017 QualType Canonical;
1018 if (!T->isCanonical()) {
1019 Canonical = getBlockPointerType(getCanonicalType(T));
1020
1021 // Get the new insert position for the node we care about.
1022 BlockPointerType *NewIP =
1023 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001024 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +00001025 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001026 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +00001027 Types.push_back(New);
1028 BlockPointerTypes.InsertNode(New, InsertPos);
1029 return QualType(New, 0);
1030}
1031
Sebastian Redlce6fff02009-03-16 23:22:08 +00001032/// getLValueReferenceType - Return the uniqued reference to the type for an
1033/// lvalue reference to the specified type.
1034QualType ASTContext::getLValueReferenceType(QualType T) {
Chris Lattner4b009652007-07-25 00:24:17 +00001035 // Unique pointers, to guarantee there is only one pointer of a particular
1036 // structure.
1037 llvm::FoldingSetNodeID ID;
1038 ReferenceType::Profile(ID, T);
1039
1040 void *InsertPos = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +00001041 if (LValueReferenceType *RT =
1042 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001043 return QualType(RT, 0);
Sebastian Redlce6fff02009-03-16 23:22:08 +00001044
Chris Lattner4b009652007-07-25 00:24:17 +00001045 // If the referencee type isn't canonical, this won't be a canonical type
1046 // either, so fill in the canonical type field.
1047 QualType Canonical;
1048 if (!T->isCanonical()) {
Sebastian Redlce6fff02009-03-16 23:22:08 +00001049 Canonical = getLValueReferenceType(getCanonicalType(T));
1050
Chris Lattner4b009652007-07-25 00:24:17 +00001051 // Get the new insert position for the node we care about.
Sebastian Redlce6fff02009-03-16 23:22:08 +00001052 LValueReferenceType *NewIP =
1053 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001054 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001055 }
1056
Sebastian Redlce6fff02009-03-16 23:22:08 +00001057 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001058 Types.push_back(New);
Sebastian Redlce6fff02009-03-16 23:22:08 +00001059 LValueReferenceTypes.InsertNode(New, InsertPos);
1060 return QualType(New, 0);
1061}
1062
1063/// getRValueReferenceType - Return the uniqued reference to the type for an
1064/// rvalue reference to the specified type.
1065QualType ASTContext::getRValueReferenceType(QualType T) {
1066 // Unique pointers, to guarantee there is only one pointer of a particular
1067 // structure.
1068 llvm::FoldingSetNodeID ID;
1069 ReferenceType::Profile(ID, T);
1070
1071 void *InsertPos = 0;
1072 if (RValueReferenceType *RT =
1073 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1074 return QualType(RT, 0);
1075
1076 // If the referencee type isn't canonical, this won't be a canonical type
1077 // either, so fill in the canonical type field.
1078 QualType Canonical;
1079 if (!T->isCanonical()) {
1080 Canonical = getRValueReferenceType(getCanonicalType(T));
1081
1082 // Get the new insert position for the node we care about.
1083 RValueReferenceType *NewIP =
1084 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1085 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1086 }
1087
1088 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1089 Types.push_back(New);
1090 RValueReferenceTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001091 return QualType(New, 0);
1092}
1093
Sebastian Redl75555032009-01-24 21:16:55 +00001094/// getMemberPointerType - Return the uniqued reference to the type for a
1095/// member pointer to the specified type, in the specified class.
1096QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1097{
1098 // Unique pointers, to guarantee there is only one pointer of a particular
1099 // structure.
1100 llvm::FoldingSetNodeID ID;
1101 MemberPointerType::Profile(ID, T, Cls);
1102
1103 void *InsertPos = 0;
1104 if (MemberPointerType *PT =
1105 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1106 return QualType(PT, 0);
1107
1108 // If the pointee or class type isn't canonical, this won't be a canonical
1109 // type either, so fill in the canonical type field.
1110 QualType Canonical;
1111 if (!T->isCanonical()) {
1112 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1113
1114 // Get the new insert position for the node we care about.
1115 MemberPointerType *NewIP =
1116 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1117 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1118 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001119 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +00001120 Types.push_back(New);
1121 MemberPointerTypes.InsertNode(New, InsertPos);
1122 return QualType(New, 0);
1123}
1124
Steve Naroff83c13012007-08-30 01:06:46 +00001125/// getConstantArrayType - Return the unique reference to the type for an
1126/// array of the specified element type.
1127QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +00001128 const llvm::APInt &ArySize,
1129 ArrayType::ArraySizeModifier ASM,
1130 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001131 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001132 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001133
1134 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +00001135 if (ConstantArrayType *ATP =
1136 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001137 return QualType(ATP, 0);
1138
1139 // If the element type isn't canonical, this won't be a canonical type either,
1140 // so fill in the canonical type field.
1141 QualType Canonical;
1142 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001143 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +00001144 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001145 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +00001146 ConstantArrayType *NewIP =
1147 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001148 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001149 }
1150
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001151 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001152 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001153 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001154 Types.push_back(New);
1155 return QualType(New, 0);
1156}
1157
Steve Naroffe2579e32007-08-30 18:14:25 +00001158/// getVariableArrayType - Returns a non-unique reference to the type for a
1159/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +00001160QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1161 ArrayType::ArraySizeModifier ASM,
1162 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +00001163 // Since we don't unique expressions, it isn't possible to unique VLA's
1164 // that have an expression provided for their size.
1165
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001166 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001167 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001168
1169 VariableArrayTypes.push_back(New);
1170 Types.push_back(New);
1171 return QualType(New, 0);
1172}
1173
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001174/// getDependentSizedArrayType - Returns a non-unique reference to
1175/// the type for a dependently-sized array of the specified element
1176/// type. FIXME: We will need these to be uniqued, or at least
1177/// comparable, at some point.
1178QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1179 ArrayType::ArraySizeModifier ASM,
1180 unsigned EltTypeQuals) {
1181 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1182 "Size must be type- or value-dependent!");
1183
1184 // Since we don't unique expressions, it isn't possible to unique
1185 // dependently-sized array types.
1186
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001187 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001188 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1189 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001190
1191 DependentSizedArrayTypes.push_back(New);
1192 Types.push_back(New);
1193 return QualType(New, 0);
1194}
1195
Eli Friedman8ff07782008-02-15 18:16:39 +00001196QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1197 ArrayType::ArraySizeModifier ASM,
1198 unsigned EltTypeQuals) {
1199 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001200 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001201
1202 void *InsertPos = 0;
1203 if (IncompleteArrayType *ATP =
1204 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1205 return QualType(ATP, 0);
1206
1207 // If the element type isn't canonical, this won't be a canonical type
1208 // either, so fill in the canonical type field.
1209 QualType Canonical;
1210
1211 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001212 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001213 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001214
1215 // Get the new insert position for the node we care about.
1216 IncompleteArrayType *NewIP =
1217 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001218 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001219 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001220
Steve Naroff93fd2112009-01-27 22:08:43 +00001221 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001222 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001223
1224 IncompleteArrayTypes.InsertNode(New, InsertPos);
1225 Types.push_back(New);
1226 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001227}
1228
Chris Lattner4b009652007-07-25 00:24:17 +00001229/// getVectorType - Return the unique reference to a vector type of
1230/// the specified element type and size. VectorType must be a built-in type.
1231QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1232 BuiltinType *baseType;
1233
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001234 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001235 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1236
1237 // Check if we've already instantiated a vector of this type.
1238 llvm::FoldingSetNodeID ID;
1239 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1240 void *InsertPos = 0;
1241 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1242 return QualType(VTP, 0);
1243
1244 // If the element type isn't canonical, this won't be a canonical type either,
1245 // so fill in the canonical type field.
1246 QualType Canonical;
1247 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001248 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001249
1250 // Get the new insert position for the node we care about.
1251 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001252 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001253 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001254 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001255 VectorTypes.InsertNode(New, InsertPos);
1256 Types.push_back(New);
1257 return QualType(New, 0);
1258}
1259
Nate Begemanaf6ed502008-04-18 23:10:10 +00001260/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001261/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001262QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001263 BuiltinType *baseType;
1264
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001265 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001266 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001267
1268 // Check if we've already instantiated a vector of this type.
1269 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001270 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001271 void *InsertPos = 0;
1272 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1273 return QualType(VTP, 0);
1274
1275 // If the element type isn't canonical, this won't be a canonical type either,
1276 // so fill in the canonical type field.
1277 QualType Canonical;
1278 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001279 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001280
1281 // Get the new insert position for the node we care about.
1282 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001283 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001284 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001285 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001286 VectorTypes.InsertNode(New, InsertPos);
1287 Types.push_back(New);
1288 return QualType(New, 0);
1289}
1290
Douglas Gregor4fa58902009-02-26 23:50:07 +00001291/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001292///
Douglas Gregor4fa58902009-02-26 23:50:07 +00001293QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Chris Lattner4b009652007-07-25 00:24:17 +00001294 // Unique functions, to guarantee there is only one function of a particular
1295 // structure.
1296 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001297 FunctionNoProtoType::Profile(ID, ResultTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001298
1299 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001300 if (FunctionNoProtoType *FT =
1301 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001302 return QualType(FT, 0);
1303
1304 QualType Canonical;
1305 if (!ResultTy->isCanonical()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00001306 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001307
1308 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001309 FunctionNoProtoType *NewIP =
1310 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001311 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001312 }
1313
Douglas Gregor4fa58902009-02-26 23:50:07 +00001314 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001315 Types.push_back(New);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001316 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001317 return QualType(New, 0);
1318}
1319
1320/// getFunctionType - Return a normal function type with a typed argument
1321/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001322QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001323 unsigned NumArgs, bool isVariadic,
Sebastian Redl74942cd2009-04-30 19:20:55 +00001324 unsigned TypeQuals, bool hasExceptionSpec,
1325 bool hasAnyExceptionSpec, unsigned NumExs,
1326 const QualType *ExArray) {
Chris Lattner4b009652007-07-25 00:24:17 +00001327 // Unique functions, to guarantee there is only one function of a particular
1328 // structure.
1329 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001330 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl74942cd2009-04-30 19:20:55 +00001331 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1332 NumExs, ExArray);
Chris Lattner4b009652007-07-25 00:24:17 +00001333
1334 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001335 if (FunctionProtoType *FTP =
1336 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001337 return QualType(FTP, 0);
1338
1339 // Determine whether the type being created is already canonical or not.
1340 bool isCanonical = ResultTy->isCanonical();
1341 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1342 if (!ArgArray[i]->isCanonical())
1343 isCanonical = false;
1344
1345 // If this type isn't canonical, get the canonical version of it.
1346 QualType Canonical;
1347 if (!isCanonical) {
1348 llvm::SmallVector<QualType, 16> CanonicalArgs;
1349 CanonicalArgs.reserve(NumArgs);
1350 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001351 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redl74942cd2009-04-30 19:20:55 +00001352 llvm::SmallVector<QualType, 2> CanonicalExs;
1353 CanonicalExs.reserve(NumExs);
1354 for (unsigned i = 0; i != NumExs; ++i)
1355 CanonicalExs.push_back(getCanonicalType(ExArray[i]));
1356
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001357 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001358 &CanonicalArgs[0], NumArgs,
Sebastian Redl74942cd2009-04-30 19:20:55 +00001359 isVariadic, TypeQuals, hasExceptionSpec,
1360 hasAnyExceptionSpec, NumExs, &CanonicalExs[0]);
1361
Chris Lattner4b009652007-07-25 00:24:17 +00001362 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001363 FunctionProtoType *NewIP =
1364 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001365 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001366 }
Sebastian Redl74942cd2009-04-30 19:20:55 +00001367
Douglas Gregor4fa58902009-02-26 23:50:07 +00001368 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl74942cd2009-04-30 19:20:55 +00001369 // for two variable size arrays (for parameter and exception types) at the
1370 // end of them.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001371 FunctionProtoType *FTP =
Sebastian Redl74942cd2009-04-30 19:20:55 +00001372 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1373 NumArgs*sizeof(QualType) +
1374 NumExs*sizeof(QualType), 8);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001375 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl74942cd2009-04-30 19:20:55 +00001376 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1377 ExArray, NumExs, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001378 Types.push_back(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001379 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001380 return QualType(FTP, 0);
1381}
1382
Douglas Gregor1d661552008-04-13 21:07:44 +00001383/// getTypeDeclType - Return the unique reference to the type for the
1384/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001385QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001386 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001387 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1388
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001389 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001390 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001391 else if (isa<TemplateTypeParmDecl>(Decl)) {
1392 assert(false && "Template type parameter types are always available.");
1393 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001394 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001395
Douglas Gregor2e047592009-02-28 01:32:25 +00001396 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001397 if (PrevDecl)
1398 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001399 else
1400 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001401 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001402 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1403 if (PrevDecl)
1404 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001405 else
1406 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001407 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001408 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001409 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001410
Ted Kremenek46a837c2008-09-05 17:16:31 +00001411 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001412 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001413}
1414
Chris Lattner4b009652007-07-25 00:24:17 +00001415/// getTypedefType - Return the unique reference to the type for the
1416/// specified typename decl.
1417QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1418 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1419
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001420 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001421 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001422 Types.push_back(Decl->TypeForDecl);
1423 return QualType(Decl->TypeForDecl, 0);
1424}
1425
Ted Kremenek42730c52008-01-07 19:49:32 +00001426/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001427/// specified ObjC interface decl.
Daniel Dunbarbe1ff272009-04-22 04:34:53 +00001428QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001429 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1430
Daniel Dunbarbe1ff272009-04-22 04:34:53 +00001431 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1432 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001433 Types.push_back(Decl->TypeForDecl);
1434 return QualType(Decl->TypeForDecl, 0);
1435}
1436
Douglas Gregora4918772009-02-05 23:33:38 +00001437/// \brief Retrieve the template type parameter type for a template
1438/// parameter with the given depth, index, and (optionally) name.
1439QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1440 IdentifierInfo *Name) {
1441 llvm::FoldingSetNodeID ID;
1442 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1443 void *InsertPos = 0;
1444 TemplateTypeParmType *TypeParm
1445 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1446
1447 if (TypeParm)
1448 return QualType(TypeParm, 0);
1449
1450 if (Name)
1451 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1452 getTemplateTypeParmType(Depth, Index));
1453 else
1454 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1455
1456 Types.push_back(TypeParm);
1457 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1458
1459 return QualType(TypeParm, 0);
1460}
1461
Douglas Gregor8e458f42009-02-09 18:46:07 +00001462QualType
Douglas Gregordd13e842009-03-30 22:58:21 +00001463ASTContext::getTemplateSpecializationType(TemplateName Template,
1464 const TemplateArgument *Args,
1465 unsigned NumArgs,
1466 QualType Canon) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001467 if (!Canon.isNull())
1468 Canon = getCanonicalType(Canon);
Douglas Gregor9c7825b2009-02-26 22:19:44 +00001469
Douglas Gregor8e458f42009-02-09 18:46:07 +00001470 llvm::FoldingSetNodeID ID;
Douglas Gregordd13e842009-03-30 22:58:21 +00001471 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001472
Douglas Gregor8e458f42009-02-09 18:46:07 +00001473 void *InsertPos = 0;
Douglas Gregordd13e842009-03-30 22:58:21 +00001474 TemplateSpecializationType *Spec
1475 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001476
1477 if (Spec)
1478 return QualType(Spec, 0);
1479
Douglas Gregordd13e842009-03-30 22:58:21 +00001480 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001481 sizeof(TemplateArgument) * NumArgs),
1482 8);
Douglas Gregordd13e842009-03-30 22:58:21 +00001483 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001484 Types.push_back(Spec);
Douglas Gregordd13e842009-03-30 22:58:21 +00001485 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001486
1487 return QualType(Spec, 0);
1488}
1489
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001490QualType
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001491ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001492 QualType NamedType) {
1493 llvm::FoldingSetNodeID ID;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001494 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001495
1496 void *InsertPos = 0;
1497 QualifiedNameType *T
1498 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1499 if (T)
1500 return QualType(T, 0);
1501
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001502 T = new (*this) QualifiedNameType(NNS, NamedType,
1503 getCanonicalType(NamedType));
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001504 Types.push_back(T);
1505 QualifiedNameTypes.InsertNode(T, InsertPos);
1506 return QualType(T, 0);
1507}
1508
Douglas Gregord3022602009-03-27 23:10:48 +00001509QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1510 const IdentifierInfo *Name,
1511 QualType Canon) {
1512 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1513
1514 if (Canon.isNull()) {
1515 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1516 if (CanonNNS != NNS)
1517 Canon = getTypenameType(CanonNNS, Name);
1518 }
1519
1520 llvm::FoldingSetNodeID ID;
1521 TypenameType::Profile(ID, NNS, Name);
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, Name, Canon);
1530 Types.push_back(T);
1531 TypenameTypes.InsertNode(T, InsertPos);
1532 return QualType(T, 0);
1533}
1534
Douglas Gregor77da5802009-04-01 00:28:59 +00001535QualType
1536ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1537 const TemplateSpecializationType *TemplateId,
1538 QualType Canon) {
1539 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1540
1541 if (Canon.isNull()) {
1542 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1543 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1544 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1545 const TemplateSpecializationType *CanonTemplateId
1546 = CanonType->getAsTemplateSpecializationType();
1547 assert(CanonTemplateId &&
1548 "Canonical type must also be a template specialization type");
1549 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1550 }
1551 }
1552
1553 llvm::FoldingSetNodeID ID;
1554 TypenameType::Profile(ID, NNS, TemplateId);
1555
1556 void *InsertPos = 0;
1557 TypenameType *T
1558 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1559 if (T)
1560 return QualType(T, 0);
1561
1562 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1563 Types.push_back(T);
1564 TypenameTypes.InsertNode(T, InsertPos);
1565 return QualType(T, 0);
1566}
1567
Chris Lattnere1352302008-04-07 04:56:42 +00001568/// CmpProtocolNames - Comparison predicate for sorting protocols
1569/// alphabetically.
1570static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1571 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001572 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001573}
1574
1575static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1576 unsigned &NumProtocols) {
1577 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1578
1579 // Sort protocols, keyed by name.
1580 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1581
1582 // Remove duplicates.
1583 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1584 NumProtocols = ProtocolsEnd-Protocols;
1585}
1586
1587
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001588/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1589/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001590QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1591 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001592 // Sort the protocol list alphabetically to canonicalize it.
1593 SortAndUniqueProtocols(Protocols, NumProtocols);
1594
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001595 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001596 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001597
1598 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001599 if (ObjCQualifiedInterfaceType *QT =
1600 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001601 return QualType(QT, 0);
1602
1603 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001604 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001605 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001606
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001607 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001608 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001609 return QualType(QType, 0);
1610}
1611
Chris Lattnere1352302008-04-07 04:56:42 +00001612/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1613/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001614QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001615 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001616 // Sort the protocol list alphabetically to canonicalize it.
1617 SortAndUniqueProtocols(Protocols, NumProtocols);
1618
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001619 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001620 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001621
1622 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001623 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001624 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001625 return QualType(QT, 0);
1626
1627 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001628 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001629 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001630 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001631 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001632 return QualType(QType, 0);
1633}
1634
Douglas Gregor4fa58902009-02-26 23:50:07 +00001635/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1636/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff0604dd92007-08-01 18:02:17 +00001637/// multiple declarations that refer to "typeof(x)" all contain different
1638/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1639/// on canonical type's (which are always unique).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001640QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001641 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001642 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001643 Types.push_back(toe);
1644 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001645}
1646
Steve Naroff0604dd92007-08-01 18:02:17 +00001647/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1648/// TypeOfType AST's. The only motivation to unique these nodes would be
1649/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1650/// an issue. This doesn't effect the type checker, since it operates
1651/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001652QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001653 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001654 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001655 Types.push_back(tot);
1656 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001657}
1658
Chris Lattner4b009652007-07-25 00:24:17 +00001659/// getTagDeclType - Return the unique reference to the type for the
1660/// specified TagDecl (struct/union/class/enum) decl.
1661QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001662 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001663 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001664}
1665
1666/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1667/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1668/// needs to agree with the definition in <stddef.h>.
1669QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001670 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001671}
1672
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001673/// getSignedWCharType - Return the type of "signed wchar_t".
1674/// Used when in C++, as a GCC extension.
1675QualType ASTContext::getSignedWCharType() const {
1676 // FIXME: derive from "Target" ?
1677 return WCharTy;
1678}
1679
1680/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1681/// Used when in C++, as a GCC extension.
1682QualType ASTContext::getUnsignedWCharType() const {
1683 // FIXME: derive from "Target" ?
1684 return UnsignedIntTy;
1685}
1686
Chris Lattner4b009652007-07-25 00:24:17 +00001687/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1688/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1689QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001690 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001691}
1692
Chris Lattner19eb97e2008-04-02 05:18:44 +00001693//===----------------------------------------------------------------------===//
1694// Type Operators
1695//===----------------------------------------------------------------------===//
1696
Chris Lattner3dae6f42008-04-06 22:41:35 +00001697/// getCanonicalType - Return the canonical (structural) type corresponding to
1698/// the specified potentially non-canonical type. The non-canonical version
1699/// of a type may have many "decorated" versions of types. Decorators can
1700/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1701/// to be free of any of these, allowing two canonical types to be compared
1702/// for exact equality with a simple pointer comparison.
1703QualType ASTContext::getCanonicalType(QualType T) {
1704 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001705
1706 // If the result has type qualifiers, make sure to canonicalize them as well.
1707 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1708 if (TypeQuals == 0) return CanType;
1709
1710 // If the type qualifiers are on an array type, get the canonical type of the
1711 // array with the qualifiers applied to the element type.
1712 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1713 if (!AT)
1714 return CanType.getQualifiedType(TypeQuals);
1715
1716 // Get the canonical version of the element with the extra qualifiers on it.
1717 // This can recursively sink qualifiers through multiple levels of arrays.
1718 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1719 NewEltTy = getCanonicalType(NewEltTy);
1720
1721 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1722 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1723 CAT->getIndexTypeQualifier());
1724 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1725 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1726 IAT->getIndexTypeQualifier());
1727
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001728 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1729 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1730 DSAT->getSizeModifier(),
1731 DSAT->getIndexTypeQualifier());
1732
Chris Lattnera1923f62008-08-04 07:31:14 +00001733 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1734 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1735 VAT->getSizeModifier(),
1736 VAT->getIndexTypeQualifier());
1737}
1738
Douglas Gregord3022602009-03-27 23:10:48 +00001739NestedNameSpecifier *
1740ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1741 if (!NNS)
1742 return 0;
1743
1744 switch (NNS->getKind()) {
1745 case NestedNameSpecifier::Identifier:
1746 // Canonicalize the prefix but keep the identifier the same.
1747 return NestedNameSpecifier::Create(*this,
1748 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1749 NNS->getAsIdentifier());
1750
1751 case NestedNameSpecifier::Namespace:
1752 // A namespace is canonical; build a nested-name-specifier with
1753 // this namespace and no prefix.
1754 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1755
1756 case NestedNameSpecifier::TypeSpec:
1757 case NestedNameSpecifier::TypeSpecWithTemplate: {
1758 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1759 NestedNameSpecifier *Prefix = 0;
1760
1761 // FIXME: This isn't the right check!
1762 if (T->isDependentType())
1763 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1764
1765 return NestedNameSpecifier::Create(*this, Prefix,
1766 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1767 T.getTypePtr());
1768 }
1769
1770 case NestedNameSpecifier::Global:
1771 // The global specifier is canonical and unique.
1772 return NNS;
1773 }
1774
1775 // Required to silence a GCC warning
1776 return 0;
1777}
1778
Chris Lattnera1923f62008-08-04 07:31:14 +00001779
1780const ArrayType *ASTContext::getAsArrayType(QualType T) {
1781 // Handle the non-qualified case efficiently.
1782 if (T.getCVRQualifiers() == 0) {
1783 // Handle the common positive case fast.
1784 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1785 return AT;
1786 }
1787
1788 // Handle the common negative case fast, ignoring CVR qualifiers.
1789 QualType CType = T->getCanonicalTypeInternal();
1790
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001791 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001792 // test.
1793 if (!isa<ArrayType>(CType) &&
1794 !isa<ArrayType>(CType.getUnqualifiedType()))
1795 return 0;
1796
1797 // Apply any CVR qualifiers from the array type to the element type. This
1798 // implements C99 6.7.3p8: "If the specification of an array type includes
1799 // any type qualifiers, the element type is so qualified, not the array type."
1800
1801 // If we get here, we either have type qualifiers on the type, or we have
1802 // sugar such as a typedef in the way. If we have type qualifiers on the type
1803 // we must propagate them down into the elemeng type.
1804 unsigned CVRQuals = T.getCVRQualifiers();
1805 unsigned AddrSpace = 0;
1806 Type *Ty = T.getTypePtr();
1807
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001808 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001809 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001810 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1811 AddrSpace = EXTQT->getAddressSpace();
1812 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001813 } else {
1814 T = Ty->getDesugaredType();
1815 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1816 break;
1817 CVRQuals |= T.getCVRQualifiers();
1818 Ty = T.getTypePtr();
1819 }
1820 }
1821
1822 // If we have a simple case, just return now.
1823 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1824 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1825 return ATy;
1826
1827 // Otherwise, we have an array and we have qualifiers on it. Push the
1828 // qualifiers into the array element type and return a new array type.
1829 // Get the canonical version of the element with the extra qualifiers on it.
1830 // This can recursively sink qualifiers through multiple levels of arrays.
1831 QualType NewEltTy = ATy->getElementType();
1832 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001833 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001834 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1835
1836 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1837 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1838 CAT->getSizeModifier(),
1839 CAT->getIndexTypeQualifier()));
1840 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1841 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1842 IAT->getSizeModifier(),
1843 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001844
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001845 if (const DependentSizedArrayType *DSAT
1846 = dyn_cast<DependentSizedArrayType>(ATy))
1847 return cast<ArrayType>(
1848 getDependentSizedArrayType(NewEltTy,
1849 DSAT->getSizeExpr(),
1850 DSAT->getSizeModifier(),
1851 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001852
Chris Lattnera1923f62008-08-04 07:31:14 +00001853 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1854 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1855 VAT->getSizeModifier(),
1856 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001857}
1858
1859
Chris Lattner19eb97e2008-04-02 05:18:44 +00001860/// getArrayDecayedType - Return the properly qualified result of decaying the
1861/// specified array type to a pointer. This operation is non-trivial when
1862/// handling typedefs etc. The canonical type of "T" must be an array type,
1863/// this returns a pointer to a properly qualified element of the array.
1864///
1865/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1866QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001867 // Get the element type with 'getAsArrayType' so that we don't lose any
1868 // typedefs in the element type of the array. This also handles propagation
1869 // of type qualifiers from the array type into the element type if present
1870 // (C99 6.7.3p8).
1871 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1872 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001873
Chris Lattnera1923f62008-08-04 07:31:14 +00001874 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001875
1876 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001877 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001878}
1879
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001880QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001881 QualType ElemTy = VAT->getElementType();
1882
1883 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1884 return getBaseElementType(VAT);
1885
1886 return ElemTy;
1887}
1888
Chris Lattner4b009652007-07-25 00:24:17 +00001889/// getFloatingRank - Return a relative rank for floating point types.
1890/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001891static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001892 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001893 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001894
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001895 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001896 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001897 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001898 case BuiltinType::Float: return FloatRank;
1899 case BuiltinType::Double: return DoubleRank;
1900 case BuiltinType::LongDouble: return LongDoubleRank;
1901 }
1902}
1903
Steve Narofffa0c4532007-08-27 01:41:48 +00001904/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1905/// point or a complex type (based on typeDomain/typeSize).
1906/// 'typeDomain' is a real floating point or complex type.
1907/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001908QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1909 QualType Domain) const {
1910 FloatingRank EltRank = getFloatingRank(Size);
1911 if (Domain->isComplexType()) {
1912 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001913 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001914 case FloatRank: return FloatComplexTy;
1915 case DoubleRank: return DoubleComplexTy;
1916 case LongDoubleRank: return LongDoubleComplexTy;
1917 }
Chris Lattner4b009652007-07-25 00:24:17 +00001918 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001919
1920 assert(Domain->isRealFloatingType() && "Unknown domain!");
1921 switch (EltRank) {
1922 default: assert(0 && "getFloatingRank(): illegal value for rank");
1923 case FloatRank: return FloatTy;
1924 case DoubleRank: return DoubleTy;
1925 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001926 }
Chris Lattner4b009652007-07-25 00:24:17 +00001927}
1928
Chris Lattner51285d82008-04-06 23:55:33 +00001929/// getFloatingTypeOrder - Compare the rank of the two specified floating
1930/// point types, ignoring the domain of the type (i.e. 'double' ==
1931/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1932/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001933int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1934 FloatingRank LHSR = getFloatingRank(LHS);
1935 FloatingRank RHSR = getFloatingRank(RHS);
1936
1937 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001938 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001939 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001940 return 1;
1941 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001942}
1943
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001944/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1945/// routine will assert if passed a built-in type that isn't an integer or enum,
1946/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001947unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001948 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001949 if (EnumType* ET = dyn_cast<EnumType>(T))
1950 T = ET->getDecl()->getIntegerType().getTypePtr();
1951
1952 // There are two things which impact the integer rank: the width, and
1953 // the ordering of builtins. The builtin ordering is encoded in the
1954 // bottom three bits; the width is encoded in the bits above that.
1955 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1956 return FWIT->getWidth() << 3;
1957 }
1958
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001959 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001960 default: assert(0 && "getIntegerRank(): not a built-in integer");
1961 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001962 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001963 case BuiltinType::Char_S:
1964 case BuiltinType::Char_U:
1965 case BuiltinType::SChar:
1966 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001967 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001968 case BuiltinType::Short:
1969 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001970 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001971 case BuiltinType::Int:
1972 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001973 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001974 case BuiltinType::Long:
1975 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001976 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001977 case BuiltinType::LongLong:
1978 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001979 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner6cc7e412009-04-30 02:43:43 +00001980 case BuiltinType::Int128:
1981 case BuiltinType::UInt128:
1982 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001983 }
1984}
1985
Chris Lattner51285d82008-04-06 23:55:33 +00001986/// getIntegerTypeOrder - Returns the highest ranked integer type:
1987/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1988/// LHS < RHS, return -1.
1989int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001990 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1991 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001992 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001993
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001994 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1995 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001996
Chris Lattner51285d82008-04-06 23:55:33 +00001997 unsigned LHSRank = getIntegerRank(LHSC);
1998 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001999
Chris Lattner51285d82008-04-06 23:55:33 +00002000 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2001 if (LHSRank == RHSRank) return 0;
2002 return LHSRank > RHSRank ? 1 : -1;
2003 }
Chris Lattner4b009652007-07-25 00:24:17 +00002004
Chris Lattner51285d82008-04-06 23:55:33 +00002005 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2006 if (LHSUnsigned) {
2007 // If the unsigned [LHS] type is larger, return it.
2008 if (LHSRank >= RHSRank)
2009 return 1;
2010
2011 // If the signed type can represent all values of the unsigned type, it
2012 // wins. Because we are dealing with 2's complement and types that are
2013 // powers of two larger than each other, this is always safe.
2014 return -1;
2015 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00002016
Chris Lattner51285d82008-04-06 23:55:33 +00002017 // If the unsigned [RHS] type is larger, return it.
2018 if (RHSRank >= LHSRank)
2019 return -1;
2020
2021 // If the signed type can represent all values of the unsigned type, it
2022 // wins. Because we are dealing with 2's complement and types that are
2023 // powers of two larger than each other, this is always safe.
2024 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00002025}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002026
2027// getCFConstantStringType - Return the type used for constant CFStrings.
2028QualType ASTContext::getCFConstantStringType() {
2029 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00002030 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00002031 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00002032 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002033 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002034
2035 // const int *isa;
2036 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002037 // int flags;
2038 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002039 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002040 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002041 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002042 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00002043
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002044 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00002045 for (unsigned i = 0; i < 4; ++i) {
2046 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2047 SourceLocation(), 0,
2048 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002049 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002050 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002051 }
2052
2053 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002054 }
2055
2056 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00002057}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002058
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002059void ASTContext::setCFConstantStringType(QualType T) {
2060 const RecordType *Rec = T->getAsRecordType();
2061 assert(Rec && "Invalid CFConstantStringType");
2062 CFConstantStringTypeDecl = Rec->getDecl();
2063}
2064
Anders Carlssonf58cac72008-08-30 19:34:46 +00002065QualType ASTContext::getObjCFastEnumerationStateType()
2066{
2067 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00002068 ObjCFastEnumerationStateTypeDecl =
2069 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2070 &Idents.get("__objcFastEnumerationState"));
2071
Anders Carlssonf58cac72008-08-30 19:34:46 +00002072 QualType FieldTypes[] = {
2073 UnsignedLongTy,
2074 getPointerType(ObjCIdType),
2075 getPointerType(UnsignedLongTy),
2076 getConstantArrayType(UnsignedLongTy,
2077 llvm::APInt(32, 5), ArrayType::Normal, 0)
2078 };
2079
Douglas Gregor8acb7272008-12-11 16:49:14 +00002080 for (size_t i = 0; i < 4; ++i) {
2081 FieldDecl *Field = FieldDecl::Create(*this,
2082 ObjCFastEnumerationStateTypeDecl,
2083 SourceLocation(), 0,
2084 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002085 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002086 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002087 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00002088
Douglas Gregor8acb7272008-12-11 16:49:14 +00002089 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00002090 }
2091
2092 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2093}
2094
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002095void ASTContext::setObjCFastEnumerationStateType(QualType T) {
2096 const RecordType *Rec = T->getAsRecordType();
2097 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2098 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2099}
2100
Anders Carlssone3f02572007-10-29 06:33:42 +00002101// This returns true if a type has been typedefed to BOOL:
2102// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00002103static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002104 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00002105 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2106 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002107
2108 return false;
2109}
2110
Ted Kremenek42730c52008-01-07 19:49:32 +00002111/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002112/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00002113int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00002114 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002115
2116 // Make all integer and enum types at least as large as an int
2117 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002118 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002119 // Treat arrays as pointers, since that's how they're passed in.
2120 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002121 sz = getTypeSize(VoidPtrTy);
2122 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002123}
2124
Ted Kremenek42730c52008-01-07 19:49:32 +00002125/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002126/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002127void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00002128 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002129 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002130 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00002131 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002132 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002133 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002134 // Compute size of all parameters.
2135 // Start with computing size of a pointer in number of bytes.
2136 // FIXME: There might(should) be a better way of doing this computation!
2137 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00002138 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002139 // The first two arguments (self and _cmd) are pointers; account for
2140 // their size.
2141 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002142 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2143 E = Decl->param_end(); PI != E; ++PI) {
2144 QualType PType = (*PI)->getType();
2145 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00002146 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002147 ParmOffset += sz;
2148 }
2149 S += llvm::utostr(ParmOffset);
2150 S += "@0:";
2151 S += llvm::utostr(PtrSize);
2152
2153 // Argument types.
2154 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002155 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2156 E = Decl->param_end(); PI != E; ++PI) {
2157 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002158 QualType PType = PVDecl->getOriginalType();
2159 if (const ArrayType *AT =
Steve Naroff78380fb2009-04-14 00:03:58 +00002160 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2161 // Use array's original type only if it has known number of
2162 // elements.
Steve Naroff6777bf32009-04-14 00:40:09 +00002163 if (!isa<ConstantArrayType>(AT))
Steve Naroff78380fb2009-04-14 00:03:58 +00002164 PType = PVDecl->getType();
2165 } else if (PType->isFunctionType())
2166 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002167 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002168 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002169 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002170 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002171 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00002172 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002173 }
2174}
2175
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002176/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002177/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002178/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2179/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002180/// Property attributes are stored as a comma-delimited C string. The simple
2181/// attributes readonly and bycopy are encoded as single characters. The
2182/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2183/// encoded as single characters, followed by an identifier. Property types
2184/// are also encoded as a parametrized attribute. The characters used to encode
2185/// these attributes are defined by the following enumeration:
2186/// @code
2187/// enum PropertyAttributes {
2188/// kPropertyReadOnly = 'R', // property is read-only.
2189/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2190/// kPropertyByref = '&', // property is a reference to the value last assigned
2191/// kPropertyDynamic = 'D', // property is dynamic
2192/// kPropertyGetter = 'G', // followed by getter selector name
2193/// kPropertySetter = 'S', // followed by setter selector name
2194/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2195/// kPropertyType = 't' // followed by old-style type encoding.
2196/// kPropertyWeak = 'W' // 'weak' property
2197/// kPropertyStrong = 'P' // property GC'able
2198/// kPropertyNonAtomic = 'N' // property non-atomic
2199/// };
2200/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002201void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2202 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00002203 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002204 // Collect information from the property implementation decl(s).
2205 bool Dynamic = false;
2206 ObjCPropertyImplDecl *SynthesizePID = 0;
2207
2208 // FIXME: Duplicated code due to poor abstraction.
2209 if (Container) {
2210 if (const ObjCCategoryImplDecl *CID =
2211 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2212 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregorcd19b572009-04-23 01:02:12 +00002213 i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2214 i != e; ++i) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002215 ObjCPropertyImplDecl *PID = *i;
2216 if (PID->getPropertyDecl() == PD) {
2217 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2218 Dynamic = true;
2219 } else {
2220 SynthesizePID = PID;
2221 }
2222 }
2223 }
2224 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002225 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002226 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregorcd19b572009-04-23 01:02:12 +00002227 i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2228 i != e; ++i) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002229 ObjCPropertyImplDecl *PID = *i;
2230 if (PID->getPropertyDecl() == PD) {
2231 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2232 Dynamic = true;
2233 } else {
2234 SynthesizePID = PID;
2235 }
2236 }
2237 }
2238 }
2239 }
2240
2241 // FIXME: This is not very efficient.
2242 S = "T";
2243
2244 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002245 // GCC has some special rules regarding encoding of properties which
2246 // closely resembles encoding of ivars.
Daniel Dunbar701c8502009-04-20 06:37:24 +00002247 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002248 true /* outermost type */,
2249 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002250
2251 if (PD->isReadOnly()) {
2252 S += ",R";
2253 } else {
2254 switch (PD->getSetterKind()) {
2255 case ObjCPropertyDecl::Assign: break;
2256 case ObjCPropertyDecl::Copy: S += ",C"; break;
2257 case ObjCPropertyDecl::Retain: S += ",&"; break;
2258 }
2259 }
2260
2261 // It really isn't clear at all what this means, since properties
2262 // are "dynamic by default".
2263 if (Dynamic)
2264 S += ",D";
2265
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002266 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2267 S += ",N";
2268
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002269 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2270 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002271 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002272 }
2273
2274 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2275 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002276 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002277 }
2278
2279 if (SynthesizePID) {
2280 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2281 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002282 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002283 }
2284
2285 // FIXME: OBJCGC: weak & strong
2286}
2287
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002288/// getLegacyIntegralTypeEncoding -
2289/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002290/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002291/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2292///
2293void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2294 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2295 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002296 if (BT->getKind() == BuiltinType::ULong &&
2297 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002298 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002299 else
2300 if (BT->getKind() == BuiltinType::Long &&
2301 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002302 PointeeTy = IntTy;
2303 }
2304 }
2305}
2306
Fariborz Jahanian248db262008-01-22 22:44:46 +00002307void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002308 const FieldDecl *Field) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002309 // We follow the behavior of gcc, expanding structures which are
2310 // directly pointed to, and expanding embedded structures. Note that
2311 // these rules are sufficient to prevent recursive encoding of the
2312 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002313 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2314 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002315}
2316
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002317static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002318 const FieldDecl *FD) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002319 const Expr *E = FD->getBitWidth();
2320 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2321 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman5255e7a2009-04-26 19:19:15 +00002322 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002323 S += 'b';
2324 S += llvm::utostr(N);
2325}
2326
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002327void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2328 bool ExpandPointedToStructures,
2329 bool ExpandStructures,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002330 const FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002331 bool OutermostType,
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002332 bool EncodingProperty) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002333 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002334 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002335 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002336 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002337 else {
2338 char encoding;
2339 switch (BT->getKind()) {
2340 default: assert(0 && "Unhandled builtin type kind");
2341 case BuiltinType::Void: encoding = 'v'; break;
2342 case BuiltinType::Bool: encoding = 'B'; break;
2343 case BuiltinType::Char_U:
2344 case BuiltinType::UChar: encoding = 'C'; break;
2345 case BuiltinType::UShort: encoding = 'S'; break;
2346 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002347 case BuiltinType::ULong:
2348 encoding =
2349 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2350 break;
Chris Lattner6cc7e412009-04-30 02:43:43 +00002351 case BuiltinType::UInt128: encoding = 'T'; break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002352 case BuiltinType::ULongLong: encoding = 'Q'; break;
2353 case BuiltinType::Char_S:
2354 case BuiltinType::SChar: encoding = 'c'; break;
2355 case BuiltinType::Short: encoding = 's'; break;
2356 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002357 case BuiltinType::Long:
2358 encoding =
2359 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2360 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002361 case BuiltinType::LongLong: encoding = 'q'; break;
Chris Lattner6cc7e412009-04-30 02:43:43 +00002362 case BuiltinType::Int128: encoding = 't'; break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002363 case BuiltinType::Float: encoding = 'f'; break;
2364 case BuiltinType::Double: encoding = 'd'; break;
2365 case BuiltinType::LongDouble: encoding = 'd'; break;
2366 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002367
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002368 S += encoding;
2369 }
Anders Carlsson70e16dd2009-04-09 21:55:45 +00002370 } else if (const ComplexType *CT = T->getAsComplexType()) {
2371 S += 'j';
2372 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2373 false);
2374 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002375 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2376 ExpandPointedToStructures,
2377 ExpandStructures, FD);
2378 if (FD || EncodingProperty) {
2379 // Note that we do extended encoding of protocol qualifer list
2380 // Only when doing ivar or property encoding.
2381 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2382 S += '"';
2383 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2384 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2385 S += '<';
2386 S += Proto->getNameAsString();
2387 S += '>';
2388 }
2389 S += '"';
2390 }
2391 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002392 }
2393 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002394 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002395 bool isReadOnly = false;
2396 // For historical/compatibility reasons, the read-only qualifier of the
2397 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2398 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2399 // Also, do not emit the 'r' for anything but the outermost type!
2400 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2401 if (OutermostType && T.isConstQualified()) {
2402 isReadOnly = true;
2403 S += 'r';
2404 }
2405 }
2406 else if (OutermostType) {
2407 QualType P = PointeeTy;
2408 while (P->getAsPointerType())
2409 P = P->getAsPointerType()->getPointeeType();
2410 if (P.isConstQualified()) {
2411 isReadOnly = true;
2412 S += 'r';
2413 }
2414 }
2415 if (isReadOnly) {
2416 // Another legacy compatibility encoding. Some ObjC qualifier and type
2417 // combinations need to be rearranged.
2418 // Rewrite "in const" from "nr" to "rn"
2419 const char * s = S.c_str();
2420 int len = S.length();
2421 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2422 std::string replace = "rn";
2423 S.replace(S.end()-2, S.end(), replace);
2424 }
2425 }
Steve Naroff17c03822009-02-12 17:52:19 +00002426 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002427 S += '@';
2428 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002429 }
2430 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002431 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002432 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002433 // Another historical/compatibility reason.
2434 // We encode the underlying type which comes out as
2435 // {...};
2436 S += '^';
2437 getObjCEncodingForTypeImpl(PointeeTy, S,
2438 false, ExpandPointedToStructures,
2439 NULL);
2440 return;
2441 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002442 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002443 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002444 const ObjCInterfaceType *OIT =
2445 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002446 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002447 S += '"';
2448 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002449 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2450 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2451 S += '<';
2452 S += Proto->getNameAsString();
2453 S += '>';
2454 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002455 S += '"';
2456 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002457 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002458 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002459 S += '#';
2460 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002461 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002462 S += ':';
2463 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002464 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002465
2466 if (PointeeTy->isCharType()) {
2467 // char pointer types should be encoded as '*' unless it is a
2468 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002469 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002470 S += '*';
2471 return;
2472 }
2473 }
2474
2475 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002476 getLegacyIntegralTypeEncoding(PointeeTy);
2477
2478 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002479 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002480 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002481 } else if (const ArrayType *AT =
2482 // Ignore type qualifiers etc.
2483 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002484 if (isa<IncompleteArrayType>(AT)) {
2485 // Incomplete arrays are encoded as a pointer to the array element.
2486 S += '^';
2487
2488 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2489 false, ExpandStructures, FD);
2490 } else {
2491 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002492
Anders Carlsson858c64d2009-02-22 01:38:57 +00002493 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2494 S += llvm::utostr(CAT->getSize().getZExtValue());
2495 else {
2496 //Variable length arrays are encoded as a regular array with 0 elements.
2497 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2498 S += '0';
2499 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002500
Anders Carlsson858c64d2009-02-22 01:38:57 +00002501 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2502 false, ExpandStructures, FD);
2503 S += ']';
2504 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002505 } else if (T->getAsFunctionType()) {
2506 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002507 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002508 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002509 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002510 // Anonymous structures print as '?'
2511 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2512 S += II->getName();
2513 } else {
2514 S += '?';
2515 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002516 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002517 S += '=';
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002518 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2519 FieldEnd = RDecl->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002520 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002521 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002522 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002523 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002524 S += '"';
2525 }
2526
2527 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002528 if (Field->isBitField()) {
2529 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2530 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002531 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002532 QualType qt = Field->getType();
2533 getLegacyIntegralTypeEncoding(qt);
2534 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002535 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002536 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002537 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002538 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002539 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002540 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002541 if (FD && FD->isBitField())
2542 EncodeBitField(this, S, FD);
2543 else
2544 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002545 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002546 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002547 } else if (T->isObjCInterfaceType()) {
2548 // @encode(class_name)
2549 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2550 S += '{';
2551 const IdentifierInfo *II = OI->getIdentifier();
2552 S += II->getName();
2553 S += '=';
Chris Lattner9329cf52009-03-31 08:48:01 +00002554 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002555 CollectObjCIvars(OI, RecFields);
Chris Lattner9329cf52009-03-31 08:48:01 +00002556 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002557 if (RecFields[i]->isBitField())
2558 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2559 RecFields[i]);
2560 else
2561 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2562 FD);
2563 }
2564 S += '}';
2565 }
2566 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002567 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002568}
2569
Ted Kremenek42730c52008-01-07 19:49:32 +00002570void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002571 std::string& S) const {
2572 if (QT & Decl::OBJC_TQ_In)
2573 S += 'n';
2574 if (QT & Decl::OBJC_TQ_Inout)
2575 S += 'N';
2576 if (QT & Decl::OBJC_TQ_Out)
2577 S += 'o';
2578 if (QT & Decl::OBJC_TQ_Bycopy)
2579 S += 'O';
2580 if (QT & Decl::OBJC_TQ_Byref)
2581 S += 'R';
2582 if (QT & Decl::OBJC_TQ_Oneway)
2583 S += 'V';
2584}
2585
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002586void ASTContext::setBuiltinVaListType(QualType T)
2587{
2588 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2589
2590 BuiltinVaListType = T;
2591}
2592
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002593void ASTContext::setObjCIdType(QualType T)
Steve Naroff9d12c902007-10-15 14:41:52 +00002594{
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002595 ObjCIdType = T;
2596
2597 const TypedefType *TT = T->getAsTypedefType();
2598 if (!TT)
2599 return;
2600
2601 TypedefDecl *TD = TT->getDecl();
Steve Naroff9d12c902007-10-15 14:41:52 +00002602
2603 // typedef struct objc_object *id;
2604 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002605 // User error - caller will issue diagnostics.
2606 if (!ptr)
2607 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002608 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002609 // User error - caller will issue diagnostics.
2610 if (!rec)
2611 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002612 IdStructType = rec;
2613}
2614
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002615void ASTContext::setObjCSelType(QualType T)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002616{
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002617 ObjCSelType = T;
2618
2619 const TypedefType *TT = T->getAsTypedefType();
2620 if (!TT)
2621 return;
2622 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002623
2624 // typedef struct objc_selector *SEL;
2625 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002626 if (!ptr)
2627 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002628 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002629 if (!rec)
2630 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002631 SelStructType = rec;
2632}
2633
Ted Kremenek42730c52008-01-07 19:49:32 +00002634void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002635{
Ted Kremenek42730c52008-01-07 19:49:32 +00002636 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002637}
2638
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002639void ASTContext::setObjCClassType(QualType T)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002640{
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002641 ObjCClassType = T;
2642
2643 const TypedefType *TT = T->getAsTypedefType();
2644 if (!TT)
2645 return;
2646 TypedefDecl *TD = TT->getDecl();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002647
2648 // typedef struct objc_class *Class;
2649 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2650 assert(ptr && "'Class' incorrectly typed");
2651 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2652 assert(rec && "'Class' incorrectly typed");
2653 ClassStructType = rec;
2654}
2655
Ted Kremenek42730c52008-01-07 19:49:32 +00002656void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2657 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002658 "'NSConstantString' type already set!");
2659
Ted Kremenek42730c52008-01-07 19:49:32 +00002660 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002661}
2662
Douglas Gregordd13e842009-03-30 22:58:21 +00002663/// \brief Retrieve the template name that represents a qualified
2664/// template name such as \c std::vector.
2665TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2666 bool TemplateKeyword,
2667 TemplateDecl *Template) {
2668 llvm::FoldingSetNodeID ID;
2669 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2670
2671 void *InsertPos = 0;
2672 QualifiedTemplateName *QTN =
2673 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2674 if (!QTN) {
2675 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2676 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2677 }
2678
2679 return TemplateName(QTN);
2680}
2681
2682/// \brief Retrieve the template name that represents a dependent
2683/// template name such as \c MetaFun::template apply.
2684TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2685 const IdentifierInfo *Name) {
2686 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2687
2688 llvm::FoldingSetNodeID ID;
2689 DependentTemplateName::Profile(ID, NNS, Name);
2690
2691 void *InsertPos = 0;
2692 DependentTemplateName *QTN =
2693 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2694
2695 if (QTN)
2696 return TemplateName(QTN);
2697
2698 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2699 if (CanonNNS == NNS) {
2700 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2701 } else {
2702 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2703 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2704 }
2705
2706 DependentTemplateNames.InsertNode(QTN, InsertPos);
2707 return TemplateName(QTN);
2708}
2709
Douglas Gregorc6507e42008-11-03 14:12:49 +00002710/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002711/// TargetInfo, produce the corresponding type. The unsigned @p Type
2712/// is actually a value of type @c TargetInfo::IntType.
2713QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002714 switch (Type) {
2715 case TargetInfo::NoInt: return QualType();
2716 case TargetInfo::SignedShort: return ShortTy;
2717 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2718 case TargetInfo::SignedInt: return IntTy;
2719 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2720 case TargetInfo::SignedLong: return LongTy;
2721 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2722 case TargetInfo::SignedLongLong: return LongLongTy;
2723 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2724 }
2725
2726 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002727 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002728}
Ted Kremenek118930e2008-07-24 23:58:27 +00002729
2730//===----------------------------------------------------------------------===//
2731// Type Predicates.
2732//===----------------------------------------------------------------------===//
2733
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002734/// isObjCNSObjectType - Return true if this is an NSObject object using
2735/// NSObject attribute on a c-style pointer type.
2736/// FIXME - Make it work directly on types.
2737///
2738bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2739 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2740 if (TypedefDecl *TD = TDT->getDecl())
2741 if (TD->getAttr<ObjCNSObjectAttr>())
2742 return true;
2743 }
2744 return false;
2745}
2746
Ted Kremenek118930e2008-07-24 23:58:27 +00002747/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2748/// to an object type. This includes "id" and "Class" (two 'special' pointers
2749/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2750/// ID type).
2751bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroff6805fc42009-02-23 18:36:16 +00002752 if (Ty->isObjCQualifiedIdType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002753 return true;
2754
Steve Naroffd9e00802008-10-21 18:24:04 +00002755 // Blocks are objects.
2756 if (Ty->isBlockPointerType())
2757 return true;
2758
2759 // All other object types are pointers.
Chris Lattnera008d172009-04-12 23:51:02 +00002760 const PointerType *PT = Ty->getAsPointerType();
2761 if (PT == 0)
Ted Kremenek118930e2008-07-24 23:58:27 +00002762 return false;
2763
Chris Lattnera008d172009-04-12 23:51:02 +00002764 // If this a pointer to an interface (e.g. NSString*), it is ok.
2765 if (PT->getPointeeType()->isObjCInterfaceType() ||
2766 // If is has NSObject attribute, OK as well.
2767 isObjCNSObjectType(Ty))
2768 return true;
2769
Ted Kremenek118930e2008-07-24 23:58:27 +00002770 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2771 // pointer types. This looks for the typedef specifically, not for the
Chris Lattnera008d172009-04-12 23:51:02 +00002772 // underlying type. Iteratively strip off typedefs so that we can handle
2773 // typedefs of typedefs.
2774 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2775 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2776 Ty.getUnqualifiedType() == getObjCClassType())
2777 return true;
2778
2779 Ty = TDT->getDecl()->getUnderlyingType();
2780 }
Ted Kremenek118930e2008-07-24 23:58:27 +00002781
Chris Lattnera008d172009-04-12 23:51:02 +00002782 return false;
Ted Kremenek118930e2008-07-24 23:58:27 +00002783}
2784
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002785/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2786/// garbage collection attribute.
2787///
2788QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002789 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002790 if (getLangOptions().ObjC1 &&
2791 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002792 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002793 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002794 // (or pointers to them) be treated as though they were declared
2795 // as __strong.
2796 if (GCAttrs == QualType::GCNone) {
2797 if (isObjCObjectPointerType(Ty))
2798 GCAttrs = QualType::Strong;
2799 else if (Ty->isPointerType())
2800 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2801 }
Fariborz Jahaniand7b01972009-04-11 00:00:54 +00002802 // Non-pointers have none gc'able attribute regardless of the attribute
2803 // set on them.
2804 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2805 return QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002806 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002807 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002808}
2809
Chris Lattner6ff358b2008-04-07 06:51:04 +00002810//===----------------------------------------------------------------------===//
2811// Type Compatibility Testing
2812//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002813
Steve Naroff3454b6c2008-09-04 15:10:53 +00002814/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002815/// block types. Types must be strictly compatible here. For example,
2816/// C unfortunately doesn't produce an error for the following:
2817///
2818/// int (*emptyArgFunc)();
2819/// int (*intArgList)(int) = emptyArgFunc;
2820///
2821/// For blocks, we will produce an error for the following (similar to C++):
2822///
2823/// int (^emptyArgBlock)();
2824/// int (^intArgBlock)(int) = emptyArgBlock;
2825///
2826/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2827///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002828bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002829 const FunctionType *lbase = lhs->getAsFunctionType();
2830 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002831 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2832 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stump896ceed2009-04-01 01:17:39 +00002833 if (lproto && rproto == 0)
2834 return false;
2835 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff3454b6c2008-09-04 15:10:53 +00002836}
2837
Chris Lattner6ff358b2008-04-07 06:51:04 +00002838/// areCompatVectorTypes - Return true if the two specified vector types are
2839/// compatible.
2840static bool areCompatVectorTypes(const VectorType *LHS,
2841 const VectorType *RHS) {
2842 assert(LHS->isCanonical() && RHS->isCanonical());
2843 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002844 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002845}
2846
Eli Friedman0d9549b2008-08-22 00:56:42 +00002847/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002848/// compatible for assignment from RHS to LHS. This handles validation of any
2849/// protocol qualifiers on the LHS or RHS.
2850///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002851bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2852 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002853 // Verify that the base decls are compatible: the RHS must be a subclass of
2854 // the LHS.
2855 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2856 return false;
2857
2858 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2859 // protocol qualified at all, then we are good.
2860 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2861 return true;
2862
2863 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2864 // isn't a superset.
2865 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2866 return true; // FIXME: should return false!
2867
2868 // Finally, we must have two protocol-qualified interfaces.
2869 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2870 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ff358b2008-04-07 06:51:04 +00002871
Steve Naroff98e71b82009-03-01 16:12:44 +00002872 // All LHS protocols must have a presence on the RHS.
2873 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ff358b2008-04-07 06:51:04 +00002874
Steve Naroff98e71b82009-03-01 16:12:44 +00002875 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2876 LHSPE = LHSP->qual_end();
2877 LHSPI != LHSPE; LHSPI++) {
2878 bool RHSImplementsProtocol = false;
2879
2880 // If the RHS doesn't implement the protocol on the left, the types
2881 // are incompatible.
2882 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2883 RHSPE = RHSP->qual_end();
2884 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2885 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2886 RHSImplementsProtocol = true;
2887 }
2888 // FIXME: For better diagnostics, consider passing back the protocol name.
2889 if (!RHSImplementsProtocol)
2890 return false;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002891 }
Steve Naroff98e71b82009-03-01 16:12:44 +00002892 // The RHS implements all protocols listed on the LHS.
2893 return true;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002894}
2895
Steve Naroff17c03822009-02-12 17:52:19 +00002896bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2897 // get the "pointed to" types
2898 const PointerType *LHSPT = LHS->getAsPointerType();
2899 const PointerType *RHSPT = RHS->getAsPointerType();
2900
2901 if (!LHSPT || !RHSPT)
2902 return false;
2903
2904 QualType lhptee = LHSPT->getPointeeType();
2905 QualType rhptee = RHSPT->getPointeeType();
2906 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2907 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2908 // ID acts sort of like void* for ObjC interfaces
2909 if (LHSIface && isObjCIdStructType(rhptee))
2910 return true;
2911 if (RHSIface && isObjCIdStructType(lhptee))
2912 return true;
2913 if (!LHSIface || !RHSIface)
2914 return false;
2915 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2916 canAssignObjCInterfaces(RHSIface, LHSIface);
2917}
2918
Steve Naroff85f0dc52007-10-15 20:41:53 +00002919/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2920/// both shall have the identically qualified version of a compatible type.
2921/// C99 6.2.7p1: Two types have compatible types if their types are the
2922/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002923bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2924 return !mergeTypes(LHS, RHS).isNull();
2925}
2926
2927QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2928 const FunctionType *lbase = lhs->getAsFunctionType();
2929 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002930 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2931 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002932 bool allLTypes = true;
2933 bool allRTypes = true;
2934
2935 // Check return type
2936 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2937 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002938 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2939 allLTypes = false;
2940 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2941 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002942
2943 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl74942cd2009-04-30 19:20:55 +00002944 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
2945 "C++ shouldn't be here");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002946 unsigned lproto_nargs = lproto->getNumArgs();
2947 unsigned rproto_nargs = rproto->getNumArgs();
2948
2949 // Compatible functions must have the same number of arguments
2950 if (lproto_nargs != rproto_nargs)
2951 return QualType();
2952
2953 // Variadic and non-variadic functions aren't compatible
2954 if (lproto->isVariadic() != rproto->isVariadic())
2955 return QualType();
2956
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002957 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2958 return QualType();
2959
Eli Friedman0d9549b2008-08-22 00:56:42 +00002960 // Check argument compatibility
2961 llvm::SmallVector<QualType, 10> types;
2962 for (unsigned i = 0; i < lproto_nargs; i++) {
2963 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2964 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2965 QualType argtype = mergeTypes(largtype, rargtype);
2966 if (argtype.isNull()) return QualType();
2967 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002968 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2969 allLTypes = false;
2970 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2971 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002972 }
2973 if (allLTypes) return lhs;
2974 if (allRTypes) return rhs;
2975 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002976 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002977 }
2978
2979 if (lproto) allRTypes = false;
2980 if (rproto) allLTypes = false;
2981
Douglas Gregor4fa58902009-02-26 23:50:07 +00002982 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002983 if (proto) {
Sebastian Redl74942cd2009-04-30 19:20:55 +00002984 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002985 if (proto->isVariadic()) return QualType();
2986 // Check that the types are compatible with the types that
2987 // would result from default argument promotions (C99 6.7.5.3p15).
2988 // The only types actually affected are promotable integer
2989 // types and floats, which would be passed as a different
2990 // type depending on whether the prototype is visible.
2991 unsigned proto_nargs = proto->getNumArgs();
2992 for (unsigned i = 0; i < proto_nargs; ++i) {
2993 QualType argTy = proto->getArgType(i);
2994 if (argTy->isPromotableIntegerType() ||
2995 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2996 return QualType();
2997 }
2998
2999 if (allLTypes) return lhs;
3000 if (allRTypes) return rhs;
3001 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00003002 proto->getNumArgs(), lproto->isVariadic(),
3003 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00003004 }
3005
3006 if (allLTypes) return lhs;
3007 if (allRTypes) return rhs;
Douglas Gregor4fa58902009-02-26 23:50:07 +00003008 return getFunctionNoProtoType(retType);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003009}
3010
3011QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00003012 // C++ [expr]: If an expression initially has the type "reference to T", the
3013 // type is adjusted to "T" prior to any further analysis, the expression
3014 // designates the object or function denoted by the reference, and the
Sebastian Redlce6fff02009-03-16 23:22:08 +00003015 // expression is an lvalue unless the reference is an rvalue reference and
3016 // the expression is a function call (possibly inside parentheses).
Eli Friedman0d9549b2008-08-22 00:56:42 +00003017 // FIXME: C++ shouldn't be going through here! The rules are different
3018 // enough that they should be handled separately.
Sebastian Redlce6fff02009-03-16 23:22:08 +00003019 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3020 // shouldn't be going through here!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003021 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00003022 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00003023 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00003024 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00003025
Eli Friedman0d9549b2008-08-22 00:56:42 +00003026 QualType LHSCan = getCanonicalType(LHS),
3027 RHSCan = getCanonicalType(RHS);
3028
3029 // If two types are identical, they are compatible.
3030 if (LHSCan == RHSCan)
3031 return LHS;
3032
3033 // If the qualifiers are different, the types aren't compatible
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003034 // Note that we handle extended qualifiers later, in the
3035 // case for ExtQualType.
3036 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman0d9549b2008-08-22 00:56:42 +00003037 return QualType();
3038
Fariborz Jahanian0dc684e2009-04-15 21:54:48 +00003039 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
3040 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman0d9549b2008-08-22 00:56:42 +00003041
Chris Lattnerc38d4522008-01-14 05:45:46 +00003042 // We want to consider the two function types to be the same for these
3043 // comparisons, just force one to the other.
3044 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3045 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00003046
3047 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00003048 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3049 LHSClass = Type::ConstantArray;
3050 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3051 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003052
Nate Begemanaf6ed502008-04-18 23:10:10 +00003053 // Canonicalize ExtVector -> Vector.
3054 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3055 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00003056
Chris Lattner7cdcb252008-04-07 06:38:24 +00003057 // Consider qualified interfaces and interfaces the same.
3058 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
3059 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003060
Chris Lattnerb5709e22008-04-07 05:43:21 +00003061 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00003062 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003063 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3064 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanian0dc684e2009-04-15 21:54:48 +00003065
Steve Naroff0773c582009-04-14 15:11:46 +00003066 // 'id' and 'Class' act sort of like void* for ObjC interfaces
3067 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00003068 return LHS;
Steve Naroff0773c582009-04-14 15:11:46 +00003069 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00003070 return RHS;
3071
Steve Naroff28ceff72008-12-10 22:14:21 +00003072 // ID is compatible with all qualified id types.
3073 if (LHS->isObjCQualifiedIdType()) {
3074 if (const PointerType *PT = RHS->getAsPointerType()) {
3075 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00003076 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00003077 return LHS;
3078 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3079 // Unfortunately, this API is part of Sema (which we don't have access
3080 // to. Need to refactor. The following check is insufficient, since we
3081 // need to make sure the class implements the protocol.
3082 if (pType->isObjCInterfaceType())
3083 return LHS;
3084 }
3085 }
3086 if (RHS->isObjCQualifiedIdType()) {
3087 if (const PointerType *PT = LHS->getAsPointerType()) {
3088 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00003089 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00003090 return RHS;
3091 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3092 // Unfortunately, this API is part of Sema (which we don't have access
3093 // to. Need to refactor. The following check is insufficient, since we
3094 // need to make sure the class implements the protocol.
3095 if (pType->isObjCInterfaceType())
3096 return RHS;
3097 }
3098 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003099 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3100 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003101 if (const EnumType* ETy = LHS->getAsEnumType()) {
3102 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3103 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00003104 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003105 if (const EnumType* ETy = RHS->getAsEnumType()) {
3106 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3107 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00003108 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003109
Eli Friedman0d9549b2008-08-22 00:56:42 +00003110 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003111 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003112
Steve Naroffc88babe2008-01-09 22:43:08 +00003113 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00003114 switch (LHSClass) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00003115#define TYPE(Class, Base)
3116#define ABSTRACT_TYPE(Class, Base)
3117#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3118#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3119#include "clang/AST/TypeNodes.def"
3120 assert(false && "Non-canonical and dependent types shouldn't get here");
3121 return QualType();
3122
Sebastian Redlce6fff02009-03-16 23:22:08 +00003123 case Type::LValueReference:
3124 case Type::RValueReference:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003125 case Type::MemberPointer:
3126 assert(false && "C++ should never be in mergeTypes");
3127 return QualType();
3128
3129 case Type::IncompleteArray:
3130 case Type::VariableArray:
3131 case Type::FunctionProto:
3132 case Type::ExtVector:
3133 case Type::ObjCQualifiedInterface:
3134 assert(false && "Types are eliminated above");
3135 return QualType();
3136
Chris Lattnerc38d4522008-01-14 05:45:46 +00003137 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003138 {
3139 // Merge two pointer types, while trying to preserve typedef info
3140 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3141 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3142 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3143 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003144 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3145 return LHS;
3146 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3147 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003148 return getPointerType(ResultType);
3149 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00003150 case Type::BlockPointer:
3151 {
3152 // Merge two block pointer types, while trying to preserve typedef info
3153 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3154 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3155 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3156 if (ResultType.isNull()) return QualType();
3157 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3158 return LHS;
3159 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3160 return RHS;
3161 return getBlockPointerType(ResultType);
3162 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003163 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003164 {
3165 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3166 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3167 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3168 return QualType();
3169
3170 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3171 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3172 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3173 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003174 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3175 return LHS;
3176 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3177 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003178 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3179 ArrayType::ArraySizeModifier(), 0);
3180 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3181 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003182 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3183 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003184 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3185 return LHS;
3186 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3187 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003188 if (LVAT) {
3189 // FIXME: This isn't correct! But tricky to implement because
3190 // the array's size has to be the size of LHS, but the type
3191 // has to be different.
3192 return LHS;
3193 }
3194 if (RVAT) {
3195 // FIXME: This isn't correct! But tricky to implement because
3196 // the array's size has to be the size of RHS, but the type
3197 // has to be different.
3198 return RHS;
3199 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003200 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3201 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003202 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003203 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003204 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003205 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor4fa58902009-02-26 23:50:07 +00003206 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003207 case Type::Enum:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003208 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00003209 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3210 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003211 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00003212 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003213 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003214 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00003215 case Type::Complex:
3216 // Distinct complex types are incompatible.
3217 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003218 case Type::Vector:
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003219 // FIXME: The merged type should be an ExtVector!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003220 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3221 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003222 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003223 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003224 // Check if the interfaces are assignment compatible.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003225 // FIXME: This should be type compatibility, e.g. whether
3226 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff0bbc1352009-02-21 16:18:07 +00003227 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3228 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3229 if (LHSIface && RHSIface &&
3230 canAssignObjCInterfaces(LHSIface, RHSIface))
3231 return LHS;
3232
Eli Friedman0d9549b2008-08-22 00:56:42 +00003233 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003234 }
Steve Naroff28ceff72008-12-10 22:14:21 +00003235 case Type::ObjCQualifiedId:
3236 // Distinct qualified id's are not compatible.
3237 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003238 case Type::FixedWidthInt:
3239 // Distinct fixed-width integers are not compatible.
3240 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003241 case Type::ExtQual:
3242 // FIXME: ExtQual types can be compatible even if they're not
3243 // identical!
3244 return QualType();
3245 // First attempt at an implementation, but I'm not really sure it's
3246 // right...
3247#if 0
3248 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3249 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3250 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3251 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3252 return QualType();
3253 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3254 LHSBase = QualType(LQual->getBaseType(), 0);
3255 RHSBase = QualType(RQual->getBaseType(), 0);
3256 ResultType = mergeTypes(LHSBase, RHSBase);
3257 if (ResultType.isNull()) return QualType();
3258 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3259 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3260 return LHS;
3261 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3262 return RHS;
3263 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3264 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3265 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3266 return ResultType;
3267#endif
Douglas Gregordd13e842009-03-30 22:58:21 +00003268
3269 case Type::TemplateSpecialization:
3270 assert(false && "Dependent types have no size");
3271 break;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003272 }
Douglas Gregor4fa58902009-02-26 23:50:07 +00003273
3274 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003275}
Ted Kremenek738e6c02007-10-31 17:10:13 +00003276
Chris Lattner1d78a862008-04-07 07:01:58 +00003277//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00003278// Integer Predicates
3279//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00003280
Eli Friedman0832dbc2008-06-28 06:23:08 +00003281unsigned ASTContext::getIntWidth(QualType T) {
3282 if (T == BoolTy)
3283 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00003284 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3285 return FWIT->getWidth();
3286 }
3287 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00003288 return (unsigned)getTypeSize(T);
3289}
3290
3291QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3292 assert(T->isSignedIntegerType() && "Unexpected type");
3293 if (const EnumType* ETy = T->getAsEnumType())
3294 T = ETy->getDecl()->getIntegerType();
3295 const BuiltinType* BTy = T->getAsBuiltinType();
3296 assert (BTy && "Unexpected signed integer type");
3297 switch (BTy->getKind()) {
3298 case BuiltinType::Char_S:
3299 case BuiltinType::SChar:
3300 return UnsignedCharTy;
3301 case BuiltinType::Short:
3302 return UnsignedShortTy;
3303 case BuiltinType::Int:
3304 return UnsignedIntTy;
3305 case BuiltinType::Long:
3306 return UnsignedLongTy;
3307 case BuiltinType::LongLong:
3308 return UnsignedLongLongTy;
Chris Lattner6cc7e412009-04-30 02:43:43 +00003309 case BuiltinType::Int128:
3310 return UnsignedInt128Ty;
Eli Friedman0832dbc2008-06-28 06:23:08 +00003311 default:
3312 assert(0 && "Unexpected signed integer type");
3313 return QualType();
3314 }
3315}
3316
Douglas Gregorc34897d2009-04-09 22:27:44 +00003317ExternalASTSource::~ExternalASTSource() { }
3318
3319void ExternalASTSource::PrintStats() { }