blob: 3b1a11cb79547f519e0b46d88586d0aa620fd6ee [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000019#include "clang/AST/ExternalASTSource.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000021#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000023#include "llvm/ADT/StringExtras.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000025#include "llvm/Support/MemoryBuffer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner61710852008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Douglas Gregor2deaea32009-04-22 18:49:13 +000035 bool FreeMem, unsigned size_reserve,
36 bool InitializeBuiltins) :
Douglas Gregorab452ba2009-03-26 23:50:42 +000037 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
38 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Douglas Gregor2cf26342009-04-09 22:27:44 +000039 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels),
40 ExternalSource(0) {
Daniel Dunbare91593e2008-08-11 04:54:23 +000041 if (size_reserve > 0) Types.reserve(size_reserve);
42 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
Douglas Gregor7a9cbed2009-04-26 03:57:37 +000044 BuiltinInfo.InitializeTargetBuiltins(Target);
Douglas Gregor2deaea32009-04-22 18:49:13 +000045 if (InitializeBuiltins)
46 this->InitializeBuiltins(idents);
Daniel Dunbare91593e2008-08-11 04:54:23 +000047}
48
Reid Spencer5f016e22007-07-11 17:01:13 +000049ASTContext::~ASTContext() {
50 // Deallocate all the types.
51 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000052 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000053 Types.pop_back();
54 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000055
Nuno Lopesb74668e2008-12-17 22:30:25 +000056 {
57 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
58 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
59 while (I != E) {
60 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
61 delete R;
62 }
63 }
64
65 {
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +000066 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
67 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopesb74668e2008-12-17 22:30:25 +000068 while (I != E) {
69 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
70 delete R;
71 }
72 }
73
Douglas Gregorab452ba2009-03-26 23:50:42 +000074 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000075 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
76 NNS = NestedNameSpecifiers.begin(),
77 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregore7dcd782009-03-27 23:25:45 +000078 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000079 /* Increment in loop */)
80 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000081
82 if (GlobalNestedNameSpecifier)
83 GlobalNestedNameSpecifier->Destroy(*this);
84
Eli Friedmanb26153c2008-05-27 03:08:09 +000085 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000086}
87
Douglas Gregor2deaea32009-04-22 18:49:13 +000088void ASTContext::InitializeBuiltins(IdentifierTable &idents) {
Douglas Gregor2deaea32009-04-22 18:49:13 +000089 BuiltinInfo.InitializeBuiltins(idents, LangOpts.NoBuiltin);
90}
91
Douglas Gregor2cf26342009-04-09 22:27:44 +000092void
93ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
94 ExternalSource.reset(Source.take());
95}
96
Reid Spencer5f016e22007-07-11 17:01:13 +000097void ASTContext::PrintStats() const {
98 fprintf(stderr, "*** AST Context Stats:\n");
99 fprintf(stderr, " %d types total.\n", (int)Types.size());
100 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000101 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000102 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
103 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
104
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000106 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
107 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000108 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000109 unsigned NumExtQual = 0;
110
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
112 Type *T = Types[i];
113 if (isa<BuiltinType>(T))
114 ++NumBuiltin;
115 else if (isa<PointerType>(T))
116 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000117 else if (isa<BlockPointerType>(T))
118 ++NumBlockPointer;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000119 else if (isa<LValueReferenceType>(T))
120 ++NumLValueReference;
121 else if (isa<RValueReferenceType>(T))
122 ++NumRValueReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000123 else if (isa<MemberPointerType>(T))
124 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000125 else if (isa<ComplexType>(T))
126 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 else if (isa<ArrayType>(T))
128 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000129 else if (isa<VectorType>(T))
130 ++NumVector;
Douglas Gregor72564e72009-02-26 23:50:07 +0000131 else if (isa<FunctionNoProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 ++NumFunctionNP;
Douglas Gregor72564e72009-02-26 23:50:07 +0000133 else if (isa<FunctionProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 ++NumFunctionP;
135 else if (isa<TypedefType>(T))
136 ++NumTypeName;
137 else if (TagType *TT = dyn_cast<TagType>(T)) {
138 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000139 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000141 case TagDecl::TK_struct: ++NumTagStruct; break;
142 case TagDecl::TK_union: ++NumTagUnion; break;
143 case TagDecl::TK_class: ++NumTagClass; break;
144 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000146 } else if (isa<ObjCInterfaceType>(T))
147 ++NumObjCInterfaces;
148 else if (isa<ObjCQualifiedInterfaceType>(T))
149 ++NumObjCQualifiedInterfaces;
150 else if (isa<ObjCQualifiedIdType>(T))
151 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000152 else if (isa<TypeOfType>(T))
153 ++NumTypeOfTypes;
Douglas Gregor72564e72009-02-26 23:50:07 +0000154 else if (isa<TypeOfExprType>(T))
155 ++NumTypeOfExprTypes;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000156 else if (isa<ExtQualType>(T))
157 ++NumExtQual;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000158 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000159 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 assert(0 && "Unknown type!");
161 }
162 }
163
164 fprintf(stderr, " %d builtin types\n", NumBuiltin);
165 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000166 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000167 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
168 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000169 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000170 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000172 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
174 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
175 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
176 fprintf(stderr, " %d tagged types\n", NumTagged);
177 fprintf(stderr, " %d struct types\n", NumTagStruct);
178 fprintf(stderr, " %d union types\n", NumTagUnion);
179 fprintf(stderr, " %d class types\n", NumTagClass);
180 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000181 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000182 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000183 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000184 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000185 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000186 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor72564e72009-02-26 23:50:07 +0000187 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000188 fprintf(stderr, " %d attribute-qualified types\n", NumExtQual);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000189
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
191 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000192 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000193 NumLValueReference*sizeof(LValueReferenceType)+
194 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000195 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000196 NumFunctionP*sizeof(FunctionProtoType)+
197 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000198 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000199 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)+
200 NumExtQual*sizeof(ExtQualType)));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000201
202 if (ExternalSource.get()) {
203 fprintf(stderr, "\n");
204 ExternalSource->PrintStats();
205 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000206}
207
208
209void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000210 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000211}
212
Reid Spencer5f016e22007-07-11 17:01:13 +0000213void ASTContext::InitBuiltinTypes() {
214 assert(VoidTy.isNull() && "Context reinitialized?");
215
216 // C99 6.2.5p19.
217 InitBuiltinType(VoidTy, BuiltinType::Void);
218
219 // C99 6.2.5p2.
220 InitBuiltinType(BoolTy, BuiltinType::Bool);
221 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000222 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 InitBuiltinType(CharTy, BuiltinType::Char_S);
224 else
225 InitBuiltinType(CharTy, BuiltinType::Char_U);
226 // C99 6.2.5p4.
227 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
228 InitBuiltinType(ShortTy, BuiltinType::Short);
229 InitBuiltinType(IntTy, BuiltinType::Int);
230 InitBuiltinType(LongTy, BuiltinType::Long);
231 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
232
233 // C99 6.2.5p6.
234 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
235 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
236 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
237 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
238 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
239
240 // C99 6.2.5p10.
241 InitBuiltinType(FloatTy, BuiltinType::Float);
242 InitBuiltinType(DoubleTy, BuiltinType::Double);
243 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000244
Chris Lattner2df9ced2009-04-30 02:43:43 +0000245 // GNU extension, 128-bit integers.
246 InitBuiltinType(Int128Ty, BuiltinType::Int128);
247 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
248
Chris Lattner3a250322009-02-26 23:43:47 +0000249 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
250 InitBuiltinType(WCharTy, BuiltinType::WChar);
251 else // C99
252 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000253
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000254 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000255 InitBuiltinType(OverloadTy, BuiltinType::Overload);
256
257 // Placeholder type for type-dependent expressions whose type is
258 // completely unknown. No code should ever check a type against
259 // DependentTy and users should never see it; however, it is here to
260 // help diagnose failures to properly check for type-dependent
261 // expressions.
262 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000263
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 // C99 6.2.5p11.
265 FloatComplexTy = getComplexType(FloatTy);
266 DoubleComplexTy = getComplexType(DoubleTy);
267 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000268
Steve Naroff7e219e42007-10-15 14:41:52 +0000269 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000270 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000271 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000272 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000273 ClassStructType = 0;
274
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000275 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000276
277 // void * type
278 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000279}
280
Chris Lattner464175b2007-07-18 17:52:12 +0000281//===----------------------------------------------------------------------===//
282// Type Sizing and Analysis
283//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000284
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000285/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
286/// scalar floating point type.
287const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
288 const BuiltinType *BT = T->getAsBuiltinType();
289 assert(BT && "Not a floating point type!");
290 switch (BT->getKind()) {
291 default: assert(0 && "Not a floating point type!");
292 case BuiltinType::Float: return Target.getFloatFormat();
293 case BuiltinType::Double: return Target.getDoubleFormat();
294 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
295 }
296}
297
Chris Lattneraf707ab2009-01-24 21:53:27 +0000298/// getDeclAlign - Return a conservative estimate of the alignment of the
299/// specified decl. Note that bitfields do not have a valid alignment, so
300/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000301unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000302 unsigned Align = Target.getCharWidth();
303
304 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
305 Align = std::max(Align, AA->getAlignment());
306
Chris Lattneraf707ab2009-01-24 21:53:27 +0000307 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
308 QualType T = VD->getType();
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000309 if (const ReferenceType* RT = T->getAsReferenceType()) {
310 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000311 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000312 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
313 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000314 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
315 T = cast<ArrayType>(T)->getElementType();
316
317 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
318 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000319 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000320
321 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000322}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000323
Chris Lattnera7674d82007-07-13 22:13:22 +0000324/// getTypeSize - Return the size of the specified type, in bits. This method
325/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000326std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000327ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000328 uint64_t Width=0;
329 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000330 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000331#define TYPE(Class, Base)
332#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000333#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000334#define DEPENDENT_TYPE(Class, Base) case Type::Class:
335#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000336 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000337 break;
338
Chris Lattner692233e2007-07-13 22:27:08 +0000339 case Type::FunctionNoProto:
340 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000341 // GCC extension: alignof(function) = 32 bits
342 Width = 0;
343 Align = 32;
344 break;
345
Douglas Gregor72564e72009-02-26 23:50:07 +0000346 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000347 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000348 Width = 0;
349 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
350 break;
351
Steve Narofffb22d962007-08-30 01:06:46 +0000352 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000353 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000354
Chris Lattner98be4942008-03-05 18:54:05 +0000355 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000356 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000357 Align = EltInfo.second;
358 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000359 }
Nate Begeman213541a2008-04-18 23:10:10 +0000360 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000361 case Type::Vector: {
362 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000363 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000364 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000365 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000366 // If the alignment is not a power of 2, round up to the next power of 2.
367 // This happens for non-power-of-2 length vectors.
368 // FIXME: this should probably be a target property.
369 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000370 break;
371 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000372
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000373 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000374 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000375 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000376 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000377 // GCC extension: alignof(void) = 8 bits.
378 Width = 0;
379 Align = 8;
380 break;
381
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000382 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000383 Width = Target.getBoolWidth();
384 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000385 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000386 case BuiltinType::Char_S:
387 case BuiltinType::Char_U:
388 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000389 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000390 Width = Target.getCharWidth();
391 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000392 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000393 case BuiltinType::WChar:
394 Width = Target.getWCharWidth();
395 Align = Target.getWCharAlign();
396 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000397 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000398 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000399 Width = Target.getShortWidth();
400 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000401 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000402 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000403 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000404 Width = Target.getIntWidth();
405 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000406 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000407 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000408 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000409 Width = Target.getLongWidth();
410 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000411 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000412 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000413 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000414 Width = Target.getLongLongWidth();
415 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000416 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000417 case BuiltinType::Int128:
418 case BuiltinType::UInt128:
419 Width = 128;
420 Align = 128; // int128_t is 128-bit aligned on all targets.
421 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000422 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000423 Width = Target.getFloatWidth();
424 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000425 break;
426 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000427 Width = Target.getDoubleWidth();
428 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000429 break;
430 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000431 Width = Target.getLongDoubleWidth();
432 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000433 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000434 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000435 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000436 case Type::FixedWidthInt:
437 // FIXME: This isn't precisely correct; the width/alignment should depend
438 // on the available types for the target
439 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000440 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000441 Align = Width;
442 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000443 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000444 // FIXME: Pointers into different addr spaces could have different sizes and
445 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000446 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000447 case Type::ObjCQualifiedId:
Douglas Gregor72564e72009-02-26 23:50:07 +0000448 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000449 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000450 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000451 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000452 case Type::BlockPointer: {
453 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
454 Width = Target.getPointerWidth(AS);
455 Align = Target.getPointerAlign(AS);
456 break;
457 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000458 case Type::Pointer: {
459 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000460 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000461 Align = Target.getPointerAlign(AS);
462 break;
463 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000464 case Type::LValueReference:
465 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000466 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000467 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000468 // FIXME: This is wrong for struct layout: a reference in a struct has
469 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000470 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000471 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000472 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000473 // the GCC ABI, where pointers to data are one pointer large, pointers to
474 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000475 // other compilers too, we need to delegate this completely to TargetInfo
476 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000477 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
478 unsigned AS = Pointee.getAddressSpace();
479 Width = Target.getPointerWidth(AS);
480 if (Pointee->isFunctionType())
481 Width *= 2;
482 Align = Target.getPointerAlign(AS);
483 // GCC aligns at single pointer width.
484 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000485 case Type::Complex: {
486 // Complex types have the same alignment as their elements, but twice the
487 // size.
488 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000489 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000490 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000491 Align = EltInfo.second;
492 break;
493 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000494 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000495 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000496 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
497 Width = Layout.getSize();
498 Align = Layout.getAlignment();
499 break;
500 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000501 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000502 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000503 const TagType *TT = cast<TagType>(T);
504
505 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000506 Width = 1;
507 Align = 1;
508 break;
509 }
510
Daniel Dunbar1d751182008-11-08 05:48:37 +0000511 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000512 return getTypeInfo(ET->getDecl()->getIntegerType());
513
Daniel Dunbar1d751182008-11-08 05:48:37 +0000514 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000515 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
516 Width = Layout.getSize();
517 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000518 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000519 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000520
Douglas Gregor18857642009-04-30 17:32:17 +0000521 case Type::Typedef: {
522 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
523 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
524 Align = Aligned->getAlignment();
525 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
526 } else
527 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000528 break;
Chris Lattner71763312008-04-06 22:05:18 +0000529 }
Douglas Gregor18857642009-04-30 17:32:17 +0000530
531 case Type::TypeOfExpr:
532 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
533 .getTypePtr());
534
535 case Type::TypeOf:
536 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
537
538 case Type::QualifiedName:
539 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
540
541 case Type::TemplateSpecialization:
542 assert(getCanonicalType(T) != T &&
543 "Cannot request the size of a dependent type");
544 // FIXME: this is likely to be wrong once we support template
545 // aliases, since a template alias could refer to a typedef that
546 // has an __aligned__ attribute on it.
547 return getTypeInfo(getCanonicalType(T));
548 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000549
Chris Lattner464175b2007-07-18 17:52:12 +0000550 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000551 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000552}
553
Chris Lattner34ebde42009-01-27 18:08:34 +0000554/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
555/// type for the current target in bits. This can be different than the ABI
556/// alignment in cases where it is beneficial for performance to overalign
557/// a data type.
558unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
559 unsigned ABIAlign = getTypeAlign(T);
560
561 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000562 if (T->isSpecificBuiltinType(BuiltinType::Double))
563 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000564
565 return ABIAlign;
566}
567
568
Devang Patel8b277042008-06-04 21:22:16 +0000569/// LayoutField - Field layout.
570void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000571 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000572 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000573 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000574 uint64_t FieldOffset = IsUnion ? 0 : Size;
575 uint64_t FieldSize;
576 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000577
578 // FIXME: Should this override struct packing? Probably we want to
579 // take the minimum?
580 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
581 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000582
583 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
584 // TODO: Need to check this algorithm on other targets!
585 // (tested on Linux-X86)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000586 FieldSize = BitWidthExpr->EvaluateAsInt(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000587
588 std::pair<uint64_t, unsigned> FieldInfo =
589 Context.getTypeInfo(FD->getType());
590 uint64_t TypeSize = FieldInfo.first;
591
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000592 // Determine the alignment of this bitfield. The packing
593 // attributes define a maximum and the alignment attribute defines
594 // a minimum.
595 // FIXME: What is the right behavior when the specified alignment
596 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000597 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000598 if (FieldPacking)
599 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000600 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
601 FieldAlign = std::max(FieldAlign, AA->getAlignment());
602
603 // Check if we need to add padding to give the field the correct
604 // alignment.
605 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
606 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
607
608 // Padding members don't affect overall alignment
609 if (!FD->getIdentifier())
610 FieldAlign = 1;
611 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000612 if (FD->getType()->isIncompleteArrayType()) {
613 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000614 // query getTypeInfo about these, so we figure it out here.
615 // Flexible array members don't have any size, but they
616 // have to be aligned appropriately for their element type.
617 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000618 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000619 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson2f1169f2009-04-10 05:31:15 +0000620 } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
621 unsigned AS = RT->getPointeeType().getAddressSpace();
622 FieldSize = Context.Target.getPointerWidth(AS);
623 FieldAlign = Context.Target.getPointerAlign(AS);
Devang Patel8b277042008-06-04 21:22:16 +0000624 } else {
625 std::pair<uint64_t, unsigned> FieldInfo =
626 Context.getTypeInfo(FD->getType());
627 FieldSize = FieldInfo.first;
628 FieldAlign = FieldInfo.second;
629 }
630
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000631 // Determine the alignment of this bitfield. The packing
632 // attributes define a maximum and the alignment attribute defines
633 // a minimum. Additionally, the packing alignment must be at least
634 // a byte for non-bitfields.
635 //
636 // FIXME: What is the right behavior when the specified alignment
637 // is smaller than the specified packing?
638 if (FieldPacking)
639 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000640 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
641 FieldAlign = std::max(FieldAlign, AA->getAlignment());
642
643 // Round up the current record size to the field's alignment boundary.
644 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
645 }
646
647 // Place this field at the current location.
648 FieldOffsets[FieldNo] = FieldOffset;
649
650 // Reserve space for this field.
651 if (IsUnion) {
652 Size = std::max(Size, FieldSize);
653 } else {
654 Size = FieldOffset + FieldSize;
655 }
656
Daniel Dunbard6884a02009-05-04 05:16:21 +0000657 // Remember the next available offset.
658 NextOffset = Size;
659
Devang Patel8b277042008-06-04 21:22:16 +0000660 // Remember max struct/class alignment.
661 Alignment = std::max(Alignment, FieldAlign);
662}
663
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000664static void CollectLocalObjCIvars(ASTContext *Ctx,
665 const ObjCInterfaceDecl *OI,
666 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000667 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
668 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000669 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000670 if (!IVDecl->isInvalidDecl())
671 Fields.push_back(cast<FieldDecl>(IVDecl));
672 }
673}
674
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000675void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
676 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
677 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
678 CollectObjCIvars(SuperClass, Fields);
679 CollectLocalObjCIvars(this, OI, Fields);
680}
681
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000682/// getInterfaceLayoutImpl - Get or compute information about the
683/// layout of the given interface.
684///
685/// \param Impl - If given, also include the layout of the interface's
686/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000687const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000688ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
689 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000690 assert(!D->isForwardDecl() && "Invalid interface decl!");
691
Devang Patel44a3dde2008-06-04 21:54:36 +0000692 // Look up this layout, if already laid out, return what we have.
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000693 ObjCContainerDecl *Key =
694 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
695 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
696 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000697
Daniel Dunbar453addb2009-05-03 11:16:44 +0000698 unsigned FieldCount = D->ivar_size();
699 // Add in synthesized ivar count if laying out an implementation.
700 if (Impl) {
701 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
702 E = D->prop_end(*this); I != E; ++I)
703 if ((*I)->getPropertyIvarDecl())
704 ++FieldCount;
705
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000706 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +0000707 // entry. Note we can't cache this because we simply free all
708 // entries later; however we shouldn't look up implementations
709 // frequently.
710 if (FieldCount == D->ivar_size())
711 return getObjCLayout(D, 0);
712 }
713
Devang Patel6a5a34c2008-06-06 02:14:01 +0000714 ASTRecordLayout *NewEntry = NULL;
Devang Patel6a5a34c2008-06-06 02:14:01 +0000715 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Devang Patel6a5a34c2008-06-06 02:14:01 +0000716 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
717 unsigned Alignment = SL.getAlignment();
718 uint64_t Size = SL.getSize();
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000719
Daniel Dunbard6884a02009-05-04 05:16:21 +0000720 // If we are using tight interface packing, then we start laying
721 // out ivars not at the end of the superclass structure, but at
722 // the next byte following the last field.
723 if (getLangOptions().ObjCTightLayout)
724 Size = llvm::RoundUpToAlignment(SL.NextOffset, 8);
725
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000726 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout(Size, Alignment);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000727 NewEntry->InitializeLayout(FieldCount);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000728 } else {
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000729 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout();
Devang Patel6a5a34c2008-06-06 02:14:01 +0000730 NewEntry->InitializeLayout(FieldCount);
731 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000732
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000733 unsigned StructPacking = 0;
734 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
735 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000736
737 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
738 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
739 AA->getAlignment()));
740
741 // Layout each ivar sequentially.
742 unsigned i = 0;
743 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
744 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
745 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000746 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000747 }
Daniel Dunbar453addb2009-05-03 11:16:44 +0000748 // And synthesized ivars, if this is an implementation.
749 if (Impl) {
750 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
751 E = D->prop_end(*this); I != E; ++I) {
752 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
753 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
754 }
Fariborz Jahanian18191882009-03-31 18:11:23 +0000755 }
Fariborz Jahanian99eee362009-04-01 19:37:34 +0000756
Devang Patel44a3dde2008-06-04 21:54:36 +0000757 // Finally, round the size of the total struct up to the alignment of the
758 // struct itself.
759 NewEntry->FinalizeLayout();
760 return *NewEntry;
761}
762
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000763const ASTRecordLayout &
764ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
765 return getObjCLayout(D, 0);
766}
767
768const ASTRecordLayout &
769ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
770 return getObjCLayout(D->getClassInterface(), D);
771}
772
Devang Patel88a981b2007-11-01 19:11:01 +0000773/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000774/// specified record (struct/union/class), which indicates its size and field
775/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000776const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000777 D = D->getDefinition(*this);
778 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000779
Chris Lattner464175b2007-07-18 17:52:12 +0000780 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000781 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000782 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000783
Devang Patel88a981b2007-11-01 19:11:01 +0000784 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
785 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
786 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000787 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000788
Douglas Gregore267ff32008-12-11 20:41:00 +0000789 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000790 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
791 D->field_end(*this)));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000792 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000793
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000794 unsigned StructPacking = 0;
795 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
796 StructPacking = PA->getAlignment();
797
Eli Friedman4bd998b2008-05-30 09:31:38 +0000798 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000799 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
800 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000801
Eli Friedman4bd998b2008-05-30 09:31:38 +0000802 // Layout each field, for now, just sequentially, respecting alignment. In
803 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000804 unsigned FieldIdx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000805 for (RecordDecl::field_iterator Field = D->field_begin(*this),
806 FieldEnd = D->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000807 Field != FieldEnd; (void)++Field, ++FieldIdx)
808 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000809
810 // Finally, round the size of the total struct up to the alignment of the
811 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000812 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000813 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000814}
815
Chris Lattnera7674d82007-07-13 22:13:22 +0000816//===----------------------------------------------------------------------===//
817// Type creation/memoization methods
818//===----------------------------------------------------------------------===//
819
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000820QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000821 QualType CanT = getCanonicalType(T);
822 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000823 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000824
825 // If we are composing extended qualifiers together, merge together into one
826 // ExtQualType node.
827 unsigned CVRQuals = T.getCVRQualifiers();
828 QualType::GCAttrTypes GCAttr = QualType::GCNone;
829 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000830
Chris Lattnerb7d25532009-02-18 22:53:11 +0000831 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
832 // If this type already has an address space specified, it cannot get
833 // another one.
834 assert(EQT->getAddressSpace() == 0 &&
835 "Type cannot be in multiple addr spaces!");
836 GCAttr = EQT->getObjCGCAttr();
837 TypeNode = EQT->getBaseType();
838 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000839
Chris Lattnerb7d25532009-02-18 22:53:11 +0000840 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000841 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000842 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000843 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000844 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000845 return QualType(EXTQy, CVRQuals);
846
Christopher Lambebb97e92008-02-04 02:31:56 +0000847 // If the base type isn't canonical, this won't be a canonical type either,
848 // so fill in the canonical type field.
849 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000850 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000851 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000852
Chris Lattnerb7d25532009-02-18 22:53:11 +0000853 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000854 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000855 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000856 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000857 ExtQualType *New =
858 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000859 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000860 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000861 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000862}
863
Chris Lattnerb7d25532009-02-18 22:53:11 +0000864QualType ASTContext::getObjCGCQualType(QualType T,
865 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000866 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000867 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000868 return T;
869
Chris Lattnerb7d25532009-02-18 22:53:11 +0000870 // If we are composing extended qualifiers together, merge together into one
871 // ExtQualType node.
872 unsigned CVRQuals = T.getCVRQualifiers();
873 Type *TypeNode = T.getTypePtr();
874 unsigned AddressSpace = 0;
875
876 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
877 // If this type already has an address space specified, it cannot get
878 // another one.
879 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
880 "Type cannot be in multiple addr spaces!");
881 AddressSpace = EQT->getAddressSpace();
882 TypeNode = EQT->getBaseType();
883 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000884
885 // Check if we've already instantiated an gc qual'd type of this type.
886 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000887 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000888 void *InsertPos = 0;
889 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000890 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000891
892 // If the base type isn't canonical, this won't be a canonical type either,
893 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000894 // FIXME: Isn't this also not canonical if the base type is a array
895 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000896 QualType Canonical;
897 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000898 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000899
Chris Lattnerb7d25532009-02-18 22:53:11 +0000900 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000901 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
902 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
903 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000904 ExtQualType *New =
905 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000906 ExtQualTypes.InsertNode(New, InsertPos);
907 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000908 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000909}
Chris Lattnera7674d82007-07-13 22:13:22 +0000910
Reid Spencer5f016e22007-07-11 17:01:13 +0000911/// getComplexType - Return the uniqued reference to the type for a complex
912/// number with the specified element type.
913QualType ASTContext::getComplexType(QualType T) {
914 // Unique pointers, to guarantee there is only one pointer of a particular
915 // structure.
916 llvm::FoldingSetNodeID ID;
917 ComplexType::Profile(ID, T);
918
919 void *InsertPos = 0;
920 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
921 return QualType(CT, 0);
922
923 // If the pointee type isn't canonical, this won't be a canonical type either,
924 // so fill in the canonical type field.
925 QualType Canonical;
926 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000927 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000928
929 // Get the new insert position for the node we care about.
930 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000931 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000932 }
Steve Narofff83820b2009-01-27 22:08:43 +0000933 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000934 Types.push_back(New);
935 ComplexTypes.InsertNode(New, InsertPos);
936 return QualType(New, 0);
937}
938
Eli Friedmanf98aba32009-02-13 02:31:07 +0000939QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
940 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
941 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
942 FixedWidthIntType *&Entry = Map[Width];
943 if (!Entry)
944 Entry = new FixedWidthIntType(Width, Signed);
945 return QualType(Entry, 0);
946}
Reid Spencer5f016e22007-07-11 17:01:13 +0000947
948/// getPointerType - Return the uniqued reference to the type for a pointer to
949/// the specified type.
950QualType ASTContext::getPointerType(QualType T) {
951 // Unique pointers, to guarantee there is only one pointer of a particular
952 // structure.
953 llvm::FoldingSetNodeID ID;
954 PointerType::Profile(ID, T);
955
956 void *InsertPos = 0;
957 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
958 return QualType(PT, 0);
959
960 // If the pointee type isn't canonical, this won't be a canonical type either,
961 // so fill in the canonical type field.
962 QualType Canonical;
963 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000964 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000965
966 // Get the new insert position for the node we care about.
967 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000968 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000969 }
Steve Narofff83820b2009-01-27 22:08:43 +0000970 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000971 Types.push_back(New);
972 PointerTypes.InsertNode(New, InsertPos);
973 return QualType(New, 0);
974}
975
Steve Naroff5618bd42008-08-27 16:04:49 +0000976/// getBlockPointerType - Return the uniqued reference to the type for
977/// a pointer to the specified block.
978QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000979 assert(T->isFunctionType() && "block of function types only");
980 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000981 // structure.
982 llvm::FoldingSetNodeID ID;
983 BlockPointerType::Profile(ID, T);
984
985 void *InsertPos = 0;
986 if (BlockPointerType *PT =
987 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
988 return QualType(PT, 0);
989
Steve Naroff296e8d52008-08-28 19:20:44 +0000990 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000991 // type either so fill in the canonical type field.
992 QualType Canonical;
993 if (!T->isCanonical()) {
994 Canonical = getBlockPointerType(getCanonicalType(T));
995
996 // Get the new insert position for the node we care about.
997 BlockPointerType *NewIP =
998 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000999 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001000 }
Steve Narofff83820b2009-01-27 22:08:43 +00001001 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001002 Types.push_back(New);
1003 BlockPointerTypes.InsertNode(New, InsertPos);
1004 return QualType(New, 0);
1005}
1006
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001007/// getLValueReferenceType - Return the uniqued reference to the type for an
1008/// lvalue reference to the specified type.
1009QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001010 // Unique pointers, to guarantee there is only one pointer of a particular
1011 // structure.
1012 llvm::FoldingSetNodeID ID;
1013 ReferenceType::Profile(ID, T);
1014
1015 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001016 if (LValueReferenceType *RT =
1017 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001018 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001019
Reid Spencer5f016e22007-07-11 17:01:13 +00001020 // If the referencee type isn't canonical, this won't be a canonical type
1021 // either, so fill in the canonical type field.
1022 QualType Canonical;
1023 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001024 Canonical = getLValueReferenceType(getCanonicalType(T));
1025
Reid Spencer5f016e22007-07-11 17:01:13 +00001026 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001027 LValueReferenceType *NewIP =
1028 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001029 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001030 }
1031
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001032 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001033 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001034 LValueReferenceTypes.InsertNode(New, InsertPos);
1035 return QualType(New, 0);
1036}
1037
1038/// getRValueReferenceType - Return the uniqued reference to the type for an
1039/// rvalue reference to the specified type.
1040QualType ASTContext::getRValueReferenceType(QualType T) {
1041 // Unique pointers, to guarantee there is only one pointer of a particular
1042 // structure.
1043 llvm::FoldingSetNodeID ID;
1044 ReferenceType::Profile(ID, T);
1045
1046 void *InsertPos = 0;
1047 if (RValueReferenceType *RT =
1048 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1049 return QualType(RT, 0);
1050
1051 // If the referencee type isn't canonical, this won't be a canonical type
1052 // either, so fill in the canonical type field.
1053 QualType Canonical;
1054 if (!T->isCanonical()) {
1055 Canonical = getRValueReferenceType(getCanonicalType(T));
1056
1057 // Get the new insert position for the node we care about.
1058 RValueReferenceType *NewIP =
1059 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1060 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1061 }
1062
1063 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1064 Types.push_back(New);
1065 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 return QualType(New, 0);
1067}
1068
Sebastian Redlf30208a2009-01-24 21:16:55 +00001069/// getMemberPointerType - Return the uniqued reference to the type for a
1070/// member pointer to the specified type, in the specified class.
1071QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1072{
1073 // Unique pointers, to guarantee there is only one pointer of a particular
1074 // structure.
1075 llvm::FoldingSetNodeID ID;
1076 MemberPointerType::Profile(ID, T, Cls);
1077
1078 void *InsertPos = 0;
1079 if (MemberPointerType *PT =
1080 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1081 return QualType(PT, 0);
1082
1083 // If the pointee or class type isn't canonical, this won't be a canonical
1084 // type either, so fill in the canonical type field.
1085 QualType Canonical;
1086 if (!T->isCanonical()) {
1087 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1088
1089 // Get the new insert position for the node we care about.
1090 MemberPointerType *NewIP =
1091 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1092 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1093 }
Steve Narofff83820b2009-01-27 22:08:43 +00001094 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001095 Types.push_back(New);
1096 MemberPointerTypes.InsertNode(New, InsertPos);
1097 return QualType(New, 0);
1098}
1099
Steve Narofffb22d962007-08-30 01:06:46 +00001100/// getConstantArrayType - Return the unique reference to the type for an
1101/// array of the specified element type.
1102QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +00001103 const llvm::APInt &ArySize,
1104 ArrayType::ArraySizeModifier ASM,
1105 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001106 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001107 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001108
1109 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001110 if (ConstantArrayType *ATP =
1111 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001112 return QualType(ATP, 0);
1113
1114 // If the element type isn't canonical, this won't be a canonical type either,
1115 // so fill in the canonical type field.
1116 QualType Canonical;
1117 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001118 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001119 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001120 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001121 ConstantArrayType *NewIP =
1122 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001123 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001124 }
1125
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001126 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001127 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001128 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001129 Types.push_back(New);
1130 return QualType(New, 0);
1131}
1132
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001133/// getVariableArrayType - Returns a non-unique reference to the type for a
1134/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001135QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1136 ArrayType::ArraySizeModifier ASM,
1137 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001138 // Since we don't unique expressions, it isn't possible to unique VLA's
1139 // that have an expression provided for their size.
1140
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001141 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001142 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001143
1144 VariableArrayTypes.push_back(New);
1145 Types.push_back(New);
1146 return QualType(New, 0);
1147}
1148
Douglas Gregor898574e2008-12-05 23:32:09 +00001149/// getDependentSizedArrayType - Returns a non-unique reference to
1150/// the type for a dependently-sized array of the specified element
1151/// type. FIXME: We will need these to be uniqued, or at least
1152/// comparable, at some point.
1153QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1154 ArrayType::ArraySizeModifier ASM,
1155 unsigned EltTypeQuals) {
1156 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1157 "Size must be type- or value-dependent!");
1158
1159 // Since we don't unique expressions, it isn't possible to unique
1160 // dependently-sized array types.
1161
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001162 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001163 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1164 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001165
1166 DependentSizedArrayTypes.push_back(New);
1167 Types.push_back(New);
1168 return QualType(New, 0);
1169}
1170
Eli Friedmanc5773c42008-02-15 18:16:39 +00001171QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1172 ArrayType::ArraySizeModifier ASM,
1173 unsigned EltTypeQuals) {
1174 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001175 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001176
1177 void *InsertPos = 0;
1178 if (IncompleteArrayType *ATP =
1179 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1180 return QualType(ATP, 0);
1181
1182 // If the element type isn't canonical, this won't be a canonical type
1183 // either, so fill in the canonical type field.
1184 QualType Canonical;
1185
1186 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001187 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001188 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001189
1190 // Get the new insert position for the node we care about.
1191 IncompleteArrayType *NewIP =
1192 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001193 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001194 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001195
Steve Narofff83820b2009-01-27 22:08:43 +00001196 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001197 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001198
1199 IncompleteArrayTypes.InsertNode(New, InsertPos);
1200 Types.push_back(New);
1201 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001202}
1203
Steve Naroff73322922007-07-18 18:00:27 +00001204/// getVectorType - Return the unique reference to a vector type of
1205/// the specified element type and size. VectorType must be a built-in type.
1206QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001207 BuiltinType *baseType;
1208
Chris Lattnerf52ab252008-04-06 22:59:24 +00001209 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001210 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001211
1212 // Check if we've already instantiated a vector of this type.
1213 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001214 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 void *InsertPos = 0;
1216 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1217 return QualType(VTP, 0);
1218
1219 // If the element type isn't canonical, this won't be a canonical type either,
1220 // so fill in the canonical type field.
1221 QualType Canonical;
1222 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001223 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001224
1225 // Get the new insert position for the node we care about.
1226 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001227 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001228 }
Steve Narofff83820b2009-01-27 22:08:43 +00001229 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 VectorTypes.InsertNode(New, InsertPos);
1231 Types.push_back(New);
1232 return QualType(New, 0);
1233}
1234
Nate Begeman213541a2008-04-18 23:10:10 +00001235/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001236/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001237QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001238 BuiltinType *baseType;
1239
Chris Lattnerf52ab252008-04-06 22:59:24 +00001240 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001241 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001242
1243 // Check if we've already instantiated a vector of this type.
1244 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001245 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001246 void *InsertPos = 0;
1247 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1248 return QualType(VTP, 0);
1249
1250 // If the element type isn't canonical, this won't be a canonical type either,
1251 // so fill in the canonical type field.
1252 QualType Canonical;
1253 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001254 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001255
1256 // Get the new insert position for the node we care about.
1257 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001258 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001259 }
Steve Narofff83820b2009-01-27 22:08:43 +00001260 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001261 VectorTypes.InsertNode(New, InsertPos);
1262 Types.push_back(New);
1263 return QualType(New, 0);
1264}
1265
Douglas Gregor72564e72009-02-26 23:50:07 +00001266/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001267///
Douglas Gregor72564e72009-02-26 23:50:07 +00001268QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 // Unique functions, to guarantee there is only one function of a particular
1270 // structure.
1271 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001272 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001273
1274 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001275 if (FunctionNoProtoType *FT =
1276 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001277 return QualType(FT, 0);
1278
1279 QualType Canonical;
1280 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001281 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001282
1283 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001284 FunctionNoProtoType *NewIP =
1285 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001286 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001287 }
1288
Douglas Gregor72564e72009-02-26 23:50:07 +00001289 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001290 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001291 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001292 return QualType(New, 0);
1293}
1294
1295/// getFunctionType - Return a normal function type with a typed argument
1296/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001297QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001298 unsigned NumArgs, bool isVariadic,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001299 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001300 // Unique functions, to guarantee there is only one function of a particular
1301 // structure.
1302 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001303 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001304 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001305
1306 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001307 if (FunctionProtoType *FTP =
1308 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001309 return QualType(FTP, 0);
1310
1311 // Determine whether the type being created is already canonical or not.
1312 bool isCanonical = ResultTy->isCanonical();
1313 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1314 if (!ArgArray[i]->isCanonical())
1315 isCanonical = false;
1316
1317 // If this type isn't canonical, get the canonical version of it.
1318 QualType Canonical;
1319 if (!isCanonical) {
1320 llvm::SmallVector<QualType, 16> CanonicalArgs;
1321 CanonicalArgs.reserve(NumArgs);
1322 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001323 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001324
Chris Lattnerf52ab252008-04-06 22:59:24 +00001325 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001326 &CanonicalArgs[0], NumArgs,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001327 isVariadic, TypeQuals);
1328
Reid Spencer5f016e22007-07-11 17:01:13 +00001329 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001330 FunctionProtoType *NewIP =
1331 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001332 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001333 }
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001334
Douglas Gregor72564e72009-02-26 23:50:07 +00001335 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001336 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001337 FunctionProtoType *FTP =
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001338 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1339 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001340 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001341 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001342 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001343 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001344 return QualType(FTP, 0);
1345}
1346
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001347/// getTypeDeclType - Return the unique reference to the type for the
1348/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001349QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001350 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001351 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1352
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001353 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001354 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001355 else if (isa<TemplateTypeParmDecl>(Decl)) {
1356 assert(false && "Template type parameter types are always available.");
1357 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001358 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001359
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001360 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001361 if (PrevDecl)
1362 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001363 else
1364 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001365 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001366 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1367 if (PrevDecl)
1368 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001369 else
1370 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001371 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001372 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001373 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001374
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001375 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001376 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001377}
1378
Reid Spencer5f016e22007-07-11 17:01:13 +00001379/// getTypedefType - Return the unique reference to the type for the
1380/// specified typename decl.
1381QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1382 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1383
Chris Lattnerf52ab252008-04-06 22:59:24 +00001384 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001385 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001386 Types.push_back(Decl->TypeForDecl);
1387 return QualType(Decl->TypeForDecl, 0);
1388}
1389
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001390/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001391/// specified ObjC interface decl.
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001392QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001393 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1394
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001395 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1396 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff3536b442007-09-06 21:24:23 +00001397 Types.push_back(Decl->TypeForDecl);
1398 return QualType(Decl->TypeForDecl, 0);
1399}
1400
Douglas Gregorfab9d672009-02-05 23:33:38 +00001401/// \brief Retrieve the template type parameter type for a template
1402/// parameter with the given depth, index, and (optionally) name.
1403QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1404 IdentifierInfo *Name) {
1405 llvm::FoldingSetNodeID ID;
1406 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1407 void *InsertPos = 0;
1408 TemplateTypeParmType *TypeParm
1409 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1410
1411 if (TypeParm)
1412 return QualType(TypeParm, 0);
1413
1414 if (Name)
1415 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1416 getTemplateTypeParmType(Depth, Index));
1417 else
1418 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1419
1420 Types.push_back(TypeParm);
1421 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1422
1423 return QualType(TypeParm, 0);
1424}
1425
Douglas Gregor55f6b142009-02-09 18:46:07 +00001426QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001427ASTContext::getTemplateSpecializationType(TemplateName Template,
1428 const TemplateArgument *Args,
1429 unsigned NumArgs,
1430 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001431 if (!Canon.isNull())
1432 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001433
Douglas Gregor55f6b142009-02-09 18:46:07 +00001434 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001435 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001436
Douglas Gregor55f6b142009-02-09 18:46:07 +00001437 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001438 TemplateSpecializationType *Spec
1439 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001440
1441 if (Spec)
1442 return QualType(Spec, 0);
1443
Douglas Gregor7532dc62009-03-30 22:58:21 +00001444 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001445 sizeof(TemplateArgument) * NumArgs),
1446 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001447 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001448 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001449 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001450
1451 return QualType(Spec, 0);
1452}
1453
Douglas Gregore4e5b052009-03-19 00:18:19 +00001454QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001455ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001456 QualType NamedType) {
1457 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001458 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001459
1460 void *InsertPos = 0;
1461 QualifiedNameType *T
1462 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1463 if (T)
1464 return QualType(T, 0);
1465
Douglas Gregorab452ba2009-03-26 23:50:42 +00001466 T = new (*this) QualifiedNameType(NNS, NamedType,
1467 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001468 Types.push_back(T);
1469 QualifiedNameTypes.InsertNode(T, InsertPos);
1470 return QualType(T, 0);
1471}
1472
Douglas Gregord57959a2009-03-27 23:10:48 +00001473QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1474 const IdentifierInfo *Name,
1475 QualType Canon) {
1476 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1477
1478 if (Canon.isNull()) {
1479 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1480 if (CanonNNS != NNS)
1481 Canon = getTypenameType(CanonNNS, Name);
1482 }
1483
1484 llvm::FoldingSetNodeID ID;
1485 TypenameType::Profile(ID, NNS, Name);
1486
1487 void *InsertPos = 0;
1488 TypenameType *T
1489 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1490 if (T)
1491 return QualType(T, 0);
1492
1493 T = new (*this) TypenameType(NNS, Name, Canon);
1494 Types.push_back(T);
1495 TypenameTypes.InsertNode(T, InsertPos);
1496 return QualType(T, 0);
1497}
1498
Douglas Gregor17343172009-04-01 00:28:59 +00001499QualType
1500ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1501 const TemplateSpecializationType *TemplateId,
1502 QualType Canon) {
1503 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1504
1505 if (Canon.isNull()) {
1506 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1507 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1508 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1509 const TemplateSpecializationType *CanonTemplateId
1510 = CanonType->getAsTemplateSpecializationType();
1511 assert(CanonTemplateId &&
1512 "Canonical type must also be a template specialization type");
1513 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1514 }
1515 }
1516
1517 llvm::FoldingSetNodeID ID;
1518 TypenameType::Profile(ID, NNS, TemplateId);
1519
1520 void *InsertPos = 0;
1521 TypenameType *T
1522 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1523 if (T)
1524 return QualType(T, 0);
1525
1526 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1527 Types.push_back(T);
1528 TypenameTypes.InsertNode(T, InsertPos);
1529 return QualType(T, 0);
1530}
1531
Chris Lattner88cb27a2008-04-07 04:56:42 +00001532/// CmpProtocolNames - Comparison predicate for sorting protocols
1533/// alphabetically.
1534static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1535 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001536 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001537}
1538
1539static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1540 unsigned &NumProtocols) {
1541 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1542
1543 // Sort protocols, keyed by name.
1544 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1545
1546 // Remove duplicates.
1547 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1548 NumProtocols = ProtocolsEnd-Protocols;
1549}
1550
1551
Chris Lattner065f0d72008-04-07 04:44:08 +00001552/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1553/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001554QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1555 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001556 // Sort the protocol list alphabetically to canonicalize it.
1557 SortAndUniqueProtocols(Protocols, NumProtocols);
1558
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001559 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001560 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001561
1562 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001563 if (ObjCQualifiedInterfaceType *QT =
1564 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001565 return QualType(QT, 0);
1566
1567 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001568 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001569 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001570
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001571 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001572 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001573 return QualType(QType, 0);
1574}
1575
Chris Lattner88cb27a2008-04-07 04:56:42 +00001576/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1577/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001578QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001579 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001580 // Sort the protocol list alphabetically to canonicalize it.
1581 SortAndUniqueProtocols(Protocols, NumProtocols);
1582
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001583 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001584 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001585
1586 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001587 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001588 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001589 return QualType(QT, 0);
1590
1591 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001592 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001593 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001594 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001595 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001596 return QualType(QType, 0);
1597}
1598
Douglas Gregor72564e72009-02-26 23:50:07 +00001599/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1600/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001601/// multiple declarations that refer to "typeof(x)" all contain different
1602/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1603/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001604QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001605 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001606 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001607 Types.push_back(toe);
1608 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001609}
1610
Steve Naroff9752f252007-08-01 18:02:17 +00001611/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1612/// TypeOfType AST's. The only motivation to unique these nodes would be
1613/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1614/// an issue. This doesn't effect the type checker, since it operates
1615/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001616QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001617 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001618 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001619 Types.push_back(tot);
1620 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001621}
1622
Reid Spencer5f016e22007-07-11 17:01:13 +00001623/// getTagDeclType - Return the unique reference to the type for the
1624/// specified TagDecl (struct/union/class/enum) decl.
1625QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001626 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001627 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001628}
1629
1630/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1631/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1632/// needs to agree with the definition in <stddef.h>.
1633QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001634 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001635}
1636
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001637/// getSignedWCharType - Return the type of "signed wchar_t".
1638/// Used when in C++, as a GCC extension.
1639QualType ASTContext::getSignedWCharType() const {
1640 // FIXME: derive from "Target" ?
1641 return WCharTy;
1642}
1643
1644/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1645/// Used when in C++, as a GCC extension.
1646QualType ASTContext::getUnsignedWCharType() const {
1647 // FIXME: derive from "Target" ?
1648 return UnsignedIntTy;
1649}
1650
Chris Lattner8b9023b2007-07-13 03:05:23 +00001651/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1652/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1653QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001654 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001655}
1656
Chris Lattnere6327742008-04-02 05:18:44 +00001657//===----------------------------------------------------------------------===//
1658// Type Operators
1659//===----------------------------------------------------------------------===//
1660
Chris Lattner77c96472008-04-06 22:41:35 +00001661/// getCanonicalType - Return the canonical (structural) type corresponding to
1662/// the specified potentially non-canonical type. The non-canonical version
1663/// of a type may have many "decorated" versions of types. Decorators can
1664/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1665/// to be free of any of these, allowing two canonical types to be compared
1666/// for exact equality with a simple pointer comparison.
1667QualType ASTContext::getCanonicalType(QualType T) {
1668 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001669
1670 // If the result has type qualifiers, make sure to canonicalize them as well.
1671 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1672 if (TypeQuals == 0) return CanType;
1673
1674 // If the type qualifiers are on an array type, get the canonical type of the
1675 // array with the qualifiers applied to the element type.
1676 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1677 if (!AT)
1678 return CanType.getQualifiedType(TypeQuals);
1679
1680 // Get the canonical version of the element with the extra qualifiers on it.
1681 // This can recursively sink qualifiers through multiple levels of arrays.
1682 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1683 NewEltTy = getCanonicalType(NewEltTy);
1684
1685 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1686 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1687 CAT->getIndexTypeQualifier());
1688 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1689 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1690 IAT->getIndexTypeQualifier());
1691
Douglas Gregor898574e2008-12-05 23:32:09 +00001692 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1693 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1694 DSAT->getSizeModifier(),
1695 DSAT->getIndexTypeQualifier());
1696
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001697 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1698 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1699 VAT->getSizeModifier(),
1700 VAT->getIndexTypeQualifier());
1701}
1702
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001703TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
1704 // If this template name refers to a template, the canonical
1705 // template name merely stores the template itself.
1706 if (TemplateDecl *Template = Name.getAsTemplateDecl())
1707 return TemplateName(Template);
1708
1709 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
1710 assert(DTN && "Non-dependent template names must refer to template decls.");
1711 return DTN->CanonicalTemplateName;
1712}
1713
Douglas Gregord57959a2009-03-27 23:10:48 +00001714NestedNameSpecifier *
1715ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1716 if (!NNS)
1717 return 0;
1718
1719 switch (NNS->getKind()) {
1720 case NestedNameSpecifier::Identifier:
1721 // Canonicalize the prefix but keep the identifier the same.
1722 return NestedNameSpecifier::Create(*this,
1723 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1724 NNS->getAsIdentifier());
1725
1726 case NestedNameSpecifier::Namespace:
1727 // A namespace is canonical; build a nested-name-specifier with
1728 // this namespace and no prefix.
1729 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1730
1731 case NestedNameSpecifier::TypeSpec:
1732 case NestedNameSpecifier::TypeSpecWithTemplate: {
1733 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1734 NestedNameSpecifier *Prefix = 0;
1735
1736 // FIXME: This isn't the right check!
1737 if (T->isDependentType())
1738 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1739
1740 return NestedNameSpecifier::Create(*this, Prefix,
1741 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1742 T.getTypePtr());
1743 }
1744
1745 case NestedNameSpecifier::Global:
1746 // The global specifier is canonical and unique.
1747 return NNS;
1748 }
1749
1750 // Required to silence a GCC warning
1751 return 0;
1752}
1753
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001754
1755const ArrayType *ASTContext::getAsArrayType(QualType T) {
1756 // Handle the non-qualified case efficiently.
1757 if (T.getCVRQualifiers() == 0) {
1758 // Handle the common positive case fast.
1759 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1760 return AT;
1761 }
1762
1763 // Handle the common negative case fast, ignoring CVR qualifiers.
1764 QualType CType = T->getCanonicalTypeInternal();
1765
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001766 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001767 // test.
1768 if (!isa<ArrayType>(CType) &&
1769 !isa<ArrayType>(CType.getUnqualifiedType()))
1770 return 0;
1771
1772 // Apply any CVR qualifiers from the array type to the element type. This
1773 // implements C99 6.7.3p8: "If the specification of an array type includes
1774 // any type qualifiers, the element type is so qualified, not the array type."
1775
1776 // If we get here, we either have type qualifiers on the type, or we have
1777 // sugar such as a typedef in the way. If we have type qualifiers on the type
1778 // we must propagate them down into the elemeng type.
1779 unsigned CVRQuals = T.getCVRQualifiers();
1780 unsigned AddrSpace = 0;
1781 Type *Ty = T.getTypePtr();
1782
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001783 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001784 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001785 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1786 AddrSpace = EXTQT->getAddressSpace();
1787 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001788 } else {
1789 T = Ty->getDesugaredType();
1790 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1791 break;
1792 CVRQuals |= T.getCVRQualifiers();
1793 Ty = T.getTypePtr();
1794 }
1795 }
1796
1797 // If we have a simple case, just return now.
1798 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1799 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1800 return ATy;
1801
1802 // Otherwise, we have an array and we have qualifiers on it. Push the
1803 // qualifiers into the array element type and return a new array type.
1804 // Get the canonical version of the element with the extra qualifiers on it.
1805 // This can recursively sink qualifiers through multiple levels of arrays.
1806 QualType NewEltTy = ATy->getElementType();
1807 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001808 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001809 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1810
1811 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1812 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1813 CAT->getSizeModifier(),
1814 CAT->getIndexTypeQualifier()));
1815 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1816 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1817 IAT->getSizeModifier(),
1818 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001819
Douglas Gregor898574e2008-12-05 23:32:09 +00001820 if (const DependentSizedArrayType *DSAT
1821 = dyn_cast<DependentSizedArrayType>(ATy))
1822 return cast<ArrayType>(
1823 getDependentSizedArrayType(NewEltTy,
1824 DSAT->getSizeExpr(),
1825 DSAT->getSizeModifier(),
1826 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001827
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001828 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1829 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1830 VAT->getSizeModifier(),
1831 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001832}
1833
1834
Chris Lattnere6327742008-04-02 05:18:44 +00001835/// getArrayDecayedType - Return the properly qualified result of decaying the
1836/// specified array type to a pointer. This operation is non-trivial when
1837/// handling typedefs etc. The canonical type of "T" must be an array type,
1838/// this returns a pointer to a properly qualified element of the array.
1839///
1840/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1841QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001842 // Get the element type with 'getAsArrayType' so that we don't lose any
1843 // typedefs in the element type of the array. This also handles propagation
1844 // of type qualifiers from the array type into the element type if present
1845 // (C99 6.7.3p8).
1846 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1847 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001848
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001849 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001850
1851 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001852 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001853}
1854
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001855QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001856 QualType ElemTy = VAT->getElementType();
1857
1858 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1859 return getBaseElementType(VAT);
1860
1861 return ElemTy;
1862}
1863
Reid Spencer5f016e22007-07-11 17:01:13 +00001864/// getFloatingRank - Return a relative rank for floating point types.
1865/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001866static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001867 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001868 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001869
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001870 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001871 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001872 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001873 case BuiltinType::Float: return FloatRank;
1874 case BuiltinType::Double: return DoubleRank;
1875 case BuiltinType::LongDouble: return LongDoubleRank;
1876 }
1877}
1878
Steve Naroff716c7302007-08-27 01:41:48 +00001879/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1880/// point or a complex type (based on typeDomain/typeSize).
1881/// 'typeDomain' is a real floating point or complex type.
1882/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001883QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1884 QualType Domain) const {
1885 FloatingRank EltRank = getFloatingRank(Size);
1886 if (Domain->isComplexType()) {
1887 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001888 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001889 case FloatRank: return FloatComplexTy;
1890 case DoubleRank: return DoubleComplexTy;
1891 case LongDoubleRank: return LongDoubleComplexTy;
1892 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001893 }
Chris Lattner1361b112008-04-06 23:58:54 +00001894
1895 assert(Domain->isRealFloatingType() && "Unknown domain!");
1896 switch (EltRank) {
1897 default: assert(0 && "getFloatingRank(): illegal value for rank");
1898 case FloatRank: return FloatTy;
1899 case DoubleRank: return DoubleTy;
1900 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001901 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001902}
1903
Chris Lattner7cfeb082008-04-06 23:55:33 +00001904/// getFloatingTypeOrder - Compare the rank of the two specified floating
1905/// point types, ignoring the domain of the type (i.e. 'double' ==
1906/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1907/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001908int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1909 FloatingRank LHSR = getFloatingRank(LHS);
1910 FloatingRank RHSR = getFloatingRank(RHS);
1911
1912 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001913 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001914 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001915 return 1;
1916 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001917}
1918
Chris Lattnerf52ab252008-04-06 22:59:24 +00001919/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1920/// routine will assert if passed a built-in type that isn't an integer or enum,
1921/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001922unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001923 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001924 if (EnumType* ET = dyn_cast<EnumType>(T))
1925 T = ET->getDecl()->getIntegerType().getTypePtr();
1926
1927 // There are two things which impact the integer rank: the width, and
1928 // the ordering of builtins. The builtin ordering is encoded in the
1929 // bottom three bits; the width is encoded in the bits above that.
1930 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1931 return FWIT->getWidth() << 3;
1932 }
1933
Chris Lattnerf52ab252008-04-06 22:59:24 +00001934 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001935 default: assert(0 && "getIntegerRank(): not a built-in integer");
1936 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001937 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001938 case BuiltinType::Char_S:
1939 case BuiltinType::Char_U:
1940 case BuiltinType::SChar:
1941 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001942 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001943 case BuiltinType::Short:
1944 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001945 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001946 case BuiltinType::Int:
1947 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001948 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001949 case BuiltinType::Long:
1950 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001951 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001952 case BuiltinType::LongLong:
1953 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001954 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00001955 case BuiltinType::Int128:
1956 case BuiltinType::UInt128:
1957 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001958 }
1959}
1960
Chris Lattner7cfeb082008-04-06 23:55:33 +00001961/// getIntegerTypeOrder - Returns the highest ranked integer type:
1962/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1963/// LHS < RHS, return -1.
1964int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001965 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1966 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001967 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001968
Chris Lattnerf52ab252008-04-06 22:59:24 +00001969 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1970 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001971
Chris Lattner7cfeb082008-04-06 23:55:33 +00001972 unsigned LHSRank = getIntegerRank(LHSC);
1973 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001974
Chris Lattner7cfeb082008-04-06 23:55:33 +00001975 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1976 if (LHSRank == RHSRank) return 0;
1977 return LHSRank > RHSRank ? 1 : -1;
1978 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001979
Chris Lattner7cfeb082008-04-06 23:55:33 +00001980 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1981 if (LHSUnsigned) {
1982 // If the unsigned [LHS] type is larger, return it.
1983 if (LHSRank >= RHSRank)
1984 return 1;
1985
1986 // If the signed type can represent all values of the unsigned type, it
1987 // wins. Because we are dealing with 2's complement and types that are
1988 // powers of two larger than each other, this is always safe.
1989 return -1;
1990 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001991
Chris Lattner7cfeb082008-04-06 23:55:33 +00001992 // If the unsigned [RHS] type is larger, return it.
1993 if (RHSRank >= LHSRank)
1994 return -1;
1995
1996 // If the signed type can represent all values of the unsigned type, it
1997 // wins. Because we are dealing with 2's complement and types that are
1998 // powers of two larger than each other, this is always safe.
1999 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002000}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002001
2002// getCFConstantStringType - Return the type used for constant CFStrings.
2003QualType ASTContext::getCFConstantStringType() {
2004 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00002005 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002006 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002007 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002008 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00002009
2010 // const int *isa;
2011 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002012 // int flags;
2013 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002014 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002015 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00002016 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002017 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00002018
Anders Carlsson71993dd2007-08-17 05:31:46 +00002019 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002020 for (unsigned i = 0; i < 4; ++i) {
2021 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2022 SourceLocation(), 0,
2023 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002024 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002025 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002026 }
2027
2028 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002029 }
2030
2031 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002032}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002033
Douglas Gregor319ac892009-04-23 22:29:11 +00002034void ASTContext::setCFConstantStringType(QualType T) {
2035 const RecordType *Rec = T->getAsRecordType();
2036 assert(Rec && "Invalid CFConstantStringType");
2037 CFConstantStringTypeDecl = Rec->getDecl();
2038}
2039
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002040QualType ASTContext::getObjCFastEnumerationStateType()
2041{
2042 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002043 ObjCFastEnumerationStateTypeDecl =
2044 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2045 &Idents.get("__objcFastEnumerationState"));
2046
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002047 QualType FieldTypes[] = {
2048 UnsignedLongTy,
2049 getPointerType(ObjCIdType),
2050 getPointerType(UnsignedLongTy),
2051 getConstantArrayType(UnsignedLongTy,
2052 llvm::APInt(32, 5), ArrayType::Normal, 0)
2053 };
2054
Douglas Gregor44b43212008-12-11 16:49:14 +00002055 for (size_t i = 0; i < 4; ++i) {
2056 FieldDecl *Field = FieldDecl::Create(*this,
2057 ObjCFastEnumerationStateTypeDecl,
2058 SourceLocation(), 0,
2059 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002060 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002061 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002062 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002063
Douglas Gregor44b43212008-12-11 16:49:14 +00002064 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002065 }
2066
2067 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2068}
2069
Douglas Gregor319ac892009-04-23 22:29:11 +00002070void ASTContext::setObjCFastEnumerationStateType(QualType T) {
2071 const RecordType *Rec = T->getAsRecordType();
2072 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2073 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2074}
2075
Anders Carlssone8c49532007-10-29 06:33:42 +00002076// This returns true if a type has been typedefed to BOOL:
2077// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002078static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002079 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002080 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2081 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002082
2083 return false;
2084}
2085
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002086/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002087/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002088int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002089 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002090
2091 // Make all integer and enum types at least as large as an int
2092 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002093 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002094 // Treat arrays as pointers, since that's how they're passed in.
2095 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002096 sz = getTypeSize(VoidPtrTy);
2097 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002098}
2099
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002100/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002101/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002102void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002103 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002104 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002105 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002106 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002107 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002108 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002109 // Compute size of all parameters.
2110 // Start with computing size of a pointer in number of bytes.
2111 // FIXME: There might(should) be a better way of doing this computation!
2112 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002113 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002114 // The first two arguments (self and _cmd) are pointers; account for
2115 // their size.
2116 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002117 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2118 E = Decl->param_end(); PI != E; ++PI) {
2119 QualType PType = (*PI)->getType();
2120 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002121 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002122 ParmOffset += sz;
2123 }
2124 S += llvm::utostr(ParmOffset);
2125 S += "@0:";
2126 S += llvm::utostr(PtrSize);
2127
2128 // Argument types.
2129 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002130 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2131 E = Decl->param_end(); PI != E; ++PI) {
2132 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002133 QualType PType = PVDecl->getOriginalType();
2134 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002135 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2136 // Use array's original type only if it has known number of
2137 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002138 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002139 PType = PVDecl->getType();
2140 } else if (PType->isFunctionType())
2141 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002142 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002143 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002144 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002145 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002146 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002147 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002148 }
2149}
2150
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002151/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002152/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002153/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2154/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002155/// Property attributes are stored as a comma-delimited C string. The simple
2156/// attributes readonly and bycopy are encoded as single characters. The
2157/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2158/// encoded as single characters, followed by an identifier. Property types
2159/// are also encoded as a parametrized attribute. The characters used to encode
2160/// these attributes are defined by the following enumeration:
2161/// @code
2162/// enum PropertyAttributes {
2163/// kPropertyReadOnly = 'R', // property is read-only.
2164/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2165/// kPropertyByref = '&', // property is a reference to the value last assigned
2166/// kPropertyDynamic = 'D', // property is dynamic
2167/// kPropertyGetter = 'G', // followed by getter selector name
2168/// kPropertySetter = 'S', // followed by setter selector name
2169/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2170/// kPropertyType = 't' // followed by old-style type encoding.
2171/// kPropertyWeak = 'W' // 'weak' property
2172/// kPropertyStrong = 'P' // property GC'able
2173/// kPropertyNonAtomic = 'N' // property non-atomic
2174/// };
2175/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002176void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2177 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002178 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002179 // Collect information from the property implementation decl(s).
2180 bool Dynamic = false;
2181 ObjCPropertyImplDecl *SynthesizePID = 0;
2182
2183 // FIXME: Duplicated code due to poor abstraction.
2184 if (Container) {
2185 if (const ObjCCategoryImplDecl *CID =
2186 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2187 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002188 i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2189 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002190 ObjCPropertyImplDecl *PID = *i;
2191 if (PID->getPropertyDecl() == PD) {
2192 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2193 Dynamic = true;
2194 } else {
2195 SynthesizePID = PID;
2196 }
2197 }
2198 }
2199 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002200 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002201 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002202 i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2203 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002204 ObjCPropertyImplDecl *PID = *i;
2205 if (PID->getPropertyDecl() == PD) {
2206 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2207 Dynamic = true;
2208 } else {
2209 SynthesizePID = PID;
2210 }
2211 }
2212 }
2213 }
2214 }
2215
2216 // FIXME: This is not very efficient.
2217 S = "T";
2218
2219 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002220 // GCC has some special rules regarding encoding of properties which
2221 // closely resembles encoding of ivars.
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002222 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002223 true /* outermost type */,
2224 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002225
2226 if (PD->isReadOnly()) {
2227 S += ",R";
2228 } else {
2229 switch (PD->getSetterKind()) {
2230 case ObjCPropertyDecl::Assign: break;
2231 case ObjCPropertyDecl::Copy: S += ",C"; break;
2232 case ObjCPropertyDecl::Retain: S += ",&"; break;
2233 }
2234 }
2235
2236 // It really isn't clear at all what this means, since properties
2237 // are "dynamic by default".
2238 if (Dynamic)
2239 S += ",D";
2240
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002241 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2242 S += ",N";
2243
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002244 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2245 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002246 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002247 }
2248
2249 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2250 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002251 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002252 }
2253
2254 if (SynthesizePID) {
2255 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2256 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002257 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002258 }
2259
2260 // FIXME: OBJCGC: weak & strong
2261}
2262
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002263/// getLegacyIntegralTypeEncoding -
2264/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002265/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002266/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2267///
2268void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2269 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2270 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002271 if (BT->getKind() == BuiltinType::ULong &&
2272 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002273 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002274 else
2275 if (BT->getKind() == BuiltinType::Long &&
2276 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002277 PointeeTy = IntTy;
2278 }
2279 }
2280}
2281
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002282void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002283 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002284 // We follow the behavior of gcc, expanding structures which are
2285 // directly pointed to, and expanding embedded structures. Note that
2286 // these rules are sufficient to prevent recursive encoding of the
2287 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002288 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2289 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002290}
2291
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002292static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002293 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002294 const Expr *E = FD->getBitWidth();
2295 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2296 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002297 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002298 S += 'b';
2299 S += llvm::utostr(N);
2300}
2301
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002302void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2303 bool ExpandPointedToStructures,
2304 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002305 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002306 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002307 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002308 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002309 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002310 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002311 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002312 else {
2313 char encoding;
2314 switch (BT->getKind()) {
2315 default: assert(0 && "Unhandled builtin type kind");
2316 case BuiltinType::Void: encoding = 'v'; break;
2317 case BuiltinType::Bool: encoding = 'B'; break;
2318 case BuiltinType::Char_U:
2319 case BuiltinType::UChar: encoding = 'C'; break;
2320 case BuiltinType::UShort: encoding = 'S'; break;
2321 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002322 case BuiltinType::ULong:
2323 encoding =
2324 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2325 break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002326 case BuiltinType::UInt128: encoding = 'T'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002327 case BuiltinType::ULongLong: encoding = 'Q'; break;
2328 case BuiltinType::Char_S:
2329 case BuiltinType::SChar: encoding = 'c'; break;
2330 case BuiltinType::Short: encoding = 's'; break;
2331 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002332 case BuiltinType::Long:
2333 encoding =
2334 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2335 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002336 case BuiltinType::LongLong: encoding = 'q'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002337 case BuiltinType::Int128: encoding = 't'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002338 case BuiltinType::Float: encoding = 'f'; break;
2339 case BuiltinType::Double: encoding = 'd'; break;
2340 case BuiltinType::LongDouble: encoding = 'd'; break;
2341 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002342
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002343 S += encoding;
2344 }
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002345 } else if (const ComplexType *CT = T->getAsComplexType()) {
2346 S += 'j';
2347 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2348 false);
2349 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002350 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2351 ExpandPointedToStructures,
2352 ExpandStructures, FD);
2353 if (FD || EncodingProperty) {
2354 // Note that we do extended encoding of protocol qualifer list
2355 // Only when doing ivar or property encoding.
2356 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2357 S += '"';
2358 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2359 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2360 S += '<';
2361 S += Proto->getNameAsString();
2362 S += '>';
2363 }
2364 S += '"';
2365 }
2366 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002367 }
2368 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002369 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002370 bool isReadOnly = false;
2371 // For historical/compatibility reasons, the read-only qualifier of the
2372 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2373 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2374 // Also, do not emit the 'r' for anything but the outermost type!
2375 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2376 if (OutermostType && T.isConstQualified()) {
2377 isReadOnly = true;
2378 S += 'r';
2379 }
2380 }
2381 else if (OutermostType) {
2382 QualType P = PointeeTy;
2383 while (P->getAsPointerType())
2384 P = P->getAsPointerType()->getPointeeType();
2385 if (P.isConstQualified()) {
2386 isReadOnly = true;
2387 S += 'r';
2388 }
2389 }
2390 if (isReadOnly) {
2391 // Another legacy compatibility encoding. Some ObjC qualifier and type
2392 // combinations need to be rearranged.
2393 // Rewrite "in const" from "nr" to "rn"
2394 const char * s = S.c_str();
2395 int len = S.length();
2396 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2397 std::string replace = "rn";
2398 S.replace(S.end()-2, S.end(), replace);
2399 }
2400 }
Steve Naroff389bf462009-02-12 17:52:19 +00002401 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002402 S += '@';
2403 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002404 }
2405 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002406 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002407 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002408 // Another historical/compatibility reason.
2409 // We encode the underlying type which comes out as
2410 // {...};
2411 S += '^';
2412 getObjCEncodingForTypeImpl(PointeeTy, S,
2413 false, ExpandPointedToStructures,
2414 NULL);
2415 return;
2416 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002417 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002418 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002419 const ObjCInterfaceType *OIT =
2420 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002421 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002422 S += '"';
2423 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002424 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2425 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2426 S += '<';
2427 S += Proto->getNameAsString();
2428 S += '>';
2429 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002430 S += '"';
2431 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002432 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002433 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002434 S += '#';
2435 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002436 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002437 S += ':';
2438 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002439 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002440
2441 if (PointeeTy->isCharType()) {
2442 // char pointer types should be encoded as '*' unless it is a
2443 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002444 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002445 S += '*';
2446 return;
2447 }
2448 }
2449
2450 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002451 getLegacyIntegralTypeEncoding(PointeeTy);
2452
2453 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002454 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002455 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002456 } else if (const ArrayType *AT =
2457 // Ignore type qualifiers etc.
2458 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002459 if (isa<IncompleteArrayType>(AT)) {
2460 // Incomplete arrays are encoded as a pointer to the array element.
2461 S += '^';
2462
2463 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2464 false, ExpandStructures, FD);
2465 } else {
2466 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002467
Anders Carlsson559a8332009-02-22 01:38:57 +00002468 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2469 S += llvm::utostr(CAT->getSize().getZExtValue());
2470 else {
2471 //Variable length arrays are encoded as a regular array with 0 elements.
2472 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2473 S += '0';
2474 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002475
Anders Carlsson559a8332009-02-22 01:38:57 +00002476 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2477 false, ExpandStructures, FD);
2478 S += ']';
2479 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002480 } else if (T->getAsFunctionType()) {
2481 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002482 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002483 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002484 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002485 // Anonymous structures print as '?'
2486 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2487 S += II->getName();
2488 } else {
2489 S += '?';
2490 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002491 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002492 S += '=';
Douglas Gregor6ab35242009-04-09 21:40:53 +00002493 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2494 FieldEnd = RDecl->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +00002495 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002496 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002497 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002498 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002499 S += '"';
2500 }
2501
2502 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002503 if (Field->isBitField()) {
2504 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2505 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002506 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002507 QualType qt = Field->getType();
2508 getLegacyIntegralTypeEncoding(qt);
2509 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002510 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002511 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002512 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002513 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002514 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002515 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002516 if (FD && FD->isBitField())
2517 EncodeBitField(this, S, FD);
2518 else
2519 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002520 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002521 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002522 } else if (T->isObjCInterfaceType()) {
2523 // @encode(class_name)
2524 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2525 S += '{';
2526 const IdentifierInfo *II = OI->getIdentifier();
2527 S += II->getName();
2528 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002529 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002530 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002531 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002532 if (RecFields[i]->isBitField())
2533 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2534 RecFields[i]);
2535 else
2536 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2537 FD);
2538 }
2539 S += '}';
2540 }
2541 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002542 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002543}
2544
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002545void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002546 std::string& S) const {
2547 if (QT & Decl::OBJC_TQ_In)
2548 S += 'n';
2549 if (QT & Decl::OBJC_TQ_Inout)
2550 S += 'N';
2551 if (QT & Decl::OBJC_TQ_Out)
2552 S += 'o';
2553 if (QT & Decl::OBJC_TQ_Bycopy)
2554 S += 'O';
2555 if (QT & Decl::OBJC_TQ_Byref)
2556 S += 'R';
2557 if (QT & Decl::OBJC_TQ_Oneway)
2558 S += 'V';
2559}
2560
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002561void ASTContext::setBuiltinVaListType(QualType T)
2562{
2563 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2564
2565 BuiltinVaListType = T;
2566}
2567
Douglas Gregor319ac892009-04-23 22:29:11 +00002568void ASTContext::setObjCIdType(QualType T)
Steve Naroff7e219e42007-10-15 14:41:52 +00002569{
Douglas Gregor319ac892009-04-23 22:29:11 +00002570 ObjCIdType = T;
2571
2572 const TypedefType *TT = T->getAsTypedefType();
2573 if (!TT)
2574 return;
2575
2576 TypedefDecl *TD = TT->getDecl();
Steve Naroff7e219e42007-10-15 14:41:52 +00002577
2578 // typedef struct objc_object *id;
2579 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002580 // User error - caller will issue diagnostics.
2581 if (!ptr)
2582 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002583 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002584 // User error - caller will issue diagnostics.
2585 if (!rec)
2586 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002587 IdStructType = rec;
2588}
2589
Douglas Gregor319ac892009-04-23 22:29:11 +00002590void ASTContext::setObjCSelType(QualType T)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002591{
Douglas Gregor319ac892009-04-23 22:29:11 +00002592 ObjCSelType = T;
2593
2594 const TypedefType *TT = T->getAsTypedefType();
2595 if (!TT)
2596 return;
2597 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002598
2599 // typedef struct objc_selector *SEL;
2600 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002601 if (!ptr)
2602 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002603 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002604 if (!rec)
2605 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002606 SelStructType = rec;
2607}
2608
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002609void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002610{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002611 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002612}
2613
Douglas Gregor319ac892009-04-23 22:29:11 +00002614void ASTContext::setObjCClassType(QualType T)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002615{
Douglas Gregor319ac892009-04-23 22:29:11 +00002616 ObjCClassType = T;
2617
2618 const TypedefType *TT = T->getAsTypedefType();
2619 if (!TT)
2620 return;
2621 TypedefDecl *TD = TT->getDecl();
Anders Carlsson8baaca52007-10-31 02:53:19 +00002622
2623 // typedef struct objc_class *Class;
2624 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2625 assert(ptr && "'Class' incorrectly typed");
2626 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2627 assert(rec && "'Class' incorrectly typed");
2628 ClassStructType = rec;
2629}
2630
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002631void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2632 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002633 "'NSConstantString' type already set!");
2634
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002635 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002636}
2637
Douglas Gregor7532dc62009-03-30 22:58:21 +00002638/// \brief Retrieve the template name that represents a qualified
2639/// template name such as \c std::vector.
2640TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2641 bool TemplateKeyword,
2642 TemplateDecl *Template) {
2643 llvm::FoldingSetNodeID ID;
2644 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2645
2646 void *InsertPos = 0;
2647 QualifiedTemplateName *QTN =
2648 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2649 if (!QTN) {
2650 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2651 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2652 }
2653
2654 return TemplateName(QTN);
2655}
2656
2657/// \brief Retrieve the template name that represents a dependent
2658/// template name such as \c MetaFun::template apply.
2659TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2660 const IdentifierInfo *Name) {
2661 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2662
2663 llvm::FoldingSetNodeID ID;
2664 DependentTemplateName::Profile(ID, NNS, Name);
2665
2666 void *InsertPos = 0;
2667 DependentTemplateName *QTN =
2668 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2669
2670 if (QTN)
2671 return TemplateName(QTN);
2672
2673 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2674 if (CanonNNS == NNS) {
2675 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2676 } else {
2677 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2678 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2679 }
2680
2681 DependentTemplateNames.InsertNode(QTN, InsertPos);
2682 return TemplateName(QTN);
2683}
2684
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002685/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002686/// TargetInfo, produce the corresponding type. The unsigned @p Type
2687/// is actually a value of type @c TargetInfo::IntType.
2688QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002689 switch (Type) {
2690 case TargetInfo::NoInt: return QualType();
2691 case TargetInfo::SignedShort: return ShortTy;
2692 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2693 case TargetInfo::SignedInt: return IntTy;
2694 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2695 case TargetInfo::SignedLong: return LongTy;
2696 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2697 case TargetInfo::SignedLongLong: return LongLongTy;
2698 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2699 }
2700
2701 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002702 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002703}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002704
2705//===----------------------------------------------------------------------===//
2706// Type Predicates.
2707//===----------------------------------------------------------------------===//
2708
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002709/// isObjCNSObjectType - Return true if this is an NSObject object using
2710/// NSObject attribute on a c-style pointer type.
2711/// FIXME - Make it work directly on types.
2712///
2713bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2714 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2715 if (TypedefDecl *TD = TDT->getDecl())
2716 if (TD->getAttr<ObjCNSObjectAttr>())
2717 return true;
2718 }
2719 return false;
2720}
2721
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002722/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2723/// to an object type. This includes "id" and "Class" (two 'special' pointers
2724/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2725/// ID type).
2726bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002727 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002728 return true;
2729
Steve Naroff6ae98502008-10-21 18:24:04 +00002730 // Blocks are objects.
2731 if (Ty->isBlockPointerType())
2732 return true;
2733
2734 // All other object types are pointers.
Chris Lattner16ede0e2009-04-12 23:51:02 +00002735 const PointerType *PT = Ty->getAsPointerType();
2736 if (PT == 0)
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002737 return false;
2738
Chris Lattner16ede0e2009-04-12 23:51:02 +00002739 // If this a pointer to an interface (e.g. NSString*), it is ok.
2740 if (PT->getPointeeType()->isObjCInterfaceType() ||
2741 // If is has NSObject attribute, OK as well.
2742 isObjCNSObjectType(Ty))
2743 return true;
2744
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002745 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2746 // pointer types. This looks for the typedef specifically, not for the
Chris Lattner16ede0e2009-04-12 23:51:02 +00002747 // underlying type. Iteratively strip off typedefs so that we can handle
2748 // typedefs of typedefs.
2749 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2750 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2751 Ty.getUnqualifiedType() == getObjCClassType())
2752 return true;
2753
2754 Ty = TDT->getDecl()->getUnderlyingType();
2755 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002756
Chris Lattner16ede0e2009-04-12 23:51:02 +00002757 return false;
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002758}
2759
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002760/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2761/// garbage collection attribute.
2762///
2763QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002764 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002765 if (getLangOptions().ObjC1 &&
2766 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002767 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002768 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002769 // (or pointers to them) be treated as though they were declared
2770 // as __strong.
2771 if (GCAttrs == QualType::GCNone) {
2772 if (isObjCObjectPointerType(Ty))
2773 GCAttrs = QualType::Strong;
2774 else if (Ty->isPointerType())
2775 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2776 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00002777 // Non-pointers have none gc'able attribute regardless of the attribute
2778 // set on them.
2779 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2780 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002781 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002782 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002783}
2784
Chris Lattner6ac46a42008-04-07 06:51:04 +00002785//===----------------------------------------------------------------------===//
2786// Type Compatibility Testing
2787//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002788
Steve Naroff1c7d0672008-09-04 15:10:53 +00002789/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002790/// block types. Types must be strictly compatible here. For example,
2791/// C unfortunately doesn't produce an error for the following:
2792///
2793/// int (*emptyArgFunc)();
2794/// int (*intArgList)(int) = emptyArgFunc;
2795///
2796/// For blocks, we will produce an error for the following (similar to C++):
2797///
2798/// int (^emptyArgBlock)();
2799/// int (^intArgBlock)(int) = emptyArgBlock;
2800///
2801/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2802///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002803bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002804 const FunctionType *lbase = lhs->getAsFunctionType();
2805 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002806 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2807 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stumpaab0f7a2009-04-01 01:17:39 +00002808 if (lproto && rproto == 0)
2809 return false;
2810 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff1c7d0672008-09-04 15:10:53 +00002811}
2812
Chris Lattner6ac46a42008-04-07 06:51:04 +00002813/// areCompatVectorTypes - Return true if the two specified vector types are
2814/// compatible.
2815static bool areCompatVectorTypes(const VectorType *LHS,
2816 const VectorType *RHS) {
2817 assert(LHS->isCanonical() && RHS->isCanonical());
2818 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002819 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002820}
2821
Eli Friedman3d815e72008-08-22 00:56:42 +00002822/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002823/// compatible for assignment from RHS to LHS. This handles validation of any
2824/// protocol qualifiers on the LHS or RHS.
2825///
Eli Friedman3d815e72008-08-22 00:56:42 +00002826bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2827 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002828 // Verify that the base decls are compatible: the RHS must be a subclass of
2829 // the LHS.
2830 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2831 return false;
2832
2833 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2834 // protocol qualified at all, then we are good.
2835 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2836 return true;
2837
2838 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2839 // isn't a superset.
2840 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2841 return true; // FIXME: should return false!
2842
2843 // Finally, we must have two protocol-qualified interfaces.
2844 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2845 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002846
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002847 // All LHS protocols must have a presence on the RHS.
2848 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002849
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002850 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2851 LHSPE = LHSP->qual_end();
2852 LHSPI != LHSPE; LHSPI++) {
2853 bool RHSImplementsProtocol = false;
2854
2855 // If the RHS doesn't implement the protocol on the left, the types
2856 // are incompatible.
2857 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2858 RHSPE = RHSP->qual_end();
2859 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2860 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2861 RHSImplementsProtocol = true;
2862 }
2863 // FIXME: For better diagnostics, consider passing back the protocol name.
2864 if (!RHSImplementsProtocol)
2865 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002866 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002867 // The RHS implements all protocols listed on the LHS.
2868 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002869}
2870
Steve Naroff389bf462009-02-12 17:52:19 +00002871bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2872 // get the "pointed to" types
2873 const PointerType *LHSPT = LHS->getAsPointerType();
2874 const PointerType *RHSPT = RHS->getAsPointerType();
2875
2876 if (!LHSPT || !RHSPT)
2877 return false;
2878
2879 QualType lhptee = LHSPT->getPointeeType();
2880 QualType rhptee = RHSPT->getPointeeType();
2881 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2882 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2883 // ID acts sort of like void* for ObjC interfaces
2884 if (LHSIface && isObjCIdStructType(rhptee))
2885 return true;
2886 if (RHSIface && isObjCIdStructType(lhptee))
2887 return true;
2888 if (!LHSIface || !RHSIface)
2889 return false;
2890 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2891 canAssignObjCInterfaces(RHSIface, LHSIface);
2892}
2893
Steve Naroffec0550f2007-10-15 20:41:53 +00002894/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2895/// both shall have the identically qualified version of a compatible type.
2896/// C99 6.2.7p1: Two types have compatible types if their types are the
2897/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002898bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2899 return !mergeTypes(LHS, RHS).isNull();
2900}
2901
2902QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2903 const FunctionType *lbase = lhs->getAsFunctionType();
2904 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002905 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2906 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002907 bool allLTypes = true;
2908 bool allRTypes = true;
2909
2910 // Check return type
2911 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2912 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002913 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2914 allLTypes = false;
2915 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2916 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002917
2918 if (lproto && rproto) { // two C99 style function prototypes
2919 unsigned lproto_nargs = lproto->getNumArgs();
2920 unsigned rproto_nargs = rproto->getNumArgs();
2921
2922 // Compatible functions must have the same number of arguments
2923 if (lproto_nargs != rproto_nargs)
2924 return QualType();
2925
2926 // Variadic and non-variadic functions aren't compatible
2927 if (lproto->isVariadic() != rproto->isVariadic())
2928 return QualType();
2929
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002930 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2931 return QualType();
2932
Eli Friedman3d815e72008-08-22 00:56:42 +00002933 // Check argument compatibility
2934 llvm::SmallVector<QualType, 10> types;
2935 for (unsigned i = 0; i < lproto_nargs; i++) {
2936 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2937 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2938 QualType argtype = mergeTypes(largtype, rargtype);
2939 if (argtype.isNull()) return QualType();
2940 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002941 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2942 allLTypes = false;
2943 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2944 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002945 }
2946 if (allLTypes) return lhs;
2947 if (allRTypes) return rhs;
2948 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002949 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002950 }
2951
2952 if (lproto) allRTypes = false;
2953 if (rproto) allLTypes = false;
2954
Douglas Gregor72564e72009-02-26 23:50:07 +00002955 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002956 if (proto) {
2957 if (proto->isVariadic()) return QualType();
2958 // Check that the types are compatible with the types that
2959 // would result from default argument promotions (C99 6.7.5.3p15).
2960 // The only types actually affected are promotable integer
2961 // types and floats, which would be passed as a different
2962 // type depending on whether the prototype is visible.
2963 unsigned proto_nargs = proto->getNumArgs();
2964 for (unsigned i = 0; i < proto_nargs; ++i) {
2965 QualType argTy = proto->getArgType(i);
2966 if (argTy->isPromotableIntegerType() ||
2967 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2968 return QualType();
2969 }
2970
2971 if (allLTypes) return lhs;
2972 if (allRTypes) return rhs;
2973 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002974 proto->getNumArgs(), lproto->isVariadic(),
2975 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002976 }
2977
2978 if (allLTypes) return lhs;
2979 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002980 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002981}
2982
2983QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002984 // C++ [expr]: If an expression initially has the type "reference to T", the
2985 // type is adjusted to "T" prior to any further analysis, the expression
2986 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002987 // expression is an lvalue unless the reference is an rvalue reference and
2988 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00002989 // FIXME: C++ shouldn't be going through here! The rules are different
2990 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002991 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2992 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00002993 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002994 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002995 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002996 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002997
Eli Friedman3d815e72008-08-22 00:56:42 +00002998 QualType LHSCan = getCanonicalType(LHS),
2999 RHSCan = getCanonicalType(RHS);
3000
3001 // If two types are identical, they are compatible.
3002 if (LHSCan == RHSCan)
3003 return LHS;
3004
3005 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003006 // Note that we handle extended qualifiers later, in the
3007 // case for ExtQualType.
3008 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00003009 return QualType();
3010
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003011 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
3012 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003013
Chris Lattner1adb8832008-01-14 05:45:46 +00003014 // We want to consider the two function types to be the same for these
3015 // comparisons, just force one to the other.
3016 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3017 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003018
3019 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003020 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3021 LHSClass = Type::ConstantArray;
3022 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3023 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00003024
Nate Begeman213541a2008-04-18 23:10:10 +00003025 // Canonicalize ExtVector -> Vector.
3026 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3027 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00003028
Chris Lattnerb0489812008-04-07 06:38:24 +00003029 // Consider qualified interfaces and interfaces the same.
3030 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
3031 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00003032
Chris Lattnera36a61f2008-04-07 05:43:21 +00003033 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003034 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003035 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3036 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003037
Steve Naroffd824c9c2009-04-14 15:11:46 +00003038 // 'id' and 'Class' act sort of like void* for ObjC interfaces
3039 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003040 return LHS;
Steve Naroffd824c9c2009-04-14 15:11:46 +00003041 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003042 return RHS;
3043
Steve Naroffbc76dd02008-12-10 22:14:21 +00003044 // ID is compatible with all qualified id types.
3045 if (LHS->isObjCQualifiedIdType()) {
3046 if (const PointerType *PT = RHS->getAsPointerType()) {
3047 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003048 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003049 return LHS;
3050 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3051 // Unfortunately, this API is part of Sema (which we don't have access
3052 // to. Need to refactor. The following check is insufficient, since we
3053 // need to make sure the class implements the protocol.
3054 if (pType->isObjCInterfaceType())
3055 return LHS;
3056 }
3057 }
3058 if (RHS->isObjCQualifiedIdType()) {
3059 if (const PointerType *PT = LHS->getAsPointerType()) {
3060 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003061 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003062 return RHS;
3063 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3064 // Unfortunately, this API is part of Sema (which we don't have access
3065 // to. Need to refactor. The following check is insufficient, since we
3066 // need to make sure the class implements the protocol.
3067 if (pType->isObjCInterfaceType())
3068 return RHS;
3069 }
3070 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003071 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3072 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00003073 if (const EnumType* ETy = LHS->getAsEnumType()) {
3074 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3075 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003076 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003077 if (const EnumType* ETy = RHS->getAsEnumType()) {
3078 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3079 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003080 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003081
Eli Friedman3d815e72008-08-22 00:56:42 +00003082 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003083 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003084
Steve Naroff4a746782008-01-09 22:43:08 +00003085 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003086 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003087#define TYPE(Class, Base)
3088#define ABSTRACT_TYPE(Class, Base)
3089#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3090#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3091#include "clang/AST/TypeNodes.def"
3092 assert(false && "Non-canonical and dependent types shouldn't get here");
3093 return QualType();
3094
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003095 case Type::LValueReference:
3096 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003097 case Type::MemberPointer:
3098 assert(false && "C++ should never be in mergeTypes");
3099 return QualType();
3100
3101 case Type::IncompleteArray:
3102 case Type::VariableArray:
3103 case Type::FunctionProto:
3104 case Type::ExtVector:
3105 case Type::ObjCQualifiedInterface:
3106 assert(false && "Types are eliminated above");
3107 return QualType();
3108
Chris Lattner1adb8832008-01-14 05:45:46 +00003109 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003110 {
3111 // Merge two pointer types, while trying to preserve typedef info
3112 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3113 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3114 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3115 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003116 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3117 return LHS;
3118 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3119 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003120 return getPointerType(ResultType);
3121 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003122 case Type::BlockPointer:
3123 {
3124 // Merge two block pointer types, while trying to preserve typedef info
3125 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3126 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3127 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3128 if (ResultType.isNull()) return QualType();
3129 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3130 return LHS;
3131 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3132 return RHS;
3133 return getBlockPointerType(ResultType);
3134 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003135 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003136 {
3137 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3138 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3139 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3140 return QualType();
3141
3142 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3143 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3144 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3145 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003146 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3147 return LHS;
3148 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3149 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003150 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3151 ArrayType::ArraySizeModifier(), 0);
3152 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3153 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003154 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3155 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003156 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3157 return LHS;
3158 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3159 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003160 if (LVAT) {
3161 // FIXME: This isn't correct! But tricky to implement because
3162 // the array's size has to be the size of LHS, but the type
3163 // has to be different.
3164 return LHS;
3165 }
3166 if (RVAT) {
3167 // FIXME: This isn't correct! But tricky to implement because
3168 // the array's size has to be the size of RHS, but the type
3169 // has to be different.
3170 return RHS;
3171 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003172 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3173 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003174 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003175 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003176 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003177 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003178 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003179 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003180 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003181 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3182 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003183 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003184 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003185 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003186 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003187 case Type::Complex:
3188 // Distinct complex types are incompatible.
3189 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003190 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003191 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003192 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3193 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003194 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003195 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003196 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003197 // FIXME: This should be type compatibility, e.g. whether
3198 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003199 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3200 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3201 if (LHSIface && RHSIface &&
3202 canAssignObjCInterfaces(LHSIface, RHSIface))
3203 return LHS;
3204
Eli Friedman3d815e72008-08-22 00:56:42 +00003205 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003206 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003207 case Type::ObjCQualifiedId:
3208 // Distinct qualified id's are not compatible.
3209 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003210 case Type::FixedWidthInt:
3211 // Distinct fixed-width integers are not compatible.
3212 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003213 case Type::ExtQual:
3214 // FIXME: ExtQual types can be compatible even if they're not
3215 // identical!
3216 return QualType();
3217 // First attempt at an implementation, but I'm not really sure it's
3218 // right...
3219#if 0
3220 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3221 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3222 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3223 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3224 return QualType();
3225 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3226 LHSBase = QualType(LQual->getBaseType(), 0);
3227 RHSBase = QualType(RQual->getBaseType(), 0);
3228 ResultType = mergeTypes(LHSBase, RHSBase);
3229 if (ResultType.isNull()) return QualType();
3230 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3231 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3232 return LHS;
3233 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3234 return RHS;
3235 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3236 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3237 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3238 return ResultType;
3239#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003240
3241 case Type::TemplateSpecialization:
3242 assert(false && "Dependent types have no size");
3243 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003244 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003245
3246 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003247}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003248
Chris Lattner5426bf62008-04-07 07:01:58 +00003249//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003250// Integer Predicates
3251//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003252
Eli Friedmanad74a752008-06-28 06:23:08 +00003253unsigned ASTContext::getIntWidth(QualType T) {
3254 if (T == BoolTy)
3255 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003256 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3257 return FWIT->getWidth();
3258 }
3259 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003260 return (unsigned)getTypeSize(T);
3261}
3262
3263QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3264 assert(T->isSignedIntegerType() && "Unexpected type");
3265 if (const EnumType* ETy = T->getAsEnumType())
3266 T = ETy->getDecl()->getIntegerType();
3267 const BuiltinType* BTy = T->getAsBuiltinType();
3268 assert (BTy && "Unexpected signed integer type");
3269 switch (BTy->getKind()) {
3270 case BuiltinType::Char_S:
3271 case BuiltinType::SChar:
3272 return UnsignedCharTy;
3273 case BuiltinType::Short:
3274 return UnsignedShortTy;
3275 case BuiltinType::Int:
3276 return UnsignedIntTy;
3277 case BuiltinType::Long:
3278 return UnsignedLongTy;
3279 case BuiltinType::LongLong:
3280 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00003281 case BuiltinType::Int128:
3282 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00003283 default:
3284 assert(0 && "Unexpected signed integer type");
3285 return QualType();
3286 }
3287}
3288
Douglas Gregor2cf26342009-04-09 22:27:44 +00003289ExternalASTSource::~ExternalASTSource() { }
3290
3291void ExternalASTSource::PrintStats() { }