blob: 32adf3ae7353fa663357d6abf2e1c1ea1a4c3c72 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000019#include "clang/AST/ExternalASTSource.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000021#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000023#include "llvm/ADT/StringExtras.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000025#include "llvm/Support/MemoryBuffer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner61710852008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Douglas Gregor2deaea32009-04-22 18:49:13 +000035 bool FreeMem, unsigned size_reserve,
36 bool InitializeBuiltins) :
Douglas Gregorab452ba2009-03-26 23:50:42 +000037 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
38 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Douglas Gregor2cf26342009-04-09 22:27:44 +000039 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels),
40 ExternalSource(0) {
Daniel Dunbare91593e2008-08-11 04:54:23 +000041 if (size_reserve > 0) Types.reserve(size_reserve);
42 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
Douglas Gregor7a9cbed2009-04-26 03:57:37 +000044 BuiltinInfo.InitializeTargetBuiltins(Target);
Douglas Gregor2deaea32009-04-22 18:49:13 +000045 if (InitializeBuiltins)
46 this->InitializeBuiltins(idents);
Daniel Dunbare91593e2008-08-11 04:54:23 +000047}
48
Reid Spencer5f016e22007-07-11 17:01:13 +000049ASTContext::~ASTContext() {
50 // Deallocate all the types.
51 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000052 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000053 Types.pop_back();
54 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000055
Nuno Lopesb74668e2008-12-17 22:30:25 +000056 {
57 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
58 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
59 while (I != E) {
60 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
61 delete R;
62 }
63 }
64
65 {
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +000066 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
67 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopesb74668e2008-12-17 22:30:25 +000068 while (I != E) {
69 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
70 delete R;
71 }
72 }
73
Douglas Gregorab452ba2009-03-26 23:50:42 +000074 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000075 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
76 NNS = NestedNameSpecifiers.begin(),
77 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregore7dcd782009-03-27 23:25:45 +000078 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000079 /* Increment in loop */)
80 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000081
82 if (GlobalNestedNameSpecifier)
83 GlobalNestedNameSpecifier->Destroy(*this);
84
Eli Friedmanb26153c2008-05-27 03:08:09 +000085 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000086}
87
Douglas Gregor2deaea32009-04-22 18:49:13 +000088void ASTContext::InitializeBuiltins(IdentifierTable &idents) {
Douglas Gregor2deaea32009-04-22 18:49:13 +000089 BuiltinInfo.InitializeBuiltins(idents, LangOpts.NoBuiltin);
90}
91
Douglas Gregor2cf26342009-04-09 22:27:44 +000092void
93ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
94 ExternalSource.reset(Source.take());
95}
96
Reid Spencer5f016e22007-07-11 17:01:13 +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 Dunbar248e1c02008-09-26 03:23:00 +0000101 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000102 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
103 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
104
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000106 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
107 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000108 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000109 unsigned NumExtQual = 0;
110
Reid Spencer5f016e22007-07-11 17:01:13 +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 Dunbar248e1c02008-09-26 03:23:00 +0000117 else if (isa<BlockPointerType>(T))
118 ++NumBlockPointer;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000119 else if (isa<LValueReferenceType>(T))
120 ++NumLValueReference;
121 else if (isa<RValueReferenceType>(T))
122 ++NumRValueReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000123 else if (isa<MemberPointerType>(T))
124 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000125 else if (isa<ComplexType>(T))
126 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 else if (isa<ArrayType>(T))
128 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000129 else if (isa<VectorType>(T))
130 ++NumVector;
Douglas Gregor72564e72009-02-26 23:50:07 +0000131 else if (isa<FunctionNoProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 ++NumFunctionNP;
Douglas Gregor72564e72009-02-26 23:50:07 +0000133 else if (isa<FunctionProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 ++NumFunctionP;
135 else if (isa<TypedefType>(T))
136 ++NumTypeName;
137 else if (TagType *TT = dyn_cast<TagType>(T)) {
138 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000139 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 }
Ted Kremeneka526c5c2008-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 Naroff6cc18962008-05-21 15:59:22 +0000152 else if (isa<TypeOfType>(T))
153 ++NumTypeOfTypes;
Douglas Gregor72564e72009-02-26 23:50:07 +0000154 else if (isa<TypeOfExprType>(T))
155 ++NumTypeOfExprTypes;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000156 else if (isa<ExtQualType>(T))
157 ++NumExtQual;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000158 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000159 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +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 Dunbar248e1c02008-09-26 03:23:00 +0000166 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000167 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
168 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000169 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000170 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000172 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 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 Kremeneka526c5c2008-01-07 19:49:32 +0000181 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000182 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000183 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000184 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000185 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000186 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor72564e72009-02-26 23:50:07 +0000187 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000188 fprintf(stderr, " %d attribute-qualified types\n", NumExtQual);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000189
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
191 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000192 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000193 NumLValueReference*sizeof(LValueReferenceType)+
194 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000195 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000196 NumFunctionP*sizeof(FunctionProtoType)+
197 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000198 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000199 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)+
200 NumExtQual*sizeof(ExtQualType)));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000201
202 if (ExternalSource.get()) {
203 fprintf(stderr, "\n");
204 ExternalSource->PrintStats();
205 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000206}
207
208
209void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000210 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000211}
212
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner98be4942008-03-05 18:54:05 +0000222 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +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);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000244
Chris Lattner2df9ced2009-04-30 02:43:43 +0000245 // GNU extension, 128-bit integers.
246 InitBuiltinType(Int128Ty, BuiltinType::Int128);
247 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
248
Chris Lattner3a250322009-02-26 23:43:47 +0000249 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
250 InitBuiltinType(WCharTy, BuiltinType::WChar);
251 else // C99
252 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000253
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000254 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000255 InitBuiltinType(OverloadTy, BuiltinType::Overload);
256
257 // Placeholder type for type-dependent expressions whose type is
258 // completely unknown. No code should ever check a type against
259 // DependentTy and users should never see it; however, it is here to
260 // help diagnose failures to properly check for type-dependent
261 // expressions.
262 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000263
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 // C99 6.2.5p11.
265 FloatComplexTy = getComplexType(FloatTy);
266 DoubleComplexTy = getComplexType(DoubleTy);
267 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000268
Steve Naroff7e219e42007-10-15 14:41:52 +0000269 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000270 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000271 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000272 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000273 ClassStructType = 0;
274
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000275 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000276
277 // void * type
278 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000279
280 // nullptr type (C++0x 2.14.7)
281 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000282}
283
Chris Lattner464175b2007-07-18 17:52:12 +0000284//===----------------------------------------------------------------------===//
285// Type Sizing and Analysis
286//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000287
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000288/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
289/// scalar floating point type.
290const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
291 const BuiltinType *BT = T->getAsBuiltinType();
292 assert(BT && "Not a floating point type!");
293 switch (BT->getKind()) {
294 default: assert(0 && "Not a floating point type!");
295 case BuiltinType::Float: return Target.getFloatFormat();
296 case BuiltinType::Double: return Target.getDoubleFormat();
297 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
298 }
299}
300
Chris Lattneraf707ab2009-01-24 21:53:27 +0000301/// getDeclAlign - Return a conservative estimate of the alignment of the
302/// specified decl. Note that bitfields do not have a valid alignment, so
303/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000304unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000305 unsigned Align = Target.getCharWidth();
306
307 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
308 Align = std::max(Align, AA->getAlignment());
309
Chris Lattneraf707ab2009-01-24 21:53:27 +0000310 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
311 QualType T = VD->getType();
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000312 if (const ReferenceType* RT = T->getAsReferenceType()) {
313 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000314 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000315 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
316 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000317 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
318 T = cast<ArrayType>(T)->getElementType();
319
320 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
321 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000322 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000323
324 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000325}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000326
Chris Lattnera7674d82007-07-13 22:13:22 +0000327/// getTypeSize - Return the size of the specified type, in bits. This method
328/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000329std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000330ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000331 uint64_t Width=0;
332 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000333 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000334#define TYPE(Class, Base)
335#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000336#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000337#define DEPENDENT_TYPE(Class, Base) case Type::Class:
338#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000339 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000340 break;
341
Chris Lattner692233e2007-07-13 22:27:08 +0000342 case Type::FunctionNoProto:
343 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000344 // GCC extension: alignof(function) = 32 bits
345 Width = 0;
346 Align = 32;
347 break;
348
Douglas Gregor72564e72009-02-26 23:50:07 +0000349 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000350 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000351 Width = 0;
352 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
353 break;
354
Steve Narofffb22d962007-08-30 01:06:46 +0000355 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000356 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000357
Chris Lattner98be4942008-03-05 18:54:05 +0000358 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000359 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000360 Align = EltInfo.second;
361 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000362 }
Nate Begeman213541a2008-04-18 23:10:10 +0000363 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000364 case Type::Vector: {
365 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000366 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000367 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000368 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000369 // If the alignment is not a power of 2, round up to the next power of 2.
370 // This happens for non-power-of-2 length vectors.
371 // FIXME: this should probably be a target property.
372 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000373 break;
374 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000375
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000376 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000377 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000378 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000379 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000380 // GCC extension: alignof(void) = 8 bits.
381 Width = 0;
382 Align = 8;
383 break;
384
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000385 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000386 Width = Target.getBoolWidth();
387 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000388 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000389 case BuiltinType::Char_S:
390 case BuiltinType::Char_U:
391 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000392 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000393 Width = Target.getCharWidth();
394 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000395 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000396 case BuiltinType::WChar:
397 Width = Target.getWCharWidth();
398 Align = Target.getWCharAlign();
399 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000400 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000401 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000402 Width = Target.getShortWidth();
403 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000404 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000405 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000406 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000407 Width = Target.getIntWidth();
408 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000409 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000410 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000411 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000412 Width = Target.getLongWidth();
413 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000414 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000415 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000416 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000417 Width = Target.getLongLongWidth();
418 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000419 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000420 case BuiltinType::Int128:
421 case BuiltinType::UInt128:
422 Width = 128;
423 Align = 128; // int128_t is 128-bit aligned on all targets.
424 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000425 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000426 Width = Target.getFloatWidth();
427 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000428 break;
429 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000430 Width = Target.getDoubleWidth();
431 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000432 break;
433 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000434 Width = Target.getLongDoubleWidth();
435 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000436 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000437 case BuiltinType::NullPtr:
438 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
439 Align = Target.getPointerAlign(0); // == sizeof(void*)
Chris Lattnera7674d82007-07-13 22:13:22 +0000440 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000441 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000442 case Type::FixedWidthInt:
443 // FIXME: This isn't precisely correct; the width/alignment should depend
444 // on the available types for the target
445 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000446 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000447 Align = Width;
448 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000449 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000450 // FIXME: Pointers into different addr spaces could have different sizes and
451 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000452 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000453 case Type::ObjCQualifiedId:
Douglas Gregor72564e72009-02-26 23:50:07 +0000454 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000455 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000456 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000457 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000458 case Type::BlockPointer: {
459 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
460 Width = Target.getPointerWidth(AS);
461 Align = Target.getPointerAlign(AS);
462 break;
463 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000464 case Type::Pointer: {
465 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000466 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000467 Align = Target.getPointerAlign(AS);
468 break;
469 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000470 case Type::LValueReference:
471 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000472 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000473 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000474 // FIXME: This is wrong for struct layout: a reference in a struct has
475 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000476 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000477 case Type::MemberPointer: {
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000478 // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
479 // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
480 // If we ever want to support other ABIs this needs to be abstracted.
481
Sebastian Redlf30208a2009-01-24 21:16:55 +0000482 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000483 std::pair<uint64_t, unsigned> PtrDiffInfo =
484 getTypeInfo(getPointerDiffType());
485 Width = PtrDiffInfo.first;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000486 if (Pointee->isFunctionType())
487 Width *= 2;
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000488 Align = PtrDiffInfo.second;
489 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000490 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000491 case Type::Complex: {
492 // Complex types have the same alignment as their elements, but twice the
493 // size.
494 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000495 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000496 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000497 Align = EltInfo.second;
498 break;
499 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000500 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000501 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000502 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
503 Width = Layout.getSize();
504 Align = Layout.getAlignment();
505 break;
506 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000507 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000508 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000509 const TagType *TT = cast<TagType>(T);
510
511 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000512 Width = 1;
513 Align = 1;
514 break;
515 }
516
Daniel Dunbar1d751182008-11-08 05:48:37 +0000517 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000518 return getTypeInfo(ET->getDecl()->getIntegerType());
519
Daniel Dunbar1d751182008-11-08 05:48:37 +0000520 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000521 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
522 Width = Layout.getSize();
523 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000524 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000525 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000526
Douglas Gregor18857642009-04-30 17:32:17 +0000527 case Type::Typedef: {
528 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
529 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
530 Align = Aligned->getAlignment();
531 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
532 } else
533 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000534 break;
Chris Lattner71763312008-04-06 22:05:18 +0000535 }
Douglas Gregor18857642009-04-30 17:32:17 +0000536
537 case Type::TypeOfExpr:
538 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
539 .getTypePtr());
540
541 case Type::TypeOf:
542 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
543
544 case Type::QualifiedName:
545 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
546
547 case Type::TemplateSpecialization:
548 assert(getCanonicalType(T) != T &&
549 "Cannot request the size of a dependent type");
550 // FIXME: this is likely to be wrong once we support template
551 // aliases, since a template alias could refer to a typedef that
552 // has an __aligned__ attribute on it.
553 return getTypeInfo(getCanonicalType(T));
554 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000555
Chris Lattner464175b2007-07-18 17:52:12 +0000556 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000557 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000558}
559
Chris Lattner34ebde42009-01-27 18:08:34 +0000560/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
561/// type for the current target in bits. This can be different than the ABI
562/// alignment in cases where it is beneficial for performance to overalign
563/// a data type.
564unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
565 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman1eed6022009-05-25 21:27:19 +0000566
567 // Double and long long should be naturally aligned if possible.
568 if (const ComplexType* CT = T->getAsComplexType())
569 T = CT->getElementType().getTypePtr();
570 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
571 T->isSpecificBuiltinType(BuiltinType::LongLong))
572 return std::max(ABIAlign, (unsigned)getTypeSize(T));
573
Chris Lattner34ebde42009-01-27 18:08:34 +0000574 return ABIAlign;
575}
576
577
Devang Patel8b277042008-06-04 21:22:16 +0000578/// LayoutField - Field layout.
579void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000580 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000581 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000582 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000583 uint64_t FieldOffset = IsUnion ? 0 : Size;
584 uint64_t FieldSize;
585 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000586
587 // FIXME: Should this override struct packing? Probably we want to
588 // take the minimum?
589 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
590 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000591
592 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
593 // TODO: Need to check this algorithm on other targets!
594 // (tested on Linux-X86)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000595 FieldSize = BitWidthExpr->EvaluateAsInt(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000596
597 std::pair<uint64_t, unsigned> FieldInfo =
598 Context.getTypeInfo(FD->getType());
599 uint64_t TypeSize = FieldInfo.first;
600
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000601 // Determine the alignment of this bitfield. The packing
602 // attributes define a maximum and the alignment attribute defines
603 // a minimum.
604 // FIXME: What is the right behavior when the specified alignment
605 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000606 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000607 if (FieldPacking)
608 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000609 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
610 FieldAlign = std::max(FieldAlign, AA->getAlignment());
611
612 // Check if we need to add padding to give the field the correct
613 // alignment.
614 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
615 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
616
617 // Padding members don't affect overall alignment
618 if (!FD->getIdentifier())
619 FieldAlign = 1;
620 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000621 if (FD->getType()->isIncompleteArrayType()) {
622 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000623 // query getTypeInfo about these, so we figure it out here.
624 // Flexible array members don't have any size, but they
625 // have to be aligned appropriately for their element type.
626 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000627 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000628 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson2f1169f2009-04-10 05:31:15 +0000629 } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
630 unsigned AS = RT->getPointeeType().getAddressSpace();
631 FieldSize = Context.Target.getPointerWidth(AS);
632 FieldAlign = Context.Target.getPointerAlign(AS);
Devang Patel8b277042008-06-04 21:22:16 +0000633 } else {
634 std::pair<uint64_t, unsigned> FieldInfo =
635 Context.getTypeInfo(FD->getType());
636 FieldSize = FieldInfo.first;
637 FieldAlign = FieldInfo.second;
638 }
639
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000640 // Determine the alignment of this bitfield. The packing
641 // attributes define a maximum and the alignment attribute defines
642 // a minimum. Additionally, the packing alignment must be at least
643 // a byte for non-bitfields.
644 //
645 // FIXME: What is the right behavior when the specified alignment
646 // is smaller than the specified packing?
647 if (FieldPacking)
648 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000649 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
650 FieldAlign = std::max(FieldAlign, AA->getAlignment());
651
652 // Round up the current record size to the field's alignment boundary.
653 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
654 }
655
656 // Place this field at the current location.
657 FieldOffsets[FieldNo] = FieldOffset;
658
659 // Reserve space for this field.
660 if (IsUnion) {
661 Size = std::max(Size, FieldSize);
662 } else {
663 Size = FieldOffset + FieldSize;
664 }
665
Daniel Dunbard6884a02009-05-04 05:16:21 +0000666 // Remember the next available offset.
667 NextOffset = Size;
668
Devang Patel8b277042008-06-04 21:22:16 +0000669 // Remember max struct/class alignment.
670 Alignment = std::max(Alignment, FieldAlign);
671}
672
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000673static void CollectLocalObjCIvars(ASTContext *Ctx,
674 const ObjCInterfaceDecl *OI,
675 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000676 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
677 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000678 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000679 if (!IVDecl->isInvalidDecl())
680 Fields.push_back(cast<FieldDecl>(IVDecl));
681 }
682}
683
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000684void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
685 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
686 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
687 CollectObjCIvars(SuperClass, Fields);
688 CollectLocalObjCIvars(this, OI, Fields);
689}
690
Fariborz Jahanian98200742009-05-12 18:14:29 +0000691void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
692 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
693 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(*this),
694 E = PD->prop_end(*this); I != E; ++I)
695 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
696 Ivars.push_back(Ivar);
697
698 // Also look into nested protocols.
699 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
700 E = PD->protocol_end(); P != E; ++P)
701 CollectProtocolSynthesizedIvars(*P, Ivars);
702}
703
704/// CollectSynthesizedIvars -
705/// This routine collect synthesized ivars for the designated class.
706///
707void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
708 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
709 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*this),
710 E = OI->prop_end(*this); I != E; ++I) {
711 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
712 Ivars.push_back(Ivar);
713 }
714 // Also look into interface's protocol list for properties declared
715 // in the protocol and whose ivars are synthesized.
716 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
717 PE = OI->protocol_end(); P != PE; ++P) {
718 ObjCProtocolDecl *PD = (*P);
719 CollectProtocolSynthesizedIvars(PD, Ivars);
720 }
721}
722
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000723/// getInterfaceLayoutImpl - Get or compute information about the
724/// layout of the given interface.
725///
726/// \param Impl - If given, also include the layout of the interface's
727/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000728const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000729ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
730 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000731 assert(!D->isForwardDecl() && "Invalid interface decl!");
732
Devang Patel44a3dde2008-06-04 21:54:36 +0000733 // Look up this layout, if already laid out, return what we have.
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000734 ObjCContainerDecl *Key =
735 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
736 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
737 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000738
Daniel Dunbar453addb2009-05-03 11:16:44 +0000739 unsigned FieldCount = D->ivar_size();
740 // Add in synthesized ivar count if laying out an implementation.
741 if (Impl) {
Fariborz Jahanian98200742009-05-12 18:14:29 +0000742 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
743 CollectSynthesizedIvars(D, Ivars);
744 FieldCount += Ivars.size();
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000745 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +0000746 // entry. Note we can't cache this because we simply free all
747 // entries later; however we shouldn't look up implementations
748 // frequently.
749 if (FieldCount == D->ivar_size())
750 return getObjCLayout(D, 0);
751 }
752
Devang Patel6a5a34c2008-06-06 02:14:01 +0000753 ASTRecordLayout *NewEntry = NULL;
Devang Patel6a5a34c2008-06-06 02:14:01 +0000754 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Devang Patel6a5a34c2008-06-06 02:14:01 +0000755 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
756 unsigned Alignment = SL.getAlignment();
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000757
Daniel Dunbar913af352009-05-07 21:58:26 +0000758 // We start laying out ivars not at the end of the superclass
759 // structure, but at the next byte following the last field.
760 uint64_t Size = llvm::RoundUpToAlignment(SL.NextOffset, 8);
Daniel Dunbard6884a02009-05-04 05:16:21 +0000761
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000762 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout(Size, Alignment);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000763 NewEntry->InitializeLayout(FieldCount);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000764 } else {
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000765 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout();
Devang Patel6a5a34c2008-06-06 02:14:01 +0000766 NewEntry->InitializeLayout(FieldCount);
767 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000768
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000769 unsigned StructPacking = 0;
770 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
771 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000772
773 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
774 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
775 AA->getAlignment()));
776
777 // Layout each ivar sequentially.
778 unsigned i = 0;
779 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
780 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
781 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000782 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000783 }
Daniel Dunbar453addb2009-05-03 11:16:44 +0000784 // And synthesized ivars, if this is an implementation.
785 if (Impl) {
Fariborz Jahanian98200742009-05-12 18:14:29 +0000786 // FIXME. Do we need to colltect twice?
787 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
788 CollectSynthesizedIvars(D, Ivars);
789 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
790 NewEntry->LayoutField(Ivars[k], i++, false, StructPacking, *this);
Fariborz Jahanian18191882009-03-31 18:11:23 +0000791 }
Fariborz Jahanian99eee362009-04-01 19:37:34 +0000792
Devang Patel44a3dde2008-06-04 21:54:36 +0000793 // Finally, round the size of the total struct up to the alignment of the
794 // struct itself.
795 NewEntry->FinalizeLayout();
796 return *NewEntry;
797}
798
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000799const ASTRecordLayout &
800ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
801 return getObjCLayout(D, 0);
802}
803
804const ASTRecordLayout &
805ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
806 return getObjCLayout(D->getClassInterface(), D);
807}
808
Devang Patel88a981b2007-11-01 19:11:01 +0000809/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000810/// specified record (struct/union/class), which indicates its size and field
811/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000812const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000813 D = D->getDefinition(*this);
814 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000815
Chris Lattner464175b2007-07-18 17:52:12 +0000816 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000817 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000818 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000819
Devang Patel88a981b2007-11-01 19:11:01 +0000820 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
821 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
822 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000823 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000824
Douglas Gregore267ff32008-12-11 20:41:00 +0000825 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000826 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
827 D->field_end(*this)));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000828 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000829
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000830 unsigned StructPacking = 0;
831 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
832 StructPacking = PA->getAlignment();
833
Eli Friedman4bd998b2008-05-30 09:31:38 +0000834 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000835 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
836 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000837
Eli Friedman4bd998b2008-05-30 09:31:38 +0000838 // Layout each field, for now, just sequentially, respecting alignment. In
839 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000840 unsigned FieldIdx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000841 for (RecordDecl::field_iterator Field = D->field_begin(*this),
842 FieldEnd = D->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000843 Field != FieldEnd; (void)++Field, ++FieldIdx)
844 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000845
846 // Finally, round the size of the total struct up to the alignment of the
847 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000848 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000849 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000850}
851
Chris Lattnera7674d82007-07-13 22:13:22 +0000852//===----------------------------------------------------------------------===//
853// Type creation/memoization methods
854//===----------------------------------------------------------------------===//
855
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000856QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000857 QualType CanT = getCanonicalType(T);
858 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000859 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000860
861 // If we are composing extended qualifiers together, merge together into one
862 // ExtQualType node.
863 unsigned CVRQuals = T.getCVRQualifiers();
864 QualType::GCAttrTypes GCAttr = QualType::GCNone;
865 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000866
Chris Lattnerb7d25532009-02-18 22:53:11 +0000867 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
868 // If this type already has an address space specified, it cannot get
869 // another one.
870 assert(EQT->getAddressSpace() == 0 &&
871 "Type cannot be in multiple addr spaces!");
872 GCAttr = EQT->getObjCGCAttr();
873 TypeNode = EQT->getBaseType();
874 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000875
Chris Lattnerb7d25532009-02-18 22:53:11 +0000876 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000877 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000878 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000879 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000880 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000881 return QualType(EXTQy, CVRQuals);
882
Christopher Lambebb97e92008-02-04 02:31:56 +0000883 // If the base type isn't canonical, this won't be a canonical type either,
884 // so fill in the canonical type field.
885 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000886 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000887 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000888
Chris Lattnerb7d25532009-02-18 22:53:11 +0000889 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000890 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000891 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000892 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000893 ExtQualType *New =
894 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000895 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000896 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000897 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000898}
899
Chris Lattnerb7d25532009-02-18 22:53:11 +0000900QualType ASTContext::getObjCGCQualType(QualType T,
901 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000902 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000903 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000904 return T;
905
Chris Lattnerb7d25532009-02-18 22:53:11 +0000906 // If we are composing extended qualifiers together, merge together into one
907 // ExtQualType node.
908 unsigned CVRQuals = T.getCVRQualifiers();
909 Type *TypeNode = T.getTypePtr();
910 unsigned AddressSpace = 0;
911
912 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
913 // If this type already has an address space specified, it cannot get
914 // another one.
915 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
916 "Type cannot be in multiple addr spaces!");
917 AddressSpace = EQT->getAddressSpace();
918 TypeNode = EQT->getBaseType();
919 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000920
921 // Check if we've already instantiated an gc qual'd type of this type.
922 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000923 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000924 void *InsertPos = 0;
925 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000926 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000927
928 // If the base type isn't canonical, this won't be a canonical type either,
929 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000930 // FIXME: Isn't this also not canonical if the base type is a array
931 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000932 QualType Canonical;
933 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000934 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000935
Chris Lattnerb7d25532009-02-18 22:53:11 +0000936 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000937 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
938 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
939 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000940 ExtQualType *New =
941 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000942 ExtQualTypes.InsertNode(New, InsertPos);
943 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000944 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000945}
Chris Lattnera7674d82007-07-13 22:13:22 +0000946
Reid Spencer5f016e22007-07-11 17:01:13 +0000947/// getComplexType - Return the uniqued reference to the type for a complex
948/// number with the specified element type.
949QualType ASTContext::getComplexType(QualType T) {
950 // Unique pointers, to guarantee there is only one pointer of a particular
951 // structure.
952 llvm::FoldingSetNodeID ID;
953 ComplexType::Profile(ID, T);
954
955 void *InsertPos = 0;
956 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
957 return QualType(CT, 0);
958
959 // If the pointee type isn't canonical, this won't be a canonical type either,
960 // so fill in the canonical type field.
961 QualType Canonical;
962 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000963 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000964
965 // Get the new insert position for the node we care about.
966 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000967 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000968 }
Steve Narofff83820b2009-01-27 22:08:43 +0000969 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000970 Types.push_back(New);
971 ComplexTypes.InsertNode(New, InsertPos);
972 return QualType(New, 0);
973}
974
Eli Friedmanf98aba32009-02-13 02:31:07 +0000975QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
976 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
977 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
978 FixedWidthIntType *&Entry = Map[Width];
979 if (!Entry)
980 Entry = new FixedWidthIntType(Width, Signed);
981 return QualType(Entry, 0);
982}
Reid Spencer5f016e22007-07-11 17:01:13 +0000983
984/// getPointerType - Return the uniqued reference to the type for a pointer to
985/// the specified type.
986QualType ASTContext::getPointerType(QualType T) {
987 // Unique pointers, to guarantee there is only one pointer of a particular
988 // structure.
989 llvm::FoldingSetNodeID ID;
990 PointerType::Profile(ID, T);
991
992 void *InsertPos = 0;
993 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
994 return QualType(PT, 0);
995
996 // If the pointee type isn't canonical, this won't be a canonical type either,
997 // so fill in the canonical type field.
998 QualType Canonical;
999 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001000 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +00001001
1002 // Get the new insert position for the node we care about.
1003 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001004 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 }
Steve Narofff83820b2009-01-27 22:08:43 +00001006 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001007 Types.push_back(New);
1008 PointerTypes.InsertNode(New, InsertPos);
1009 return QualType(New, 0);
1010}
1011
Steve Naroff5618bd42008-08-27 16:04:49 +00001012/// getBlockPointerType - Return the uniqued reference to the type for
1013/// a pointer to the specified block.
1014QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +00001015 assert(T->isFunctionType() && "block of function types only");
1016 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +00001017 // structure.
1018 llvm::FoldingSetNodeID ID;
1019 BlockPointerType::Profile(ID, T);
1020
1021 void *InsertPos = 0;
1022 if (BlockPointerType *PT =
1023 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1024 return QualType(PT, 0);
1025
Steve Naroff296e8d52008-08-28 19:20:44 +00001026 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +00001027 // type either so fill in the canonical type field.
1028 QualType Canonical;
1029 if (!T->isCanonical()) {
1030 Canonical = getBlockPointerType(getCanonicalType(T));
1031
1032 // Get the new insert position for the node we care about.
1033 BlockPointerType *NewIP =
1034 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001035 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001036 }
Steve Narofff83820b2009-01-27 22:08:43 +00001037 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001038 Types.push_back(New);
1039 BlockPointerTypes.InsertNode(New, InsertPos);
1040 return QualType(New, 0);
1041}
1042
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001043/// getLValueReferenceType - Return the uniqued reference to the type for an
1044/// lvalue reference to the specified type.
1045QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001046 // Unique pointers, to guarantee there is only one pointer of a particular
1047 // structure.
1048 llvm::FoldingSetNodeID ID;
1049 ReferenceType::Profile(ID, T);
1050
1051 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001052 if (LValueReferenceType *RT =
1053 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001054 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001055
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 // If the referencee type isn't canonical, this won't be a canonical type
1057 // either, so fill in the canonical type field.
1058 QualType Canonical;
1059 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001060 Canonical = getLValueReferenceType(getCanonicalType(T));
1061
Reid Spencer5f016e22007-07-11 17:01:13 +00001062 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001063 LValueReferenceType *NewIP =
1064 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001065 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 }
1067
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001068 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001070 LValueReferenceTypes.InsertNode(New, InsertPos);
1071 return QualType(New, 0);
1072}
1073
1074/// getRValueReferenceType - Return the uniqued reference to the type for an
1075/// rvalue reference to the specified type.
1076QualType ASTContext::getRValueReferenceType(QualType T) {
1077 // Unique pointers, to guarantee there is only one pointer of a particular
1078 // structure.
1079 llvm::FoldingSetNodeID ID;
1080 ReferenceType::Profile(ID, T);
1081
1082 void *InsertPos = 0;
1083 if (RValueReferenceType *RT =
1084 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1085 return QualType(RT, 0);
1086
1087 // If the referencee type isn't canonical, this won't be a canonical type
1088 // either, so fill in the canonical type field.
1089 QualType Canonical;
1090 if (!T->isCanonical()) {
1091 Canonical = getRValueReferenceType(getCanonicalType(T));
1092
1093 // Get the new insert position for the node we care about.
1094 RValueReferenceType *NewIP =
1095 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1096 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1097 }
1098
1099 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1100 Types.push_back(New);
1101 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001102 return QualType(New, 0);
1103}
1104
Sebastian Redlf30208a2009-01-24 21:16:55 +00001105/// getMemberPointerType - Return the uniqued reference to the type for a
1106/// member pointer to the specified type, in the specified class.
1107QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1108{
1109 // Unique pointers, to guarantee there is only one pointer of a particular
1110 // structure.
1111 llvm::FoldingSetNodeID ID;
1112 MemberPointerType::Profile(ID, T, Cls);
1113
1114 void *InsertPos = 0;
1115 if (MemberPointerType *PT =
1116 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1117 return QualType(PT, 0);
1118
1119 // If the pointee or class type isn't canonical, this won't be a canonical
1120 // type either, so fill in the canonical type field.
1121 QualType Canonical;
1122 if (!T->isCanonical()) {
1123 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1124
1125 // Get the new insert position for the node we care about.
1126 MemberPointerType *NewIP =
1127 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1128 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1129 }
Steve Narofff83820b2009-01-27 22:08:43 +00001130 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001131 Types.push_back(New);
1132 MemberPointerTypes.InsertNode(New, InsertPos);
1133 return QualType(New, 0);
1134}
1135
Steve Narofffb22d962007-08-30 01:06:46 +00001136/// getConstantArrayType - Return the unique reference to the type for an
1137/// array of the specified element type.
1138QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001139 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001140 ArrayType::ArraySizeModifier ASM,
1141 unsigned EltTypeQuals) {
Chris Lattner38aeec72009-05-13 04:12:56 +00001142 // Convert the array size into a canonical width matching the pointer size for
1143 // the target.
1144 llvm::APInt ArySize(ArySizeIn);
1145 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1146
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001148 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001149
1150 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001151 if (ConstantArrayType *ATP =
1152 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 return QualType(ATP, 0);
1154
1155 // If the element type isn't canonical, this won't be a canonical type either,
1156 // so fill in the canonical type field.
1157 QualType Canonical;
1158 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001159 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001160 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001161 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001162 ConstantArrayType *NewIP =
1163 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001164 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001165 }
1166
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001167 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001168 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001169 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001170 Types.push_back(New);
1171 return QualType(New, 0);
1172}
1173
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001174/// getVariableArrayType - Returns a non-unique reference to the type for a
1175/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001176QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1177 ArrayType::ArraySizeModifier ASM,
1178 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001179 // Since we don't unique expressions, it isn't possible to unique VLA's
1180 // that have an expression provided for their size.
1181
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001182 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001183 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001184
1185 VariableArrayTypes.push_back(New);
1186 Types.push_back(New);
1187 return QualType(New, 0);
1188}
1189
Douglas Gregor898574e2008-12-05 23:32:09 +00001190/// getDependentSizedArrayType - Returns a non-unique reference to
1191/// the type for a dependently-sized array of the specified element
1192/// type. FIXME: We will need these to be uniqued, or at least
1193/// comparable, at some point.
1194QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1195 ArrayType::ArraySizeModifier ASM,
1196 unsigned EltTypeQuals) {
1197 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1198 "Size must be type- or value-dependent!");
1199
1200 // Since we don't unique expressions, it isn't possible to unique
1201 // dependently-sized array types.
1202
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001203 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001204 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1205 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001206
1207 DependentSizedArrayTypes.push_back(New);
1208 Types.push_back(New);
1209 return QualType(New, 0);
1210}
1211
Eli Friedmanc5773c42008-02-15 18:16:39 +00001212QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1213 ArrayType::ArraySizeModifier ASM,
1214 unsigned EltTypeQuals) {
1215 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001216 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001217
1218 void *InsertPos = 0;
1219 if (IncompleteArrayType *ATP =
1220 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1221 return QualType(ATP, 0);
1222
1223 // If the element type isn't canonical, this won't be a canonical type
1224 // either, so fill in the canonical type field.
1225 QualType Canonical;
1226
1227 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001228 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001229 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001230
1231 // Get the new insert position for the node we care about.
1232 IncompleteArrayType *NewIP =
1233 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001234 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001235 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001236
Steve Narofff83820b2009-01-27 22:08:43 +00001237 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001238 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001239
1240 IncompleteArrayTypes.InsertNode(New, InsertPos);
1241 Types.push_back(New);
1242 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001243}
1244
Steve Naroff73322922007-07-18 18:00:27 +00001245/// getVectorType - Return the unique reference to a vector type of
1246/// the specified element type and size. VectorType must be a built-in type.
1247QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 BuiltinType *baseType;
1249
Chris Lattnerf52ab252008-04-06 22:59:24 +00001250 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001251 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001252
1253 // Check if we've already instantiated a vector of this type.
1254 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001255 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 void *InsertPos = 0;
1257 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1258 return QualType(VTP, 0);
1259
1260 // If the element type isn't canonical, this won't be a canonical type either,
1261 // so fill in the canonical type field.
1262 QualType Canonical;
1263 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001264 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001265
1266 // Get the new insert position for the node we care about.
1267 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001268 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 }
Steve Narofff83820b2009-01-27 22:08:43 +00001270 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 VectorTypes.InsertNode(New, InsertPos);
1272 Types.push_back(New);
1273 return QualType(New, 0);
1274}
1275
Nate Begeman213541a2008-04-18 23:10:10 +00001276/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001277/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001278QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001279 BuiltinType *baseType;
1280
Chris Lattnerf52ab252008-04-06 22:59:24 +00001281 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001282 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001283
1284 // Check if we've already instantiated a vector of this type.
1285 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001286 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001287 void *InsertPos = 0;
1288 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1289 return QualType(VTP, 0);
1290
1291 // If the element type isn't canonical, this won't be a canonical type either,
1292 // so fill in the canonical type field.
1293 QualType Canonical;
1294 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001295 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001296
1297 // Get the new insert position for the node we care about.
1298 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001299 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001300 }
Steve Narofff83820b2009-01-27 22:08:43 +00001301 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001302 VectorTypes.InsertNode(New, InsertPos);
1303 Types.push_back(New);
1304 return QualType(New, 0);
1305}
1306
Douglas Gregor72564e72009-02-26 23:50:07 +00001307/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001308///
Douglas Gregor72564e72009-02-26 23:50:07 +00001309QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001310 // Unique functions, to guarantee there is only one function of a particular
1311 // structure.
1312 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001313 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001314
1315 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001316 if (FunctionNoProtoType *FT =
1317 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001318 return QualType(FT, 0);
1319
1320 QualType Canonical;
1321 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001322 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001323
1324 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001325 FunctionNoProtoType *NewIP =
1326 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001327 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001328 }
1329
Douglas Gregor72564e72009-02-26 23:50:07 +00001330 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001331 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001332 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001333 return QualType(New, 0);
1334}
1335
1336/// getFunctionType - Return a normal function type with a typed argument
1337/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001338QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001339 unsigned NumArgs, bool isVariadic,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001340 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001341 // Unique functions, to guarantee there is only one function of a particular
1342 // structure.
1343 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001344 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001345 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001346
1347 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001348 if (FunctionProtoType *FTP =
1349 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001350 return QualType(FTP, 0);
1351
1352 // Determine whether the type being created is already canonical or not.
1353 bool isCanonical = ResultTy->isCanonical();
1354 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1355 if (!ArgArray[i]->isCanonical())
1356 isCanonical = false;
1357
1358 // If this type isn't canonical, get the canonical version of it.
1359 QualType Canonical;
1360 if (!isCanonical) {
1361 llvm::SmallVector<QualType, 16> CanonicalArgs;
1362 CanonicalArgs.reserve(NumArgs);
1363 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001364 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001365
Chris Lattnerf52ab252008-04-06 22:59:24 +00001366 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001367 CanonicalArgs.data(), NumArgs,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001368 isVariadic, TypeQuals);
1369
Reid Spencer5f016e22007-07-11 17:01:13 +00001370 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001371 FunctionProtoType *NewIP =
1372 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001373 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001374 }
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001375
Douglas Gregor72564e72009-02-26 23:50:07 +00001376 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001377 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001378 FunctionProtoType *FTP =
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001379 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1380 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001381 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001382 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001383 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001384 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001385 return QualType(FTP, 0);
1386}
1387
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001388/// getTypeDeclType - Return the unique reference to the type for the
1389/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001390QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001391 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001392 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1393
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001394 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001395 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001396 else if (isa<TemplateTypeParmDecl>(Decl)) {
1397 assert(false && "Template type parameter types are always available.");
1398 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001399 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001400
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001401 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001402 if (PrevDecl)
1403 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001404 else
1405 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001406 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001407 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1408 if (PrevDecl)
1409 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001410 else
1411 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001412 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001413 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001414 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001415
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001416 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001417 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001418}
1419
Reid Spencer5f016e22007-07-11 17:01:13 +00001420/// getTypedefType - Return the unique reference to the type for the
1421/// specified typename decl.
1422QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1423 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1424
Chris Lattnerf52ab252008-04-06 22:59:24 +00001425 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001426 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001427 Types.push_back(Decl->TypeForDecl);
1428 return QualType(Decl->TypeForDecl, 0);
1429}
1430
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001431/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001432/// specified ObjC interface decl.
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001433QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001434 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1435
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001436 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1437 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff3536b442007-09-06 21:24:23 +00001438 Types.push_back(Decl->TypeForDecl);
1439 return QualType(Decl->TypeForDecl, 0);
1440}
1441
Douglas Gregorfab9d672009-02-05 23:33:38 +00001442/// \brief Retrieve the template type parameter type for a template
1443/// parameter with the given depth, index, and (optionally) name.
1444QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1445 IdentifierInfo *Name) {
1446 llvm::FoldingSetNodeID ID;
1447 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1448 void *InsertPos = 0;
1449 TemplateTypeParmType *TypeParm
1450 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1451
1452 if (TypeParm)
1453 return QualType(TypeParm, 0);
1454
1455 if (Name)
1456 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1457 getTemplateTypeParmType(Depth, Index));
1458 else
1459 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1460
1461 Types.push_back(TypeParm);
1462 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1463
1464 return QualType(TypeParm, 0);
1465}
1466
Douglas Gregor55f6b142009-02-09 18:46:07 +00001467QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001468ASTContext::getTemplateSpecializationType(TemplateName Template,
1469 const TemplateArgument *Args,
1470 unsigned NumArgs,
1471 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001472 if (!Canon.isNull())
1473 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001474
Douglas Gregor55f6b142009-02-09 18:46:07 +00001475 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001476 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001477
Douglas Gregor55f6b142009-02-09 18:46:07 +00001478 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001479 TemplateSpecializationType *Spec
1480 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001481
1482 if (Spec)
1483 return QualType(Spec, 0);
1484
Douglas Gregor7532dc62009-03-30 22:58:21 +00001485 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001486 sizeof(TemplateArgument) * NumArgs),
1487 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001488 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001489 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001490 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001491
1492 return QualType(Spec, 0);
1493}
1494
Douglas Gregore4e5b052009-03-19 00:18:19 +00001495QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001496ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001497 QualType NamedType) {
1498 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001499 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001500
1501 void *InsertPos = 0;
1502 QualifiedNameType *T
1503 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1504 if (T)
1505 return QualType(T, 0);
1506
Douglas Gregorab452ba2009-03-26 23:50:42 +00001507 T = new (*this) QualifiedNameType(NNS, NamedType,
1508 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001509 Types.push_back(T);
1510 QualifiedNameTypes.InsertNode(T, InsertPos);
1511 return QualType(T, 0);
1512}
1513
Douglas Gregord57959a2009-03-27 23:10:48 +00001514QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1515 const IdentifierInfo *Name,
1516 QualType Canon) {
1517 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1518
1519 if (Canon.isNull()) {
1520 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1521 if (CanonNNS != NNS)
1522 Canon = getTypenameType(CanonNNS, Name);
1523 }
1524
1525 llvm::FoldingSetNodeID ID;
1526 TypenameType::Profile(ID, NNS, Name);
1527
1528 void *InsertPos = 0;
1529 TypenameType *T
1530 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1531 if (T)
1532 return QualType(T, 0);
1533
1534 T = new (*this) TypenameType(NNS, Name, Canon);
1535 Types.push_back(T);
1536 TypenameTypes.InsertNode(T, InsertPos);
1537 return QualType(T, 0);
1538}
1539
Douglas Gregor17343172009-04-01 00:28:59 +00001540QualType
1541ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1542 const TemplateSpecializationType *TemplateId,
1543 QualType Canon) {
1544 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1545
1546 if (Canon.isNull()) {
1547 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1548 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1549 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1550 const TemplateSpecializationType *CanonTemplateId
1551 = CanonType->getAsTemplateSpecializationType();
1552 assert(CanonTemplateId &&
1553 "Canonical type must also be a template specialization type");
1554 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1555 }
1556 }
1557
1558 llvm::FoldingSetNodeID ID;
1559 TypenameType::Profile(ID, NNS, TemplateId);
1560
1561 void *InsertPos = 0;
1562 TypenameType *T
1563 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1564 if (T)
1565 return QualType(T, 0);
1566
1567 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1568 Types.push_back(T);
1569 TypenameTypes.InsertNode(T, InsertPos);
1570 return QualType(T, 0);
1571}
1572
Chris Lattner88cb27a2008-04-07 04:56:42 +00001573/// CmpProtocolNames - Comparison predicate for sorting protocols
1574/// alphabetically.
1575static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1576 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001577 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001578}
1579
1580static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1581 unsigned &NumProtocols) {
1582 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1583
1584 // Sort protocols, keyed by name.
1585 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1586
1587 // Remove duplicates.
1588 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1589 NumProtocols = ProtocolsEnd-Protocols;
1590}
1591
1592
Chris Lattner065f0d72008-04-07 04:44:08 +00001593/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1594/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001595QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1596 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001597 // Sort the protocol list alphabetically to canonicalize it.
1598 SortAndUniqueProtocols(Protocols, NumProtocols);
1599
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001600 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001601 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001602
1603 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001604 if (ObjCQualifiedInterfaceType *QT =
1605 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001606 return QualType(QT, 0);
1607
1608 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001609 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001610 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001611
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001612 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001613 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001614 return QualType(QType, 0);
1615}
1616
Chris Lattner88cb27a2008-04-07 04:56:42 +00001617/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1618/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001619QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001620 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001621 // Sort the protocol list alphabetically to canonicalize it.
1622 SortAndUniqueProtocols(Protocols, NumProtocols);
1623
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001624 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001625 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001626
1627 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001628 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001629 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001630 return QualType(QT, 0);
1631
1632 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001633 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001634 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001635 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001636 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001637 return QualType(QType, 0);
1638}
1639
Douglas Gregor72564e72009-02-26 23:50:07 +00001640/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1641/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001642/// multiple declarations that refer to "typeof(x)" all contain different
1643/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1644/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001645QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001646 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001647 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001648 Types.push_back(toe);
1649 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001650}
1651
Steve Naroff9752f252007-08-01 18:02:17 +00001652/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1653/// TypeOfType AST's. The only motivation to unique these nodes would be
1654/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1655/// an issue. This doesn't effect the type checker, since it operates
1656/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001657QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001658 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001659 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001660 Types.push_back(tot);
1661 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001662}
1663
Reid Spencer5f016e22007-07-11 17:01:13 +00001664/// getTagDeclType - Return the unique reference to the type for the
1665/// specified TagDecl (struct/union/class/enum) decl.
1666QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001667 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001668 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001669}
1670
1671/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1672/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1673/// needs to agree with the definition in <stddef.h>.
1674QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001675 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001676}
1677
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001678/// getSignedWCharType - Return the type of "signed wchar_t".
1679/// Used when in C++, as a GCC extension.
1680QualType ASTContext::getSignedWCharType() const {
1681 // FIXME: derive from "Target" ?
1682 return WCharTy;
1683}
1684
1685/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1686/// Used when in C++, as a GCC extension.
1687QualType ASTContext::getUnsignedWCharType() const {
1688 // FIXME: derive from "Target" ?
1689 return UnsignedIntTy;
1690}
1691
Chris Lattner8b9023b2007-07-13 03:05:23 +00001692/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1693/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1694QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001695 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001696}
1697
Chris Lattnere6327742008-04-02 05:18:44 +00001698//===----------------------------------------------------------------------===//
1699// Type Operators
1700//===----------------------------------------------------------------------===//
1701
Chris Lattner77c96472008-04-06 22:41:35 +00001702/// getCanonicalType - Return the canonical (structural) type corresponding to
1703/// the specified potentially non-canonical type. The non-canonical version
1704/// of a type may have many "decorated" versions of types. Decorators can
1705/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1706/// to be free of any of these, allowing two canonical types to be compared
1707/// for exact equality with a simple pointer comparison.
1708QualType ASTContext::getCanonicalType(QualType T) {
1709 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001710
1711 // If the result has type qualifiers, make sure to canonicalize them as well.
1712 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1713 if (TypeQuals == 0) return CanType;
1714
1715 // If the type qualifiers are on an array type, get the canonical type of the
1716 // array with the qualifiers applied to the element type.
1717 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1718 if (!AT)
1719 return CanType.getQualifiedType(TypeQuals);
1720
1721 // Get the canonical version of the element with the extra qualifiers on it.
1722 // This can recursively sink qualifiers through multiple levels of arrays.
1723 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1724 NewEltTy = getCanonicalType(NewEltTy);
1725
1726 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1727 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1728 CAT->getIndexTypeQualifier());
1729 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1730 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1731 IAT->getIndexTypeQualifier());
1732
Douglas Gregor898574e2008-12-05 23:32:09 +00001733 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1734 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1735 DSAT->getSizeModifier(),
1736 DSAT->getIndexTypeQualifier());
1737
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001738 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1739 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1740 VAT->getSizeModifier(),
1741 VAT->getIndexTypeQualifier());
1742}
1743
Douglas Gregor7da97d02009-05-10 22:57:19 +00001744Decl *ASTContext::getCanonicalDecl(Decl *D) {
Douglas Gregorc4ccf012009-05-10 22:59:12 +00001745 if (!D)
1746 return 0;
1747
Douglas Gregor7da97d02009-05-10 22:57:19 +00001748 if (TagDecl *Tag = dyn_cast<TagDecl>(D)) {
1749 QualType T = getTagDeclType(Tag);
1750 return cast<TagDecl>(cast<TagType>(T.getTypePtr()->CanonicalType)
1751 ->getDecl());
1752 }
1753
1754 if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(D)) {
1755 while (Template->getPreviousDeclaration())
1756 Template = Template->getPreviousDeclaration();
1757 return Template;
1758 }
1759
1760 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
1761 while (Function->getPreviousDeclaration())
1762 Function = Function->getPreviousDeclaration();
1763 return const_cast<FunctionDecl *>(Function);
1764 }
1765
1766 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
1767 while (Var->getPreviousDeclaration())
1768 Var = Var->getPreviousDeclaration();
1769 return const_cast<VarDecl *>(Var);
1770 }
1771
1772 return D;
1773}
1774
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001775TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
1776 // If this template name refers to a template, the canonical
1777 // template name merely stores the template itself.
1778 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Douglas Gregor7da97d02009-05-10 22:57:19 +00001779 return TemplateName(cast<TemplateDecl>(getCanonicalDecl(Template)));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001780
1781 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
1782 assert(DTN && "Non-dependent template names must refer to template decls.");
1783 return DTN->CanonicalTemplateName;
1784}
1785
Douglas Gregord57959a2009-03-27 23:10:48 +00001786NestedNameSpecifier *
1787ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1788 if (!NNS)
1789 return 0;
1790
1791 switch (NNS->getKind()) {
1792 case NestedNameSpecifier::Identifier:
1793 // Canonicalize the prefix but keep the identifier the same.
1794 return NestedNameSpecifier::Create(*this,
1795 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1796 NNS->getAsIdentifier());
1797
1798 case NestedNameSpecifier::Namespace:
1799 // A namespace is canonical; build a nested-name-specifier with
1800 // this namespace and no prefix.
1801 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1802
1803 case NestedNameSpecifier::TypeSpec:
1804 case NestedNameSpecifier::TypeSpecWithTemplate: {
1805 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1806 NestedNameSpecifier *Prefix = 0;
1807
1808 // FIXME: This isn't the right check!
1809 if (T->isDependentType())
1810 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1811
1812 return NestedNameSpecifier::Create(*this, Prefix,
1813 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1814 T.getTypePtr());
1815 }
1816
1817 case NestedNameSpecifier::Global:
1818 // The global specifier is canonical and unique.
1819 return NNS;
1820 }
1821
1822 // Required to silence a GCC warning
1823 return 0;
1824}
1825
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001826
1827const ArrayType *ASTContext::getAsArrayType(QualType T) {
1828 // Handle the non-qualified case efficiently.
1829 if (T.getCVRQualifiers() == 0) {
1830 // Handle the common positive case fast.
1831 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1832 return AT;
1833 }
1834
1835 // Handle the common negative case fast, ignoring CVR qualifiers.
1836 QualType CType = T->getCanonicalTypeInternal();
1837
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001838 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001839 // test.
1840 if (!isa<ArrayType>(CType) &&
1841 !isa<ArrayType>(CType.getUnqualifiedType()))
1842 return 0;
1843
1844 // Apply any CVR qualifiers from the array type to the element type. This
1845 // implements C99 6.7.3p8: "If the specification of an array type includes
1846 // any type qualifiers, the element type is so qualified, not the array type."
1847
1848 // If we get here, we either have type qualifiers on the type, or we have
1849 // sugar such as a typedef in the way. If we have type qualifiers on the type
1850 // we must propagate them down into the elemeng type.
1851 unsigned CVRQuals = T.getCVRQualifiers();
1852 unsigned AddrSpace = 0;
1853 Type *Ty = T.getTypePtr();
1854
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001855 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001856 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001857 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1858 AddrSpace = EXTQT->getAddressSpace();
1859 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001860 } else {
1861 T = Ty->getDesugaredType();
1862 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1863 break;
1864 CVRQuals |= T.getCVRQualifiers();
1865 Ty = T.getTypePtr();
1866 }
1867 }
1868
1869 // If we have a simple case, just return now.
1870 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1871 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1872 return ATy;
1873
1874 // Otherwise, we have an array and we have qualifiers on it. Push the
1875 // qualifiers into the array element type and return a new array type.
1876 // Get the canonical version of the element with the extra qualifiers on it.
1877 // This can recursively sink qualifiers through multiple levels of arrays.
1878 QualType NewEltTy = ATy->getElementType();
1879 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001880 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001881 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1882
1883 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1884 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1885 CAT->getSizeModifier(),
1886 CAT->getIndexTypeQualifier()));
1887 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1888 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1889 IAT->getSizeModifier(),
1890 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001891
Douglas Gregor898574e2008-12-05 23:32:09 +00001892 if (const DependentSizedArrayType *DSAT
1893 = dyn_cast<DependentSizedArrayType>(ATy))
1894 return cast<ArrayType>(
1895 getDependentSizedArrayType(NewEltTy,
1896 DSAT->getSizeExpr(),
1897 DSAT->getSizeModifier(),
1898 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001899
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001900 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1901 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1902 VAT->getSizeModifier(),
1903 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001904}
1905
1906
Chris Lattnere6327742008-04-02 05:18:44 +00001907/// getArrayDecayedType - Return the properly qualified result of decaying the
1908/// specified array type to a pointer. This operation is non-trivial when
1909/// handling typedefs etc. The canonical type of "T" must be an array type,
1910/// this returns a pointer to a properly qualified element of the array.
1911///
1912/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1913QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001914 // Get the element type with 'getAsArrayType' so that we don't lose any
1915 // typedefs in the element type of the array. This also handles propagation
1916 // of type qualifiers from the array type into the element type if present
1917 // (C99 6.7.3p8).
1918 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1919 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001920
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001921 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001922
1923 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001924 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001925}
1926
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001927QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001928 QualType ElemTy = VAT->getElementType();
1929
1930 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1931 return getBaseElementType(VAT);
1932
1933 return ElemTy;
1934}
1935
Reid Spencer5f016e22007-07-11 17:01:13 +00001936/// getFloatingRank - Return a relative rank for floating point types.
1937/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001938static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001939 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001940 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001941
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001942 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001943 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001944 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001945 case BuiltinType::Float: return FloatRank;
1946 case BuiltinType::Double: return DoubleRank;
1947 case BuiltinType::LongDouble: return LongDoubleRank;
1948 }
1949}
1950
Steve Naroff716c7302007-08-27 01:41:48 +00001951/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1952/// point or a complex type (based on typeDomain/typeSize).
1953/// 'typeDomain' is a real floating point or complex type.
1954/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001955QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1956 QualType Domain) const {
1957 FloatingRank EltRank = getFloatingRank(Size);
1958 if (Domain->isComplexType()) {
1959 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001960 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001961 case FloatRank: return FloatComplexTy;
1962 case DoubleRank: return DoubleComplexTy;
1963 case LongDoubleRank: return LongDoubleComplexTy;
1964 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001965 }
Chris Lattner1361b112008-04-06 23:58:54 +00001966
1967 assert(Domain->isRealFloatingType() && "Unknown domain!");
1968 switch (EltRank) {
1969 default: assert(0 && "getFloatingRank(): illegal value for rank");
1970 case FloatRank: return FloatTy;
1971 case DoubleRank: return DoubleTy;
1972 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001973 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001974}
1975
Chris Lattner7cfeb082008-04-06 23:55:33 +00001976/// getFloatingTypeOrder - Compare the rank of the two specified floating
1977/// point types, ignoring the domain of the type (i.e. 'double' ==
1978/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1979/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001980int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1981 FloatingRank LHSR = getFloatingRank(LHS);
1982 FloatingRank RHSR = getFloatingRank(RHS);
1983
1984 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001985 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001986 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001987 return 1;
1988 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001989}
1990
Chris Lattnerf52ab252008-04-06 22:59:24 +00001991/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1992/// routine will assert if passed a built-in type that isn't an integer or enum,
1993/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001994unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001995 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001996 if (EnumType* ET = dyn_cast<EnumType>(T))
1997 T = ET->getDecl()->getIntegerType().getTypePtr();
1998
1999 // There are two things which impact the integer rank: the width, and
2000 // the ordering of builtins. The builtin ordering is encoded in the
2001 // bottom three bits; the width is encoded in the bits above that.
2002 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
2003 return FWIT->getWidth() << 3;
2004 }
2005
Chris Lattnerf52ab252008-04-06 22:59:24 +00002006 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00002007 default: assert(0 && "getIntegerRank(): not a built-in integer");
2008 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002009 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002010 case BuiltinType::Char_S:
2011 case BuiltinType::Char_U:
2012 case BuiltinType::SChar:
2013 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002014 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002015 case BuiltinType::Short:
2016 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002017 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002018 case BuiltinType::Int:
2019 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002020 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002021 case BuiltinType::Long:
2022 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002023 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002024 case BuiltinType::LongLong:
2025 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002026 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00002027 case BuiltinType::Int128:
2028 case BuiltinType::UInt128:
2029 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00002030 }
2031}
2032
Chris Lattner7cfeb082008-04-06 23:55:33 +00002033/// getIntegerTypeOrder - Returns the highest ranked integer type:
2034/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
2035/// LHS < RHS, return -1.
2036int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002037 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2038 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00002039 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002040
Chris Lattnerf52ab252008-04-06 22:59:24 +00002041 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2042 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00002043
Chris Lattner7cfeb082008-04-06 23:55:33 +00002044 unsigned LHSRank = getIntegerRank(LHSC);
2045 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00002046
Chris Lattner7cfeb082008-04-06 23:55:33 +00002047 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2048 if (LHSRank == RHSRank) return 0;
2049 return LHSRank > RHSRank ? 1 : -1;
2050 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002051
Chris Lattner7cfeb082008-04-06 23:55:33 +00002052 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2053 if (LHSUnsigned) {
2054 // If the unsigned [LHS] type is larger, return it.
2055 if (LHSRank >= RHSRank)
2056 return 1;
2057
2058 // If the signed type can represent all values of the unsigned type, it
2059 // wins. Because we are dealing with 2's complement and types that are
2060 // powers of two larger than each other, this is always safe.
2061 return -1;
2062 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002063
Chris Lattner7cfeb082008-04-06 23:55:33 +00002064 // If the unsigned [RHS] type is larger, return it.
2065 if (RHSRank >= LHSRank)
2066 return -1;
2067
2068 // If the signed type can represent all values of the unsigned type, it
2069 // wins. Because we are dealing with 2's complement and types that are
2070 // powers of two larger than each other, this is always safe.
2071 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002072}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002073
2074// getCFConstantStringType - Return the type used for constant CFStrings.
2075QualType ASTContext::getCFConstantStringType() {
2076 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00002077 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002078 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002079 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002080 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00002081
2082 // const int *isa;
2083 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002084 // int flags;
2085 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002086 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002087 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00002088 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002089 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00002090
Anders Carlsson71993dd2007-08-17 05:31:46 +00002091 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002092 for (unsigned i = 0; i < 4; ++i) {
2093 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2094 SourceLocation(), 0,
2095 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002096 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002097 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002098 }
2099
2100 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002101 }
2102
2103 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002104}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002105
Douglas Gregor319ac892009-04-23 22:29:11 +00002106void ASTContext::setCFConstantStringType(QualType T) {
2107 const RecordType *Rec = T->getAsRecordType();
2108 assert(Rec && "Invalid CFConstantStringType");
2109 CFConstantStringTypeDecl = Rec->getDecl();
2110}
2111
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002112QualType ASTContext::getObjCFastEnumerationStateType()
2113{
2114 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002115 ObjCFastEnumerationStateTypeDecl =
2116 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2117 &Idents.get("__objcFastEnumerationState"));
2118
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002119 QualType FieldTypes[] = {
2120 UnsignedLongTy,
2121 getPointerType(ObjCIdType),
2122 getPointerType(UnsignedLongTy),
2123 getConstantArrayType(UnsignedLongTy,
2124 llvm::APInt(32, 5), ArrayType::Normal, 0)
2125 };
2126
Douglas Gregor44b43212008-12-11 16:49:14 +00002127 for (size_t i = 0; i < 4; ++i) {
2128 FieldDecl *Field = FieldDecl::Create(*this,
2129 ObjCFastEnumerationStateTypeDecl,
2130 SourceLocation(), 0,
2131 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002132 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002133 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002134 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002135
Douglas Gregor44b43212008-12-11 16:49:14 +00002136 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002137 }
2138
2139 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2140}
2141
Douglas Gregor319ac892009-04-23 22:29:11 +00002142void ASTContext::setObjCFastEnumerationStateType(QualType T) {
2143 const RecordType *Rec = T->getAsRecordType();
2144 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2145 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2146}
2147
Anders Carlssone8c49532007-10-29 06:33:42 +00002148// This returns true if a type has been typedefed to BOOL:
2149// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002150static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002151 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002152 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2153 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002154
2155 return false;
2156}
2157
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002158/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002159/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002160int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002161 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002162
2163 // Make all integer and enum types at least as large as an int
2164 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002165 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002166 // Treat arrays as pointers, since that's how they're passed in.
2167 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002168 sz = getTypeSize(VoidPtrTy);
2169 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002170}
2171
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002172/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002173/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002174void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002175 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002176 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002177 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002178 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002179 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002180 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002181 // Compute size of all parameters.
2182 // Start with computing size of a pointer in number of bytes.
2183 // FIXME: There might(should) be a better way of doing this computation!
2184 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002185 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002186 // The first two arguments (self and _cmd) are pointers; account for
2187 // their size.
2188 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002189 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2190 E = Decl->param_end(); PI != E; ++PI) {
2191 QualType PType = (*PI)->getType();
2192 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002193 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002194 ParmOffset += sz;
2195 }
2196 S += llvm::utostr(ParmOffset);
2197 S += "@0:";
2198 S += llvm::utostr(PtrSize);
2199
2200 // Argument types.
2201 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002202 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2203 E = Decl->param_end(); PI != E; ++PI) {
2204 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002205 QualType PType = PVDecl->getOriginalType();
2206 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002207 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2208 // Use array's original type only if it has known number of
2209 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002210 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002211 PType = PVDecl->getType();
2212 } else if (PType->isFunctionType())
2213 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002214 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002215 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002216 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002217 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002218 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002219 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002220 }
2221}
2222
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002223/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002224/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002225/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2226/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002227/// Property attributes are stored as a comma-delimited C string. The simple
2228/// attributes readonly and bycopy are encoded as single characters. The
2229/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2230/// encoded as single characters, followed by an identifier. Property types
2231/// are also encoded as a parametrized attribute. The characters used to encode
2232/// these attributes are defined by the following enumeration:
2233/// @code
2234/// enum PropertyAttributes {
2235/// kPropertyReadOnly = 'R', // property is read-only.
2236/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2237/// kPropertyByref = '&', // property is a reference to the value last assigned
2238/// kPropertyDynamic = 'D', // property is dynamic
2239/// kPropertyGetter = 'G', // followed by getter selector name
2240/// kPropertySetter = 'S', // followed by setter selector name
2241/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2242/// kPropertyType = 't' // followed by old-style type encoding.
2243/// kPropertyWeak = 'W' // 'weak' property
2244/// kPropertyStrong = 'P' // property GC'able
2245/// kPropertyNonAtomic = 'N' // property non-atomic
2246/// };
2247/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002248void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2249 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002250 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002251 // Collect information from the property implementation decl(s).
2252 bool Dynamic = false;
2253 ObjCPropertyImplDecl *SynthesizePID = 0;
2254
2255 // FIXME: Duplicated code due to poor abstraction.
2256 if (Container) {
2257 if (const ObjCCategoryImplDecl *CID =
2258 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2259 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002260 i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2261 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002262 ObjCPropertyImplDecl *PID = *i;
2263 if (PID->getPropertyDecl() == PD) {
2264 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2265 Dynamic = true;
2266 } else {
2267 SynthesizePID = PID;
2268 }
2269 }
2270 }
2271 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002272 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002273 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002274 i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2275 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002276 ObjCPropertyImplDecl *PID = *i;
2277 if (PID->getPropertyDecl() == PD) {
2278 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2279 Dynamic = true;
2280 } else {
2281 SynthesizePID = PID;
2282 }
2283 }
2284 }
2285 }
2286 }
2287
2288 // FIXME: This is not very efficient.
2289 S = "T";
2290
2291 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002292 // GCC has some special rules regarding encoding of properties which
2293 // closely resembles encoding of ivars.
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002294 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002295 true /* outermost type */,
2296 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002297
2298 if (PD->isReadOnly()) {
2299 S += ",R";
2300 } else {
2301 switch (PD->getSetterKind()) {
2302 case ObjCPropertyDecl::Assign: break;
2303 case ObjCPropertyDecl::Copy: S += ",C"; break;
2304 case ObjCPropertyDecl::Retain: S += ",&"; break;
2305 }
2306 }
2307
2308 // It really isn't clear at all what this means, since properties
2309 // are "dynamic by default".
2310 if (Dynamic)
2311 S += ",D";
2312
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002313 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2314 S += ",N";
2315
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002316 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2317 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002318 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002319 }
2320
2321 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2322 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002323 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002324 }
2325
2326 if (SynthesizePID) {
2327 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2328 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002329 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002330 }
2331
2332 // FIXME: OBJCGC: weak & strong
2333}
2334
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002335/// getLegacyIntegralTypeEncoding -
2336/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002337/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002338/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2339///
2340void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2341 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2342 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002343 if (BT->getKind() == BuiltinType::ULong &&
2344 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002345 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002346 else
2347 if (BT->getKind() == BuiltinType::Long &&
2348 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002349 PointeeTy = IntTy;
2350 }
2351 }
2352}
2353
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002354void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002355 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002356 // We follow the behavior of gcc, expanding structures which are
2357 // directly pointed to, and expanding embedded structures. Note that
2358 // these rules are sufficient to prevent recursive encoding of the
2359 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002360 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2361 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002362}
2363
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002364static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002365 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002366 const Expr *E = FD->getBitWidth();
2367 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2368 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002369 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002370 S += 'b';
2371 S += llvm::utostr(N);
2372}
2373
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002374void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2375 bool ExpandPointedToStructures,
2376 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002377 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002378 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002379 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002380 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002381 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002382 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002383 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002384 else {
2385 char encoding;
2386 switch (BT->getKind()) {
2387 default: assert(0 && "Unhandled builtin type kind");
2388 case BuiltinType::Void: encoding = 'v'; break;
2389 case BuiltinType::Bool: encoding = 'B'; break;
2390 case BuiltinType::Char_U:
2391 case BuiltinType::UChar: encoding = 'C'; break;
2392 case BuiltinType::UShort: encoding = 'S'; break;
2393 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002394 case BuiltinType::ULong:
2395 encoding =
2396 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2397 break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002398 case BuiltinType::UInt128: encoding = 'T'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002399 case BuiltinType::ULongLong: encoding = 'Q'; break;
2400 case BuiltinType::Char_S:
2401 case BuiltinType::SChar: encoding = 'c'; break;
2402 case BuiltinType::Short: encoding = 's'; break;
2403 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002404 case BuiltinType::Long:
2405 encoding =
2406 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2407 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002408 case BuiltinType::LongLong: encoding = 'q'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002409 case BuiltinType::Int128: encoding = 't'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002410 case BuiltinType::Float: encoding = 'f'; break;
2411 case BuiltinType::Double: encoding = 'd'; break;
2412 case BuiltinType::LongDouble: encoding = 'd'; break;
2413 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002414
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002415 S += encoding;
2416 }
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002417 } else if (const ComplexType *CT = T->getAsComplexType()) {
2418 S += 'j';
2419 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2420 false);
2421 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002422 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2423 ExpandPointedToStructures,
2424 ExpandStructures, FD);
2425 if (FD || EncodingProperty) {
2426 // Note that we do extended encoding of protocol qualifer list
2427 // Only when doing ivar or property encoding.
2428 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2429 S += '"';
2430 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2431 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2432 S += '<';
2433 S += Proto->getNameAsString();
2434 S += '>';
2435 }
2436 S += '"';
2437 }
2438 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002439 }
2440 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002441 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002442 bool isReadOnly = false;
2443 // For historical/compatibility reasons, the read-only qualifier of the
2444 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2445 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2446 // Also, do not emit the 'r' for anything but the outermost type!
2447 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2448 if (OutermostType && T.isConstQualified()) {
2449 isReadOnly = true;
2450 S += 'r';
2451 }
2452 }
2453 else if (OutermostType) {
2454 QualType P = PointeeTy;
2455 while (P->getAsPointerType())
2456 P = P->getAsPointerType()->getPointeeType();
2457 if (P.isConstQualified()) {
2458 isReadOnly = true;
2459 S += 'r';
2460 }
2461 }
2462 if (isReadOnly) {
2463 // Another legacy compatibility encoding. Some ObjC qualifier and type
2464 // combinations need to be rearranged.
2465 // Rewrite "in const" from "nr" to "rn"
2466 const char * s = S.c_str();
2467 int len = S.length();
2468 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2469 std::string replace = "rn";
2470 S.replace(S.end()-2, S.end(), replace);
2471 }
2472 }
Steve Naroff389bf462009-02-12 17:52:19 +00002473 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002474 S += '@';
2475 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002476 }
2477 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002478 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002479 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002480 // Another historical/compatibility reason.
2481 // We encode the underlying type which comes out as
2482 // {...};
2483 S += '^';
2484 getObjCEncodingForTypeImpl(PointeeTy, S,
2485 false, ExpandPointedToStructures,
2486 NULL);
2487 return;
2488 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002489 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002490 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002491 const ObjCInterfaceType *OIT =
2492 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002493 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002494 S += '"';
2495 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002496 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2497 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2498 S += '<';
2499 S += Proto->getNameAsString();
2500 S += '>';
2501 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002502 S += '"';
2503 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002504 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002505 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002506 S += '#';
2507 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002508 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002509 S += ':';
2510 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002511 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002512
2513 if (PointeeTy->isCharType()) {
2514 // char pointer types should be encoded as '*' unless it is a
2515 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002516 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002517 S += '*';
2518 return;
2519 }
2520 }
2521
2522 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002523 getLegacyIntegralTypeEncoding(PointeeTy);
2524
2525 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002526 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002527 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002528 } else if (const ArrayType *AT =
2529 // Ignore type qualifiers etc.
2530 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002531 if (isa<IncompleteArrayType>(AT)) {
2532 // Incomplete arrays are encoded as a pointer to the array element.
2533 S += '^';
2534
2535 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2536 false, ExpandStructures, FD);
2537 } else {
2538 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002539
Anders Carlsson559a8332009-02-22 01:38:57 +00002540 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2541 S += llvm::utostr(CAT->getSize().getZExtValue());
2542 else {
2543 //Variable length arrays are encoded as a regular array with 0 elements.
2544 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2545 S += '0';
2546 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002547
Anders Carlsson559a8332009-02-22 01:38:57 +00002548 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2549 false, ExpandStructures, FD);
2550 S += ']';
2551 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002552 } else if (T->getAsFunctionType()) {
2553 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002554 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002555 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002556 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002557 // Anonymous structures print as '?'
2558 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2559 S += II->getName();
2560 } else {
2561 S += '?';
2562 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002563 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002564 S += '=';
Douglas Gregor6ab35242009-04-09 21:40:53 +00002565 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2566 FieldEnd = RDecl->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +00002567 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002568 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002569 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002570 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002571 S += '"';
2572 }
2573
2574 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002575 if (Field->isBitField()) {
2576 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2577 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002578 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002579 QualType qt = Field->getType();
2580 getLegacyIntegralTypeEncoding(qt);
2581 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002582 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002583 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002584 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002585 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002586 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002587 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002588 if (FD && FD->isBitField())
2589 EncodeBitField(this, S, FD);
2590 else
2591 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002592 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002593 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002594 } else if (T->isObjCInterfaceType()) {
2595 // @encode(class_name)
2596 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2597 S += '{';
2598 const IdentifierInfo *II = OI->getIdentifier();
2599 S += II->getName();
2600 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002601 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002602 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002603 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002604 if (RecFields[i]->isBitField())
2605 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2606 RecFields[i]);
2607 else
2608 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2609 FD);
2610 }
2611 S += '}';
2612 }
2613 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002614 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002615}
2616
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002617void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002618 std::string& S) const {
2619 if (QT & Decl::OBJC_TQ_In)
2620 S += 'n';
2621 if (QT & Decl::OBJC_TQ_Inout)
2622 S += 'N';
2623 if (QT & Decl::OBJC_TQ_Out)
2624 S += 'o';
2625 if (QT & Decl::OBJC_TQ_Bycopy)
2626 S += 'O';
2627 if (QT & Decl::OBJC_TQ_Byref)
2628 S += 'R';
2629 if (QT & Decl::OBJC_TQ_Oneway)
2630 S += 'V';
2631}
2632
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002633void ASTContext::setBuiltinVaListType(QualType T)
2634{
2635 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2636
2637 BuiltinVaListType = T;
2638}
2639
Douglas Gregor319ac892009-04-23 22:29:11 +00002640void ASTContext::setObjCIdType(QualType T)
Steve Naroff7e219e42007-10-15 14:41:52 +00002641{
Douglas Gregor319ac892009-04-23 22:29:11 +00002642 ObjCIdType = T;
2643
2644 const TypedefType *TT = T->getAsTypedefType();
2645 if (!TT)
2646 return;
2647
2648 TypedefDecl *TD = TT->getDecl();
Steve Naroff7e219e42007-10-15 14:41:52 +00002649
2650 // typedef struct objc_object *id;
2651 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002652 // User error - caller will issue diagnostics.
2653 if (!ptr)
2654 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002655 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002656 // User error - caller will issue diagnostics.
2657 if (!rec)
2658 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002659 IdStructType = rec;
2660}
2661
Douglas Gregor319ac892009-04-23 22:29:11 +00002662void ASTContext::setObjCSelType(QualType T)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002663{
Douglas Gregor319ac892009-04-23 22:29:11 +00002664 ObjCSelType = T;
2665
2666 const TypedefType *TT = T->getAsTypedefType();
2667 if (!TT)
2668 return;
2669 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002670
2671 // typedef struct objc_selector *SEL;
2672 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002673 if (!ptr)
2674 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002675 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002676 if (!rec)
2677 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002678 SelStructType = rec;
2679}
2680
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002681void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002682{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002683 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002684}
2685
Douglas Gregor319ac892009-04-23 22:29:11 +00002686void ASTContext::setObjCClassType(QualType T)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002687{
Douglas Gregor319ac892009-04-23 22:29:11 +00002688 ObjCClassType = T;
2689
2690 const TypedefType *TT = T->getAsTypedefType();
2691 if (!TT)
2692 return;
2693 TypedefDecl *TD = TT->getDecl();
Anders Carlsson8baaca52007-10-31 02:53:19 +00002694
2695 // typedef struct objc_class *Class;
2696 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2697 assert(ptr && "'Class' incorrectly typed");
2698 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2699 assert(rec && "'Class' incorrectly typed");
2700 ClassStructType = rec;
2701}
2702
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002703void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2704 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002705 "'NSConstantString' type already set!");
2706
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002707 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002708}
2709
Douglas Gregor7532dc62009-03-30 22:58:21 +00002710/// \brief Retrieve the template name that represents a qualified
2711/// template name such as \c std::vector.
2712TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2713 bool TemplateKeyword,
2714 TemplateDecl *Template) {
2715 llvm::FoldingSetNodeID ID;
2716 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2717
2718 void *InsertPos = 0;
2719 QualifiedTemplateName *QTN =
2720 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2721 if (!QTN) {
2722 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2723 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2724 }
2725
2726 return TemplateName(QTN);
2727}
2728
2729/// \brief Retrieve the template name that represents a dependent
2730/// template name such as \c MetaFun::template apply.
2731TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2732 const IdentifierInfo *Name) {
2733 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2734
2735 llvm::FoldingSetNodeID ID;
2736 DependentTemplateName::Profile(ID, NNS, Name);
2737
2738 void *InsertPos = 0;
2739 DependentTemplateName *QTN =
2740 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2741
2742 if (QTN)
2743 return TemplateName(QTN);
2744
2745 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2746 if (CanonNNS == NNS) {
2747 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2748 } else {
2749 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2750 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2751 }
2752
2753 DependentTemplateNames.InsertNode(QTN, InsertPos);
2754 return TemplateName(QTN);
2755}
2756
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002757/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002758/// TargetInfo, produce the corresponding type. The unsigned @p Type
2759/// is actually a value of type @c TargetInfo::IntType.
2760QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002761 switch (Type) {
2762 case TargetInfo::NoInt: return QualType();
2763 case TargetInfo::SignedShort: return ShortTy;
2764 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2765 case TargetInfo::SignedInt: return IntTy;
2766 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2767 case TargetInfo::SignedLong: return LongTy;
2768 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2769 case TargetInfo::SignedLongLong: return LongLongTy;
2770 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2771 }
2772
2773 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002774 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002775}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002776
2777//===----------------------------------------------------------------------===//
2778// Type Predicates.
2779//===----------------------------------------------------------------------===//
2780
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002781/// isObjCNSObjectType - Return true if this is an NSObject object using
2782/// NSObject attribute on a c-style pointer type.
2783/// FIXME - Make it work directly on types.
2784///
2785bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2786 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2787 if (TypedefDecl *TD = TDT->getDecl())
2788 if (TD->getAttr<ObjCNSObjectAttr>())
2789 return true;
2790 }
2791 return false;
2792}
2793
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002794/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2795/// to an object type. This includes "id" and "Class" (two 'special' pointers
2796/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2797/// ID type).
2798bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002799 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002800 return true;
2801
Steve Naroff6ae98502008-10-21 18:24:04 +00002802 // Blocks are objects.
2803 if (Ty->isBlockPointerType())
2804 return true;
2805
2806 // All other object types are pointers.
Chris Lattner16ede0e2009-04-12 23:51:02 +00002807 const PointerType *PT = Ty->getAsPointerType();
2808 if (PT == 0)
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002809 return false;
2810
Chris Lattner16ede0e2009-04-12 23:51:02 +00002811 // If this a pointer to an interface (e.g. NSString*), it is ok.
2812 if (PT->getPointeeType()->isObjCInterfaceType() ||
2813 // If is has NSObject attribute, OK as well.
2814 isObjCNSObjectType(Ty))
2815 return true;
2816
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002817 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2818 // pointer types. This looks for the typedef specifically, not for the
Chris Lattner16ede0e2009-04-12 23:51:02 +00002819 // underlying type. Iteratively strip off typedefs so that we can handle
2820 // typedefs of typedefs.
2821 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2822 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2823 Ty.getUnqualifiedType() == getObjCClassType())
2824 return true;
2825
2826 Ty = TDT->getDecl()->getUnderlyingType();
2827 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002828
Chris Lattner16ede0e2009-04-12 23:51:02 +00002829 return false;
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002830}
2831
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002832/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2833/// garbage collection attribute.
2834///
2835QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002836 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002837 if (getLangOptions().ObjC1 &&
2838 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002839 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002840 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002841 // (or pointers to them) be treated as though they were declared
2842 // as __strong.
2843 if (GCAttrs == QualType::GCNone) {
2844 if (isObjCObjectPointerType(Ty))
2845 GCAttrs = QualType::Strong;
2846 else if (Ty->isPointerType())
2847 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2848 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00002849 // Non-pointers have none gc'able attribute regardless of the attribute
2850 // set on them.
2851 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2852 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002853 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002854 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002855}
2856
Chris Lattner6ac46a42008-04-07 06:51:04 +00002857//===----------------------------------------------------------------------===//
2858// Type Compatibility Testing
2859//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002860
Steve Naroff1c7d0672008-09-04 15:10:53 +00002861/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002862/// block types. Types must be strictly compatible here. For example,
2863/// C unfortunately doesn't produce an error for the following:
2864///
2865/// int (*emptyArgFunc)();
2866/// int (*intArgList)(int) = emptyArgFunc;
2867///
2868/// For blocks, we will produce an error for the following (similar to C++):
2869///
2870/// int (^emptyArgBlock)();
2871/// int (^intArgBlock)(int) = emptyArgBlock;
2872///
2873/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2874///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002875bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002876 const FunctionType *lbase = lhs->getAsFunctionType();
2877 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002878 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2879 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stumpaab0f7a2009-04-01 01:17:39 +00002880 if (lproto && rproto == 0)
2881 return false;
2882 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff1c7d0672008-09-04 15:10:53 +00002883}
2884
Chris Lattner6ac46a42008-04-07 06:51:04 +00002885/// areCompatVectorTypes - Return true if the two specified vector types are
2886/// compatible.
2887static bool areCompatVectorTypes(const VectorType *LHS,
2888 const VectorType *RHS) {
2889 assert(LHS->isCanonical() && RHS->isCanonical());
2890 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002891 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002892}
2893
Eli Friedman3d815e72008-08-22 00:56:42 +00002894/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002895/// compatible for assignment from RHS to LHS. This handles validation of any
2896/// protocol qualifiers on the LHS or RHS.
2897///
Eli Friedman3d815e72008-08-22 00:56:42 +00002898bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2899 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002900 // Verify that the base decls are compatible: the RHS must be a subclass of
2901 // the LHS.
2902 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2903 return false;
2904
2905 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2906 // protocol qualified at all, then we are good.
2907 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2908 return true;
2909
2910 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2911 // isn't a superset.
2912 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2913 return true; // FIXME: should return false!
2914
2915 // Finally, we must have two protocol-qualified interfaces.
2916 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2917 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002918
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002919 // All LHS protocols must have a presence on the RHS.
2920 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002921
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002922 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2923 LHSPE = LHSP->qual_end();
2924 LHSPI != LHSPE; LHSPI++) {
2925 bool RHSImplementsProtocol = false;
2926
2927 // If the RHS doesn't implement the protocol on the left, the types
2928 // are incompatible.
2929 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2930 RHSPE = RHSP->qual_end();
2931 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2932 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2933 RHSImplementsProtocol = true;
2934 }
2935 // FIXME: For better diagnostics, consider passing back the protocol name.
2936 if (!RHSImplementsProtocol)
2937 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002938 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002939 // The RHS implements all protocols listed on the LHS.
2940 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002941}
2942
Steve Naroff389bf462009-02-12 17:52:19 +00002943bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2944 // get the "pointed to" types
2945 const PointerType *LHSPT = LHS->getAsPointerType();
2946 const PointerType *RHSPT = RHS->getAsPointerType();
2947
2948 if (!LHSPT || !RHSPT)
2949 return false;
2950
2951 QualType lhptee = LHSPT->getPointeeType();
2952 QualType rhptee = RHSPT->getPointeeType();
2953 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2954 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2955 // ID acts sort of like void* for ObjC interfaces
2956 if (LHSIface && isObjCIdStructType(rhptee))
2957 return true;
2958 if (RHSIface && isObjCIdStructType(lhptee))
2959 return true;
2960 if (!LHSIface || !RHSIface)
2961 return false;
2962 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2963 canAssignObjCInterfaces(RHSIface, LHSIface);
2964}
2965
Steve Naroffec0550f2007-10-15 20:41:53 +00002966/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2967/// both shall have the identically qualified version of a compatible type.
2968/// C99 6.2.7p1: Two types have compatible types if their types are the
2969/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002970bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2971 return !mergeTypes(LHS, RHS).isNull();
2972}
2973
2974QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2975 const FunctionType *lbase = lhs->getAsFunctionType();
2976 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002977 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2978 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002979 bool allLTypes = true;
2980 bool allRTypes = true;
2981
2982 // Check return type
2983 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2984 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002985 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2986 allLTypes = false;
2987 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2988 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002989
2990 if (lproto && rproto) { // two C99 style function prototypes
2991 unsigned lproto_nargs = lproto->getNumArgs();
2992 unsigned rproto_nargs = rproto->getNumArgs();
2993
2994 // Compatible functions must have the same number of arguments
2995 if (lproto_nargs != rproto_nargs)
2996 return QualType();
2997
2998 // Variadic and non-variadic functions aren't compatible
2999 if (lproto->isVariadic() != rproto->isVariadic())
3000 return QualType();
3001
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003002 if (lproto->getTypeQuals() != rproto->getTypeQuals())
3003 return QualType();
3004
Eli Friedman3d815e72008-08-22 00:56:42 +00003005 // Check argument compatibility
3006 llvm::SmallVector<QualType, 10> types;
3007 for (unsigned i = 0; i < lproto_nargs; i++) {
3008 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
3009 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
3010 QualType argtype = mergeTypes(largtype, rargtype);
3011 if (argtype.isNull()) return QualType();
3012 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00003013 if (getCanonicalType(argtype) != getCanonicalType(largtype))
3014 allLTypes = false;
3015 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
3016 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00003017 }
3018 if (allLTypes) return lhs;
3019 if (allRTypes) return rhs;
3020 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003021 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00003022 }
3023
3024 if (lproto) allRTypes = false;
3025 if (rproto) allLTypes = false;
3026
Douglas Gregor72564e72009-02-26 23:50:07 +00003027 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00003028 if (proto) {
3029 if (proto->isVariadic()) return QualType();
3030 // Check that the types are compatible with the types that
3031 // would result from default argument promotions (C99 6.7.5.3p15).
3032 // The only types actually affected are promotable integer
3033 // types and floats, which would be passed as a different
3034 // type depending on whether the prototype is visible.
3035 unsigned proto_nargs = proto->getNumArgs();
3036 for (unsigned i = 0; i < proto_nargs; ++i) {
3037 QualType argTy = proto->getArgType(i);
3038 if (argTy->isPromotableIntegerType() ||
3039 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
3040 return QualType();
3041 }
3042
3043 if (allLTypes) return lhs;
3044 if (allRTypes) return rhs;
3045 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003046 proto->getNumArgs(), lproto->isVariadic(),
3047 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00003048 }
3049
3050 if (allLTypes) return lhs;
3051 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00003052 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00003053}
3054
3055QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00003056 // C++ [expr]: If an expression initially has the type "reference to T", the
3057 // type is adjusted to "T" prior to any further analysis, the expression
3058 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003059 // expression is an lvalue unless the reference is an rvalue reference and
3060 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00003061 // FIXME: C++ shouldn't be going through here! The rules are different
3062 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003063 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3064 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00003065 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003066 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00003067 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003068 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00003069
Eli Friedman3d815e72008-08-22 00:56:42 +00003070 QualType LHSCan = getCanonicalType(LHS),
3071 RHSCan = getCanonicalType(RHS);
3072
3073 // If two types are identical, they are compatible.
3074 if (LHSCan == RHSCan)
3075 return LHS;
3076
3077 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003078 // Note that we handle extended qualifiers later, in the
3079 // case for ExtQualType.
3080 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00003081 return QualType();
3082
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003083 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
3084 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003085
Chris Lattner1adb8832008-01-14 05:45:46 +00003086 // We want to consider the two function types to be the same for these
3087 // comparisons, just force one to the other.
3088 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3089 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003090
3091 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003092 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3093 LHSClass = Type::ConstantArray;
3094 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3095 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00003096
Nate Begeman213541a2008-04-18 23:10:10 +00003097 // Canonicalize ExtVector -> Vector.
3098 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3099 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00003100
Chris Lattnerb0489812008-04-07 06:38:24 +00003101 // Consider qualified interfaces and interfaces the same.
3102 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
3103 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00003104
Chris Lattnera36a61f2008-04-07 05:43:21 +00003105 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003106 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003107 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3108 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003109
Steve Naroffd824c9c2009-04-14 15:11:46 +00003110 // 'id' and 'Class' act sort of like void* for ObjC interfaces
3111 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003112 return LHS;
Steve Naroffd824c9c2009-04-14 15:11:46 +00003113 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003114 return RHS;
3115
Steve Naroffbc76dd02008-12-10 22:14:21 +00003116 // ID is compatible with all qualified id types.
3117 if (LHS->isObjCQualifiedIdType()) {
3118 if (const PointerType *PT = RHS->getAsPointerType()) {
3119 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003120 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003121 return LHS;
3122 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3123 // Unfortunately, this API is part of Sema (which we don't have access
3124 // to. Need to refactor. The following check is insufficient, since we
3125 // need to make sure the class implements the protocol.
3126 if (pType->isObjCInterfaceType())
3127 return LHS;
3128 }
3129 }
3130 if (RHS->isObjCQualifiedIdType()) {
3131 if (const PointerType *PT = LHS->getAsPointerType()) {
3132 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003133 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003134 return RHS;
3135 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3136 // Unfortunately, this API is part of Sema (which we don't have access
3137 // to. Need to refactor. The following check is insufficient, since we
3138 // need to make sure the class implements the protocol.
3139 if (pType->isObjCInterfaceType())
3140 return RHS;
3141 }
3142 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003143 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3144 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00003145 if (const EnumType* ETy = LHS->getAsEnumType()) {
3146 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3147 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003148 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003149 if (const EnumType* ETy = RHS->getAsEnumType()) {
3150 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3151 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003152 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003153
Eli Friedman3d815e72008-08-22 00:56:42 +00003154 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003155 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003156
Steve Naroff4a746782008-01-09 22:43:08 +00003157 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003158 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003159#define TYPE(Class, Base)
3160#define ABSTRACT_TYPE(Class, Base)
3161#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3162#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3163#include "clang/AST/TypeNodes.def"
3164 assert(false && "Non-canonical and dependent types shouldn't get here");
3165 return QualType();
3166
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003167 case Type::LValueReference:
3168 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003169 case Type::MemberPointer:
3170 assert(false && "C++ should never be in mergeTypes");
3171 return QualType();
3172
3173 case Type::IncompleteArray:
3174 case Type::VariableArray:
3175 case Type::FunctionProto:
3176 case Type::ExtVector:
3177 case Type::ObjCQualifiedInterface:
3178 assert(false && "Types are eliminated above");
3179 return QualType();
3180
Chris Lattner1adb8832008-01-14 05:45:46 +00003181 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003182 {
3183 // Merge two pointer types, while trying to preserve typedef info
3184 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3185 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3186 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3187 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003188 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3189 return LHS;
3190 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3191 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003192 return getPointerType(ResultType);
3193 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003194 case Type::BlockPointer:
3195 {
3196 // Merge two block pointer types, while trying to preserve typedef info
3197 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3198 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3199 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3200 if (ResultType.isNull()) return QualType();
3201 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3202 return LHS;
3203 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3204 return RHS;
3205 return getBlockPointerType(ResultType);
3206 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003207 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003208 {
3209 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3210 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3211 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3212 return QualType();
3213
3214 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3215 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3216 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3217 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003218 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3219 return LHS;
3220 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3221 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003222 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3223 ArrayType::ArraySizeModifier(), 0);
3224 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3225 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003226 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3227 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003228 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3229 return LHS;
3230 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3231 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003232 if (LVAT) {
3233 // FIXME: This isn't correct! But tricky to implement because
3234 // the array's size has to be the size of LHS, but the type
3235 // has to be different.
3236 return LHS;
3237 }
3238 if (RVAT) {
3239 // FIXME: This isn't correct! But tricky to implement because
3240 // the array's size has to be the size of RHS, but the type
3241 // has to be different.
3242 return RHS;
3243 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003244 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3245 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003246 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003247 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003248 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003249 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003250 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003251 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003252 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003253 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3254 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003255 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003256 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003257 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003258 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003259 case Type::Complex:
3260 // Distinct complex types are incompatible.
3261 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003262 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003263 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003264 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3265 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003266 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003267 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003268 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003269 // FIXME: This should be type compatibility, e.g. whether
3270 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003271 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3272 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3273 if (LHSIface && RHSIface &&
3274 canAssignObjCInterfaces(LHSIface, RHSIface))
3275 return LHS;
3276
Eli Friedman3d815e72008-08-22 00:56:42 +00003277 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003278 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003279 case Type::ObjCQualifiedId:
3280 // Distinct qualified id's are not compatible.
3281 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003282 case Type::FixedWidthInt:
3283 // Distinct fixed-width integers are not compatible.
3284 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003285 case Type::ExtQual:
3286 // FIXME: ExtQual types can be compatible even if they're not
3287 // identical!
3288 return QualType();
3289 // First attempt at an implementation, but I'm not really sure it's
3290 // right...
3291#if 0
3292 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3293 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3294 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3295 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3296 return QualType();
3297 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3298 LHSBase = QualType(LQual->getBaseType(), 0);
3299 RHSBase = QualType(RQual->getBaseType(), 0);
3300 ResultType = mergeTypes(LHSBase, RHSBase);
3301 if (ResultType.isNull()) return QualType();
3302 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3303 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3304 return LHS;
3305 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3306 return RHS;
3307 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3308 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3309 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3310 return ResultType;
3311#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003312
3313 case Type::TemplateSpecialization:
3314 assert(false && "Dependent types have no size");
3315 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003316 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003317
3318 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003319}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003320
Chris Lattner5426bf62008-04-07 07:01:58 +00003321//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003322// Integer Predicates
3323//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003324
Eli Friedmanad74a752008-06-28 06:23:08 +00003325unsigned ASTContext::getIntWidth(QualType T) {
3326 if (T == BoolTy)
3327 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003328 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3329 return FWIT->getWidth();
3330 }
3331 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003332 return (unsigned)getTypeSize(T);
3333}
3334
3335QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3336 assert(T->isSignedIntegerType() && "Unexpected type");
3337 if (const EnumType* ETy = T->getAsEnumType())
3338 T = ETy->getDecl()->getIntegerType();
3339 const BuiltinType* BTy = T->getAsBuiltinType();
3340 assert (BTy && "Unexpected signed integer type");
3341 switch (BTy->getKind()) {
3342 case BuiltinType::Char_S:
3343 case BuiltinType::SChar:
3344 return UnsignedCharTy;
3345 case BuiltinType::Short:
3346 return UnsignedShortTy;
3347 case BuiltinType::Int:
3348 return UnsignedIntTy;
3349 case BuiltinType::Long:
3350 return UnsignedLongTy;
3351 case BuiltinType::LongLong:
3352 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00003353 case BuiltinType::Int128:
3354 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00003355 default:
3356 assert(0 && "Unexpected signed integer type");
3357 return QualType();
3358 }
3359}
3360
Douglas Gregor2cf26342009-04-09 22:27:44 +00003361ExternalASTSource::~ExternalASTSource() { }
3362
3363void ExternalASTSource::PrintStats() { }