blob: 6cd016cab818ba2fdf7de0fa803c367cdb2345e4 [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;
416 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000417 Width = Target.getFloatWidth();
418 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000419 break;
420 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000421 Width = Target.getDoubleWidth();
422 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000423 break;
424 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000425 Width = Target.getLongDoubleWidth();
426 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000427 break;
Chris Lattner2df9ced2009-04-30 02:43:43 +0000428 case BuiltinType::Int128:
429 case BuiltinType::UInt128:
430 Width = 128;
431
432 // FIXME: Is this correct for all targets?
433 Align = 128;
434 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000435 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000436 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000437 case Type::FixedWidthInt:
438 // FIXME: This isn't precisely correct; the width/alignment should depend
439 // on the available types for the target
440 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000441 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000442 Align = Width;
443 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000444 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000445 // FIXME: Pointers into different addr spaces could have different sizes and
446 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000447 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000448 case Type::ObjCQualifiedId:
Douglas Gregor72564e72009-02-26 23:50:07 +0000449 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000450 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000451 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000452 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000453 case Type::BlockPointer: {
454 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
455 Width = Target.getPointerWidth(AS);
456 Align = Target.getPointerAlign(AS);
457 break;
458 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000459 case Type::Pointer: {
460 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000461 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000462 Align = Target.getPointerAlign(AS);
463 break;
464 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000465 case Type::LValueReference:
466 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000467 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000468 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000469 // FIXME: This is wrong for struct layout: a reference in a struct has
470 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000471 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000472 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000473 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000474 // the GCC ABI, where pointers to data are one pointer large, pointers to
475 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000476 // other compilers too, we need to delegate this completely to TargetInfo
477 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000478 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
479 unsigned AS = Pointee.getAddressSpace();
480 Width = Target.getPointerWidth(AS);
481 if (Pointee->isFunctionType())
482 Width *= 2;
483 Align = Target.getPointerAlign(AS);
484 // GCC aligns at single pointer width.
485 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000486 case Type::Complex: {
487 // Complex types have the same alignment as their elements, but twice the
488 // size.
489 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000490 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000491 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000492 Align = EltInfo.second;
493 break;
494 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000495 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000496 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000497 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
498 Width = Layout.getSize();
499 Align = Layout.getAlignment();
500 break;
501 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000502 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000503 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000504 const TagType *TT = cast<TagType>(T);
505
506 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000507 Width = 1;
508 Align = 1;
509 break;
510 }
511
Daniel Dunbar1d751182008-11-08 05:48:37 +0000512 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000513 return getTypeInfo(ET->getDecl()->getIntegerType());
514
Daniel Dunbar1d751182008-11-08 05:48:37 +0000515 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000516 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
517 Width = Layout.getSize();
518 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000519 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000520 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000521
522 case Type::TemplateSpecialization:
523 assert(false && "Dependent types have no size");
524 break;
Chris Lattner71763312008-04-06 22:05:18 +0000525 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000526
Chris Lattner464175b2007-07-18 17:52:12 +0000527 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000528 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000529}
530
Chris Lattner34ebde42009-01-27 18:08:34 +0000531/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
532/// type for the current target in bits. This can be different than the ABI
533/// alignment in cases where it is beneficial for performance to overalign
534/// a data type.
535unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
536 unsigned ABIAlign = getTypeAlign(T);
537
538 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000539 if (T->isSpecificBuiltinType(BuiltinType::Double))
540 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000541
542 return ABIAlign;
543}
544
545
Devang Patel8b277042008-06-04 21:22:16 +0000546/// LayoutField - Field layout.
547void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000548 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000549 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000550 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000551 uint64_t FieldOffset = IsUnion ? 0 : Size;
552 uint64_t FieldSize;
553 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000554
555 // FIXME: Should this override struct packing? Probably we want to
556 // take the minimum?
557 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
558 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000559
560 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
561 // TODO: Need to check this algorithm on other targets!
562 // (tested on Linux-X86)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000563 FieldSize = BitWidthExpr->EvaluateAsInt(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000564
565 std::pair<uint64_t, unsigned> FieldInfo =
566 Context.getTypeInfo(FD->getType());
567 uint64_t TypeSize = FieldInfo.first;
568
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000569 // Determine the alignment of this bitfield. The packing
570 // attributes define a maximum and the alignment attribute defines
571 // a minimum.
572 // FIXME: What is the right behavior when the specified alignment
573 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000574 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000575 if (FieldPacking)
576 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000577 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
578 FieldAlign = std::max(FieldAlign, AA->getAlignment());
579
580 // Check if we need to add padding to give the field the correct
581 // alignment.
582 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
583 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
584
585 // Padding members don't affect overall alignment
586 if (!FD->getIdentifier())
587 FieldAlign = 1;
588 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000589 if (FD->getType()->isIncompleteArrayType()) {
590 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000591 // query getTypeInfo about these, so we figure it out here.
592 // Flexible array members don't have any size, but they
593 // have to be aligned appropriately for their element type.
594 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000595 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000596 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson2f1169f2009-04-10 05:31:15 +0000597 } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
598 unsigned AS = RT->getPointeeType().getAddressSpace();
599 FieldSize = Context.Target.getPointerWidth(AS);
600 FieldAlign = Context.Target.getPointerAlign(AS);
Devang Patel8b277042008-06-04 21:22:16 +0000601 } else {
602 std::pair<uint64_t, unsigned> FieldInfo =
603 Context.getTypeInfo(FD->getType());
604 FieldSize = FieldInfo.first;
605 FieldAlign = FieldInfo.second;
606 }
607
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000608 // Determine the alignment of this bitfield. The packing
609 // attributes define a maximum and the alignment attribute defines
610 // a minimum. Additionally, the packing alignment must be at least
611 // a byte for non-bitfields.
612 //
613 // FIXME: What is the right behavior when the specified alignment
614 // is smaller than the specified packing?
615 if (FieldPacking)
616 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000617 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
618 FieldAlign = std::max(FieldAlign, AA->getAlignment());
619
620 // Round up the current record size to the field's alignment boundary.
621 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
622 }
623
624 // Place this field at the current location.
625 FieldOffsets[FieldNo] = FieldOffset;
626
627 // Reserve space for this field.
628 if (IsUnion) {
629 Size = std::max(Size, FieldSize);
630 } else {
631 Size = FieldOffset + FieldSize;
632 }
633
634 // Remember max struct/class alignment.
635 Alignment = std::max(Alignment, FieldAlign);
636}
637
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000638static void CollectLocalObjCIvars(ASTContext *Ctx,
639 const ObjCInterfaceDecl *OI,
640 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000641 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
642 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000643 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000644 if (!IVDecl->isInvalidDecl())
645 Fields.push_back(cast<FieldDecl>(IVDecl));
646 }
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +0000647 // look into properties.
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000648 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*Ctx),
649 E = OI->prop_end(*Ctx); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000650 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +0000651 Fields.push_back(cast<FieldDecl>(IV));
652 }
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000653}
654
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000655void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
656 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
657 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
658 CollectObjCIvars(SuperClass, Fields);
659 CollectLocalObjCIvars(this, OI, Fields);
660}
661
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000662/// addRecordToClass - produces record info. for the class for its
663/// ivars and all those inherited.
664///
Chris Lattnerf1690852009-03-31 08:48:01 +0000665const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D) {
Daniel Dunbar75da6742009-04-22 10:56:29 +0000666 assert(!D->isForwardDecl() && "Invalid decl!");
667
Chris Lattner23499252009-03-31 09:24:30 +0000668 RecordDecl *&RD = ASTRecordForInterface[D];
Daniel Dunbar75da6742009-04-22 10:56:29 +0000669 if (RD)
670 return RD;
Chris Lattnerf1690852009-03-31 08:48:01 +0000671
672 llvm::SmallVector<FieldDecl*, 32> RecFields;
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000673 CollectLocalObjCIvars(this, D, RecFields);
Chris Lattner23499252009-03-31 09:24:30 +0000674
Daniel Dunbar75da6742009-04-22 10:56:29 +0000675 RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, D->getLocation(),
676 D->getIdentifier());
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000677 const RecordDecl *SRD;
678 if (const ObjCInterfaceDecl *SuperClass = D->getSuperClass()) {
679 SRD = addRecordToClass(SuperClass);
680 } else {
681 SRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, SourceLocation(), 0);
682 const_cast<RecordDecl*>(SRD)->completeDefinition(*this);
683 }
684
685 RD->addDecl(*this,
686 FieldDecl::Create(*this, RD,
687 SourceLocation(),
688 0,
689 getTagDeclType(const_cast<RecordDecl*>(SRD)),
690 0, false));
691
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000692 /// FIXME! Can do collection of ivars and adding to the record while
693 /// doing it.
Chris Lattner16ff7052009-03-31 08:58:42 +0000694 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000695 RD->addDecl(*this,
696 FieldDecl::Create(*this, RD,
Chris Lattner23499252009-03-31 09:24:30 +0000697 RecFields[i]->getLocation(),
698 RecFields[i]->getIdentifier(),
699 RecFields[i]->getType(),
700 RecFields[i]->getBitWidth(), false));
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000701 }
Chris Lattnerf1690852009-03-31 08:48:01 +0000702
Chris Lattner23499252009-03-31 09:24:30 +0000703 RD->completeDefinition(*this);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000704 return RD;
705}
Devang Patel44a3dde2008-06-04 21:54:36 +0000706
Chris Lattner61710852008-10-05 17:34:18 +0000707/// getASTObjcInterfaceLayout - Get or compute information about the layout of
708/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000709/// position information.
710const ASTRecordLayout &
711ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
712 // Look up this layout, if already laid out, return what we have.
713 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
714 if (Entry) return *Entry;
715
716 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
717 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000718 ASTRecordLayout *NewEntry = NULL;
Fariborz Jahanian0c7ce5b2009-04-08 21:54:52 +0000719 // FIXME. Add actual count of synthesized ivars, instead of count
720 // of properties which is the upper bound, but is safe.
Daniel Dunbar4af44122009-04-08 20:18:15 +0000721 unsigned FieldCount =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000722 D->ivar_size() + std::distance(D->prop_begin(*this), D->prop_end(*this));
Devang Patel6a5a34c2008-06-06 02:14:01 +0000723 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
724 FieldCount++;
725 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
726 unsigned Alignment = SL.getAlignment();
727 uint64_t Size = SL.getSize();
728 NewEntry = new ASTRecordLayout(Size, Alignment);
729 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000730 // Super class is at the beginning of the layout.
731 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000732 } else {
733 NewEntry = new ASTRecordLayout();
734 NewEntry->InitializeLayout(FieldCount);
735 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000736 Entry = NewEntry;
737
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000738 unsigned StructPacking = 0;
739 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
740 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000741
742 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
743 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
744 AA->getAlignment()));
745
746 // Layout each ivar sequentially.
747 unsigned i = 0;
748 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
749 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
750 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000751 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000752 }
Fariborz Jahanian18191882009-03-31 18:11:23 +0000753 // Also synthesized ivars
Douglas Gregor6ab35242009-04-09 21:40:53 +0000754 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
755 E = D->prop_end(*this); I != E; ++I) {
Fariborz Jahanian18191882009-03-31 18:11:23 +0000756 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
757 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
758 }
Fariborz Jahanian99eee362009-04-01 19:37:34 +0000759
Devang Patel44a3dde2008-06-04 21:54:36 +0000760 // Finally, round the size of the total struct up to the alignment of the
761 // struct itself.
762 NewEntry->FinalizeLayout();
763 return *NewEntry;
764}
765
Devang Patel88a981b2007-11-01 19:11:01 +0000766/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000767/// specified record (struct/union/class), which indicates its size and field
768/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000769const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000770 D = D->getDefinition(*this);
771 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000772
Chris Lattner464175b2007-07-18 17:52:12 +0000773 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000774 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000775 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000776
Devang Patel88a981b2007-11-01 19:11:01 +0000777 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
778 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
779 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000780 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000781
Douglas Gregore267ff32008-12-11 20:41:00 +0000782 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000783 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
784 D->field_end(*this)));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000785 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000786
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000787 unsigned StructPacking = 0;
788 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
789 StructPacking = PA->getAlignment();
790
Eli Friedman4bd998b2008-05-30 09:31:38 +0000791 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000792 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
793 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000794
Eli Friedman4bd998b2008-05-30 09:31:38 +0000795 // Layout each field, for now, just sequentially, respecting alignment. In
796 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000797 unsigned FieldIdx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000798 for (RecordDecl::field_iterator Field = D->field_begin(*this),
799 FieldEnd = D->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000800 Field != FieldEnd; (void)++Field, ++FieldIdx)
801 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000802
803 // Finally, round the size of the total struct up to the alignment of the
804 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000805 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000806 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000807}
808
Chris Lattnera7674d82007-07-13 22:13:22 +0000809//===----------------------------------------------------------------------===//
810// Type creation/memoization methods
811//===----------------------------------------------------------------------===//
812
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000813QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000814 QualType CanT = getCanonicalType(T);
815 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000816 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000817
818 // If we are composing extended qualifiers together, merge together into one
819 // ExtQualType node.
820 unsigned CVRQuals = T.getCVRQualifiers();
821 QualType::GCAttrTypes GCAttr = QualType::GCNone;
822 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000823
Chris Lattnerb7d25532009-02-18 22:53:11 +0000824 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
825 // If this type already has an address space specified, it cannot get
826 // another one.
827 assert(EQT->getAddressSpace() == 0 &&
828 "Type cannot be in multiple addr spaces!");
829 GCAttr = EQT->getObjCGCAttr();
830 TypeNode = EQT->getBaseType();
831 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000832
Chris Lattnerb7d25532009-02-18 22:53:11 +0000833 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000834 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000835 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000836 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000837 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000838 return QualType(EXTQy, CVRQuals);
839
Christopher Lambebb97e92008-02-04 02:31:56 +0000840 // If the base type isn't canonical, this won't be a canonical type either,
841 // so fill in the canonical type field.
842 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000843 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000844 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000845
Chris Lattnerb7d25532009-02-18 22:53:11 +0000846 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000847 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000848 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000849 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000850 ExtQualType *New =
851 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000852 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000853 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000854 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000855}
856
Chris Lattnerb7d25532009-02-18 22:53:11 +0000857QualType ASTContext::getObjCGCQualType(QualType T,
858 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000859 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000860 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000861 return T;
862
Chris Lattnerb7d25532009-02-18 22:53:11 +0000863 // If we are composing extended qualifiers together, merge together into one
864 // ExtQualType node.
865 unsigned CVRQuals = T.getCVRQualifiers();
866 Type *TypeNode = T.getTypePtr();
867 unsigned AddressSpace = 0;
868
869 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
870 // If this type already has an address space specified, it cannot get
871 // another one.
872 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
873 "Type cannot be in multiple addr spaces!");
874 AddressSpace = EQT->getAddressSpace();
875 TypeNode = EQT->getBaseType();
876 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000877
878 // Check if we've already instantiated an gc qual'd type of this type.
879 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000880 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000881 void *InsertPos = 0;
882 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000883 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000884
885 // If the base type isn't canonical, this won't be a canonical type either,
886 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000887 // FIXME: Isn't this also not canonical if the base type is a array
888 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000889 QualType Canonical;
890 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000891 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000892
Chris Lattnerb7d25532009-02-18 22:53:11 +0000893 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000894 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
895 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
896 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000897 ExtQualType *New =
898 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000899 ExtQualTypes.InsertNode(New, InsertPos);
900 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000901 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000902}
Chris Lattnera7674d82007-07-13 22:13:22 +0000903
Reid Spencer5f016e22007-07-11 17:01:13 +0000904/// getComplexType - Return the uniqued reference to the type for a complex
905/// number with the specified element type.
906QualType ASTContext::getComplexType(QualType T) {
907 // Unique pointers, to guarantee there is only one pointer of a particular
908 // structure.
909 llvm::FoldingSetNodeID ID;
910 ComplexType::Profile(ID, T);
911
912 void *InsertPos = 0;
913 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
914 return QualType(CT, 0);
915
916 // If the pointee type isn't canonical, this won't be a canonical type either,
917 // so fill in the canonical type field.
918 QualType Canonical;
919 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000920 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000921
922 // Get the new insert position for the node we care about.
923 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000924 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000925 }
Steve Narofff83820b2009-01-27 22:08:43 +0000926 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000927 Types.push_back(New);
928 ComplexTypes.InsertNode(New, InsertPos);
929 return QualType(New, 0);
930}
931
Eli Friedmanf98aba32009-02-13 02:31:07 +0000932QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
933 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
934 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
935 FixedWidthIntType *&Entry = Map[Width];
936 if (!Entry)
937 Entry = new FixedWidthIntType(Width, Signed);
938 return QualType(Entry, 0);
939}
Reid Spencer5f016e22007-07-11 17:01:13 +0000940
941/// getPointerType - Return the uniqued reference to the type for a pointer to
942/// the specified type.
943QualType ASTContext::getPointerType(QualType T) {
944 // Unique pointers, to guarantee there is only one pointer of a particular
945 // structure.
946 llvm::FoldingSetNodeID ID;
947 PointerType::Profile(ID, T);
948
949 void *InsertPos = 0;
950 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
951 return QualType(PT, 0);
952
953 // If the pointee type isn't canonical, this won't be a canonical type either,
954 // so fill in the canonical type field.
955 QualType Canonical;
956 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000957 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000958
959 // Get the new insert position for the node we care about.
960 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000961 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 }
Steve Narofff83820b2009-01-27 22:08:43 +0000963 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000964 Types.push_back(New);
965 PointerTypes.InsertNode(New, InsertPos);
966 return QualType(New, 0);
967}
968
Steve Naroff5618bd42008-08-27 16:04:49 +0000969/// getBlockPointerType - Return the uniqued reference to the type for
970/// a pointer to the specified block.
971QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000972 assert(T->isFunctionType() && "block of function types only");
973 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000974 // structure.
975 llvm::FoldingSetNodeID ID;
976 BlockPointerType::Profile(ID, T);
977
978 void *InsertPos = 0;
979 if (BlockPointerType *PT =
980 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
981 return QualType(PT, 0);
982
Steve Naroff296e8d52008-08-28 19:20:44 +0000983 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000984 // type either so fill in the canonical type field.
985 QualType Canonical;
986 if (!T->isCanonical()) {
987 Canonical = getBlockPointerType(getCanonicalType(T));
988
989 // Get the new insert position for the node we care about.
990 BlockPointerType *NewIP =
991 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000992 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000993 }
Steve Narofff83820b2009-01-27 22:08:43 +0000994 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000995 Types.push_back(New);
996 BlockPointerTypes.InsertNode(New, InsertPos);
997 return QualType(New, 0);
998}
999
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001000/// getLValueReferenceType - Return the uniqued reference to the type for an
1001/// lvalue reference to the specified type.
1002QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001003 // Unique pointers, to guarantee there is only one pointer of a particular
1004 // structure.
1005 llvm::FoldingSetNodeID ID;
1006 ReferenceType::Profile(ID, T);
1007
1008 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001009 if (LValueReferenceType *RT =
1010 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001011 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001012
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 // If the referencee type isn't canonical, this won't be a canonical type
1014 // either, so fill in the canonical type field.
1015 QualType Canonical;
1016 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001017 Canonical = getLValueReferenceType(getCanonicalType(T));
1018
Reid Spencer5f016e22007-07-11 17:01:13 +00001019 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001020 LValueReferenceType *NewIP =
1021 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001022 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001023 }
1024
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001025 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001026 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001027 LValueReferenceTypes.InsertNode(New, InsertPos);
1028 return QualType(New, 0);
1029}
1030
1031/// getRValueReferenceType - Return the uniqued reference to the type for an
1032/// rvalue reference to the specified type.
1033QualType ASTContext::getRValueReferenceType(QualType T) {
1034 // Unique pointers, to guarantee there is only one pointer of a particular
1035 // structure.
1036 llvm::FoldingSetNodeID ID;
1037 ReferenceType::Profile(ID, T);
1038
1039 void *InsertPos = 0;
1040 if (RValueReferenceType *RT =
1041 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1042 return QualType(RT, 0);
1043
1044 // If the referencee type isn't canonical, this won't be a canonical type
1045 // either, so fill in the canonical type field.
1046 QualType Canonical;
1047 if (!T->isCanonical()) {
1048 Canonical = getRValueReferenceType(getCanonicalType(T));
1049
1050 // Get the new insert position for the node we care about.
1051 RValueReferenceType *NewIP =
1052 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1053 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1054 }
1055
1056 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1057 Types.push_back(New);
1058 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 return QualType(New, 0);
1060}
1061
Sebastian Redlf30208a2009-01-24 21:16:55 +00001062/// getMemberPointerType - Return the uniqued reference to the type for a
1063/// member pointer to the specified type, in the specified class.
1064QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1065{
1066 // Unique pointers, to guarantee there is only one pointer of a particular
1067 // structure.
1068 llvm::FoldingSetNodeID ID;
1069 MemberPointerType::Profile(ID, T, Cls);
1070
1071 void *InsertPos = 0;
1072 if (MemberPointerType *PT =
1073 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1074 return QualType(PT, 0);
1075
1076 // If the pointee or class type isn't canonical, this won't be a canonical
1077 // type either, so fill in the canonical type field.
1078 QualType Canonical;
1079 if (!T->isCanonical()) {
1080 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1081
1082 // Get the new insert position for the node we care about.
1083 MemberPointerType *NewIP =
1084 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1085 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1086 }
Steve Narofff83820b2009-01-27 22:08:43 +00001087 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001088 Types.push_back(New);
1089 MemberPointerTypes.InsertNode(New, InsertPos);
1090 return QualType(New, 0);
1091}
1092
Steve Narofffb22d962007-08-30 01:06:46 +00001093/// getConstantArrayType - Return the unique reference to the type for an
1094/// array of the specified element type.
1095QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +00001096 const llvm::APInt &ArySize,
1097 ArrayType::ArraySizeModifier ASM,
1098 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001099 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001100 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001101
1102 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001103 if (ConstantArrayType *ATP =
1104 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001105 return QualType(ATP, 0);
1106
1107 // If the element type isn't canonical, this won't be a canonical type either,
1108 // so fill in the canonical type field.
1109 QualType Canonical;
1110 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001111 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001112 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001113 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001114 ConstantArrayType *NewIP =
1115 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001116 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001117 }
1118
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001119 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001120 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001121 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001122 Types.push_back(New);
1123 return QualType(New, 0);
1124}
1125
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001126/// getVariableArrayType - Returns a non-unique reference to the type for a
1127/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001128QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1129 ArrayType::ArraySizeModifier ASM,
1130 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001131 // Since we don't unique expressions, it isn't possible to unique VLA's
1132 // that have an expression provided for their size.
1133
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001134 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001135 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001136
1137 VariableArrayTypes.push_back(New);
1138 Types.push_back(New);
1139 return QualType(New, 0);
1140}
1141
Douglas Gregor898574e2008-12-05 23:32:09 +00001142/// getDependentSizedArrayType - Returns a non-unique reference to
1143/// the type for a dependently-sized array of the specified element
1144/// type. FIXME: We will need these to be uniqued, or at least
1145/// comparable, at some point.
1146QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1147 ArrayType::ArraySizeModifier ASM,
1148 unsigned EltTypeQuals) {
1149 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1150 "Size must be type- or value-dependent!");
1151
1152 // Since we don't unique expressions, it isn't possible to unique
1153 // dependently-sized array types.
1154
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001155 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001156 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1157 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001158
1159 DependentSizedArrayTypes.push_back(New);
1160 Types.push_back(New);
1161 return QualType(New, 0);
1162}
1163
Eli Friedmanc5773c42008-02-15 18:16:39 +00001164QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1165 ArrayType::ArraySizeModifier ASM,
1166 unsigned EltTypeQuals) {
1167 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001168 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001169
1170 void *InsertPos = 0;
1171 if (IncompleteArrayType *ATP =
1172 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1173 return QualType(ATP, 0);
1174
1175 // If the element type isn't canonical, this won't be a canonical type
1176 // either, so fill in the canonical type field.
1177 QualType Canonical;
1178
1179 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001180 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001181 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001182
1183 // Get the new insert position for the node we care about.
1184 IncompleteArrayType *NewIP =
1185 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001186 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001187 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001188
Steve Narofff83820b2009-01-27 22:08:43 +00001189 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001190 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001191
1192 IncompleteArrayTypes.InsertNode(New, InsertPos);
1193 Types.push_back(New);
1194 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001195}
1196
Steve Naroff73322922007-07-18 18:00:27 +00001197/// getVectorType - Return the unique reference to a vector type of
1198/// the specified element type and size. VectorType must be a built-in type.
1199QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 BuiltinType *baseType;
1201
Chris Lattnerf52ab252008-04-06 22:59:24 +00001202 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001203 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001204
1205 // Check if we've already instantiated a vector of this type.
1206 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001207 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001208 void *InsertPos = 0;
1209 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1210 return QualType(VTP, 0);
1211
1212 // If the element type isn't canonical, this won't be a canonical type either,
1213 // so fill in the canonical type field.
1214 QualType Canonical;
1215 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001216 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001217
1218 // Get the new insert position for the node we care about.
1219 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001220 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001221 }
Steve Narofff83820b2009-01-27 22:08:43 +00001222 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001223 VectorTypes.InsertNode(New, InsertPos);
1224 Types.push_back(New);
1225 return QualType(New, 0);
1226}
1227
Nate Begeman213541a2008-04-18 23:10:10 +00001228/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001229/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001230QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001231 BuiltinType *baseType;
1232
Chris Lattnerf52ab252008-04-06 22:59:24 +00001233 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001234 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001235
1236 // Check if we've already instantiated a vector of this type.
1237 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001238 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001239 void *InsertPos = 0;
1240 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1241 return QualType(VTP, 0);
1242
1243 // If the element type isn't canonical, this won't be a canonical type either,
1244 // so fill in the canonical type field.
1245 QualType Canonical;
1246 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001247 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001248
1249 // Get the new insert position for the node we care about.
1250 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001251 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001252 }
Steve Narofff83820b2009-01-27 22:08:43 +00001253 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001254 VectorTypes.InsertNode(New, InsertPos);
1255 Types.push_back(New);
1256 return QualType(New, 0);
1257}
1258
Douglas Gregor72564e72009-02-26 23:50:07 +00001259/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001260///
Douglas Gregor72564e72009-02-26 23:50:07 +00001261QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001262 // Unique functions, to guarantee there is only one function of a particular
1263 // structure.
1264 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001265 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001266
1267 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001268 if (FunctionNoProtoType *FT =
1269 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 return QualType(FT, 0);
1271
1272 QualType Canonical;
1273 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001274 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001275
1276 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001277 FunctionNoProtoType *NewIP =
1278 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001279 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 }
1281
Douglas Gregor72564e72009-02-26 23:50:07 +00001282 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001284 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001285 return QualType(New, 0);
1286}
1287
1288/// getFunctionType - Return a normal function type with a typed argument
1289/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001290QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001291 unsigned NumArgs, bool isVariadic,
1292 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001293 // Unique functions, to guarantee there is only one function of a particular
1294 // structure.
1295 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001296 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001297 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001298
1299 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001300 if (FunctionProtoType *FTP =
1301 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001302 return QualType(FTP, 0);
1303
1304 // Determine whether the type being created is already canonical or not.
1305 bool isCanonical = ResultTy->isCanonical();
1306 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1307 if (!ArgArray[i]->isCanonical())
1308 isCanonical = false;
1309
1310 // If this type isn't canonical, get the canonical version of it.
1311 QualType Canonical;
1312 if (!isCanonical) {
1313 llvm::SmallVector<QualType, 16> CanonicalArgs;
1314 CanonicalArgs.reserve(NumArgs);
1315 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001316 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001317
Chris Lattnerf52ab252008-04-06 22:59:24 +00001318 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001319 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001320 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001321
1322 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001323 FunctionProtoType *NewIP =
1324 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001325 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001326 }
1327
Douglas Gregor72564e72009-02-26 23:50:07 +00001328 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001329 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001330 FunctionProtoType *FTP =
1331 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001332 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001333 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001334 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001336 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001337 return QualType(FTP, 0);
1338}
1339
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001340/// getTypeDeclType - Return the unique reference to the type for the
1341/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001342QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001343 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001344 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1345
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001346 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001347 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001348 else if (isa<TemplateTypeParmDecl>(Decl)) {
1349 assert(false && "Template type parameter types are always available.");
1350 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001351 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001352
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001353 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001354 if (PrevDecl)
1355 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001356 else
1357 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001358 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001359 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1360 if (PrevDecl)
1361 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001362 else
1363 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001364 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001365 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001366 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001367
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001368 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001369 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001370}
1371
Reid Spencer5f016e22007-07-11 17:01:13 +00001372/// getTypedefType - Return the unique reference to the type for the
1373/// specified typename decl.
1374QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1375 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1376
Chris Lattnerf52ab252008-04-06 22:59:24 +00001377 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001378 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001379 Types.push_back(Decl->TypeForDecl);
1380 return QualType(Decl->TypeForDecl, 0);
1381}
1382
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001383/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001384/// specified ObjC interface decl.
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001385QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001386 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1387
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001388 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1389 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff3536b442007-09-06 21:24:23 +00001390 Types.push_back(Decl->TypeForDecl);
1391 return QualType(Decl->TypeForDecl, 0);
1392}
1393
Douglas Gregorfab9d672009-02-05 23:33:38 +00001394/// \brief Retrieve the template type parameter type for a template
1395/// parameter with the given depth, index, and (optionally) name.
1396QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1397 IdentifierInfo *Name) {
1398 llvm::FoldingSetNodeID ID;
1399 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1400 void *InsertPos = 0;
1401 TemplateTypeParmType *TypeParm
1402 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1403
1404 if (TypeParm)
1405 return QualType(TypeParm, 0);
1406
1407 if (Name)
1408 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1409 getTemplateTypeParmType(Depth, Index));
1410 else
1411 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1412
1413 Types.push_back(TypeParm);
1414 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1415
1416 return QualType(TypeParm, 0);
1417}
1418
Douglas Gregor55f6b142009-02-09 18:46:07 +00001419QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001420ASTContext::getTemplateSpecializationType(TemplateName Template,
1421 const TemplateArgument *Args,
1422 unsigned NumArgs,
1423 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001424 if (!Canon.isNull())
1425 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001426
Douglas Gregor55f6b142009-02-09 18:46:07 +00001427 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001428 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001429
Douglas Gregor55f6b142009-02-09 18:46:07 +00001430 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001431 TemplateSpecializationType *Spec
1432 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001433
1434 if (Spec)
1435 return QualType(Spec, 0);
1436
Douglas Gregor7532dc62009-03-30 22:58:21 +00001437 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001438 sizeof(TemplateArgument) * NumArgs),
1439 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001440 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001441 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001442 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001443
1444 return QualType(Spec, 0);
1445}
1446
Douglas Gregore4e5b052009-03-19 00:18:19 +00001447QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001448ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001449 QualType NamedType) {
1450 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001451 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001452
1453 void *InsertPos = 0;
1454 QualifiedNameType *T
1455 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1456 if (T)
1457 return QualType(T, 0);
1458
Douglas Gregorab452ba2009-03-26 23:50:42 +00001459 T = new (*this) QualifiedNameType(NNS, NamedType,
1460 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001461 Types.push_back(T);
1462 QualifiedNameTypes.InsertNode(T, InsertPos);
1463 return QualType(T, 0);
1464}
1465
Douglas Gregord57959a2009-03-27 23:10:48 +00001466QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1467 const IdentifierInfo *Name,
1468 QualType Canon) {
1469 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1470
1471 if (Canon.isNull()) {
1472 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1473 if (CanonNNS != NNS)
1474 Canon = getTypenameType(CanonNNS, Name);
1475 }
1476
1477 llvm::FoldingSetNodeID ID;
1478 TypenameType::Profile(ID, NNS, Name);
1479
1480 void *InsertPos = 0;
1481 TypenameType *T
1482 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1483 if (T)
1484 return QualType(T, 0);
1485
1486 T = new (*this) TypenameType(NNS, Name, Canon);
1487 Types.push_back(T);
1488 TypenameTypes.InsertNode(T, InsertPos);
1489 return QualType(T, 0);
1490}
1491
Douglas Gregor17343172009-04-01 00:28:59 +00001492QualType
1493ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1494 const TemplateSpecializationType *TemplateId,
1495 QualType Canon) {
1496 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1497
1498 if (Canon.isNull()) {
1499 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1500 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1501 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1502 const TemplateSpecializationType *CanonTemplateId
1503 = CanonType->getAsTemplateSpecializationType();
1504 assert(CanonTemplateId &&
1505 "Canonical type must also be a template specialization type");
1506 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1507 }
1508 }
1509
1510 llvm::FoldingSetNodeID ID;
1511 TypenameType::Profile(ID, NNS, TemplateId);
1512
1513 void *InsertPos = 0;
1514 TypenameType *T
1515 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1516 if (T)
1517 return QualType(T, 0);
1518
1519 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1520 Types.push_back(T);
1521 TypenameTypes.InsertNode(T, InsertPos);
1522 return QualType(T, 0);
1523}
1524
Chris Lattner88cb27a2008-04-07 04:56:42 +00001525/// CmpProtocolNames - Comparison predicate for sorting protocols
1526/// alphabetically.
1527static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1528 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001529 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001530}
1531
1532static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1533 unsigned &NumProtocols) {
1534 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1535
1536 // Sort protocols, keyed by name.
1537 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1538
1539 // Remove duplicates.
1540 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1541 NumProtocols = ProtocolsEnd-Protocols;
1542}
1543
1544
Chris Lattner065f0d72008-04-07 04:44:08 +00001545/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1546/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001547QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1548 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001549 // Sort the protocol list alphabetically to canonicalize it.
1550 SortAndUniqueProtocols(Protocols, NumProtocols);
1551
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001552 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001553 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001554
1555 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001556 if (ObjCQualifiedInterfaceType *QT =
1557 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001558 return QualType(QT, 0);
1559
1560 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001561 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001562 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001563
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001564 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001565 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001566 return QualType(QType, 0);
1567}
1568
Chris Lattner88cb27a2008-04-07 04:56:42 +00001569/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1570/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001571QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001572 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001573 // Sort the protocol list alphabetically to canonicalize it.
1574 SortAndUniqueProtocols(Protocols, NumProtocols);
1575
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001576 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001577 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001578
1579 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001580 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001581 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001582 return QualType(QT, 0);
1583
1584 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001585 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001586 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001587 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001588 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001589 return QualType(QType, 0);
1590}
1591
Douglas Gregor72564e72009-02-26 23:50:07 +00001592/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1593/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001594/// multiple declarations that refer to "typeof(x)" all contain different
1595/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1596/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001597QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001598 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001599 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001600 Types.push_back(toe);
1601 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001602}
1603
Steve Naroff9752f252007-08-01 18:02:17 +00001604/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1605/// TypeOfType AST's. The only motivation to unique these nodes would be
1606/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1607/// an issue. This doesn't effect the type checker, since it operates
1608/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001609QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001610 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001611 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001612 Types.push_back(tot);
1613 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001614}
1615
Reid Spencer5f016e22007-07-11 17:01:13 +00001616/// getTagDeclType - Return the unique reference to the type for the
1617/// specified TagDecl (struct/union/class/enum) decl.
1618QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001619 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001620 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001621}
1622
1623/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1624/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1625/// needs to agree with the definition in <stddef.h>.
1626QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001627 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001628}
1629
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001630/// getSignedWCharType - Return the type of "signed wchar_t".
1631/// Used when in C++, as a GCC extension.
1632QualType ASTContext::getSignedWCharType() const {
1633 // FIXME: derive from "Target" ?
1634 return WCharTy;
1635}
1636
1637/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1638/// Used when in C++, as a GCC extension.
1639QualType ASTContext::getUnsignedWCharType() const {
1640 // FIXME: derive from "Target" ?
1641 return UnsignedIntTy;
1642}
1643
Chris Lattner8b9023b2007-07-13 03:05:23 +00001644/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1645/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1646QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001647 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001648}
1649
Chris Lattnere6327742008-04-02 05:18:44 +00001650//===----------------------------------------------------------------------===//
1651// Type Operators
1652//===----------------------------------------------------------------------===//
1653
Chris Lattner77c96472008-04-06 22:41:35 +00001654/// getCanonicalType - Return the canonical (structural) type corresponding to
1655/// the specified potentially non-canonical type. The non-canonical version
1656/// of a type may have many "decorated" versions of types. Decorators can
1657/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1658/// to be free of any of these, allowing two canonical types to be compared
1659/// for exact equality with a simple pointer comparison.
1660QualType ASTContext::getCanonicalType(QualType T) {
1661 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001662
1663 // If the result has type qualifiers, make sure to canonicalize them as well.
1664 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1665 if (TypeQuals == 0) return CanType;
1666
1667 // If the type qualifiers are on an array type, get the canonical type of the
1668 // array with the qualifiers applied to the element type.
1669 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1670 if (!AT)
1671 return CanType.getQualifiedType(TypeQuals);
1672
1673 // Get the canonical version of the element with the extra qualifiers on it.
1674 // This can recursively sink qualifiers through multiple levels of arrays.
1675 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1676 NewEltTy = getCanonicalType(NewEltTy);
1677
1678 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1679 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1680 CAT->getIndexTypeQualifier());
1681 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1682 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1683 IAT->getIndexTypeQualifier());
1684
Douglas Gregor898574e2008-12-05 23:32:09 +00001685 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1686 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1687 DSAT->getSizeModifier(),
1688 DSAT->getIndexTypeQualifier());
1689
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001690 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1691 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1692 VAT->getSizeModifier(),
1693 VAT->getIndexTypeQualifier());
1694}
1695
Douglas Gregord57959a2009-03-27 23:10:48 +00001696NestedNameSpecifier *
1697ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1698 if (!NNS)
1699 return 0;
1700
1701 switch (NNS->getKind()) {
1702 case NestedNameSpecifier::Identifier:
1703 // Canonicalize the prefix but keep the identifier the same.
1704 return NestedNameSpecifier::Create(*this,
1705 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1706 NNS->getAsIdentifier());
1707
1708 case NestedNameSpecifier::Namespace:
1709 // A namespace is canonical; build a nested-name-specifier with
1710 // this namespace and no prefix.
1711 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1712
1713 case NestedNameSpecifier::TypeSpec:
1714 case NestedNameSpecifier::TypeSpecWithTemplate: {
1715 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1716 NestedNameSpecifier *Prefix = 0;
1717
1718 // FIXME: This isn't the right check!
1719 if (T->isDependentType())
1720 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1721
1722 return NestedNameSpecifier::Create(*this, Prefix,
1723 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1724 T.getTypePtr());
1725 }
1726
1727 case NestedNameSpecifier::Global:
1728 // The global specifier is canonical and unique.
1729 return NNS;
1730 }
1731
1732 // Required to silence a GCC warning
1733 return 0;
1734}
1735
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001736
1737const ArrayType *ASTContext::getAsArrayType(QualType T) {
1738 // Handle the non-qualified case efficiently.
1739 if (T.getCVRQualifiers() == 0) {
1740 // Handle the common positive case fast.
1741 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1742 return AT;
1743 }
1744
1745 // Handle the common negative case fast, ignoring CVR qualifiers.
1746 QualType CType = T->getCanonicalTypeInternal();
1747
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001748 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001749 // test.
1750 if (!isa<ArrayType>(CType) &&
1751 !isa<ArrayType>(CType.getUnqualifiedType()))
1752 return 0;
1753
1754 // Apply any CVR qualifiers from the array type to the element type. This
1755 // implements C99 6.7.3p8: "If the specification of an array type includes
1756 // any type qualifiers, the element type is so qualified, not the array type."
1757
1758 // If we get here, we either have type qualifiers on the type, or we have
1759 // sugar such as a typedef in the way. If we have type qualifiers on the type
1760 // we must propagate them down into the elemeng type.
1761 unsigned CVRQuals = T.getCVRQualifiers();
1762 unsigned AddrSpace = 0;
1763 Type *Ty = T.getTypePtr();
1764
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001765 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001766 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001767 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1768 AddrSpace = EXTQT->getAddressSpace();
1769 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001770 } else {
1771 T = Ty->getDesugaredType();
1772 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1773 break;
1774 CVRQuals |= T.getCVRQualifiers();
1775 Ty = T.getTypePtr();
1776 }
1777 }
1778
1779 // If we have a simple case, just return now.
1780 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1781 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1782 return ATy;
1783
1784 // Otherwise, we have an array and we have qualifiers on it. Push the
1785 // qualifiers into the array element type and return a new array type.
1786 // Get the canonical version of the element with the extra qualifiers on it.
1787 // This can recursively sink qualifiers through multiple levels of arrays.
1788 QualType NewEltTy = ATy->getElementType();
1789 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001790 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001791 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1792
1793 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1794 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1795 CAT->getSizeModifier(),
1796 CAT->getIndexTypeQualifier()));
1797 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1798 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1799 IAT->getSizeModifier(),
1800 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001801
Douglas Gregor898574e2008-12-05 23:32:09 +00001802 if (const DependentSizedArrayType *DSAT
1803 = dyn_cast<DependentSizedArrayType>(ATy))
1804 return cast<ArrayType>(
1805 getDependentSizedArrayType(NewEltTy,
1806 DSAT->getSizeExpr(),
1807 DSAT->getSizeModifier(),
1808 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001809
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001810 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1811 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1812 VAT->getSizeModifier(),
1813 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001814}
1815
1816
Chris Lattnere6327742008-04-02 05:18:44 +00001817/// getArrayDecayedType - Return the properly qualified result of decaying the
1818/// specified array type to a pointer. This operation is non-trivial when
1819/// handling typedefs etc. The canonical type of "T" must be an array type,
1820/// this returns a pointer to a properly qualified element of the array.
1821///
1822/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1823QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001824 // Get the element type with 'getAsArrayType' so that we don't lose any
1825 // typedefs in the element type of the array. This also handles propagation
1826 // of type qualifiers from the array type into the element type if present
1827 // (C99 6.7.3p8).
1828 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1829 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001830
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001831 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001832
1833 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001834 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001835}
1836
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001837QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001838 QualType ElemTy = VAT->getElementType();
1839
1840 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1841 return getBaseElementType(VAT);
1842
1843 return ElemTy;
1844}
1845
Reid Spencer5f016e22007-07-11 17:01:13 +00001846/// getFloatingRank - Return a relative rank for floating point types.
1847/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001848static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001849 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001850 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001851
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001852 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001853 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001854 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001855 case BuiltinType::Float: return FloatRank;
1856 case BuiltinType::Double: return DoubleRank;
1857 case BuiltinType::LongDouble: return LongDoubleRank;
1858 }
1859}
1860
Steve Naroff716c7302007-08-27 01:41:48 +00001861/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1862/// point or a complex type (based on typeDomain/typeSize).
1863/// 'typeDomain' is a real floating point or complex type.
1864/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001865QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1866 QualType Domain) const {
1867 FloatingRank EltRank = getFloatingRank(Size);
1868 if (Domain->isComplexType()) {
1869 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001870 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001871 case FloatRank: return FloatComplexTy;
1872 case DoubleRank: return DoubleComplexTy;
1873 case LongDoubleRank: return LongDoubleComplexTy;
1874 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001875 }
Chris Lattner1361b112008-04-06 23:58:54 +00001876
1877 assert(Domain->isRealFloatingType() && "Unknown domain!");
1878 switch (EltRank) {
1879 default: assert(0 && "getFloatingRank(): illegal value for rank");
1880 case FloatRank: return FloatTy;
1881 case DoubleRank: return DoubleTy;
1882 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001883 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001884}
1885
Chris Lattner7cfeb082008-04-06 23:55:33 +00001886/// getFloatingTypeOrder - Compare the rank of the two specified floating
1887/// point types, ignoring the domain of the type (i.e. 'double' ==
1888/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1889/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001890int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1891 FloatingRank LHSR = getFloatingRank(LHS);
1892 FloatingRank RHSR = getFloatingRank(RHS);
1893
1894 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001895 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001896 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001897 return 1;
1898 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001899}
1900
Chris Lattnerf52ab252008-04-06 22:59:24 +00001901/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1902/// routine will assert if passed a built-in type that isn't an integer or enum,
1903/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001904unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001905 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001906 if (EnumType* ET = dyn_cast<EnumType>(T))
1907 T = ET->getDecl()->getIntegerType().getTypePtr();
1908
1909 // There are two things which impact the integer rank: the width, and
1910 // the ordering of builtins. The builtin ordering is encoded in the
1911 // bottom three bits; the width is encoded in the bits above that.
1912 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1913 return FWIT->getWidth() << 3;
1914 }
1915
Chris Lattnerf52ab252008-04-06 22:59:24 +00001916 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001917 default: assert(0 && "getIntegerRank(): not a built-in integer");
1918 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001919 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001920 case BuiltinType::Char_S:
1921 case BuiltinType::Char_U:
1922 case BuiltinType::SChar:
1923 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001924 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001925 case BuiltinType::Short:
1926 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001927 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001928 case BuiltinType::Int:
1929 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001930 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001931 case BuiltinType::Long:
1932 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001933 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001934 case BuiltinType::LongLong:
1935 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001936 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00001937 case BuiltinType::Int128:
1938 case BuiltinType::UInt128:
1939 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001940 }
1941}
1942
Chris Lattner7cfeb082008-04-06 23:55:33 +00001943/// getIntegerTypeOrder - Returns the highest ranked integer type:
1944/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1945/// LHS < RHS, return -1.
1946int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001947 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1948 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001949 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001950
Chris Lattnerf52ab252008-04-06 22:59:24 +00001951 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1952 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001953
Chris Lattner7cfeb082008-04-06 23:55:33 +00001954 unsigned LHSRank = getIntegerRank(LHSC);
1955 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001956
Chris Lattner7cfeb082008-04-06 23:55:33 +00001957 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1958 if (LHSRank == RHSRank) return 0;
1959 return LHSRank > RHSRank ? 1 : -1;
1960 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001961
Chris Lattner7cfeb082008-04-06 23:55:33 +00001962 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1963 if (LHSUnsigned) {
1964 // If the unsigned [LHS] type is larger, return it.
1965 if (LHSRank >= RHSRank)
1966 return 1;
1967
1968 // If the signed type can represent all values of the unsigned type, it
1969 // wins. Because we are dealing with 2's complement and types that are
1970 // powers of two larger than each other, this is always safe.
1971 return -1;
1972 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001973
Chris Lattner7cfeb082008-04-06 23:55:33 +00001974 // If the unsigned [RHS] type is larger, return it.
1975 if (RHSRank >= LHSRank)
1976 return -1;
1977
1978 // If the signed type can represent all values of the unsigned type, it
1979 // wins. Because we are dealing with 2's complement and types that are
1980 // powers of two larger than each other, this is always safe.
1981 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001982}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001983
1984// getCFConstantStringType - Return the type used for constant CFStrings.
1985QualType ASTContext::getCFConstantStringType() {
1986 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001987 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001988 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001989 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001990 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001991
1992 // const int *isa;
1993 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001994 // int flags;
1995 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001996 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001997 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001998 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001999 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00002000
Anders Carlsson71993dd2007-08-17 05:31:46 +00002001 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002002 for (unsigned i = 0; i < 4; ++i) {
2003 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2004 SourceLocation(), 0,
2005 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002006 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002007 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002008 }
2009
2010 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002011 }
2012
2013 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002014}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002015
Douglas Gregor319ac892009-04-23 22:29:11 +00002016void ASTContext::setCFConstantStringType(QualType T) {
2017 const RecordType *Rec = T->getAsRecordType();
2018 assert(Rec && "Invalid CFConstantStringType");
2019 CFConstantStringTypeDecl = Rec->getDecl();
2020}
2021
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002022QualType ASTContext::getObjCFastEnumerationStateType()
2023{
2024 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002025 ObjCFastEnumerationStateTypeDecl =
2026 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2027 &Idents.get("__objcFastEnumerationState"));
2028
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002029 QualType FieldTypes[] = {
2030 UnsignedLongTy,
2031 getPointerType(ObjCIdType),
2032 getPointerType(UnsignedLongTy),
2033 getConstantArrayType(UnsignedLongTy,
2034 llvm::APInt(32, 5), ArrayType::Normal, 0)
2035 };
2036
Douglas Gregor44b43212008-12-11 16:49:14 +00002037 for (size_t i = 0; i < 4; ++i) {
2038 FieldDecl *Field = FieldDecl::Create(*this,
2039 ObjCFastEnumerationStateTypeDecl,
2040 SourceLocation(), 0,
2041 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002042 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002043 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002044 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002045
Douglas Gregor44b43212008-12-11 16:49:14 +00002046 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002047 }
2048
2049 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2050}
2051
Douglas Gregor319ac892009-04-23 22:29:11 +00002052void ASTContext::setObjCFastEnumerationStateType(QualType T) {
2053 const RecordType *Rec = T->getAsRecordType();
2054 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2055 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2056}
2057
Anders Carlssone8c49532007-10-29 06:33:42 +00002058// This returns true if a type has been typedefed to BOOL:
2059// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002060static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002061 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002062 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2063 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002064
2065 return false;
2066}
2067
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002068/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002069/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002070int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002071 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002072
2073 // Make all integer and enum types at least as large as an int
2074 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002075 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002076 // Treat arrays as pointers, since that's how they're passed in.
2077 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002078 sz = getTypeSize(VoidPtrTy);
2079 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002080}
2081
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002082/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002083/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002084void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002085 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002086 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002087 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002088 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002089 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002090 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002091 // Compute size of all parameters.
2092 // Start with computing size of a pointer in number of bytes.
2093 // FIXME: There might(should) be a better way of doing this computation!
2094 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002095 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002096 // The first two arguments (self and _cmd) are pointers; account for
2097 // their size.
2098 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002099 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2100 E = Decl->param_end(); PI != E; ++PI) {
2101 QualType PType = (*PI)->getType();
2102 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002103 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002104 ParmOffset += sz;
2105 }
2106 S += llvm::utostr(ParmOffset);
2107 S += "@0:";
2108 S += llvm::utostr(PtrSize);
2109
2110 // Argument types.
2111 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002112 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2113 E = Decl->param_end(); PI != E; ++PI) {
2114 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002115 QualType PType = PVDecl->getOriginalType();
2116 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002117 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2118 // Use array's original type only if it has known number of
2119 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002120 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002121 PType = PVDecl->getType();
2122 } else if (PType->isFunctionType())
2123 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002124 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002125 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002126 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002127 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002128 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002129 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002130 }
2131}
2132
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002133/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002134/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002135/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2136/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002137/// Property attributes are stored as a comma-delimited C string. The simple
2138/// attributes readonly and bycopy are encoded as single characters. The
2139/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2140/// encoded as single characters, followed by an identifier. Property types
2141/// are also encoded as a parametrized attribute. The characters used to encode
2142/// these attributes are defined by the following enumeration:
2143/// @code
2144/// enum PropertyAttributes {
2145/// kPropertyReadOnly = 'R', // property is read-only.
2146/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2147/// kPropertyByref = '&', // property is a reference to the value last assigned
2148/// kPropertyDynamic = 'D', // property is dynamic
2149/// kPropertyGetter = 'G', // followed by getter selector name
2150/// kPropertySetter = 'S', // followed by setter selector name
2151/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2152/// kPropertyType = 't' // followed by old-style type encoding.
2153/// kPropertyWeak = 'W' // 'weak' property
2154/// kPropertyStrong = 'P' // property GC'able
2155/// kPropertyNonAtomic = 'N' // property non-atomic
2156/// };
2157/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002158void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2159 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002160 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002161 // Collect information from the property implementation decl(s).
2162 bool Dynamic = false;
2163 ObjCPropertyImplDecl *SynthesizePID = 0;
2164
2165 // FIXME: Duplicated code due to poor abstraction.
2166 if (Container) {
2167 if (const ObjCCategoryImplDecl *CID =
2168 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2169 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002170 i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2171 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002172 ObjCPropertyImplDecl *PID = *i;
2173 if (PID->getPropertyDecl() == PD) {
2174 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2175 Dynamic = true;
2176 } else {
2177 SynthesizePID = PID;
2178 }
2179 }
2180 }
2181 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002182 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002183 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002184 i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2185 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002186 ObjCPropertyImplDecl *PID = *i;
2187 if (PID->getPropertyDecl() == PD) {
2188 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2189 Dynamic = true;
2190 } else {
2191 SynthesizePID = PID;
2192 }
2193 }
2194 }
2195 }
2196 }
2197
2198 // FIXME: This is not very efficient.
2199 S = "T";
2200
2201 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002202 // GCC has some special rules regarding encoding of properties which
2203 // closely resembles encoding of ivars.
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002204 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002205 true /* outermost type */,
2206 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002207
2208 if (PD->isReadOnly()) {
2209 S += ",R";
2210 } else {
2211 switch (PD->getSetterKind()) {
2212 case ObjCPropertyDecl::Assign: break;
2213 case ObjCPropertyDecl::Copy: S += ",C"; break;
2214 case ObjCPropertyDecl::Retain: S += ",&"; break;
2215 }
2216 }
2217
2218 // It really isn't clear at all what this means, since properties
2219 // are "dynamic by default".
2220 if (Dynamic)
2221 S += ",D";
2222
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002223 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2224 S += ",N";
2225
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002226 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2227 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002228 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002229 }
2230
2231 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2232 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002233 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002234 }
2235
2236 if (SynthesizePID) {
2237 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2238 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002239 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002240 }
2241
2242 // FIXME: OBJCGC: weak & strong
2243}
2244
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002245/// getLegacyIntegralTypeEncoding -
2246/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002247/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002248/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2249///
2250void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2251 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2252 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002253 if (BT->getKind() == BuiltinType::ULong &&
2254 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002255 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002256 else
2257 if (BT->getKind() == BuiltinType::Long &&
2258 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002259 PointeeTy = IntTy;
2260 }
2261 }
2262}
2263
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002264void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002265 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002266 // We follow the behavior of gcc, expanding structures which are
2267 // directly pointed to, and expanding embedded structures. Note that
2268 // these rules are sufficient to prevent recursive encoding of the
2269 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002270 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2271 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002272}
2273
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002274static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002275 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002276 const Expr *E = FD->getBitWidth();
2277 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2278 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002279 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002280 S += 'b';
2281 S += llvm::utostr(N);
2282}
2283
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002284void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2285 bool ExpandPointedToStructures,
2286 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002287 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002288 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002289 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002290 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002291 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002292 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002293 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002294 else {
2295 char encoding;
2296 switch (BT->getKind()) {
2297 default: assert(0 && "Unhandled builtin type kind");
2298 case BuiltinType::Void: encoding = 'v'; break;
2299 case BuiltinType::Bool: encoding = 'B'; break;
2300 case BuiltinType::Char_U:
2301 case BuiltinType::UChar: encoding = 'C'; break;
2302 case BuiltinType::UShort: encoding = 'S'; break;
2303 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002304 case BuiltinType::ULong:
2305 encoding =
2306 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2307 break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002308 case BuiltinType::UInt128: encoding = 'T'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002309 case BuiltinType::ULongLong: encoding = 'Q'; break;
2310 case BuiltinType::Char_S:
2311 case BuiltinType::SChar: encoding = 'c'; break;
2312 case BuiltinType::Short: encoding = 's'; break;
2313 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002314 case BuiltinType::Long:
2315 encoding =
2316 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2317 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002318 case BuiltinType::LongLong: encoding = 'q'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002319 case BuiltinType::Int128: encoding = 't'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002320 case BuiltinType::Float: encoding = 'f'; break;
2321 case BuiltinType::Double: encoding = 'd'; break;
2322 case BuiltinType::LongDouble: encoding = 'd'; break;
2323 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002324
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002325 S += encoding;
2326 }
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002327 } else if (const ComplexType *CT = T->getAsComplexType()) {
2328 S += 'j';
2329 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2330 false);
2331 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002332 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2333 ExpandPointedToStructures,
2334 ExpandStructures, FD);
2335 if (FD || EncodingProperty) {
2336 // Note that we do extended encoding of protocol qualifer list
2337 // Only when doing ivar or property encoding.
2338 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2339 S += '"';
2340 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2341 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2342 S += '<';
2343 S += Proto->getNameAsString();
2344 S += '>';
2345 }
2346 S += '"';
2347 }
2348 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002349 }
2350 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002351 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002352 bool isReadOnly = false;
2353 // For historical/compatibility reasons, the read-only qualifier of the
2354 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2355 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2356 // Also, do not emit the 'r' for anything but the outermost type!
2357 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2358 if (OutermostType && T.isConstQualified()) {
2359 isReadOnly = true;
2360 S += 'r';
2361 }
2362 }
2363 else if (OutermostType) {
2364 QualType P = PointeeTy;
2365 while (P->getAsPointerType())
2366 P = P->getAsPointerType()->getPointeeType();
2367 if (P.isConstQualified()) {
2368 isReadOnly = true;
2369 S += 'r';
2370 }
2371 }
2372 if (isReadOnly) {
2373 // Another legacy compatibility encoding. Some ObjC qualifier and type
2374 // combinations need to be rearranged.
2375 // Rewrite "in const" from "nr" to "rn"
2376 const char * s = S.c_str();
2377 int len = S.length();
2378 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2379 std::string replace = "rn";
2380 S.replace(S.end()-2, S.end(), replace);
2381 }
2382 }
Steve Naroff389bf462009-02-12 17:52:19 +00002383 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002384 S += '@';
2385 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002386 }
2387 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002388 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002389 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002390 // Another historical/compatibility reason.
2391 // We encode the underlying type which comes out as
2392 // {...};
2393 S += '^';
2394 getObjCEncodingForTypeImpl(PointeeTy, S,
2395 false, ExpandPointedToStructures,
2396 NULL);
2397 return;
2398 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002399 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002400 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002401 const ObjCInterfaceType *OIT =
2402 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002403 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002404 S += '"';
2405 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002406 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2407 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2408 S += '<';
2409 S += Proto->getNameAsString();
2410 S += '>';
2411 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002412 S += '"';
2413 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002414 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002415 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002416 S += '#';
2417 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002418 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002419 S += ':';
2420 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002421 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002422
2423 if (PointeeTy->isCharType()) {
2424 // char pointer types should be encoded as '*' unless it is a
2425 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002426 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002427 S += '*';
2428 return;
2429 }
2430 }
2431
2432 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002433 getLegacyIntegralTypeEncoding(PointeeTy);
2434
2435 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002436 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002437 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002438 } else if (const ArrayType *AT =
2439 // Ignore type qualifiers etc.
2440 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002441 if (isa<IncompleteArrayType>(AT)) {
2442 // Incomplete arrays are encoded as a pointer to the array element.
2443 S += '^';
2444
2445 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2446 false, ExpandStructures, FD);
2447 } else {
2448 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002449
Anders Carlsson559a8332009-02-22 01:38:57 +00002450 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2451 S += llvm::utostr(CAT->getSize().getZExtValue());
2452 else {
2453 //Variable length arrays are encoded as a regular array with 0 elements.
2454 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2455 S += '0';
2456 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002457
Anders Carlsson559a8332009-02-22 01:38:57 +00002458 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2459 false, ExpandStructures, FD);
2460 S += ']';
2461 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002462 } else if (T->getAsFunctionType()) {
2463 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002464 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002465 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002466 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002467 // Anonymous structures print as '?'
2468 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2469 S += II->getName();
2470 } else {
2471 S += '?';
2472 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002473 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002474 S += '=';
Douglas Gregor6ab35242009-04-09 21:40:53 +00002475 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2476 FieldEnd = RDecl->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +00002477 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002478 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002479 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002480 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002481 S += '"';
2482 }
2483
2484 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002485 if (Field->isBitField()) {
2486 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2487 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002488 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002489 QualType qt = Field->getType();
2490 getLegacyIntegralTypeEncoding(qt);
2491 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002492 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002493 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002494 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002495 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002496 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002497 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002498 if (FD && FD->isBitField())
2499 EncodeBitField(this, S, FD);
2500 else
2501 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002502 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002503 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002504 } else if (T->isObjCInterfaceType()) {
2505 // @encode(class_name)
2506 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2507 S += '{';
2508 const IdentifierInfo *II = OI->getIdentifier();
2509 S += II->getName();
2510 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002511 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002512 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002513 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002514 if (RecFields[i]->isBitField())
2515 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2516 RecFields[i]);
2517 else
2518 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2519 FD);
2520 }
2521 S += '}';
2522 }
2523 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002524 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002525}
2526
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002527void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002528 std::string& S) const {
2529 if (QT & Decl::OBJC_TQ_In)
2530 S += 'n';
2531 if (QT & Decl::OBJC_TQ_Inout)
2532 S += 'N';
2533 if (QT & Decl::OBJC_TQ_Out)
2534 S += 'o';
2535 if (QT & Decl::OBJC_TQ_Bycopy)
2536 S += 'O';
2537 if (QT & Decl::OBJC_TQ_Byref)
2538 S += 'R';
2539 if (QT & Decl::OBJC_TQ_Oneway)
2540 S += 'V';
2541}
2542
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002543void ASTContext::setBuiltinVaListType(QualType T)
2544{
2545 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2546
2547 BuiltinVaListType = T;
2548}
2549
Douglas Gregor319ac892009-04-23 22:29:11 +00002550void ASTContext::setObjCIdType(QualType T)
Steve Naroff7e219e42007-10-15 14:41:52 +00002551{
Douglas Gregor319ac892009-04-23 22:29:11 +00002552 ObjCIdType = T;
2553
2554 const TypedefType *TT = T->getAsTypedefType();
2555 if (!TT)
2556 return;
2557
2558 TypedefDecl *TD = TT->getDecl();
Steve Naroff7e219e42007-10-15 14:41:52 +00002559
2560 // typedef struct objc_object *id;
2561 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002562 // User error - caller will issue diagnostics.
2563 if (!ptr)
2564 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002565 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002566 // User error - caller will issue diagnostics.
2567 if (!rec)
2568 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002569 IdStructType = rec;
2570}
2571
Douglas Gregor319ac892009-04-23 22:29:11 +00002572void ASTContext::setObjCSelType(QualType T)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002573{
Douglas Gregor319ac892009-04-23 22:29:11 +00002574 ObjCSelType = T;
2575
2576 const TypedefType *TT = T->getAsTypedefType();
2577 if (!TT)
2578 return;
2579 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002580
2581 // typedef struct objc_selector *SEL;
2582 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002583 if (!ptr)
2584 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002585 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002586 if (!rec)
2587 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002588 SelStructType = rec;
2589}
2590
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002591void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002592{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002593 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002594}
2595
Douglas Gregor319ac892009-04-23 22:29:11 +00002596void ASTContext::setObjCClassType(QualType T)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002597{
Douglas Gregor319ac892009-04-23 22:29:11 +00002598 ObjCClassType = T;
2599
2600 const TypedefType *TT = T->getAsTypedefType();
2601 if (!TT)
2602 return;
2603 TypedefDecl *TD = TT->getDecl();
Anders Carlsson8baaca52007-10-31 02:53:19 +00002604
2605 // typedef struct objc_class *Class;
2606 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2607 assert(ptr && "'Class' incorrectly typed");
2608 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2609 assert(rec && "'Class' incorrectly typed");
2610 ClassStructType = rec;
2611}
2612
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002613void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2614 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002615 "'NSConstantString' type already set!");
2616
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002617 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002618}
2619
Douglas Gregor7532dc62009-03-30 22:58:21 +00002620/// \brief Retrieve the template name that represents a qualified
2621/// template name such as \c std::vector.
2622TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2623 bool TemplateKeyword,
2624 TemplateDecl *Template) {
2625 llvm::FoldingSetNodeID ID;
2626 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2627
2628 void *InsertPos = 0;
2629 QualifiedTemplateName *QTN =
2630 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2631 if (!QTN) {
2632 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2633 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2634 }
2635
2636 return TemplateName(QTN);
2637}
2638
2639/// \brief Retrieve the template name that represents a dependent
2640/// template name such as \c MetaFun::template apply.
2641TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2642 const IdentifierInfo *Name) {
2643 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2644
2645 llvm::FoldingSetNodeID ID;
2646 DependentTemplateName::Profile(ID, NNS, Name);
2647
2648 void *InsertPos = 0;
2649 DependentTemplateName *QTN =
2650 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2651
2652 if (QTN)
2653 return TemplateName(QTN);
2654
2655 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2656 if (CanonNNS == NNS) {
2657 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2658 } else {
2659 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2660 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2661 }
2662
2663 DependentTemplateNames.InsertNode(QTN, InsertPos);
2664 return TemplateName(QTN);
2665}
2666
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002667/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002668/// TargetInfo, produce the corresponding type. The unsigned @p Type
2669/// is actually a value of type @c TargetInfo::IntType.
2670QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002671 switch (Type) {
2672 case TargetInfo::NoInt: return QualType();
2673 case TargetInfo::SignedShort: return ShortTy;
2674 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2675 case TargetInfo::SignedInt: return IntTy;
2676 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2677 case TargetInfo::SignedLong: return LongTy;
2678 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2679 case TargetInfo::SignedLongLong: return LongLongTy;
2680 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2681 }
2682
2683 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002684 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002685}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002686
2687//===----------------------------------------------------------------------===//
2688// Type Predicates.
2689//===----------------------------------------------------------------------===//
2690
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002691/// isObjCNSObjectType - Return true if this is an NSObject object using
2692/// NSObject attribute on a c-style pointer type.
2693/// FIXME - Make it work directly on types.
2694///
2695bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2696 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2697 if (TypedefDecl *TD = TDT->getDecl())
2698 if (TD->getAttr<ObjCNSObjectAttr>())
2699 return true;
2700 }
2701 return false;
2702}
2703
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002704/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2705/// to an object type. This includes "id" and "Class" (two 'special' pointers
2706/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2707/// ID type).
2708bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002709 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002710 return true;
2711
Steve Naroff6ae98502008-10-21 18:24:04 +00002712 // Blocks are objects.
2713 if (Ty->isBlockPointerType())
2714 return true;
2715
2716 // All other object types are pointers.
Chris Lattner16ede0e2009-04-12 23:51:02 +00002717 const PointerType *PT = Ty->getAsPointerType();
2718 if (PT == 0)
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002719 return false;
2720
Chris Lattner16ede0e2009-04-12 23:51:02 +00002721 // If this a pointer to an interface (e.g. NSString*), it is ok.
2722 if (PT->getPointeeType()->isObjCInterfaceType() ||
2723 // If is has NSObject attribute, OK as well.
2724 isObjCNSObjectType(Ty))
2725 return true;
2726
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002727 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2728 // pointer types. This looks for the typedef specifically, not for the
Chris Lattner16ede0e2009-04-12 23:51:02 +00002729 // underlying type. Iteratively strip off typedefs so that we can handle
2730 // typedefs of typedefs.
2731 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2732 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2733 Ty.getUnqualifiedType() == getObjCClassType())
2734 return true;
2735
2736 Ty = TDT->getDecl()->getUnderlyingType();
2737 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002738
Chris Lattner16ede0e2009-04-12 23:51:02 +00002739 return false;
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002740}
2741
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002742/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2743/// garbage collection attribute.
2744///
2745QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002746 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002747 if (getLangOptions().ObjC1 &&
2748 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002749 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002750 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002751 // (or pointers to them) be treated as though they were declared
2752 // as __strong.
2753 if (GCAttrs == QualType::GCNone) {
2754 if (isObjCObjectPointerType(Ty))
2755 GCAttrs = QualType::Strong;
2756 else if (Ty->isPointerType())
2757 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2758 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00002759 // Non-pointers have none gc'able attribute regardless of the attribute
2760 // set on them.
2761 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2762 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002763 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002764 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002765}
2766
Chris Lattner6ac46a42008-04-07 06:51:04 +00002767//===----------------------------------------------------------------------===//
2768// Type Compatibility Testing
2769//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002770
Steve Naroff1c7d0672008-09-04 15:10:53 +00002771/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002772/// block types. Types must be strictly compatible here. For example,
2773/// C unfortunately doesn't produce an error for the following:
2774///
2775/// int (*emptyArgFunc)();
2776/// int (*intArgList)(int) = emptyArgFunc;
2777///
2778/// For blocks, we will produce an error for the following (similar to C++):
2779///
2780/// int (^emptyArgBlock)();
2781/// int (^intArgBlock)(int) = emptyArgBlock;
2782///
2783/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2784///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002785bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002786 const FunctionType *lbase = lhs->getAsFunctionType();
2787 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002788 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2789 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stumpaab0f7a2009-04-01 01:17:39 +00002790 if (lproto && rproto == 0)
2791 return false;
2792 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff1c7d0672008-09-04 15:10:53 +00002793}
2794
Chris Lattner6ac46a42008-04-07 06:51:04 +00002795/// areCompatVectorTypes - Return true if the two specified vector types are
2796/// compatible.
2797static bool areCompatVectorTypes(const VectorType *LHS,
2798 const VectorType *RHS) {
2799 assert(LHS->isCanonical() && RHS->isCanonical());
2800 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002801 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002802}
2803
Eli Friedman3d815e72008-08-22 00:56:42 +00002804/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002805/// compatible for assignment from RHS to LHS. This handles validation of any
2806/// protocol qualifiers on the LHS or RHS.
2807///
Eli Friedman3d815e72008-08-22 00:56:42 +00002808bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2809 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002810 // Verify that the base decls are compatible: the RHS must be a subclass of
2811 // the LHS.
2812 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2813 return false;
2814
2815 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2816 // protocol qualified at all, then we are good.
2817 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2818 return true;
2819
2820 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2821 // isn't a superset.
2822 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2823 return true; // FIXME: should return false!
2824
2825 // Finally, we must have two protocol-qualified interfaces.
2826 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2827 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002828
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002829 // All LHS protocols must have a presence on the RHS.
2830 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002831
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002832 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2833 LHSPE = LHSP->qual_end();
2834 LHSPI != LHSPE; LHSPI++) {
2835 bool RHSImplementsProtocol = false;
2836
2837 // If the RHS doesn't implement the protocol on the left, the types
2838 // are incompatible.
2839 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2840 RHSPE = RHSP->qual_end();
2841 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2842 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2843 RHSImplementsProtocol = true;
2844 }
2845 // FIXME: For better diagnostics, consider passing back the protocol name.
2846 if (!RHSImplementsProtocol)
2847 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002848 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002849 // The RHS implements all protocols listed on the LHS.
2850 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002851}
2852
Steve Naroff389bf462009-02-12 17:52:19 +00002853bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2854 // get the "pointed to" types
2855 const PointerType *LHSPT = LHS->getAsPointerType();
2856 const PointerType *RHSPT = RHS->getAsPointerType();
2857
2858 if (!LHSPT || !RHSPT)
2859 return false;
2860
2861 QualType lhptee = LHSPT->getPointeeType();
2862 QualType rhptee = RHSPT->getPointeeType();
2863 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2864 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2865 // ID acts sort of like void* for ObjC interfaces
2866 if (LHSIface && isObjCIdStructType(rhptee))
2867 return true;
2868 if (RHSIface && isObjCIdStructType(lhptee))
2869 return true;
2870 if (!LHSIface || !RHSIface)
2871 return false;
2872 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2873 canAssignObjCInterfaces(RHSIface, LHSIface);
2874}
2875
Steve Naroffec0550f2007-10-15 20:41:53 +00002876/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2877/// both shall have the identically qualified version of a compatible type.
2878/// C99 6.2.7p1: Two types have compatible types if their types are the
2879/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002880bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2881 return !mergeTypes(LHS, RHS).isNull();
2882}
2883
2884QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2885 const FunctionType *lbase = lhs->getAsFunctionType();
2886 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002887 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2888 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002889 bool allLTypes = true;
2890 bool allRTypes = true;
2891
2892 // Check return type
2893 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2894 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002895 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2896 allLTypes = false;
2897 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2898 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002899
2900 if (lproto && rproto) { // two C99 style function prototypes
2901 unsigned lproto_nargs = lproto->getNumArgs();
2902 unsigned rproto_nargs = rproto->getNumArgs();
2903
2904 // Compatible functions must have the same number of arguments
2905 if (lproto_nargs != rproto_nargs)
2906 return QualType();
2907
2908 // Variadic and non-variadic functions aren't compatible
2909 if (lproto->isVariadic() != rproto->isVariadic())
2910 return QualType();
2911
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002912 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2913 return QualType();
2914
Eli Friedman3d815e72008-08-22 00:56:42 +00002915 // Check argument compatibility
2916 llvm::SmallVector<QualType, 10> types;
2917 for (unsigned i = 0; i < lproto_nargs; i++) {
2918 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2919 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2920 QualType argtype = mergeTypes(largtype, rargtype);
2921 if (argtype.isNull()) return QualType();
2922 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002923 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2924 allLTypes = false;
2925 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2926 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002927 }
2928 if (allLTypes) return lhs;
2929 if (allRTypes) return rhs;
2930 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002931 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002932 }
2933
2934 if (lproto) allRTypes = false;
2935 if (rproto) allLTypes = false;
2936
Douglas Gregor72564e72009-02-26 23:50:07 +00002937 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002938 if (proto) {
2939 if (proto->isVariadic()) return QualType();
2940 // Check that the types are compatible with the types that
2941 // would result from default argument promotions (C99 6.7.5.3p15).
2942 // The only types actually affected are promotable integer
2943 // types and floats, which would be passed as a different
2944 // type depending on whether the prototype is visible.
2945 unsigned proto_nargs = proto->getNumArgs();
2946 for (unsigned i = 0; i < proto_nargs; ++i) {
2947 QualType argTy = proto->getArgType(i);
2948 if (argTy->isPromotableIntegerType() ||
2949 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2950 return QualType();
2951 }
2952
2953 if (allLTypes) return lhs;
2954 if (allRTypes) return rhs;
2955 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002956 proto->getNumArgs(), lproto->isVariadic(),
2957 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002958 }
2959
2960 if (allLTypes) return lhs;
2961 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002962 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002963}
2964
2965QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002966 // C++ [expr]: If an expression initially has the type "reference to T", the
2967 // type is adjusted to "T" prior to any further analysis, the expression
2968 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002969 // expression is an lvalue unless the reference is an rvalue reference and
2970 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00002971 // FIXME: C++ shouldn't be going through here! The rules are different
2972 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002973 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2974 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00002975 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002976 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002977 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002978 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002979
Eli Friedman3d815e72008-08-22 00:56:42 +00002980 QualType LHSCan = getCanonicalType(LHS),
2981 RHSCan = getCanonicalType(RHS);
2982
2983 // If two types are identical, they are compatible.
2984 if (LHSCan == RHSCan)
2985 return LHS;
2986
2987 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002988 // Note that we handle extended qualifiers later, in the
2989 // case for ExtQualType.
2990 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00002991 return QualType();
2992
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00002993 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
2994 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00002995
Chris Lattner1adb8832008-01-14 05:45:46 +00002996 // We want to consider the two function types to be the same for these
2997 // comparisons, just force one to the other.
2998 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2999 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003000
3001 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003002 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3003 LHSClass = Type::ConstantArray;
3004 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3005 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00003006
Nate Begeman213541a2008-04-18 23:10:10 +00003007 // Canonicalize ExtVector -> Vector.
3008 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3009 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00003010
Chris Lattnerb0489812008-04-07 06:38:24 +00003011 // Consider qualified interfaces and interfaces the same.
3012 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
3013 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00003014
Chris Lattnera36a61f2008-04-07 05:43:21 +00003015 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003016 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003017 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3018 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003019
Steve Naroffd824c9c2009-04-14 15:11:46 +00003020 // 'id' and 'Class' act sort of like void* for ObjC interfaces
3021 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003022 return LHS;
Steve Naroffd824c9c2009-04-14 15:11:46 +00003023 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003024 return RHS;
3025
Steve Naroffbc76dd02008-12-10 22:14:21 +00003026 // ID is compatible with all qualified id types.
3027 if (LHS->isObjCQualifiedIdType()) {
3028 if (const PointerType *PT = RHS->getAsPointerType()) {
3029 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003030 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003031 return LHS;
3032 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3033 // Unfortunately, this API is part of Sema (which we don't have access
3034 // to. Need to refactor. The following check is insufficient, since we
3035 // need to make sure the class implements the protocol.
3036 if (pType->isObjCInterfaceType())
3037 return LHS;
3038 }
3039 }
3040 if (RHS->isObjCQualifiedIdType()) {
3041 if (const PointerType *PT = LHS->getAsPointerType()) {
3042 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003043 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003044 return RHS;
3045 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3046 // Unfortunately, this API is part of Sema (which we don't have access
3047 // to. Need to refactor. The following check is insufficient, since we
3048 // need to make sure the class implements the protocol.
3049 if (pType->isObjCInterfaceType())
3050 return RHS;
3051 }
3052 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003053 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3054 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00003055 if (const EnumType* ETy = LHS->getAsEnumType()) {
3056 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3057 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003058 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003059 if (const EnumType* ETy = RHS->getAsEnumType()) {
3060 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3061 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003062 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003063
Eli Friedman3d815e72008-08-22 00:56:42 +00003064 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003065 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003066
Steve Naroff4a746782008-01-09 22:43:08 +00003067 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003068 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003069#define TYPE(Class, Base)
3070#define ABSTRACT_TYPE(Class, Base)
3071#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3072#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3073#include "clang/AST/TypeNodes.def"
3074 assert(false && "Non-canonical and dependent types shouldn't get here");
3075 return QualType();
3076
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003077 case Type::LValueReference:
3078 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003079 case Type::MemberPointer:
3080 assert(false && "C++ should never be in mergeTypes");
3081 return QualType();
3082
3083 case Type::IncompleteArray:
3084 case Type::VariableArray:
3085 case Type::FunctionProto:
3086 case Type::ExtVector:
3087 case Type::ObjCQualifiedInterface:
3088 assert(false && "Types are eliminated above");
3089 return QualType();
3090
Chris Lattner1adb8832008-01-14 05:45:46 +00003091 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003092 {
3093 // Merge two pointer types, while trying to preserve typedef info
3094 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3095 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3096 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3097 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003098 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3099 return LHS;
3100 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3101 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003102 return getPointerType(ResultType);
3103 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003104 case Type::BlockPointer:
3105 {
3106 // Merge two block pointer types, while trying to preserve typedef info
3107 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3108 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3109 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3110 if (ResultType.isNull()) return QualType();
3111 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3112 return LHS;
3113 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3114 return RHS;
3115 return getBlockPointerType(ResultType);
3116 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003117 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003118 {
3119 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3120 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3121 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3122 return QualType();
3123
3124 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3125 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3126 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3127 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003128 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3129 return LHS;
3130 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3131 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003132 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3133 ArrayType::ArraySizeModifier(), 0);
3134 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3135 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003136 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3137 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003138 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3139 return LHS;
3140 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3141 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003142 if (LVAT) {
3143 // FIXME: This isn't correct! But tricky to implement because
3144 // the array's size has to be the size of LHS, but the type
3145 // has to be different.
3146 return LHS;
3147 }
3148 if (RVAT) {
3149 // FIXME: This isn't correct! But tricky to implement because
3150 // the array's size has to be the size of RHS, but the type
3151 // has to be different.
3152 return RHS;
3153 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003154 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3155 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003156 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003157 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003158 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003159 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003160 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003161 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003162 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003163 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3164 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003165 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003166 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003167 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003168 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003169 case Type::Complex:
3170 // Distinct complex types are incompatible.
3171 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003172 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003173 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003174 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3175 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003176 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003177 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003178 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003179 // FIXME: This should be type compatibility, e.g. whether
3180 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003181 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3182 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3183 if (LHSIface && RHSIface &&
3184 canAssignObjCInterfaces(LHSIface, RHSIface))
3185 return LHS;
3186
Eli Friedman3d815e72008-08-22 00:56:42 +00003187 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003188 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003189 case Type::ObjCQualifiedId:
3190 // Distinct qualified id's are not compatible.
3191 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003192 case Type::FixedWidthInt:
3193 // Distinct fixed-width integers are not compatible.
3194 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003195 case Type::ExtQual:
3196 // FIXME: ExtQual types can be compatible even if they're not
3197 // identical!
3198 return QualType();
3199 // First attempt at an implementation, but I'm not really sure it's
3200 // right...
3201#if 0
3202 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3203 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3204 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3205 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3206 return QualType();
3207 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3208 LHSBase = QualType(LQual->getBaseType(), 0);
3209 RHSBase = QualType(RQual->getBaseType(), 0);
3210 ResultType = mergeTypes(LHSBase, RHSBase);
3211 if (ResultType.isNull()) return QualType();
3212 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3213 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3214 return LHS;
3215 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3216 return RHS;
3217 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3218 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3219 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3220 return ResultType;
3221#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003222
3223 case Type::TemplateSpecialization:
3224 assert(false && "Dependent types have no size");
3225 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003226 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003227
3228 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003229}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003230
Chris Lattner5426bf62008-04-07 07:01:58 +00003231//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003232// Integer Predicates
3233//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003234
Eli Friedmanad74a752008-06-28 06:23:08 +00003235unsigned ASTContext::getIntWidth(QualType T) {
3236 if (T == BoolTy)
3237 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003238 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3239 return FWIT->getWidth();
3240 }
3241 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003242 return (unsigned)getTypeSize(T);
3243}
3244
3245QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3246 assert(T->isSignedIntegerType() && "Unexpected type");
3247 if (const EnumType* ETy = T->getAsEnumType())
3248 T = ETy->getDecl()->getIntegerType();
3249 const BuiltinType* BTy = T->getAsBuiltinType();
3250 assert (BTy && "Unexpected signed integer type");
3251 switch (BTy->getKind()) {
3252 case BuiltinType::Char_S:
3253 case BuiltinType::SChar:
3254 return UnsignedCharTy;
3255 case BuiltinType::Short:
3256 return UnsignedShortTy;
3257 case BuiltinType::Int:
3258 return UnsignedIntTy;
3259 case BuiltinType::Long:
3260 return UnsignedLongTy;
3261 case BuiltinType::LongLong:
3262 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00003263 case BuiltinType::Int128:
3264 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00003265 default:
3266 assert(0 && "Unexpected signed integer type");
3267 return QualType();
3268 }
3269}
3270
Douglas Gregor2cf26342009-04-09 22:27:44 +00003271ExternalASTSource::~ExternalASTSource() { }
3272
3273void ExternalASTSource::PrintStats() { }