blob: 48d9fe4ea9b9703879fe9ea46f26ac1ee89781c4 [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 =
Steve Naroffab76d452009-04-14 00:03:58 +00002084 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2085 // Use array's original type only if it has known number of
2086 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002087 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002088 PType = PVDecl->getType();
2089 } else if (PType->isFunctionType())
2090 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002091 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002092 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002093 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002094 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002095 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002096 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002097 }
2098}
2099
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002100/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002101/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002102/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2103/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002104/// Property attributes are stored as a comma-delimited C string. The simple
2105/// attributes readonly and bycopy are encoded as single characters. The
2106/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2107/// encoded as single characters, followed by an identifier. Property types
2108/// are also encoded as a parametrized attribute. The characters used to encode
2109/// these attributes are defined by the following enumeration:
2110/// @code
2111/// enum PropertyAttributes {
2112/// kPropertyReadOnly = 'R', // property is read-only.
2113/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2114/// kPropertyByref = '&', // property is a reference to the value last assigned
2115/// kPropertyDynamic = 'D', // property is dynamic
2116/// kPropertyGetter = 'G', // followed by getter selector name
2117/// kPropertySetter = 'S', // followed by setter selector name
2118/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2119/// kPropertyType = 't' // followed by old-style type encoding.
2120/// kPropertyWeak = 'W' // 'weak' property
2121/// kPropertyStrong = 'P' // property GC'able
2122/// kPropertyNonAtomic = 'N' // property non-atomic
2123/// };
2124/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002125void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2126 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002127 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002128 // Collect information from the property implementation decl(s).
2129 bool Dynamic = false;
2130 ObjCPropertyImplDecl *SynthesizePID = 0;
2131
2132 // FIXME: Duplicated code due to poor abstraction.
2133 if (Container) {
2134 if (const ObjCCategoryImplDecl *CID =
2135 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2136 for (ObjCCategoryImplDecl::propimpl_iterator
2137 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2138 ObjCPropertyImplDecl *PID = *i;
2139 if (PID->getPropertyDecl() == PD) {
2140 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2141 Dynamic = true;
2142 } else {
2143 SynthesizePID = PID;
2144 }
2145 }
2146 }
2147 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002148 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002149 for (ObjCCategoryImplDecl::propimpl_iterator
2150 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2151 ObjCPropertyImplDecl *PID = *i;
2152 if (PID->getPropertyDecl() == PD) {
2153 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2154 Dynamic = true;
2155 } else {
2156 SynthesizePID = PID;
2157 }
2158 }
2159 }
2160 }
2161 }
2162
2163 // FIXME: This is not very efficient.
2164 S = "T";
2165
2166 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002167 // GCC has some special rules regarding encoding of properties which
2168 // closely resembles encoding of ivars.
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002169 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002170 true /* outermost type */,
2171 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002172
2173 if (PD->isReadOnly()) {
2174 S += ",R";
2175 } else {
2176 switch (PD->getSetterKind()) {
2177 case ObjCPropertyDecl::Assign: break;
2178 case ObjCPropertyDecl::Copy: S += ",C"; break;
2179 case ObjCPropertyDecl::Retain: S += ",&"; break;
2180 }
2181 }
2182
2183 // It really isn't clear at all what this means, since properties
2184 // are "dynamic by default".
2185 if (Dynamic)
2186 S += ",D";
2187
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002188 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2189 S += ",N";
2190
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002191 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2192 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002193 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002194 }
2195
2196 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2197 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002198 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002199 }
2200
2201 if (SynthesizePID) {
2202 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2203 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002204 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002205 }
2206
2207 // FIXME: OBJCGC: weak & strong
2208}
2209
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002210/// getLegacyIntegralTypeEncoding -
2211/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002212/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002213/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2214///
2215void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2216 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2217 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002218 if (BT->getKind() == BuiltinType::ULong &&
2219 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002220 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002221 else
2222 if (BT->getKind() == BuiltinType::Long &&
2223 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002224 PointeeTy = IntTy;
2225 }
2226 }
2227}
2228
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002229void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002230 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002231 // We follow the behavior of gcc, expanding structures which are
2232 // directly pointed to, and expanding embedded structures. Note that
2233 // these rules are sufficient to prevent recursive encoding of the
2234 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002235 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2236 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002237}
2238
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002239static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002240 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002241 const Expr *E = FD->getBitWidth();
2242 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2243 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2244 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2245 S += 'b';
2246 S += llvm::utostr(N);
2247}
2248
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002249void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2250 bool ExpandPointedToStructures,
2251 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002252 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002253 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002254 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002255 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002256 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002257 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002258 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002259 else {
2260 char encoding;
2261 switch (BT->getKind()) {
2262 default: assert(0 && "Unhandled builtin type kind");
2263 case BuiltinType::Void: encoding = 'v'; break;
2264 case BuiltinType::Bool: encoding = 'B'; break;
2265 case BuiltinType::Char_U:
2266 case BuiltinType::UChar: encoding = 'C'; break;
2267 case BuiltinType::UShort: encoding = 'S'; break;
2268 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002269 case BuiltinType::ULong:
2270 encoding =
2271 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2272 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002273 case BuiltinType::ULongLong: encoding = 'Q'; break;
2274 case BuiltinType::Char_S:
2275 case BuiltinType::SChar: encoding = 'c'; break;
2276 case BuiltinType::Short: encoding = 's'; break;
2277 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002278 case BuiltinType::Long:
2279 encoding =
2280 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2281 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002282 case BuiltinType::LongLong: encoding = 'q'; break;
2283 case BuiltinType::Float: encoding = 'f'; break;
2284 case BuiltinType::Double: encoding = 'd'; break;
2285 case BuiltinType::LongDouble: encoding = 'd'; break;
2286 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002287
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002288 S += encoding;
2289 }
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002290 } else if (const ComplexType *CT = T->getAsComplexType()) {
2291 S += 'j';
2292 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2293 false);
2294 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002295 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2296 ExpandPointedToStructures,
2297 ExpandStructures, FD);
2298 if (FD || EncodingProperty) {
2299 // Note that we do extended encoding of protocol qualifer list
2300 // Only when doing ivar or property encoding.
2301 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2302 S += '"';
2303 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2304 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2305 S += '<';
2306 S += Proto->getNameAsString();
2307 S += '>';
2308 }
2309 S += '"';
2310 }
2311 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002312 }
2313 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002314 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002315 bool isReadOnly = false;
2316 // For historical/compatibility reasons, the read-only qualifier of the
2317 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2318 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2319 // Also, do not emit the 'r' for anything but the outermost type!
2320 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2321 if (OutermostType && T.isConstQualified()) {
2322 isReadOnly = true;
2323 S += 'r';
2324 }
2325 }
2326 else if (OutermostType) {
2327 QualType P = PointeeTy;
2328 while (P->getAsPointerType())
2329 P = P->getAsPointerType()->getPointeeType();
2330 if (P.isConstQualified()) {
2331 isReadOnly = true;
2332 S += 'r';
2333 }
2334 }
2335 if (isReadOnly) {
2336 // Another legacy compatibility encoding. Some ObjC qualifier and type
2337 // combinations need to be rearranged.
2338 // Rewrite "in const" from "nr" to "rn"
2339 const char * s = S.c_str();
2340 int len = S.length();
2341 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2342 std::string replace = "rn";
2343 S.replace(S.end()-2, S.end(), replace);
2344 }
2345 }
Steve Naroff389bf462009-02-12 17:52:19 +00002346 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002347 S += '@';
2348 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002349 }
2350 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002351 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002352 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002353 // Another historical/compatibility reason.
2354 // We encode the underlying type which comes out as
2355 // {...};
2356 S += '^';
2357 getObjCEncodingForTypeImpl(PointeeTy, S,
2358 false, ExpandPointedToStructures,
2359 NULL);
2360 return;
2361 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002362 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002363 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002364 const ObjCInterfaceType *OIT =
2365 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002366 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002367 S += '"';
2368 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002369 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2370 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2371 S += '<';
2372 S += Proto->getNameAsString();
2373 S += '>';
2374 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002375 S += '"';
2376 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002377 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002378 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002379 S += '#';
2380 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002381 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002382 S += ':';
2383 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002384 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002385
2386 if (PointeeTy->isCharType()) {
2387 // char pointer types should be encoded as '*' unless it is a
2388 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002389 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002390 S += '*';
2391 return;
2392 }
2393 }
2394
2395 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002396 getLegacyIntegralTypeEncoding(PointeeTy);
2397
2398 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002399 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002400 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002401 } else if (const ArrayType *AT =
2402 // Ignore type qualifiers etc.
2403 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002404 if (isa<IncompleteArrayType>(AT)) {
2405 // Incomplete arrays are encoded as a pointer to the array element.
2406 S += '^';
2407
2408 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2409 false, ExpandStructures, FD);
2410 } else {
2411 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002412
Anders Carlsson559a8332009-02-22 01:38:57 +00002413 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2414 S += llvm::utostr(CAT->getSize().getZExtValue());
2415 else {
2416 //Variable length arrays are encoded as a regular array with 0 elements.
2417 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2418 S += '0';
2419 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002420
Anders Carlsson559a8332009-02-22 01:38:57 +00002421 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2422 false, ExpandStructures, FD);
2423 S += ']';
2424 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002425 } else if (T->getAsFunctionType()) {
2426 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002427 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002428 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002429 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002430 // Anonymous structures print as '?'
2431 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2432 S += II->getName();
2433 } else {
2434 S += '?';
2435 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002436 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002437 S += '=';
Douglas Gregor6ab35242009-04-09 21:40:53 +00002438 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2439 FieldEnd = RDecl->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +00002440 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002441 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002442 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002443 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002444 S += '"';
2445 }
2446
2447 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002448 if (Field->isBitField()) {
2449 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2450 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002451 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002452 QualType qt = Field->getType();
2453 getLegacyIntegralTypeEncoding(qt);
2454 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002455 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002456 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002457 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002458 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002459 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002460 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002461 if (FD && FD->isBitField())
2462 EncodeBitField(this, S, FD);
2463 else
2464 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002465 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002466 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002467 } else if (T->isObjCInterfaceType()) {
2468 // @encode(class_name)
2469 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2470 S += '{';
2471 const IdentifierInfo *II = OI->getIdentifier();
2472 S += II->getName();
2473 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002474 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002475 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002476 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002477 if (RecFields[i]->isBitField())
2478 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2479 RecFields[i]);
2480 else
2481 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2482 FD);
2483 }
2484 S += '}';
2485 }
2486 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002487 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002488}
2489
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002490void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002491 std::string& S) const {
2492 if (QT & Decl::OBJC_TQ_In)
2493 S += 'n';
2494 if (QT & Decl::OBJC_TQ_Inout)
2495 S += 'N';
2496 if (QT & Decl::OBJC_TQ_Out)
2497 S += 'o';
2498 if (QT & Decl::OBJC_TQ_Bycopy)
2499 S += 'O';
2500 if (QT & Decl::OBJC_TQ_Byref)
2501 S += 'R';
2502 if (QT & Decl::OBJC_TQ_Oneway)
2503 S += 'V';
2504}
2505
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002506void ASTContext::setBuiltinVaListType(QualType T)
2507{
2508 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2509
2510 BuiltinVaListType = T;
2511}
2512
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002513void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002514{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002515 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002516
2517 // typedef struct objc_object *id;
2518 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002519 // User error - caller will issue diagnostics.
2520 if (!ptr)
2521 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002522 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002523 // User error - caller will issue diagnostics.
2524 if (!rec)
2525 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002526 IdStructType = rec;
2527}
2528
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002529void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002530{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002531 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002532
2533 // typedef struct objc_selector *SEL;
2534 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002535 if (!ptr)
2536 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002537 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002538 if (!rec)
2539 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002540 SelStructType = rec;
2541}
2542
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002543void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002544{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002545 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002546}
2547
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002548void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002549{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002550 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002551
2552 // typedef struct objc_class *Class;
2553 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2554 assert(ptr && "'Class' incorrectly typed");
2555 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2556 assert(rec && "'Class' incorrectly typed");
2557 ClassStructType = rec;
2558}
2559
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002560void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2561 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002562 "'NSConstantString' type already set!");
2563
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002564 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002565}
2566
Douglas Gregor7532dc62009-03-30 22:58:21 +00002567/// \brief Retrieve the template name that represents a qualified
2568/// template name such as \c std::vector.
2569TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2570 bool TemplateKeyword,
2571 TemplateDecl *Template) {
2572 llvm::FoldingSetNodeID ID;
2573 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2574
2575 void *InsertPos = 0;
2576 QualifiedTemplateName *QTN =
2577 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2578 if (!QTN) {
2579 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2580 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2581 }
2582
2583 return TemplateName(QTN);
2584}
2585
2586/// \brief Retrieve the template name that represents a dependent
2587/// template name such as \c MetaFun::template apply.
2588TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2589 const IdentifierInfo *Name) {
2590 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2591
2592 llvm::FoldingSetNodeID ID;
2593 DependentTemplateName::Profile(ID, NNS, Name);
2594
2595 void *InsertPos = 0;
2596 DependentTemplateName *QTN =
2597 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2598
2599 if (QTN)
2600 return TemplateName(QTN);
2601
2602 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2603 if (CanonNNS == NNS) {
2604 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2605 } else {
2606 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2607 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2608 }
2609
2610 DependentTemplateNames.InsertNode(QTN, InsertPos);
2611 return TemplateName(QTN);
2612}
2613
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002614/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002615/// TargetInfo, produce the corresponding type. The unsigned @p Type
2616/// is actually a value of type @c TargetInfo::IntType.
2617QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002618 switch (Type) {
2619 case TargetInfo::NoInt: return QualType();
2620 case TargetInfo::SignedShort: return ShortTy;
2621 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2622 case TargetInfo::SignedInt: return IntTy;
2623 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2624 case TargetInfo::SignedLong: return LongTy;
2625 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2626 case TargetInfo::SignedLongLong: return LongLongTy;
2627 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2628 }
2629
2630 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002631 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002632}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002633
2634//===----------------------------------------------------------------------===//
2635// Type Predicates.
2636//===----------------------------------------------------------------------===//
2637
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002638/// isObjCNSObjectType - Return true if this is an NSObject object using
2639/// NSObject attribute on a c-style pointer type.
2640/// FIXME - Make it work directly on types.
2641///
2642bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2643 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2644 if (TypedefDecl *TD = TDT->getDecl())
2645 if (TD->getAttr<ObjCNSObjectAttr>())
2646 return true;
2647 }
2648 return false;
2649}
2650
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002651/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2652/// to an object type. This includes "id" and "Class" (two 'special' pointers
2653/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2654/// ID type).
2655bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002656 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002657 return true;
2658
Steve Naroff6ae98502008-10-21 18:24:04 +00002659 // Blocks are objects.
2660 if (Ty->isBlockPointerType())
2661 return true;
2662
2663 // All other object types are pointers.
Chris Lattner16ede0e2009-04-12 23:51:02 +00002664 const PointerType *PT = Ty->getAsPointerType();
2665 if (PT == 0)
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002666 return false;
2667
Chris Lattner16ede0e2009-04-12 23:51:02 +00002668 // If this a pointer to an interface (e.g. NSString*), it is ok.
2669 if (PT->getPointeeType()->isObjCInterfaceType() ||
2670 // If is has NSObject attribute, OK as well.
2671 isObjCNSObjectType(Ty))
2672 return true;
2673
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002674 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2675 // pointer types. This looks for the typedef specifically, not for the
Chris Lattner16ede0e2009-04-12 23:51:02 +00002676 // underlying type. Iteratively strip off typedefs so that we can handle
2677 // typedefs of typedefs.
2678 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2679 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2680 Ty.getUnqualifiedType() == getObjCClassType())
2681 return true;
2682
2683 Ty = TDT->getDecl()->getUnderlyingType();
2684 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002685
Chris Lattner16ede0e2009-04-12 23:51:02 +00002686 return false;
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002687}
2688
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002689/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2690/// garbage collection attribute.
2691///
2692QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002693 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002694 if (getLangOptions().ObjC1 &&
2695 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002696 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002697 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002698 // (or pointers to them) be treated as though they were declared
2699 // as __strong.
2700 if (GCAttrs == QualType::GCNone) {
2701 if (isObjCObjectPointerType(Ty))
2702 GCAttrs = QualType::Strong;
2703 else if (Ty->isPointerType())
2704 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2705 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00002706 // Non-pointers have none gc'able attribute regardless of the attribute
2707 // set on them.
2708 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2709 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002710 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002711 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002712}
2713
Chris Lattner6ac46a42008-04-07 06:51:04 +00002714//===----------------------------------------------------------------------===//
2715// Type Compatibility Testing
2716//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002717
Steve Naroff1c7d0672008-09-04 15:10:53 +00002718/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002719/// block types. Types must be strictly compatible here. For example,
2720/// C unfortunately doesn't produce an error for the following:
2721///
2722/// int (*emptyArgFunc)();
2723/// int (*intArgList)(int) = emptyArgFunc;
2724///
2725/// For blocks, we will produce an error for the following (similar to C++):
2726///
2727/// int (^emptyArgBlock)();
2728/// int (^intArgBlock)(int) = emptyArgBlock;
2729///
2730/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2731///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002732bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002733 const FunctionType *lbase = lhs->getAsFunctionType();
2734 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002735 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2736 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stumpaab0f7a2009-04-01 01:17:39 +00002737 if (lproto && rproto == 0)
2738 return false;
2739 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff1c7d0672008-09-04 15:10:53 +00002740}
2741
Chris Lattner6ac46a42008-04-07 06:51:04 +00002742/// areCompatVectorTypes - Return true if the two specified vector types are
2743/// compatible.
2744static bool areCompatVectorTypes(const VectorType *LHS,
2745 const VectorType *RHS) {
2746 assert(LHS->isCanonical() && RHS->isCanonical());
2747 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002748 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002749}
2750
Eli Friedman3d815e72008-08-22 00:56:42 +00002751/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002752/// compatible for assignment from RHS to LHS. This handles validation of any
2753/// protocol qualifiers on the LHS or RHS.
2754///
Eli Friedman3d815e72008-08-22 00:56:42 +00002755bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2756 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002757 // Verify that the base decls are compatible: the RHS must be a subclass of
2758 // the LHS.
2759 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2760 return false;
2761
2762 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2763 // protocol qualified at all, then we are good.
2764 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2765 return true;
2766
2767 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2768 // isn't a superset.
2769 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2770 return true; // FIXME: should return false!
2771
2772 // Finally, we must have two protocol-qualified interfaces.
2773 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2774 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002775
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002776 // All LHS protocols must have a presence on the RHS.
2777 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002778
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002779 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2780 LHSPE = LHSP->qual_end();
2781 LHSPI != LHSPE; LHSPI++) {
2782 bool RHSImplementsProtocol = false;
2783
2784 // If the RHS doesn't implement the protocol on the left, the types
2785 // are incompatible.
2786 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2787 RHSPE = RHSP->qual_end();
2788 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2789 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2790 RHSImplementsProtocol = true;
2791 }
2792 // FIXME: For better diagnostics, consider passing back the protocol name.
2793 if (!RHSImplementsProtocol)
2794 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002795 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002796 // The RHS implements all protocols listed on the LHS.
2797 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002798}
2799
Steve Naroff389bf462009-02-12 17:52:19 +00002800bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2801 // get the "pointed to" types
2802 const PointerType *LHSPT = LHS->getAsPointerType();
2803 const PointerType *RHSPT = RHS->getAsPointerType();
2804
2805 if (!LHSPT || !RHSPT)
2806 return false;
2807
2808 QualType lhptee = LHSPT->getPointeeType();
2809 QualType rhptee = RHSPT->getPointeeType();
2810 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2811 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2812 // ID acts sort of like void* for ObjC interfaces
2813 if (LHSIface && isObjCIdStructType(rhptee))
2814 return true;
2815 if (RHSIface && isObjCIdStructType(lhptee))
2816 return true;
2817 if (!LHSIface || !RHSIface)
2818 return false;
2819 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2820 canAssignObjCInterfaces(RHSIface, LHSIface);
2821}
2822
Steve Naroffec0550f2007-10-15 20:41:53 +00002823/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2824/// both shall have the identically qualified version of a compatible type.
2825/// C99 6.2.7p1: Two types have compatible types if their types are the
2826/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002827bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2828 return !mergeTypes(LHS, RHS).isNull();
2829}
2830
2831QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2832 const FunctionType *lbase = lhs->getAsFunctionType();
2833 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002834 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2835 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002836 bool allLTypes = true;
2837 bool allRTypes = true;
2838
2839 // Check return type
2840 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2841 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002842 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2843 allLTypes = false;
2844 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2845 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002846
2847 if (lproto && rproto) { // two C99 style function prototypes
2848 unsigned lproto_nargs = lproto->getNumArgs();
2849 unsigned rproto_nargs = rproto->getNumArgs();
2850
2851 // Compatible functions must have the same number of arguments
2852 if (lproto_nargs != rproto_nargs)
2853 return QualType();
2854
2855 // Variadic and non-variadic functions aren't compatible
2856 if (lproto->isVariadic() != rproto->isVariadic())
2857 return QualType();
2858
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002859 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2860 return QualType();
2861
Eli Friedman3d815e72008-08-22 00:56:42 +00002862 // Check argument compatibility
2863 llvm::SmallVector<QualType, 10> types;
2864 for (unsigned i = 0; i < lproto_nargs; i++) {
2865 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2866 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2867 QualType argtype = mergeTypes(largtype, rargtype);
2868 if (argtype.isNull()) return QualType();
2869 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002870 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2871 allLTypes = false;
2872 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2873 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002874 }
2875 if (allLTypes) return lhs;
2876 if (allRTypes) return rhs;
2877 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002878 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002879 }
2880
2881 if (lproto) allRTypes = false;
2882 if (rproto) allLTypes = false;
2883
Douglas Gregor72564e72009-02-26 23:50:07 +00002884 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002885 if (proto) {
2886 if (proto->isVariadic()) return QualType();
2887 // Check that the types are compatible with the types that
2888 // would result from default argument promotions (C99 6.7.5.3p15).
2889 // The only types actually affected are promotable integer
2890 // types and floats, which would be passed as a different
2891 // type depending on whether the prototype is visible.
2892 unsigned proto_nargs = proto->getNumArgs();
2893 for (unsigned i = 0; i < proto_nargs; ++i) {
2894 QualType argTy = proto->getArgType(i);
2895 if (argTy->isPromotableIntegerType() ||
2896 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2897 return QualType();
2898 }
2899
2900 if (allLTypes) return lhs;
2901 if (allRTypes) return rhs;
2902 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002903 proto->getNumArgs(), lproto->isVariadic(),
2904 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002905 }
2906
2907 if (allLTypes) return lhs;
2908 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002909 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002910}
2911
2912QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002913 // C++ [expr]: If an expression initially has the type "reference to T", the
2914 // type is adjusted to "T" prior to any further analysis, the expression
2915 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002916 // expression is an lvalue unless the reference is an rvalue reference and
2917 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00002918 // FIXME: C++ shouldn't be going through here! The rules are different
2919 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002920 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2921 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00002922 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002923 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002924 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002925 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002926
Eli Friedman3d815e72008-08-22 00:56:42 +00002927 QualType LHSCan = getCanonicalType(LHS),
2928 RHSCan = getCanonicalType(RHS);
2929
2930 // If two types are identical, they are compatible.
2931 if (LHSCan == RHSCan)
2932 return LHS;
2933
2934 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002935 // Note that we handle extended qualifiers later, in the
2936 // case for ExtQualType.
2937 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00002938 return QualType();
2939
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00002940 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
2941 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00002942
Chris Lattner1adb8832008-01-14 05:45:46 +00002943 // We want to consider the two function types to be the same for these
2944 // comparisons, just force one to the other.
2945 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2946 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002947
2948 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002949 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2950 LHSClass = Type::ConstantArray;
2951 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2952 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002953
Nate Begeman213541a2008-04-18 23:10:10 +00002954 // Canonicalize ExtVector -> Vector.
2955 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2956 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002957
Chris Lattnerb0489812008-04-07 06:38:24 +00002958 // Consider qualified interfaces and interfaces the same.
2959 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2960 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002961
Chris Lattnera36a61f2008-04-07 05:43:21 +00002962 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002963 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002964 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2965 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00002966
Steve Naroffd824c9c2009-04-14 15:11:46 +00002967 // 'id' and 'Class' act sort of like void* for ObjC interfaces
2968 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00002969 return LHS;
Steve Naroffd824c9c2009-04-14 15:11:46 +00002970 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00002971 return RHS;
2972
Steve Naroffbc76dd02008-12-10 22:14:21 +00002973 // ID is compatible with all qualified id types.
2974 if (LHS->isObjCQualifiedIdType()) {
2975 if (const PointerType *PT = RHS->getAsPointerType()) {
2976 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00002977 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002978 return LHS;
2979 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2980 // Unfortunately, this API is part of Sema (which we don't have access
2981 // to. Need to refactor. The following check is insufficient, since we
2982 // need to make sure the class implements the protocol.
2983 if (pType->isObjCInterfaceType())
2984 return LHS;
2985 }
2986 }
2987 if (RHS->isObjCQualifiedIdType()) {
2988 if (const PointerType *PT = LHS->getAsPointerType()) {
2989 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00002990 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002991 return RHS;
2992 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2993 // Unfortunately, this API is part of Sema (which we don't have access
2994 // to. Need to refactor. The following check is insufficient, since we
2995 // need to make sure the class implements the protocol.
2996 if (pType->isObjCInterfaceType())
2997 return RHS;
2998 }
2999 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003000 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3001 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00003002 if (const EnumType* ETy = LHS->getAsEnumType()) {
3003 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3004 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003005 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003006 if (const EnumType* ETy = RHS->getAsEnumType()) {
3007 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3008 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003009 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003010
Eli Friedman3d815e72008-08-22 00:56:42 +00003011 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003012 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003013
Steve Naroff4a746782008-01-09 22:43:08 +00003014 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003015 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003016#define TYPE(Class, Base)
3017#define ABSTRACT_TYPE(Class, Base)
3018#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3019#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3020#include "clang/AST/TypeNodes.def"
3021 assert(false && "Non-canonical and dependent types shouldn't get here");
3022 return QualType();
3023
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003024 case Type::LValueReference:
3025 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003026 case Type::MemberPointer:
3027 assert(false && "C++ should never be in mergeTypes");
3028 return QualType();
3029
3030 case Type::IncompleteArray:
3031 case Type::VariableArray:
3032 case Type::FunctionProto:
3033 case Type::ExtVector:
3034 case Type::ObjCQualifiedInterface:
3035 assert(false && "Types are eliminated above");
3036 return QualType();
3037
Chris Lattner1adb8832008-01-14 05:45:46 +00003038 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003039 {
3040 // Merge two pointer types, while trying to preserve typedef info
3041 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3042 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3043 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3044 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003045 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3046 return LHS;
3047 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3048 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003049 return getPointerType(ResultType);
3050 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003051 case Type::BlockPointer:
3052 {
3053 // Merge two block pointer types, while trying to preserve typedef info
3054 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3055 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3056 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3057 if (ResultType.isNull()) return QualType();
3058 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3059 return LHS;
3060 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3061 return RHS;
3062 return getBlockPointerType(ResultType);
3063 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003064 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003065 {
3066 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3067 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3068 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3069 return QualType();
3070
3071 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3072 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3073 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3074 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003075 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3076 return LHS;
3077 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3078 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003079 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3080 ArrayType::ArraySizeModifier(), 0);
3081 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3082 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003083 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3084 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003085 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3086 return LHS;
3087 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3088 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003089 if (LVAT) {
3090 // FIXME: This isn't correct! But tricky to implement because
3091 // the array's size has to be the size of LHS, but the type
3092 // has to be different.
3093 return LHS;
3094 }
3095 if (RVAT) {
3096 // FIXME: This isn't correct! But tricky to implement because
3097 // the array's size has to be the size of RHS, but the type
3098 // has to be different.
3099 return RHS;
3100 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003101 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3102 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003103 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003104 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003105 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003106 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003107 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003108 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003109 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003110 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3111 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003112 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003113 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003114 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003115 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003116 case Type::Complex:
3117 // Distinct complex types are incompatible.
3118 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003119 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003120 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003121 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3122 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003123 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003124 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003125 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003126 // FIXME: This should be type compatibility, e.g. whether
3127 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003128 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3129 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3130 if (LHSIface && RHSIface &&
3131 canAssignObjCInterfaces(LHSIface, RHSIface))
3132 return LHS;
3133
Eli Friedman3d815e72008-08-22 00:56:42 +00003134 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003135 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003136 case Type::ObjCQualifiedId:
3137 // Distinct qualified id's are not compatible.
3138 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003139 case Type::FixedWidthInt:
3140 // Distinct fixed-width integers are not compatible.
3141 return QualType();
3142 case Type::ObjCQualifiedClass:
3143 // Distinct qualified classes are not compatible.
3144 return QualType();
3145 case Type::ExtQual:
3146 // FIXME: ExtQual types can be compatible even if they're not
3147 // identical!
3148 return QualType();
3149 // First attempt at an implementation, but I'm not really sure it's
3150 // right...
3151#if 0
3152 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3153 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3154 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3155 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3156 return QualType();
3157 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3158 LHSBase = QualType(LQual->getBaseType(), 0);
3159 RHSBase = QualType(RQual->getBaseType(), 0);
3160 ResultType = mergeTypes(LHSBase, RHSBase);
3161 if (ResultType.isNull()) return QualType();
3162 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3163 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3164 return LHS;
3165 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3166 return RHS;
3167 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3168 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3169 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3170 return ResultType;
3171#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003172
3173 case Type::TemplateSpecialization:
3174 assert(false && "Dependent types have no size");
3175 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003176 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003177
3178 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003179}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003180
Chris Lattner5426bf62008-04-07 07:01:58 +00003181//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003182// Integer Predicates
3183//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003184
Eli Friedmanad74a752008-06-28 06:23:08 +00003185unsigned ASTContext::getIntWidth(QualType T) {
3186 if (T == BoolTy)
3187 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003188 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3189 return FWIT->getWidth();
3190 }
3191 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003192 return (unsigned)getTypeSize(T);
3193}
3194
3195QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3196 assert(T->isSignedIntegerType() && "Unexpected type");
3197 if (const EnumType* ETy = T->getAsEnumType())
3198 T = ETy->getDecl()->getIntegerType();
3199 const BuiltinType* BTy = T->getAsBuiltinType();
3200 assert (BTy && "Unexpected signed integer type");
3201 switch (BTy->getKind()) {
3202 case BuiltinType::Char_S:
3203 case BuiltinType::SChar:
3204 return UnsignedCharTy;
3205 case BuiltinType::Short:
3206 return UnsignedShortTy;
3207 case BuiltinType::Int:
3208 return UnsignedIntTy;
3209 case BuiltinType::Long:
3210 return UnsignedLongTy;
3211 case BuiltinType::LongLong:
3212 return UnsignedLongLongTy;
3213 default:
3214 assert(0 && "Unexpected signed integer type");
3215 return QualType();
3216 }
3217}
3218
3219
3220//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00003221// Serialization Support
3222//===----------------------------------------------------------------------===//
3223
Chris Lattnera9376d42009-03-28 03:45:20 +00003224enum {
3225 BasicMetadataBlock = 1,
3226 ASTContextBlock = 2,
3227 DeclsBlock = 3
3228};
3229
Chris Lattner557c5b12009-03-28 04:27:18 +00003230void ASTContext::EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const{
3231 // Create bitstream.
3232 llvm::BitstreamWriter Stream(Buffer);
3233
3234 // Emit the preamble.
3235 Stream.Emit((unsigned)'B', 8);
3236 Stream.Emit((unsigned)'C', 8);
3237 Stream.Emit(0xC, 4);
3238 Stream.Emit(0xF, 4);
3239 Stream.Emit(0xE, 4);
3240 Stream.Emit(0x0, 4);
3241
3242 // Create serializer.
3243 llvm::Serializer S(Stream);
3244
Chris Lattnera9376d42009-03-28 03:45:20 +00003245 // ===---------------------------------------------------===/
3246 // Serialize the "Translation Unit" metadata.
3247 // ===---------------------------------------------------===/
3248
3249 // Emit ASTContext.
3250 S.EnterBlock(ASTContextBlock);
3251 S.EmitOwnedPtr(this);
3252 S.ExitBlock(); // exit "ASTContextBlock"
3253
3254 S.EnterBlock(BasicMetadataBlock);
3255
3256 // Block for SourceManager and Target. Allows easy skipping
3257 // around to the block for the Selectors during deserialization.
3258 S.EnterBlock();
3259
3260 // Emit the SourceManager.
3261 S.Emit(getSourceManager());
3262
3263 // Emit the Target.
3264 S.EmitPtr(&Target);
3265 S.EmitCStr(Target.getTargetTriple());
3266
3267 S.ExitBlock(); // exit "SourceManager and Target Block"
3268
3269 // Emit the Selectors.
3270 S.Emit(Selectors);
3271
3272 // Emit the Identifier Table.
3273 S.Emit(Idents);
3274
3275 S.ExitBlock(); // exit "BasicMetadataBlock"
3276}
3277
3278
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003279/// Emit - Serialize an ASTContext object to Bitcode.
3280void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003281 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00003282 S.EmitRef(SourceMgr);
3283 S.EmitRef(Target);
3284 S.EmitRef(Idents);
3285 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003286
Ted Kremenekfee04522007-10-31 22:44:07 +00003287 // Emit the size of the type vector so that we can reserve that size
3288 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00003289 S.EmitInt(Types.size());
3290
Ted Kremenek03ed4402007-11-13 22:02:55 +00003291 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3292 I!=E;++I)
3293 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00003294
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003295 S.EmitOwnedPtr(TUDecl);
3296
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003297 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003298}
3299
Chris Lattner557c5b12009-03-28 04:27:18 +00003300
3301ASTContext *ASTContext::ReadASTBitcodeBuffer(llvm::MemoryBuffer &Buffer,
3302 FileManager &FMgr) {
3303 // Check if the file is of the proper length.
3304 if (Buffer.getBufferSize() & 0x3) {
3305 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
3306 return 0;
3307 }
3308
3309 // Create the bitstream reader.
3310 unsigned char *BufPtr = (unsigned char *)Buffer.getBufferStart();
3311 llvm::BitstreamReader Stream(BufPtr, BufPtr+Buffer.getBufferSize());
3312
3313 if (Stream.Read(8) != 'B' ||
3314 Stream.Read(8) != 'C' ||
3315 Stream.Read(4) != 0xC ||
3316 Stream.Read(4) != 0xF ||
3317 Stream.Read(4) != 0xE ||
3318 Stream.Read(4) != 0x0) {
3319 // FIXME: Provide diagnostic.
3320 return NULL;
3321 }
3322
3323 // Create the deserializer.
3324 llvm::Deserializer Dezr(Stream);
3325
Chris Lattnera9376d42009-03-28 03:45:20 +00003326 // ===---------------------------------------------------===/
3327 // Deserialize the "Translation Unit" metadata.
3328 // ===---------------------------------------------------===/
3329
3330 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
3331 // (which will appear earlier) and record its location.
3332
3333 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
3334 assert (FoundBlock);
3335
3336 llvm::Deserializer::Location ASTContextBlockLoc =
3337 Dezr.getCurrentBlockLocation();
3338
3339 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
3340 assert (FoundBlock);
3341
3342 // Read the SourceManager.
3343 SourceManager::CreateAndRegister(Dezr, FMgr);
3344
3345 { // Read the TargetInfo.
3346 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
3347 char* triple = Dezr.ReadCStr(NULL,0,true);
3348 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
3349 delete [] triple;
3350 }
3351
3352 // For Selectors, we must read the identifier table first because the
3353 // SelectorTable depends on the identifiers being already deserialized.
3354 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
3355 Dezr.SkipBlock();
3356
3357 // Read the identifier table.
3358 IdentifierTable::CreateAndRegister(Dezr);
3359
3360 // Now jump back and read the selectors.
3361 Dezr.JumpTo(SelectorBlkLoc);
3362 SelectorTable::CreateAndRegister(Dezr);
3363
3364 // Now jump back to ASTContextBlock and read the ASTContext.
3365 Dezr.JumpTo(ASTContextBlockLoc);
3366 return Dezr.ReadOwnedPtr<ASTContext>();
3367}
3368
Ted Kremenek0f84c002007-11-13 00:25:37 +00003369ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003370
3371 // Read the language options.
3372 LangOptions LOpts;
3373 LOpts.Read(D);
3374
Ted Kremenekfee04522007-10-31 22:44:07 +00003375 SourceManager &SM = D.ReadRef<SourceManager>();
3376 TargetInfo &t = D.ReadRef<TargetInfo>();
3377 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3378 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00003379
Ted Kremenekfee04522007-10-31 22:44:07 +00003380 unsigned size_reserve = D.ReadInt();
3381
Douglas Gregor2e1cd422008-11-17 14:58:09 +00003382 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3383 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00003384
Ted Kremenek03ed4402007-11-13 22:02:55 +00003385 for (unsigned i = 0; i < size_reserve; ++i)
3386 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00003387
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003388 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3389
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003390 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00003391
3392 return A;
3393}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003394
3395ExternalASTSource::~ExternalASTSource() { }
3396
3397void ExternalASTSource::PrintStats() { }