blob: 5f97df904cc343c8e3a3f876ca09c143f295d202 [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 {
66 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
67 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
68 while (I != E) {
69 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
70 delete R;
71 }
72 }
73
74 {
Chris Lattner23499252009-03-31 09:24:30 +000075 llvm::DenseMap<const ObjCInterfaceDecl*, RecordDecl*>::iterator
Nuno Lopesb74668e2008-12-17 22:30:25 +000076 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
77 while (I != E) {
Chris Lattner23499252009-03-31 09:24:30 +000078 RecordDecl *R = (I++)->second;
Nuno Lopesb74668e2008-12-17 22:30:25 +000079 R->Destroy(*this);
80 }
81 }
82
Douglas Gregorab452ba2009-03-26 23:50:42 +000083 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000084 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
85 NNS = NestedNameSpecifiers.begin(),
86 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregore7dcd782009-03-27 23:25:45 +000087 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000088 /* Increment in loop */)
89 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000090
91 if (GlobalNestedNameSpecifier)
92 GlobalNestedNameSpecifier->Destroy(*this);
93
Eli Friedmanb26153c2008-05-27 03:08:09 +000094 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000095}
96
Douglas Gregor2deaea32009-04-22 18:49:13 +000097void ASTContext::InitializeBuiltins(IdentifierTable &idents) {
Douglas Gregor2deaea32009-04-22 18:49:13 +000098 BuiltinInfo.InitializeBuiltins(idents, LangOpts.NoBuiltin);
99}
100
Douglas Gregor2cf26342009-04-09 22:27:44 +0000101void
102ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
103 ExternalSource.reset(Source.take());
104}
105
Reid Spencer5f016e22007-07-11 17:01:13 +0000106void ASTContext::PrintStats() const {
107 fprintf(stderr, "*** AST Context Stats:\n");
108 fprintf(stderr, " %d types total.\n", (int)Types.size());
109 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000110 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000111 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
112 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
113
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000115 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
116 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000117 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000118 unsigned NumExtQual = 0;
119
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
121 Type *T = Types[i];
122 if (isa<BuiltinType>(T))
123 ++NumBuiltin;
124 else if (isa<PointerType>(T))
125 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000126 else if (isa<BlockPointerType>(T))
127 ++NumBlockPointer;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000128 else if (isa<LValueReferenceType>(T))
129 ++NumLValueReference;
130 else if (isa<RValueReferenceType>(T))
131 ++NumRValueReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000132 else if (isa<MemberPointerType>(T))
133 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000134 else if (isa<ComplexType>(T))
135 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 else if (isa<ArrayType>(T))
137 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000138 else if (isa<VectorType>(T))
139 ++NumVector;
Douglas Gregor72564e72009-02-26 23:50:07 +0000140 else if (isa<FunctionNoProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 ++NumFunctionNP;
Douglas Gregor72564e72009-02-26 23:50:07 +0000142 else if (isa<FunctionProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 ++NumFunctionP;
144 else if (isa<TypedefType>(T))
145 ++NumTypeName;
146 else if (TagType *TT = dyn_cast<TagType>(T)) {
147 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000148 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000150 case TagDecl::TK_struct: ++NumTagStruct; break;
151 case TagDecl::TK_union: ++NumTagUnion; break;
152 case TagDecl::TK_class: ++NumTagClass; break;
153 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000155 } else if (isa<ObjCInterfaceType>(T))
156 ++NumObjCInterfaces;
157 else if (isa<ObjCQualifiedInterfaceType>(T))
158 ++NumObjCQualifiedInterfaces;
159 else if (isa<ObjCQualifiedIdType>(T))
160 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000161 else if (isa<TypeOfType>(T))
162 ++NumTypeOfTypes;
Douglas Gregor72564e72009-02-26 23:50:07 +0000163 else if (isa<TypeOfExprType>(T))
164 ++NumTypeOfExprTypes;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000165 else if (isa<ExtQualType>(T))
166 ++NumExtQual;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000167 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000168 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 assert(0 && "Unknown type!");
170 }
171 }
172
173 fprintf(stderr, " %d builtin types\n", NumBuiltin);
174 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000175 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000176 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
177 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000178 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000179 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000181 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
183 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
184 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
185 fprintf(stderr, " %d tagged types\n", NumTagged);
186 fprintf(stderr, " %d struct types\n", NumTagStruct);
187 fprintf(stderr, " %d union types\n", NumTagUnion);
188 fprintf(stderr, " %d class types\n", NumTagClass);
189 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000190 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000191 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000192 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000193 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000194 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000195 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor72564e72009-02-26 23:50:07 +0000196 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000197 fprintf(stderr, " %d attribute-qualified types\n", NumExtQual);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000198
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
200 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000201 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000202 NumLValueReference*sizeof(LValueReferenceType)+
203 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000204 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000205 NumFunctionP*sizeof(FunctionProtoType)+
206 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000207 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000208 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)+
209 NumExtQual*sizeof(ExtQualType)));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000210
211 if (ExternalSource.get()) {
212 fprintf(stderr, "\n");
213 ExternalSource->PrintStats();
214 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000215}
216
217
218void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000219 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000220}
221
Reid Spencer5f016e22007-07-11 17:01:13 +0000222void ASTContext::InitBuiltinTypes() {
223 assert(VoidTy.isNull() && "Context reinitialized?");
224
225 // C99 6.2.5p19.
226 InitBuiltinType(VoidTy, BuiltinType::Void);
227
228 // C99 6.2.5p2.
229 InitBuiltinType(BoolTy, BuiltinType::Bool);
230 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000231 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 InitBuiltinType(CharTy, BuiltinType::Char_S);
233 else
234 InitBuiltinType(CharTy, BuiltinType::Char_U);
235 // C99 6.2.5p4.
236 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
237 InitBuiltinType(ShortTy, BuiltinType::Short);
238 InitBuiltinType(IntTy, BuiltinType::Int);
239 InitBuiltinType(LongTy, BuiltinType::Long);
240 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
241
242 // C99 6.2.5p6.
243 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
244 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
245 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
246 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
247 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
248
249 // C99 6.2.5p10.
250 InitBuiltinType(FloatTy, BuiltinType::Float);
251 InitBuiltinType(DoubleTy, BuiltinType::Double);
252 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000253
Chris Lattner2df9ced2009-04-30 02:43:43 +0000254 // GNU extension, 128-bit integers.
255 InitBuiltinType(Int128Ty, BuiltinType::Int128);
256 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
257
Chris Lattner3a250322009-02-26 23:43:47 +0000258 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
259 InitBuiltinType(WCharTy, BuiltinType::WChar);
260 else // C99
261 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000262
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000263 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000264 InitBuiltinType(OverloadTy, BuiltinType::Overload);
265
266 // Placeholder type for type-dependent expressions whose type is
267 // completely unknown. No code should ever check a type against
268 // DependentTy and users should never see it; however, it is here to
269 // help diagnose failures to properly check for type-dependent
270 // expressions.
271 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000272
Reid Spencer5f016e22007-07-11 17:01:13 +0000273 // C99 6.2.5p11.
274 FloatComplexTy = getComplexType(FloatTy);
275 DoubleComplexTy = getComplexType(DoubleTy);
276 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000277
Steve Naroff7e219e42007-10-15 14:41:52 +0000278 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000279 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000280 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000281 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000282 ClassStructType = 0;
283
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000284 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000285
286 // void * type
287 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000288}
289
Chris Lattner464175b2007-07-18 17:52:12 +0000290//===----------------------------------------------------------------------===//
291// Type Sizing and Analysis
292//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000293
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000294/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
295/// scalar floating point type.
296const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
297 const BuiltinType *BT = T->getAsBuiltinType();
298 assert(BT && "Not a floating point type!");
299 switch (BT->getKind()) {
300 default: assert(0 && "Not a floating point type!");
301 case BuiltinType::Float: return Target.getFloatFormat();
302 case BuiltinType::Double: return Target.getDoubleFormat();
303 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
304 }
305}
306
Chris Lattneraf707ab2009-01-24 21:53:27 +0000307/// getDeclAlign - Return a conservative estimate of the alignment of the
308/// specified decl. Note that bitfields do not have a valid alignment, so
309/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000310unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000311 unsigned Align = Target.getCharWidth();
312
313 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
314 Align = std::max(Align, AA->getAlignment());
315
Chris Lattneraf707ab2009-01-24 21:53:27 +0000316 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
317 QualType T = VD->getType();
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000318 if (const ReferenceType* RT = T->getAsReferenceType()) {
319 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000320 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000321 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
322 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000323 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
324 T = cast<ArrayType>(T)->getElementType();
325
326 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
327 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000328 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000329
330 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000331}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000332
Chris Lattnera7674d82007-07-13 22:13:22 +0000333/// getTypeSize - Return the size of the specified type, in bits. This method
334/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000335std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000336ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000337 T = getCanonicalType(T);
Mike Stump5e301002009-02-27 18:32:39 +0000338 uint64_t Width=0;
339 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000340 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000341#define TYPE(Class, Base)
342#define ABSTRACT_TYPE(Class, Base)
343#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
344#define DEPENDENT_TYPE(Class, Base) case Type::Class:
345#include "clang/AST/TypeNodes.def"
346 assert(false && "Should not see non-canonical or dependent types");
347 break;
348
Chris Lattner692233e2007-07-13 22:27:08 +0000349 case Type::FunctionNoProto:
350 case Type::FunctionProto:
Douglas Gregor72564e72009-02-26 23:50:07 +0000351 case Type::IncompleteArray:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000352 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000353 case Type::VariableArray:
354 assert(0 && "VLAs not implemented yet!");
355 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:
380 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000381 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000382 Width = Target.getBoolWidth();
383 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000384 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000385 case BuiltinType::Char_S:
386 case BuiltinType::Char_U:
387 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000388 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000389 Width = Target.getCharWidth();
390 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000391 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000392 case BuiltinType::WChar:
393 Width = Target.getWCharWidth();
394 Align = Target.getWCharAlign();
395 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000396 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000397 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000398 Width = Target.getShortWidth();
399 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000400 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000401 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000402 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000403 Width = Target.getIntWidth();
404 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000405 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000406 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000407 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000408 Width = Target.getLongWidth();
409 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000410 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000411 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000412 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000413 Width = Target.getLongLongWidth();
414 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000415 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000416 case BuiltinType::Int128:
417 case BuiltinType::UInt128:
418 Width = 128;
419 Align = 128; // int128_t is 128-bit aligned on all targets.
420 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000421 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000422 Width = Target.getFloatWidth();
423 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000424 break;
425 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000426 Width = Target.getDoubleWidth();
427 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000428 break;
429 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000430 Width = Target.getLongDoubleWidth();
431 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000432 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000433 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000434 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000435 case Type::FixedWidthInt:
436 // FIXME: This isn't precisely correct; the width/alignment should depend
437 // on the available types for the target
438 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000439 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000440 Align = Width;
441 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000442 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000443 // FIXME: Pointers into different addr spaces could have different sizes and
444 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000445 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000446 case Type::ObjCQualifiedId:
Douglas Gregor72564e72009-02-26 23:50:07 +0000447 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000448 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000449 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000450 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000451 case Type::BlockPointer: {
452 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
453 Width = Target.getPointerWidth(AS);
454 Align = Target.getPointerAlign(AS);
455 break;
456 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000457 case Type::Pointer: {
458 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000459 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000460 Align = Target.getPointerAlign(AS);
461 break;
462 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000463 case Type::LValueReference:
464 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000465 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000466 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000467 // FIXME: This is wrong for struct layout: a reference in a struct has
468 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000469 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000470 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000471 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000472 // the GCC ABI, where pointers to data are one pointer large, pointers to
473 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000474 // other compilers too, we need to delegate this completely to TargetInfo
475 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000476 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
477 unsigned AS = Pointee.getAddressSpace();
478 Width = Target.getPointerWidth(AS);
479 if (Pointee->isFunctionType())
480 Width *= 2;
481 Align = Target.getPointerAlign(AS);
482 // GCC aligns at single pointer width.
483 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000484 case Type::Complex: {
485 // Complex types have the same alignment as their elements, but twice the
486 // size.
487 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000488 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000489 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000490 Align = EltInfo.second;
491 break;
492 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000493 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000494 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000495 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
496 Width = Layout.getSize();
497 Align = Layout.getAlignment();
498 break;
499 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000500 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000501 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000502 const TagType *TT = cast<TagType>(T);
503
504 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000505 Width = 1;
506 Align = 1;
507 break;
508 }
509
Daniel Dunbar1d751182008-11-08 05:48:37 +0000510 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000511 return getTypeInfo(ET->getDecl()->getIntegerType());
512
Daniel Dunbar1d751182008-11-08 05:48:37 +0000513 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000514 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
515 Width = Layout.getSize();
516 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000517 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000518 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000519
520 case Type::TemplateSpecialization:
521 assert(false && "Dependent types have no size");
522 break;
Chris Lattner71763312008-04-06 22:05:18 +0000523 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000524
Chris Lattner464175b2007-07-18 17:52:12 +0000525 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000526 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000527}
528
Chris Lattner34ebde42009-01-27 18:08:34 +0000529/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
530/// type for the current target in bits. This can be different than the ABI
531/// alignment in cases where it is beneficial for performance to overalign
532/// a data type.
533unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
534 unsigned ABIAlign = getTypeAlign(T);
535
536 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000537 if (T->isSpecificBuiltinType(BuiltinType::Double))
538 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000539
540 return ABIAlign;
541}
542
543
Devang Patel8b277042008-06-04 21:22:16 +0000544/// LayoutField - Field layout.
545void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000546 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000547 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000548 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000549 uint64_t FieldOffset = IsUnion ? 0 : Size;
550 uint64_t FieldSize;
551 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000552
553 // FIXME: Should this override struct packing? Probably we want to
554 // take the minimum?
555 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
556 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000557
558 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
559 // TODO: Need to check this algorithm on other targets!
560 // (tested on Linux-X86)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000561 FieldSize = BitWidthExpr->EvaluateAsInt(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000562
563 std::pair<uint64_t, unsigned> FieldInfo =
564 Context.getTypeInfo(FD->getType());
565 uint64_t TypeSize = FieldInfo.first;
566
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000567 // Determine the alignment of this bitfield. The packing
568 // attributes define a maximum and the alignment attribute defines
569 // a minimum.
570 // FIXME: What is the right behavior when the specified alignment
571 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000572 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000573 if (FieldPacking)
574 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000575 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
576 FieldAlign = std::max(FieldAlign, AA->getAlignment());
577
578 // Check if we need to add padding to give the field the correct
579 // alignment.
580 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
581 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
582
583 // Padding members don't affect overall alignment
584 if (!FD->getIdentifier())
585 FieldAlign = 1;
586 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000587 if (FD->getType()->isIncompleteArrayType()) {
588 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000589 // query getTypeInfo about these, so we figure it out here.
590 // Flexible array members don't have any size, but they
591 // have to be aligned appropriately for their element type.
592 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000593 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000594 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson2f1169f2009-04-10 05:31:15 +0000595 } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
596 unsigned AS = RT->getPointeeType().getAddressSpace();
597 FieldSize = Context.Target.getPointerWidth(AS);
598 FieldAlign = Context.Target.getPointerAlign(AS);
Devang Patel8b277042008-06-04 21:22:16 +0000599 } else {
600 std::pair<uint64_t, unsigned> FieldInfo =
601 Context.getTypeInfo(FD->getType());
602 FieldSize = FieldInfo.first;
603 FieldAlign = FieldInfo.second;
604 }
605
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000606 // Determine the alignment of this bitfield. The packing
607 // attributes define a maximum and the alignment attribute defines
608 // a minimum. Additionally, the packing alignment must be at least
609 // a byte for non-bitfields.
610 //
611 // FIXME: What is the right behavior when the specified alignment
612 // is smaller than the specified packing?
613 if (FieldPacking)
614 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000615 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
616 FieldAlign = std::max(FieldAlign, AA->getAlignment());
617
618 // Round up the current record size to the field's alignment boundary.
619 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
620 }
621
622 // Place this field at the current location.
623 FieldOffsets[FieldNo] = FieldOffset;
624
625 // Reserve space for this field.
626 if (IsUnion) {
627 Size = std::max(Size, FieldSize);
628 } else {
629 Size = FieldOffset + FieldSize;
630 }
631
632 // Remember max struct/class alignment.
633 Alignment = std::max(Alignment, FieldAlign);
634}
635
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000636static void CollectLocalObjCIvars(ASTContext *Ctx,
637 const ObjCInterfaceDecl *OI,
638 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000639 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
640 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000641 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000642 if (!IVDecl->isInvalidDecl())
643 Fields.push_back(cast<FieldDecl>(IVDecl));
644 }
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +0000645 // look into properties.
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000646 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*Ctx),
647 E = OI->prop_end(*Ctx); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000648 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +0000649 Fields.push_back(cast<FieldDecl>(IV));
650 }
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000651}
652
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000653void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
654 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
655 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
656 CollectObjCIvars(SuperClass, Fields);
657 CollectLocalObjCIvars(this, OI, Fields);
658}
659
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000660/// addRecordToClass - produces record info. for the class for its
661/// ivars and all those inherited.
662///
Chris Lattnerf1690852009-03-31 08:48:01 +0000663const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D) {
Daniel Dunbar75da6742009-04-22 10:56:29 +0000664 assert(!D->isForwardDecl() && "Invalid decl!");
665
Chris Lattner23499252009-03-31 09:24:30 +0000666 RecordDecl *&RD = ASTRecordForInterface[D];
Daniel Dunbar75da6742009-04-22 10:56:29 +0000667 if (RD)
668 return RD;
Chris Lattnerf1690852009-03-31 08:48:01 +0000669
670 llvm::SmallVector<FieldDecl*, 32> RecFields;
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000671 CollectLocalObjCIvars(this, D, RecFields);
Chris Lattner23499252009-03-31 09:24:30 +0000672
Daniel Dunbar75da6742009-04-22 10:56:29 +0000673 RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, D->getLocation(),
674 D->getIdentifier());
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000675 const RecordDecl *SRD;
676 if (const ObjCInterfaceDecl *SuperClass = D->getSuperClass()) {
677 SRD = addRecordToClass(SuperClass);
678 } else {
679 SRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, SourceLocation(), 0);
680 const_cast<RecordDecl*>(SRD)->completeDefinition(*this);
681 }
682
683 RD->addDecl(*this,
684 FieldDecl::Create(*this, RD,
685 SourceLocation(),
686 0,
687 getTagDeclType(const_cast<RecordDecl*>(SRD)),
688 0, false));
689
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000690 /// FIXME! Can do collection of ivars and adding to the record while
691 /// doing it.
Chris Lattner16ff7052009-03-31 08:58:42 +0000692 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000693 RD->addDecl(*this,
694 FieldDecl::Create(*this, RD,
Chris Lattner23499252009-03-31 09:24:30 +0000695 RecFields[i]->getLocation(),
696 RecFields[i]->getIdentifier(),
697 RecFields[i]->getType(),
698 RecFields[i]->getBitWidth(), false));
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000699 }
Chris Lattnerf1690852009-03-31 08:48:01 +0000700
Chris Lattner23499252009-03-31 09:24:30 +0000701 RD->completeDefinition(*this);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000702 return RD;
703}
Devang Patel44a3dde2008-06-04 21:54:36 +0000704
Chris Lattner61710852008-10-05 17:34:18 +0000705/// getASTObjcInterfaceLayout - Get or compute information about the layout of
706/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000707/// position information.
708const ASTRecordLayout &
709ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
710 // Look up this layout, if already laid out, return what we have.
711 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
712 if (Entry) return *Entry;
713
714 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
715 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000716 ASTRecordLayout *NewEntry = NULL;
Fariborz Jahanian0c7ce5b2009-04-08 21:54:52 +0000717 // FIXME. Add actual count of synthesized ivars, instead of count
718 // of properties which is the upper bound, but is safe.
Daniel Dunbar4af44122009-04-08 20:18:15 +0000719 unsigned FieldCount =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000720 D->ivar_size() + std::distance(D->prop_begin(*this), D->prop_end(*this));
Devang Patel6a5a34c2008-06-06 02:14:01 +0000721 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
722 FieldCount++;
723 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
724 unsigned Alignment = SL.getAlignment();
725 uint64_t Size = SL.getSize();
726 NewEntry = new ASTRecordLayout(Size, Alignment);
727 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000728 // Super class is at the beginning of the layout.
729 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000730 } else {
731 NewEntry = new ASTRecordLayout();
732 NewEntry->InitializeLayout(FieldCount);
733 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000734 Entry = NewEntry;
735
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 }
Fariborz Jahanian18191882009-03-31 18:11:23 +0000751 // Also synthesized ivars
Douglas Gregor6ab35242009-04-09 21:40:53 +0000752 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
753 E = D->prop_end(*this); I != E; ++I) {
Fariborz Jahanian18191882009-03-31 18:11:23 +0000754 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
755 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
756 }
Fariborz Jahanian99eee362009-04-01 19:37:34 +0000757
Devang Patel44a3dde2008-06-04 21:54:36 +0000758 // Finally, round the size of the total struct up to the alignment of the
759 // struct itself.
760 NewEntry->FinalizeLayout();
761 return *NewEntry;
762}
763
Devang Patel88a981b2007-11-01 19:11:01 +0000764/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000765/// specified record (struct/union/class), which indicates its size and field
766/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000767const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000768 D = D->getDefinition(*this);
769 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000770
Chris Lattner464175b2007-07-18 17:52:12 +0000771 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000772 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000773 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000774
Devang Patel88a981b2007-11-01 19:11:01 +0000775 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
776 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
777 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000778 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000779
Douglas Gregore267ff32008-12-11 20:41:00 +0000780 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000781 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
782 D->field_end(*this)));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000783 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000784
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000785 unsigned StructPacking = 0;
786 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
787 StructPacking = PA->getAlignment();
788
Eli Friedman4bd998b2008-05-30 09:31:38 +0000789 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000790 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
791 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000792
Eli Friedman4bd998b2008-05-30 09:31:38 +0000793 // Layout each field, for now, just sequentially, respecting alignment. In
794 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000795 unsigned FieldIdx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000796 for (RecordDecl::field_iterator Field = D->field_begin(*this),
797 FieldEnd = D->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000798 Field != FieldEnd; (void)++Field, ++FieldIdx)
799 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000800
801 // Finally, round the size of the total struct up to the alignment of the
802 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000803 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000804 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000805}
806
Chris Lattnera7674d82007-07-13 22:13:22 +0000807//===----------------------------------------------------------------------===//
808// Type creation/memoization methods
809//===----------------------------------------------------------------------===//
810
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000811QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000812 QualType CanT = getCanonicalType(T);
813 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000814 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000815
816 // If we are composing extended qualifiers together, merge together into one
817 // ExtQualType node.
818 unsigned CVRQuals = T.getCVRQualifiers();
819 QualType::GCAttrTypes GCAttr = QualType::GCNone;
820 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000821
Chris Lattnerb7d25532009-02-18 22:53:11 +0000822 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
823 // If this type already has an address space specified, it cannot get
824 // another one.
825 assert(EQT->getAddressSpace() == 0 &&
826 "Type cannot be in multiple addr spaces!");
827 GCAttr = EQT->getObjCGCAttr();
828 TypeNode = EQT->getBaseType();
829 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000830
Chris Lattnerb7d25532009-02-18 22:53:11 +0000831 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000832 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000833 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000834 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000835 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000836 return QualType(EXTQy, CVRQuals);
837
Christopher Lambebb97e92008-02-04 02:31:56 +0000838 // If the base type isn't canonical, this won't be a canonical type either,
839 // so fill in the canonical type field.
840 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000841 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000842 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000843
Chris Lattnerb7d25532009-02-18 22:53:11 +0000844 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000845 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000846 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000847 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000848 ExtQualType *New =
849 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000850 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000851 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000852 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000853}
854
Chris Lattnerb7d25532009-02-18 22:53:11 +0000855QualType ASTContext::getObjCGCQualType(QualType T,
856 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000857 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000858 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000859 return T;
860
Chris Lattnerb7d25532009-02-18 22:53:11 +0000861 // If we are composing extended qualifiers together, merge together into one
862 // ExtQualType node.
863 unsigned CVRQuals = T.getCVRQualifiers();
864 Type *TypeNode = T.getTypePtr();
865 unsigned AddressSpace = 0;
866
867 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
868 // If this type already has an address space specified, it cannot get
869 // another one.
870 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
871 "Type cannot be in multiple addr spaces!");
872 AddressSpace = EQT->getAddressSpace();
873 TypeNode = EQT->getBaseType();
874 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000875
876 // Check if we've already instantiated an gc qual'd type of this type.
877 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000878 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000879 void *InsertPos = 0;
880 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000881 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000882
883 // If the base type isn't canonical, this won't be a canonical type either,
884 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000885 // FIXME: Isn't this also not canonical if the base type is a array
886 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000887 QualType Canonical;
888 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000889 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000890
Chris Lattnerb7d25532009-02-18 22:53:11 +0000891 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000892 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
893 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
894 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000895 ExtQualType *New =
896 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000897 ExtQualTypes.InsertNode(New, InsertPos);
898 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000899 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000900}
Chris Lattnera7674d82007-07-13 22:13:22 +0000901
Reid Spencer5f016e22007-07-11 17:01:13 +0000902/// getComplexType - Return the uniqued reference to the type for a complex
903/// number with the specified element type.
904QualType ASTContext::getComplexType(QualType T) {
905 // Unique pointers, to guarantee there is only one pointer of a particular
906 // structure.
907 llvm::FoldingSetNodeID ID;
908 ComplexType::Profile(ID, T);
909
910 void *InsertPos = 0;
911 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
912 return QualType(CT, 0);
913
914 // If the pointee type isn't canonical, this won't be a canonical type either,
915 // so fill in the canonical type field.
916 QualType Canonical;
917 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000918 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000919
920 // Get the new insert position for the node we care about.
921 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000922 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 }
Steve Narofff83820b2009-01-27 22:08:43 +0000924 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000925 Types.push_back(New);
926 ComplexTypes.InsertNode(New, InsertPos);
927 return QualType(New, 0);
928}
929
Eli Friedmanf98aba32009-02-13 02:31:07 +0000930QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
931 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
932 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
933 FixedWidthIntType *&Entry = Map[Width];
934 if (!Entry)
935 Entry = new FixedWidthIntType(Width, Signed);
936 return QualType(Entry, 0);
937}
Reid Spencer5f016e22007-07-11 17:01:13 +0000938
939/// getPointerType - Return the uniqued reference to the type for a pointer to
940/// the specified type.
941QualType ASTContext::getPointerType(QualType T) {
942 // Unique pointers, to guarantee there is only one pointer of a particular
943 // structure.
944 llvm::FoldingSetNodeID ID;
945 PointerType::Profile(ID, T);
946
947 void *InsertPos = 0;
948 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
949 return QualType(PT, 0);
950
951 // If the pointee type isn't canonical, this won't be a canonical type either,
952 // so fill in the canonical type field.
953 QualType Canonical;
954 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000955 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000956
957 // Get the new insert position for the node we care about.
958 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000959 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000960 }
Steve Narofff83820b2009-01-27 22:08:43 +0000961 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 Types.push_back(New);
963 PointerTypes.InsertNode(New, InsertPos);
964 return QualType(New, 0);
965}
966
Steve Naroff5618bd42008-08-27 16:04:49 +0000967/// getBlockPointerType - Return the uniqued reference to the type for
968/// a pointer to the specified block.
969QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000970 assert(T->isFunctionType() && "block of function types only");
971 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000972 // structure.
973 llvm::FoldingSetNodeID ID;
974 BlockPointerType::Profile(ID, T);
975
976 void *InsertPos = 0;
977 if (BlockPointerType *PT =
978 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
979 return QualType(PT, 0);
980
Steve Naroff296e8d52008-08-28 19:20:44 +0000981 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000982 // type either so fill in the canonical type field.
983 QualType Canonical;
984 if (!T->isCanonical()) {
985 Canonical = getBlockPointerType(getCanonicalType(T));
986
987 // Get the new insert position for the node we care about.
988 BlockPointerType *NewIP =
989 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000990 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000991 }
Steve Narofff83820b2009-01-27 22:08:43 +0000992 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000993 Types.push_back(New);
994 BlockPointerTypes.InsertNode(New, InsertPos);
995 return QualType(New, 0);
996}
997
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000998/// getLValueReferenceType - Return the uniqued reference to the type for an
999/// lvalue reference to the specified type.
1000QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001001 // Unique pointers, to guarantee there is only one pointer of a particular
1002 // structure.
1003 llvm::FoldingSetNodeID ID;
1004 ReferenceType::Profile(ID, T);
1005
1006 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001007 if (LValueReferenceType *RT =
1008 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001010
Reid Spencer5f016e22007-07-11 17:01:13 +00001011 // If the referencee type isn't canonical, this won't be a canonical type
1012 // either, so fill in the canonical type field.
1013 QualType Canonical;
1014 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001015 Canonical = getLValueReferenceType(getCanonicalType(T));
1016
Reid Spencer5f016e22007-07-11 17:01:13 +00001017 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001018 LValueReferenceType *NewIP =
1019 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001020 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001021 }
1022
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001023 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001024 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001025 LValueReferenceTypes.InsertNode(New, InsertPos);
1026 return QualType(New, 0);
1027}
1028
1029/// getRValueReferenceType - Return the uniqued reference to the type for an
1030/// rvalue reference to the specified type.
1031QualType ASTContext::getRValueReferenceType(QualType T) {
1032 // Unique pointers, to guarantee there is only one pointer of a particular
1033 // structure.
1034 llvm::FoldingSetNodeID ID;
1035 ReferenceType::Profile(ID, T);
1036
1037 void *InsertPos = 0;
1038 if (RValueReferenceType *RT =
1039 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1040 return QualType(RT, 0);
1041
1042 // If the referencee type isn't canonical, this won't be a canonical type
1043 // either, so fill in the canonical type field.
1044 QualType Canonical;
1045 if (!T->isCanonical()) {
1046 Canonical = getRValueReferenceType(getCanonicalType(T));
1047
1048 // Get the new insert position for the node we care about.
1049 RValueReferenceType *NewIP =
1050 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1051 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1052 }
1053
1054 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1055 Types.push_back(New);
1056 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001057 return QualType(New, 0);
1058}
1059
Sebastian Redlf30208a2009-01-24 21:16:55 +00001060/// getMemberPointerType - Return the uniqued reference to the type for a
1061/// member pointer to the specified type, in the specified class.
1062QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1063{
1064 // Unique pointers, to guarantee there is only one pointer of a particular
1065 // structure.
1066 llvm::FoldingSetNodeID ID;
1067 MemberPointerType::Profile(ID, T, Cls);
1068
1069 void *InsertPos = 0;
1070 if (MemberPointerType *PT =
1071 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1072 return QualType(PT, 0);
1073
1074 // If the pointee or class type isn't canonical, this won't be a canonical
1075 // type either, so fill in the canonical type field.
1076 QualType Canonical;
1077 if (!T->isCanonical()) {
1078 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1079
1080 // Get the new insert position for the node we care about.
1081 MemberPointerType *NewIP =
1082 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1083 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1084 }
Steve Narofff83820b2009-01-27 22:08:43 +00001085 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001086 Types.push_back(New);
1087 MemberPointerTypes.InsertNode(New, InsertPos);
1088 return QualType(New, 0);
1089}
1090
Steve Narofffb22d962007-08-30 01:06:46 +00001091/// getConstantArrayType - Return the unique reference to the type for an
1092/// array of the specified element type.
1093QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +00001094 const llvm::APInt &ArySize,
1095 ArrayType::ArraySizeModifier ASM,
1096 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001097 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001098 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001099
1100 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001101 if (ConstantArrayType *ATP =
1102 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001103 return QualType(ATP, 0);
1104
1105 // If the element type isn't canonical, this won't be a canonical type either,
1106 // so fill in the canonical type field.
1107 QualType Canonical;
1108 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001109 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001110 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001111 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001112 ConstantArrayType *NewIP =
1113 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001114 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001115 }
1116
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001117 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001118 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001119 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001120 Types.push_back(New);
1121 return QualType(New, 0);
1122}
1123
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001124/// getVariableArrayType - Returns a non-unique reference to the type for a
1125/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001126QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1127 ArrayType::ArraySizeModifier ASM,
1128 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001129 // Since we don't unique expressions, it isn't possible to unique VLA's
1130 // that have an expression provided for their size.
1131
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001132 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001133 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001134
1135 VariableArrayTypes.push_back(New);
1136 Types.push_back(New);
1137 return QualType(New, 0);
1138}
1139
Douglas Gregor898574e2008-12-05 23:32:09 +00001140/// getDependentSizedArrayType - Returns a non-unique reference to
1141/// the type for a dependently-sized array of the specified element
1142/// type. FIXME: We will need these to be uniqued, or at least
1143/// comparable, at some point.
1144QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1145 ArrayType::ArraySizeModifier ASM,
1146 unsigned EltTypeQuals) {
1147 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1148 "Size must be type- or value-dependent!");
1149
1150 // Since we don't unique expressions, it isn't possible to unique
1151 // dependently-sized array types.
1152
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001153 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001154 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1155 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001156
1157 DependentSizedArrayTypes.push_back(New);
1158 Types.push_back(New);
1159 return QualType(New, 0);
1160}
1161
Eli Friedmanc5773c42008-02-15 18:16:39 +00001162QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1163 ArrayType::ArraySizeModifier ASM,
1164 unsigned EltTypeQuals) {
1165 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001166 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001167
1168 void *InsertPos = 0;
1169 if (IncompleteArrayType *ATP =
1170 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1171 return QualType(ATP, 0);
1172
1173 // If the element type isn't canonical, this won't be a canonical type
1174 // either, so fill in the canonical type field.
1175 QualType Canonical;
1176
1177 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001178 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001179 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001180
1181 // Get the new insert position for the node we care about.
1182 IncompleteArrayType *NewIP =
1183 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001184 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001185 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001186
Steve Narofff83820b2009-01-27 22:08:43 +00001187 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001188 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001189
1190 IncompleteArrayTypes.InsertNode(New, InsertPos);
1191 Types.push_back(New);
1192 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001193}
1194
Steve Naroff73322922007-07-18 18:00:27 +00001195/// getVectorType - Return the unique reference to a vector type of
1196/// the specified element type and size. VectorType must be a built-in type.
1197QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 BuiltinType *baseType;
1199
Chris Lattnerf52ab252008-04-06 22:59:24 +00001200 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001201 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001202
1203 // Check if we've already instantiated a vector of this type.
1204 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001205 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 void *InsertPos = 0;
1207 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1208 return QualType(VTP, 0);
1209
1210 // If the element type isn't canonical, this won't be a canonical type either,
1211 // so fill in the canonical type field.
1212 QualType Canonical;
1213 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001214 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001215
1216 // Get the new insert position for the node we care about.
1217 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001218 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 }
Steve Narofff83820b2009-01-27 22:08:43 +00001220 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001221 VectorTypes.InsertNode(New, InsertPos);
1222 Types.push_back(New);
1223 return QualType(New, 0);
1224}
1225
Nate Begeman213541a2008-04-18 23:10:10 +00001226/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001227/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001228QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001229 BuiltinType *baseType;
1230
Chris Lattnerf52ab252008-04-06 22:59:24 +00001231 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001232 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001233
1234 // Check if we've already instantiated a vector of this type.
1235 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001236 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001237 void *InsertPos = 0;
1238 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1239 return QualType(VTP, 0);
1240
1241 // If the element type isn't canonical, this won't be a canonical type either,
1242 // so fill in the canonical type field.
1243 QualType Canonical;
1244 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001245 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001246
1247 // Get the new insert position for the node we care about.
1248 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001249 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001250 }
Steve Narofff83820b2009-01-27 22:08:43 +00001251 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001252 VectorTypes.InsertNode(New, InsertPos);
1253 Types.push_back(New);
1254 return QualType(New, 0);
1255}
1256
Douglas Gregor72564e72009-02-26 23:50:07 +00001257/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001258///
Douglas Gregor72564e72009-02-26 23:50:07 +00001259QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001260 // Unique functions, to guarantee there is only one function of a particular
1261 // structure.
1262 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001263 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001264
1265 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001266 if (FunctionNoProtoType *FT =
1267 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001268 return QualType(FT, 0);
1269
1270 QualType Canonical;
1271 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001272 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001273
1274 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001275 FunctionNoProtoType *NewIP =
1276 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001277 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001278 }
1279
Douglas Gregor72564e72009-02-26 23:50:07 +00001280 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001281 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001282 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 return QualType(New, 0);
1284}
1285
1286/// getFunctionType - Return a normal function type with a typed argument
1287/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001288QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001289 unsigned NumArgs, bool isVariadic,
1290 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001291 // Unique functions, to guarantee there is only one function of a particular
1292 // structure.
1293 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001294 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001295 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001296
1297 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001298 if (FunctionProtoType *FTP =
1299 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001300 return QualType(FTP, 0);
1301
1302 // Determine whether the type being created is already canonical or not.
1303 bool isCanonical = ResultTy->isCanonical();
1304 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1305 if (!ArgArray[i]->isCanonical())
1306 isCanonical = false;
1307
1308 // If this type isn't canonical, get the canonical version of it.
1309 QualType Canonical;
1310 if (!isCanonical) {
1311 llvm::SmallVector<QualType, 16> CanonicalArgs;
1312 CanonicalArgs.reserve(NumArgs);
1313 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001314 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001315
Chris Lattnerf52ab252008-04-06 22:59:24 +00001316 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001317 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001318 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001319
1320 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001321 FunctionProtoType *NewIP =
1322 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001323 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001324 }
1325
Douglas Gregor72564e72009-02-26 23:50:07 +00001326 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001327 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001328 FunctionProtoType *FTP =
1329 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001330 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001331 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001332 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001333 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001334 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 return QualType(FTP, 0);
1336}
1337
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001338/// getTypeDeclType - Return the unique reference to the type for the
1339/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001340QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001341 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001342 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1343
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001344 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001345 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001346 else if (isa<TemplateTypeParmDecl>(Decl)) {
1347 assert(false && "Template type parameter types are always available.");
1348 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001349 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001350
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001351 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001352 if (PrevDecl)
1353 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001354 else
1355 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001356 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001357 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1358 if (PrevDecl)
1359 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001360 else
1361 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001362 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001363 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001364 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001365
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001366 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001367 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001368}
1369
Reid Spencer5f016e22007-07-11 17:01:13 +00001370/// getTypedefType - Return the unique reference to the type for the
1371/// specified typename decl.
1372QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1373 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1374
Chris Lattnerf52ab252008-04-06 22:59:24 +00001375 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001376 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001377 Types.push_back(Decl->TypeForDecl);
1378 return QualType(Decl->TypeForDecl, 0);
1379}
1380
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001381/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001382/// specified ObjC interface decl.
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001383QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001384 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1385
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001386 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1387 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff3536b442007-09-06 21:24:23 +00001388 Types.push_back(Decl->TypeForDecl);
1389 return QualType(Decl->TypeForDecl, 0);
1390}
1391
Douglas Gregorfab9d672009-02-05 23:33:38 +00001392/// \brief Retrieve the template type parameter type for a template
1393/// parameter with the given depth, index, and (optionally) name.
1394QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1395 IdentifierInfo *Name) {
1396 llvm::FoldingSetNodeID ID;
1397 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1398 void *InsertPos = 0;
1399 TemplateTypeParmType *TypeParm
1400 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1401
1402 if (TypeParm)
1403 return QualType(TypeParm, 0);
1404
1405 if (Name)
1406 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1407 getTemplateTypeParmType(Depth, Index));
1408 else
1409 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1410
1411 Types.push_back(TypeParm);
1412 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1413
1414 return QualType(TypeParm, 0);
1415}
1416
Douglas Gregor55f6b142009-02-09 18:46:07 +00001417QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001418ASTContext::getTemplateSpecializationType(TemplateName Template,
1419 const TemplateArgument *Args,
1420 unsigned NumArgs,
1421 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001422 if (!Canon.isNull())
1423 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001424
Douglas Gregor55f6b142009-02-09 18:46:07 +00001425 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001426 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001427
Douglas Gregor55f6b142009-02-09 18:46:07 +00001428 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001429 TemplateSpecializationType *Spec
1430 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001431
1432 if (Spec)
1433 return QualType(Spec, 0);
1434
Douglas Gregor7532dc62009-03-30 22:58:21 +00001435 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001436 sizeof(TemplateArgument) * NumArgs),
1437 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001438 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001439 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001440 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001441
1442 return QualType(Spec, 0);
1443}
1444
Douglas Gregore4e5b052009-03-19 00:18:19 +00001445QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001446ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001447 QualType NamedType) {
1448 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001449 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001450
1451 void *InsertPos = 0;
1452 QualifiedNameType *T
1453 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1454 if (T)
1455 return QualType(T, 0);
1456
Douglas Gregorab452ba2009-03-26 23:50:42 +00001457 T = new (*this) QualifiedNameType(NNS, NamedType,
1458 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001459 Types.push_back(T);
1460 QualifiedNameTypes.InsertNode(T, InsertPos);
1461 return QualType(T, 0);
1462}
1463
Douglas Gregord57959a2009-03-27 23:10:48 +00001464QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1465 const IdentifierInfo *Name,
1466 QualType Canon) {
1467 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1468
1469 if (Canon.isNull()) {
1470 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1471 if (CanonNNS != NNS)
1472 Canon = getTypenameType(CanonNNS, Name);
1473 }
1474
1475 llvm::FoldingSetNodeID ID;
1476 TypenameType::Profile(ID, NNS, Name);
1477
1478 void *InsertPos = 0;
1479 TypenameType *T
1480 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1481 if (T)
1482 return QualType(T, 0);
1483
1484 T = new (*this) TypenameType(NNS, Name, Canon);
1485 Types.push_back(T);
1486 TypenameTypes.InsertNode(T, InsertPos);
1487 return QualType(T, 0);
1488}
1489
Douglas Gregor17343172009-04-01 00:28:59 +00001490QualType
1491ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1492 const TemplateSpecializationType *TemplateId,
1493 QualType Canon) {
1494 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1495
1496 if (Canon.isNull()) {
1497 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1498 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1499 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1500 const TemplateSpecializationType *CanonTemplateId
1501 = CanonType->getAsTemplateSpecializationType();
1502 assert(CanonTemplateId &&
1503 "Canonical type must also be a template specialization type");
1504 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1505 }
1506 }
1507
1508 llvm::FoldingSetNodeID ID;
1509 TypenameType::Profile(ID, NNS, TemplateId);
1510
1511 void *InsertPos = 0;
1512 TypenameType *T
1513 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1514 if (T)
1515 return QualType(T, 0);
1516
1517 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1518 Types.push_back(T);
1519 TypenameTypes.InsertNode(T, InsertPos);
1520 return QualType(T, 0);
1521}
1522
Chris Lattner88cb27a2008-04-07 04:56:42 +00001523/// CmpProtocolNames - Comparison predicate for sorting protocols
1524/// alphabetically.
1525static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1526 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001527 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001528}
1529
1530static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1531 unsigned &NumProtocols) {
1532 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1533
1534 // Sort protocols, keyed by name.
1535 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1536
1537 // Remove duplicates.
1538 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1539 NumProtocols = ProtocolsEnd-Protocols;
1540}
1541
1542
Chris Lattner065f0d72008-04-07 04:44:08 +00001543/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1544/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001545QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1546 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001547 // Sort the protocol list alphabetically to canonicalize it.
1548 SortAndUniqueProtocols(Protocols, NumProtocols);
1549
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001550 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001551 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001552
1553 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001554 if (ObjCQualifiedInterfaceType *QT =
1555 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001556 return QualType(QT, 0);
1557
1558 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001559 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001560 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001561
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001562 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001563 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001564 return QualType(QType, 0);
1565}
1566
Chris Lattner88cb27a2008-04-07 04:56:42 +00001567/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1568/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001569QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001570 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001571 // Sort the protocol list alphabetically to canonicalize it.
1572 SortAndUniqueProtocols(Protocols, NumProtocols);
1573
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001574 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001575 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001576
1577 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001578 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001579 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001580 return QualType(QT, 0);
1581
1582 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001583 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001584 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001585 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001586 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001587 return QualType(QType, 0);
1588}
1589
Douglas Gregor72564e72009-02-26 23:50:07 +00001590/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1591/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001592/// multiple declarations that refer to "typeof(x)" all contain different
1593/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1594/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001595QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001596 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001597 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001598 Types.push_back(toe);
1599 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001600}
1601
Steve Naroff9752f252007-08-01 18:02:17 +00001602/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1603/// TypeOfType AST's. The only motivation to unique these nodes would be
1604/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1605/// an issue. This doesn't effect the type checker, since it operates
1606/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001607QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001608 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001609 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001610 Types.push_back(tot);
1611 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001612}
1613
Reid Spencer5f016e22007-07-11 17:01:13 +00001614/// getTagDeclType - Return the unique reference to the type for the
1615/// specified TagDecl (struct/union/class/enum) decl.
1616QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001617 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001618 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001619}
1620
1621/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1622/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1623/// needs to agree with the definition in <stddef.h>.
1624QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001625 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001626}
1627
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001628/// getSignedWCharType - Return the type of "signed wchar_t".
1629/// Used when in C++, as a GCC extension.
1630QualType ASTContext::getSignedWCharType() const {
1631 // FIXME: derive from "Target" ?
1632 return WCharTy;
1633}
1634
1635/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1636/// Used when in C++, as a GCC extension.
1637QualType ASTContext::getUnsignedWCharType() const {
1638 // FIXME: derive from "Target" ?
1639 return UnsignedIntTy;
1640}
1641
Chris Lattner8b9023b2007-07-13 03:05:23 +00001642/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1643/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1644QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001645 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001646}
1647
Chris Lattnere6327742008-04-02 05:18:44 +00001648//===----------------------------------------------------------------------===//
1649// Type Operators
1650//===----------------------------------------------------------------------===//
1651
Chris Lattner77c96472008-04-06 22:41:35 +00001652/// getCanonicalType - Return the canonical (structural) type corresponding to
1653/// the specified potentially non-canonical type. The non-canonical version
1654/// of a type may have many "decorated" versions of types. Decorators can
1655/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1656/// to be free of any of these, allowing two canonical types to be compared
1657/// for exact equality with a simple pointer comparison.
1658QualType ASTContext::getCanonicalType(QualType T) {
1659 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001660
1661 // If the result has type qualifiers, make sure to canonicalize them as well.
1662 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1663 if (TypeQuals == 0) return CanType;
1664
1665 // If the type qualifiers are on an array type, get the canonical type of the
1666 // array with the qualifiers applied to the element type.
1667 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1668 if (!AT)
1669 return CanType.getQualifiedType(TypeQuals);
1670
1671 // Get the canonical version of the element with the extra qualifiers on it.
1672 // This can recursively sink qualifiers through multiple levels of arrays.
1673 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1674 NewEltTy = getCanonicalType(NewEltTy);
1675
1676 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1677 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1678 CAT->getIndexTypeQualifier());
1679 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1680 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1681 IAT->getIndexTypeQualifier());
1682
Douglas Gregor898574e2008-12-05 23:32:09 +00001683 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1684 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1685 DSAT->getSizeModifier(),
1686 DSAT->getIndexTypeQualifier());
1687
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001688 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1689 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1690 VAT->getSizeModifier(),
1691 VAT->getIndexTypeQualifier());
1692}
1693
Douglas Gregord57959a2009-03-27 23:10:48 +00001694NestedNameSpecifier *
1695ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1696 if (!NNS)
1697 return 0;
1698
1699 switch (NNS->getKind()) {
1700 case NestedNameSpecifier::Identifier:
1701 // Canonicalize the prefix but keep the identifier the same.
1702 return NestedNameSpecifier::Create(*this,
1703 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1704 NNS->getAsIdentifier());
1705
1706 case NestedNameSpecifier::Namespace:
1707 // A namespace is canonical; build a nested-name-specifier with
1708 // this namespace and no prefix.
1709 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1710
1711 case NestedNameSpecifier::TypeSpec:
1712 case NestedNameSpecifier::TypeSpecWithTemplate: {
1713 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1714 NestedNameSpecifier *Prefix = 0;
1715
1716 // FIXME: This isn't the right check!
1717 if (T->isDependentType())
1718 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1719
1720 return NestedNameSpecifier::Create(*this, Prefix,
1721 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1722 T.getTypePtr());
1723 }
1724
1725 case NestedNameSpecifier::Global:
1726 // The global specifier is canonical and unique.
1727 return NNS;
1728 }
1729
1730 // Required to silence a GCC warning
1731 return 0;
1732}
1733
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001734
1735const ArrayType *ASTContext::getAsArrayType(QualType T) {
1736 // Handle the non-qualified case efficiently.
1737 if (T.getCVRQualifiers() == 0) {
1738 // Handle the common positive case fast.
1739 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1740 return AT;
1741 }
1742
1743 // Handle the common negative case fast, ignoring CVR qualifiers.
1744 QualType CType = T->getCanonicalTypeInternal();
1745
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001746 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001747 // test.
1748 if (!isa<ArrayType>(CType) &&
1749 !isa<ArrayType>(CType.getUnqualifiedType()))
1750 return 0;
1751
1752 // Apply any CVR qualifiers from the array type to the element type. This
1753 // implements C99 6.7.3p8: "If the specification of an array type includes
1754 // any type qualifiers, the element type is so qualified, not the array type."
1755
1756 // If we get here, we either have type qualifiers on the type, or we have
1757 // sugar such as a typedef in the way. If we have type qualifiers on the type
1758 // we must propagate them down into the elemeng type.
1759 unsigned CVRQuals = T.getCVRQualifiers();
1760 unsigned AddrSpace = 0;
1761 Type *Ty = T.getTypePtr();
1762
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001763 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001764 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001765 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1766 AddrSpace = EXTQT->getAddressSpace();
1767 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001768 } else {
1769 T = Ty->getDesugaredType();
1770 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1771 break;
1772 CVRQuals |= T.getCVRQualifiers();
1773 Ty = T.getTypePtr();
1774 }
1775 }
1776
1777 // If we have a simple case, just return now.
1778 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1779 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1780 return ATy;
1781
1782 // Otherwise, we have an array and we have qualifiers on it. Push the
1783 // qualifiers into the array element type and return a new array type.
1784 // Get the canonical version of the element with the extra qualifiers on it.
1785 // This can recursively sink qualifiers through multiple levels of arrays.
1786 QualType NewEltTy = ATy->getElementType();
1787 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001788 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001789 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1790
1791 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1792 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1793 CAT->getSizeModifier(),
1794 CAT->getIndexTypeQualifier()));
1795 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1796 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1797 IAT->getSizeModifier(),
1798 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001799
Douglas Gregor898574e2008-12-05 23:32:09 +00001800 if (const DependentSizedArrayType *DSAT
1801 = dyn_cast<DependentSizedArrayType>(ATy))
1802 return cast<ArrayType>(
1803 getDependentSizedArrayType(NewEltTy,
1804 DSAT->getSizeExpr(),
1805 DSAT->getSizeModifier(),
1806 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001807
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001808 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1809 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1810 VAT->getSizeModifier(),
1811 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001812}
1813
1814
Chris Lattnere6327742008-04-02 05:18:44 +00001815/// getArrayDecayedType - Return the properly qualified result of decaying the
1816/// specified array type to a pointer. This operation is non-trivial when
1817/// handling typedefs etc. The canonical type of "T" must be an array type,
1818/// this returns a pointer to a properly qualified element of the array.
1819///
1820/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1821QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001822 // Get the element type with 'getAsArrayType' so that we don't lose any
1823 // typedefs in the element type of the array. This also handles propagation
1824 // of type qualifiers from the array type into the element type if present
1825 // (C99 6.7.3p8).
1826 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1827 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001828
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001829 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001830
1831 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001832 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001833}
1834
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001835QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001836 QualType ElemTy = VAT->getElementType();
1837
1838 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1839 return getBaseElementType(VAT);
1840
1841 return ElemTy;
1842}
1843
Reid Spencer5f016e22007-07-11 17:01:13 +00001844/// getFloatingRank - Return a relative rank for floating point types.
1845/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001846static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001847 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001848 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001849
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001850 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001851 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001852 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001853 case BuiltinType::Float: return FloatRank;
1854 case BuiltinType::Double: return DoubleRank;
1855 case BuiltinType::LongDouble: return LongDoubleRank;
1856 }
1857}
1858
Steve Naroff716c7302007-08-27 01:41:48 +00001859/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1860/// point or a complex type (based on typeDomain/typeSize).
1861/// 'typeDomain' is a real floating point or complex type.
1862/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001863QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1864 QualType Domain) const {
1865 FloatingRank EltRank = getFloatingRank(Size);
1866 if (Domain->isComplexType()) {
1867 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001868 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001869 case FloatRank: return FloatComplexTy;
1870 case DoubleRank: return DoubleComplexTy;
1871 case LongDoubleRank: return LongDoubleComplexTy;
1872 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001873 }
Chris Lattner1361b112008-04-06 23:58:54 +00001874
1875 assert(Domain->isRealFloatingType() && "Unknown domain!");
1876 switch (EltRank) {
1877 default: assert(0 && "getFloatingRank(): illegal value for rank");
1878 case FloatRank: return FloatTy;
1879 case DoubleRank: return DoubleTy;
1880 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001881 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001882}
1883
Chris Lattner7cfeb082008-04-06 23:55:33 +00001884/// getFloatingTypeOrder - Compare the rank of the two specified floating
1885/// point types, ignoring the domain of the type (i.e. 'double' ==
1886/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1887/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001888int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1889 FloatingRank LHSR = getFloatingRank(LHS);
1890 FloatingRank RHSR = getFloatingRank(RHS);
1891
1892 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001893 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001894 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001895 return 1;
1896 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001897}
1898
Chris Lattnerf52ab252008-04-06 22:59:24 +00001899/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1900/// routine will assert if passed a built-in type that isn't an integer or enum,
1901/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001902unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001903 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001904 if (EnumType* ET = dyn_cast<EnumType>(T))
1905 T = ET->getDecl()->getIntegerType().getTypePtr();
1906
1907 // There are two things which impact the integer rank: the width, and
1908 // the ordering of builtins. The builtin ordering is encoded in the
1909 // bottom three bits; the width is encoded in the bits above that.
1910 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1911 return FWIT->getWidth() << 3;
1912 }
1913
Chris Lattnerf52ab252008-04-06 22:59:24 +00001914 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001915 default: assert(0 && "getIntegerRank(): not a built-in integer");
1916 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001917 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001918 case BuiltinType::Char_S:
1919 case BuiltinType::Char_U:
1920 case BuiltinType::SChar:
1921 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001922 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001923 case BuiltinType::Short:
1924 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001925 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001926 case BuiltinType::Int:
1927 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001928 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001929 case BuiltinType::Long:
1930 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001931 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001932 case BuiltinType::LongLong:
1933 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001934 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00001935 case BuiltinType::Int128:
1936 case BuiltinType::UInt128:
1937 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001938 }
1939}
1940
Chris Lattner7cfeb082008-04-06 23:55:33 +00001941/// getIntegerTypeOrder - Returns the highest ranked integer type:
1942/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1943/// LHS < RHS, return -1.
1944int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001945 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1946 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001947 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001948
Chris Lattnerf52ab252008-04-06 22:59:24 +00001949 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1950 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001951
Chris Lattner7cfeb082008-04-06 23:55:33 +00001952 unsigned LHSRank = getIntegerRank(LHSC);
1953 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001954
Chris Lattner7cfeb082008-04-06 23:55:33 +00001955 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1956 if (LHSRank == RHSRank) return 0;
1957 return LHSRank > RHSRank ? 1 : -1;
1958 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001959
Chris Lattner7cfeb082008-04-06 23:55:33 +00001960 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1961 if (LHSUnsigned) {
1962 // If the unsigned [LHS] type is larger, return it.
1963 if (LHSRank >= RHSRank)
1964 return 1;
1965
1966 // If the signed type can represent all values of the unsigned type, it
1967 // wins. Because we are dealing with 2's complement and types that are
1968 // powers of two larger than each other, this is always safe.
1969 return -1;
1970 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001971
Chris Lattner7cfeb082008-04-06 23:55:33 +00001972 // If the unsigned [RHS] type is larger, return it.
1973 if (RHSRank >= LHSRank)
1974 return -1;
1975
1976 // If the signed type can represent all values of the unsigned type, it
1977 // wins. Because we are dealing with 2's complement and types that are
1978 // powers of two larger than each other, this is always safe.
1979 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001980}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001981
1982// getCFConstantStringType - Return the type used for constant CFStrings.
1983QualType ASTContext::getCFConstantStringType() {
1984 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001985 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001986 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001987 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001988 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001989
1990 // const int *isa;
1991 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001992 // int flags;
1993 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001994 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001995 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001996 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001997 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001998
Anders Carlsson71993dd2007-08-17 05:31:46 +00001999 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002000 for (unsigned i = 0; i < 4; ++i) {
2001 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2002 SourceLocation(), 0,
2003 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002004 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002005 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002006 }
2007
2008 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002009 }
2010
2011 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002012}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002013
Douglas Gregor319ac892009-04-23 22:29:11 +00002014void ASTContext::setCFConstantStringType(QualType T) {
2015 const RecordType *Rec = T->getAsRecordType();
2016 assert(Rec && "Invalid CFConstantStringType");
2017 CFConstantStringTypeDecl = Rec->getDecl();
2018}
2019
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002020QualType ASTContext::getObjCFastEnumerationStateType()
2021{
2022 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002023 ObjCFastEnumerationStateTypeDecl =
2024 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2025 &Idents.get("__objcFastEnumerationState"));
2026
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002027 QualType FieldTypes[] = {
2028 UnsignedLongTy,
2029 getPointerType(ObjCIdType),
2030 getPointerType(UnsignedLongTy),
2031 getConstantArrayType(UnsignedLongTy,
2032 llvm::APInt(32, 5), ArrayType::Normal, 0)
2033 };
2034
Douglas Gregor44b43212008-12-11 16:49:14 +00002035 for (size_t i = 0; i < 4; ++i) {
2036 FieldDecl *Field = FieldDecl::Create(*this,
2037 ObjCFastEnumerationStateTypeDecl,
2038 SourceLocation(), 0,
2039 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002040 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002041 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002042 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002043
Douglas Gregor44b43212008-12-11 16:49:14 +00002044 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002045 }
2046
2047 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2048}
2049
Douglas Gregor319ac892009-04-23 22:29:11 +00002050void ASTContext::setObjCFastEnumerationStateType(QualType T) {
2051 const RecordType *Rec = T->getAsRecordType();
2052 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2053 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2054}
2055
Anders Carlssone8c49532007-10-29 06:33:42 +00002056// This returns true if a type has been typedefed to BOOL:
2057// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002058static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002059 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002060 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2061 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002062
2063 return false;
2064}
2065
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002066/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002067/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002068int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002069 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002070
2071 // Make all integer and enum types at least as large as an int
2072 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002073 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002074 // Treat arrays as pointers, since that's how they're passed in.
2075 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002076 sz = getTypeSize(VoidPtrTy);
2077 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002078}
2079
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002080/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002081/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002082void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002083 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002084 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002085 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002086 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002087 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002088 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002089 // Compute size of all parameters.
2090 // Start with computing size of a pointer in number of bytes.
2091 // FIXME: There might(should) be a better way of doing this computation!
2092 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002093 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002094 // The first two arguments (self and _cmd) are pointers; account for
2095 // their size.
2096 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002097 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2098 E = Decl->param_end(); PI != E; ++PI) {
2099 QualType PType = (*PI)->getType();
2100 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002101 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002102 ParmOffset += sz;
2103 }
2104 S += llvm::utostr(ParmOffset);
2105 S += "@0:";
2106 S += llvm::utostr(PtrSize);
2107
2108 // Argument types.
2109 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002110 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2111 E = Decl->param_end(); PI != E; ++PI) {
2112 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002113 QualType PType = PVDecl->getOriginalType();
2114 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002115 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2116 // Use array's original type only if it has known number of
2117 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002118 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002119 PType = PVDecl->getType();
2120 } else if (PType->isFunctionType())
2121 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002122 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002123 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002124 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002125 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002126 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002127 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002128 }
2129}
2130
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002131/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002132/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002133/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2134/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002135/// Property attributes are stored as a comma-delimited C string. The simple
2136/// attributes readonly and bycopy are encoded as single characters. The
2137/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2138/// encoded as single characters, followed by an identifier. Property types
2139/// are also encoded as a parametrized attribute. The characters used to encode
2140/// these attributes are defined by the following enumeration:
2141/// @code
2142/// enum PropertyAttributes {
2143/// kPropertyReadOnly = 'R', // property is read-only.
2144/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2145/// kPropertyByref = '&', // property is a reference to the value last assigned
2146/// kPropertyDynamic = 'D', // property is dynamic
2147/// kPropertyGetter = 'G', // followed by getter selector name
2148/// kPropertySetter = 'S', // followed by setter selector name
2149/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2150/// kPropertyType = 't' // followed by old-style type encoding.
2151/// kPropertyWeak = 'W' // 'weak' property
2152/// kPropertyStrong = 'P' // property GC'able
2153/// kPropertyNonAtomic = 'N' // property non-atomic
2154/// };
2155/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002156void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2157 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002158 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002159 // Collect information from the property implementation decl(s).
2160 bool Dynamic = false;
2161 ObjCPropertyImplDecl *SynthesizePID = 0;
2162
2163 // FIXME: Duplicated code due to poor abstraction.
2164 if (Container) {
2165 if (const ObjCCategoryImplDecl *CID =
2166 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2167 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002168 i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2169 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002170 ObjCPropertyImplDecl *PID = *i;
2171 if (PID->getPropertyDecl() == PD) {
2172 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2173 Dynamic = true;
2174 } else {
2175 SynthesizePID = PID;
2176 }
2177 }
2178 }
2179 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002180 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002181 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002182 i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2183 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002184 ObjCPropertyImplDecl *PID = *i;
2185 if (PID->getPropertyDecl() == PD) {
2186 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2187 Dynamic = true;
2188 } else {
2189 SynthesizePID = PID;
2190 }
2191 }
2192 }
2193 }
2194 }
2195
2196 // FIXME: This is not very efficient.
2197 S = "T";
2198
2199 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002200 // GCC has some special rules regarding encoding of properties which
2201 // closely resembles encoding of ivars.
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002202 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002203 true /* outermost type */,
2204 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002205
2206 if (PD->isReadOnly()) {
2207 S += ",R";
2208 } else {
2209 switch (PD->getSetterKind()) {
2210 case ObjCPropertyDecl::Assign: break;
2211 case ObjCPropertyDecl::Copy: S += ",C"; break;
2212 case ObjCPropertyDecl::Retain: S += ",&"; break;
2213 }
2214 }
2215
2216 // It really isn't clear at all what this means, since properties
2217 // are "dynamic by default".
2218 if (Dynamic)
2219 S += ",D";
2220
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002221 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2222 S += ",N";
2223
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002224 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2225 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002226 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002227 }
2228
2229 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2230 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002231 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002232 }
2233
2234 if (SynthesizePID) {
2235 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2236 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002237 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002238 }
2239
2240 // FIXME: OBJCGC: weak & strong
2241}
2242
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002243/// getLegacyIntegralTypeEncoding -
2244/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002245/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002246/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2247///
2248void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2249 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2250 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002251 if (BT->getKind() == BuiltinType::ULong &&
2252 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002253 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002254 else
2255 if (BT->getKind() == BuiltinType::Long &&
2256 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002257 PointeeTy = IntTy;
2258 }
2259 }
2260}
2261
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002262void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002263 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002264 // We follow the behavior of gcc, expanding structures which are
2265 // directly pointed to, and expanding embedded structures. Note that
2266 // these rules are sufficient to prevent recursive encoding of the
2267 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002268 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2269 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002270}
2271
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002272static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002273 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002274 const Expr *E = FD->getBitWidth();
2275 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2276 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002277 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002278 S += 'b';
2279 S += llvm::utostr(N);
2280}
2281
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002282void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2283 bool ExpandPointedToStructures,
2284 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002285 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002286 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002287 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002288 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002289 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002290 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002291 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002292 else {
2293 char encoding;
2294 switch (BT->getKind()) {
2295 default: assert(0 && "Unhandled builtin type kind");
2296 case BuiltinType::Void: encoding = 'v'; break;
2297 case BuiltinType::Bool: encoding = 'B'; break;
2298 case BuiltinType::Char_U:
2299 case BuiltinType::UChar: encoding = 'C'; break;
2300 case BuiltinType::UShort: encoding = 'S'; break;
2301 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002302 case BuiltinType::ULong:
2303 encoding =
2304 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2305 break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002306 case BuiltinType::UInt128: encoding = 'T'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002307 case BuiltinType::ULongLong: encoding = 'Q'; break;
2308 case BuiltinType::Char_S:
2309 case BuiltinType::SChar: encoding = 'c'; break;
2310 case BuiltinType::Short: encoding = 's'; break;
2311 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002312 case BuiltinType::Long:
2313 encoding =
2314 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2315 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002316 case BuiltinType::LongLong: encoding = 'q'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002317 case BuiltinType::Int128: encoding = 't'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002318 case BuiltinType::Float: encoding = 'f'; break;
2319 case BuiltinType::Double: encoding = 'd'; break;
2320 case BuiltinType::LongDouble: encoding = 'd'; break;
2321 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002322
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002323 S += encoding;
2324 }
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002325 } else if (const ComplexType *CT = T->getAsComplexType()) {
2326 S += 'j';
2327 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2328 false);
2329 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002330 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2331 ExpandPointedToStructures,
2332 ExpandStructures, FD);
2333 if (FD || EncodingProperty) {
2334 // Note that we do extended encoding of protocol qualifer list
2335 // Only when doing ivar or property encoding.
2336 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2337 S += '"';
2338 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2339 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2340 S += '<';
2341 S += Proto->getNameAsString();
2342 S += '>';
2343 }
2344 S += '"';
2345 }
2346 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002347 }
2348 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002349 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002350 bool isReadOnly = false;
2351 // For historical/compatibility reasons, the read-only qualifier of the
2352 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2353 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2354 // Also, do not emit the 'r' for anything but the outermost type!
2355 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2356 if (OutermostType && T.isConstQualified()) {
2357 isReadOnly = true;
2358 S += 'r';
2359 }
2360 }
2361 else if (OutermostType) {
2362 QualType P = PointeeTy;
2363 while (P->getAsPointerType())
2364 P = P->getAsPointerType()->getPointeeType();
2365 if (P.isConstQualified()) {
2366 isReadOnly = true;
2367 S += 'r';
2368 }
2369 }
2370 if (isReadOnly) {
2371 // Another legacy compatibility encoding. Some ObjC qualifier and type
2372 // combinations need to be rearranged.
2373 // Rewrite "in const" from "nr" to "rn"
2374 const char * s = S.c_str();
2375 int len = S.length();
2376 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2377 std::string replace = "rn";
2378 S.replace(S.end()-2, S.end(), replace);
2379 }
2380 }
Steve Naroff389bf462009-02-12 17:52:19 +00002381 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002382 S += '@';
2383 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002384 }
2385 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002386 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002387 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002388 // Another historical/compatibility reason.
2389 // We encode the underlying type which comes out as
2390 // {...};
2391 S += '^';
2392 getObjCEncodingForTypeImpl(PointeeTy, S,
2393 false, ExpandPointedToStructures,
2394 NULL);
2395 return;
2396 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002397 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002398 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002399 const ObjCInterfaceType *OIT =
2400 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002401 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002402 S += '"';
2403 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002404 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2405 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2406 S += '<';
2407 S += Proto->getNameAsString();
2408 S += '>';
2409 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002410 S += '"';
2411 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002412 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002413 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002414 S += '#';
2415 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002416 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002417 S += ':';
2418 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002419 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002420
2421 if (PointeeTy->isCharType()) {
2422 // char pointer types should be encoded as '*' unless it is a
2423 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002424 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002425 S += '*';
2426 return;
2427 }
2428 }
2429
2430 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002431 getLegacyIntegralTypeEncoding(PointeeTy);
2432
2433 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002434 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002435 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002436 } else if (const ArrayType *AT =
2437 // Ignore type qualifiers etc.
2438 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002439 if (isa<IncompleteArrayType>(AT)) {
2440 // Incomplete arrays are encoded as a pointer to the array element.
2441 S += '^';
2442
2443 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2444 false, ExpandStructures, FD);
2445 } else {
2446 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002447
Anders Carlsson559a8332009-02-22 01:38:57 +00002448 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2449 S += llvm::utostr(CAT->getSize().getZExtValue());
2450 else {
2451 //Variable length arrays are encoded as a regular array with 0 elements.
2452 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2453 S += '0';
2454 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002455
Anders Carlsson559a8332009-02-22 01:38:57 +00002456 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2457 false, ExpandStructures, FD);
2458 S += ']';
2459 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002460 } else if (T->getAsFunctionType()) {
2461 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002462 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002463 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002464 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002465 // Anonymous structures print as '?'
2466 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2467 S += II->getName();
2468 } else {
2469 S += '?';
2470 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002471 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002472 S += '=';
Douglas Gregor6ab35242009-04-09 21:40:53 +00002473 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2474 FieldEnd = RDecl->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +00002475 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002476 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002477 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002478 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002479 S += '"';
2480 }
2481
2482 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002483 if (Field->isBitField()) {
2484 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2485 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002486 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002487 QualType qt = Field->getType();
2488 getLegacyIntegralTypeEncoding(qt);
2489 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002490 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002491 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002492 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002493 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002494 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002495 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002496 if (FD && FD->isBitField())
2497 EncodeBitField(this, S, FD);
2498 else
2499 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002500 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002501 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002502 } else if (T->isObjCInterfaceType()) {
2503 // @encode(class_name)
2504 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2505 S += '{';
2506 const IdentifierInfo *II = OI->getIdentifier();
2507 S += II->getName();
2508 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002509 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002510 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002511 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002512 if (RecFields[i]->isBitField())
2513 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2514 RecFields[i]);
2515 else
2516 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2517 FD);
2518 }
2519 S += '}';
2520 }
2521 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002522 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002523}
2524
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002525void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002526 std::string& S) const {
2527 if (QT & Decl::OBJC_TQ_In)
2528 S += 'n';
2529 if (QT & Decl::OBJC_TQ_Inout)
2530 S += 'N';
2531 if (QT & Decl::OBJC_TQ_Out)
2532 S += 'o';
2533 if (QT & Decl::OBJC_TQ_Bycopy)
2534 S += 'O';
2535 if (QT & Decl::OBJC_TQ_Byref)
2536 S += 'R';
2537 if (QT & Decl::OBJC_TQ_Oneway)
2538 S += 'V';
2539}
2540
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002541void ASTContext::setBuiltinVaListType(QualType T)
2542{
2543 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2544
2545 BuiltinVaListType = T;
2546}
2547
Douglas Gregor319ac892009-04-23 22:29:11 +00002548void ASTContext::setObjCIdType(QualType T)
Steve Naroff7e219e42007-10-15 14:41:52 +00002549{
Douglas Gregor319ac892009-04-23 22:29:11 +00002550 ObjCIdType = T;
2551
2552 const TypedefType *TT = T->getAsTypedefType();
2553 if (!TT)
2554 return;
2555
2556 TypedefDecl *TD = TT->getDecl();
Steve Naroff7e219e42007-10-15 14:41:52 +00002557
2558 // typedef struct objc_object *id;
2559 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002560 // User error - caller will issue diagnostics.
2561 if (!ptr)
2562 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002563 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002564 // User error - caller will issue diagnostics.
2565 if (!rec)
2566 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002567 IdStructType = rec;
2568}
2569
Douglas Gregor319ac892009-04-23 22:29:11 +00002570void ASTContext::setObjCSelType(QualType T)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002571{
Douglas Gregor319ac892009-04-23 22:29:11 +00002572 ObjCSelType = T;
2573
2574 const TypedefType *TT = T->getAsTypedefType();
2575 if (!TT)
2576 return;
2577 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002578
2579 // typedef struct objc_selector *SEL;
2580 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002581 if (!ptr)
2582 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002583 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002584 if (!rec)
2585 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002586 SelStructType = rec;
2587}
2588
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002589void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002590{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002591 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002592}
2593
Douglas Gregor319ac892009-04-23 22:29:11 +00002594void ASTContext::setObjCClassType(QualType T)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002595{
Douglas Gregor319ac892009-04-23 22:29:11 +00002596 ObjCClassType = T;
2597
2598 const TypedefType *TT = T->getAsTypedefType();
2599 if (!TT)
2600 return;
2601 TypedefDecl *TD = TT->getDecl();
Anders Carlsson8baaca52007-10-31 02:53:19 +00002602
2603 // typedef struct objc_class *Class;
2604 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2605 assert(ptr && "'Class' incorrectly typed");
2606 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2607 assert(rec && "'Class' incorrectly typed");
2608 ClassStructType = rec;
2609}
2610
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002611void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2612 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002613 "'NSConstantString' type already set!");
2614
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002615 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002616}
2617
Douglas Gregor7532dc62009-03-30 22:58:21 +00002618/// \brief Retrieve the template name that represents a qualified
2619/// template name such as \c std::vector.
2620TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2621 bool TemplateKeyword,
2622 TemplateDecl *Template) {
2623 llvm::FoldingSetNodeID ID;
2624 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2625
2626 void *InsertPos = 0;
2627 QualifiedTemplateName *QTN =
2628 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2629 if (!QTN) {
2630 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2631 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2632 }
2633
2634 return TemplateName(QTN);
2635}
2636
2637/// \brief Retrieve the template name that represents a dependent
2638/// template name such as \c MetaFun::template apply.
2639TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2640 const IdentifierInfo *Name) {
2641 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2642
2643 llvm::FoldingSetNodeID ID;
2644 DependentTemplateName::Profile(ID, NNS, Name);
2645
2646 void *InsertPos = 0;
2647 DependentTemplateName *QTN =
2648 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2649
2650 if (QTN)
2651 return TemplateName(QTN);
2652
2653 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2654 if (CanonNNS == NNS) {
2655 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2656 } else {
2657 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2658 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2659 }
2660
2661 DependentTemplateNames.InsertNode(QTN, InsertPos);
2662 return TemplateName(QTN);
2663}
2664
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002665/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002666/// TargetInfo, produce the corresponding type. The unsigned @p Type
2667/// is actually a value of type @c TargetInfo::IntType.
2668QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002669 switch (Type) {
2670 case TargetInfo::NoInt: return QualType();
2671 case TargetInfo::SignedShort: return ShortTy;
2672 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2673 case TargetInfo::SignedInt: return IntTy;
2674 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2675 case TargetInfo::SignedLong: return LongTy;
2676 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2677 case TargetInfo::SignedLongLong: return LongLongTy;
2678 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2679 }
2680
2681 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002682 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002683}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002684
2685//===----------------------------------------------------------------------===//
2686// Type Predicates.
2687//===----------------------------------------------------------------------===//
2688
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002689/// isObjCNSObjectType - Return true if this is an NSObject object using
2690/// NSObject attribute on a c-style pointer type.
2691/// FIXME - Make it work directly on types.
2692///
2693bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2694 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2695 if (TypedefDecl *TD = TDT->getDecl())
2696 if (TD->getAttr<ObjCNSObjectAttr>())
2697 return true;
2698 }
2699 return false;
2700}
2701
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002702/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2703/// to an object type. This includes "id" and "Class" (two 'special' pointers
2704/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2705/// ID type).
2706bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002707 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002708 return true;
2709
Steve Naroff6ae98502008-10-21 18:24:04 +00002710 // Blocks are objects.
2711 if (Ty->isBlockPointerType())
2712 return true;
2713
2714 // All other object types are pointers.
Chris Lattner16ede0e2009-04-12 23:51:02 +00002715 const PointerType *PT = Ty->getAsPointerType();
2716 if (PT == 0)
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002717 return false;
2718
Chris Lattner16ede0e2009-04-12 23:51:02 +00002719 // If this a pointer to an interface (e.g. NSString*), it is ok.
2720 if (PT->getPointeeType()->isObjCInterfaceType() ||
2721 // If is has NSObject attribute, OK as well.
2722 isObjCNSObjectType(Ty))
2723 return true;
2724
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002725 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2726 // pointer types. This looks for the typedef specifically, not for the
Chris Lattner16ede0e2009-04-12 23:51:02 +00002727 // underlying type. Iteratively strip off typedefs so that we can handle
2728 // typedefs of typedefs.
2729 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2730 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2731 Ty.getUnqualifiedType() == getObjCClassType())
2732 return true;
2733
2734 Ty = TDT->getDecl()->getUnderlyingType();
2735 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002736
Chris Lattner16ede0e2009-04-12 23:51:02 +00002737 return false;
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002738}
2739
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002740/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2741/// garbage collection attribute.
2742///
2743QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002744 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002745 if (getLangOptions().ObjC1 &&
2746 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002747 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002748 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002749 // (or pointers to them) be treated as though they were declared
2750 // as __strong.
2751 if (GCAttrs == QualType::GCNone) {
2752 if (isObjCObjectPointerType(Ty))
2753 GCAttrs = QualType::Strong;
2754 else if (Ty->isPointerType())
2755 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2756 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00002757 // Non-pointers have none gc'able attribute regardless of the attribute
2758 // set on them.
2759 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2760 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002761 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002762 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002763}
2764
Chris Lattner6ac46a42008-04-07 06:51:04 +00002765//===----------------------------------------------------------------------===//
2766// Type Compatibility Testing
2767//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002768
Steve Naroff1c7d0672008-09-04 15:10:53 +00002769/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002770/// block types. Types must be strictly compatible here. For example,
2771/// C unfortunately doesn't produce an error for the following:
2772///
2773/// int (*emptyArgFunc)();
2774/// int (*intArgList)(int) = emptyArgFunc;
2775///
2776/// For blocks, we will produce an error for the following (similar to C++):
2777///
2778/// int (^emptyArgBlock)();
2779/// int (^intArgBlock)(int) = emptyArgBlock;
2780///
2781/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2782///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002783bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002784 const FunctionType *lbase = lhs->getAsFunctionType();
2785 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002786 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2787 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stumpaab0f7a2009-04-01 01:17:39 +00002788 if (lproto && rproto == 0)
2789 return false;
2790 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff1c7d0672008-09-04 15:10:53 +00002791}
2792
Chris Lattner6ac46a42008-04-07 06:51:04 +00002793/// areCompatVectorTypes - Return true if the two specified vector types are
2794/// compatible.
2795static bool areCompatVectorTypes(const VectorType *LHS,
2796 const VectorType *RHS) {
2797 assert(LHS->isCanonical() && RHS->isCanonical());
2798 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002799 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002800}
2801
Eli Friedman3d815e72008-08-22 00:56:42 +00002802/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002803/// compatible for assignment from RHS to LHS. This handles validation of any
2804/// protocol qualifiers on the LHS or RHS.
2805///
Eli Friedman3d815e72008-08-22 00:56:42 +00002806bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2807 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002808 // Verify that the base decls are compatible: the RHS must be a subclass of
2809 // the LHS.
2810 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2811 return false;
2812
2813 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2814 // protocol qualified at all, then we are good.
2815 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2816 return true;
2817
2818 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2819 // isn't a superset.
2820 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2821 return true; // FIXME: should return false!
2822
2823 // Finally, we must have two protocol-qualified interfaces.
2824 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2825 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002826
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002827 // All LHS protocols must have a presence on the RHS.
2828 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002829
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002830 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2831 LHSPE = LHSP->qual_end();
2832 LHSPI != LHSPE; LHSPI++) {
2833 bool RHSImplementsProtocol = false;
2834
2835 // If the RHS doesn't implement the protocol on the left, the types
2836 // are incompatible.
2837 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2838 RHSPE = RHSP->qual_end();
2839 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2840 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2841 RHSImplementsProtocol = true;
2842 }
2843 // FIXME: For better diagnostics, consider passing back the protocol name.
2844 if (!RHSImplementsProtocol)
2845 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002846 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002847 // The RHS implements all protocols listed on the LHS.
2848 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002849}
2850
Steve Naroff389bf462009-02-12 17:52:19 +00002851bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2852 // get the "pointed to" types
2853 const PointerType *LHSPT = LHS->getAsPointerType();
2854 const PointerType *RHSPT = RHS->getAsPointerType();
2855
2856 if (!LHSPT || !RHSPT)
2857 return false;
2858
2859 QualType lhptee = LHSPT->getPointeeType();
2860 QualType rhptee = RHSPT->getPointeeType();
2861 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2862 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2863 // ID acts sort of like void* for ObjC interfaces
2864 if (LHSIface && isObjCIdStructType(rhptee))
2865 return true;
2866 if (RHSIface && isObjCIdStructType(lhptee))
2867 return true;
2868 if (!LHSIface || !RHSIface)
2869 return false;
2870 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2871 canAssignObjCInterfaces(RHSIface, LHSIface);
2872}
2873
Steve Naroffec0550f2007-10-15 20:41:53 +00002874/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2875/// both shall have the identically qualified version of a compatible type.
2876/// C99 6.2.7p1: Two types have compatible types if their types are the
2877/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002878bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2879 return !mergeTypes(LHS, RHS).isNull();
2880}
2881
2882QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2883 const FunctionType *lbase = lhs->getAsFunctionType();
2884 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002885 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2886 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002887 bool allLTypes = true;
2888 bool allRTypes = true;
2889
2890 // Check return type
2891 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2892 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002893 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2894 allLTypes = false;
2895 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2896 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002897
2898 if (lproto && rproto) { // two C99 style function prototypes
2899 unsigned lproto_nargs = lproto->getNumArgs();
2900 unsigned rproto_nargs = rproto->getNumArgs();
2901
2902 // Compatible functions must have the same number of arguments
2903 if (lproto_nargs != rproto_nargs)
2904 return QualType();
2905
2906 // Variadic and non-variadic functions aren't compatible
2907 if (lproto->isVariadic() != rproto->isVariadic())
2908 return QualType();
2909
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002910 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2911 return QualType();
2912
Eli Friedman3d815e72008-08-22 00:56:42 +00002913 // Check argument compatibility
2914 llvm::SmallVector<QualType, 10> types;
2915 for (unsigned i = 0; i < lproto_nargs; i++) {
2916 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2917 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2918 QualType argtype = mergeTypes(largtype, rargtype);
2919 if (argtype.isNull()) return QualType();
2920 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002921 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2922 allLTypes = false;
2923 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2924 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002925 }
2926 if (allLTypes) return lhs;
2927 if (allRTypes) return rhs;
2928 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002929 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002930 }
2931
2932 if (lproto) allRTypes = false;
2933 if (rproto) allLTypes = false;
2934
Douglas Gregor72564e72009-02-26 23:50:07 +00002935 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002936 if (proto) {
2937 if (proto->isVariadic()) return QualType();
2938 // Check that the types are compatible with the types that
2939 // would result from default argument promotions (C99 6.7.5.3p15).
2940 // The only types actually affected are promotable integer
2941 // types and floats, which would be passed as a different
2942 // type depending on whether the prototype is visible.
2943 unsigned proto_nargs = proto->getNumArgs();
2944 for (unsigned i = 0; i < proto_nargs; ++i) {
2945 QualType argTy = proto->getArgType(i);
2946 if (argTy->isPromotableIntegerType() ||
2947 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2948 return QualType();
2949 }
2950
2951 if (allLTypes) return lhs;
2952 if (allRTypes) return rhs;
2953 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002954 proto->getNumArgs(), lproto->isVariadic(),
2955 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002956 }
2957
2958 if (allLTypes) return lhs;
2959 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002960 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002961}
2962
2963QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002964 // C++ [expr]: If an expression initially has the type "reference to T", the
2965 // type is adjusted to "T" prior to any further analysis, the expression
2966 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002967 // expression is an lvalue unless the reference is an rvalue reference and
2968 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00002969 // FIXME: C++ shouldn't be going through here! The rules are different
2970 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002971 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2972 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00002973 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002974 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002975 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002976 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002977
Eli Friedman3d815e72008-08-22 00:56:42 +00002978 QualType LHSCan = getCanonicalType(LHS),
2979 RHSCan = getCanonicalType(RHS);
2980
2981 // If two types are identical, they are compatible.
2982 if (LHSCan == RHSCan)
2983 return LHS;
2984
2985 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002986 // Note that we handle extended qualifiers later, in the
2987 // case for ExtQualType.
2988 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00002989 return QualType();
2990
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00002991 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
2992 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00002993
Chris Lattner1adb8832008-01-14 05:45:46 +00002994 // We want to consider the two function types to be the same for these
2995 // comparisons, just force one to the other.
2996 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2997 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002998
2999 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003000 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3001 LHSClass = Type::ConstantArray;
3002 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3003 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00003004
Nate Begeman213541a2008-04-18 23:10:10 +00003005 // Canonicalize ExtVector -> Vector.
3006 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3007 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00003008
Chris Lattnerb0489812008-04-07 06:38:24 +00003009 // Consider qualified interfaces and interfaces the same.
3010 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
3011 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00003012
Chris Lattnera36a61f2008-04-07 05:43:21 +00003013 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003014 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003015 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3016 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003017
Steve Naroffd824c9c2009-04-14 15:11:46 +00003018 // 'id' and 'Class' act sort of like void* for ObjC interfaces
3019 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003020 return LHS;
Steve Naroffd824c9c2009-04-14 15:11:46 +00003021 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003022 return RHS;
3023
Steve Naroffbc76dd02008-12-10 22:14:21 +00003024 // ID is compatible with all qualified id types.
3025 if (LHS->isObjCQualifiedIdType()) {
3026 if (const PointerType *PT = RHS->getAsPointerType()) {
3027 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003028 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003029 return LHS;
3030 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3031 // Unfortunately, this API is part of Sema (which we don't have access
3032 // to. Need to refactor. The following check is insufficient, since we
3033 // need to make sure the class implements the protocol.
3034 if (pType->isObjCInterfaceType())
3035 return LHS;
3036 }
3037 }
3038 if (RHS->isObjCQualifiedIdType()) {
3039 if (const PointerType *PT = LHS->getAsPointerType()) {
3040 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003041 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003042 return RHS;
3043 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3044 // Unfortunately, this API is part of Sema (which we don't have access
3045 // to. Need to refactor. The following check is insufficient, since we
3046 // need to make sure the class implements the protocol.
3047 if (pType->isObjCInterfaceType())
3048 return RHS;
3049 }
3050 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003051 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3052 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00003053 if (const EnumType* ETy = LHS->getAsEnumType()) {
3054 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3055 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003056 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003057 if (const EnumType* ETy = RHS->getAsEnumType()) {
3058 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3059 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003060 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003061
Eli Friedman3d815e72008-08-22 00:56:42 +00003062 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003063 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003064
Steve Naroff4a746782008-01-09 22:43:08 +00003065 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003066 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003067#define TYPE(Class, Base)
3068#define ABSTRACT_TYPE(Class, Base)
3069#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3070#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3071#include "clang/AST/TypeNodes.def"
3072 assert(false && "Non-canonical and dependent types shouldn't get here");
3073 return QualType();
3074
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003075 case Type::LValueReference:
3076 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003077 case Type::MemberPointer:
3078 assert(false && "C++ should never be in mergeTypes");
3079 return QualType();
3080
3081 case Type::IncompleteArray:
3082 case Type::VariableArray:
3083 case Type::FunctionProto:
3084 case Type::ExtVector:
3085 case Type::ObjCQualifiedInterface:
3086 assert(false && "Types are eliminated above");
3087 return QualType();
3088
Chris Lattner1adb8832008-01-14 05:45:46 +00003089 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003090 {
3091 // Merge two pointer types, while trying to preserve typedef info
3092 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3093 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3094 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3095 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003096 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3097 return LHS;
3098 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3099 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003100 return getPointerType(ResultType);
3101 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003102 case Type::BlockPointer:
3103 {
3104 // Merge two block pointer types, while trying to preserve typedef info
3105 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3106 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3107 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3108 if (ResultType.isNull()) return QualType();
3109 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3110 return LHS;
3111 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3112 return RHS;
3113 return getBlockPointerType(ResultType);
3114 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003115 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003116 {
3117 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3118 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3119 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3120 return QualType();
3121
3122 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3123 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3124 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3125 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003126 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3127 return LHS;
3128 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3129 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003130 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3131 ArrayType::ArraySizeModifier(), 0);
3132 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3133 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003134 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3135 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003136 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3137 return LHS;
3138 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3139 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003140 if (LVAT) {
3141 // FIXME: This isn't correct! But tricky to implement because
3142 // the array's size has to be the size of LHS, but the type
3143 // has to be different.
3144 return LHS;
3145 }
3146 if (RVAT) {
3147 // FIXME: This isn't correct! But tricky to implement because
3148 // the array's size has to be the size of RHS, but the type
3149 // has to be different.
3150 return RHS;
3151 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003152 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3153 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003154 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003155 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003156 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003157 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003158 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003159 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003160 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003161 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3162 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003163 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003164 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003165 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003166 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003167 case Type::Complex:
3168 // Distinct complex types are incompatible.
3169 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003170 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003171 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003172 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3173 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003174 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003175 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003176 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003177 // FIXME: This should be type compatibility, e.g. whether
3178 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003179 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3180 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3181 if (LHSIface && RHSIface &&
3182 canAssignObjCInterfaces(LHSIface, RHSIface))
3183 return LHS;
3184
Eli Friedman3d815e72008-08-22 00:56:42 +00003185 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003186 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003187 case Type::ObjCQualifiedId:
3188 // Distinct qualified id's are not compatible.
3189 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003190 case Type::FixedWidthInt:
3191 // Distinct fixed-width integers are not compatible.
3192 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003193 case Type::ExtQual:
3194 // FIXME: ExtQual types can be compatible even if they're not
3195 // identical!
3196 return QualType();
3197 // First attempt at an implementation, but I'm not really sure it's
3198 // right...
3199#if 0
3200 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3201 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3202 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3203 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3204 return QualType();
3205 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3206 LHSBase = QualType(LQual->getBaseType(), 0);
3207 RHSBase = QualType(RQual->getBaseType(), 0);
3208 ResultType = mergeTypes(LHSBase, RHSBase);
3209 if (ResultType.isNull()) return QualType();
3210 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3211 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3212 return LHS;
3213 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3214 return RHS;
3215 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3216 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3217 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3218 return ResultType;
3219#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003220
3221 case Type::TemplateSpecialization:
3222 assert(false && "Dependent types have no size");
3223 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003224 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003225
3226 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003227}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003228
Chris Lattner5426bf62008-04-07 07:01:58 +00003229//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003230// Integer Predicates
3231//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003232
Eli Friedmanad74a752008-06-28 06:23:08 +00003233unsigned ASTContext::getIntWidth(QualType T) {
3234 if (T == BoolTy)
3235 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003236 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3237 return FWIT->getWidth();
3238 }
3239 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003240 return (unsigned)getTypeSize(T);
3241}
3242
3243QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3244 assert(T->isSignedIntegerType() && "Unexpected type");
3245 if (const EnumType* ETy = T->getAsEnumType())
3246 T = ETy->getDecl()->getIntegerType();
3247 const BuiltinType* BTy = T->getAsBuiltinType();
3248 assert (BTy && "Unexpected signed integer type");
3249 switch (BTy->getKind()) {
3250 case BuiltinType::Char_S:
3251 case BuiltinType::SChar:
3252 return UnsignedCharTy;
3253 case BuiltinType::Short:
3254 return UnsignedShortTy;
3255 case BuiltinType::Int:
3256 return UnsignedIntTy;
3257 case BuiltinType::Long:
3258 return UnsignedLongTy;
3259 case BuiltinType::LongLong:
3260 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00003261 case BuiltinType::Int128:
3262 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00003263 default:
3264 assert(0 && "Unexpected signed integer type");
3265 return QualType();
3266 }
3267}
3268
Douglas Gregor2cf26342009-04-09 22:27:44 +00003269ExternalASTSource::~ExternalASTSource() { }
3270
3271void ExternalASTSource::PrintStats() { }