blob: ac69a38c3dcf2423780ce22b1e324f6254e22b98 [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"
Ted Kremenek7192f8e2007-10-31 17:10:13 +000024#include "llvm/Bitcode/Serialize.h"
25#include "llvm/Bitcode/Deserialize.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000026#include "llvm/Support/MathExtras.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000027#include "llvm/Support/MemoryBuffer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
30enum FloatingRank {
31 FloatRank, DoubleRank, LongDoubleRank
32};
33
Chris Lattner61710852008-10-05 17:34:18 +000034ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
35 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000036 IdentifierTable &idents, SelectorTable &sels,
Steve Naroffc0ac4922009-01-27 23:20:32 +000037 bool FreeMem, unsigned size_reserve) :
Douglas Gregorab452ba2009-03-26 23:50:42 +000038 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
39 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Douglas Gregor2cf26342009-04-09 22:27:44 +000040 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels),
41 ExternalSource(0) {
Daniel Dunbare91593e2008-08-11 04:54:23 +000042 if (size_reserve > 0) Types.reserve(size_reserve);
43 InitBuiltinTypes();
Chris Lattner7644f072009-03-13 22:38:49 +000044 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.NoBuiltin);
Daniel Dunbare91593e2008-08-11 04:54:23 +000045 TUDecl = TranslationUnitDecl::Create(*this);
46}
47
Reid Spencer5f016e22007-07-11 17:01:13 +000048ASTContext::~ASTContext() {
49 // Deallocate all the types.
50 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000051 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000052 Types.pop_back();
53 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000054
Nuno Lopesb74668e2008-12-17 22:30:25 +000055 {
56 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
57 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
58 while (I != E) {
59 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
60 delete R;
61 }
62 }
63
64 {
65 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
66 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
67 while (I != E) {
68 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
69 delete R;
70 }
71 }
72
73 {
Chris Lattner23499252009-03-31 09:24:30 +000074 llvm::DenseMap<const ObjCInterfaceDecl*, RecordDecl*>::iterator
Nuno Lopesb74668e2008-12-17 22:30:25 +000075 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
76 while (I != E) {
Chris Lattner23499252009-03-31 09:24:30 +000077 RecordDecl *R = (I++)->second;
Nuno Lopesb74668e2008-12-17 22:30:25 +000078 R->Destroy(*this);
79 }
80 }
81
Douglas Gregorab452ba2009-03-26 23:50:42 +000082 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000083 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
84 NNS = NestedNameSpecifiers.begin(),
85 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregore7dcd782009-03-27 23:25:45 +000086 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000087 /* Increment in loop */)
88 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000089
90 if (GlobalNestedNameSpecifier)
91 GlobalNestedNameSpecifier->Destroy(*this);
92
Eli Friedmanb26153c2008-05-27 03:08:09 +000093 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000094}
95
Douglas Gregor2cf26342009-04-09 22:27:44 +000096void
97ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
98 ExternalSource.reset(Source.take());
99}
100
Reid Spencer5f016e22007-07-11 17:01:13 +0000101void ASTContext::PrintStats() const {
102 fprintf(stderr, "*** AST Context Stats:\n");
103 fprintf(stderr, " %d types total.\n", (int)Types.size());
104 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000105 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000106 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
107 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
108
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000110 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
111 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000112 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000113 unsigned NumExtQual = 0;
114
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
116 Type *T = Types[i];
117 if (isa<BuiltinType>(T))
118 ++NumBuiltin;
119 else if (isa<PointerType>(T))
120 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000121 else if (isa<BlockPointerType>(T))
122 ++NumBlockPointer;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000123 else if (isa<LValueReferenceType>(T))
124 ++NumLValueReference;
125 else if (isa<RValueReferenceType>(T))
126 ++NumRValueReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000127 else if (isa<MemberPointerType>(T))
128 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000129 else if (isa<ComplexType>(T))
130 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 else if (isa<ArrayType>(T))
132 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000133 else if (isa<VectorType>(T))
134 ++NumVector;
Douglas Gregor72564e72009-02-26 23:50:07 +0000135 else if (isa<FunctionNoProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 ++NumFunctionNP;
Douglas Gregor72564e72009-02-26 23:50:07 +0000137 else if (isa<FunctionProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 ++NumFunctionP;
139 else if (isa<TypedefType>(T))
140 ++NumTypeName;
141 else if (TagType *TT = dyn_cast<TagType>(T)) {
142 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000143 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000145 case TagDecl::TK_struct: ++NumTagStruct; break;
146 case TagDecl::TK_union: ++NumTagUnion; break;
147 case TagDecl::TK_class: ++NumTagClass; break;
148 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000150 } else if (isa<ObjCInterfaceType>(T))
151 ++NumObjCInterfaces;
152 else if (isa<ObjCQualifiedInterfaceType>(T))
153 ++NumObjCQualifiedInterfaces;
154 else if (isa<ObjCQualifiedIdType>(T))
155 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000156 else if (isa<TypeOfType>(T))
157 ++NumTypeOfTypes;
Douglas Gregor72564e72009-02-26 23:50:07 +0000158 else if (isa<TypeOfExprType>(T))
159 ++NumTypeOfExprTypes;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000160 else if (isa<ExtQualType>(T))
161 ++NumExtQual;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000162 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000163 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 assert(0 && "Unknown type!");
165 }
166 }
167
168 fprintf(stderr, " %d builtin types\n", NumBuiltin);
169 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000170 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000171 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
172 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000173 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000174 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000176 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
178 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
179 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
180 fprintf(stderr, " %d tagged types\n", NumTagged);
181 fprintf(stderr, " %d struct types\n", NumTagStruct);
182 fprintf(stderr, " %d union types\n", NumTagUnion);
183 fprintf(stderr, " %d class types\n", NumTagClass);
184 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000185 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000186 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000187 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000188 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000189 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000190 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor72564e72009-02-26 23:50:07 +0000191 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000192 fprintf(stderr, " %d attribute-qualified types\n", NumExtQual);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000193
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
195 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000196 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000197 NumLValueReference*sizeof(LValueReferenceType)+
198 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000199 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000200 NumFunctionP*sizeof(FunctionProtoType)+
201 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000202 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000203 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)+
204 NumExtQual*sizeof(ExtQualType)));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000205
206 if (ExternalSource.get()) {
207 fprintf(stderr, "\n");
208 ExternalSource->PrintStats();
209 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000210}
211
212
213void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000214 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000215}
216
Reid Spencer5f016e22007-07-11 17:01:13 +0000217void ASTContext::InitBuiltinTypes() {
218 assert(VoidTy.isNull() && "Context reinitialized?");
219
220 // C99 6.2.5p19.
221 InitBuiltinType(VoidTy, BuiltinType::Void);
222
223 // C99 6.2.5p2.
224 InitBuiltinType(BoolTy, BuiltinType::Bool);
225 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000226 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000227 InitBuiltinType(CharTy, BuiltinType::Char_S);
228 else
229 InitBuiltinType(CharTy, BuiltinType::Char_U);
230 // C99 6.2.5p4.
231 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
232 InitBuiltinType(ShortTy, BuiltinType::Short);
233 InitBuiltinType(IntTy, BuiltinType::Int);
234 InitBuiltinType(LongTy, BuiltinType::Long);
235 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
236
237 // C99 6.2.5p6.
238 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
239 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
240 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
241 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
242 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
243
244 // C99 6.2.5p10.
245 InitBuiltinType(FloatTy, BuiltinType::Float);
246 InitBuiltinType(DoubleTy, BuiltinType::Double);
247 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000248
Chris Lattner3a250322009-02-26 23:43:47 +0000249 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
250 InitBuiltinType(WCharTy, BuiltinType::WChar);
251 else // C99
252 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000253
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000254 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000255 InitBuiltinType(OverloadTy, BuiltinType::Overload);
256
257 // Placeholder type for type-dependent expressions whose type is
258 // completely unknown. No code should ever check a type against
259 // DependentTy and users should never see it; however, it is here to
260 // help diagnose failures to properly check for type-dependent
261 // expressions.
262 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000263
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 // C99 6.2.5p11.
265 FloatComplexTy = getComplexType(FloatTy);
266 DoubleComplexTy = getComplexType(DoubleTy);
267 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000268
Steve Naroff7e219e42007-10-15 14:41:52 +0000269 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000270 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000271 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000272 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000273 ClassStructType = 0;
274
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000275 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000276
277 // void * type
278 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000279}
280
Chris Lattner464175b2007-07-18 17:52:12 +0000281//===----------------------------------------------------------------------===//
282// Type Sizing and Analysis
283//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000284
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000285/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
286/// scalar floating point type.
287const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
288 const BuiltinType *BT = T->getAsBuiltinType();
289 assert(BT && "Not a floating point type!");
290 switch (BT->getKind()) {
291 default: assert(0 && "Not a floating point type!");
292 case BuiltinType::Float: return Target.getFloatFormat();
293 case BuiltinType::Double: return Target.getDoubleFormat();
294 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
295 }
296}
297
Chris Lattneraf707ab2009-01-24 21:53:27 +0000298/// getDeclAlign - Return a conservative estimate of the alignment of the
299/// specified decl. Note that bitfields do not have a valid alignment, so
300/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000301unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000302 unsigned Align = Target.getCharWidth();
303
304 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
305 Align = std::max(Align, AA->getAlignment());
306
Chris Lattneraf707ab2009-01-24 21:53:27 +0000307 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
308 QualType T = VD->getType();
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000309 if (const ReferenceType* RT = T->getAsReferenceType()) {
310 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000311 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000312 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
313 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000314 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
315 T = cast<ArrayType>(T)->getElementType();
316
317 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
318 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000319 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000320
321 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000322}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000323
Chris Lattnera7674d82007-07-13 22:13:22 +0000324/// getTypeSize - Return the size of the specified type, in bits. This method
325/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000326std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000327ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000328 T = getCanonicalType(T);
Mike Stump5e301002009-02-27 18:32:39 +0000329 uint64_t Width=0;
330 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000331 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000332#define TYPE(Class, Base)
333#define ABSTRACT_TYPE(Class, Base)
334#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
335#define DEPENDENT_TYPE(Class, Base) case Type::Class:
336#include "clang/AST/TypeNodes.def"
337 assert(false && "Should not see non-canonical or dependent types");
338 break;
339
Chris Lattner692233e2007-07-13 22:27:08 +0000340 case Type::FunctionNoProto:
341 case Type::FunctionProto:
Douglas Gregor72564e72009-02-26 23:50:07 +0000342 case Type::IncompleteArray:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000343 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000344 case Type::VariableArray:
345 assert(0 && "VLAs not implemented yet!");
346 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000347 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000348
Chris Lattner98be4942008-03-05 18:54:05 +0000349 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000350 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000351 Align = EltInfo.second;
352 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000353 }
Nate Begeman213541a2008-04-18 23:10:10 +0000354 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000355 case Type::Vector: {
356 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000357 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000358 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000359 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000360 // If the alignment is not a power of 2, round up to the next power of 2.
361 // This happens for non-power-of-2 length vectors.
362 // FIXME: this should probably be a target property.
363 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000364 break;
365 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000366
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000367 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000368 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000369 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000370 case BuiltinType::Void:
371 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000372 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000373 Width = Target.getBoolWidth();
374 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000375 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000376 case BuiltinType::Char_S:
377 case BuiltinType::Char_U:
378 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000379 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000380 Width = Target.getCharWidth();
381 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000382 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000383 case BuiltinType::WChar:
384 Width = Target.getWCharWidth();
385 Align = Target.getWCharAlign();
386 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000387 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000388 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000389 Width = Target.getShortWidth();
390 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000391 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000392 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000393 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000394 Width = Target.getIntWidth();
395 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000396 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000397 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000398 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000399 Width = Target.getLongWidth();
400 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000401 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000402 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000403 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000404 Width = Target.getLongLongWidth();
405 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000406 break;
407 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000408 Width = Target.getFloatWidth();
409 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000410 break;
411 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000412 Width = Target.getDoubleWidth();
413 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000414 break;
415 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000416 Width = Target.getLongDoubleWidth();
417 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000418 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000419 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000420 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000421 case Type::FixedWidthInt:
422 // FIXME: This isn't precisely correct; the width/alignment should depend
423 // on the available types for the target
424 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000425 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000426 Align = Width;
427 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000428 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000429 // FIXME: Pointers into different addr spaces could have different sizes and
430 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000431 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000432 case Type::ObjCQualifiedId:
Eli Friedman4bdf0872009-02-22 04:02:33 +0000433 case Type::ObjCQualifiedClass:
Douglas Gregor72564e72009-02-26 23:50:07 +0000434 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000435 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000436 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000437 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000438 case Type::BlockPointer: {
439 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
440 Width = Target.getPointerWidth(AS);
441 Align = Target.getPointerAlign(AS);
442 break;
443 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000444 case Type::Pointer: {
445 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000446 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000447 Align = Target.getPointerAlign(AS);
448 break;
449 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000450 case Type::LValueReference:
451 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000452 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000453 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000454 // FIXME: This is wrong for struct layout: a reference in a struct has
455 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000456 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000457 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000458 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000459 // the GCC ABI, where pointers to data are one pointer large, pointers to
460 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000461 // other compilers too, we need to delegate this completely to TargetInfo
462 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000463 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
464 unsigned AS = Pointee.getAddressSpace();
465 Width = Target.getPointerWidth(AS);
466 if (Pointee->isFunctionType())
467 Width *= 2;
468 Align = Target.getPointerAlign(AS);
469 // GCC aligns at single pointer width.
470 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000471 case Type::Complex: {
472 // Complex types have the same alignment as their elements, but twice the
473 // size.
474 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000475 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000476 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000477 Align = EltInfo.second;
478 break;
479 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000480 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000481 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000482 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
483 Width = Layout.getSize();
484 Align = Layout.getAlignment();
485 break;
486 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000487 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000488 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000489 const TagType *TT = cast<TagType>(T);
490
491 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000492 Width = 1;
493 Align = 1;
494 break;
495 }
496
Daniel Dunbar1d751182008-11-08 05:48:37 +0000497 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000498 return getTypeInfo(ET->getDecl()->getIntegerType());
499
Daniel Dunbar1d751182008-11-08 05:48:37 +0000500 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000501 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
502 Width = Layout.getSize();
503 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000504 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000505 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000506
507 case Type::TemplateSpecialization:
508 assert(false && "Dependent types have no size");
509 break;
Chris Lattner71763312008-04-06 22:05:18 +0000510 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000511
Chris Lattner464175b2007-07-18 17:52:12 +0000512 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000513 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000514}
515
Chris Lattner34ebde42009-01-27 18:08:34 +0000516/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
517/// type for the current target in bits. This can be different than the ABI
518/// alignment in cases where it is beneficial for performance to overalign
519/// a data type.
520unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
521 unsigned ABIAlign = getTypeAlign(T);
522
523 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000524 if (T->isSpecificBuiltinType(BuiltinType::Double))
525 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000526
527 return ABIAlign;
528}
529
530
Devang Patel8b277042008-06-04 21:22:16 +0000531/// LayoutField - Field layout.
532void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000533 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000534 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000535 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000536 uint64_t FieldOffset = IsUnion ? 0 : Size;
537 uint64_t FieldSize;
538 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000539
540 // FIXME: Should this override struct packing? Probably we want to
541 // take the minimum?
542 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
543 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000544
545 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
546 // TODO: Need to check this algorithm on other targets!
547 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000548 FieldSize =
549 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000550
551 std::pair<uint64_t, unsigned> FieldInfo =
552 Context.getTypeInfo(FD->getType());
553 uint64_t TypeSize = FieldInfo.first;
554
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000555 // Determine the alignment of this bitfield. The packing
556 // attributes define a maximum and the alignment attribute defines
557 // a minimum.
558 // FIXME: What is the right behavior when the specified alignment
559 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000560 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000561 if (FieldPacking)
562 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000563 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
564 FieldAlign = std::max(FieldAlign, AA->getAlignment());
565
566 // Check if we need to add padding to give the field the correct
567 // alignment.
568 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
569 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
570
571 // Padding members don't affect overall alignment
572 if (!FD->getIdentifier())
573 FieldAlign = 1;
574 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000575 if (FD->getType()->isIncompleteArrayType()) {
576 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000577 // query getTypeInfo about these, so we figure it out here.
578 // Flexible array members don't have any size, but they
579 // have to be aligned appropriately for their element type.
580 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000581 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000582 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson2f1169f2009-04-10 05:31:15 +0000583 } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
584 unsigned AS = RT->getPointeeType().getAddressSpace();
585 FieldSize = Context.Target.getPointerWidth(AS);
586 FieldAlign = Context.Target.getPointerAlign(AS);
Devang Patel8b277042008-06-04 21:22:16 +0000587 } else {
588 std::pair<uint64_t, unsigned> FieldInfo =
589 Context.getTypeInfo(FD->getType());
590 FieldSize = FieldInfo.first;
591 FieldAlign = FieldInfo.second;
592 }
593
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000594 // Determine the alignment of this bitfield. The packing
595 // attributes define a maximum and the alignment attribute defines
596 // a minimum. Additionally, the packing alignment must be at least
597 // a byte for non-bitfields.
598 //
599 // FIXME: What is the right behavior when the specified alignment
600 // is smaller than the specified packing?
601 if (FieldPacking)
602 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000603 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
604 FieldAlign = std::max(FieldAlign, AA->getAlignment());
605
606 // Round up the current record size to the field's alignment boundary.
607 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
608 }
609
610 // Place this field at the current location.
611 FieldOffsets[FieldNo] = FieldOffset;
612
613 // Reserve space for this field.
614 if (IsUnion) {
615 Size = std::max(Size, FieldSize);
616 } else {
617 Size = FieldOffset + FieldSize;
618 }
619
620 // Remember max struct/class alignment.
621 Alignment = std::max(Alignment, FieldAlign);
622}
623
Fariborz Jahanian88e469c2009-03-05 20:08:48 +0000624void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
Douglas Gregor6ab35242009-04-09 21:40:53 +0000625 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000626 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
627 if (SuperClass)
628 CollectObjCIvars(SuperClass, Fields);
629 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
630 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000631 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000632 if (!IVDecl->isInvalidDecl())
633 Fields.push_back(cast<FieldDecl>(IVDecl));
634 }
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +0000635 // look into properties.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000636 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*this),
637 E = OI->prop_end(*this); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000638 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +0000639 Fields.push_back(cast<FieldDecl>(IV));
640 }
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000641}
642
643/// addRecordToClass - produces record info. for the class for its
644/// ivars and all those inherited.
645///
Chris Lattnerf1690852009-03-31 08:48:01 +0000646const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D) {
Chris Lattner23499252009-03-31 09:24:30 +0000647 RecordDecl *&RD = ASTRecordForInterface[D];
648 if (RD) {
649 // If we have a record decl already and it is either a definition or if 'D'
650 // is still a forward declaration, return it.
651 if (RD->isDefinition() || D->isForwardDecl())
652 return RD;
653 }
654
655 // If D is a forward declaration, then just make a forward struct decl.
656 if (D->isForwardDecl())
657 return RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
658 D->getLocation(),
659 D->getIdentifier());
Chris Lattnerf1690852009-03-31 08:48:01 +0000660
661 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000662 CollectObjCIvars(D, RecFields);
Chris Lattner23499252009-03-31 09:24:30 +0000663
664 if (RD == 0)
665 RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, D->getLocation(),
666 D->getIdentifier());
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000667 /// FIXME! Can do collection of ivars and adding to the record while
668 /// doing it.
Chris Lattner16ff7052009-03-31 08:58:42 +0000669 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000670 RD->addDecl(*this,
671 FieldDecl::Create(*this, RD,
Chris Lattner23499252009-03-31 09:24:30 +0000672 RecFields[i]->getLocation(),
673 RecFields[i]->getIdentifier(),
674 RecFields[i]->getType(),
675 RecFields[i]->getBitWidth(), false));
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000676 }
Chris Lattnerf1690852009-03-31 08:48:01 +0000677
Chris Lattner23499252009-03-31 09:24:30 +0000678 RD->completeDefinition(*this);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000679 return RD;
680}
Devang Patel44a3dde2008-06-04 21:54:36 +0000681
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000682/// setFieldDecl - maps a field for the given Ivar reference node.
683//
684void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
685 const ObjCIvarDecl *Ivar,
686 const ObjCIvarRefExpr *MRef) {
Chris Lattnerda046392009-03-31 08:31:13 +0000687 ASTFieldForIvarRef[MRef] = OI->lookupFieldDeclForIvar(*this, Ivar);
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000688}
689
Chris Lattner61710852008-10-05 17:34:18 +0000690/// getASTObjcInterfaceLayout - Get or compute information about the layout of
691/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000692/// position information.
693const ASTRecordLayout &
694ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
695 // Look up this layout, if already laid out, return what we have.
696 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
697 if (Entry) return *Entry;
698
699 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
700 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000701 ASTRecordLayout *NewEntry = NULL;
Fariborz Jahanian0c7ce5b2009-04-08 21:54:52 +0000702 // FIXME. Add actual count of synthesized ivars, instead of count
703 // of properties which is the upper bound, but is safe.
Daniel Dunbar4af44122009-04-08 20:18:15 +0000704 unsigned FieldCount =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000705 D->ivar_size() + std::distance(D->prop_begin(*this), D->prop_end(*this));
Devang Patel6a5a34c2008-06-06 02:14:01 +0000706 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
707 FieldCount++;
708 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
709 unsigned Alignment = SL.getAlignment();
710 uint64_t Size = SL.getSize();
711 NewEntry = new ASTRecordLayout(Size, Alignment);
712 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000713 // Super class is at the beginning of the layout.
714 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000715 } else {
716 NewEntry = new ASTRecordLayout();
717 NewEntry->InitializeLayout(FieldCount);
718 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000719 Entry = NewEntry;
720
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000721 unsigned StructPacking = 0;
722 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
723 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000724
725 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
726 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
727 AA->getAlignment()));
728
729 // Layout each ivar sequentially.
730 unsigned i = 0;
731 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
732 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
733 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000734 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000735 }
Fariborz Jahanian18191882009-03-31 18:11:23 +0000736 // Also synthesized ivars
Douglas Gregor6ab35242009-04-09 21:40:53 +0000737 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
738 E = D->prop_end(*this); I != E; ++I) {
Fariborz Jahanian18191882009-03-31 18:11:23 +0000739 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
740 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
741 }
Fariborz Jahanian99eee362009-04-01 19:37:34 +0000742
Devang Patel44a3dde2008-06-04 21:54:36 +0000743 // Finally, round the size of the total struct up to the alignment of the
744 // struct itself.
745 NewEntry->FinalizeLayout();
746 return *NewEntry;
747}
748
Devang Patel88a981b2007-11-01 19:11:01 +0000749/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000750/// specified record (struct/union/class), which indicates its size and field
751/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000752const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000753 D = D->getDefinition(*this);
754 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000755
Chris Lattner464175b2007-07-18 17:52:12 +0000756 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000757 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000758 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000759
Devang Patel88a981b2007-11-01 19:11:01 +0000760 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
761 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
762 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000763 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000764
Douglas Gregore267ff32008-12-11 20:41:00 +0000765 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000766 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
767 D->field_end(*this)));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000768 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000769
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000770 unsigned StructPacking = 0;
771 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
772 StructPacking = PA->getAlignment();
773
Eli Friedman4bd998b2008-05-30 09:31:38 +0000774 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000775 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
776 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000777
Eli Friedman4bd998b2008-05-30 09:31:38 +0000778 // Layout each field, for now, just sequentially, respecting alignment. In
779 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000780 unsigned FieldIdx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000781 for (RecordDecl::field_iterator Field = D->field_begin(*this),
782 FieldEnd = D->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000783 Field != FieldEnd; (void)++Field, ++FieldIdx)
784 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000785
786 // Finally, round the size of the total struct up to the alignment of the
787 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000788 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000789 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000790}
791
Chris Lattnera7674d82007-07-13 22:13:22 +0000792//===----------------------------------------------------------------------===//
793// Type creation/memoization methods
794//===----------------------------------------------------------------------===//
795
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000796QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000797 QualType CanT = getCanonicalType(T);
798 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000799 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000800
801 // If we are composing extended qualifiers together, merge together into one
802 // ExtQualType node.
803 unsigned CVRQuals = T.getCVRQualifiers();
804 QualType::GCAttrTypes GCAttr = QualType::GCNone;
805 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000806
Chris Lattnerb7d25532009-02-18 22:53:11 +0000807 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
808 // If this type already has an address space specified, it cannot get
809 // another one.
810 assert(EQT->getAddressSpace() == 0 &&
811 "Type cannot be in multiple addr spaces!");
812 GCAttr = EQT->getObjCGCAttr();
813 TypeNode = EQT->getBaseType();
814 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000815
Chris Lattnerb7d25532009-02-18 22:53:11 +0000816 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000817 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000818 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000819 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000820 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000821 return QualType(EXTQy, CVRQuals);
822
Christopher Lambebb97e92008-02-04 02:31:56 +0000823 // If the base type isn't canonical, this won't be a canonical type either,
824 // so fill in the canonical type field.
825 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000826 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000827 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000828
Chris Lattnerb7d25532009-02-18 22:53:11 +0000829 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000830 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000831 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000832 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000833 ExtQualType *New =
834 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000835 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000836 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000837 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000838}
839
Chris Lattnerb7d25532009-02-18 22:53:11 +0000840QualType ASTContext::getObjCGCQualType(QualType T,
841 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000842 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000843 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000844 return T;
845
Chris Lattnerb7d25532009-02-18 22:53:11 +0000846 // If we are composing extended qualifiers together, merge together into one
847 // ExtQualType node.
848 unsigned CVRQuals = T.getCVRQualifiers();
849 Type *TypeNode = T.getTypePtr();
850 unsigned AddressSpace = 0;
851
852 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
853 // If this type already has an address space specified, it cannot get
854 // another one.
855 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
856 "Type cannot be in multiple addr spaces!");
857 AddressSpace = EQT->getAddressSpace();
858 TypeNode = EQT->getBaseType();
859 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000860
861 // Check if we've already instantiated an gc qual'd type of this type.
862 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000863 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000864 void *InsertPos = 0;
865 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000866 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000867
868 // If the base type isn't canonical, this won't be a canonical type either,
869 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000870 // FIXME: Isn't this also not canonical if the base type is a array
871 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000872 QualType Canonical;
873 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000874 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000875
Chris Lattnerb7d25532009-02-18 22:53:11 +0000876 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000877 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
878 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
879 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000880 ExtQualType *New =
881 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000882 ExtQualTypes.InsertNode(New, InsertPos);
883 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000884 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000885}
Chris Lattnera7674d82007-07-13 22:13:22 +0000886
Reid Spencer5f016e22007-07-11 17:01:13 +0000887/// getComplexType - Return the uniqued reference to the type for a complex
888/// number with the specified element type.
889QualType ASTContext::getComplexType(QualType T) {
890 // Unique pointers, to guarantee there is only one pointer of a particular
891 // structure.
892 llvm::FoldingSetNodeID ID;
893 ComplexType::Profile(ID, T);
894
895 void *InsertPos = 0;
896 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
897 return QualType(CT, 0);
898
899 // If the pointee type isn't canonical, this won't be a canonical type either,
900 // so fill in the canonical type field.
901 QualType Canonical;
902 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000903 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000904
905 // Get the new insert position for the node we care about.
906 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000907 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000908 }
Steve Narofff83820b2009-01-27 22:08:43 +0000909 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 Types.push_back(New);
911 ComplexTypes.InsertNode(New, InsertPos);
912 return QualType(New, 0);
913}
914
Eli Friedmanf98aba32009-02-13 02:31:07 +0000915QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
916 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
917 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
918 FixedWidthIntType *&Entry = Map[Width];
919 if (!Entry)
920 Entry = new FixedWidthIntType(Width, Signed);
921 return QualType(Entry, 0);
922}
Reid Spencer5f016e22007-07-11 17:01:13 +0000923
924/// getPointerType - Return the uniqued reference to the type for a pointer to
925/// the specified type.
926QualType ASTContext::getPointerType(QualType T) {
927 // Unique pointers, to guarantee there is only one pointer of a particular
928 // structure.
929 llvm::FoldingSetNodeID ID;
930 PointerType::Profile(ID, T);
931
932 void *InsertPos = 0;
933 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
934 return QualType(PT, 0);
935
936 // If the pointee type isn't canonical, this won't be a canonical type either,
937 // so fill in the canonical type field.
938 QualType Canonical;
939 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000940 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000941
942 // Get the new insert position for the node we care about.
943 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000944 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 }
Steve Narofff83820b2009-01-27 22:08:43 +0000946 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000947 Types.push_back(New);
948 PointerTypes.InsertNode(New, InsertPos);
949 return QualType(New, 0);
950}
951
Steve Naroff5618bd42008-08-27 16:04:49 +0000952/// getBlockPointerType - Return the uniqued reference to the type for
953/// a pointer to the specified block.
954QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000955 assert(T->isFunctionType() && "block of function types only");
956 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000957 // structure.
958 llvm::FoldingSetNodeID ID;
959 BlockPointerType::Profile(ID, T);
960
961 void *InsertPos = 0;
962 if (BlockPointerType *PT =
963 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
964 return QualType(PT, 0);
965
Steve Naroff296e8d52008-08-28 19:20:44 +0000966 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000967 // type either so fill in the canonical type field.
968 QualType Canonical;
969 if (!T->isCanonical()) {
970 Canonical = getBlockPointerType(getCanonicalType(T));
971
972 // Get the new insert position for the node we care about.
973 BlockPointerType *NewIP =
974 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000975 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000976 }
Steve Narofff83820b2009-01-27 22:08:43 +0000977 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000978 Types.push_back(New);
979 BlockPointerTypes.InsertNode(New, InsertPos);
980 return QualType(New, 0);
981}
982
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000983/// getLValueReferenceType - Return the uniqued reference to the type for an
984/// lvalue reference to the specified type.
985QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000986 // Unique pointers, to guarantee there is only one pointer of a particular
987 // structure.
988 llvm::FoldingSetNodeID ID;
989 ReferenceType::Profile(ID, T);
990
991 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000992 if (LValueReferenceType *RT =
993 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000994 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000995
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 // If the referencee type isn't canonical, this won't be a canonical type
997 // either, so fill in the canonical type field.
998 QualType Canonical;
999 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001000 Canonical = getLValueReferenceType(getCanonicalType(T));
1001
Reid Spencer5f016e22007-07-11 17:01:13 +00001002 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001003 LValueReferenceType *NewIP =
1004 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001005 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001006 }
1007
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001008 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001010 LValueReferenceTypes.InsertNode(New, InsertPos);
1011 return QualType(New, 0);
1012}
1013
1014/// getRValueReferenceType - Return the uniqued reference to the type for an
1015/// rvalue reference to the specified type.
1016QualType ASTContext::getRValueReferenceType(QualType T) {
1017 // Unique pointers, to guarantee there is only one pointer of a particular
1018 // structure.
1019 llvm::FoldingSetNodeID ID;
1020 ReferenceType::Profile(ID, T);
1021
1022 void *InsertPos = 0;
1023 if (RValueReferenceType *RT =
1024 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1025 return QualType(RT, 0);
1026
1027 // If the referencee type isn't canonical, this won't be a canonical type
1028 // either, so fill in the canonical type field.
1029 QualType Canonical;
1030 if (!T->isCanonical()) {
1031 Canonical = getRValueReferenceType(getCanonicalType(T));
1032
1033 // Get the new insert position for the node we care about.
1034 RValueReferenceType *NewIP =
1035 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1036 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1037 }
1038
1039 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1040 Types.push_back(New);
1041 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001042 return QualType(New, 0);
1043}
1044
Sebastian Redlf30208a2009-01-24 21:16:55 +00001045/// getMemberPointerType - Return the uniqued reference to the type for a
1046/// member pointer to the specified type, in the specified class.
1047QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1048{
1049 // Unique pointers, to guarantee there is only one pointer of a particular
1050 // structure.
1051 llvm::FoldingSetNodeID ID;
1052 MemberPointerType::Profile(ID, T, Cls);
1053
1054 void *InsertPos = 0;
1055 if (MemberPointerType *PT =
1056 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1057 return QualType(PT, 0);
1058
1059 // If the pointee or class type isn't canonical, this won't be a canonical
1060 // type either, so fill in the canonical type field.
1061 QualType Canonical;
1062 if (!T->isCanonical()) {
1063 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1064
1065 // Get the new insert position for the node we care about.
1066 MemberPointerType *NewIP =
1067 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1068 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1069 }
Steve Narofff83820b2009-01-27 22:08:43 +00001070 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001071 Types.push_back(New);
1072 MemberPointerTypes.InsertNode(New, InsertPos);
1073 return QualType(New, 0);
1074}
1075
Steve Narofffb22d962007-08-30 01:06:46 +00001076/// getConstantArrayType - Return the unique reference to the type for an
1077/// array of the specified element type.
1078QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +00001079 const llvm::APInt &ArySize,
1080 ArrayType::ArraySizeModifier ASM,
1081 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001082 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001083 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001084
1085 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001086 if (ConstantArrayType *ATP =
1087 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 return QualType(ATP, 0);
1089
1090 // If the element type isn't canonical, this won't be a canonical type either,
1091 // so fill in the canonical type field.
1092 QualType Canonical;
1093 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001094 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001095 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001096 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001097 ConstantArrayType *NewIP =
1098 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001099 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 }
1101
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001102 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001103 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001104 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001105 Types.push_back(New);
1106 return QualType(New, 0);
1107}
1108
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001109/// getVariableArrayType - Returns a non-unique reference to the type for a
1110/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001111QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1112 ArrayType::ArraySizeModifier ASM,
1113 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001114 // Since we don't unique expressions, it isn't possible to unique VLA's
1115 // that have an expression provided for their size.
1116
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001117 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001118 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001119
1120 VariableArrayTypes.push_back(New);
1121 Types.push_back(New);
1122 return QualType(New, 0);
1123}
1124
Douglas Gregor898574e2008-12-05 23:32:09 +00001125/// getDependentSizedArrayType - Returns a non-unique reference to
1126/// the type for a dependently-sized array of the specified element
1127/// type. FIXME: We will need these to be uniqued, or at least
1128/// comparable, at some point.
1129QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1130 ArrayType::ArraySizeModifier ASM,
1131 unsigned EltTypeQuals) {
1132 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1133 "Size must be type- or value-dependent!");
1134
1135 // Since we don't unique expressions, it isn't possible to unique
1136 // dependently-sized array types.
1137
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001138 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001139 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1140 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001141
1142 DependentSizedArrayTypes.push_back(New);
1143 Types.push_back(New);
1144 return QualType(New, 0);
1145}
1146
Eli Friedmanc5773c42008-02-15 18:16:39 +00001147QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1148 ArrayType::ArraySizeModifier ASM,
1149 unsigned EltTypeQuals) {
1150 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001151 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001152
1153 void *InsertPos = 0;
1154 if (IncompleteArrayType *ATP =
1155 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1156 return QualType(ATP, 0);
1157
1158 // If the element type isn't canonical, this won't be a canonical type
1159 // either, so fill in the canonical type field.
1160 QualType Canonical;
1161
1162 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001163 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001164 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001165
1166 // Get the new insert position for the node we care about.
1167 IncompleteArrayType *NewIP =
1168 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001169 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001170 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001171
Steve Narofff83820b2009-01-27 22:08:43 +00001172 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001173 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001174
1175 IncompleteArrayTypes.InsertNode(New, InsertPos);
1176 Types.push_back(New);
1177 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001178}
1179
Steve Naroff73322922007-07-18 18:00:27 +00001180/// getVectorType - Return the unique reference to a vector type of
1181/// the specified element type and size. VectorType must be a built-in type.
1182QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 BuiltinType *baseType;
1184
Chris Lattnerf52ab252008-04-06 22:59:24 +00001185 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001186 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001187
1188 // Check if we've already instantiated a vector of this type.
1189 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001190 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 void *InsertPos = 0;
1192 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1193 return QualType(VTP, 0);
1194
1195 // If the element type isn't canonical, this won't be a canonical type either,
1196 // so fill in the canonical type field.
1197 QualType Canonical;
1198 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001199 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001200
1201 // Get the new insert position for the node we care about.
1202 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001203 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 }
Steve Narofff83820b2009-01-27 22:08:43 +00001205 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 VectorTypes.InsertNode(New, InsertPos);
1207 Types.push_back(New);
1208 return QualType(New, 0);
1209}
1210
Nate Begeman213541a2008-04-18 23:10:10 +00001211/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001212/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001213QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001214 BuiltinType *baseType;
1215
Chris Lattnerf52ab252008-04-06 22:59:24 +00001216 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001217 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001218
1219 // Check if we've already instantiated a vector of this type.
1220 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001221 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001222 void *InsertPos = 0;
1223 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1224 return QualType(VTP, 0);
1225
1226 // If the element type isn't canonical, this won't be a canonical type either,
1227 // so fill in the canonical type field.
1228 QualType Canonical;
1229 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001230 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001231
1232 // Get the new insert position for the node we care about.
1233 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001234 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001235 }
Steve Narofff83820b2009-01-27 22:08:43 +00001236 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001237 VectorTypes.InsertNode(New, InsertPos);
1238 Types.push_back(New);
1239 return QualType(New, 0);
1240}
1241
Douglas Gregor72564e72009-02-26 23:50:07 +00001242/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001243///
Douglas Gregor72564e72009-02-26 23:50:07 +00001244QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 // Unique functions, to guarantee there is only one function of a particular
1246 // structure.
1247 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001248 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001249
1250 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001251 if (FunctionNoProtoType *FT =
1252 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 return QualType(FT, 0);
1254
1255 QualType Canonical;
1256 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001257 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001258
1259 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001260 FunctionNoProtoType *NewIP =
1261 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001262 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 }
1264
Douglas Gregor72564e72009-02-26 23:50:07 +00001265 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001266 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001267 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001268 return QualType(New, 0);
1269}
1270
1271/// getFunctionType - Return a normal function type with a typed argument
1272/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001273QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001274 unsigned NumArgs, bool isVariadic,
1275 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001276 // Unique functions, to guarantee there is only one function of a particular
1277 // structure.
1278 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001279 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001280 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001281
1282 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001283 if (FunctionProtoType *FTP =
1284 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001285 return QualType(FTP, 0);
1286
1287 // Determine whether the type being created is already canonical or not.
1288 bool isCanonical = ResultTy->isCanonical();
1289 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1290 if (!ArgArray[i]->isCanonical())
1291 isCanonical = false;
1292
1293 // If this type isn't canonical, get the canonical version of it.
1294 QualType Canonical;
1295 if (!isCanonical) {
1296 llvm::SmallVector<QualType, 16> CanonicalArgs;
1297 CanonicalArgs.reserve(NumArgs);
1298 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001299 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001300
Chris Lattnerf52ab252008-04-06 22:59:24 +00001301 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001302 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001303 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001304
1305 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001306 FunctionProtoType *NewIP =
1307 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001308 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001309 }
1310
Douglas Gregor72564e72009-02-26 23:50:07 +00001311 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001312 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001313 FunctionProtoType *FTP =
1314 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001315 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001316 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001317 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001318 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001319 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001320 return QualType(FTP, 0);
1321}
1322
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001323/// getTypeDeclType - Return the unique reference to the type for the
1324/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001325QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001326 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001327 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1328
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001329 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001330 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001331 else if (isa<TemplateTypeParmDecl>(Decl)) {
1332 assert(false && "Template type parameter types are always available.");
1333 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001334 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001335
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001336 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001337 if (PrevDecl)
1338 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001339 else
1340 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001341 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001342 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1343 if (PrevDecl)
1344 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001345 else
1346 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001347 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001348 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001349 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001350
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001351 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001352 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001353}
1354
Reid Spencer5f016e22007-07-11 17:01:13 +00001355/// getTypedefType - Return the unique reference to the type for the
1356/// specified typename decl.
1357QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1358 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1359
Chris Lattnerf52ab252008-04-06 22:59:24 +00001360 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001361 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001362 Types.push_back(Decl->TypeForDecl);
1363 return QualType(Decl->TypeForDecl, 0);
1364}
1365
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001366/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001367/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001368QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001369 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1370
Steve Narofff83820b2009-01-27 22:08:43 +00001371 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001372 Types.push_back(Decl->TypeForDecl);
1373 return QualType(Decl->TypeForDecl, 0);
1374}
1375
Douglas Gregorfab9d672009-02-05 23:33:38 +00001376/// \brief Retrieve the template type parameter type for a template
1377/// parameter with the given depth, index, and (optionally) name.
1378QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1379 IdentifierInfo *Name) {
1380 llvm::FoldingSetNodeID ID;
1381 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1382 void *InsertPos = 0;
1383 TemplateTypeParmType *TypeParm
1384 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1385
1386 if (TypeParm)
1387 return QualType(TypeParm, 0);
1388
1389 if (Name)
1390 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1391 getTemplateTypeParmType(Depth, Index));
1392 else
1393 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1394
1395 Types.push_back(TypeParm);
1396 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1397
1398 return QualType(TypeParm, 0);
1399}
1400
Douglas Gregor55f6b142009-02-09 18:46:07 +00001401QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001402ASTContext::getTemplateSpecializationType(TemplateName Template,
1403 const TemplateArgument *Args,
1404 unsigned NumArgs,
1405 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001406 if (!Canon.isNull())
1407 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001408
Douglas Gregor55f6b142009-02-09 18:46:07 +00001409 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001410 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001411
Douglas Gregor55f6b142009-02-09 18:46:07 +00001412 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001413 TemplateSpecializationType *Spec
1414 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001415
1416 if (Spec)
1417 return QualType(Spec, 0);
1418
Douglas Gregor7532dc62009-03-30 22:58:21 +00001419 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001420 sizeof(TemplateArgument) * NumArgs),
1421 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001422 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001423 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001424 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001425
1426 return QualType(Spec, 0);
1427}
1428
Douglas Gregore4e5b052009-03-19 00:18:19 +00001429QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001430ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001431 QualType NamedType) {
1432 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001433 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001434
1435 void *InsertPos = 0;
1436 QualifiedNameType *T
1437 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1438 if (T)
1439 return QualType(T, 0);
1440
Douglas Gregorab452ba2009-03-26 23:50:42 +00001441 T = new (*this) QualifiedNameType(NNS, NamedType,
1442 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001443 Types.push_back(T);
1444 QualifiedNameTypes.InsertNode(T, InsertPos);
1445 return QualType(T, 0);
1446}
1447
Douglas Gregord57959a2009-03-27 23:10:48 +00001448QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1449 const IdentifierInfo *Name,
1450 QualType Canon) {
1451 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1452
1453 if (Canon.isNull()) {
1454 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1455 if (CanonNNS != NNS)
1456 Canon = getTypenameType(CanonNNS, Name);
1457 }
1458
1459 llvm::FoldingSetNodeID ID;
1460 TypenameType::Profile(ID, NNS, Name);
1461
1462 void *InsertPos = 0;
1463 TypenameType *T
1464 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1465 if (T)
1466 return QualType(T, 0);
1467
1468 T = new (*this) TypenameType(NNS, Name, Canon);
1469 Types.push_back(T);
1470 TypenameTypes.InsertNode(T, InsertPos);
1471 return QualType(T, 0);
1472}
1473
Douglas Gregor17343172009-04-01 00:28:59 +00001474QualType
1475ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1476 const TemplateSpecializationType *TemplateId,
1477 QualType Canon) {
1478 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1479
1480 if (Canon.isNull()) {
1481 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1482 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1483 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1484 const TemplateSpecializationType *CanonTemplateId
1485 = CanonType->getAsTemplateSpecializationType();
1486 assert(CanonTemplateId &&
1487 "Canonical type must also be a template specialization type");
1488 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1489 }
1490 }
1491
1492 llvm::FoldingSetNodeID ID;
1493 TypenameType::Profile(ID, NNS, TemplateId);
1494
1495 void *InsertPos = 0;
1496 TypenameType *T
1497 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1498 if (T)
1499 return QualType(T, 0);
1500
1501 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1502 Types.push_back(T);
1503 TypenameTypes.InsertNode(T, InsertPos);
1504 return QualType(T, 0);
1505}
1506
Chris Lattner88cb27a2008-04-07 04:56:42 +00001507/// CmpProtocolNames - Comparison predicate for sorting protocols
1508/// alphabetically.
1509static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1510 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001511 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001512}
1513
1514static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1515 unsigned &NumProtocols) {
1516 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1517
1518 // Sort protocols, keyed by name.
1519 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1520
1521 // Remove duplicates.
1522 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1523 NumProtocols = ProtocolsEnd-Protocols;
1524}
1525
1526
Chris Lattner065f0d72008-04-07 04:44:08 +00001527/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1528/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001529QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1530 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001531 // Sort the protocol list alphabetically to canonicalize it.
1532 SortAndUniqueProtocols(Protocols, NumProtocols);
1533
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001534 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001535 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001536
1537 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001538 if (ObjCQualifiedInterfaceType *QT =
1539 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001540 return QualType(QT, 0);
1541
1542 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001543 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001544 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001545
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001546 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001547 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001548 return QualType(QType, 0);
1549}
1550
Chris Lattner88cb27a2008-04-07 04:56:42 +00001551/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1552/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001553QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001554 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001555 // Sort the protocol list alphabetically to canonicalize it.
1556 SortAndUniqueProtocols(Protocols, NumProtocols);
1557
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001558 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001559 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001560
1561 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001562 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001563 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001564 return QualType(QT, 0);
1565
1566 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001567 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001568 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001569 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001570 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001571 return QualType(QType, 0);
1572}
1573
Douglas Gregor72564e72009-02-26 23:50:07 +00001574/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1575/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001576/// multiple declarations that refer to "typeof(x)" all contain different
1577/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1578/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001579QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001580 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001581 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001582 Types.push_back(toe);
1583 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001584}
1585
Steve Naroff9752f252007-08-01 18:02:17 +00001586/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1587/// TypeOfType AST's. The only motivation to unique these nodes would be
1588/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1589/// an issue. This doesn't effect the type checker, since it operates
1590/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001591QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001592 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001593 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001594 Types.push_back(tot);
1595 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001596}
1597
Reid Spencer5f016e22007-07-11 17:01:13 +00001598/// getTagDeclType - Return the unique reference to the type for the
1599/// specified TagDecl (struct/union/class/enum) decl.
1600QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001601 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001602 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001603}
1604
1605/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1606/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1607/// needs to agree with the definition in <stddef.h>.
1608QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001609 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001610}
1611
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001612/// getSignedWCharType - Return the type of "signed wchar_t".
1613/// Used when in C++, as a GCC extension.
1614QualType ASTContext::getSignedWCharType() const {
1615 // FIXME: derive from "Target" ?
1616 return WCharTy;
1617}
1618
1619/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1620/// Used when in C++, as a GCC extension.
1621QualType ASTContext::getUnsignedWCharType() const {
1622 // FIXME: derive from "Target" ?
1623 return UnsignedIntTy;
1624}
1625
Chris Lattner8b9023b2007-07-13 03:05:23 +00001626/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1627/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1628QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001629 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001630}
1631
Chris Lattnere6327742008-04-02 05:18:44 +00001632//===----------------------------------------------------------------------===//
1633// Type Operators
1634//===----------------------------------------------------------------------===//
1635
Chris Lattner77c96472008-04-06 22:41:35 +00001636/// getCanonicalType - Return the canonical (structural) type corresponding to
1637/// the specified potentially non-canonical type. The non-canonical version
1638/// of a type may have many "decorated" versions of types. Decorators can
1639/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1640/// to be free of any of these, allowing two canonical types to be compared
1641/// for exact equality with a simple pointer comparison.
1642QualType ASTContext::getCanonicalType(QualType T) {
1643 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001644
1645 // If the result has type qualifiers, make sure to canonicalize them as well.
1646 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1647 if (TypeQuals == 0) return CanType;
1648
1649 // If the type qualifiers are on an array type, get the canonical type of the
1650 // array with the qualifiers applied to the element type.
1651 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1652 if (!AT)
1653 return CanType.getQualifiedType(TypeQuals);
1654
1655 // Get the canonical version of the element with the extra qualifiers on it.
1656 // This can recursively sink qualifiers through multiple levels of arrays.
1657 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1658 NewEltTy = getCanonicalType(NewEltTy);
1659
1660 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1661 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1662 CAT->getIndexTypeQualifier());
1663 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1664 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1665 IAT->getIndexTypeQualifier());
1666
Douglas Gregor898574e2008-12-05 23:32:09 +00001667 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1668 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1669 DSAT->getSizeModifier(),
1670 DSAT->getIndexTypeQualifier());
1671
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001672 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1673 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1674 VAT->getSizeModifier(),
1675 VAT->getIndexTypeQualifier());
1676}
1677
Douglas Gregord57959a2009-03-27 23:10:48 +00001678NestedNameSpecifier *
1679ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1680 if (!NNS)
1681 return 0;
1682
1683 switch (NNS->getKind()) {
1684 case NestedNameSpecifier::Identifier:
1685 // Canonicalize the prefix but keep the identifier the same.
1686 return NestedNameSpecifier::Create(*this,
1687 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1688 NNS->getAsIdentifier());
1689
1690 case NestedNameSpecifier::Namespace:
1691 // A namespace is canonical; build a nested-name-specifier with
1692 // this namespace and no prefix.
1693 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1694
1695 case NestedNameSpecifier::TypeSpec:
1696 case NestedNameSpecifier::TypeSpecWithTemplate: {
1697 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1698 NestedNameSpecifier *Prefix = 0;
1699
1700 // FIXME: This isn't the right check!
1701 if (T->isDependentType())
1702 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1703
1704 return NestedNameSpecifier::Create(*this, Prefix,
1705 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1706 T.getTypePtr());
1707 }
1708
1709 case NestedNameSpecifier::Global:
1710 // The global specifier is canonical and unique.
1711 return NNS;
1712 }
1713
1714 // Required to silence a GCC warning
1715 return 0;
1716}
1717
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001718
1719const ArrayType *ASTContext::getAsArrayType(QualType T) {
1720 // Handle the non-qualified case efficiently.
1721 if (T.getCVRQualifiers() == 0) {
1722 // Handle the common positive case fast.
1723 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1724 return AT;
1725 }
1726
1727 // Handle the common negative case fast, ignoring CVR qualifiers.
1728 QualType CType = T->getCanonicalTypeInternal();
1729
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001730 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001731 // test.
1732 if (!isa<ArrayType>(CType) &&
1733 !isa<ArrayType>(CType.getUnqualifiedType()))
1734 return 0;
1735
1736 // Apply any CVR qualifiers from the array type to the element type. This
1737 // implements C99 6.7.3p8: "If the specification of an array type includes
1738 // any type qualifiers, the element type is so qualified, not the array type."
1739
1740 // If we get here, we either have type qualifiers on the type, or we have
1741 // sugar such as a typedef in the way. If we have type qualifiers on the type
1742 // we must propagate them down into the elemeng type.
1743 unsigned CVRQuals = T.getCVRQualifiers();
1744 unsigned AddrSpace = 0;
1745 Type *Ty = T.getTypePtr();
1746
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001747 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001748 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001749 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1750 AddrSpace = EXTQT->getAddressSpace();
1751 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001752 } else {
1753 T = Ty->getDesugaredType();
1754 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1755 break;
1756 CVRQuals |= T.getCVRQualifiers();
1757 Ty = T.getTypePtr();
1758 }
1759 }
1760
1761 // If we have a simple case, just return now.
1762 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1763 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1764 return ATy;
1765
1766 // Otherwise, we have an array and we have qualifiers on it. Push the
1767 // qualifiers into the array element type and return a new array type.
1768 // Get the canonical version of the element with the extra qualifiers on it.
1769 // This can recursively sink qualifiers through multiple levels of arrays.
1770 QualType NewEltTy = ATy->getElementType();
1771 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001772 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001773 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1774
1775 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1776 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1777 CAT->getSizeModifier(),
1778 CAT->getIndexTypeQualifier()));
1779 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1780 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1781 IAT->getSizeModifier(),
1782 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001783
Douglas Gregor898574e2008-12-05 23:32:09 +00001784 if (const DependentSizedArrayType *DSAT
1785 = dyn_cast<DependentSizedArrayType>(ATy))
1786 return cast<ArrayType>(
1787 getDependentSizedArrayType(NewEltTy,
1788 DSAT->getSizeExpr(),
1789 DSAT->getSizeModifier(),
1790 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001791
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001792 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1793 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1794 VAT->getSizeModifier(),
1795 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001796}
1797
1798
Chris Lattnere6327742008-04-02 05:18:44 +00001799/// getArrayDecayedType - Return the properly qualified result of decaying the
1800/// specified array type to a pointer. This operation is non-trivial when
1801/// handling typedefs etc. The canonical type of "T" must be an array type,
1802/// this returns a pointer to a properly qualified element of the array.
1803///
1804/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1805QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001806 // Get the element type with 'getAsArrayType' so that we don't lose any
1807 // typedefs in the element type of the array. This also handles propagation
1808 // of type qualifiers from the array type into the element type if present
1809 // (C99 6.7.3p8).
1810 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1811 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001812
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001813 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001814
1815 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001816 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001817}
1818
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001819QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001820 QualType ElemTy = VAT->getElementType();
1821
1822 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1823 return getBaseElementType(VAT);
1824
1825 return ElemTy;
1826}
1827
Reid Spencer5f016e22007-07-11 17:01:13 +00001828/// getFloatingRank - Return a relative rank for floating point types.
1829/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001830static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001831 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001832 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001833
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001834 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001835 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001836 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001837 case BuiltinType::Float: return FloatRank;
1838 case BuiltinType::Double: return DoubleRank;
1839 case BuiltinType::LongDouble: return LongDoubleRank;
1840 }
1841}
1842
Steve Naroff716c7302007-08-27 01:41:48 +00001843/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1844/// point or a complex type (based on typeDomain/typeSize).
1845/// 'typeDomain' is a real floating point or complex type.
1846/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001847QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1848 QualType Domain) const {
1849 FloatingRank EltRank = getFloatingRank(Size);
1850 if (Domain->isComplexType()) {
1851 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001852 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001853 case FloatRank: return FloatComplexTy;
1854 case DoubleRank: return DoubleComplexTy;
1855 case LongDoubleRank: return LongDoubleComplexTy;
1856 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001857 }
Chris Lattner1361b112008-04-06 23:58:54 +00001858
1859 assert(Domain->isRealFloatingType() && "Unknown domain!");
1860 switch (EltRank) {
1861 default: assert(0 && "getFloatingRank(): illegal value for rank");
1862 case FloatRank: return FloatTy;
1863 case DoubleRank: return DoubleTy;
1864 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001865 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001866}
1867
Chris Lattner7cfeb082008-04-06 23:55:33 +00001868/// getFloatingTypeOrder - Compare the rank of the two specified floating
1869/// point types, ignoring the domain of the type (i.e. 'double' ==
1870/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1871/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001872int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1873 FloatingRank LHSR = getFloatingRank(LHS);
1874 FloatingRank RHSR = getFloatingRank(RHS);
1875
1876 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001877 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001878 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001879 return 1;
1880 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001881}
1882
Chris Lattnerf52ab252008-04-06 22:59:24 +00001883/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1884/// routine will assert if passed a built-in type that isn't an integer or enum,
1885/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001886unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001887 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001888 if (EnumType* ET = dyn_cast<EnumType>(T))
1889 T = ET->getDecl()->getIntegerType().getTypePtr();
1890
1891 // There are two things which impact the integer rank: the width, and
1892 // the ordering of builtins. The builtin ordering is encoded in the
1893 // bottom three bits; the width is encoded in the bits above that.
1894 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1895 return FWIT->getWidth() << 3;
1896 }
1897
Chris Lattnerf52ab252008-04-06 22:59:24 +00001898 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001899 default: assert(0 && "getIntegerRank(): not a built-in integer");
1900 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001901 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001902 case BuiltinType::Char_S:
1903 case BuiltinType::Char_U:
1904 case BuiltinType::SChar:
1905 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001906 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001907 case BuiltinType::Short:
1908 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001909 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001910 case BuiltinType::Int:
1911 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001912 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001913 case BuiltinType::Long:
1914 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001915 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001916 case BuiltinType::LongLong:
1917 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001918 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001919 }
1920}
1921
Chris Lattner7cfeb082008-04-06 23:55:33 +00001922/// getIntegerTypeOrder - Returns the highest ranked integer type:
1923/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1924/// LHS < RHS, return -1.
1925int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001926 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1927 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001928 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001929
Chris Lattnerf52ab252008-04-06 22:59:24 +00001930 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1931 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001932
Chris Lattner7cfeb082008-04-06 23:55:33 +00001933 unsigned LHSRank = getIntegerRank(LHSC);
1934 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001935
Chris Lattner7cfeb082008-04-06 23:55:33 +00001936 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1937 if (LHSRank == RHSRank) return 0;
1938 return LHSRank > RHSRank ? 1 : -1;
1939 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001940
Chris Lattner7cfeb082008-04-06 23:55:33 +00001941 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1942 if (LHSUnsigned) {
1943 // If the unsigned [LHS] type is larger, return it.
1944 if (LHSRank >= RHSRank)
1945 return 1;
1946
1947 // If the signed type can represent all values of the unsigned type, it
1948 // wins. Because we are dealing with 2's complement and types that are
1949 // powers of two larger than each other, this is always safe.
1950 return -1;
1951 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001952
Chris Lattner7cfeb082008-04-06 23:55:33 +00001953 // If the unsigned [RHS] type is larger, return it.
1954 if (RHSRank >= LHSRank)
1955 return -1;
1956
1957 // If the signed type can represent all values of the unsigned type, it
1958 // wins. Because we are dealing with 2's complement and types that are
1959 // powers of two larger than each other, this is always safe.
1960 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001961}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001962
1963// getCFConstantStringType - Return the type used for constant CFStrings.
1964QualType ASTContext::getCFConstantStringType() {
1965 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001966 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001967 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001968 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001969 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001970
1971 // const int *isa;
1972 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001973 // int flags;
1974 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001975 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001976 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001977 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001978 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001979
Anders Carlsson71993dd2007-08-17 05:31:46 +00001980 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001981 for (unsigned i = 0; i < 4; ++i) {
1982 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1983 SourceLocation(), 0,
1984 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001985 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001986 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001987 }
1988
1989 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001990 }
1991
1992 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001993}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001994
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001995QualType ASTContext::getObjCFastEnumerationStateType()
1996{
1997 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001998 ObjCFastEnumerationStateTypeDecl =
1999 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2000 &Idents.get("__objcFastEnumerationState"));
2001
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002002 QualType FieldTypes[] = {
2003 UnsignedLongTy,
2004 getPointerType(ObjCIdType),
2005 getPointerType(UnsignedLongTy),
2006 getConstantArrayType(UnsignedLongTy,
2007 llvm::APInt(32, 5), ArrayType::Normal, 0)
2008 };
2009
Douglas Gregor44b43212008-12-11 16:49:14 +00002010 for (size_t i = 0; i < 4; ++i) {
2011 FieldDecl *Field = FieldDecl::Create(*this,
2012 ObjCFastEnumerationStateTypeDecl,
2013 SourceLocation(), 0,
2014 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002015 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002016 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002017 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002018
Douglas Gregor44b43212008-12-11 16:49:14 +00002019 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002020 }
2021
2022 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2023}
2024
Anders Carlssone8c49532007-10-29 06:33:42 +00002025// This returns true if a type has been typedefed to BOOL:
2026// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002027static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002028 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002029 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2030 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002031
2032 return false;
2033}
2034
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002035/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002036/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002037int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002038 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002039
2040 // Make all integer and enum types at least as large as an int
2041 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002042 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002043 // Treat arrays as pointers, since that's how they're passed in.
2044 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002045 sz = getTypeSize(VoidPtrTy);
2046 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002047}
2048
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002049/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002050/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002051void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002052 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002053 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002054 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002055 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002056 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002057 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002058 // Compute size of all parameters.
2059 // Start with computing size of a pointer in number of bytes.
2060 // FIXME: There might(should) be a better way of doing this computation!
2061 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002062 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002063 // The first two arguments (self and _cmd) are pointers; account for
2064 // their size.
2065 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002066 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2067 E = Decl->param_end(); PI != E; ++PI) {
2068 QualType PType = (*PI)->getType();
2069 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002070 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002071 ParmOffset += sz;
2072 }
2073 S += llvm::utostr(ParmOffset);
2074 S += "@0:";
2075 S += llvm::utostr(PtrSize);
2076
2077 // Argument types.
2078 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002079 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2080 E = Decl->param_end(); PI != E; ++PI) {
2081 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002082 QualType PType = PVDecl->getOriginalType();
2083 if (const ArrayType *AT =
2084 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
2085 // Use array's original type only if it has known number of
2086 // elements.
2087 if (!dyn_cast<ConstantArrayType>(AT))
2088 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002089 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002090 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002091 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002092 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002093 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002094 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002095 }
2096}
2097
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002098/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002099/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002100/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2101/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002102/// Property attributes are stored as a comma-delimited C string. The simple
2103/// attributes readonly and bycopy are encoded as single characters. The
2104/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2105/// encoded as single characters, followed by an identifier. Property types
2106/// are also encoded as a parametrized attribute. The characters used to encode
2107/// these attributes are defined by the following enumeration:
2108/// @code
2109/// enum PropertyAttributes {
2110/// kPropertyReadOnly = 'R', // property is read-only.
2111/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2112/// kPropertyByref = '&', // property is a reference to the value last assigned
2113/// kPropertyDynamic = 'D', // property is dynamic
2114/// kPropertyGetter = 'G', // followed by getter selector name
2115/// kPropertySetter = 'S', // followed by setter selector name
2116/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2117/// kPropertyType = 't' // followed by old-style type encoding.
2118/// kPropertyWeak = 'W' // 'weak' property
2119/// kPropertyStrong = 'P' // property GC'able
2120/// kPropertyNonAtomic = 'N' // property non-atomic
2121/// };
2122/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002123void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2124 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002125 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002126 // Collect information from the property implementation decl(s).
2127 bool Dynamic = false;
2128 ObjCPropertyImplDecl *SynthesizePID = 0;
2129
2130 // FIXME: Duplicated code due to poor abstraction.
2131 if (Container) {
2132 if (const ObjCCategoryImplDecl *CID =
2133 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2134 for (ObjCCategoryImplDecl::propimpl_iterator
2135 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2136 ObjCPropertyImplDecl *PID = *i;
2137 if (PID->getPropertyDecl() == PD) {
2138 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2139 Dynamic = true;
2140 } else {
2141 SynthesizePID = PID;
2142 }
2143 }
2144 }
2145 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002146 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002147 for (ObjCCategoryImplDecl::propimpl_iterator
2148 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2149 ObjCPropertyImplDecl *PID = *i;
2150 if (PID->getPropertyDecl() == PD) {
2151 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2152 Dynamic = true;
2153 } else {
2154 SynthesizePID = PID;
2155 }
2156 }
2157 }
2158 }
2159 }
2160
2161 // FIXME: This is not very efficient.
2162 S = "T";
2163
2164 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002165 // GCC has some special rules regarding encoding of properties which
2166 // closely resembles encoding of ivars.
2167 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
2168 true /* outermost type */,
2169 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002170
2171 if (PD->isReadOnly()) {
2172 S += ",R";
2173 } else {
2174 switch (PD->getSetterKind()) {
2175 case ObjCPropertyDecl::Assign: break;
2176 case ObjCPropertyDecl::Copy: S += ",C"; break;
2177 case ObjCPropertyDecl::Retain: S += ",&"; break;
2178 }
2179 }
2180
2181 // It really isn't clear at all what this means, since properties
2182 // are "dynamic by default".
2183 if (Dynamic)
2184 S += ",D";
2185
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002186 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2187 S += ",N";
2188
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002189 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2190 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002191 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002192 }
2193
2194 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2195 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002196 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002197 }
2198
2199 if (SynthesizePID) {
2200 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2201 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002202 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002203 }
2204
2205 // FIXME: OBJCGC: weak & strong
2206}
2207
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002208/// getLegacyIntegralTypeEncoding -
2209/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002210/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002211/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2212///
2213void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2214 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2215 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002216 if (BT->getKind() == BuiltinType::ULong &&
2217 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002218 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002219 else
2220 if (BT->getKind() == BuiltinType::Long &&
2221 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002222 PointeeTy = IntTy;
2223 }
2224 }
2225}
2226
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002227void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002228 FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002229 // We follow the behavior of gcc, expanding structures which are
2230 // directly pointed to, and expanding embedded structures. Note that
2231 // these rules are sufficient to prevent recursive encoding of the
2232 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002233 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2234 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002235}
2236
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002237static void EncodeBitField(const ASTContext *Context, std::string& S,
2238 FieldDecl *FD) {
2239 const Expr *E = FD->getBitWidth();
2240 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2241 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2242 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2243 S += 'b';
2244 S += llvm::utostr(N);
2245}
2246
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002247void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2248 bool ExpandPointedToStructures,
2249 bool ExpandStructures,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002250 FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002251 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002252 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002253 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002254 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002255 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002256 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002257 else {
2258 char encoding;
2259 switch (BT->getKind()) {
2260 default: assert(0 && "Unhandled builtin type kind");
2261 case BuiltinType::Void: encoding = 'v'; break;
2262 case BuiltinType::Bool: encoding = 'B'; break;
2263 case BuiltinType::Char_U:
2264 case BuiltinType::UChar: encoding = 'C'; break;
2265 case BuiltinType::UShort: encoding = 'S'; break;
2266 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002267 case BuiltinType::ULong:
2268 encoding =
2269 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2270 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002271 case BuiltinType::ULongLong: encoding = 'Q'; break;
2272 case BuiltinType::Char_S:
2273 case BuiltinType::SChar: encoding = 'c'; break;
2274 case BuiltinType::Short: encoding = 's'; break;
2275 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002276 case BuiltinType::Long:
2277 encoding =
2278 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2279 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002280 case BuiltinType::LongLong: encoding = 'q'; break;
2281 case BuiltinType::Float: encoding = 'f'; break;
2282 case BuiltinType::Double: encoding = 'd'; break;
2283 case BuiltinType::LongDouble: encoding = 'd'; break;
2284 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002285
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002286 S += encoding;
2287 }
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002288 } else if (const ComplexType *CT = T->getAsComplexType()) {
2289 S += 'j';
2290 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2291 false);
2292 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002293 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2294 ExpandPointedToStructures,
2295 ExpandStructures, FD);
2296 if (FD || EncodingProperty) {
2297 // Note that we do extended encoding of protocol qualifer list
2298 // Only when doing ivar or property encoding.
2299 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2300 S += '"';
2301 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2302 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2303 S += '<';
2304 S += Proto->getNameAsString();
2305 S += '>';
2306 }
2307 S += '"';
2308 }
2309 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002310 }
2311 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002312 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002313 bool isReadOnly = false;
2314 // For historical/compatibility reasons, the read-only qualifier of the
2315 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2316 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2317 // Also, do not emit the 'r' for anything but the outermost type!
2318 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2319 if (OutermostType && T.isConstQualified()) {
2320 isReadOnly = true;
2321 S += 'r';
2322 }
2323 }
2324 else if (OutermostType) {
2325 QualType P = PointeeTy;
2326 while (P->getAsPointerType())
2327 P = P->getAsPointerType()->getPointeeType();
2328 if (P.isConstQualified()) {
2329 isReadOnly = true;
2330 S += 'r';
2331 }
2332 }
2333 if (isReadOnly) {
2334 // Another legacy compatibility encoding. Some ObjC qualifier and type
2335 // combinations need to be rearranged.
2336 // Rewrite "in const" from "nr" to "rn"
2337 const char * s = S.c_str();
2338 int len = S.length();
2339 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2340 std::string replace = "rn";
2341 S.replace(S.end()-2, S.end(), replace);
2342 }
2343 }
Steve Naroff389bf462009-02-12 17:52:19 +00002344 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002345 S += '@';
2346 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002347 }
2348 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002349 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002350 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002351 // Another historical/compatibility reason.
2352 // We encode the underlying type which comes out as
2353 // {...};
2354 S += '^';
2355 getObjCEncodingForTypeImpl(PointeeTy, S,
2356 false, ExpandPointedToStructures,
2357 NULL);
2358 return;
2359 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002360 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002361 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002362 const ObjCInterfaceType *OIT =
2363 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002364 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002365 S += '"';
2366 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002367 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2368 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2369 S += '<';
2370 S += Proto->getNameAsString();
2371 S += '>';
2372 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002373 S += '"';
2374 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002375 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002376 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002377 S += '#';
2378 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002379 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002380 S += ':';
2381 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002382 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002383
2384 if (PointeeTy->isCharType()) {
2385 // char pointer types should be encoded as '*' unless it is a
2386 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002387 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002388 S += '*';
2389 return;
2390 }
2391 }
2392
2393 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002394 getLegacyIntegralTypeEncoding(PointeeTy);
2395
2396 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002397 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002398 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002399 } else if (const ArrayType *AT =
2400 // Ignore type qualifiers etc.
2401 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002402 if (isa<IncompleteArrayType>(AT)) {
2403 // Incomplete arrays are encoded as a pointer to the array element.
2404 S += '^';
2405
2406 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2407 false, ExpandStructures, FD);
2408 } else {
2409 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002410
Anders Carlsson559a8332009-02-22 01:38:57 +00002411 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2412 S += llvm::utostr(CAT->getSize().getZExtValue());
2413 else {
2414 //Variable length arrays are encoded as a regular array with 0 elements.
2415 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2416 S += '0';
2417 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002418
Anders Carlsson559a8332009-02-22 01:38:57 +00002419 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2420 false, ExpandStructures, FD);
2421 S += ']';
2422 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002423 } else if (T->getAsFunctionType()) {
2424 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002425 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002426 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002427 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002428 // Anonymous structures print as '?'
2429 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2430 S += II->getName();
2431 } else {
2432 S += '?';
2433 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002434 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002435 S += '=';
Douglas Gregor6ab35242009-04-09 21:40:53 +00002436 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2437 FieldEnd = RDecl->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +00002438 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002439 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002440 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002441 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002442 S += '"';
2443 }
2444
2445 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002446 if (Field->isBitField()) {
2447 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2448 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002449 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002450 QualType qt = Field->getType();
2451 getLegacyIntegralTypeEncoding(qt);
2452 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002453 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002454 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002455 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002456 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002457 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002458 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002459 if (FD && FD->isBitField())
2460 EncodeBitField(this, S, FD);
2461 else
2462 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002463 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002464 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002465 } else if (T->isObjCInterfaceType()) {
2466 // @encode(class_name)
2467 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2468 S += '{';
2469 const IdentifierInfo *II = OI->getIdentifier();
2470 S += II->getName();
2471 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002472 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002473 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002474 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002475 if (RecFields[i]->isBitField())
2476 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2477 RecFields[i]);
2478 else
2479 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2480 FD);
2481 }
2482 S += '}';
2483 }
2484 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002485 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002486}
2487
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002488void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002489 std::string& S) const {
2490 if (QT & Decl::OBJC_TQ_In)
2491 S += 'n';
2492 if (QT & Decl::OBJC_TQ_Inout)
2493 S += 'N';
2494 if (QT & Decl::OBJC_TQ_Out)
2495 S += 'o';
2496 if (QT & Decl::OBJC_TQ_Bycopy)
2497 S += 'O';
2498 if (QT & Decl::OBJC_TQ_Byref)
2499 S += 'R';
2500 if (QT & Decl::OBJC_TQ_Oneway)
2501 S += 'V';
2502}
2503
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002504void ASTContext::setBuiltinVaListType(QualType T)
2505{
2506 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2507
2508 BuiltinVaListType = T;
2509}
2510
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002511void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002512{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002513 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002514
2515 // typedef struct objc_object *id;
2516 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002517 // User error - caller will issue diagnostics.
2518 if (!ptr)
2519 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002520 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002521 // User error - caller will issue diagnostics.
2522 if (!rec)
2523 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002524 IdStructType = rec;
2525}
2526
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002527void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002528{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002529 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002530
2531 // typedef struct objc_selector *SEL;
2532 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002533 if (!ptr)
2534 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002535 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002536 if (!rec)
2537 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002538 SelStructType = rec;
2539}
2540
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002541void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002542{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002543 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002544}
2545
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002546void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002547{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002548 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002549
2550 // typedef struct objc_class *Class;
2551 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2552 assert(ptr && "'Class' incorrectly typed");
2553 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2554 assert(rec && "'Class' incorrectly typed");
2555 ClassStructType = rec;
2556}
2557
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002558void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2559 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002560 "'NSConstantString' type already set!");
2561
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002562 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002563}
2564
Douglas Gregor7532dc62009-03-30 22:58:21 +00002565/// \brief Retrieve the template name that represents a qualified
2566/// template name such as \c std::vector.
2567TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2568 bool TemplateKeyword,
2569 TemplateDecl *Template) {
2570 llvm::FoldingSetNodeID ID;
2571 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2572
2573 void *InsertPos = 0;
2574 QualifiedTemplateName *QTN =
2575 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2576 if (!QTN) {
2577 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2578 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2579 }
2580
2581 return TemplateName(QTN);
2582}
2583
2584/// \brief Retrieve the template name that represents a dependent
2585/// template name such as \c MetaFun::template apply.
2586TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2587 const IdentifierInfo *Name) {
2588 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2589
2590 llvm::FoldingSetNodeID ID;
2591 DependentTemplateName::Profile(ID, NNS, Name);
2592
2593 void *InsertPos = 0;
2594 DependentTemplateName *QTN =
2595 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2596
2597 if (QTN)
2598 return TemplateName(QTN);
2599
2600 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2601 if (CanonNNS == NNS) {
2602 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2603 } else {
2604 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2605 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2606 }
2607
2608 DependentTemplateNames.InsertNode(QTN, InsertPos);
2609 return TemplateName(QTN);
2610}
2611
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002612/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002613/// TargetInfo, produce the corresponding type. The unsigned @p Type
2614/// is actually a value of type @c TargetInfo::IntType.
2615QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002616 switch (Type) {
2617 case TargetInfo::NoInt: return QualType();
2618 case TargetInfo::SignedShort: return ShortTy;
2619 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2620 case TargetInfo::SignedInt: return IntTy;
2621 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2622 case TargetInfo::SignedLong: return LongTy;
2623 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2624 case TargetInfo::SignedLongLong: return LongLongTy;
2625 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2626 }
2627
2628 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002629 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002630}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002631
2632//===----------------------------------------------------------------------===//
2633// Type Predicates.
2634//===----------------------------------------------------------------------===//
2635
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002636/// isObjCNSObjectType - Return true if this is an NSObject object using
2637/// NSObject attribute on a c-style pointer type.
2638/// FIXME - Make it work directly on types.
2639///
2640bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2641 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2642 if (TypedefDecl *TD = TDT->getDecl())
2643 if (TD->getAttr<ObjCNSObjectAttr>())
2644 return true;
2645 }
2646 return false;
2647}
2648
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002649/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2650/// to an object type. This includes "id" and "Class" (two 'special' pointers
2651/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2652/// ID type).
2653bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002654 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002655 return true;
2656
Steve Naroff6ae98502008-10-21 18:24:04 +00002657 // Blocks are objects.
2658 if (Ty->isBlockPointerType())
2659 return true;
2660
2661 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002662 if (!Ty->isPointerType())
2663 return false;
2664
2665 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2666 // pointer types. This looks for the typedef specifically, not for the
2667 // underlying type.
Eli Friedman5fdeae12009-03-22 23:00:19 +00002668 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2669 Ty.getUnqualifiedType() == getObjCClassType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002670 return true;
2671
2672 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002673 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2674 return true;
2675
2676 // If is has NSObject attribute, OK as well.
2677 return isObjCNSObjectType(Ty);
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002678}
2679
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002680/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2681/// garbage collection attribute.
2682///
2683QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002684 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002685 if (getLangOptions().ObjC1 &&
2686 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002687 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002688 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002689 // (or pointers to them) be treated as though they were declared
2690 // as __strong.
2691 if (GCAttrs == QualType::GCNone) {
2692 if (isObjCObjectPointerType(Ty))
2693 GCAttrs = QualType::Strong;
2694 else if (Ty->isPointerType())
2695 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2696 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00002697 // Non-pointers have none gc'able attribute regardless of the attribute
2698 // set on them.
2699 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2700 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002701 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002702 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002703}
2704
Chris Lattner6ac46a42008-04-07 06:51:04 +00002705//===----------------------------------------------------------------------===//
2706// Type Compatibility Testing
2707//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002708
Steve Naroff1c7d0672008-09-04 15:10:53 +00002709/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002710/// block types. Types must be strictly compatible here. For example,
2711/// C unfortunately doesn't produce an error for the following:
2712///
2713/// int (*emptyArgFunc)();
2714/// int (*intArgList)(int) = emptyArgFunc;
2715///
2716/// For blocks, we will produce an error for the following (similar to C++):
2717///
2718/// int (^emptyArgBlock)();
2719/// int (^intArgBlock)(int) = emptyArgBlock;
2720///
2721/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2722///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002723bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002724 const FunctionType *lbase = lhs->getAsFunctionType();
2725 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002726 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2727 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stumpaab0f7a2009-04-01 01:17:39 +00002728 if (lproto && rproto == 0)
2729 return false;
2730 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff1c7d0672008-09-04 15:10:53 +00002731}
2732
Chris Lattner6ac46a42008-04-07 06:51:04 +00002733/// areCompatVectorTypes - Return true if the two specified vector types are
2734/// compatible.
2735static bool areCompatVectorTypes(const VectorType *LHS,
2736 const VectorType *RHS) {
2737 assert(LHS->isCanonical() && RHS->isCanonical());
2738 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002739 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002740}
2741
Eli Friedman3d815e72008-08-22 00:56:42 +00002742/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002743/// compatible for assignment from RHS to LHS. This handles validation of any
2744/// protocol qualifiers on the LHS or RHS.
2745///
Eli Friedman3d815e72008-08-22 00:56:42 +00002746bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2747 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002748 // Verify that the base decls are compatible: the RHS must be a subclass of
2749 // the LHS.
2750 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2751 return false;
2752
2753 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2754 // protocol qualified at all, then we are good.
2755 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2756 return true;
2757
2758 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2759 // isn't a superset.
2760 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2761 return true; // FIXME: should return false!
2762
2763 // Finally, we must have two protocol-qualified interfaces.
2764 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2765 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002766
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002767 // All LHS protocols must have a presence on the RHS.
2768 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002769
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002770 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2771 LHSPE = LHSP->qual_end();
2772 LHSPI != LHSPE; LHSPI++) {
2773 bool RHSImplementsProtocol = false;
2774
2775 // If the RHS doesn't implement the protocol on the left, the types
2776 // are incompatible.
2777 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2778 RHSPE = RHSP->qual_end();
2779 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2780 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2781 RHSImplementsProtocol = true;
2782 }
2783 // FIXME: For better diagnostics, consider passing back the protocol name.
2784 if (!RHSImplementsProtocol)
2785 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002786 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002787 // The RHS implements all protocols listed on the LHS.
2788 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002789}
2790
Steve Naroff389bf462009-02-12 17:52:19 +00002791bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2792 // get the "pointed to" types
2793 const PointerType *LHSPT = LHS->getAsPointerType();
2794 const PointerType *RHSPT = RHS->getAsPointerType();
2795
2796 if (!LHSPT || !RHSPT)
2797 return false;
2798
2799 QualType lhptee = LHSPT->getPointeeType();
2800 QualType rhptee = RHSPT->getPointeeType();
2801 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2802 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2803 // ID acts sort of like void* for ObjC interfaces
2804 if (LHSIface && isObjCIdStructType(rhptee))
2805 return true;
2806 if (RHSIface && isObjCIdStructType(lhptee))
2807 return true;
2808 if (!LHSIface || !RHSIface)
2809 return false;
2810 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2811 canAssignObjCInterfaces(RHSIface, LHSIface);
2812}
2813
Steve Naroffec0550f2007-10-15 20:41:53 +00002814/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2815/// both shall have the identically qualified version of a compatible type.
2816/// C99 6.2.7p1: Two types have compatible types if their types are the
2817/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002818bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2819 return !mergeTypes(LHS, RHS).isNull();
2820}
2821
2822QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2823 const FunctionType *lbase = lhs->getAsFunctionType();
2824 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002825 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2826 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002827 bool allLTypes = true;
2828 bool allRTypes = true;
2829
2830 // Check return type
2831 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2832 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002833 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2834 allLTypes = false;
2835 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2836 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002837
2838 if (lproto && rproto) { // two C99 style function prototypes
2839 unsigned lproto_nargs = lproto->getNumArgs();
2840 unsigned rproto_nargs = rproto->getNumArgs();
2841
2842 // Compatible functions must have the same number of arguments
2843 if (lproto_nargs != rproto_nargs)
2844 return QualType();
2845
2846 // Variadic and non-variadic functions aren't compatible
2847 if (lproto->isVariadic() != rproto->isVariadic())
2848 return QualType();
2849
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002850 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2851 return QualType();
2852
Eli Friedman3d815e72008-08-22 00:56:42 +00002853 // Check argument compatibility
2854 llvm::SmallVector<QualType, 10> types;
2855 for (unsigned i = 0; i < lproto_nargs; i++) {
2856 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2857 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2858 QualType argtype = mergeTypes(largtype, rargtype);
2859 if (argtype.isNull()) return QualType();
2860 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002861 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2862 allLTypes = false;
2863 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2864 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002865 }
2866 if (allLTypes) return lhs;
2867 if (allRTypes) return rhs;
2868 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002869 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002870 }
2871
2872 if (lproto) allRTypes = false;
2873 if (rproto) allLTypes = false;
2874
Douglas Gregor72564e72009-02-26 23:50:07 +00002875 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002876 if (proto) {
2877 if (proto->isVariadic()) return QualType();
2878 // Check that the types are compatible with the types that
2879 // would result from default argument promotions (C99 6.7.5.3p15).
2880 // The only types actually affected are promotable integer
2881 // types and floats, which would be passed as a different
2882 // type depending on whether the prototype is visible.
2883 unsigned proto_nargs = proto->getNumArgs();
2884 for (unsigned i = 0; i < proto_nargs; ++i) {
2885 QualType argTy = proto->getArgType(i);
2886 if (argTy->isPromotableIntegerType() ||
2887 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2888 return QualType();
2889 }
2890
2891 if (allLTypes) return lhs;
2892 if (allRTypes) return rhs;
2893 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002894 proto->getNumArgs(), lproto->isVariadic(),
2895 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002896 }
2897
2898 if (allLTypes) return lhs;
2899 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002900 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002901}
2902
2903QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002904 // C++ [expr]: If an expression initially has the type "reference to T", the
2905 // type is adjusted to "T" prior to any further analysis, the expression
2906 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002907 // expression is an lvalue unless the reference is an rvalue reference and
2908 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00002909 // FIXME: C++ shouldn't be going through here! The rules are different
2910 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002911 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2912 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00002913 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002914 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002915 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002916 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002917
Eli Friedman3d815e72008-08-22 00:56:42 +00002918 QualType LHSCan = getCanonicalType(LHS),
2919 RHSCan = getCanonicalType(RHS);
2920
2921 // If two types are identical, they are compatible.
2922 if (LHSCan == RHSCan)
2923 return LHS;
2924
2925 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002926 // Note that we handle extended qualifiers later, in the
2927 // case for ExtQualType.
2928 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00002929 return QualType();
2930
2931 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2932 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2933
Chris Lattner1adb8832008-01-14 05:45:46 +00002934 // We want to consider the two function types to be the same for these
2935 // comparisons, just force one to the other.
2936 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2937 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002938
2939 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002940 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2941 LHSClass = Type::ConstantArray;
2942 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2943 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002944
Nate Begeman213541a2008-04-18 23:10:10 +00002945 // Canonicalize ExtVector -> Vector.
2946 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2947 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002948
Chris Lattnerb0489812008-04-07 06:38:24 +00002949 // Consider qualified interfaces and interfaces the same.
2950 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2951 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002952
Chris Lattnera36a61f2008-04-07 05:43:21 +00002953 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002954 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002955 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2956 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2957
2958 // ID acts sort of like void* for ObjC interfaces
2959 if (LHSIface && isObjCIdStructType(RHS))
2960 return LHS;
2961 if (RHSIface && isObjCIdStructType(LHS))
2962 return RHS;
2963
Steve Naroffbc76dd02008-12-10 22:14:21 +00002964 // ID is compatible with all qualified id types.
2965 if (LHS->isObjCQualifiedIdType()) {
2966 if (const PointerType *PT = RHS->getAsPointerType()) {
2967 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002968 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002969 return LHS;
2970 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2971 // Unfortunately, this API is part of Sema (which we don't have access
2972 // to. Need to refactor. The following check is insufficient, since we
2973 // need to make sure the class implements the protocol.
2974 if (pType->isObjCInterfaceType())
2975 return LHS;
2976 }
2977 }
2978 if (RHS->isObjCQualifiedIdType()) {
2979 if (const PointerType *PT = LHS->getAsPointerType()) {
2980 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002981 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002982 return RHS;
2983 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2984 // Unfortunately, this API is part of Sema (which we don't have access
2985 // to. Need to refactor. The following check is insufficient, since we
2986 // need to make sure the class implements the protocol.
2987 if (pType->isObjCInterfaceType())
2988 return RHS;
2989 }
2990 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002991 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2992 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002993 if (const EnumType* ETy = LHS->getAsEnumType()) {
2994 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2995 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002996 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002997 if (const EnumType* ETy = RHS->getAsEnumType()) {
2998 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2999 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003000 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003001
Eli Friedman3d815e72008-08-22 00:56:42 +00003002 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003003 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003004
Steve Naroff4a746782008-01-09 22:43:08 +00003005 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003006 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003007#define TYPE(Class, Base)
3008#define ABSTRACT_TYPE(Class, Base)
3009#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3010#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3011#include "clang/AST/TypeNodes.def"
3012 assert(false && "Non-canonical and dependent types shouldn't get here");
3013 return QualType();
3014
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003015 case Type::LValueReference:
3016 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003017 case Type::MemberPointer:
3018 assert(false && "C++ should never be in mergeTypes");
3019 return QualType();
3020
3021 case Type::IncompleteArray:
3022 case Type::VariableArray:
3023 case Type::FunctionProto:
3024 case Type::ExtVector:
3025 case Type::ObjCQualifiedInterface:
3026 assert(false && "Types are eliminated above");
3027 return QualType();
3028
Chris Lattner1adb8832008-01-14 05:45:46 +00003029 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003030 {
3031 // Merge two pointer types, while trying to preserve typedef info
3032 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3033 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3034 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3035 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003036 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3037 return LHS;
3038 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3039 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003040 return getPointerType(ResultType);
3041 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003042 case Type::BlockPointer:
3043 {
3044 // Merge two block pointer types, while trying to preserve typedef info
3045 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3046 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3047 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3048 if (ResultType.isNull()) return QualType();
3049 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3050 return LHS;
3051 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3052 return RHS;
3053 return getBlockPointerType(ResultType);
3054 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003055 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003056 {
3057 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3058 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3059 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3060 return QualType();
3061
3062 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3063 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3064 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3065 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003066 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3067 return LHS;
3068 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3069 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003070 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3071 ArrayType::ArraySizeModifier(), 0);
3072 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3073 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003074 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3075 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003076 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3077 return LHS;
3078 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3079 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003080 if (LVAT) {
3081 // FIXME: This isn't correct! But tricky to implement because
3082 // the array's size has to be the size of LHS, but the type
3083 // has to be different.
3084 return LHS;
3085 }
3086 if (RVAT) {
3087 // FIXME: This isn't correct! But tricky to implement because
3088 // the array's size has to be the size of RHS, but the type
3089 // has to be different.
3090 return RHS;
3091 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003092 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3093 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003094 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003095 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003096 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003097 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003098 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003099 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003100 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003101 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3102 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003103 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003104 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003105 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003106 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003107 case Type::Complex:
3108 // Distinct complex types are incompatible.
3109 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003110 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003111 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003112 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3113 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003114 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003115 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003116 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003117 // FIXME: This should be type compatibility, e.g. whether
3118 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003119 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3120 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3121 if (LHSIface && RHSIface &&
3122 canAssignObjCInterfaces(LHSIface, RHSIface))
3123 return LHS;
3124
Eli Friedman3d815e72008-08-22 00:56:42 +00003125 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003126 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003127 case Type::ObjCQualifiedId:
3128 // Distinct qualified id's are not compatible.
3129 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003130 case Type::FixedWidthInt:
3131 // Distinct fixed-width integers are not compatible.
3132 return QualType();
3133 case Type::ObjCQualifiedClass:
3134 // Distinct qualified classes are not compatible.
3135 return QualType();
3136 case Type::ExtQual:
3137 // FIXME: ExtQual types can be compatible even if they're not
3138 // identical!
3139 return QualType();
3140 // First attempt at an implementation, but I'm not really sure it's
3141 // right...
3142#if 0
3143 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3144 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3145 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3146 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3147 return QualType();
3148 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3149 LHSBase = QualType(LQual->getBaseType(), 0);
3150 RHSBase = QualType(RQual->getBaseType(), 0);
3151 ResultType = mergeTypes(LHSBase, RHSBase);
3152 if (ResultType.isNull()) return QualType();
3153 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3154 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3155 return LHS;
3156 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3157 return RHS;
3158 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3159 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3160 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3161 return ResultType;
3162#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003163
3164 case Type::TemplateSpecialization:
3165 assert(false && "Dependent types have no size");
3166 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003167 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003168
3169 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003170}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003171
Chris Lattner5426bf62008-04-07 07:01:58 +00003172//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003173// Integer Predicates
3174//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003175
Eli Friedmanad74a752008-06-28 06:23:08 +00003176unsigned ASTContext::getIntWidth(QualType T) {
3177 if (T == BoolTy)
3178 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003179 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3180 return FWIT->getWidth();
3181 }
3182 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003183 return (unsigned)getTypeSize(T);
3184}
3185
3186QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3187 assert(T->isSignedIntegerType() && "Unexpected type");
3188 if (const EnumType* ETy = T->getAsEnumType())
3189 T = ETy->getDecl()->getIntegerType();
3190 const BuiltinType* BTy = T->getAsBuiltinType();
3191 assert (BTy && "Unexpected signed integer type");
3192 switch (BTy->getKind()) {
3193 case BuiltinType::Char_S:
3194 case BuiltinType::SChar:
3195 return UnsignedCharTy;
3196 case BuiltinType::Short:
3197 return UnsignedShortTy;
3198 case BuiltinType::Int:
3199 return UnsignedIntTy;
3200 case BuiltinType::Long:
3201 return UnsignedLongTy;
3202 case BuiltinType::LongLong:
3203 return UnsignedLongLongTy;
3204 default:
3205 assert(0 && "Unexpected signed integer type");
3206 return QualType();
3207 }
3208}
3209
3210
3211//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00003212// Serialization Support
3213//===----------------------------------------------------------------------===//
3214
Chris Lattnera9376d42009-03-28 03:45:20 +00003215enum {
3216 BasicMetadataBlock = 1,
3217 ASTContextBlock = 2,
3218 DeclsBlock = 3
3219};
3220
Chris Lattner557c5b12009-03-28 04:27:18 +00003221void ASTContext::EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const{
3222 // Create bitstream.
3223 llvm::BitstreamWriter Stream(Buffer);
3224
3225 // Emit the preamble.
3226 Stream.Emit((unsigned)'B', 8);
3227 Stream.Emit((unsigned)'C', 8);
3228 Stream.Emit(0xC, 4);
3229 Stream.Emit(0xF, 4);
3230 Stream.Emit(0xE, 4);
3231 Stream.Emit(0x0, 4);
3232
3233 // Create serializer.
3234 llvm::Serializer S(Stream);
3235
Chris Lattnera9376d42009-03-28 03:45:20 +00003236 // ===---------------------------------------------------===/
3237 // Serialize the "Translation Unit" metadata.
3238 // ===---------------------------------------------------===/
3239
3240 // Emit ASTContext.
3241 S.EnterBlock(ASTContextBlock);
3242 S.EmitOwnedPtr(this);
3243 S.ExitBlock(); // exit "ASTContextBlock"
3244
3245 S.EnterBlock(BasicMetadataBlock);
3246
3247 // Block for SourceManager and Target. Allows easy skipping
3248 // around to the block for the Selectors during deserialization.
3249 S.EnterBlock();
3250
3251 // Emit the SourceManager.
3252 S.Emit(getSourceManager());
3253
3254 // Emit the Target.
3255 S.EmitPtr(&Target);
3256 S.EmitCStr(Target.getTargetTriple());
3257
3258 S.ExitBlock(); // exit "SourceManager and Target Block"
3259
3260 // Emit the Selectors.
3261 S.Emit(Selectors);
3262
3263 // Emit the Identifier Table.
3264 S.Emit(Idents);
3265
3266 S.ExitBlock(); // exit "BasicMetadataBlock"
3267}
3268
3269
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003270/// Emit - Serialize an ASTContext object to Bitcode.
3271void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003272 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00003273 S.EmitRef(SourceMgr);
3274 S.EmitRef(Target);
3275 S.EmitRef(Idents);
3276 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003277
Ted Kremenekfee04522007-10-31 22:44:07 +00003278 // Emit the size of the type vector so that we can reserve that size
3279 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00003280 S.EmitInt(Types.size());
3281
Ted Kremenek03ed4402007-11-13 22:02:55 +00003282 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3283 I!=E;++I)
3284 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00003285
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003286 S.EmitOwnedPtr(TUDecl);
3287
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003288 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003289}
3290
Chris Lattner557c5b12009-03-28 04:27:18 +00003291
3292ASTContext *ASTContext::ReadASTBitcodeBuffer(llvm::MemoryBuffer &Buffer,
3293 FileManager &FMgr) {
3294 // Check if the file is of the proper length.
3295 if (Buffer.getBufferSize() & 0x3) {
3296 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
3297 return 0;
3298 }
3299
3300 // Create the bitstream reader.
3301 unsigned char *BufPtr = (unsigned char *)Buffer.getBufferStart();
3302 llvm::BitstreamReader Stream(BufPtr, BufPtr+Buffer.getBufferSize());
3303
3304 if (Stream.Read(8) != 'B' ||
3305 Stream.Read(8) != 'C' ||
3306 Stream.Read(4) != 0xC ||
3307 Stream.Read(4) != 0xF ||
3308 Stream.Read(4) != 0xE ||
3309 Stream.Read(4) != 0x0) {
3310 // FIXME: Provide diagnostic.
3311 return NULL;
3312 }
3313
3314 // Create the deserializer.
3315 llvm::Deserializer Dezr(Stream);
3316
Chris Lattnera9376d42009-03-28 03:45:20 +00003317 // ===---------------------------------------------------===/
3318 // Deserialize the "Translation Unit" metadata.
3319 // ===---------------------------------------------------===/
3320
3321 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
3322 // (which will appear earlier) and record its location.
3323
3324 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
3325 assert (FoundBlock);
3326
3327 llvm::Deserializer::Location ASTContextBlockLoc =
3328 Dezr.getCurrentBlockLocation();
3329
3330 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
3331 assert (FoundBlock);
3332
3333 // Read the SourceManager.
3334 SourceManager::CreateAndRegister(Dezr, FMgr);
3335
3336 { // Read the TargetInfo.
3337 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
3338 char* triple = Dezr.ReadCStr(NULL,0,true);
3339 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
3340 delete [] triple;
3341 }
3342
3343 // For Selectors, we must read the identifier table first because the
3344 // SelectorTable depends on the identifiers being already deserialized.
3345 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
3346 Dezr.SkipBlock();
3347
3348 // Read the identifier table.
3349 IdentifierTable::CreateAndRegister(Dezr);
3350
3351 // Now jump back and read the selectors.
3352 Dezr.JumpTo(SelectorBlkLoc);
3353 SelectorTable::CreateAndRegister(Dezr);
3354
3355 // Now jump back to ASTContextBlock and read the ASTContext.
3356 Dezr.JumpTo(ASTContextBlockLoc);
3357 return Dezr.ReadOwnedPtr<ASTContext>();
3358}
3359
Ted Kremenek0f84c002007-11-13 00:25:37 +00003360ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003361
3362 // Read the language options.
3363 LangOptions LOpts;
3364 LOpts.Read(D);
3365
Ted Kremenekfee04522007-10-31 22:44:07 +00003366 SourceManager &SM = D.ReadRef<SourceManager>();
3367 TargetInfo &t = D.ReadRef<TargetInfo>();
3368 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3369 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00003370
Ted Kremenekfee04522007-10-31 22:44:07 +00003371 unsigned size_reserve = D.ReadInt();
3372
Douglas Gregor2e1cd422008-11-17 14:58:09 +00003373 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3374 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00003375
Ted Kremenek03ed4402007-11-13 22:02:55 +00003376 for (unsigned i = 0; i < size_reserve; ++i)
3377 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00003378
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003379 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3380
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003381 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00003382
3383 return A;
3384}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003385
3386ExternalASTSource::~ExternalASTSource() { }
3387
3388void ExternalASTSource::PrintStats() { }