blob: 4aed59f37e8cfd81d04b6209a22e6f3592ac57cf [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000019#include "clang/AST/ExternalASTSource.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattnerb09b31d2009-03-28 03:45:20 +000021#include "clang/Basic/SourceManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000022#include "clang/Basic/TargetInfo.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000023#include "llvm/ADT/StringExtras.h"
Nate Begeman7903d052009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Chris Lattnerf4fbc442009-03-28 04:27:18 +000025#include "llvm/Support/MemoryBuffer.h"
Chris Lattner4b009652007-07-25 00:24:17 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner2fda0ed2008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Douglas Gregorda38c6c2009-04-22 18:49:13 +000035 bool FreeMem, unsigned size_reserve,
36 bool InitializeBuiltins) :
Douglas Gregor1e589cc2009-03-26 23:50:42 +000037 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
38 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Douglas Gregorc34897d2009-04-09 22:27:44 +000039 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels),
40 ExternalSource(0) {
Daniel Dunbarde300732008-08-11 04:54:23 +000041 if (size_reserve > 0) Types.reserve(size_reserve);
42 InitBuiltinTypes();
Daniel Dunbarde300732008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
Douglas Gregord37ce612009-04-26 03:57:37 +000044 BuiltinInfo.InitializeTargetBuiltins(Target);
Douglas Gregorda38c6c2009-04-22 18:49:13 +000045 if (InitializeBuiltins)
46 this->InitializeBuiltins(idents);
Daniel Dunbarde300732008-08-11 04:54:23 +000047}
48
Chris Lattner4b009652007-07-25 00:24:17 +000049ASTContext::~ASTContext() {
50 // Deallocate all the types.
51 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000052 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000053 Types.pop_back();
54 }
Eli Friedman65489b72008-05-27 03:08:09 +000055
Nuno Lopes355a8682008-12-17 22:30:25 +000056 {
57 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
58 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
59 while (I != E) {
60 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
61 delete R;
62 }
63 }
64
65 {
Daniel Dunbar1fbaef12009-05-03 10:38:35 +000066 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
67 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopes355a8682008-12-17 22:30:25 +000068 while (I != E) {
69 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
70 delete R;
71 }
72 }
73
Douglas Gregor1e589cc2009-03-26 23:50:42 +000074 // Destroy nested-name-specifiers.
Douglas Gregor3c4eae52009-03-27 23:54:10 +000075 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
76 NNS = NestedNameSpecifiers.begin(),
77 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregorbccd97c2009-03-27 23:25:45 +000078 NNS != NNSEnd;
Douglas Gregor3c4eae52009-03-27 23:54:10 +000079 /* Increment in loop */)
80 (*NNS++).Destroy(*this);
Douglas Gregor1e589cc2009-03-26 23:50:42 +000081
82 if (GlobalNestedNameSpecifier)
83 GlobalNestedNameSpecifier->Destroy(*this);
84
Eli Friedman65489b72008-05-27 03:08:09 +000085 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000086}
87
Douglas Gregorda38c6c2009-04-22 18:49:13 +000088void ASTContext::InitializeBuiltins(IdentifierTable &idents) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +000089 BuiltinInfo.InitializeBuiltins(idents, LangOpts.NoBuiltin);
90}
91
Douglas Gregorc34897d2009-04-09 22:27:44 +000092void
93ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
94 ExternalSource.reset(Source.take());
95}
96
Chris Lattner4b009652007-07-25 00:24:17 +000097void ASTContext::PrintStats() const {
98 fprintf(stderr, "*** AST Context Stats:\n");
99 fprintf(stderr, " %d types total.\n", (int)Types.size());
100 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar47677342008-09-26 03:23:00 +0000101 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000102 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
103 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
104
Chris Lattner4b009652007-07-25 00:24:17 +0000105 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000106 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
107 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000108 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000109 unsigned NumExtQual = 0;
110
Chris Lattner4b009652007-07-25 00:24:17 +0000111 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
112 Type *T = Types[i];
113 if (isa<BuiltinType>(T))
114 ++NumBuiltin;
115 else if (isa<PointerType>(T))
116 ++NumPointer;
Daniel Dunbar47677342008-09-26 03:23:00 +0000117 else if (isa<BlockPointerType>(T))
118 ++NumBlockPointer;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000119 else if (isa<LValueReferenceType>(T))
120 ++NumLValueReference;
121 else if (isa<RValueReferenceType>(T))
122 ++NumRValueReference;
Sebastian Redl75555032009-01-24 21:16:55 +0000123 else if (isa<MemberPointerType>(T))
124 ++NumMemberPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000125 else if (isa<ComplexType>(T))
126 ++NumComplex;
127 else if (isa<ArrayType>(T))
128 ++NumArray;
129 else if (isa<VectorType>(T))
130 ++NumVector;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000131 else if (isa<FunctionNoProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000132 ++NumFunctionNP;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000133 else if (isa<FunctionProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000134 ++NumFunctionP;
135 else if (isa<TypedefType>(T))
136 ++NumTypeName;
137 else if (TagType *TT = dyn_cast<TagType>(T)) {
138 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000139 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000140 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000141 case TagDecl::TK_struct: ++NumTagStruct; break;
142 case TagDecl::TK_union: ++NumTagUnion; break;
143 case TagDecl::TK_class: ++NumTagClass; break;
144 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000145 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000146 } else if (isa<ObjCInterfaceType>(T))
147 ++NumObjCInterfaces;
148 else if (isa<ObjCQualifiedInterfaceType>(T))
149 ++NumObjCQualifiedInterfaces;
150 else if (isa<ObjCQualifiedIdType>(T))
151 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +0000152 else if (isa<TypeOfType>(T))
153 ++NumTypeOfTypes;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000154 else if (isa<TypeOfExprType>(T))
155 ++NumTypeOfExprTypes;
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000156 else if (isa<ExtQualType>(T))
157 ++NumExtQual;
Steve Naroff948fd372007-09-17 14:16:13 +0000158 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000159 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000160 assert(0 && "Unknown type!");
161 }
162 }
163
164 fprintf(stderr, " %d builtin types\n", NumBuiltin);
165 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar47677342008-09-26 03:23:00 +0000166 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000167 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
168 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redl75555032009-01-24 21:16:55 +0000169 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000170 fprintf(stderr, " %d complex types\n", NumComplex);
171 fprintf(stderr, " %d array types\n", NumArray);
172 fprintf(stderr, " %d vector types\n", NumVector);
173 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
174 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
175 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
176 fprintf(stderr, " %d tagged types\n", NumTagged);
177 fprintf(stderr, " %d struct types\n", NumTagStruct);
178 fprintf(stderr, " %d union types\n", NumTagUnion);
179 fprintf(stderr, " %d class types\n", NumTagClass);
180 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000181 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000182 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000183 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000184 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000185 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000186 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000187 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000188 fprintf(stderr, " %d attribute-qualified types\n", NumExtQual);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000189
Chris Lattner4b009652007-07-25 00:24:17 +0000190 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
191 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
192 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redlce6fff02009-03-16 23:22:08 +0000193 NumLValueReference*sizeof(LValueReferenceType)+
194 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redl75555032009-01-24 21:16:55 +0000195 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor4fa58902009-02-26 23:50:07 +0000196 NumFunctionP*sizeof(FunctionProtoType)+
197 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroffe0430632008-05-21 15:59:22 +0000198 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000199 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)+
200 NumExtQual*sizeof(ExtQualType)));
Douglas Gregorc34897d2009-04-09 22:27:44 +0000201
202 if (ExternalSource.get()) {
203 fprintf(stderr, "\n");
204 ExternalSource->PrintStats();
205 }
Chris Lattner4b009652007-07-25 00:24:17 +0000206}
207
208
209void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroff93fd2112009-01-27 22:08:43 +0000210 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000211}
212
Chris Lattner4b009652007-07-25 00:24:17 +0000213void ASTContext::InitBuiltinTypes() {
214 assert(VoidTy.isNull() && "Context reinitialized?");
215
216 // C99 6.2.5p19.
217 InitBuiltinType(VoidTy, BuiltinType::Void);
218
219 // C99 6.2.5p2.
220 InitBuiltinType(BoolTy, BuiltinType::Bool);
221 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000222 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000223 InitBuiltinType(CharTy, BuiltinType::Char_S);
224 else
225 InitBuiltinType(CharTy, BuiltinType::Char_U);
226 // C99 6.2.5p4.
227 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
228 InitBuiltinType(ShortTy, BuiltinType::Short);
229 InitBuiltinType(IntTy, BuiltinType::Int);
230 InitBuiltinType(LongTy, BuiltinType::Long);
231 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
232
233 // C99 6.2.5p6.
234 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
235 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
236 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
237 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
238 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
239
240 // C99 6.2.5p10.
241 InitBuiltinType(FloatTy, BuiltinType::Float);
242 InitBuiltinType(DoubleTy, BuiltinType::Double);
243 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000244
Chris Lattner6cc7e412009-04-30 02:43:43 +0000245 // GNU extension, 128-bit integers.
246 InitBuiltinType(Int128Ty, BuiltinType::Int128);
247 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
248
Chris Lattnere1dafe72009-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());
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000253
Douglas Gregord2baafd2008-10-21 16:13:35 +0000254 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-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 Gregord2baafd2008-10-21 16:13:35 +0000263
Chris Lattner4b009652007-07-25 00:24:17 +0000264 // C99 6.2.5p11.
265 FloatComplexTy = getComplexType(FloatTy);
266 DoubleComplexTy = getComplexType(DoubleTy);
267 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000268
Steve Naroff9d12c902007-10-15 14:41:52 +0000269 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000270 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000271 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000272 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000273 ClassStructType = 0;
274
Ted Kremenek42730c52008-01-07 19:49:32 +0000275 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000276
277 // void * type
278 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000279}
280
281//===----------------------------------------------------------------------===//
282// Type Sizing and Analysis
283//===----------------------------------------------------------------------===//
284
Chris Lattner2a674dc2008-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 Lattnerbd3153e2009-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 Dunbar96d1f1b2009-02-17 22:16:19 +0000301unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedman0ee57322009-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 Lattnerbd3153e2009-01-24 21:53:27 +0000307 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
308 QualType T = VD->getType();
Anders Carlssonaa0783b2009-04-10 04:47:03 +0000309 if (const ReferenceType* RT = T->getAsReferenceType()) {
310 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssoneeaeda32009-04-10 04:52:36 +0000311 Align = Target.getPointerAlign(AS);
Anders Carlssonaa0783b2009-04-10 04:47:03 +0000312 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
313 // Incomplete or function types default to 1.
Eli Friedman0ee57322009-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 Lattnerbd3153e2009-01-24 21:53:27 +0000319 }
Eli Friedman0ee57322009-02-22 02:56:25 +0000320
321 return Align / Target.getCharWidth();
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000322}
Chris Lattner2a674dc2008-06-30 18:32:54 +0000323
Chris Lattner4b009652007-07-25 00:24:17 +0000324/// getTypeSize - Return the size of the specified type, in bits. This method
325/// does not work on incomplete types.
326std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000327ASTContext::getTypeInfo(const Type *T) {
Mike Stump44d1f402009-02-27 18:32:39 +0000328 uint64_t Width=0;
329 unsigned Align=8;
Chris Lattner4b009652007-07-25 00:24:17 +0000330 switch (T->getTypeClass()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000331#define TYPE(Class, Base)
332#define ABSTRACT_TYPE(Class, Base)
Douglas Gregorab380272009-04-30 17:32:17 +0000333#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor4fa58902009-02-26 23:50:07 +0000334#define DEPENDENT_TYPE(Class, Base) case Type::Class:
335#include "clang/AST/TypeNodes.def"
Douglas Gregorab380272009-04-30 17:32:17 +0000336 assert(false && "Should not see dependent types");
Douglas Gregor4fa58902009-02-26 23:50:07 +0000337 break;
338
Chris Lattner4b009652007-07-25 00:24:17 +0000339 case Type::FunctionNoProto:
340 case Type::FunctionProto:
Douglas Gregorab380272009-04-30 17:32:17 +0000341 // GCC extension: alignof(function) = 32 bits
342 Width = 0;
343 Align = 32;
344 break;
345
Douglas Gregor4fa58902009-02-26 23:50:07 +0000346 case Type::IncompleteArray:
Steve Naroff83c13012007-08-30 01:06:46 +0000347 case Type::VariableArray:
Douglas Gregorab380272009-04-30 17:32:17 +0000348 Width = 0;
349 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
350 break;
351
Steve Naroff83c13012007-08-30 01:06:46 +0000352 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000353 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000354
Chris Lattner8cd0e932008-03-05 18:54:05 +0000355 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000356 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000357 Align = EltInfo.second;
358 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000359 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000360 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000361 case Type::Vector: {
362 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000363 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000364 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000365 Align = Width;
Nate Begeman7903d052009-01-18 06:42:49 +0000366 // If the alignment is not a power of 2, round up to the next power of 2.
367 // This happens for non-power-of-2 length vectors.
368 // FIXME: this should probably be a target property.
369 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000370 break;
371 }
372
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000373 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000374 switch (cast<BuiltinType>(T)->getKind()) {
375 default: assert(0 && "Unknown builtin type!");
376 case BuiltinType::Void:
Douglas Gregorab380272009-04-30 17:32:17 +0000377 // GCC extension: alignof(void) = 8 bits.
378 Width = 0;
379 Align = 8;
380 break;
381
Chris Lattnerb66237b2007-12-19 19:23:28 +0000382 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000383 Width = Target.getBoolWidth();
384 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000385 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000386 case BuiltinType::Char_S:
387 case BuiltinType::Char_U:
388 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000389 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000390 Width = Target.getCharWidth();
391 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000392 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000393 case BuiltinType::WChar:
394 Width = Target.getWCharWidth();
395 Align = Target.getWCharAlign();
396 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000397 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000398 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000399 Width = Target.getShortWidth();
400 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000401 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000402 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000403 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000404 Width = Target.getIntWidth();
405 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000406 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000407 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000408 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000409 Width = Target.getLongWidth();
410 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000411 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000412 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000413 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000414 Width = Target.getLongLongWidth();
415 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000416 break;
Chris Lattner4b11cc22009-04-30 02:55:13 +0000417 case BuiltinType::Int128:
418 case BuiltinType::UInt128:
419 Width = 128;
420 Align = 128; // int128_t is 128-bit aligned on all targets.
421 break;
Chris Lattnerb66237b2007-12-19 19:23:28 +0000422 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000423 Width = Target.getFloatWidth();
424 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000425 break;
426 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000427 Width = Target.getDoubleWidth();
428 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000429 break;
430 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000431 Width = Target.getLongDoubleWidth();
432 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000433 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000434 }
435 break;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000436 case Type::FixedWidthInt:
437 // FIXME: This isn't precisely correct; the width/alignment should depend
438 // on the available types for the target
439 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattnere9174982009-02-15 21:20:13 +0000440 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000441 Align = Width;
442 break;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000443 case Type::ExtQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000444 // FIXME: Pointers into different addr spaces could have different sizes and
445 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000446 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000447 case Type::ObjCQualifiedId:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000448 case Type::ObjCQualifiedInterface:
Chris Lattner1d78a862008-04-07 07:01:58 +0000449 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000450 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000451 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000452 case Type::BlockPointer: {
453 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
454 Width = Target.getPointerWidth(AS);
455 Align = Target.getPointerAlign(AS);
456 break;
457 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000458 case Type::Pointer: {
459 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000460 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000461 Align = Target.getPointerAlign(AS);
462 break;
463 }
Sebastian Redlce6fff02009-03-16 23:22:08 +0000464 case Type::LValueReference:
465 case Type::RValueReference:
Chris Lattner4b009652007-07-25 00:24:17 +0000466 // "When applied to a reference or a reference type, the result is the size
467 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000468 // FIXME: This is wrong for struct layout: a reference in a struct has
469 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000470 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl75555032009-01-24 21:16:55 +0000471 case Type::MemberPointer: {
Sebastian Redl18cffee2009-01-24 23:29:36 +0000472 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redl75555032009-01-24 21:16:55 +0000473 // the GCC ABI, where pointers to data are one pointer large, pointers to
474 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl18cffee2009-01-24 23:29:36 +0000475 // other compilers too, we need to delegate this completely to TargetInfo
476 // or some ABI abstraction layer.
Sebastian Redl75555032009-01-24 21:16:55 +0000477 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
478 unsigned AS = Pointee.getAddressSpace();
479 Width = Target.getPointerWidth(AS);
480 if (Pointee->isFunctionType())
481 Width *= 2;
482 Align = Target.getPointerAlign(AS);
483 // GCC aligns at single pointer width.
484 }
Chris Lattner4b009652007-07-25 00:24:17 +0000485 case Type::Complex: {
486 // Complex types have the same alignment as their elements, but twice the
487 // size.
488 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000489 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000490 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000491 Align = EltInfo.second;
492 break;
493 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000494 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000495 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000496 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
497 Width = Layout.getSize();
498 Align = Layout.getAlignment();
499 break;
500 }
Douglas Gregor4fa58902009-02-26 23:50:07 +0000501 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000502 case Type::Enum: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000503 const TagType *TT = cast<TagType>(T);
504
505 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000506 Width = 1;
507 Align = 1;
508 break;
509 }
510
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000511 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000512 return getTypeInfo(ET->getDecl()->getIntegerType());
513
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000514 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000515 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
516 Width = Layout.getSize();
517 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000518 break;
519 }
Douglas Gregordd13e842009-03-30 22:58:21 +0000520
Douglas Gregorab380272009-04-30 17:32:17 +0000521 case Type::Typedef: {
522 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
523 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
524 Align = Aligned->getAlignment();
525 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
526 } else
527 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregordd13e842009-03-30 22:58:21 +0000528 break;
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000529 }
Douglas Gregorab380272009-04-30 17:32:17 +0000530
531 case Type::TypeOfExpr:
532 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
533 .getTypePtr());
534
535 case Type::TypeOf:
536 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
537
538 case Type::QualifiedName:
539 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
540
541 case Type::TemplateSpecialization:
542 assert(getCanonicalType(T) != T &&
543 "Cannot request the size of a dependent type");
544 // FIXME: this is likely to be wrong once we support template
545 // aliases, since a template alias could refer to a typedef that
546 // has an __aligned__ attribute on it.
547 return getTypeInfo(getCanonicalType(T));
548 }
Chris Lattner4b009652007-07-25 00:24:17 +0000549
550 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000551 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000552}
553
Chris Lattner83165b52009-01-27 18:08:34 +0000554/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
555/// type for the current target in bits. This can be different than the ABI
556/// alignment in cases where it is beneficial for performance to overalign
557/// a data type.
558unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
559 unsigned ABIAlign = getTypeAlign(T);
560
561 // Doubles should be naturally aligned if possible.
Daniel Dunbarc61a8002009-02-18 19:59:32 +0000562 if (T->isSpecificBuiltinType(BuiltinType::Double))
563 return std::max(ABIAlign, 64U);
Chris Lattner83165b52009-01-27 18:08:34 +0000564
565 return ABIAlign;
566}
567
568
Devang Patelbfe323c2008-06-04 21:22:16 +0000569/// LayoutField - Field layout.
570void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000571 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000572 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000573 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000574 uint64_t FieldOffset = IsUnion ? 0 : Size;
575 uint64_t FieldSize;
576 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000577
578 // FIXME: Should this override struct packing? Probably we want to
579 // take the minimum?
580 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
581 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000582
583 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
584 // TODO: Need to check this algorithm on other targets!
585 // (tested on Linux-X86)
Eli Friedman5255e7a2009-04-26 19:19:15 +0000586 FieldSize = BitWidthExpr->EvaluateAsInt(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000587
588 std::pair<uint64_t, unsigned> FieldInfo =
589 Context.getTypeInfo(FD->getType());
590 uint64_t TypeSize = FieldInfo.first;
591
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000592 // Determine the alignment of this bitfield. The packing
593 // attributes define a maximum and the alignment attribute defines
594 // a minimum.
595 // FIXME: What is the right behavior when the specified alignment
596 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000597 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000598 if (FieldPacking)
599 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000600 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
601 FieldAlign = std::max(FieldAlign, AA->getAlignment());
602
603 // Check if we need to add padding to give the field the correct
604 // alignment.
605 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
606 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
607
608 // Padding members don't affect overall alignment
609 if (!FD->getIdentifier())
610 FieldAlign = 1;
611 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000612 if (FD->getType()->isIncompleteArrayType()) {
613 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000614 // query getTypeInfo about these, so we figure it out here.
615 // Flexible array members don't have any size, but they
616 // have to be aligned appropriately for their element type.
617 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000618 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000619 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson0843ea52009-04-10 05:31:15 +0000620 } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
621 unsigned AS = RT->getPointeeType().getAddressSpace();
622 FieldSize = Context.Target.getPointerWidth(AS);
623 FieldAlign = Context.Target.getPointerAlign(AS);
Devang Patelbfe323c2008-06-04 21:22:16 +0000624 } else {
625 std::pair<uint64_t, unsigned> FieldInfo =
626 Context.getTypeInfo(FD->getType());
627 FieldSize = FieldInfo.first;
628 FieldAlign = FieldInfo.second;
629 }
630
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000631 // Determine the alignment of this bitfield. The packing
632 // attributes define a maximum and the alignment attribute defines
633 // a minimum. Additionally, the packing alignment must be at least
634 // a byte for non-bitfields.
635 //
636 // FIXME: What is the right behavior when the specified alignment
637 // is smaller than the specified packing?
638 if (FieldPacking)
639 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000640 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
641 FieldAlign = std::max(FieldAlign, AA->getAlignment());
642
643 // Round up the current record size to the field's alignment boundary.
644 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
645 }
646
647 // Place this field at the current location.
648 FieldOffsets[FieldNo] = FieldOffset;
649
650 // Reserve space for this field.
651 if (IsUnion) {
652 Size = std::max(Size, FieldSize);
653 } else {
654 Size = FieldOffset + FieldSize;
655 }
656
Daniel Dunbar5523e862009-05-04 05:16:21 +0000657 // Remember the next available offset.
658 NextOffset = Size;
659
Devang Patelbfe323c2008-06-04 21:22:16 +0000660 // Remember max struct/class alignment.
661 Alignment = std::max(Alignment, FieldAlign);
662}
663
Daniel Dunbar1af336e2009-04-22 17:43:55 +0000664static void CollectLocalObjCIvars(ASTContext *Ctx,
665 const ObjCInterfaceDecl *OI,
666 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000667 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
668 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner9329cf52009-03-31 08:48:01 +0000669 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000670 if (!IVDecl->isInvalidDecl())
671 Fields.push_back(cast<FieldDecl>(IVDecl));
672 }
673}
674
Daniel Dunbar1af336e2009-04-22 17:43:55 +0000675void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
676 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
677 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
678 CollectObjCIvars(SuperClass, Fields);
679 CollectLocalObjCIvars(this, OI, Fields);
680}
681
Daniel Dunbar1fbaef12009-05-03 10:38:35 +0000682/// getInterfaceLayoutImpl - Get or compute information about the
683/// layout of the given interface.
684///
685/// \param Impl - If given, also include the layout of the interface's
686/// implementation. This may differ by including synthesized ivars.
Devang Patel4b6bf702008-06-04 21:54:36 +0000687const ASTRecordLayout &
Daniel Dunbar1fbaef12009-05-03 10:38:35 +0000688ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
689 const ObjCImplementationDecl *Impl) {
Daniel Dunbar94d2ede2009-05-03 13:15:50 +0000690 assert(!D->isForwardDecl() && "Invalid interface decl!");
691
Devang Patel4b6bf702008-06-04 21:54:36 +0000692 // Look up this layout, if already laid out, return what we have.
Daniel Dunbarb3170af2009-05-03 11:41:43 +0000693 ObjCContainerDecl *Key =
694 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
695 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
696 return *Entry;
Devang Patel4b6bf702008-06-04 21:54:36 +0000697
Daniel Dunbar5b9332f2009-05-03 11:16:44 +0000698 unsigned FieldCount = D->ivar_size();
699 // Add in synthesized ivar count if laying out an implementation.
700 if (Impl) {
701 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
702 E = D->prop_end(*this); I != E; ++I)
703 if ((*I)->getPropertyIvarDecl())
704 ++FieldCount;
705
Daniel Dunbarb3170af2009-05-03 11:41:43 +0000706 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar5b9332f2009-05-03 11:16:44 +0000707 // entry. Note we can't cache this because we simply free all
708 // entries later; however we shouldn't look up implementations
709 // frequently.
710 if (FieldCount == D->ivar_size())
711 return getObjCLayout(D, 0);
712 }
713
Devang Patel8682d882008-06-06 02:14:01 +0000714 ASTRecordLayout *NewEntry = NULL;
Devang Patel8682d882008-06-06 02:14:01 +0000715 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Devang Patel8682d882008-06-06 02:14:01 +0000716 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
717 unsigned Alignment = SL.getAlignment();
Daniel Dunbarb3170af2009-05-03 11:41:43 +0000718
Daniel Dunbarb5dc2942009-05-07 21:58:26 +0000719 // We start laying out ivars not at the end of the superclass
720 // structure, but at the next byte following the last field.
721 uint64_t Size = llvm::RoundUpToAlignment(SL.NextOffset, 8);
Daniel Dunbar5523e862009-05-04 05:16:21 +0000722
Daniel Dunbarb3170af2009-05-03 11:41:43 +0000723 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout(Size, Alignment);
Devang Patel8682d882008-06-06 02:14:01 +0000724 NewEntry->InitializeLayout(FieldCount);
Devang Patel8682d882008-06-06 02:14:01 +0000725 } else {
Daniel Dunbarb3170af2009-05-03 11:41:43 +0000726 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout();
Devang Patel8682d882008-06-06 02:14:01 +0000727 NewEntry->InitializeLayout(FieldCount);
728 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000729
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000730 unsigned StructPacking = 0;
731 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
732 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000733
734 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
735 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
736 AA->getAlignment()));
737
738 // Layout each ivar sequentially.
739 unsigned i = 0;
740 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
741 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
742 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000743 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000744 }
Daniel Dunbar5b9332f2009-05-03 11:16:44 +0000745 // And synthesized ivars, if this is an implementation.
746 if (Impl) {
747 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
748 E = D->prop_end(*this); I != E; ++I) {
749 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
750 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
751 }
Fariborz Jahanianfbf44642009-03-31 18:11:23 +0000752 }
Fariborz Jahanian84c45692009-04-01 19:37:34 +0000753
Devang Patel4b6bf702008-06-04 21:54:36 +0000754 // Finally, round the size of the total struct up to the alignment of the
755 // struct itself.
756 NewEntry->FinalizeLayout();
757 return *NewEntry;
758}
759
Daniel Dunbar1fbaef12009-05-03 10:38:35 +0000760const ASTRecordLayout &
761ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
762 return getObjCLayout(D, 0);
763}
764
765const ASTRecordLayout &
766ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
767 return getObjCLayout(D->getClassInterface(), D);
768}
769
Devang Patel7a78e432007-11-01 19:11:01 +0000770/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000771/// specified record (struct/union/class), which indicates its size and field
772/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000773const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000774 D = D->getDefinition(*this);
775 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000776
Chris Lattner4b009652007-07-25 00:24:17 +0000777 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000778 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000779 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000780
Devang Patel7a78e432007-11-01 19:11:01 +0000781 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
782 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
783 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000784 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000785
Douglas Gregor39677622008-12-11 20:41:00 +0000786 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000787 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
788 D->field_end(*this)));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000789 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000790
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000791 unsigned StructPacking = 0;
792 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
793 StructPacking = PA->getAlignment();
794
Eli Friedman5949a022008-05-30 09:31:38 +0000795 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000796 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
797 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000798
Eli Friedman5949a022008-05-30 09:31:38 +0000799 // Layout each field, for now, just sequentially, respecting alignment. In
800 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000801 unsigned FieldIdx = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000802 for (RecordDecl::field_iterator Field = D->field_begin(*this),
803 FieldEnd = D->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000804 Field != FieldEnd; (void)++Field, ++FieldIdx)
805 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000806
807 // Finally, round the size of the total struct up to the alignment of the
808 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000809 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000810 return *NewEntry;
811}
812
Chris Lattner4b009652007-07-25 00:24:17 +0000813//===----------------------------------------------------------------------===//
814// Type creation/memoization methods
815//===----------------------------------------------------------------------===//
816
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000817QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000818 QualType CanT = getCanonicalType(T);
819 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000820 return T;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000821
822 // If we are composing extended qualifiers together, merge together into one
823 // ExtQualType node.
824 unsigned CVRQuals = T.getCVRQualifiers();
825 QualType::GCAttrTypes GCAttr = QualType::GCNone;
826 Type *TypeNode = T.getTypePtr();
Chris Lattner35fef522008-02-20 20:55:12 +0000827
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000828 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
829 // If this type already has an address space specified, it cannot get
830 // another one.
831 assert(EQT->getAddressSpace() == 0 &&
832 "Type cannot be in multiple addr spaces!");
833 GCAttr = EQT->getObjCGCAttr();
834 TypeNode = EQT->getBaseType();
835 }
Chris Lattner35fef522008-02-20 20:55:12 +0000836
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000837 // Check if we've already instantiated this type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000838 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000839 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000840 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000841 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000842 return QualType(EXTQy, CVRQuals);
843
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000844 // If the base type isn't canonical, this won't be a canonical type either,
845 // so fill in the canonical type field.
846 QualType Canonical;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000847 if (!TypeNode->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000848 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000849
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000850 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000851 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000852 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000853 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000854 ExtQualType *New =
855 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000856 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000857 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000858 return QualType(New, CVRQuals);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000859}
860
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000861QualType ASTContext::getObjCGCQualType(QualType T,
862 QualType::GCAttrTypes GCAttr) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000863 QualType CanT = getCanonicalType(T);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000864 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000865 return T;
866
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000867 // If we are composing extended qualifiers together, merge together into one
868 // ExtQualType node.
869 unsigned CVRQuals = T.getCVRQualifiers();
870 Type *TypeNode = T.getTypePtr();
871 unsigned AddressSpace = 0;
872
873 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
874 // If this type already has an address space specified, it cannot get
875 // another one.
876 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
877 "Type cannot be in multiple addr spaces!");
878 AddressSpace = EQT->getAddressSpace();
879 TypeNode = EQT->getBaseType();
880 }
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000881
882 // Check if we've already instantiated an gc qual'd type of this type.
883 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000884 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000885 void *InsertPos = 0;
886 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000887 return QualType(EXTQy, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000888
889 // If the base type isn't canonical, this won't be a canonical type either,
890 // so fill in the canonical type field.
Eli Friedman94fcc9a2009-02-27 23:04:43 +0000891 // FIXME: Isn't this also not canonical if the base type is a array
892 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000893 QualType Canonical;
894 if (!T->isCanonical()) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000895 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000896
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000897 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000898 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
899 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
900 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000901 ExtQualType *New =
902 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000903 ExtQualTypes.InsertNode(New, InsertPos);
904 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000905 return QualType(New, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000906}
Chris Lattner4b009652007-07-25 00:24:17 +0000907
908/// getComplexType - Return the uniqued reference to the type for a complex
909/// number with the specified element type.
910QualType ASTContext::getComplexType(QualType T) {
911 // Unique pointers, to guarantee there is only one pointer of a particular
912 // structure.
913 llvm::FoldingSetNodeID ID;
914 ComplexType::Profile(ID, T);
915
916 void *InsertPos = 0;
917 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
918 return QualType(CT, 0);
919
920 // If the pointee type isn't canonical, this won't be a canonical type either,
921 // so fill in the canonical type field.
922 QualType Canonical;
923 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000924 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000925
926 // Get the new insert position for the node we care about.
927 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000928 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000929 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000930 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000931 Types.push_back(New);
932 ComplexTypes.InsertNode(New, InsertPos);
933 return QualType(New, 0);
934}
935
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000936QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
937 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
938 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
939 FixedWidthIntType *&Entry = Map[Width];
940 if (!Entry)
941 Entry = new FixedWidthIntType(Width, Signed);
942 return QualType(Entry, 0);
943}
Chris Lattner4b009652007-07-25 00:24:17 +0000944
945/// getPointerType - Return the uniqued reference to the type for a pointer to
946/// the specified type.
947QualType ASTContext::getPointerType(QualType T) {
948 // Unique pointers, to guarantee there is only one pointer of a particular
949 // structure.
950 llvm::FoldingSetNodeID ID;
951 PointerType::Profile(ID, T);
952
953 void *InsertPos = 0;
954 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
955 return QualType(PT, 0);
956
957 // If the pointee type isn't canonical, this won't be a canonical type either,
958 // so fill in the canonical type field.
959 QualType Canonical;
960 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000961 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000962
963 // Get the new insert position for the node we care about.
964 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000965 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000966 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000967 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000968 Types.push_back(New);
969 PointerTypes.InsertNode(New, InsertPos);
970 return QualType(New, 0);
971}
972
Steve Naroff7aa54752008-08-27 16:04:49 +0000973/// getBlockPointerType - Return the uniqued reference to the type for
974/// a pointer to the specified block.
975QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000976 assert(T->isFunctionType() && "block of function types only");
977 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000978 // structure.
979 llvm::FoldingSetNodeID ID;
980 BlockPointerType::Profile(ID, T);
981
982 void *InsertPos = 0;
983 if (BlockPointerType *PT =
984 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
985 return QualType(PT, 0);
986
Steve Narofffd5b19d2008-08-28 19:20:44 +0000987 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000988 // type either so fill in the canonical type field.
989 QualType Canonical;
990 if (!T->isCanonical()) {
991 Canonical = getBlockPointerType(getCanonicalType(T));
992
993 // Get the new insert position for the node we care about.
994 BlockPointerType *NewIP =
995 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000996 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000997 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000998 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000999 Types.push_back(New);
1000 BlockPointerTypes.InsertNode(New, InsertPos);
1001 return QualType(New, 0);
1002}
1003
Sebastian Redlce6fff02009-03-16 23:22:08 +00001004/// getLValueReferenceType - Return the uniqued reference to the type for an
1005/// lvalue reference to the specified type.
1006QualType ASTContext::getLValueReferenceType(QualType T) {
Chris Lattner4b009652007-07-25 00:24:17 +00001007 // Unique pointers, to guarantee there is only one pointer of a particular
1008 // structure.
1009 llvm::FoldingSetNodeID ID;
1010 ReferenceType::Profile(ID, T);
1011
1012 void *InsertPos = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +00001013 if (LValueReferenceType *RT =
1014 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001015 return QualType(RT, 0);
Sebastian Redlce6fff02009-03-16 23:22:08 +00001016
Chris Lattner4b009652007-07-25 00:24:17 +00001017 // If the referencee type isn't canonical, this won't be a canonical type
1018 // either, so fill in the canonical type field.
1019 QualType Canonical;
1020 if (!T->isCanonical()) {
Sebastian Redlce6fff02009-03-16 23:22:08 +00001021 Canonical = getLValueReferenceType(getCanonicalType(T));
1022
Chris Lattner4b009652007-07-25 00:24:17 +00001023 // Get the new insert position for the node we care about.
Sebastian Redlce6fff02009-03-16 23:22:08 +00001024 LValueReferenceType *NewIP =
1025 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001026 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001027 }
1028
Sebastian Redlce6fff02009-03-16 23:22:08 +00001029 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001030 Types.push_back(New);
Sebastian Redlce6fff02009-03-16 23:22:08 +00001031 LValueReferenceTypes.InsertNode(New, InsertPos);
1032 return QualType(New, 0);
1033}
1034
1035/// getRValueReferenceType - Return the uniqued reference to the type for an
1036/// rvalue reference to the specified type.
1037QualType ASTContext::getRValueReferenceType(QualType T) {
1038 // Unique pointers, to guarantee there is only one pointer of a particular
1039 // structure.
1040 llvm::FoldingSetNodeID ID;
1041 ReferenceType::Profile(ID, T);
1042
1043 void *InsertPos = 0;
1044 if (RValueReferenceType *RT =
1045 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1046 return QualType(RT, 0);
1047
1048 // If the referencee type isn't canonical, this won't be a canonical type
1049 // either, so fill in the canonical type field.
1050 QualType Canonical;
1051 if (!T->isCanonical()) {
1052 Canonical = getRValueReferenceType(getCanonicalType(T));
1053
1054 // Get the new insert position for the node we care about.
1055 RValueReferenceType *NewIP =
1056 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1057 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1058 }
1059
1060 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1061 Types.push_back(New);
1062 RValueReferenceTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001063 return QualType(New, 0);
1064}
1065
Sebastian Redl75555032009-01-24 21:16:55 +00001066/// getMemberPointerType - Return the uniqued reference to the type for a
1067/// member pointer to the specified type, in the specified class.
1068QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1069{
1070 // Unique pointers, to guarantee there is only one pointer of a particular
1071 // structure.
1072 llvm::FoldingSetNodeID ID;
1073 MemberPointerType::Profile(ID, T, Cls);
1074
1075 void *InsertPos = 0;
1076 if (MemberPointerType *PT =
1077 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1078 return QualType(PT, 0);
1079
1080 // If the pointee or class type isn't canonical, this won't be a canonical
1081 // type either, so fill in the canonical type field.
1082 QualType Canonical;
1083 if (!T->isCanonical()) {
1084 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1085
1086 // Get the new insert position for the node we care about.
1087 MemberPointerType *NewIP =
1088 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1089 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1090 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001091 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +00001092 Types.push_back(New);
1093 MemberPointerTypes.InsertNode(New, InsertPos);
1094 return QualType(New, 0);
1095}
1096
Steve Naroff83c13012007-08-30 01:06:46 +00001097/// getConstantArrayType - Return the unique reference to the type for an
1098/// array of the specified element type.
1099QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +00001100 const llvm::APInt &ArySize,
1101 ArrayType::ArraySizeModifier ASM,
1102 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001103 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001104 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001105
1106 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +00001107 if (ConstantArrayType *ATP =
1108 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001109 return QualType(ATP, 0);
1110
1111 // If the element type isn't canonical, this won't be a canonical type either,
1112 // so fill in the canonical type field.
1113 QualType Canonical;
1114 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001115 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +00001116 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001117 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +00001118 ConstantArrayType *NewIP =
1119 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001120 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001121 }
1122
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001123 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001124 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001125 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001126 Types.push_back(New);
1127 return QualType(New, 0);
1128}
1129
Steve Naroffe2579e32007-08-30 18:14:25 +00001130/// getVariableArrayType - Returns a non-unique reference to the type for a
1131/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +00001132QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1133 ArrayType::ArraySizeModifier ASM,
1134 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +00001135 // Since we don't unique expressions, it isn't possible to unique VLA's
1136 // that have an expression provided for their size.
1137
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001138 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001139 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001140
1141 VariableArrayTypes.push_back(New);
1142 Types.push_back(New);
1143 return QualType(New, 0);
1144}
1145
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001146/// getDependentSizedArrayType - Returns a non-unique reference to
1147/// the type for a dependently-sized array of the specified element
1148/// type. FIXME: We will need these to be uniqued, or at least
1149/// comparable, at some point.
1150QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1151 ArrayType::ArraySizeModifier ASM,
1152 unsigned EltTypeQuals) {
1153 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1154 "Size must be type- or value-dependent!");
1155
1156 // Since we don't unique expressions, it isn't possible to unique
1157 // dependently-sized array types.
1158
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001159 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001160 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1161 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001162
1163 DependentSizedArrayTypes.push_back(New);
1164 Types.push_back(New);
1165 return QualType(New, 0);
1166}
1167
Eli Friedman8ff07782008-02-15 18:16:39 +00001168QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1169 ArrayType::ArraySizeModifier ASM,
1170 unsigned EltTypeQuals) {
1171 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001172 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001173
1174 void *InsertPos = 0;
1175 if (IncompleteArrayType *ATP =
1176 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1177 return QualType(ATP, 0);
1178
1179 // If the element type isn't canonical, this won't be a canonical type
1180 // either, so fill in the canonical type field.
1181 QualType Canonical;
1182
1183 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001184 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001185 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001186
1187 // Get the new insert position for the node we care about.
1188 IncompleteArrayType *NewIP =
1189 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001190 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001191 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001192
Steve Naroff93fd2112009-01-27 22:08:43 +00001193 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001194 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001195
1196 IncompleteArrayTypes.InsertNode(New, InsertPos);
1197 Types.push_back(New);
1198 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001199}
1200
Chris Lattner4b009652007-07-25 00:24:17 +00001201/// getVectorType - Return the unique reference to a vector type of
1202/// the specified element type and size. VectorType must be a built-in type.
1203QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1204 BuiltinType *baseType;
1205
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001206 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001207 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1208
1209 // Check if we've already instantiated a vector of this type.
1210 llvm::FoldingSetNodeID ID;
1211 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1212 void *InsertPos = 0;
1213 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1214 return QualType(VTP, 0);
1215
1216 // If the element type isn't canonical, this won't be a canonical type either,
1217 // so fill in the canonical type field.
1218 QualType Canonical;
1219 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001220 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001221
1222 // Get the new insert position for the node we care about.
1223 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001224 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001225 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001226 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001227 VectorTypes.InsertNode(New, InsertPos);
1228 Types.push_back(New);
1229 return QualType(New, 0);
1230}
1231
Nate Begemanaf6ed502008-04-18 23:10:10 +00001232/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001233/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001234QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001235 BuiltinType *baseType;
1236
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001237 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001238 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001239
1240 // Check if we've already instantiated a vector of this type.
1241 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001242 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001243 void *InsertPos = 0;
1244 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1245 return QualType(VTP, 0);
1246
1247 // If the element type isn't canonical, this won't be a canonical type either,
1248 // so fill in the canonical type field.
1249 QualType Canonical;
1250 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001251 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001252
1253 // Get the new insert position for the node we care about.
1254 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001255 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001256 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001257 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001258 VectorTypes.InsertNode(New, InsertPos);
1259 Types.push_back(New);
1260 return QualType(New, 0);
1261}
1262
Douglas Gregor4fa58902009-02-26 23:50:07 +00001263/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001264///
Douglas Gregor4fa58902009-02-26 23:50:07 +00001265QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Chris Lattner4b009652007-07-25 00:24:17 +00001266 // Unique functions, to guarantee there is only one function of a particular
1267 // structure.
1268 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001269 FunctionNoProtoType::Profile(ID, ResultTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001270
1271 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001272 if (FunctionNoProtoType *FT =
1273 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001274 return QualType(FT, 0);
1275
1276 QualType Canonical;
1277 if (!ResultTy->isCanonical()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00001278 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001279
1280 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001281 FunctionNoProtoType *NewIP =
1282 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001283 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001284 }
1285
Douglas Gregor4fa58902009-02-26 23:50:07 +00001286 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001287 Types.push_back(New);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001288 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001289 return QualType(New, 0);
1290}
1291
1292/// getFunctionType - Return a normal function type with a typed argument
1293/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001294QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001295 unsigned NumArgs, bool isVariadic,
Sebastian Redlba9a3712009-05-06 23:27:55 +00001296 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001297 // Unique functions, to guarantee there is only one function of a particular
1298 // structure.
1299 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001300 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redlba9a3712009-05-06 23:27:55 +00001301 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001302
1303 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001304 if (FunctionProtoType *FTP =
1305 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001306 return QualType(FTP, 0);
1307
1308 // Determine whether the type being created is already canonical or not.
1309 bool isCanonical = ResultTy->isCanonical();
1310 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1311 if (!ArgArray[i]->isCanonical())
1312 isCanonical = false;
1313
1314 // If this type isn't canonical, get the canonical version of it.
1315 QualType Canonical;
1316 if (!isCanonical) {
1317 llvm::SmallVector<QualType, 16> CanonicalArgs;
1318 CanonicalArgs.reserve(NumArgs);
1319 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001320 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redlba9a3712009-05-06 23:27:55 +00001321
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001322 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001323 &CanonicalArgs[0], NumArgs,
Sebastian Redlba9a3712009-05-06 23:27:55 +00001324 isVariadic, TypeQuals);
1325
Chris Lattner4b009652007-07-25 00:24:17 +00001326 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001327 FunctionProtoType *NewIP =
1328 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001329 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001330 }
Sebastian Redlba9a3712009-05-06 23:27:55 +00001331
Douglas Gregor4fa58902009-02-26 23:50:07 +00001332 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redlba9a3712009-05-06 23:27:55 +00001333 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001334 FunctionProtoType *FTP =
Sebastian Redlba9a3712009-05-06 23:27:55 +00001335 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1336 NumArgs*sizeof(QualType), 8);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001337 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redlba9a3712009-05-06 23:27:55 +00001338 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001339 Types.push_back(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001340 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001341 return QualType(FTP, 0);
1342}
1343
Douglas Gregor1d661552008-04-13 21:07:44 +00001344/// getTypeDeclType - Return the unique reference to the type for the
1345/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001346QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001347 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001348 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1349
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001350 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001351 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001352 else if (isa<TemplateTypeParmDecl>(Decl)) {
1353 assert(false && "Template type parameter types are always available.");
1354 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001355 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001356
Douglas Gregor2e047592009-02-28 01:32:25 +00001357 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001358 if (PrevDecl)
1359 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001360 else
1361 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001362 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001363 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1364 if (PrevDecl)
1365 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001366 else
1367 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001368 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001369 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001370 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001371
Ted Kremenek46a837c2008-09-05 17:16:31 +00001372 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001373 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001374}
1375
Chris Lattner4b009652007-07-25 00:24:17 +00001376/// getTypedefType - Return the unique reference to the type for the
1377/// specified typename decl.
1378QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1379 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1380
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001381 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001382 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001383 Types.push_back(Decl->TypeForDecl);
1384 return QualType(Decl->TypeForDecl, 0);
1385}
1386
Ted Kremenek42730c52008-01-07 19:49:32 +00001387/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001388/// specified ObjC interface decl.
Daniel Dunbarbe1ff272009-04-22 04:34:53 +00001389QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001390 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1391
Daniel Dunbarbe1ff272009-04-22 04:34:53 +00001392 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1393 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001394 Types.push_back(Decl->TypeForDecl);
1395 return QualType(Decl->TypeForDecl, 0);
1396}
1397
Douglas Gregora4918772009-02-05 23:33:38 +00001398/// \brief Retrieve the template type parameter type for a template
1399/// parameter with the given depth, index, and (optionally) name.
1400QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1401 IdentifierInfo *Name) {
1402 llvm::FoldingSetNodeID ID;
1403 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1404 void *InsertPos = 0;
1405 TemplateTypeParmType *TypeParm
1406 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1407
1408 if (TypeParm)
1409 return QualType(TypeParm, 0);
1410
1411 if (Name)
1412 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1413 getTemplateTypeParmType(Depth, Index));
1414 else
1415 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1416
1417 Types.push_back(TypeParm);
1418 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1419
1420 return QualType(TypeParm, 0);
1421}
1422
Douglas Gregor8e458f42009-02-09 18:46:07 +00001423QualType
Douglas Gregordd13e842009-03-30 22:58:21 +00001424ASTContext::getTemplateSpecializationType(TemplateName Template,
1425 const TemplateArgument *Args,
1426 unsigned NumArgs,
1427 QualType Canon) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001428 if (!Canon.isNull())
1429 Canon = getCanonicalType(Canon);
Douglas Gregor9c7825b2009-02-26 22:19:44 +00001430
Douglas Gregor8e458f42009-02-09 18:46:07 +00001431 llvm::FoldingSetNodeID ID;
Douglas Gregordd13e842009-03-30 22:58:21 +00001432 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001433
Douglas Gregor8e458f42009-02-09 18:46:07 +00001434 void *InsertPos = 0;
Douglas Gregordd13e842009-03-30 22:58:21 +00001435 TemplateSpecializationType *Spec
1436 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001437
1438 if (Spec)
1439 return QualType(Spec, 0);
1440
Douglas Gregordd13e842009-03-30 22:58:21 +00001441 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001442 sizeof(TemplateArgument) * NumArgs),
1443 8);
Douglas Gregordd13e842009-03-30 22:58:21 +00001444 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001445 Types.push_back(Spec);
Douglas Gregordd13e842009-03-30 22:58:21 +00001446 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001447
1448 return QualType(Spec, 0);
1449}
1450
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001451QualType
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001452ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001453 QualType NamedType) {
1454 llvm::FoldingSetNodeID ID;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001455 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001456
1457 void *InsertPos = 0;
1458 QualifiedNameType *T
1459 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1460 if (T)
1461 return QualType(T, 0);
1462
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001463 T = new (*this) QualifiedNameType(NNS, NamedType,
1464 getCanonicalType(NamedType));
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001465 Types.push_back(T);
1466 QualifiedNameTypes.InsertNode(T, InsertPos);
1467 return QualType(T, 0);
1468}
1469
Douglas Gregord3022602009-03-27 23:10:48 +00001470QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1471 const IdentifierInfo *Name,
1472 QualType Canon) {
1473 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1474
1475 if (Canon.isNull()) {
1476 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1477 if (CanonNNS != NNS)
1478 Canon = getTypenameType(CanonNNS, Name);
1479 }
1480
1481 llvm::FoldingSetNodeID ID;
1482 TypenameType::Profile(ID, NNS, Name);
1483
1484 void *InsertPos = 0;
1485 TypenameType *T
1486 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1487 if (T)
1488 return QualType(T, 0);
1489
1490 T = new (*this) TypenameType(NNS, Name, Canon);
1491 Types.push_back(T);
1492 TypenameTypes.InsertNode(T, InsertPos);
1493 return QualType(T, 0);
1494}
1495
Douglas Gregor77da5802009-04-01 00:28:59 +00001496QualType
1497ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1498 const TemplateSpecializationType *TemplateId,
1499 QualType Canon) {
1500 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1501
1502 if (Canon.isNull()) {
1503 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1504 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1505 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1506 const TemplateSpecializationType *CanonTemplateId
1507 = CanonType->getAsTemplateSpecializationType();
1508 assert(CanonTemplateId &&
1509 "Canonical type must also be a template specialization type");
1510 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1511 }
1512 }
1513
1514 llvm::FoldingSetNodeID ID;
1515 TypenameType::Profile(ID, NNS, TemplateId);
1516
1517 void *InsertPos = 0;
1518 TypenameType *T
1519 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1520 if (T)
1521 return QualType(T, 0);
1522
1523 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1524 Types.push_back(T);
1525 TypenameTypes.InsertNode(T, InsertPos);
1526 return QualType(T, 0);
1527}
1528
Chris Lattnere1352302008-04-07 04:56:42 +00001529/// CmpProtocolNames - Comparison predicate for sorting protocols
1530/// alphabetically.
1531static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1532 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001533 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001534}
1535
1536static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1537 unsigned &NumProtocols) {
1538 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1539
1540 // Sort protocols, keyed by name.
1541 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1542
1543 // Remove duplicates.
1544 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1545 NumProtocols = ProtocolsEnd-Protocols;
1546}
1547
1548
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001549/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1550/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001551QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1552 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001553 // Sort the protocol list alphabetically to canonicalize it.
1554 SortAndUniqueProtocols(Protocols, NumProtocols);
1555
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001556 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001557 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001558
1559 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001560 if (ObjCQualifiedInterfaceType *QT =
1561 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001562 return QualType(QT, 0);
1563
1564 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001565 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001566 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001567
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001568 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001569 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001570 return QualType(QType, 0);
1571}
1572
Chris Lattnere1352302008-04-07 04:56:42 +00001573/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1574/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001575QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001576 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001577 // Sort the protocol list alphabetically to canonicalize it.
1578 SortAndUniqueProtocols(Protocols, NumProtocols);
1579
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001580 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001581 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001582
1583 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001584 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001585 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001586 return QualType(QT, 0);
1587
1588 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001589 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001590 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001591 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001592 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001593 return QualType(QType, 0);
1594}
1595
Douglas Gregor4fa58902009-02-26 23:50:07 +00001596/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1597/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff0604dd92007-08-01 18:02:17 +00001598/// multiple declarations that refer to "typeof(x)" all contain different
1599/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1600/// on canonical type's (which are always unique).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001601QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001602 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001603 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001604 Types.push_back(toe);
1605 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001606}
1607
Steve Naroff0604dd92007-08-01 18:02:17 +00001608/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1609/// TypeOfType AST's. The only motivation to unique these nodes would be
1610/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1611/// an issue. This doesn't effect the type checker, since it operates
1612/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001613QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001614 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001615 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001616 Types.push_back(tot);
1617 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001618}
1619
Chris Lattner4b009652007-07-25 00:24:17 +00001620/// getTagDeclType - Return the unique reference to the type for the
1621/// specified TagDecl (struct/union/class/enum) decl.
1622QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001623 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001624 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001625}
1626
1627/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1628/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1629/// needs to agree with the definition in <stddef.h>.
1630QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001631 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001632}
1633
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001634/// getSignedWCharType - Return the type of "signed wchar_t".
1635/// Used when in C++, as a GCC extension.
1636QualType ASTContext::getSignedWCharType() const {
1637 // FIXME: derive from "Target" ?
1638 return WCharTy;
1639}
1640
1641/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1642/// Used when in C++, as a GCC extension.
1643QualType ASTContext::getUnsignedWCharType() const {
1644 // FIXME: derive from "Target" ?
1645 return UnsignedIntTy;
1646}
1647
Chris Lattner4b009652007-07-25 00:24:17 +00001648/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1649/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1650QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001651 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001652}
1653
Chris Lattner19eb97e2008-04-02 05:18:44 +00001654//===----------------------------------------------------------------------===//
1655// Type Operators
1656//===----------------------------------------------------------------------===//
1657
Chris Lattner3dae6f42008-04-06 22:41:35 +00001658/// getCanonicalType - Return the canonical (structural) type corresponding to
1659/// the specified potentially non-canonical type. The non-canonical version
1660/// of a type may have many "decorated" versions of types. Decorators can
1661/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1662/// to be free of any of these, allowing two canonical types to be compared
1663/// for exact equality with a simple pointer comparison.
1664QualType ASTContext::getCanonicalType(QualType T) {
1665 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001666
1667 // If the result has type qualifiers, make sure to canonicalize them as well.
1668 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1669 if (TypeQuals == 0) return CanType;
1670
1671 // If the type qualifiers are on an array type, get the canonical type of the
1672 // array with the qualifiers applied to the element type.
1673 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1674 if (!AT)
1675 return CanType.getQualifiedType(TypeQuals);
1676
1677 // Get the canonical version of the element with the extra qualifiers on it.
1678 // This can recursively sink qualifiers through multiple levels of arrays.
1679 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1680 NewEltTy = getCanonicalType(NewEltTy);
1681
1682 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1683 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1684 CAT->getIndexTypeQualifier());
1685 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1686 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1687 IAT->getIndexTypeQualifier());
1688
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001689 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1690 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1691 DSAT->getSizeModifier(),
1692 DSAT->getIndexTypeQualifier());
1693
Chris Lattnera1923f62008-08-04 07:31:14 +00001694 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1695 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1696 VAT->getSizeModifier(),
1697 VAT->getIndexTypeQualifier());
1698}
1699
Douglas Gregorb88ba412009-05-07 06:41:52 +00001700TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
1701 // If this template name refers to a template, the canonical
1702 // template name merely stores the template itself.
1703 if (TemplateDecl *Template = Name.getAsTemplateDecl())
1704 return TemplateName(Template);
1705
1706 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
1707 assert(DTN && "Non-dependent template names must refer to template decls.");
1708 return DTN->CanonicalTemplateName;
1709}
1710
Douglas Gregord3022602009-03-27 23:10:48 +00001711NestedNameSpecifier *
1712ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1713 if (!NNS)
1714 return 0;
1715
1716 switch (NNS->getKind()) {
1717 case NestedNameSpecifier::Identifier:
1718 // Canonicalize the prefix but keep the identifier the same.
1719 return NestedNameSpecifier::Create(*this,
1720 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1721 NNS->getAsIdentifier());
1722
1723 case NestedNameSpecifier::Namespace:
1724 // A namespace is canonical; build a nested-name-specifier with
1725 // this namespace and no prefix.
1726 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1727
1728 case NestedNameSpecifier::TypeSpec:
1729 case NestedNameSpecifier::TypeSpecWithTemplate: {
1730 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1731 NestedNameSpecifier *Prefix = 0;
1732
1733 // FIXME: This isn't the right check!
1734 if (T->isDependentType())
1735 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1736
1737 return NestedNameSpecifier::Create(*this, Prefix,
1738 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1739 T.getTypePtr());
1740 }
1741
1742 case NestedNameSpecifier::Global:
1743 // The global specifier is canonical and unique.
1744 return NNS;
1745 }
1746
1747 // Required to silence a GCC warning
1748 return 0;
1749}
1750
Chris Lattnera1923f62008-08-04 07:31:14 +00001751
1752const ArrayType *ASTContext::getAsArrayType(QualType T) {
1753 // Handle the non-qualified case efficiently.
1754 if (T.getCVRQualifiers() == 0) {
1755 // Handle the common positive case fast.
1756 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1757 return AT;
1758 }
1759
1760 // Handle the common negative case fast, ignoring CVR qualifiers.
1761 QualType CType = T->getCanonicalTypeInternal();
1762
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001763 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001764 // test.
1765 if (!isa<ArrayType>(CType) &&
1766 !isa<ArrayType>(CType.getUnqualifiedType()))
1767 return 0;
1768
1769 // Apply any CVR qualifiers from the array type to the element type. This
1770 // implements C99 6.7.3p8: "If the specification of an array type includes
1771 // any type qualifiers, the element type is so qualified, not the array type."
1772
1773 // If we get here, we either have type qualifiers on the type, or we have
1774 // sugar such as a typedef in the way. If we have type qualifiers on the type
1775 // we must propagate them down into the elemeng type.
1776 unsigned CVRQuals = T.getCVRQualifiers();
1777 unsigned AddrSpace = 0;
1778 Type *Ty = T.getTypePtr();
1779
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001780 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001781 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001782 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1783 AddrSpace = EXTQT->getAddressSpace();
1784 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001785 } else {
1786 T = Ty->getDesugaredType();
1787 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1788 break;
1789 CVRQuals |= T.getCVRQualifiers();
1790 Ty = T.getTypePtr();
1791 }
1792 }
1793
1794 // If we have a simple case, just return now.
1795 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1796 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1797 return ATy;
1798
1799 // Otherwise, we have an array and we have qualifiers on it. Push the
1800 // qualifiers into the array element type and return a new array type.
1801 // Get the canonical version of the element with the extra qualifiers on it.
1802 // This can recursively sink qualifiers through multiple levels of arrays.
1803 QualType NewEltTy = ATy->getElementType();
1804 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001805 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001806 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1807
1808 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1809 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1810 CAT->getSizeModifier(),
1811 CAT->getIndexTypeQualifier()));
1812 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1813 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1814 IAT->getSizeModifier(),
1815 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001816
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001817 if (const DependentSizedArrayType *DSAT
1818 = dyn_cast<DependentSizedArrayType>(ATy))
1819 return cast<ArrayType>(
1820 getDependentSizedArrayType(NewEltTy,
1821 DSAT->getSizeExpr(),
1822 DSAT->getSizeModifier(),
1823 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001824
Chris Lattnera1923f62008-08-04 07:31:14 +00001825 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1826 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1827 VAT->getSizeModifier(),
1828 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001829}
1830
1831
Chris Lattner19eb97e2008-04-02 05:18:44 +00001832/// getArrayDecayedType - Return the properly qualified result of decaying the
1833/// specified array type to a pointer. This operation is non-trivial when
1834/// handling typedefs etc. The canonical type of "T" must be an array type,
1835/// this returns a pointer to a properly qualified element of the array.
1836///
1837/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1838QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001839 // Get the element type with 'getAsArrayType' so that we don't lose any
1840 // typedefs in the element type of the array. This also handles propagation
1841 // of type qualifiers from the array type into the element type if present
1842 // (C99 6.7.3p8).
1843 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1844 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001845
Chris Lattnera1923f62008-08-04 07:31:14 +00001846 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001847
1848 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001849 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001850}
1851
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001852QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001853 QualType ElemTy = VAT->getElementType();
1854
1855 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1856 return getBaseElementType(VAT);
1857
1858 return ElemTy;
1859}
1860
Chris Lattner4b009652007-07-25 00:24:17 +00001861/// getFloatingRank - Return a relative rank for floating point types.
1862/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001863static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001864 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001865 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001866
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001867 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001868 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001869 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001870 case BuiltinType::Float: return FloatRank;
1871 case BuiltinType::Double: return DoubleRank;
1872 case BuiltinType::LongDouble: return LongDoubleRank;
1873 }
1874}
1875
Steve Narofffa0c4532007-08-27 01:41:48 +00001876/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1877/// point or a complex type (based on typeDomain/typeSize).
1878/// 'typeDomain' is a real floating point or complex type.
1879/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001880QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1881 QualType Domain) const {
1882 FloatingRank EltRank = getFloatingRank(Size);
1883 if (Domain->isComplexType()) {
1884 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001885 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001886 case FloatRank: return FloatComplexTy;
1887 case DoubleRank: return DoubleComplexTy;
1888 case LongDoubleRank: return LongDoubleComplexTy;
1889 }
Chris Lattner4b009652007-07-25 00:24:17 +00001890 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001891
1892 assert(Domain->isRealFloatingType() && "Unknown domain!");
1893 switch (EltRank) {
1894 default: assert(0 && "getFloatingRank(): illegal value for rank");
1895 case FloatRank: return FloatTy;
1896 case DoubleRank: return DoubleTy;
1897 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001898 }
Chris Lattner4b009652007-07-25 00:24:17 +00001899}
1900
Chris Lattner51285d82008-04-06 23:55:33 +00001901/// getFloatingTypeOrder - Compare the rank of the two specified floating
1902/// point types, ignoring the domain of the type (i.e. 'double' ==
1903/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1904/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001905int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1906 FloatingRank LHSR = getFloatingRank(LHS);
1907 FloatingRank RHSR = getFloatingRank(RHS);
1908
1909 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001910 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001911 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001912 return 1;
1913 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001914}
1915
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001916/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1917/// routine will assert if passed a built-in type that isn't an integer or enum,
1918/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001919unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001920 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001921 if (EnumType* ET = dyn_cast<EnumType>(T))
1922 T = ET->getDecl()->getIntegerType().getTypePtr();
1923
1924 // There are two things which impact the integer rank: the width, and
1925 // the ordering of builtins. The builtin ordering is encoded in the
1926 // bottom three bits; the width is encoded in the bits above that.
1927 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1928 return FWIT->getWidth() << 3;
1929 }
1930
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001931 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001932 default: assert(0 && "getIntegerRank(): not a built-in integer");
1933 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001934 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001935 case BuiltinType::Char_S:
1936 case BuiltinType::Char_U:
1937 case BuiltinType::SChar:
1938 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001939 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001940 case BuiltinType::Short:
1941 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001942 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001943 case BuiltinType::Int:
1944 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001945 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001946 case BuiltinType::Long:
1947 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001948 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001949 case BuiltinType::LongLong:
1950 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001951 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner6cc7e412009-04-30 02:43:43 +00001952 case BuiltinType::Int128:
1953 case BuiltinType::UInt128:
1954 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001955 }
1956}
1957
Chris Lattner51285d82008-04-06 23:55:33 +00001958/// getIntegerTypeOrder - Returns the highest ranked integer type:
1959/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1960/// LHS < RHS, return -1.
1961int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001962 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1963 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001964 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001965
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001966 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1967 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001968
Chris Lattner51285d82008-04-06 23:55:33 +00001969 unsigned LHSRank = getIntegerRank(LHSC);
1970 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001971
Chris Lattner51285d82008-04-06 23:55:33 +00001972 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1973 if (LHSRank == RHSRank) return 0;
1974 return LHSRank > RHSRank ? 1 : -1;
1975 }
Chris Lattner4b009652007-07-25 00:24:17 +00001976
Chris Lattner51285d82008-04-06 23:55:33 +00001977 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1978 if (LHSUnsigned) {
1979 // If the unsigned [LHS] type is larger, return it.
1980 if (LHSRank >= RHSRank)
1981 return 1;
1982
1983 // If the signed type can represent all values of the unsigned type, it
1984 // wins. Because we are dealing with 2's complement and types that are
1985 // powers of two larger than each other, this is always safe.
1986 return -1;
1987 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001988
Chris Lattner51285d82008-04-06 23:55:33 +00001989 // If the unsigned [RHS] type is larger, return it.
1990 if (RHSRank >= LHSRank)
1991 return -1;
1992
1993 // If the signed type can represent all values of the unsigned type, it
1994 // wins. Because we are dealing with 2's complement and types that are
1995 // powers of two larger than each other, this is always safe.
1996 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001997}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001998
1999// getCFConstantStringType - Return the type used for constant CFStrings.
2000QualType ASTContext::getCFConstantStringType() {
2001 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00002002 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00002003 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00002004 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002005 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002006
2007 // const int *isa;
2008 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002009 // int flags;
2010 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002011 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002012 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002013 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002014 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00002015
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002016 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00002017 for (unsigned i = 0; i < 4; ++i) {
2018 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2019 SourceLocation(), 0,
2020 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002021 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002022 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002023 }
2024
2025 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002026 }
2027
2028 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00002029}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002030
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002031void ASTContext::setCFConstantStringType(QualType T) {
2032 const RecordType *Rec = T->getAsRecordType();
2033 assert(Rec && "Invalid CFConstantStringType");
2034 CFConstantStringTypeDecl = Rec->getDecl();
2035}
2036
Anders Carlssonf58cac72008-08-30 19:34:46 +00002037QualType ASTContext::getObjCFastEnumerationStateType()
2038{
2039 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00002040 ObjCFastEnumerationStateTypeDecl =
2041 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2042 &Idents.get("__objcFastEnumerationState"));
2043
Anders Carlssonf58cac72008-08-30 19:34:46 +00002044 QualType FieldTypes[] = {
2045 UnsignedLongTy,
2046 getPointerType(ObjCIdType),
2047 getPointerType(UnsignedLongTy),
2048 getConstantArrayType(UnsignedLongTy,
2049 llvm::APInt(32, 5), ArrayType::Normal, 0)
2050 };
2051
Douglas Gregor8acb7272008-12-11 16:49:14 +00002052 for (size_t i = 0; i < 4; ++i) {
2053 FieldDecl *Field = FieldDecl::Create(*this,
2054 ObjCFastEnumerationStateTypeDecl,
2055 SourceLocation(), 0,
2056 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002057 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002058 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002059 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00002060
Douglas Gregor8acb7272008-12-11 16:49:14 +00002061 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00002062 }
2063
2064 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2065}
2066
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002067void ASTContext::setObjCFastEnumerationStateType(QualType T) {
2068 const RecordType *Rec = T->getAsRecordType();
2069 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2070 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2071}
2072
Anders Carlssone3f02572007-10-29 06:33:42 +00002073// This returns true if a type has been typedefed to BOOL:
2074// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00002075static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002076 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00002077 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2078 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002079
2080 return false;
2081}
2082
Ted Kremenek42730c52008-01-07 19:49:32 +00002083/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002084/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00002085int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00002086 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002087
2088 // Make all integer and enum types at least as large as an int
2089 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002090 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002091 // Treat arrays as pointers, since that's how they're passed in.
2092 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002093 sz = getTypeSize(VoidPtrTy);
2094 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002095}
2096
Ted Kremenek42730c52008-01-07 19:49:32 +00002097/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002098/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002099void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00002100 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002101 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002102 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00002103 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002104 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002105 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002106 // Compute size of all parameters.
2107 // Start with computing size of a pointer in number of bytes.
2108 // FIXME: There might(should) be a better way of doing this computation!
2109 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00002110 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002111 // The first two arguments (self and _cmd) are pointers; account for
2112 // their size.
2113 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002114 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2115 E = Decl->param_end(); PI != E; ++PI) {
2116 QualType PType = (*PI)->getType();
2117 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00002118 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002119 ParmOffset += sz;
2120 }
2121 S += llvm::utostr(ParmOffset);
2122 S += "@0:";
2123 S += llvm::utostr(PtrSize);
2124
2125 // Argument types.
2126 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002127 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2128 E = Decl->param_end(); PI != E; ++PI) {
2129 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002130 QualType PType = PVDecl->getOriginalType();
2131 if (const ArrayType *AT =
Steve Naroff78380fb2009-04-14 00:03:58 +00002132 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2133 // Use array's original type only if it has known number of
2134 // elements.
Steve Naroff6777bf32009-04-14 00:40:09 +00002135 if (!isa<ConstantArrayType>(AT))
Steve Naroff78380fb2009-04-14 00:03:58 +00002136 PType = PVDecl->getType();
2137 } else if (PType->isFunctionType())
2138 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002139 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002140 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002141 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002142 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002143 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00002144 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002145 }
2146}
2147
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002148/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002149/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002150/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2151/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002152/// Property attributes are stored as a comma-delimited C string. The simple
2153/// attributes readonly and bycopy are encoded as single characters. The
2154/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2155/// encoded as single characters, followed by an identifier. Property types
2156/// are also encoded as a parametrized attribute. The characters used to encode
2157/// these attributes are defined by the following enumeration:
2158/// @code
2159/// enum PropertyAttributes {
2160/// kPropertyReadOnly = 'R', // property is read-only.
2161/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2162/// kPropertyByref = '&', // property is a reference to the value last assigned
2163/// kPropertyDynamic = 'D', // property is dynamic
2164/// kPropertyGetter = 'G', // followed by getter selector name
2165/// kPropertySetter = 'S', // followed by setter selector name
2166/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2167/// kPropertyType = 't' // followed by old-style type encoding.
2168/// kPropertyWeak = 'W' // 'weak' property
2169/// kPropertyStrong = 'P' // property GC'able
2170/// kPropertyNonAtomic = 'N' // property non-atomic
2171/// };
2172/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002173void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2174 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00002175 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002176 // Collect information from the property implementation decl(s).
2177 bool Dynamic = false;
2178 ObjCPropertyImplDecl *SynthesizePID = 0;
2179
2180 // FIXME: Duplicated code due to poor abstraction.
2181 if (Container) {
2182 if (const ObjCCategoryImplDecl *CID =
2183 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2184 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregorcd19b572009-04-23 01:02:12 +00002185 i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2186 i != e; ++i) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002187 ObjCPropertyImplDecl *PID = *i;
2188 if (PID->getPropertyDecl() == PD) {
2189 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2190 Dynamic = true;
2191 } else {
2192 SynthesizePID = PID;
2193 }
2194 }
2195 }
2196 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002197 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002198 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregorcd19b572009-04-23 01:02:12 +00002199 i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2200 i != e; ++i) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002201 ObjCPropertyImplDecl *PID = *i;
2202 if (PID->getPropertyDecl() == PD) {
2203 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2204 Dynamic = true;
2205 } else {
2206 SynthesizePID = PID;
2207 }
2208 }
2209 }
2210 }
2211 }
2212
2213 // FIXME: This is not very efficient.
2214 S = "T";
2215
2216 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002217 // GCC has some special rules regarding encoding of properties which
2218 // closely resembles encoding of ivars.
Daniel Dunbar701c8502009-04-20 06:37:24 +00002219 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002220 true /* outermost type */,
2221 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002222
2223 if (PD->isReadOnly()) {
2224 S += ",R";
2225 } else {
2226 switch (PD->getSetterKind()) {
2227 case ObjCPropertyDecl::Assign: break;
2228 case ObjCPropertyDecl::Copy: S += ",C"; break;
2229 case ObjCPropertyDecl::Retain: S += ",&"; break;
2230 }
2231 }
2232
2233 // It really isn't clear at all what this means, since properties
2234 // are "dynamic by default".
2235 if (Dynamic)
2236 S += ",D";
2237
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002238 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2239 S += ",N";
2240
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002241 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2242 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002243 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002244 }
2245
2246 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2247 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002248 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002249 }
2250
2251 if (SynthesizePID) {
2252 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2253 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002254 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002255 }
2256
2257 // FIXME: OBJCGC: weak & strong
2258}
2259
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002260/// getLegacyIntegralTypeEncoding -
2261/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002262/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002263/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2264///
2265void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2266 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2267 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002268 if (BT->getKind() == BuiltinType::ULong &&
2269 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002270 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002271 else
2272 if (BT->getKind() == BuiltinType::Long &&
2273 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002274 PointeeTy = IntTy;
2275 }
2276 }
2277}
2278
Fariborz Jahanian248db262008-01-22 22:44:46 +00002279void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002280 const FieldDecl *Field) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002281 // We follow the behavior of gcc, expanding structures which are
2282 // directly pointed to, and expanding embedded structures. Note that
2283 // these rules are sufficient to prevent recursive encoding of the
2284 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002285 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2286 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002287}
2288
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002289static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002290 const FieldDecl *FD) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002291 const Expr *E = FD->getBitWidth();
2292 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2293 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman5255e7a2009-04-26 19:19:15 +00002294 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002295 S += 'b';
2296 S += llvm::utostr(N);
2297}
2298
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002299void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2300 bool ExpandPointedToStructures,
2301 bool ExpandStructures,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002302 const FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002303 bool OutermostType,
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002304 bool EncodingProperty) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002305 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002306 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002307 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002308 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002309 else {
2310 char encoding;
2311 switch (BT->getKind()) {
2312 default: assert(0 && "Unhandled builtin type kind");
2313 case BuiltinType::Void: encoding = 'v'; break;
2314 case BuiltinType::Bool: encoding = 'B'; break;
2315 case BuiltinType::Char_U:
2316 case BuiltinType::UChar: encoding = 'C'; break;
2317 case BuiltinType::UShort: encoding = 'S'; break;
2318 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002319 case BuiltinType::ULong:
2320 encoding =
2321 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2322 break;
Chris Lattner6cc7e412009-04-30 02:43:43 +00002323 case BuiltinType::UInt128: encoding = 'T'; break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002324 case BuiltinType::ULongLong: encoding = 'Q'; break;
2325 case BuiltinType::Char_S:
2326 case BuiltinType::SChar: encoding = 'c'; break;
2327 case BuiltinType::Short: encoding = 's'; break;
2328 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002329 case BuiltinType::Long:
2330 encoding =
2331 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2332 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002333 case BuiltinType::LongLong: encoding = 'q'; break;
Chris Lattner6cc7e412009-04-30 02:43:43 +00002334 case BuiltinType::Int128: encoding = 't'; break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002335 case BuiltinType::Float: encoding = 'f'; break;
2336 case BuiltinType::Double: encoding = 'd'; break;
2337 case BuiltinType::LongDouble: encoding = 'd'; break;
2338 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002339
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002340 S += encoding;
2341 }
Anders Carlsson70e16dd2009-04-09 21:55:45 +00002342 } else if (const ComplexType *CT = T->getAsComplexType()) {
2343 S += 'j';
2344 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2345 false);
2346 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002347 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2348 ExpandPointedToStructures,
2349 ExpandStructures, FD);
2350 if (FD || EncodingProperty) {
2351 // Note that we do extended encoding of protocol qualifer list
2352 // Only when doing ivar or property encoding.
2353 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2354 S += '"';
2355 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2356 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2357 S += '<';
2358 S += Proto->getNameAsString();
2359 S += '>';
2360 }
2361 S += '"';
2362 }
2363 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002364 }
2365 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002366 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002367 bool isReadOnly = false;
2368 // For historical/compatibility reasons, the read-only qualifier of the
2369 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2370 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2371 // Also, do not emit the 'r' for anything but the outermost type!
2372 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2373 if (OutermostType && T.isConstQualified()) {
2374 isReadOnly = true;
2375 S += 'r';
2376 }
2377 }
2378 else if (OutermostType) {
2379 QualType P = PointeeTy;
2380 while (P->getAsPointerType())
2381 P = P->getAsPointerType()->getPointeeType();
2382 if (P.isConstQualified()) {
2383 isReadOnly = true;
2384 S += 'r';
2385 }
2386 }
2387 if (isReadOnly) {
2388 // Another legacy compatibility encoding. Some ObjC qualifier and type
2389 // combinations need to be rearranged.
2390 // Rewrite "in const" from "nr" to "rn"
2391 const char * s = S.c_str();
2392 int len = S.length();
2393 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2394 std::string replace = "rn";
2395 S.replace(S.end()-2, S.end(), replace);
2396 }
2397 }
Steve Naroff17c03822009-02-12 17:52:19 +00002398 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002399 S += '@';
2400 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002401 }
2402 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002403 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002404 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002405 // Another historical/compatibility reason.
2406 // We encode the underlying type which comes out as
2407 // {...};
2408 S += '^';
2409 getObjCEncodingForTypeImpl(PointeeTy, S,
2410 false, ExpandPointedToStructures,
2411 NULL);
2412 return;
2413 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002414 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002415 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002416 const ObjCInterfaceType *OIT =
2417 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002418 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002419 S += '"';
2420 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002421 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2422 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2423 S += '<';
2424 S += Proto->getNameAsString();
2425 S += '>';
2426 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002427 S += '"';
2428 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002429 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002430 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002431 S += '#';
2432 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002433 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002434 S += ':';
2435 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002436 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002437
2438 if (PointeeTy->isCharType()) {
2439 // char pointer types should be encoded as '*' unless it is a
2440 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002441 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002442 S += '*';
2443 return;
2444 }
2445 }
2446
2447 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002448 getLegacyIntegralTypeEncoding(PointeeTy);
2449
2450 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002451 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002452 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002453 } else if (const ArrayType *AT =
2454 // Ignore type qualifiers etc.
2455 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002456 if (isa<IncompleteArrayType>(AT)) {
2457 // Incomplete arrays are encoded as a pointer to the array element.
2458 S += '^';
2459
2460 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2461 false, ExpandStructures, FD);
2462 } else {
2463 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002464
Anders Carlsson858c64d2009-02-22 01:38:57 +00002465 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2466 S += llvm::utostr(CAT->getSize().getZExtValue());
2467 else {
2468 //Variable length arrays are encoded as a regular array with 0 elements.
2469 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2470 S += '0';
2471 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002472
Anders Carlsson858c64d2009-02-22 01:38:57 +00002473 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2474 false, ExpandStructures, FD);
2475 S += ']';
2476 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002477 } else if (T->getAsFunctionType()) {
2478 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002479 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002480 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002481 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002482 // Anonymous structures print as '?'
2483 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2484 S += II->getName();
2485 } else {
2486 S += '?';
2487 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002488 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002489 S += '=';
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002490 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2491 FieldEnd = RDecl->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002492 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002493 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002494 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002495 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002496 S += '"';
2497 }
2498
2499 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002500 if (Field->isBitField()) {
2501 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2502 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002503 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002504 QualType qt = Field->getType();
2505 getLegacyIntegralTypeEncoding(qt);
2506 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002507 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002508 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002509 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002510 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002511 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002512 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002513 if (FD && FD->isBitField())
2514 EncodeBitField(this, S, FD);
2515 else
2516 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002517 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002518 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002519 } else if (T->isObjCInterfaceType()) {
2520 // @encode(class_name)
2521 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2522 S += '{';
2523 const IdentifierInfo *II = OI->getIdentifier();
2524 S += II->getName();
2525 S += '=';
Chris Lattner9329cf52009-03-31 08:48:01 +00002526 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002527 CollectObjCIvars(OI, RecFields);
Chris Lattner9329cf52009-03-31 08:48:01 +00002528 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002529 if (RecFields[i]->isBitField())
2530 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2531 RecFields[i]);
2532 else
2533 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2534 FD);
2535 }
2536 S += '}';
2537 }
2538 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002539 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002540}
2541
Ted Kremenek42730c52008-01-07 19:49:32 +00002542void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002543 std::string& S) const {
2544 if (QT & Decl::OBJC_TQ_In)
2545 S += 'n';
2546 if (QT & Decl::OBJC_TQ_Inout)
2547 S += 'N';
2548 if (QT & Decl::OBJC_TQ_Out)
2549 S += 'o';
2550 if (QT & Decl::OBJC_TQ_Bycopy)
2551 S += 'O';
2552 if (QT & Decl::OBJC_TQ_Byref)
2553 S += 'R';
2554 if (QT & Decl::OBJC_TQ_Oneway)
2555 S += 'V';
2556}
2557
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002558void ASTContext::setBuiltinVaListType(QualType T)
2559{
2560 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2561
2562 BuiltinVaListType = T;
2563}
2564
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002565void ASTContext::setObjCIdType(QualType T)
Steve Naroff9d12c902007-10-15 14:41:52 +00002566{
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002567 ObjCIdType = T;
2568
2569 const TypedefType *TT = T->getAsTypedefType();
2570 if (!TT)
2571 return;
2572
2573 TypedefDecl *TD = TT->getDecl();
Steve Naroff9d12c902007-10-15 14:41:52 +00002574
2575 // typedef struct objc_object *id;
2576 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002577 // User error - caller will issue diagnostics.
2578 if (!ptr)
2579 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002580 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002581 // User error - caller will issue diagnostics.
2582 if (!rec)
2583 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002584 IdStructType = rec;
2585}
2586
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002587void ASTContext::setObjCSelType(QualType T)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002588{
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002589 ObjCSelType = T;
2590
2591 const TypedefType *TT = T->getAsTypedefType();
2592 if (!TT)
2593 return;
2594 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002595
2596 // typedef struct objc_selector *SEL;
2597 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002598 if (!ptr)
2599 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002600 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002601 if (!rec)
2602 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002603 SelStructType = rec;
2604}
2605
Ted Kremenek42730c52008-01-07 19:49:32 +00002606void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002607{
Ted Kremenek42730c52008-01-07 19:49:32 +00002608 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002609}
2610
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002611void ASTContext::setObjCClassType(QualType T)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002612{
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002613 ObjCClassType = T;
2614
2615 const TypedefType *TT = T->getAsTypedefType();
2616 if (!TT)
2617 return;
2618 TypedefDecl *TD = TT->getDecl();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002619
2620 // typedef struct objc_class *Class;
2621 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2622 assert(ptr && "'Class' incorrectly typed");
2623 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2624 assert(rec && "'Class' incorrectly typed");
2625 ClassStructType = rec;
2626}
2627
Ted Kremenek42730c52008-01-07 19:49:32 +00002628void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2629 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002630 "'NSConstantString' type already set!");
2631
Ted Kremenek42730c52008-01-07 19:49:32 +00002632 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002633}
2634
Douglas Gregordd13e842009-03-30 22:58:21 +00002635/// \brief Retrieve the template name that represents a qualified
2636/// template name such as \c std::vector.
2637TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2638 bool TemplateKeyword,
2639 TemplateDecl *Template) {
2640 llvm::FoldingSetNodeID ID;
2641 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2642
2643 void *InsertPos = 0;
2644 QualifiedTemplateName *QTN =
2645 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2646 if (!QTN) {
2647 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2648 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2649 }
2650
2651 return TemplateName(QTN);
2652}
2653
2654/// \brief Retrieve the template name that represents a dependent
2655/// template name such as \c MetaFun::template apply.
2656TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2657 const IdentifierInfo *Name) {
2658 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2659
2660 llvm::FoldingSetNodeID ID;
2661 DependentTemplateName::Profile(ID, NNS, Name);
2662
2663 void *InsertPos = 0;
2664 DependentTemplateName *QTN =
2665 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2666
2667 if (QTN)
2668 return TemplateName(QTN);
2669
2670 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2671 if (CanonNNS == NNS) {
2672 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2673 } else {
2674 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2675 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2676 }
2677
2678 DependentTemplateNames.InsertNode(QTN, InsertPos);
2679 return TemplateName(QTN);
2680}
2681
Douglas Gregorc6507e42008-11-03 14:12:49 +00002682/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002683/// TargetInfo, produce the corresponding type. The unsigned @p Type
2684/// is actually a value of type @c TargetInfo::IntType.
2685QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002686 switch (Type) {
2687 case TargetInfo::NoInt: return QualType();
2688 case TargetInfo::SignedShort: return ShortTy;
2689 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2690 case TargetInfo::SignedInt: return IntTy;
2691 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2692 case TargetInfo::SignedLong: return LongTy;
2693 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2694 case TargetInfo::SignedLongLong: return LongLongTy;
2695 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2696 }
2697
2698 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002699 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002700}
Ted Kremenek118930e2008-07-24 23:58:27 +00002701
2702//===----------------------------------------------------------------------===//
2703// Type Predicates.
2704//===----------------------------------------------------------------------===//
2705
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002706/// isObjCNSObjectType - Return true if this is an NSObject object using
2707/// NSObject attribute on a c-style pointer type.
2708/// FIXME - Make it work directly on types.
2709///
2710bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2711 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2712 if (TypedefDecl *TD = TDT->getDecl())
2713 if (TD->getAttr<ObjCNSObjectAttr>())
2714 return true;
2715 }
2716 return false;
2717}
2718
Ted Kremenek118930e2008-07-24 23:58:27 +00002719/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2720/// to an object type. This includes "id" and "Class" (two 'special' pointers
2721/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2722/// ID type).
2723bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroff6805fc42009-02-23 18:36:16 +00002724 if (Ty->isObjCQualifiedIdType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002725 return true;
2726
Steve Naroffd9e00802008-10-21 18:24:04 +00002727 // Blocks are objects.
2728 if (Ty->isBlockPointerType())
2729 return true;
2730
2731 // All other object types are pointers.
Chris Lattnera008d172009-04-12 23:51:02 +00002732 const PointerType *PT = Ty->getAsPointerType();
2733 if (PT == 0)
Ted Kremenek118930e2008-07-24 23:58:27 +00002734 return false;
2735
Chris Lattnera008d172009-04-12 23:51:02 +00002736 // If this a pointer to an interface (e.g. NSString*), it is ok.
2737 if (PT->getPointeeType()->isObjCInterfaceType() ||
2738 // If is has NSObject attribute, OK as well.
2739 isObjCNSObjectType(Ty))
2740 return true;
2741
Ted Kremenek118930e2008-07-24 23:58:27 +00002742 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2743 // pointer types. This looks for the typedef specifically, not for the
Chris Lattnera008d172009-04-12 23:51:02 +00002744 // underlying type. Iteratively strip off typedefs so that we can handle
2745 // typedefs of typedefs.
2746 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2747 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2748 Ty.getUnqualifiedType() == getObjCClassType())
2749 return true;
2750
2751 Ty = TDT->getDecl()->getUnderlyingType();
2752 }
Ted Kremenek118930e2008-07-24 23:58:27 +00002753
Chris Lattnera008d172009-04-12 23:51:02 +00002754 return false;
Ted Kremenek118930e2008-07-24 23:58:27 +00002755}
2756
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002757/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2758/// garbage collection attribute.
2759///
2760QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002761 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002762 if (getLangOptions().ObjC1 &&
2763 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002764 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002765 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002766 // (or pointers to them) be treated as though they were declared
2767 // as __strong.
2768 if (GCAttrs == QualType::GCNone) {
2769 if (isObjCObjectPointerType(Ty))
2770 GCAttrs = QualType::Strong;
2771 else if (Ty->isPointerType())
2772 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2773 }
Fariborz Jahaniand7b01972009-04-11 00:00:54 +00002774 // Non-pointers have none gc'able attribute regardless of the attribute
2775 // set on them.
2776 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2777 return QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002778 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002779 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002780}
2781
Chris Lattner6ff358b2008-04-07 06:51:04 +00002782//===----------------------------------------------------------------------===//
2783// Type Compatibility Testing
2784//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002785
Steve Naroff3454b6c2008-09-04 15:10:53 +00002786/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002787/// block types. Types must be strictly compatible here. For example,
2788/// C unfortunately doesn't produce an error for the following:
2789///
2790/// int (*emptyArgFunc)();
2791/// int (*intArgList)(int) = emptyArgFunc;
2792///
2793/// For blocks, we will produce an error for the following (similar to C++):
2794///
2795/// int (^emptyArgBlock)();
2796/// int (^intArgBlock)(int) = emptyArgBlock;
2797///
2798/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2799///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002800bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002801 const FunctionType *lbase = lhs->getAsFunctionType();
2802 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002803 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2804 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stump896ceed2009-04-01 01:17:39 +00002805 if (lproto && rproto == 0)
2806 return false;
2807 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff3454b6c2008-09-04 15:10:53 +00002808}
2809
Chris Lattner6ff358b2008-04-07 06:51:04 +00002810/// areCompatVectorTypes - Return true if the two specified vector types are
2811/// compatible.
2812static bool areCompatVectorTypes(const VectorType *LHS,
2813 const VectorType *RHS) {
2814 assert(LHS->isCanonical() && RHS->isCanonical());
2815 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002816 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002817}
2818
Eli Friedman0d9549b2008-08-22 00:56:42 +00002819/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002820/// compatible for assignment from RHS to LHS. This handles validation of any
2821/// protocol qualifiers on the LHS or RHS.
2822///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002823bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2824 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002825 // Verify that the base decls are compatible: the RHS must be a subclass of
2826 // the LHS.
2827 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2828 return false;
2829
2830 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2831 // protocol qualified at all, then we are good.
2832 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2833 return true;
2834
2835 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2836 // isn't a superset.
2837 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2838 return true; // FIXME: should return false!
2839
2840 // Finally, we must have two protocol-qualified interfaces.
2841 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2842 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ff358b2008-04-07 06:51:04 +00002843
Steve Naroff98e71b82009-03-01 16:12:44 +00002844 // All LHS protocols must have a presence on the RHS.
2845 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ff358b2008-04-07 06:51:04 +00002846
Steve Naroff98e71b82009-03-01 16:12:44 +00002847 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2848 LHSPE = LHSP->qual_end();
2849 LHSPI != LHSPE; LHSPI++) {
2850 bool RHSImplementsProtocol = false;
2851
2852 // If the RHS doesn't implement the protocol on the left, the types
2853 // are incompatible.
2854 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2855 RHSPE = RHSP->qual_end();
2856 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2857 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2858 RHSImplementsProtocol = true;
2859 }
2860 // FIXME: For better diagnostics, consider passing back the protocol name.
2861 if (!RHSImplementsProtocol)
2862 return false;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002863 }
Steve Naroff98e71b82009-03-01 16:12:44 +00002864 // The RHS implements all protocols listed on the LHS.
2865 return true;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002866}
2867
Steve Naroff17c03822009-02-12 17:52:19 +00002868bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2869 // get the "pointed to" types
2870 const PointerType *LHSPT = LHS->getAsPointerType();
2871 const PointerType *RHSPT = RHS->getAsPointerType();
2872
2873 if (!LHSPT || !RHSPT)
2874 return false;
2875
2876 QualType lhptee = LHSPT->getPointeeType();
2877 QualType rhptee = RHSPT->getPointeeType();
2878 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2879 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2880 // ID acts sort of like void* for ObjC interfaces
2881 if (LHSIface && isObjCIdStructType(rhptee))
2882 return true;
2883 if (RHSIface && isObjCIdStructType(lhptee))
2884 return true;
2885 if (!LHSIface || !RHSIface)
2886 return false;
2887 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2888 canAssignObjCInterfaces(RHSIface, LHSIface);
2889}
2890
Steve Naroff85f0dc52007-10-15 20:41:53 +00002891/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2892/// both shall have the identically qualified version of a compatible type.
2893/// C99 6.2.7p1: Two types have compatible types if their types are the
2894/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002895bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2896 return !mergeTypes(LHS, RHS).isNull();
2897}
2898
2899QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2900 const FunctionType *lbase = lhs->getAsFunctionType();
2901 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002902 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2903 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002904 bool allLTypes = true;
2905 bool allRTypes = true;
2906
2907 // Check return type
2908 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2909 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002910 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2911 allLTypes = false;
2912 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2913 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002914
2915 if (lproto && rproto) { // two C99 style function prototypes
2916 unsigned lproto_nargs = lproto->getNumArgs();
2917 unsigned rproto_nargs = rproto->getNumArgs();
2918
2919 // Compatible functions must have the same number of arguments
2920 if (lproto_nargs != rproto_nargs)
2921 return QualType();
2922
2923 // Variadic and non-variadic functions aren't compatible
2924 if (lproto->isVariadic() != rproto->isVariadic())
2925 return QualType();
2926
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002927 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2928 return QualType();
2929
Eli Friedman0d9549b2008-08-22 00:56:42 +00002930 // Check argument compatibility
2931 llvm::SmallVector<QualType, 10> types;
2932 for (unsigned i = 0; i < lproto_nargs; i++) {
2933 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2934 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2935 QualType argtype = mergeTypes(largtype, rargtype);
2936 if (argtype.isNull()) return QualType();
2937 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002938 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2939 allLTypes = false;
2940 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2941 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002942 }
2943 if (allLTypes) return lhs;
2944 if (allRTypes) return rhs;
2945 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002946 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002947 }
2948
2949 if (lproto) allRTypes = false;
2950 if (rproto) allLTypes = false;
2951
Douglas Gregor4fa58902009-02-26 23:50:07 +00002952 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002953 if (proto) {
2954 if (proto->isVariadic()) return QualType();
2955 // Check that the types are compatible with the types that
2956 // would result from default argument promotions (C99 6.7.5.3p15).
2957 // The only types actually affected are promotable integer
2958 // types and floats, which would be passed as a different
2959 // type depending on whether the prototype is visible.
2960 unsigned proto_nargs = proto->getNumArgs();
2961 for (unsigned i = 0; i < proto_nargs; ++i) {
2962 QualType argTy = proto->getArgType(i);
2963 if (argTy->isPromotableIntegerType() ||
2964 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2965 return QualType();
2966 }
2967
2968 if (allLTypes) return lhs;
2969 if (allRTypes) return rhs;
2970 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002971 proto->getNumArgs(), lproto->isVariadic(),
2972 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002973 }
2974
2975 if (allLTypes) return lhs;
2976 if (allRTypes) return rhs;
Douglas Gregor4fa58902009-02-26 23:50:07 +00002977 return getFunctionNoProtoType(retType);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002978}
2979
2980QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002981 // C++ [expr]: If an expression initially has the type "reference to T", the
2982 // type is adjusted to "T" prior to any further analysis, the expression
2983 // designates the object or function denoted by the reference, and the
Sebastian Redlce6fff02009-03-16 23:22:08 +00002984 // expression is an lvalue unless the reference is an rvalue reference and
2985 // the expression is a function call (possibly inside parentheses).
Eli Friedman0d9549b2008-08-22 00:56:42 +00002986 // FIXME: C++ shouldn't be going through here! The rules are different
2987 // enough that they should be handled separately.
Sebastian Redlce6fff02009-03-16 23:22:08 +00002988 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2989 // shouldn't be going through here!
Eli Friedman0d9549b2008-08-22 00:56:42 +00002990 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002991 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002992 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002993 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002994
Eli Friedman0d9549b2008-08-22 00:56:42 +00002995 QualType LHSCan = getCanonicalType(LHS),
2996 RHSCan = getCanonicalType(RHS);
2997
2998 // If two types are identical, they are compatible.
2999 if (LHSCan == RHSCan)
3000 return LHS;
3001
3002 // If the qualifiers are different, the types aren't compatible
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003003 // Note that we handle extended qualifiers later, in the
3004 // case for ExtQualType.
3005 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman0d9549b2008-08-22 00:56:42 +00003006 return QualType();
3007
Fariborz Jahanian0dc684e2009-04-15 21:54:48 +00003008 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
3009 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman0d9549b2008-08-22 00:56:42 +00003010
Chris Lattnerc38d4522008-01-14 05:45:46 +00003011 // We want to consider the two function types to be the same for these
3012 // comparisons, just force one to the other.
3013 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3014 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00003015
3016 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00003017 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3018 LHSClass = Type::ConstantArray;
3019 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3020 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003021
Nate Begemanaf6ed502008-04-18 23:10:10 +00003022 // Canonicalize ExtVector -> Vector.
3023 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3024 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00003025
Chris Lattner7cdcb252008-04-07 06:38:24 +00003026 // Consider qualified interfaces and interfaces the same.
3027 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
3028 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003029
Chris Lattnerb5709e22008-04-07 05:43:21 +00003030 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00003031 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003032 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3033 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanian0dc684e2009-04-15 21:54:48 +00003034
Steve Naroff0773c582009-04-14 15:11:46 +00003035 // 'id' and 'Class' act sort of like void* for ObjC interfaces
3036 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00003037 return LHS;
Steve Naroff0773c582009-04-14 15:11:46 +00003038 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00003039 return RHS;
3040
Steve Naroff28ceff72008-12-10 22:14:21 +00003041 // ID is compatible with all qualified id types.
3042 if (LHS->isObjCQualifiedIdType()) {
3043 if (const PointerType *PT = RHS->getAsPointerType()) {
3044 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00003045 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00003046 return LHS;
3047 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3048 // Unfortunately, this API is part of Sema (which we don't have access
3049 // to. Need to refactor. The following check is insufficient, since we
3050 // need to make sure the class implements the protocol.
3051 if (pType->isObjCInterfaceType())
3052 return LHS;
3053 }
3054 }
3055 if (RHS->isObjCQualifiedIdType()) {
3056 if (const PointerType *PT = LHS->getAsPointerType()) {
3057 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00003058 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00003059 return RHS;
3060 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3061 // Unfortunately, this API is part of Sema (which we don't have access
3062 // to. Need to refactor. The following check is insufficient, since we
3063 // need to make sure the class implements the protocol.
3064 if (pType->isObjCInterfaceType())
3065 return RHS;
3066 }
3067 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003068 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3069 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003070 if (const EnumType* ETy = LHS->getAsEnumType()) {
3071 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3072 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00003073 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003074 if (const EnumType* ETy = RHS->getAsEnumType()) {
3075 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3076 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00003077 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003078
Eli Friedman0d9549b2008-08-22 00:56:42 +00003079 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003080 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003081
Steve Naroffc88babe2008-01-09 22:43:08 +00003082 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00003083 switch (LHSClass) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00003084#define TYPE(Class, Base)
3085#define ABSTRACT_TYPE(Class, Base)
3086#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3087#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3088#include "clang/AST/TypeNodes.def"
3089 assert(false && "Non-canonical and dependent types shouldn't get here");
3090 return QualType();
3091
Sebastian Redlce6fff02009-03-16 23:22:08 +00003092 case Type::LValueReference:
3093 case Type::RValueReference:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003094 case Type::MemberPointer:
3095 assert(false && "C++ should never be in mergeTypes");
3096 return QualType();
3097
3098 case Type::IncompleteArray:
3099 case Type::VariableArray:
3100 case Type::FunctionProto:
3101 case Type::ExtVector:
3102 case Type::ObjCQualifiedInterface:
3103 assert(false && "Types are eliminated above");
3104 return QualType();
3105
Chris Lattnerc38d4522008-01-14 05:45:46 +00003106 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003107 {
3108 // Merge two pointer types, while trying to preserve typedef info
3109 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3110 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3111 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3112 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003113 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3114 return LHS;
3115 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3116 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003117 return getPointerType(ResultType);
3118 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00003119 case Type::BlockPointer:
3120 {
3121 // Merge two block pointer types, while trying to preserve typedef info
3122 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3123 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3124 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3125 if (ResultType.isNull()) return QualType();
3126 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3127 return LHS;
3128 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3129 return RHS;
3130 return getBlockPointerType(ResultType);
3131 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003132 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003133 {
3134 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3135 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3136 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3137 return QualType();
3138
3139 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3140 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3141 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3142 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003143 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3144 return LHS;
3145 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3146 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003147 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3148 ArrayType::ArraySizeModifier(), 0);
3149 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3150 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003151 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3152 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003153 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3154 return LHS;
3155 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3156 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003157 if (LVAT) {
3158 // FIXME: This isn't correct! But tricky to implement because
3159 // the array's size has to be the size of LHS, but the type
3160 // has to be different.
3161 return LHS;
3162 }
3163 if (RVAT) {
3164 // FIXME: This isn't correct! But tricky to implement because
3165 // the array's size has to be the size of RHS, but the type
3166 // has to be different.
3167 return RHS;
3168 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003169 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3170 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003171 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003172 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003173 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003174 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor4fa58902009-02-26 23:50:07 +00003175 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003176 case Type::Enum:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003177 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00003178 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3179 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003180 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00003181 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003182 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003183 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00003184 case Type::Complex:
3185 // Distinct complex types are incompatible.
3186 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003187 case Type::Vector:
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003188 // FIXME: The merged type should be an ExtVector!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003189 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3190 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003191 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003192 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003193 // Check if the interfaces are assignment compatible.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003194 // FIXME: This should be type compatibility, e.g. whether
3195 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff0bbc1352009-02-21 16:18:07 +00003196 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3197 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3198 if (LHSIface && RHSIface &&
3199 canAssignObjCInterfaces(LHSIface, RHSIface))
3200 return LHS;
3201
Eli Friedman0d9549b2008-08-22 00:56:42 +00003202 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003203 }
Steve Naroff28ceff72008-12-10 22:14:21 +00003204 case Type::ObjCQualifiedId:
3205 // Distinct qualified id's are not compatible.
3206 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003207 case Type::FixedWidthInt:
3208 // Distinct fixed-width integers are not compatible.
3209 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003210 case Type::ExtQual:
3211 // FIXME: ExtQual types can be compatible even if they're not
3212 // identical!
3213 return QualType();
3214 // First attempt at an implementation, but I'm not really sure it's
3215 // right...
3216#if 0
3217 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3218 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3219 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3220 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3221 return QualType();
3222 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3223 LHSBase = QualType(LQual->getBaseType(), 0);
3224 RHSBase = QualType(RQual->getBaseType(), 0);
3225 ResultType = mergeTypes(LHSBase, RHSBase);
3226 if (ResultType.isNull()) return QualType();
3227 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3228 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3229 return LHS;
3230 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3231 return RHS;
3232 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3233 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3234 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3235 return ResultType;
3236#endif
Douglas Gregordd13e842009-03-30 22:58:21 +00003237
3238 case Type::TemplateSpecialization:
3239 assert(false && "Dependent types have no size");
3240 break;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003241 }
Douglas Gregor4fa58902009-02-26 23:50:07 +00003242
3243 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003244}
Ted Kremenek738e6c02007-10-31 17:10:13 +00003245
Chris Lattner1d78a862008-04-07 07:01:58 +00003246//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00003247// Integer Predicates
3248//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00003249
Eli Friedman0832dbc2008-06-28 06:23:08 +00003250unsigned ASTContext::getIntWidth(QualType T) {
3251 if (T == BoolTy)
3252 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00003253 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3254 return FWIT->getWidth();
3255 }
3256 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00003257 return (unsigned)getTypeSize(T);
3258}
3259
3260QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3261 assert(T->isSignedIntegerType() && "Unexpected type");
3262 if (const EnumType* ETy = T->getAsEnumType())
3263 T = ETy->getDecl()->getIntegerType();
3264 const BuiltinType* BTy = T->getAsBuiltinType();
3265 assert (BTy && "Unexpected signed integer type");
3266 switch (BTy->getKind()) {
3267 case BuiltinType::Char_S:
3268 case BuiltinType::SChar:
3269 return UnsignedCharTy;
3270 case BuiltinType::Short:
3271 return UnsignedShortTy;
3272 case BuiltinType::Int:
3273 return UnsignedIntTy;
3274 case BuiltinType::Long:
3275 return UnsignedLongTy;
3276 case BuiltinType::LongLong:
3277 return UnsignedLongLongTy;
Chris Lattner6cc7e412009-04-30 02:43:43 +00003278 case BuiltinType::Int128:
3279 return UnsignedInt128Ty;
Eli Friedman0832dbc2008-06-28 06:23:08 +00003280 default:
3281 assert(0 && "Unexpected signed integer type");
3282 return QualType();
3283 }
3284}
3285
Douglas Gregorc34897d2009-04-09 22:27:44 +00003286ExternalASTSource::~ExternalASTSource() { }
3287
3288void ExternalASTSource::PrintStats() { }