blob: 133e8c20b885928355467126a3ed272487eb809b [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000019#include "clang/AST/ExternalASTSource.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000021#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000023#include "llvm/ADT/StringExtras.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000025#include "llvm/Support/MemoryBuffer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner61710852008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Douglas Gregor2deaea32009-04-22 18:49:13 +000035 bool FreeMem, unsigned size_reserve,
36 bool InitializeBuiltins) :
Douglas Gregorab452ba2009-03-26 23:50:42 +000037 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
38 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Douglas Gregor2cf26342009-04-09 22:27:44 +000039 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels),
40 ExternalSource(0) {
Daniel Dunbare91593e2008-08-11 04:54:23 +000041 if (size_reserve > 0) Types.reserve(size_reserve);
42 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
Douglas Gregor7a9cbed2009-04-26 03:57:37 +000044 BuiltinInfo.InitializeTargetBuiltins(Target);
Douglas Gregor2deaea32009-04-22 18:49:13 +000045 if (InitializeBuiltins)
46 this->InitializeBuiltins(idents);
Daniel Dunbare91593e2008-08-11 04:54:23 +000047}
48
Reid Spencer5f016e22007-07-11 17:01:13 +000049ASTContext::~ASTContext() {
50 // Deallocate all the types.
51 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000052 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000053 Types.pop_back();
54 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000055
Nuno Lopesb74668e2008-12-17 22:30:25 +000056 {
57 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
58 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
59 while (I != E) {
60 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
61 delete R;
62 }
63 }
64
65 {
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +000066 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
67 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopesb74668e2008-12-17 22:30:25 +000068 while (I != E) {
69 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
70 delete R;
71 }
72 }
73
Douglas Gregorab452ba2009-03-26 23:50:42 +000074 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000075 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
76 NNS = NestedNameSpecifiers.begin(),
77 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregore7dcd782009-03-27 23:25:45 +000078 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000079 /* Increment in loop */)
80 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000081
82 if (GlobalNestedNameSpecifier)
83 GlobalNestedNameSpecifier->Destroy(*this);
84
Eli Friedmanb26153c2008-05-27 03:08:09 +000085 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000086}
87
Douglas Gregor2deaea32009-04-22 18:49:13 +000088void ASTContext::InitializeBuiltins(IdentifierTable &idents) {
Douglas Gregor2deaea32009-04-22 18:49:13 +000089 BuiltinInfo.InitializeBuiltins(idents, LangOpts.NoBuiltin);
90}
91
Douglas Gregor2cf26342009-04-09 22:27:44 +000092void
93ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
94 ExternalSource.reset(Source.take());
95}
96
Reid Spencer5f016e22007-07-11 17:01:13 +000097void ASTContext::PrintStats() const {
98 fprintf(stderr, "*** AST Context Stats:\n");
99 fprintf(stderr, " %d types total.\n", (int)Types.size());
100 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000101 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000102 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
103 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
104
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000106 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
107 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000108 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000109 unsigned NumExtQual = 0;
110
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
112 Type *T = Types[i];
113 if (isa<BuiltinType>(T))
114 ++NumBuiltin;
115 else if (isa<PointerType>(T))
116 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000117 else if (isa<BlockPointerType>(T))
118 ++NumBlockPointer;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000119 else if (isa<LValueReferenceType>(T))
120 ++NumLValueReference;
121 else if (isa<RValueReferenceType>(T))
122 ++NumRValueReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000123 else if (isa<MemberPointerType>(T))
124 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000125 else if (isa<ComplexType>(T))
126 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 else if (isa<ArrayType>(T))
128 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000129 else if (isa<VectorType>(T))
130 ++NumVector;
Douglas Gregor72564e72009-02-26 23:50:07 +0000131 else if (isa<FunctionNoProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 ++NumFunctionNP;
Douglas Gregor72564e72009-02-26 23:50:07 +0000133 else if (isa<FunctionProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 ++NumFunctionP;
135 else if (isa<TypedefType>(T))
136 ++NumTypeName;
137 else if (TagType *TT = dyn_cast<TagType>(T)) {
138 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000139 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000141 case TagDecl::TK_struct: ++NumTagStruct; break;
142 case TagDecl::TK_union: ++NumTagUnion; break;
143 case TagDecl::TK_class: ++NumTagClass; break;
144 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000146 } else if (isa<ObjCInterfaceType>(T))
147 ++NumObjCInterfaces;
148 else if (isa<ObjCQualifiedInterfaceType>(T))
149 ++NumObjCQualifiedInterfaces;
150 else if (isa<ObjCQualifiedIdType>(T))
151 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000152 else if (isa<TypeOfType>(T))
153 ++NumTypeOfTypes;
Douglas Gregor72564e72009-02-26 23:50:07 +0000154 else if (isa<TypeOfExprType>(T))
155 ++NumTypeOfExprTypes;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000156 else if (isa<ExtQualType>(T))
157 ++NumExtQual;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000158 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000159 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 assert(0 && "Unknown type!");
161 }
162 }
163
164 fprintf(stderr, " %d builtin types\n", NumBuiltin);
165 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000166 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000167 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
168 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000169 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000170 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000172 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
174 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
175 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
176 fprintf(stderr, " %d tagged types\n", NumTagged);
177 fprintf(stderr, " %d struct types\n", NumTagStruct);
178 fprintf(stderr, " %d union types\n", NumTagUnion);
179 fprintf(stderr, " %d class types\n", NumTagClass);
180 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000181 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000182 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000183 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000184 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000185 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000186 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor72564e72009-02-26 23:50:07 +0000187 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000188 fprintf(stderr, " %d attribute-qualified types\n", NumExtQual);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000189
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
191 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000192 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000193 NumLValueReference*sizeof(LValueReferenceType)+
194 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000195 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000196 NumFunctionP*sizeof(FunctionProtoType)+
197 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000198 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000199 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)+
200 NumExtQual*sizeof(ExtQualType)));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000201
202 if (ExternalSource.get()) {
203 fprintf(stderr, "\n");
204 ExternalSource->PrintStats();
205 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000206}
207
208
209void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000210 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000211}
212
Reid Spencer5f016e22007-07-11 17:01:13 +0000213void ASTContext::InitBuiltinTypes() {
214 assert(VoidTy.isNull() && "Context reinitialized?");
215
216 // C99 6.2.5p19.
217 InitBuiltinType(VoidTy, BuiltinType::Void);
218
219 // C99 6.2.5p2.
220 InitBuiltinType(BoolTy, BuiltinType::Bool);
221 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000222 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 InitBuiltinType(CharTy, BuiltinType::Char_S);
224 else
225 InitBuiltinType(CharTy, BuiltinType::Char_U);
226 // C99 6.2.5p4.
227 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
228 InitBuiltinType(ShortTy, BuiltinType::Short);
229 InitBuiltinType(IntTy, BuiltinType::Int);
230 InitBuiltinType(LongTy, BuiltinType::Long);
231 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
232
233 // C99 6.2.5p6.
234 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
235 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
236 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
237 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
238 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
239
240 // C99 6.2.5p10.
241 InitBuiltinType(FloatTy, BuiltinType::Float);
242 InitBuiltinType(DoubleTy, BuiltinType::Double);
243 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000244
Chris Lattner2df9ced2009-04-30 02:43:43 +0000245 // GNU extension, 128-bit integers.
246 InitBuiltinType(Int128Ty, BuiltinType::Int128);
247 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
248
Chris Lattner3a250322009-02-26 23:43:47 +0000249 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
250 InitBuiltinType(WCharTy, BuiltinType::WChar);
251 else // C99
252 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000253
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000254 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000255 InitBuiltinType(OverloadTy, BuiltinType::Overload);
256
257 // Placeholder type for type-dependent expressions whose type is
258 // completely unknown. No code should ever check a type against
259 // DependentTy and users should never see it; however, it is here to
260 // help diagnose failures to properly check for type-dependent
261 // expressions.
262 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000263
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 // C99 6.2.5p11.
265 FloatComplexTy = getComplexType(FloatTy);
266 DoubleComplexTy = getComplexType(DoubleTy);
267 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000268
Steve Naroff7e219e42007-10-15 14:41:52 +0000269 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000270 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000271 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000272 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000273 ClassStructType = 0;
274
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000275 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000276
277 // void * type
278 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000279
280 // nullptr type (C++0x 2.14.7)
281 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000282}
283
Chris Lattner464175b2007-07-18 17:52:12 +0000284//===----------------------------------------------------------------------===//
285// Type Sizing and Analysis
286//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000287
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000288/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
289/// scalar floating point type.
290const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
291 const BuiltinType *BT = T->getAsBuiltinType();
292 assert(BT && "Not a floating point type!");
293 switch (BT->getKind()) {
294 default: assert(0 && "Not a floating point type!");
295 case BuiltinType::Float: return Target.getFloatFormat();
296 case BuiltinType::Double: return Target.getDoubleFormat();
297 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
298 }
299}
300
Chris Lattneraf707ab2009-01-24 21:53:27 +0000301/// getDeclAlign - Return a conservative estimate of the alignment of the
302/// specified decl. Note that bitfields do not have a valid alignment, so
303/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000304unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000305 unsigned Align = Target.getCharWidth();
306
307 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
308 Align = std::max(Align, AA->getAlignment());
309
Chris Lattneraf707ab2009-01-24 21:53:27 +0000310 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
311 QualType T = VD->getType();
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000312 if (const ReferenceType* RT = T->getAsReferenceType()) {
313 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000314 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000315 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
316 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000317 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
318 T = cast<ArrayType>(T)->getElementType();
319
320 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
321 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000322 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000323
324 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000325}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000326
Chris Lattnera7674d82007-07-13 22:13:22 +0000327/// getTypeSize - Return the size of the specified type, in bits. This method
328/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000329std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000330ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000331 uint64_t Width=0;
332 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000333 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000334#define TYPE(Class, Base)
335#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000336#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000337#define DEPENDENT_TYPE(Class, Base) case Type::Class:
338#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000339 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000340 break;
341
Chris Lattner692233e2007-07-13 22:27:08 +0000342 case Type::FunctionNoProto:
343 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000344 // GCC extension: alignof(function) = 32 bits
345 Width = 0;
346 Align = 32;
347 break;
348
Douglas Gregor72564e72009-02-26 23:50:07 +0000349 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000350 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000351 Width = 0;
352 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
353 break;
354
Steve Narofffb22d962007-08-30 01:06:46 +0000355 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000356 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000357
Chris Lattner98be4942008-03-05 18:54:05 +0000358 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000359 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000360 Align = EltInfo.second;
361 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000362 }
Nate Begeman213541a2008-04-18 23:10:10 +0000363 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000364 case Type::Vector: {
365 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000366 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000367 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000368 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000369 // If the alignment is not a power of 2, round up to the next power of 2.
370 // This happens for non-power-of-2 length vectors.
371 // FIXME: this should probably be a target property.
372 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000373 break;
374 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000375
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000376 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000377 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000378 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000379 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000380 // GCC extension: alignof(void) = 8 bits.
381 Width = 0;
382 Align = 8;
383 break;
384
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000385 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000386 Width = Target.getBoolWidth();
387 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000388 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000389 case BuiltinType::Char_S:
390 case BuiltinType::Char_U:
391 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000392 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000393 Width = Target.getCharWidth();
394 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000395 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000396 case BuiltinType::WChar:
397 Width = Target.getWCharWidth();
398 Align = Target.getWCharAlign();
399 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000400 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000401 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000402 Width = Target.getShortWidth();
403 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000404 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000405 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000406 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000407 Width = Target.getIntWidth();
408 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000409 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000410 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000411 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000412 Width = Target.getLongWidth();
413 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000414 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000415 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000416 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000417 Width = Target.getLongLongWidth();
418 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000419 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000420 case BuiltinType::Int128:
421 case BuiltinType::UInt128:
422 Width = 128;
423 Align = 128; // int128_t is 128-bit aligned on all targets.
424 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000425 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000426 Width = Target.getFloatWidth();
427 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000428 break;
429 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000430 Width = Target.getDoubleWidth();
431 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000432 break;
433 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000434 Width = Target.getLongDoubleWidth();
435 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000436 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000437 case BuiltinType::NullPtr:
438 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
439 Align = Target.getPointerAlign(0); // == sizeof(void*)
Chris Lattnera7674d82007-07-13 22:13:22 +0000440 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000441 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000442 case Type::FixedWidthInt:
443 // FIXME: This isn't precisely correct; the width/alignment should depend
444 // on the available types for the target
445 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000446 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000447 Align = Width;
448 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000449 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000450 // FIXME: Pointers into different addr spaces could have different sizes and
451 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000452 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000453 case Type::ObjCQualifiedId:
Douglas Gregor72564e72009-02-26 23:50:07 +0000454 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000455 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000456 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000457 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000458 case Type::BlockPointer: {
459 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
460 Width = Target.getPointerWidth(AS);
461 Align = Target.getPointerAlign(AS);
462 break;
463 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000464 case Type::Pointer: {
465 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000466 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000467 Align = Target.getPointerAlign(AS);
468 break;
469 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000470 case Type::LValueReference:
471 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000472 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000473 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000474 // FIXME: This is wrong for struct layout: a reference in a struct has
475 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000476 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000477 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000478 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000479 // the GCC ABI, where pointers to data are one pointer large, pointers to
480 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000481 // other compilers too, we need to delegate this completely to TargetInfo
482 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000483 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
484 unsigned AS = Pointee.getAddressSpace();
485 Width = Target.getPointerWidth(AS);
486 if (Pointee->isFunctionType())
487 Width *= 2;
488 Align = Target.getPointerAlign(AS);
489 // GCC aligns at single pointer width.
490 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000491 case Type::Complex: {
492 // Complex types have the same alignment as their elements, but twice the
493 // size.
494 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000495 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000496 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000497 Align = EltInfo.second;
498 break;
499 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000500 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000501 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000502 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
503 Width = Layout.getSize();
504 Align = Layout.getAlignment();
505 break;
506 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000507 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000508 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000509 const TagType *TT = cast<TagType>(T);
510
511 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000512 Width = 1;
513 Align = 1;
514 break;
515 }
516
Daniel Dunbar1d751182008-11-08 05:48:37 +0000517 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000518 return getTypeInfo(ET->getDecl()->getIntegerType());
519
Daniel Dunbar1d751182008-11-08 05:48:37 +0000520 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000521 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
522 Width = Layout.getSize();
523 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000524 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000525 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000526
Douglas Gregor18857642009-04-30 17:32:17 +0000527 case Type::Typedef: {
528 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
529 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
530 Align = Aligned->getAlignment();
531 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
532 } else
533 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000534 break;
Chris Lattner71763312008-04-06 22:05:18 +0000535 }
Douglas Gregor18857642009-04-30 17:32:17 +0000536
537 case Type::TypeOfExpr:
538 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
539 .getTypePtr());
540
541 case Type::TypeOf:
542 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
543
544 case Type::QualifiedName:
545 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
546
547 case Type::TemplateSpecialization:
548 assert(getCanonicalType(T) != T &&
549 "Cannot request the size of a dependent type");
550 // FIXME: this is likely to be wrong once we support template
551 // aliases, since a template alias could refer to a typedef that
552 // has an __aligned__ attribute on it.
553 return getTypeInfo(getCanonicalType(T));
554 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000555
Chris Lattner464175b2007-07-18 17:52:12 +0000556 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000557 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000558}
559
Chris Lattner34ebde42009-01-27 18:08:34 +0000560/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
561/// type for the current target in bits. This can be different than the ABI
562/// alignment in cases where it is beneficial for performance to overalign
563/// a data type.
564unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
565 unsigned ABIAlign = getTypeAlign(T);
566
567 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000568 if (T->isSpecificBuiltinType(BuiltinType::Double))
569 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000570
571 return ABIAlign;
572}
573
574
Devang Patel8b277042008-06-04 21:22:16 +0000575/// LayoutField - Field layout.
576void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000577 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000578 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000579 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000580 uint64_t FieldOffset = IsUnion ? 0 : Size;
581 uint64_t FieldSize;
582 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000583
584 // FIXME: Should this override struct packing? Probably we want to
585 // take the minimum?
586 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
587 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000588
589 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
590 // TODO: Need to check this algorithm on other targets!
591 // (tested on Linux-X86)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000592 FieldSize = BitWidthExpr->EvaluateAsInt(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000593
594 std::pair<uint64_t, unsigned> FieldInfo =
595 Context.getTypeInfo(FD->getType());
596 uint64_t TypeSize = FieldInfo.first;
597
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000598 // Determine the alignment of this bitfield. The packing
599 // attributes define a maximum and the alignment attribute defines
600 // a minimum.
601 // FIXME: What is the right behavior when the specified alignment
602 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000603 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000604 if (FieldPacking)
605 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000606 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
607 FieldAlign = std::max(FieldAlign, AA->getAlignment());
608
609 // Check if we need to add padding to give the field the correct
610 // alignment.
611 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
612 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
613
614 // Padding members don't affect overall alignment
615 if (!FD->getIdentifier())
616 FieldAlign = 1;
617 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000618 if (FD->getType()->isIncompleteArrayType()) {
619 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000620 // query getTypeInfo about these, so we figure it out here.
621 // Flexible array members don't have any size, but they
622 // have to be aligned appropriately for their element type.
623 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000624 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000625 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson2f1169f2009-04-10 05:31:15 +0000626 } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
627 unsigned AS = RT->getPointeeType().getAddressSpace();
628 FieldSize = Context.Target.getPointerWidth(AS);
629 FieldAlign = Context.Target.getPointerAlign(AS);
Devang Patel8b277042008-06-04 21:22:16 +0000630 } else {
631 std::pair<uint64_t, unsigned> FieldInfo =
632 Context.getTypeInfo(FD->getType());
633 FieldSize = FieldInfo.first;
634 FieldAlign = FieldInfo.second;
635 }
636
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000637 // Determine the alignment of this bitfield. The packing
638 // attributes define a maximum and the alignment attribute defines
639 // a minimum. Additionally, the packing alignment must be at least
640 // a byte for non-bitfields.
641 //
642 // FIXME: What is the right behavior when the specified alignment
643 // is smaller than the specified packing?
644 if (FieldPacking)
645 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000646 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
647 FieldAlign = std::max(FieldAlign, AA->getAlignment());
648
649 // Round up the current record size to the field's alignment boundary.
650 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
651 }
652
653 // Place this field at the current location.
654 FieldOffsets[FieldNo] = FieldOffset;
655
656 // Reserve space for this field.
657 if (IsUnion) {
658 Size = std::max(Size, FieldSize);
659 } else {
660 Size = FieldOffset + FieldSize;
661 }
662
Daniel Dunbard6884a02009-05-04 05:16:21 +0000663 // Remember the next available offset.
664 NextOffset = Size;
665
Devang Patel8b277042008-06-04 21:22:16 +0000666 // Remember max struct/class alignment.
667 Alignment = std::max(Alignment, FieldAlign);
668}
669
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000670static void CollectLocalObjCIvars(ASTContext *Ctx,
671 const ObjCInterfaceDecl *OI,
672 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000673 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
674 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000675 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000676 if (!IVDecl->isInvalidDecl())
677 Fields.push_back(cast<FieldDecl>(IVDecl));
678 }
679}
680
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000681void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
682 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
683 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
684 CollectObjCIvars(SuperClass, Fields);
685 CollectLocalObjCIvars(this, OI, Fields);
686}
687
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000688/// getInterfaceLayoutImpl - Get or compute information about the
689/// layout of the given interface.
690///
691/// \param Impl - If given, also include the layout of the interface's
692/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000693const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000694ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
695 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000696 assert(!D->isForwardDecl() && "Invalid interface decl!");
697
Devang Patel44a3dde2008-06-04 21:54:36 +0000698 // Look up this layout, if already laid out, return what we have.
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000699 ObjCContainerDecl *Key =
700 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
701 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
702 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000703
Daniel Dunbar453addb2009-05-03 11:16:44 +0000704 unsigned FieldCount = D->ivar_size();
705 // Add in synthesized ivar count if laying out an implementation.
706 if (Impl) {
707 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
708 E = D->prop_end(*this); I != E; ++I)
709 if ((*I)->getPropertyIvarDecl())
710 ++FieldCount;
711
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000712 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +0000713 // entry. Note we can't cache this because we simply free all
714 // entries later; however we shouldn't look up implementations
715 // frequently.
716 if (FieldCount == D->ivar_size())
717 return getObjCLayout(D, 0);
718 }
719
Devang Patel6a5a34c2008-06-06 02:14:01 +0000720 ASTRecordLayout *NewEntry = NULL;
Devang Patel6a5a34c2008-06-06 02:14:01 +0000721 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Devang Patel6a5a34c2008-06-06 02:14:01 +0000722 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
723 unsigned Alignment = SL.getAlignment();
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000724
Daniel Dunbar913af352009-05-07 21:58:26 +0000725 // We start laying out ivars not at the end of the superclass
726 // structure, but at the next byte following the last field.
727 uint64_t Size = llvm::RoundUpToAlignment(SL.NextOffset, 8);
Daniel Dunbard6884a02009-05-04 05:16:21 +0000728
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000729 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout(Size, Alignment);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000730 NewEntry->InitializeLayout(FieldCount);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000731 } else {
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000732 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout();
Devang Patel6a5a34c2008-06-06 02:14:01 +0000733 NewEntry->InitializeLayout(FieldCount);
734 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000735
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000736 unsigned StructPacking = 0;
737 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
738 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000739
740 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
741 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
742 AA->getAlignment()));
743
744 // Layout each ivar sequentially.
745 unsigned i = 0;
746 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
747 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
748 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000749 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000750 }
Daniel Dunbar453addb2009-05-03 11:16:44 +0000751 // And synthesized ivars, if this is an implementation.
752 if (Impl) {
753 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
754 E = D->prop_end(*this); I != E; ++I) {
755 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
756 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
757 }
Fariborz Jahanian18191882009-03-31 18:11:23 +0000758 }
Fariborz Jahanian99eee362009-04-01 19:37:34 +0000759
Devang Patel44a3dde2008-06-04 21:54:36 +0000760 // Finally, round the size of the total struct up to the alignment of the
761 // struct itself.
762 NewEntry->FinalizeLayout();
763 return *NewEntry;
764}
765
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000766const ASTRecordLayout &
767ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
768 return getObjCLayout(D, 0);
769}
770
771const ASTRecordLayout &
772ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
773 return getObjCLayout(D->getClassInterface(), D);
774}
775
Devang Patel88a981b2007-11-01 19:11:01 +0000776/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000777/// specified record (struct/union/class), which indicates its size and field
778/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000779const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000780 D = D->getDefinition(*this);
781 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000782
Chris Lattner464175b2007-07-18 17:52:12 +0000783 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000784 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000785 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000786
Devang Patel88a981b2007-11-01 19:11:01 +0000787 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
788 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
789 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000790 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000791
Douglas Gregore267ff32008-12-11 20:41:00 +0000792 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000793 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
794 D->field_end(*this)));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000795 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000796
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000797 unsigned StructPacking = 0;
798 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
799 StructPacking = PA->getAlignment();
800
Eli Friedman4bd998b2008-05-30 09:31:38 +0000801 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000802 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
803 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000804
Eli Friedman4bd998b2008-05-30 09:31:38 +0000805 // Layout each field, for now, just sequentially, respecting alignment. In
806 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000807 unsigned FieldIdx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000808 for (RecordDecl::field_iterator Field = D->field_begin(*this),
809 FieldEnd = D->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000810 Field != FieldEnd; (void)++Field, ++FieldIdx)
811 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000812
813 // Finally, round the size of the total struct up to the alignment of the
814 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000815 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000816 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000817}
818
Chris Lattnera7674d82007-07-13 22:13:22 +0000819//===----------------------------------------------------------------------===//
820// Type creation/memoization methods
821//===----------------------------------------------------------------------===//
822
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000823QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000824 QualType CanT = getCanonicalType(T);
825 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000826 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000827
828 // If we are composing extended qualifiers together, merge together into one
829 // ExtQualType node.
830 unsigned CVRQuals = T.getCVRQualifiers();
831 QualType::GCAttrTypes GCAttr = QualType::GCNone;
832 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000833
Chris Lattnerb7d25532009-02-18 22:53:11 +0000834 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
835 // If this type already has an address space specified, it cannot get
836 // another one.
837 assert(EQT->getAddressSpace() == 0 &&
838 "Type cannot be in multiple addr spaces!");
839 GCAttr = EQT->getObjCGCAttr();
840 TypeNode = EQT->getBaseType();
841 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000842
Chris Lattnerb7d25532009-02-18 22:53:11 +0000843 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000844 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000845 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000846 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000847 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000848 return QualType(EXTQy, CVRQuals);
849
Christopher Lambebb97e92008-02-04 02:31:56 +0000850 // If the base type isn't canonical, this won't be a canonical type either,
851 // so fill in the canonical type field.
852 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000853 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000854 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000855
Chris Lattnerb7d25532009-02-18 22:53:11 +0000856 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000857 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000858 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000859 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000860 ExtQualType *New =
861 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000862 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000863 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000864 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000865}
866
Chris Lattnerb7d25532009-02-18 22:53:11 +0000867QualType ASTContext::getObjCGCQualType(QualType T,
868 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000869 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000870 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000871 return T;
872
Chris Lattnerb7d25532009-02-18 22:53:11 +0000873 // If we are composing extended qualifiers together, merge together into one
874 // ExtQualType node.
875 unsigned CVRQuals = T.getCVRQualifiers();
876 Type *TypeNode = T.getTypePtr();
877 unsigned AddressSpace = 0;
878
879 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
880 // If this type already has an address space specified, it cannot get
881 // another one.
882 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
883 "Type cannot be in multiple addr spaces!");
884 AddressSpace = EQT->getAddressSpace();
885 TypeNode = EQT->getBaseType();
886 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000887
888 // Check if we've already instantiated an gc qual'd type of this type.
889 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000890 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000891 void *InsertPos = 0;
892 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000893 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000894
895 // If the base type isn't canonical, this won't be a canonical type either,
896 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000897 // FIXME: Isn't this also not canonical if the base type is a array
898 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000899 QualType Canonical;
900 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000901 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000902
Chris Lattnerb7d25532009-02-18 22:53:11 +0000903 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000904 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
905 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
906 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000907 ExtQualType *New =
908 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000909 ExtQualTypes.InsertNode(New, InsertPos);
910 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000911 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000912}
Chris Lattnera7674d82007-07-13 22:13:22 +0000913
Reid Spencer5f016e22007-07-11 17:01:13 +0000914/// getComplexType - Return the uniqued reference to the type for a complex
915/// number with the specified element type.
916QualType ASTContext::getComplexType(QualType T) {
917 // Unique pointers, to guarantee there is only one pointer of a particular
918 // structure.
919 llvm::FoldingSetNodeID ID;
920 ComplexType::Profile(ID, T);
921
922 void *InsertPos = 0;
923 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
924 return QualType(CT, 0);
925
926 // If the pointee type isn't canonical, this won't be a canonical type either,
927 // so fill in the canonical type field.
928 QualType Canonical;
929 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000930 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000931
932 // Get the new insert position for the node we care about.
933 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000934 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000935 }
Steve Narofff83820b2009-01-27 22:08:43 +0000936 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000937 Types.push_back(New);
938 ComplexTypes.InsertNode(New, InsertPos);
939 return QualType(New, 0);
940}
941
Eli Friedmanf98aba32009-02-13 02:31:07 +0000942QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
943 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
944 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
945 FixedWidthIntType *&Entry = Map[Width];
946 if (!Entry)
947 Entry = new FixedWidthIntType(Width, Signed);
948 return QualType(Entry, 0);
949}
Reid Spencer5f016e22007-07-11 17:01:13 +0000950
951/// getPointerType - Return the uniqued reference to the type for a pointer to
952/// the specified type.
953QualType ASTContext::getPointerType(QualType T) {
954 // Unique pointers, to guarantee there is only one pointer of a particular
955 // structure.
956 llvm::FoldingSetNodeID ID;
957 PointerType::Profile(ID, T);
958
959 void *InsertPos = 0;
960 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
961 return QualType(PT, 0);
962
963 // If the pointee type isn't canonical, this won't be a canonical type either,
964 // so fill in the canonical type field.
965 QualType Canonical;
966 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000967 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000968
969 // Get the new insert position for the node we care about.
970 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000971 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000972 }
Steve Narofff83820b2009-01-27 22:08:43 +0000973 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 Types.push_back(New);
975 PointerTypes.InsertNode(New, InsertPos);
976 return QualType(New, 0);
977}
978
Steve Naroff5618bd42008-08-27 16:04:49 +0000979/// getBlockPointerType - Return the uniqued reference to the type for
980/// a pointer to the specified block.
981QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000982 assert(T->isFunctionType() && "block of function types only");
983 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000984 // structure.
985 llvm::FoldingSetNodeID ID;
986 BlockPointerType::Profile(ID, T);
987
988 void *InsertPos = 0;
989 if (BlockPointerType *PT =
990 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
991 return QualType(PT, 0);
992
Steve Naroff296e8d52008-08-28 19:20:44 +0000993 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000994 // type either so fill in the canonical type field.
995 QualType Canonical;
996 if (!T->isCanonical()) {
997 Canonical = getBlockPointerType(getCanonicalType(T));
998
999 // Get the new insert position for the node we care about.
1000 BlockPointerType *NewIP =
1001 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001002 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001003 }
Steve Narofff83820b2009-01-27 22:08:43 +00001004 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001005 Types.push_back(New);
1006 BlockPointerTypes.InsertNode(New, InsertPos);
1007 return QualType(New, 0);
1008}
1009
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001010/// getLValueReferenceType - Return the uniqued reference to the type for an
1011/// lvalue reference to the specified type.
1012QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 // Unique pointers, to guarantee there is only one pointer of a particular
1014 // structure.
1015 llvm::FoldingSetNodeID ID;
1016 ReferenceType::Profile(ID, T);
1017
1018 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001019 if (LValueReferenceType *RT =
1020 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001021 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001022
Reid Spencer5f016e22007-07-11 17:01:13 +00001023 // If the referencee type isn't canonical, this won't be a canonical type
1024 // either, so fill in the canonical type field.
1025 QualType Canonical;
1026 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001027 Canonical = getLValueReferenceType(getCanonicalType(T));
1028
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001030 LValueReferenceType *NewIP =
1031 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001032 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001033 }
1034
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001035 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001036 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001037 LValueReferenceTypes.InsertNode(New, InsertPos);
1038 return QualType(New, 0);
1039}
1040
1041/// getRValueReferenceType - Return the uniqued reference to the type for an
1042/// rvalue reference to the specified type.
1043QualType ASTContext::getRValueReferenceType(QualType T) {
1044 // Unique pointers, to guarantee there is only one pointer of a particular
1045 // structure.
1046 llvm::FoldingSetNodeID ID;
1047 ReferenceType::Profile(ID, T);
1048
1049 void *InsertPos = 0;
1050 if (RValueReferenceType *RT =
1051 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1052 return QualType(RT, 0);
1053
1054 // If the referencee type isn't canonical, this won't be a canonical type
1055 // either, so fill in the canonical type field.
1056 QualType Canonical;
1057 if (!T->isCanonical()) {
1058 Canonical = getRValueReferenceType(getCanonicalType(T));
1059
1060 // Get the new insert position for the node we care about.
1061 RValueReferenceType *NewIP =
1062 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1063 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1064 }
1065
1066 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1067 Types.push_back(New);
1068 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 return QualType(New, 0);
1070}
1071
Sebastian Redlf30208a2009-01-24 21:16:55 +00001072/// getMemberPointerType - Return the uniqued reference to the type for a
1073/// member pointer to the specified type, in the specified class.
1074QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1075{
1076 // Unique pointers, to guarantee there is only one pointer of a particular
1077 // structure.
1078 llvm::FoldingSetNodeID ID;
1079 MemberPointerType::Profile(ID, T, Cls);
1080
1081 void *InsertPos = 0;
1082 if (MemberPointerType *PT =
1083 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1084 return QualType(PT, 0);
1085
1086 // If the pointee or class type isn't canonical, this won't be a canonical
1087 // type either, so fill in the canonical type field.
1088 QualType Canonical;
1089 if (!T->isCanonical()) {
1090 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1091
1092 // Get the new insert position for the node we care about.
1093 MemberPointerType *NewIP =
1094 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1095 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1096 }
Steve Narofff83820b2009-01-27 22:08:43 +00001097 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001098 Types.push_back(New);
1099 MemberPointerTypes.InsertNode(New, InsertPos);
1100 return QualType(New, 0);
1101}
1102
Steve Narofffb22d962007-08-30 01:06:46 +00001103/// getConstantArrayType - Return the unique reference to the type for an
1104/// array of the specified element type.
1105QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +00001106 const llvm::APInt &ArySize,
1107 ArrayType::ArraySizeModifier ASM,
1108 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001109 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001110 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001111
1112 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001113 if (ConstantArrayType *ATP =
1114 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001115 return QualType(ATP, 0);
1116
1117 // If the element type isn't canonical, this won't be a canonical type either,
1118 // so fill in the canonical type field.
1119 QualType Canonical;
1120 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001121 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001122 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001124 ConstantArrayType *NewIP =
1125 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001126 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001127 }
1128
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001129 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001130 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001131 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001132 Types.push_back(New);
1133 return QualType(New, 0);
1134}
1135
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001136/// getVariableArrayType - Returns a non-unique reference to the type for a
1137/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001138QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1139 ArrayType::ArraySizeModifier ASM,
1140 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001141 // Since we don't unique expressions, it isn't possible to unique VLA's
1142 // that have an expression provided for their size.
1143
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001144 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001145 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001146
1147 VariableArrayTypes.push_back(New);
1148 Types.push_back(New);
1149 return QualType(New, 0);
1150}
1151
Douglas Gregor898574e2008-12-05 23:32:09 +00001152/// getDependentSizedArrayType - Returns a non-unique reference to
1153/// the type for a dependently-sized array of the specified element
1154/// type. FIXME: We will need these to be uniqued, or at least
1155/// comparable, at some point.
1156QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1157 ArrayType::ArraySizeModifier ASM,
1158 unsigned EltTypeQuals) {
1159 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1160 "Size must be type- or value-dependent!");
1161
1162 // Since we don't unique expressions, it isn't possible to unique
1163 // dependently-sized array types.
1164
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001165 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001166 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1167 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001168
1169 DependentSizedArrayTypes.push_back(New);
1170 Types.push_back(New);
1171 return QualType(New, 0);
1172}
1173
Eli Friedmanc5773c42008-02-15 18:16:39 +00001174QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1175 ArrayType::ArraySizeModifier ASM,
1176 unsigned EltTypeQuals) {
1177 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001178 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001179
1180 void *InsertPos = 0;
1181 if (IncompleteArrayType *ATP =
1182 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1183 return QualType(ATP, 0);
1184
1185 // If the element type isn't canonical, this won't be a canonical type
1186 // either, so fill in the canonical type field.
1187 QualType Canonical;
1188
1189 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001190 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001191 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001192
1193 // Get the new insert position for the node we care about.
1194 IncompleteArrayType *NewIP =
1195 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001196 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001197 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001198
Steve Narofff83820b2009-01-27 22:08:43 +00001199 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001200 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001201
1202 IncompleteArrayTypes.InsertNode(New, InsertPos);
1203 Types.push_back(New);
1204 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001205}
1206
Steve Naroff73322922007-07-18 18:00:27 +00001207/// getVectorType - Return the unique reference to a vector type of
1208/// the specified element type and size. VectorType must be a built-in type.
1209QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 BuiltinType *baseType;
1211
Chris Lattnerf52ab252008-04-06 22:59:24 +00001212 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001213 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001214
1215 // Check if we've already instantiated a vector of this type.
1216 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001217 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001218 void *InsertPos = 0;
1219 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1220 return QualType(VTP, 0);
1221
1222 // If the element type isn't canonical, this won't be a canonical type either,
1223 // so fill in the canonical type field.
1224 QualType Canonical;
1225 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001226 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001227
1228 // Get the new insert position for the node we care about.
1229 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001230 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001231 }
Steve Narofff83820b2009-01-27 22:08:43 +00001232 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001233 VectorTypes.InsertNode(New, InsertPos);
1234 Types.push_back(New);
1235 return QualType(New, 0);
1236}
1237
Nate Begeman213541a2008-04-18 23:10:10 +00001238/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001239/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001240QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001241 BuiltinType *baseType;
1242
Chris Lattnerf52ab252008-04-06 22:59:24 +00001243 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001244 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001245
1246 // Check if we've already instantiated a vector of this type.
1247 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001248 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001249 void *InsertPos = 0;
1250 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1251 return QualType(VTP, 0);
1252
1253 // If the element type isn't canonical, this won't be a canonical type either,
1254 // so fill in the canonical type field.
1255 QualType Canonical;
1256 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001257 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001258
1259 // Get the new insert position for the node we care about.
1260 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001261 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001262 }
Steve Narofff83820b2009-01-27 22:08:43 +00001263 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001264 VectorTypes.InsertNode(New, InsertPos);
1265 Types.push_back(New);
1266 return QualType(New, 0);
1267}
1268
Douglas Gregor72564e72009-02-26 23:50:07 +00001269/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001270///
Douglas Gregor72564e72009-02-26 23:50:07 +00001271QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 // Unique functions, to guarantee there is only one function of a particular
1273 // structure.
1274 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001275 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001276
1277 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001278 if (FunctionNoProtoType *FT =
1279 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 return QualType(FT, 0);
1281
1282 QualType Canonical;
1283 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001284 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001285
1286 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001287 FunctionNoProtoType *NewIP =
1288 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001289 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001290 }
1291
Douglas Gregor72564e72009-02-26 23:50:07 +00001292 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001293 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001294 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001295 return QualType(New, 0);
1296}
1297
1298/// getFunctionType - Return a normal function type with a typed argument
1299/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001300QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001301 unsigned NumArgs, bool isVariadic,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001302 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001303 // Unique functions, to guarantee there is only one function of a particular
1304 // structure.
1305 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001306 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001307 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001308
1309 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001310 if (FunctionProtoType *FTP =
1311 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 return QualType(FTP, 0);
1313
1314 // Determine whether the type being created is already canonical or not.
1315 bool isCanonical = ResultTy->isCanonical();
1316 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1317 if (!ArgArray[i]->isCanonical())
1318 isCanonical = false;
1319
1320 // If this type isn't canonical, get the canonical version of it.
1321 QualType Canonical;
1322 if (!isCanonical) {
1323 llvm::SmallVector<QualType, 16> CanonicalArgs;
1324 CanonicalArgs.reserve(NumArgs);
1325 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001326 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001327
Chris Lattnerf52ab252008-04-06 22:59:24 +00001328 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001329 &CanonicalArgs[0], NumArgs,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001330 isVariadic, TypeQuals);
1331
Reid Spencer5f016e22007-07-11 17:01:13 +00001332 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001333 FunctionProtoType *NewIP =
1334 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001335 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001336 }
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001337
Douglas Gregor72564e72009-02-26 23:50:07 +00001338 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001339 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001340 FunctionProtoType *FTP =
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001341 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1342 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001343 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001344 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001345 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001346 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001347 return QualType(FTP, 0);
1348}
1349
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001350/// getTypeDeclType - Return the unique reference to the type for the
1351/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001352QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001353 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001354 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1355
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001356 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001357 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001358 else if (isa<TemplateTypeParmDecl>(Decl)) {
1359 assert(false && "Template type parameter types are always available.");
1360 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001361 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001362
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001363 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001364 if (PrevDecl)
1365 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001366 else
1367 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001368 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001369 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1370 if (PrevDecl)
1371 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001372 else
1373 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001374 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001375 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001376 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001377
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001378 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001379 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001380}
1381
Reid Spencer5f016e22007-07-11 17:01:13 +00001382/// getTypedefType - Return the unique reference to the type for the
1383/// specified typename decl.
1384QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1385 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1386
Chris Lattnerf52ab252008-04-06 22:59:24 +00001387 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001388 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001389 Types.push_back(Decl->TypeForDecl);
1390 return QualType(Decl->TypeForDecl, 0);
1391}
1392
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001393/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001394/// specified ObjC interface decl.
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001395QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001396 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1397
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001398 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1399 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff3536b442007-09-06 21:24:23 +00001400 Types.push_back(Decl->TypeForDecl);
1401 return QualType(Decl->TypeForDecl, 0);
1402}
1403
Douglas Gregorfab9d672009-02-05 23:33:38 +00001404/// \brief Retrieve the template type parameter type for a template
1405/// parameter with the given depth, index, and (optionally) name.
1406QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1407 IdentifierInfo *Name) {
1408 llvm::FoldingSetNodeID ID;
1409 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1410 void *InsertPos = 0;
1411 TemplateTypeParmType *TypeParm
1412 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1413
1414 if (TypeParm)
1415 return QualType(TypeParm, 0);
1416
1417 if (Name)
1418 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1419 getTemplateTypeParmType(Depth, Index));
1420 else
1421 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1422
1423 Types.push_back(TypeParm);
1424 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1425
1426 return QualType(TypeParm, 0);
1427}
1428
Douglas Gregor55f6b142009-02-09 18:46:07 +00001429QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001430ASTContext::getTemplateSpecializationType(TemplateName Template,
1431 const TemplateArgument *Args,
1432 unsigned NumArgs,
1433 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001434 if (!Canon.isNull())
1435 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001436
Douglas Gregor55f6b142009-02-09 18:46:07 +00001437 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001438 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001439
Douglas Gregor55f6b142009-02-09 18:46:07 +00001440 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001441 TemplateSpecializationType *Spec
1442 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001443
1444 if (Spec)
1445 return QualType(Spec, 0);
1446
Douglas Gregor7532dc62009-03-30 22:58:21 +00001447 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001448 sizeof(TemplateArgument) * NumArgs),
1449 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001450 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001451 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001452 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001453
1454 return QualType(Spec, 0);
1455}
1456
Douglas Gregore4e5b052009-03-19 00:18:19 +00001457QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001458ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001459 QualType NamedType) {
1460 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001461 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001462
1463 void *InsertPos = 0;
1464 QualifiedNameType *T
1465 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1466 if (T)
1467 return QualType(T, 0);
1468
Douglas Gregorab452ba2009-03-26 23:50:42 +00001469 T = new (*this) QualifiedNameType(NNS, NamedType,
1470 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001471 Types.push_back(T);
1472 QualifiedNameTypes.InsertNode(T, InsertPos);
1473 return QualType(T, 0);
1474}
1475
Douglas Gregord57959a2009-03-27 23:10:48 +00001476QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1477 const IdentifierInfo *Name,
1478 QualType Canon) {
1479 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1480
1481 if (Canon.isNull()) {
1482 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1483 if (CanonNNS != NNS)
1484 Canon = getTypenameType(CanonNNS, Name);
1485 }
1486
1487 llvm::FoldingSetNodeID ID;
1488 TypenameType::Profile(ID, NNS, Name);
1489
1490 void *InsertPos = 0;
1491 TypenameType *T
1492 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1493 if (T)
1494 return QualType(T, 0);
1495
1496 T = new (*this) TypenameType(NNS, Name, Canon);
1497 Types.push_back(T);
1498 TypenameTypes.InsertNode(T, InsertPos);
1499 return QualType(T, 0);
1500}
1501
Douglas Gregor17343172009-04-01 00:28:59 +00001502QualType
1503ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1504 const TemplateSpecializationType *TemplateId,
1505 QualType Canon) {
1506 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1507
1508 if (Canon.isNull()) {
1509 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1510 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1511 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1512 const TemplateSpecializationType *CanonTemplateId
1513 = CanonType->getAsTemplateSpecializationType();
1514 assert(CanonTemplateId &&
1515 "Canonical type must also be a template specialization type");
1516 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1517 }
1518 }
1519
1520 llvm::FoldingSetNodeID ID;
1521 TypenameType::Profile(ID, NNS, TemplateId);
1522
1523 void *InsertPos = 0;
1524 TypenameType *T
1525 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1526 if (T)
1527 return QualType(T, 0);
1528
1529 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1530 Types.push_back(T);
1531 TypenameTypes.InsertNode(T, InsertPos);
1532 return QualType(T, 0);
1533}
1534
Chris Lattner88cb27a2008-04-07 04:56:42 +00001535/// CmpProtocolNames - Comparison predicate for sorting protocols
1536/// alphabetically.
1537static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1538 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001539 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001540}
1541
1542static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1543 unsigned &NumProtocols) {
1544 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1545
1546 // Sort protocols, keyed by name.
1547 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1548
1549 // Remove duplicates.
1550 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1551 NumProtocols = ProtocolsEnd-Protocols;
1552}
1553
1554
Chris Lattner065f0d72008-04-07 04:44:08 +00001555/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1556/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001557QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1558 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001559 // Sort the protocol list alphabetically to canonicalize it.
1560 SortAndUniqueProtocols(Protocols, NumProtocols);
1561
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001562 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001563 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001564
1565 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001566 if (ObjCQualifiedInterfaceType *QT =
1567 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001568 return QualType(QT, 0);
1569
1570 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001571 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001572 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001573
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001574 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001575 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001576 return QualType(QType, 0);
1577}
1578
Chris Lattner88cb27a2008-04-07 04:56:42 +00001579/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1580/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001581QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001582 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001583 // Sort the protocol list alphabetically to canonicalize it.
1584 SortAndUniqueProtocols(Protocols, NumProtocols);
1585
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001586 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001587 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001588
1589 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001590 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001591 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001592 return QualType(QT, 0);
1593
1594 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001595 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001596 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001597 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001598 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001599 return QualType(QType, 0);
1600}
1601
Douglas Gregor72564e72009-02-26 23:50:07 +00001602/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1603/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001604/// multiple declarations that refer to "typeof(x)" all contain different
1605/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1606/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001607QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001608 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001609 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001610 Types.push_back(toe);
1611 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001612}
1613
Steve Naroff9752f252007-08-01 18:02:17 +00001614/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1615/// TypeOfType AST's. The only motivation to unique these nodes would be
1616/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1617/// an issue. This doesn't effect the type checker, since it operates
1618/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001619QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001620 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001621 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001622 Types.push_back(tot);
1623 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001624}
1625
Reid Spencer5f016e22007-07-11 17:01:13 +00001626/// getTagDeclType - Return the unique reference to the type for the
1627/// specified TagDecl (struct/union/class/enum) decl.
1628QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001629 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001630 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001631}
1632
1633/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1634/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1635/// needs to agree with the definition in <stddef.h>.
1636QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001637 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001638}
1639
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001640/// getSignedWCharType - Return the type of "signed wchar_t".
1641/// Used when in C++, as a GCC extension.
1642QualType ASTContext::getSignedWCharType() const {
1643 // FIXME: derive from "Target" ?
1644 return WCharTy;
1645}
1646
1647/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1648/// Used when in C++, as a GCC extension.
1649QualType ASTContext::getUnsignedWCharType() const {
1650 // FIXME: derive from "Target" ?
1651 return UnsignedIntTy;
1652}
1653
Chris Lattner8b9023b2007-07-13 03:05:23 +00001654/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1655/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1656QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001657 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001658}
1659
Chris Lattnere6327742008-04-02 05:18:44 +00001660//===----------------------------------------------------------------------===//
1661// Type Operators
1662//===----------------------------------------------------------------------===//
1663
Chris Lattner77c96472008-04-06 22:41:35 +00001664/// getCanonicalType - Return the canonical (structural) type corresponding to
1665/// the specified potentially non-canonical type. The non-canonical version
1666/// of a type may have many "decorated" versions of types. Decorators can
1667/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1668/// to be free of any of these, allowing two canonical types to be compared
1669/// for exact equality with a simple pointer comparison.
1670QualType ASTContext::getCanonicalType(QualType T) {
1671 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001672
1673 // If the result has type qualifiers, make sure to canonicalize them as well.
1674 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1675 if (TypeQuals == 0) return CanType;
1676
1677 // If the type qualifiers are on an array type, get the canonical type of the
1678 // array with the qualifiers applied to the element type.
1679 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1680 if (!AT)
1681 return CanType.getQualifiedType(TypeQuals);
1682
1683 // Get the canonical version of the element with the extra qualifiers on it.
1684 // This can recursively sink qualifiers through multiple levels of arrays.
1685 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1686 NewEltTy = getCanonicalType(NewEltTy);
1687
1688 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1689 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1690 CAT->getIndexTypeQualifier());
1691 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1692 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1693 IAT->getIndexTypeQualifier());
1694
Douglas Gregor898574e2008-12-05 23:32:09 +00001695 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1696 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1697 DSAT->getSizeModifier(),
1698 DSAT->getIndexTypeQualifier());
1699
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001700 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1701 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1702 VAT->getSizeModifier(),
1703 VAT->getIndexTypeQualifier());
1704}
1705
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001706TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
1707 // If this template name refers to a template, the canonical
1708 // template name merely stores the template itself.
1709 if (TemplateDecl *Template = Name.getAsTemplateDecl())
1710 return TemplateName(Template);
1711
1712 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
1713 assert(DTN && "Non-dependent template names must refer to template decls.");
1714 return DTN->CanonicalTemplateName;
1715}
1716
Douglas Gregord57959a2009-03-27 23:10:48 +00001717NestedNameSpecifier *
1718ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1719 if (!NNS)
1720 return 0;
1721
1722 switch (NNS->getKind()) {
1723 case NestedNameSpecifier::Identifier:
1724 // Canonicalize the prefix but keep the identifier the same.
1725 return NestedNameSpecifier::Create(*this,
1726 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1727 NNS->getAsIdentifier());
1728
1729 case NestedNameSpecifier::Namespace:
1730 // A namespace is canonical; build a nested-name-specifier with
1731 // this namespace and no prefix.
1732 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1733
1734 case NestedNameSpecifier::TypeSpec:
1735 case NestedNameSpecifier::TypeSpecWithTemplate: {
1736 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1737 NestedNameSpecifier *Prefix = 0;
1738
1739 // FIXME: This isn't the right check!
1740 if (T->isDependentType())
1741 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1742
1743 return NestedNameSpecifier::Create(*this, Prefix,
1744 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1745 T.getTypePtr());
1746 }
1747
1748 case NestedNameSpecifier::Global:
1749 // The global specifier is canonical and unique.
1750 return NNS;
1751 }
1752
1753 // Required to silence a GCC warning
1754 return 0;
1755}
1756
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001757
1758const ArrayType *ASTContext::getAsArrayType(QualType T) {
1759 // Handle the non-qualified case efficiently.
1760 if (T.getCVRQualifiers() == 0) {
1761 // Handle the common positive case fast.
1762 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1763 return AT;
1764 }
1765
1766 // Handle the common negative case fast, ignoring CVR qualifiers.
1767 QualType CType = T->getCanonicalTypeInternal();
1768
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001769 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001770 // test.
1771 if (!isa<ArrayType>(CType) &&
1772 !isa<ArrayType>(CType.getUnqualifiedType()))
1773 return 0;
1774
1775 // Apply any CVR qualifiers from the array type to the element type. This
1776 // implements C99 6.7.3p8: "If the specification of an array type includes
1777 // any type qualifiers, the element type is so qualified, not the array type."
1778
1779 // If we get here, we either have type qualifiers on the type, or we have
1780 // sugar such as a typedef in the way. If we have type qualifiers on the type
1781 // we must propagate them down into the elemeng type.
1782 unsigned CVRQuals = T.getCVRQualifiers();
1783 unsigned AddrSpace = 0;
1784 Type *Ty = T.getTypePtr();
1785
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001786 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001787 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001788 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1789 AddrSpace = EXTQT->getAddressSpace();
1790 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001791 } else {
1792 T = Ty->getDesugaredType();
1793 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1794 break;
1795 CVRQuals |= T.getCVRQualifiers();
1796 Ty = T.getTypePtr();
1797 }
1798 }
1799
1800 // If we have a simple case, just return now.
1801 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1802 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1803 return ATy;
1804
1805 // Otherwise, we have an array and we have qualifiers on it. Push the
1806 // qualifiers into the array element type and return a new array type.
1807 // Get the canonical version of the element with the extra qualifiers on it.
1808 // This can recursively sink qualifiers through multiple levels of arrays.
1809 QualType NewEltTy = ATy->getElementType();
1810 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001811 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001812 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1813
1814 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1815 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1816 CAT->getSizeModifier(),
1817 CAT->getIndexTypeQualifier()));
1818 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1819 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1820 IAT->getSizeModifier(),
1821 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001822
Douglas Gregor898574e2008-12-05 23:32:09 +00001823 if (const DependentSizedArrayType *DSAT
1824 = dyn_cast<DependentSizedArrayType>(ATy))
1825 return cast<ArrayType>(
1826 getDependentSizedArrayType(NewEltTy,
1827 DSAT->getSizeExpr(),
1828 DSAT->getSizeModifier(),
1829 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001830
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001831 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1832 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1833 VAT->getSizeModifier(),
1834 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001835}
1836
1837
Chris Lattnere6327742008-04-02 05:18:44 +00001838/// getArrayDecayedType - Return the properly qualified result of decaying the
1839/// specified array type to a pointer. This operation is non-trivial when
1840/// handling typedefs etc. The canonical type of "T" must be an array type,
1841/// this returns a pointer to a properly qualified element of the array.
1842///
1843/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1844QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001845 // Get the element type with 'getAsArrayType' so that we don't lose any
1846 // typedefs in the element type of the array. This also handles propagation
1847 // of type qualifiers from the array type into the element type if present
1848 // (C99 6.7.3p8).
1849 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1850 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001851
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001852 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001853
1854 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001855 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001856}
1857
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001858QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001859 QualType ElemTy = VAT->getElementType();
1860
1861 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1862 return getBaseElementType(VAT);
1863
1864 return ElemTy;
1865}
1866
Reid Spencer5f016e22007-07-11 17:01:13 +00001867/// getFloatingRank - Return a relative rank for floating point types.
1868/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001869static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001870 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001871 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001872
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001873 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001874 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001875 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001876 case BuiltinType::Float: return FloatRank;
1877 case BuiltinType::Double: return DoubleRank;
1878 case BuiltinType::LongDouble: return LongDoubleRank;
1879 }
1880}
1881
Steve Naroff716c7302007-08-27 01:41:48 +00001882/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1883/// point or a complex type (based on typeDomain/typeSize).
1884/// 'typeDomain' is a real floating point or complex type.
1885/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001886QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1887 QualType Domain) const {
1888 FloatingRank EltRank = getFloatingRank(Size);
1889 if (Domain->isComplexType()) {
1890 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001891 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001892 case FloatRank: return FloatComplexTy;
1893 case DoubleRank: return DoubleComplexTy;
1894 case LongDoubleRank: return LongDoubleComplexTy;
1895 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001896 }
Chris Lattner1361b112008-04-06 23:58:54 +00001897
1898 assert(Domain->isRealFloatingType() && "Unknown domain!");
1899 switch (EltRank) {
1900 default: assert(0 && "getFloatingRank(): illegal value for rank");
1901 case FloatRank: return FloatTy;
1902 case DoubleRank: return DoubleTy;
1903 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001904 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001905}
1906
Chris Lattner7cfeb082008-04-06 23:55:33 +00001907/// getFloatingTypeOrder - Compare the rank of the two specified floating
1908/// point types, ignoring the domain of the type (i.e. 'double' ==
1909/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1910/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001911int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1912 FloatingRank LHSR = getFloatingRank(LHS);
1913 FloatingRank RHSR = getFloatingRank(RHS);
1914
1915 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001916 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001917 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001918 return 1;
1919 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001920}
1921
Chris Lattnerf52ab252008-04-06 22:59:24 +00001922/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1923/// routine will assert if passed a built-in type that isn't an integer or enum,
1924/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001925unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001926 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001927 if (EnumType* ET = dyn_cast<EnumType>(T))
1928 T = ET->getDecl()->getIntegerType().getTypePtr();
1929
1930 // There are two things which impact the integer rank: the width, and
1931 // the ordering of builtins. The builtin ordering is encoded in the
1932 // bottom three bits; the width is encoded in the bits above that.
1933 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1934 return FWIT->getWidth() << 3;
1935 }
1936
Chris Lattnerf52ab252008-04-06 22:59:24 +00001937 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001938 default: assert(0 && "getIntegerRank(): not a built-in integer");
1939 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001940 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001941 case BuiltinType::Char_S:
1942 case BuiltinType::Char_U:
1943 case BuiltinType::SChar:
1944 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001945 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001946 case BuiltinType::Short:
1947 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001948 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001949 case BuiltinType::Int:
1950 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001951 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001952 case BuiltinType::Long:
1953 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001954 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001955 case BuiltinType::LongLong:
1956 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001957 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00001958 case BuiltinType::Int128:
1959 case BuiltinType::UInt128:
1960 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001961 }
1962}
1963
Chris Lattner7cfeb082008-04-06 23:55:33 +00001964/// getIntegerTypeOrder - Returns the highest ranked integer type:
1965/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1966/// LHS < RHS, return -1.
1967int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001968 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1969 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001970 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001971
Chris Lattnerf52ab252008-04-06 22:59:24 +00001972 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1973 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001974
Chris Lattner7cfeb082008-04-06 23:55:33 +00001975 unsigned LHSRank = getIntegerRank(LHSC);
1976 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001977
Chris Lattner7cfeb082008-04-06 23:55:33 +00001978 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1979 if (LHSRank == RHSRank) return 0;
1980 return LHSRank > RHSRank ? 1 : -1;
1981 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001982
Chris Lattner7cfeb082008-04-06 23:55:33 +00001983 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1984 if (LHSUnsigned) {
1985 // If the unsigned [LHS] type is larger, return it.
1986 if (LHSRank >= RHSRank)
1987 return 1;
1988
1989 // If the signed type can represent all values of the unsigned type, it
1990 // wins. Because we are dealing with 2's complement and types that are
1991 // powers of two larger than each other, this is always safe.
1992 return -1;
1993 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001994
Chris Lattner7cfeb082008-04-06 23:55:33 +00001995 // If the unsigned [RHS] type is larger, return it.
1996 if (RHSRank >= LHSRank)
1997 return -1;
1998
1999 // If the signed type can represent all values of the unsigned type, it
2000 // wins. Because we are dealing with 2's complement and types that are
2001 // powers of two larger than each other, this is always safe.
2002 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002003}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002004
2005// getCFConstantStringType - Return the type used for constant CFStrings.
2006QualType ASTContext::getCFConstantStringType() {
2007 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00002008 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002009 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002010 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002011 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00002012
2013 // const int *isa;
2014 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002015 // int flags;
2016 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002017 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002018 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00002019 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002020 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00002021
Anders Carlsson71993dd2007-08-17 05:31:46 +00002022 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002023 for (unsigned i = 0; i < 4; ++i) {
2024 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2025 SourceLocation(), 0,
2026 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002027 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002028 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002029 }
2030
2031 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002032 }
2033
2034 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002035}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002036
Douglas Gregor319ac892009-04-23 22:29:11 +00002037void ASTContext::setCFConstantStringType(QualType T) {
2038 const RecordType *Rec = T->getAsRecordType();
2039 assert(Rec && "Invalid CFConstantStringType");
2040 CFConstantStringTypeDecl = Rec->getDecl();
2041}
2042
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002043QualType ASTContext::getObjCFastEnumerationStateType()
2044{
2045 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002046 ObjCFastEnumerationStateTypeDecl =
2047 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2048 &Idents.get("__objcFastEnumerationState"));
2049
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002050 QualType FieldTypes[] = {
2051 UnsignedLongTy,
2052 getPointerType(ObjCIdType),
2053 getPointerType(UnsignedLongTy),
2054 getConstantArrayType(UnsignedLongTy,
2055 llvm::APInt(32, 5), ArrayType::Normal, 0)
2056 };
2057
Douglas Gregor44b43212008-12-11 16:49:14 +00002058 for (size_t i = 0; i < 4; ++i) {
2059 FieldDecl *Field = FieldDecl::Create(*this,
2060 ObjCFastEnumerationStateTypeDecl,
2061 SourceLocation(), 0,
2062 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002063 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002064 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002065 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002066
Douglas Gregor44b43212008-12-11 16:49:14 +00002067 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002068 }
2069
2070 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2071}
2072
Douglas Gregor319ac892009-04-23 22:29:11 +00002073void ASTContext::setObjCFastEnumerationStateType(QualType T) {
2074 const RecordType *Rec = T->getAsRecordType();
2075 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2076 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2077}
2078
Anders Carlssone8c49532007-10-29 06:33:42 +00002079// This returns true if a type has been typedefed to BOOL:
2080// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002081static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002082 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002083 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2084 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002085
2086 return false;
2087}
2088
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002089/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002090/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002091int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002092 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002093
2094 // Make all integer and enum types at least as large as an int
2095 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002096 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002097 // Treat arrays as pointers, since that's how they're passed in.
2098 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002099 sz = getTypeSize(VoidPtrTy);
2100 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002101}
2102
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002103/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002104/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002105void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002106 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002107 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002108 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002109 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002110 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002111 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002112 // Compute size of all parameters.
2113 // Start with computing size of a pointer in number of bytes.
2114 // FIXME: There might(should) be a better way of doing this computation!
2115 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002116 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002117 // The first two arguments (self and _cmd) are pointers; account for
2118 // their size.
2119 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002120 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2121 E = Decl->param_end(); PI != E; ++PI) {
2122 QualType PType = (*PI)->getType();
2123 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002124 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002125 ParmOffset += sz;
2126 }
2127 S += llvm::utostr(ParmOffset);
2128 S += "@0:";
2129 S += llvm::utostr(PtrSize);
2130
2131 // Argument types.
2132 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002133 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2134 E = Decl->param_end(); PI != E; ++PI) {
2135 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002136 QualType PType = PVDecl->getOriginalType();
2137 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002138 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2139 // Use array's original type only if it has known number of
2140 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002141 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002142 PType = PVDecl->getType();
2143 } else if (PType->isFunctionType())
2144 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002145 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002146 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002147 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002148 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002149 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002150 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002151 }
2152}
2153
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002154/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002155/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002156/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2157/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002158/// Property attributes are stored as a comma-delimited C string. The simple
2159/// attributes readonly and bycopy are encoded as single characters. The
2160/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2161/// encoded as single characters, followed by an identifier. Property types
2162/// are also encoded as a parametrized attribute. The characters used to encode
2163/// these attributes are defined by the following enumeration:
2164/// @code
2165/// enum PropertyAttributes {
2166/// kPropertyReadOnly = 'R', // property is read-only.
2167/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2168/// kPropertyByref = '&', // property is a reference to the value last assigned
2169/// kPropertyDynamic = 'D', // property is dynamic
2170/// kPropertyGetter = 'G', // followed by getter selector name
2171/// kPropertySetter = 'S', // followed by setter selector name
2172/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2173/// kPropertyType = 't' // followed by old-style type encoding.
2174/// kPropertyWeak = 'W' // 'weak' property
2175/// kPropertyStrong = 'P' // property GC'able
2176/// kPropertyNonAtomic = 'N' // property non-atomic
2177/// };
2178/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002179void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2180 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002181 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002182 // Collect information from the property implementation decl(s).
2183 bool Dynamic = false;
2184 ObjCPropertyImplDecl *SynthesizePID = 0;
2185
2186 // FIXME: Duplicated code due to poor abstraction.
2187 if (Container) {
2188 if (const ObjCCategoryImplDecl *CID =
2189 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2190 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002191 i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2192 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002193 ObjCPropertyImplDecl *PID = *i;
2194 if (PID->getPropertyDecl() == PD) {
2195 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2196 Dynamic = true;
2197 } else {
2198 SynthesizePID = PID;
2199 }
2200 }
2201 }
2202 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002203 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002204 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002205 i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2206 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002207 ObjCPropertyImplDecl *PID = *i;
2208 if (PID->getPropertyDecl() == PD) {
2209 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2210 Dynamic = true;
2211 } else {
2212 SynthesizePID = PID;
2213 }
2214 }
2215 }
2216 }
2217 }
2218
2219 // FIXME: This is not very efficient.
2220 S = "T";
2221
2222 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002223 // GCC has some special rules regarding encoding of properties which
2224 // closely resembles encoding of ivars.
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002225 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002226 true /* outermost type */,
2227 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002228
2229 if (PD->isReadOnly()) {
2230 S += ",R";
2231 } else {
2232 switch (PD->getSetterKind()) {
2233 case ObjCPropertyDecl::Assign: break;
2234 case ObjCPropertyDecl::Copy: S += ",C"; break;
2235 case ObjCPropertyDecl::Retain: S += ",&"; break;
2236 }
2237 }
2238
2239 // It really isn't clear at all what this means, since properties
2240 // are "dynamic by default".
2241 if (Dynamic)
2242 S += ",D";
2243
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002244 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2245 S += ",N";
2246
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002247 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2248 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002249 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002250 }
2251
2252 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2253 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002254 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002255 }
2256
2257 if (SynthesizePID) {
2258 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2259 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002260 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002261 }
2262
2263 // FIXME: OBJCGC: weak & strong
2264}
2265
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002266/// getLegacyIntegralTypeEncoding -
2267/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002268/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002269/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2270///
2271void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2272 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2273 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002274 if (BT->getKind() == BuiltinType::ULong &&
2275 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002276 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002277 else
2278 if (BT->getKind() == BuiltinType::Long &&
2279 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002280 PointeeTy = IntTy;
2281 }
2282 }
2283}
2284
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002285void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002286 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002287 // We follow the behavior of gcc, expanding structures which are
2288 // directly pointed to, and expanding embedded structures. Note that
2289 // these rules are sufficient to prevent recursive encoding of the
2290 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002291 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2292 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002293}
2294
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002295static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002296 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002297 const Expr *E = FD->getBitWidth();
2298 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2299 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002300 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002301 S += 'b';
2302 S += llvm::utostr(N);
2303}
2304
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002305void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2306 bool ExpandPointedToStructures,
2307 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002308 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002309 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002310 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002311 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002312 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002313 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002314 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002315 else {
2316 char encoding;
2317 switch (BT->getKind()) {
2318 default: assert(0 && "Unhandled builtin type kind");
2319 case BuiltinType::Void: encoding = 'v'; break;
2320 case BuiltinType::Bool: encoding = 'B'; break;
2321 case BuiltinType::Char_U:
2322 case BuiltinType::UChar: encoding = 'C'; break;
2323 case BuiltinType::UShort: encoding = 'S'; break;
2324 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002325 case BuiltinType::ULong:
2326 encoding =
2327 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2328 break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002329 case BuiltinType::UInt128: encoding = 'T'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002330 case BuiltinType::ULongLong: encoding = 'Q'; break;
2331 case BuiltinType::Char_S:
2332 case BuiltinType::SChar: encoding = 'c'; break;
2333 case BuiltinType::Short: encoding = 's'; break;
2334 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002335 case BuiltinType::Long:
2336 encoding =
2337 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2338 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002339 case BuiltinType::LongLong: encoding = 'q'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002340 case BuiltinType::Int128: encoding = 't'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002341 case BuiltinType::Float: encoding = 'f'; break;
2342 case BuiltinType::Double: encoding = 'd'; break;
2343 case BuiltinType::LongDouble: encoding = 'd'; break;
2344 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002345
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002346 S += encoding;
2347 }
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002348 } else if (const ComplexType *CT = T->getAsComplexType()) {
2349 S += 'j';
2350 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2351 false);
2352 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002353 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2354 ExpandPointedToStructures,
2355 ExpandStructures, FD);
2356 if (FD || EncodingProperty) {
2357 // Note that we do extended encoding of protocol qualifer list
2358 // Only when doing ivar or property encoding.
2359 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2360 S += '"';
2361 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2362 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2363 S += '<';
2364 S += Proto->getNameAsString();
2365 S += '>';
2366 }
2367 S += '"';
2368 }
2369 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002370 }
2371 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002372 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002373 bool isReadOnly = false;
2374 // For historical/compatibility reasons, the read-only qualifier of the
2375 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2376 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2377 // Also, do not emit the 'r' for anything but the outermost type!
2378 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2379 if (OutermostType && T.isConstQualified()) {
2380 isReadOnly = true;
2381 S += 'r';
2382 }
2383 }
2384 else if (OutermostType) {
2385 QualType P = PointeeTy;
2386 while (P->getAsPointerType())
2387 P = P->getAsPointerType()->getPointeeType();
2388 if (P.isConstQualified()) {
2389 isReadOnly = true;
2390 S += 'r';
2391 }
2392 }
2393 if (isReadOnly) {
2394 // Another legacy compatibility encoding. Some ObjC qualifier and type
2395 // combinations need to be rearranged.
2396 // Rewrite "in const" from "nr" to "rn"
2397 const char * s = S.c_str();
2398 int len = S.length();
2399 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2400 std::string replace = "rn";
2401 S.replace(S.end()-2, S.end(), replace);
2402 }
2403 }
Steve Naroff389bf462009-02-12 17:52:19 +00002404 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002405 S += '@';
2406 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002407 }
2408 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002409 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002410 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002411 // Another historical/compatibility reason.
2412 // We encode the underlying type which comes out as
2413 // {...};
2414 S += '^';
2415 getObjCEncodingForTypeImpl(PointeeTy, S,
2416 false, ExpandPointedToStructures,
2417 NULL);
2418 return;
2419 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002420 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002421 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002422 const ObjCInterfaceType *OIT =
2423 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002424 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002425 S += '"';
2426 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002427 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2428 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2429 S += '<';
2430 S += Proto->getNameAsString();
2431 S += '>';
2432 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002433 S += '"';
2434 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002435 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002436 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002437 S += '#';
2438 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002439 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002440 S += ':';
2441 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002442 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002443
2444 if (PointeeTy->isCharType()) {
2445 // char pointer types should be encoded as '*' unless it is a
2446 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002447 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002448 S += '*';
2449 return;
2450 }
2451 }
2452
2453 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002454 getLegacyIntegralTypeEncoding(PointeeTy);
2455
2456 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002457 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002458 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002459 } else if (const ArrayType *AT =
2460 // Ignore type qualifiers etc.
2461 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002462 if (isa<IncompleteArrayType>(AT)) {
2463 // Incomplete arrays are encoded as a pointer to the array element.
2464 S += '^';
2465
2466 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2467 false, ExpandStructures, FD);
2468 } else {
2469 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002470
Anders Carlsson559a8332009-02-22 01:38:57 +00002471 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2472 S += llvm::utostr(CAT->getSize().getZExtValue());
2473 else {
2474 //Variable length arrays are encoded as a regular array with 0 elements.
2475 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2476 S += '0';
2477 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002478
Anders Carlsson559a8332009-02-22 01:38:57 +00002479 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2480 false, ExpandStructures, FD);
2481 S += ']';
2482 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002483 } else if (T->getAsFunctionType()) {
2484 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002485 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002486 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002487 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002488 // Anonymous structures print as '?'
2489 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2490 S += II->getName();
2491 } else {
2492 S += '?';
2493 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002494 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002495 S += '=';
Douglas Gregor6ab35242009-04-09 21:40:53 +00002496 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2497 FieldEnd = RDecl->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +00002498 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002499 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002500 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002501 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002502 S += '"';
2503 }
2504
2505 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002506 if (Field->isBitField()) {
2507 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2508 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002509 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002510 QualType qt = Field->getType();
2511 getLegacyIntegralTypeEncoding(qt);
2512 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002513 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002514 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002515 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002516 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002517 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002518 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002519 if (FD && FD->isBitField())
2520 EncodeBitField(this, S, FD);
2521 else
2522 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002523 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002524 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002525 } else if (T->isObjCInterfaceType()) {
2526 // @encode(class_name)
2527 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2528 S += '{';
2529 const IdentifierInfo *II = OI->getIdentifier();
2530 S += II->getName();
2531 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002532 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002533 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002534 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002535 if (RecFields[i]->isBitField())
2536 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2537 RecFields[i]);
2538 else
2539 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2540 FD);
2541 }
2542 S += '}';
2543 }
2544 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002545 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002546}
2547
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002548void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002549 std::string& S) const {
2550 if (QT & Decl::OBJC_TQ_In)
2551 S += 'n';
2552 if (QT & Decl::OBJC_TQ_Inout)
2553 S += 'N';
2554 if (QT & Decl::OBJC_TQ_Out)
2555 S += 'o';
2556 if (QT & Decl::OBJC_TQ_Bycopy)
2557 S += 'O';
2558 if (QT & Decl::OBJC_TQ_Byref)
2559 S += 'R';
2560 if (QT & Decl::OBJC_TQ_Oneway)
2561 S += 'V';
2562}
2563
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002564void ASTContext::setBuiltinVaListType(QualType T)
2565{
2566 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2567
2568 BuiltinVaListType = T;
2569}
2570
Douglas Gregor319ac892009-04-23 22:29:11 +00002571void ASTContext::setObjCIdType(QualType T)
Steve Naroff7e219e42007-10-15 14:41:52 +00002572{
Douglas Gregor319ac892009-04-23 22:29:11 +00002573 ObjCIdType = T;
2574
2575 const TypedefType *TT = T->getAsTypedefType();
2576 if (!TT)
2577 return;
2578
2579 TypedefDecl *TD = TT->getDecl();
Steve Naroff7e219e42007-10-15 14:41:52 +00002580
2581 // typedef struct objc_object *id;
2582 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002583 // User error - caller will issue diagnostics.
2584 if (!ptr)
2585 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002586 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002587 // User error - caller will issue diagnostics.
2588 if (!rec)
2589 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002590 IdStructType = rec;
2591}
2592
Douglas Gregor319ac892009-04-23 22:29:11 +00002593void ASTContext::setObjCSelType(QualType T)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002594{
Douglas Gregor319ac892009-04-23 22:29:11 +00002595 ObjCSelType = T;
2596
2597 const TypedefType *TT = T->getAsTypedefType();
2598 if (!TT)
2599 return;
2600 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002601
2602 // typedef struct objc_selector *SEL;
2603 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002604 if (!ptr)
2605 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002606 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002607 if (!rec)
2608 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002609 SelStructType = rec;
2610}
2611
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002612void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002613{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002614 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002615}
2616
Douglas Gregor319ac892009-04-23 22:29:11 +00002617void ASTContext::setObjCClassType(QualType T)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002618{
Douglas Gregor319ac892009-04-23 22:29:11 +00002619 ObjCClassType = T;
2620
2621 const TypedefType *TT = T->getAsTypedefType();
2622 if (!TT)
2623 return;
2624 TypedefDecl *TD = TT->getDecl();
Anders Carlsson8baaca52007-10-31 02:53:19 +00002625
2626 // typedef struct objc_class *Class;
2627 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2628 assert(ptr && "'Class' incorrectly typed");
2629 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2630 assert(rec && "'Class' incorrectly typed");
2631 ClassStructType = rec;
2632}
2633
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002634void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2635 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002636 "'NSConstantString' type already set!");
2637
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002638 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002639}
2640
Douglas Gregor7532dc62009-03-30 22:58:21 +00002641/// \brief Retrieve the template name that represents a qualified
2642/// template name such as \c std::vector.
2643TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2644 bool TemplateKeyword,
2645 TemplateDecl *Template) {
2646 llvm::FoldingSetNodeID ID;
2647 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2648
2649 void *InsertPos = 0;
2650 QualifiedTemplateName *QTN =
2651 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2652 if (!QTN) {
2653 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2654 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2655 }
2656
2657 return TemplateName(QTN);
2658}
2659
2660/// \brief Retrieve the template name that represents a dependent
2661/// template name such as \c MetaFun::template apply.
2662TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2663 const IdentifierInfo *Name) {
2664 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2665
2666 llvm::FoldingSetNodeID ID;
2667 DependentTemplateName::Profile(ID, NNS, Name);
2668
2669 void *InsertPos = 0;
2670 DependentTemplateName *QTN =
2671 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2672
2673 if (QTN)
2674 return TemplateName(QTN);
2675
2676 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2677 if (CanonNNS == NNS) {
2678 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2679 } else {
2680 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2681 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2682 }
2683
2684 DependentTemplateNames.InsertNode(QTN, InsertPos);
2685 return TemplateName(QTN);
2686}
2687
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002688/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002689/// TargetInfo, produce the corresponding type. The unsigned @p Type
2690/// is actually a value of type @c TargetInfo::IntType.
2691QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002692 switch (Type) {
2693 case TargetInfo::NoInt: return QualType();
2694 case TargetInfo::SignedShort: return ShortTy;
2695 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2696 case TargetInfo::SignedInt: return IntTy;
2697 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2698 case TargetInfo::SignedLong: return LongTy;
2699 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2700 case TargetInfo::SignedLongLong: return LongLongTy;
2701 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2702 }
2703
2704 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002705 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002706}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002707
2708//===----------------------------------------------------------------------===//
2709// Type Predicates.
2710//===----------------------------------------------------------------------===//
2711
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002712/// isObjCNSObjectType - Return true if this is an NSObject object using
2713/// NSObject attribute on a c-style pointer type.
2714/// FIXME - Make it work directly on types.
2715///
2716bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2717 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2718 if (TypedefDecl *TD = TDT->getDecl())
2719 if (TD->getAttr<ObjCNSObjectAttr>())
2720 return true;
2721 }
2722 return false;
2723}
2724
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002725/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2726/// to an object type. This includes "id" and "Class" (two 'special' pointers
2727/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2728/// ID type).
2729bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002730 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002731 return true;
2732
Steve Naroff6ae98502008-10-21 18:24:04 +00002733 // Blocks are objects.
2734 if (Ty->isBlockPointerType())
2735 return true;
2736
2737 // All other object types are pointers.
Chris Lattner16ede0e2009-04-12 23:51:02 +00002738 const PointerType *PT = Ty->getAsPointerType();
2739 if (PT == 0)
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002740 return false;
2741
Chris Lattner16ede0e2009-04-12 23:51:02 +00002742 // If this a pointer to an interface (e.g. NSString*), it is ok.
2743 if (PT->getPointeeType()->isObjCInterfaceType() ||
2744 // If is has NSObject attribute, OK as well.
2745 isObjCNSObjectType(Ty))
2746 return true;
2747
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002748 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2749 // pointer types. This looks for the typedef specifically, not for the
Chris Lattner16ede0e2009-04-12 23:51:02 +00002750 // underlying type. Iteratively strip off typedefs so that we can handle
2751 // typedefs of typedefs.
2752 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2753 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2754 Ty.getUnqualifiedType() == getObjCClassType())
2755 return true;
2756
2757 Ty = TDT->getDecl()->getUnderlyingType();
2758 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002759
Chris Lattner16ede0e2009-04-12 23:51:02 +00002760 return false;
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002761}
2762
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002763/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2764/// garbage collection attribute.
2765///
2766QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002767 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002768 if (getLangOptions().ObjC1 &&
2769 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002770 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002771 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002772 // (or pointers to them) be treated as though they were declared
2773 // as __strong.
2774 if (GCAttrs == QualType::GCNone) {
2775 if (isObjCObjectPointerType(Ty))
2776 GCAttrs = QualType::Strong;
2777 else if (Ty->isPointerType())
2778 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2779 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00002780 // Non-pointers have none gc'able attribute regardless of the attribute
2781 // set on them.
2782 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2783 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002784 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002785 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002786}
2787
Chris Lattner6ac46a42008-04-07 06:51:04 +00002788//===----------------------------------------------------------------------===//
2789// Type Compatibility Testing
2790//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002791
Steve Naroff1c7d0672008-09-04 15:10:53 +00002792/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002793/// block types. Types must be strictly compatible here. For example,
2794/// C unfortunately doesn't produce an error for the following:
2795///
2796/// int (*emptyArgFunc)();
2797/// int (*intArgList)(int) = emptyArgFunc;
2798///
2799/// For blocks, we will produce an error for the following (similar to C++):
2800///
2801/// int (^emptyArgBlock)();
2802/// int (^intArgBlock)(int) = emptyArgBlock;
2803///
2804/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2805///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002806bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002807 const FunctionType *lbase = lhs->getAsFunctionType();
2808 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002809 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2810 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stumpaab0f7a2009-04-01 01:17:39 +00002811 if (lproto && rproto == 0)
2812 return false;
2813 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff1c7d0672008-09-04 15:10:53 +00002814}
2815
Chris Lattner6ac46a42008-04-07 06:51:04 +00002816/// areCompatVectorTypes - Return true if the two specified vector types are
2817/// compatible.
2818static bool areCompatVectorTypes(const VectorType *LHS,
2819 const VectorType *RHS) {
2820 assert(LHS->isCanonical() && RHS->isCanonical());
2821 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002822 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002823}
2824
Eli Friedman3d815e72008-08-22 00:56:42 +00002825/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002826/// compatible for assignment from RHS to LHS. This handles validation of any
2827/// protocol qualifiers on the LHS or RHS.
2828///
Eli Friedman3d815e72008-08-22 00:56:42 +00002829bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2830 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002831 // Verify that the base decls are compatible: the RHS must be a subclass of
2832 // the LHS.
2833 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2834 return false;
2835
2836 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2837 // protocol qualified at all, then we are good.
2838 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2839 return true;
2840
2841 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2842 // isn't a superset.
2843 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2844 return true; // FIXME: should return false!
2845
2846 // Finally, we must have two protocol-qualified interfaces.
2847 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2848 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002849
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002850 // All LHS protocols must have a presence on the RHS.
2851 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002852
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002853 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2854 LHSPE = LHSP->qual_end();
2855 LHSPI != LHSPE; LHSPI++) {
2856 bool RHSImplementsProtocol = false;
2857
2858 // If the RHS doesn't implement the protocol on the left, the types
2859 // are incompatible.
2860 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2861 RHSPE = RHSP->qual_end();
2862 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2863 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2864 RHSImplementsProtocol = true;
2865 }
2866 // FIXME: For better diagnostics, consider passing back the protocol name.
2867 if (!RHSImplementsProtocol)
2868 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002869 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002870 // The RHS implements all protocols listed on the LHS.
2871 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002872}
2873
Steve Naroff389bf462009-02-12 17:52:19 +00002874bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2875 // get the "pointed to" types
2876 const PointerType *LHSPT = LHS->getAsPointerType();
2877 const PointerType *RHSPT = RHS->getAsPointerType();
2878
2879 if (!LHSPT || !RHSPT)
2880 return false;
2881
2882 QualType lhptee = LHSPT->getPointeeType();
2883 QualType rhptee = RHSPT->getPointeeType();
2884 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2885 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2886 // ID acts sort of like void* for ObjC interfaces
2887 if (LHSIface && isObjCIdStructType(rhptee))
2888 return true;
2889 if (RHSIface && isObjCIdStructType(lhptee))
2890 return true;
2891 if (!LHSIface || !RHSIface)
2892 return false;
2893 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2894 canAssignObjCInterfaces(RHSIface, LHSIface);
2895}
2896
Steve Naroffec0550f2007-10-15 20:41:53 +00002897/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2898/// both shall have the identically qualified version of a compatible type.
2899/// C99 6.2.7p1: Two types have compatible types if their types are the
2900/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002901bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2902 return !mergeTypes(LHS, RHS).isNull();
2903}
2904
2905QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2906 const FunctionType *lbase = lhs->getAsFunctionType();
2907 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002908 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2909 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002910 bool allLTypes = true;
2911 bool allRTypes = true;
2912
2913 // Check return type
2914 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2915 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002916 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2917 allLTypes = false;
2918 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2919 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002920
2921 if (lproto && rproto) { // two C99 style function prototypes
2922 unsigned lproto_nargs = lproto->getNumArgs();
2923 unsigned rproto_nargs = rproto->getNumArgs();
2924
2925 // Compatible functions must have the same number of arguments
2926 if (lproto_nargs != rproto_nargs)
2927 return QualType();
2928
2929 // Variadic and non-variadic functions aren't compatible
2930 if (lproto->isVariadic() != rproto->isVariadic())
2931 return QualType();
2932
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002933 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2934 return QualType();
2935
Eli Friedman3d815e72008-08-22 00:56:42 +00002936 // Check argument compatibility
2937 llvm::SmallVector<QualType, 10> types;
2938 for (unsigned i = 0; i < lproto_nargs; i++) {
2939 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2940 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2941 QualType argtype = mergeTypes(largtype, rargtype);
2942 if (argtype.isNull()) return QualType();
2943 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002944 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2945 allLTypes = false;
2946 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2947 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002948 }
2949 if (allLTypes) return lhs;
2950 if (allRTypes) return rhs;
2951 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002952 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002953 }
2954
2955 if (lproto) allRTypes = false;
2956 if (rproto) allLTypes = false;
2957
Douglas Gregor72564e72009-02-26 23:50:07 +00002958 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002959 if (proto) {
2960 if (proto->isVariadic()) return QualType();
2961 // Check that the types are compatible with the types that
2962 // would result from default argument promotions (C99 6.7.5.3p15).
2963 // The only types actually affected are promotable integer
2964 // types and floats, which would be passed as a different
2965 // type depending on whether the prototype is visible.
2966 unsigned proto_nargs = proto->getNumArgs();
2967 for (unsigned i = 0; i < proto_nargs; ++i) {
2968 QualType argTy = proto->getArgType(i);
2969 if (argTy->isPromotableIntegerType() ||
2970 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2971 return QualType();
2972 }
2973
2974 if (allLTypes) return lhs;
2975 if (allRTypes) return rhs;
2976 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002977 proto->getNumArgs(), lproto->isVariadic(),
2978 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002979 }
2980
2981 if (allLTypes) return lhs;
2982 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002983 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002984}
2985
2986QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002987 // C++ [expr]: If an expression initially has the type "reference to T", the
2988 // type is adjusted to "T" prior to any further analysis, the expression
2989 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002990 // expression is an lvalue unless the reference is an rvalue reference and
2991 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00002992 // FIXME: C++ shouldn't be going through here! The rules are different
2993 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002994 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2995 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00002996 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002997 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002998 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002999 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00003000
Eli Friedman3d815e72008-08-22 00:56:42 +00003001 QualType LHSCan = getCanonicalType(LHS),
3002 RHSCan = getCanonicalType(RHS);
3003
3004 // If two types are identical, they are compatible.
3005 if (LHSCan == RHSCan)
3006 return LHS;
3007
3008 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003009 // Note that we handle extended qualifiers later, in the
3010 // case for ExtQualType.
3011 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00003012 return QualType();
3013
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003014 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
3015 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003016
Chris Lattner1adb8832008-01-14 05:45:46 +00003017 // We want to consider the two function types to be the same for these
3018 // comparisons, just force one to the other.
3019 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3020 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003021
3022 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003023 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3024 LHSClass = Type::ConstantArray;
3025 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3026 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00003027
Nate Begeman213541a2008-04-18 23:10:10 +00003028 // Canonicalize ExtVector -> Vector.
3029 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3030 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00003031
Chris Lattnerb0489812008-04-07 06:38:24 +00003032 // Consider qualified interfaces and interfaces the same.
3033 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
3034 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00003035
Chris Lattnera36a61f2008-04-07 05:43:21 +00003036 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003037 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003038 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3039 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003040
Steve Naroffd824c9c2009-04-14 15:11:46 +00003041 // 'id' and 'Class' act sort of like void* for ObjC interfaces
3042 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003043 return LHS;
Steve Naroffd824c9c2009-04-14 15:11:46 +00003044 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003045 return RHS;
3046
Steve Naroffbc76dd02008-12-10 22:14:21 +00003047 // ID is compatible with all qualified id types.
3048 if (LHS->isObjCQualifiedIdType()) {
3049 if (const PointerType *PT = RHS->getAsPointerType()) {
3050 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003051 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003052 return LHS;
3053 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3054 // Unfortunately, this API is part of Sema (which we don't have access
3055 // to. Need to refactor. The following check is insufficient, since we
3056 // need to make sure the class implements the protocol.
3057 if (pType->isObjCInterfaceType())
3058 return LHS;
3059 }
3060 }
3061 if (RHS->isObjCQualifiedIdType()) {
3062 if (const PointerType *PT = LHS->getAsPointerType()) {
3063 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003064 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003065 return RHS;
3066 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3067 // Unfortunately, this API is part of Sema (which we don't have access
3068 // to. Need to refactor. The following check is insufficient, since we
3069 // need to make sure the class implements the protocol.
3070 if (pType->isObjCInterfaceType())
3071 return RHS;
3072 }
3073 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003074 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3075 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00003076 if (const EnumType* ETy = LHS->getAsEnumType()) {
3077 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3078 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003079 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003080 if (const EnumType* ETy = RHS->getAsEnumType()) {
3081 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3082 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003083 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003084
Eli Friedman3d815e72008-08-22 00:56:42 +00003085 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003086 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003087
Steve Naroff4a746782008-01-09 22:43:08 +00003088 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003089 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003090#define TYPE(Class, Base)
3091#define ABSTRACT_TYPE(Class, Base)
3092#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3093#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3094#include "clang/AST/TypeNodes.def"
3095 assert(false && "Non-canonical and dependent types shouldn't get here");
3096 return QualType();
3097
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003098 case Type::LValueReference:
3099 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003100 case Type::MemberPointer:
3101 assert(false && "C++ should never be in mergeTypes");
3102 return QualType();
3103
3104 case Type::IncompleteArray:
3105 case Type::VariableArray:
3106 case Type::FunctionProto:
3107 case Type::ExtVector:
3108 case Type::ObjCQualifiedInterface:
3109 assert(false && "Types are eliminated above");
3110 return QualType();
3111
Chris Lattner1adb8832008-01-14 05:45:46 +00003112 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003113 {
3114 // Merge two pointer types, while trying to preserve typedef info
3115 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3116 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3117 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3118 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003119 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3120 return LHS;
3121 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3122 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003123 return getPointerType(ResultType);
3124 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003125 case Type::BlockPointer:
3126 {
3127 // Merge two block pointer types, while trying to preserve typedef info
3128 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3129 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3130 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3131 if (ResultType.isNull()) return QualType();
3132 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3133 return LHS;
3134 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3135 return RHS;
3136 return getBlockPointerType(ResultType);
3137 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003138 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003139 {
3140 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3141 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3142 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3143 return QualType();
3144
3145 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3146 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3147 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3148 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003149 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3150 return LHS;
3151 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3152 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003153 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3154 ArrayType::ArraySizeModifier(), 0);
3155 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3156 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003157 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3158 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003159 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3160 return LHS;
3161 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3162 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003163 if (LVAT) {
3164 // FIXME: This isn't correct! But tricky to implement because
3165 // the array's size has to be the size of LHS, but the type
3166 // has to be different.
3167 return LHS;
3168 }
3169 if (RVAT) {
3170 // FIXME: This isn't correct! But tricky to implement because
3171 // the array's size has to be the size of RHS, but the type
3172 // has to be different.
3173 return RHS;
3174 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003175 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3176 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003177 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003178 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003179 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003180 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003181 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003182 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003183 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003184 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3185 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003186 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003187 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003188 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003189 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003190 case Type::Complex:
3191 // Distinct complex types are incompatible.
3192 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003193 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003194 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003195 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3196 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003197 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003198 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003199 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003200 // FIXME: This should be type compatibility, e.g. whether
3201 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003202 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3203 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3204 if (LHSIface && RHSIface &&
3205 canAssignObjCInterfaces(LHSIface, RHSIface))
3206 return LHS;
3207
Eli Friedman3d815e72008-08-22 00:56:42 +00003208 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003209 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003210 case Type::ObjCQualifiedId:
3211 // Distinct qualified id's are not compatible.
3212 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003213 case Type::FixedWidthInt:
3214 // Distinct fixed-width integers are not compatible.
3215 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003216 case Type::ExtQual:
3217 // FIXME: ExtQual types can be compatible even if they're not
3218 // identical!
3219 return QualType();
3220 // First attempt at an implementation, but I'm not really sure it's
3221 // right...
3222#if 0
3223 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3224 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3225 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3226 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3227 return QualType();
3228 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3229 LHSBase = QualType(LQual->getBaseType(), 0);
3230 RHSBase = QualType(RQual->getBaseType(), 0);
3231 ResultType = mergeTypes(LHSBase, RHSBase);
3232 if (ResultType.isNull()) return QualType();
3233 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3234 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3235 return LHS;
3236 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3237 return RHS;
3238 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3239 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3240 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3241 return ResultType;
3242#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003243
3244 case Type::TemplateSpecialization:
3245 assert(false && "Dependent types have no size");
3246 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003247 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003248
3249 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003250}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003251
Chris Lattner5426bf62008-04-07 07:01:58 +00003252//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003253// Integer Predicates
3254//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003255
Eli Friedmanad74a752008-06-28 06:23:08 +00003256unsigned ASTContext::getIntWidth(QualType T) {
3257 if (T == BoolTy)
3258 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003259 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3260 return FWIT->getWidth();
3261 }
3262 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003263 return (unsigned)getTypeSize(T);
3264}
3265
3266QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3267 assert(T->isSignedIntegerType() && "Unexpected type");
3268 if (const EnumType* ETy = T->getAsEnumType())
3269 T = ETy->getDecl()->getIntegerType();
3270 const BuiltinType* BTy = T->getAsBuiltinType();
3271 assert (BTy && "Unexpected signed integer type");
3272 switch (BTy->getKind()) {
3273 case BuiltinType::Char_S:
3274 case BuiltinType::SChar:
3275 return UnsignedCharTy;
3276 case BuiltinType::Short:
3277 return UnsignedShortTy;
3278 case BuiltinType::Int:
3279 return UnsignedIntTy;
3280 case BuiltinType::Long:
3281 return UnsignedLongTy;
3282 case BuiltinType::LongLong:
3283 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00003284 case BuiltinType::Int128:
3285 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00003286 default:
3287 assert(0 && "Unexpected signed integer type");
3288 return QualType();
3289 }
3290}
3291
Douglas Gregor2cf26342009-04-09 22:27:44 +00003292ExternalASTSource::~ExternalASTSource() { }
3293
3294void ExternalASTSource::PrintStats() { }