blob: 3466d7040d4716c66b657ffd39e7a36ec986128e [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 Gregor2deaea32009-04-22 18:49:13 +000044 if (InitializeBuiltins)
45 this->InitializeBuiltins(idents);
Daniel Dunbare91593e2008-08-11 04:54:23 +000046}
47
Reid Spencer5f016e22007-07-11 17:01:13 +000048ASTContext::~ASTContext() {
49 // Deallocate all the types.
50 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000051 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000052 Types.pop_back();
53 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000054
Nuno Lopesb74668e2008-12-17 22:30:25 +000055 {
56 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
57 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
58 while (I != E) {
59 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
60 delete R;
61 }
62 }
63
64 {
65 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
66 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
67 while (I != E) {
68 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
69 delete R;
70 }
71 }
72
73 {
Chris Lattner23499252009-03-31 09:24:30 +000074 llvm::DenseMap<const ObjCInterfaceDecl*, RecordDecl*>::iterator
Nuno Lopesb74668e2008-12-17 22:30:25 +000075 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
76 while (I != E) {
Chris Lattner23499252009-03-31 09:24:30 +000077 RecordDecl *R = (I++)->second;
Nuno Lopesb74668e2008-12-17 22:30:25 +000078 R->Destroy(*this);
79 }
80 }
81
Douglas Gregorab452ba2009-03-26 23:50:42 +000082 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000083 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
84 NNS = NestedNameSpecifiers.begin(),
85 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregore7dcd782009-03-27 23:25:45 +000086 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000087 /* Increment in loop */)
88 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000089
90 if (GlobalNestedNameSpecifier)
91 GlobalNestedNameSpecifier->Destroy(*this);
92
Eli Friedmanb26153c2008-05-27 03:08:09 +000093 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000094}
95
Douglas Gregor2deaea32009-04-22 18:49:13 +000096void ASTContext::InitializeBuiltins(IdentifierTable &idents) {
97 BuiltinInfo.InitializeTargetBuiltins(Target);
98 BuiltinInfo.InitializeBuiltins(idents, LangOpts.NoBuiltin);
99}
100
Douglas Gregor2cf26342009-04-09 22:27:44 +0000101void
102ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
103 ExternalSource.reset(Source.take());
104}
105
Reid Spencer5f016e22007-07-11 17:01:13 +0000106void ASTContext::PrintStats() const {
107 fprintf(stderr, "*** AST Context Stats:\n");
108 fprintf(stderr, " %d types total.\n", (int)Types.size());
109 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000110 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000111 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
112 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
113
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000115 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
116 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000117 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000118 unsigned NumExtQual = 0;
119
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
121 Type *T = Types[i];
122 if (isa<BuiltinType>(T))
123 ++NumBuiltin;
124 else if (isa<PointerType>(T))
125 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000126 else if (isa<BlockPointerType>(T))
127 ++NumBlockPointer;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000128 else if (isa<LValueReferenceType>(T))
129 ++NumLValueReference;
130 else if (isa<RValueReferenceType>(T))
131 ++NumRValueReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000132 else if (isa<MemberPointerType>(T))
133 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000134 else if (isa<ComplexType>(T))
135 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 else if (isa<ArrayType>(T))
137 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000138 else if (isa<VectorType>(T))
139 ++NumVector;
Douglas Gregor72564e72009-02-26 23:50:07 +0000140 else if (isa<FunctionNoProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 ++NumFunctionNP;
Douglas Gregor72564e72009-02-26 23:50:07 +0000142 else if (isa<FunctionProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 ++NumFunctionP;
144 else if (isa<TypedefType>(T))
145 ++NumTypeName;
146 else if (TagType *TT = dyn_cast<TagType>(T)) {
147 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000148 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000150 case TagDecl::TK_struct: ++NumTagStruct; break;
151 case TagDecl::TK_union: ++NumTagUnion; break;
152 case TagDecl::TK_class: ++NumTagClass; break;
153 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000155 } else if (isa<ObjCInterfaceType>(T))
156 ++NumObjCInterfaces;
157 else if (isa<ObjCQualifiedInterfaceType>(T))
158 ++NumObjCQualifiedInterfaces;
159 else if (isa<ObjCQualifiedIdType>(T))
160 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000161 else if (isa<TypeOfType>(T))
162 ++NumTypeOfTypes;
Douglas Gregor72564e72009-02-26 23:50:07 +0000163 else if (isa<TypeOfExprType>(T))
164 ++NumTypeOfExprTypes;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000165 else if (isa<ExtQualType>(T))
166 ++NumExtQual;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000167 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000168 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 assert(0 && "Unknown type!");
170 }
171 }
172
173 fprintf(stderr, " %d builtin types\n", NumBuiltin);
174 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000175 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000176 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
177 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000178 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000179 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000181 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
183 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
184 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
185 fprintf(stderr, " %d tagged types\n", NumTagged);
186 fprintf(stderr, " %d struct types\n", NumTagStruct);
187 fprintf(stderr, " %d union types\n", NumTagUnion);
188 fprintf(stderr, " %d class types\n", NumTagClass);
189 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000190 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000191 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000192 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000193 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000194 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000195 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor72564e72009-02-26 23:50:07 +0000196 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000197 fprintf(stderr, " %d attribute-qualified types\n", NumExtQual);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000198
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
200 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000201 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000202 NumLValueReference*sizeof(LValueReferenceType)+
203 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000204 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000205 NumFunctionP*sizeof(FunctionProtoType)+
206 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000207 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000208 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)+
209 NumExtQual*sizeof(ExtQualType)));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000210
211 if (ExternalSource.get()) {
212 fprintf(stderr, "\n");
213 ExternalSource->PrintStats();
214 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000215}
216
217
218void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000219 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000220}
221
Reid Spencer5f016e22007-07-11 17:01:13 +0000222void ASTContext::InitBuiltinTypes() {
223 assert(VoidTy.isNull() && "Context reinitialized?");
224
225 // C99 6.2.5p19.
226 InitBuiltinType(VoidTy, BuiltinType::Void);
227
228 // C99 6.2.5p2.
229 InitBuiltinType(BoolTy, BuiltinType::Bool);
230 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000231 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 InitBuiltinType(CharTy, BuiltinType::Char_S);
233 else
234 InitBuiltinType(CharTy, BuiltinType::Char_U);
235 // C99 6.2.5p4.
236 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
237 InitBuiltinType(ShortTy, BuiltinType::Short);
238 InitBuiltinType(IntTy, BuiltinType::Int);
239 InitBuiltinType(LongTy, BuiltinType::Long);
240 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
241
242 // C99 6.2.5p6.
243 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
244 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
245 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
246 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
247 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
248
249 // C99 6.2.5p10.
250 InitBuiltinType(FloatTy, BuiltinType::Float);
251 InitBuiltinType(DoubleTy, BuiltinType::Double);
252 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000253
Chris Lattner3a250322009-02-26 23:43:47 +0000254 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
255 InitBuiltinType(WCharTy, BuiltinType::WChar);
256 else // C99
257 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000258
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000259 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000260 InitBuiltinType(OverloadTy, BuiltinType::Overload);
261
262 // Placeholder type for type-dependent expressions whose type is
263 // completely unknown. No code should ever check a type against
264 // DependentTy and users should never see it; however, it is here to
265 // help diagnose failures to properly check for type-dependent
266 // expressions.
267 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000268
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 // C99 6.2.5p11.
270 FloatComplexTy = getComplexType(FloatTy);
271 DoubleComplexTy = getComplexType(DoubleTy);
272 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000273
Steve Naroff7e219e42007-10-15 14:41:52 +0000274 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000275 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000276 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000277 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000278 ClassStructType = 0;
279
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000280 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000281
282 // void * type
283 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000284}
285
Chris Lattner464175b2007-07-18 17:52:12 +0000286//===----------------------------------------------------------------------===//
287// Type Sizing and Analysis
288//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000289
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000290/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
291/// scalar floating point type.
292const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
293 const BuiltinType *BT = T->getAsBuiltinType();
294 assert(BT && "Not a floating point type!");
295 switch (BT->getKind()) {
296 default: assert(0 && "Not a floating point type!");
297 case BuiltinType::Float: return Target.getFloatFormat();
298 case BuiltinType::Double: return Target.getDoubleFormat();
299 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
300 }
301}
302
Chris Lattneraf707ab2009-01-24 21:53:27 +0000303/// getDeclAlign - Return a conservative estimate of the alignment of the
304/// specified decl. Note that bitfields do not have a valid alignment, so
305/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000306unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000307 unsigned Align = Target.getCharWidth();
308
309 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
310 Align = std::max(Align, AA->getAlignment());
311
Chris Lattneraf707ab2009-01-24 21:53:27 +0000312 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
313 QualType T = VD->getType();
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000314 if (const ReferenceType* RT = T->getAsReferenceType()) {
315 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000316 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000317 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
318 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000319 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
320 T = cast<ArrayType>(T)->getElementType();
321
322 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
323 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000324 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000325
326 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000327}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000328
Chris Lattnera7674d82007-07-13 22:13:22 +0000329/// getTypeSize - Return the size of the specified type, in bits. This method
330/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000331std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000332ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000333 T = getCanonicalType(T);
Mike Stump5e301002009-02-27 18:32:39 +0000334 uint64_t Width=0;
335 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000336 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000337#define TYPE(Class, Base)
338#define ABSTRACT_TYPE(Class, Base)
339#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
340#define DEPENDENT_TYPE(Class, Base) case Type::Class:
341#include "clang/AST/TypeNodes.def"
342 assert(false && "Should not see non-canonical or dependent types");
343 break;
344
Chris Lattner692233e2007-07-13 22:27:08 +0000345 case Type::FunctionNoProto:
346 case Type::FunctionProto:
Douglas Gregor72564e72009-02-26 23:50:07 +0000347 case Type::IncompleteArray:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000348 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000349 case Type::VariableArray:
350 assert(0 && "VLAs not implemented yet!");
351 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000352 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000353
Chris Lattner98be4942008-03-05 18:54:05 +0000354 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000355 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000356 Align = EltInfo.second;
357 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000358 }
Nate Begeman213541a2008-04-18 23:10:10 +0000359 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000360 case Type::Vector: {
361 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000362 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000363 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000364 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000365 // If the alignment is not a power of 2, round up to the next power of 2.
366 // This happens for non-power-of-2 length vectors.
367 // FIXME: this should probably be a target property.
368 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000369 break;
370 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000371
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000372 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000373 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000374 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000375 case BuiltinType::Void:
376 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000377 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000378 Width = Target.getBoolWidth();
379 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000380 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000381 case BuiltinType::Char_S:
382 case BuiltinType::Char_U:
383 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000384 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000385 Width = Target.getCharWidth();
386 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000387 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000388 case BuiltinType::WChar:
389 Width = Target.getWCharWidth();
390 Align = Target.getWCharAlign();
391 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000392 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000393 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000394 Width = Target.getShortWidth();
395 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000396 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000397 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000398 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000399 Width = Target.getIntWidth();
400 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000401 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000402 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000403 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000404 Width = Target.getLongWidth();
405 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000406 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000407 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000408 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000409 Width = Target.getLongLongWidth();
410 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000411 break;
412 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000413 Width = Target.getFloatWidth();
414 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000415 break;
416 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000417 Width = Target.getDoubleWidth();
418 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000419 break;
420 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000421 Width = Target.getLongDoubleWidth();
422 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000423 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000424 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000425 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000426 case Type::FixedWidthInt:
427 // FIXME: This isn't precisely correct; the width/alignment should depend
428 // on the available types for the target
429 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000430 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000431 Align = Width;
432 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000433 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000434 // FIXME: Pointers into different addr spaces could have different sizes and
435 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000436 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000437 case Type::ObjCQualifiedId:
Douglas Gregor72564e72009-02-26 23:50:07 +0000438 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000439 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000440 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000441 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000442 case Type::BlockPointer: {
443 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
444 Width = Target.getPointerWidth(AS);
445 Align = Target.getPointerAlign(AS);
446 break;
447 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000448 case Type::Pointer: {
449 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000450 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000451 Align = Target.getPointerAlign(AS);
452 break;
453 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000454 case Type::LValueReference:
455 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000456 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000457 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000458 // FIXME: This is wrong for struct layout: a reference in a struct has
459 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000460 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000461 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000462 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000463 // the GCC ABI, where pointers to data are one pointer large, pointers to
464 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000465 // other compilers too, we need to delegate this completely to TargetInfo
466 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000467 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
468 unsigned AS = Pointee.getAddressSpace();
469 Width = Target.getPointerWidth(AS);
470 if (Pointee->isFunctionType())
471 Width *= 2;
472 Align = Target.getPointerAlign(AS);
473 // GCC aligns at single pointer width.
474 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000475 case Type::Complex: {
476 // Complex types have the same alignment as their elements, but twice the
477 // size.
478 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000479 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000480 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000481 Align = EltInfo.second;
482 break;
483 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000484 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000485 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000486 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
487 Width = Layout.getSize();
488 Align = Layout.getAlignment();
489 break;
490 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000491 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000492 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000493 const TagType *TT = cast<TagType>(T);
494
495 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000496 Width = 1;
497 Align = 1;
498 break;
499 }
500
Daniel Dunbar1d751182008-11-08 05:48:37 +0000501 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000502 return getTypeInfo(ET->getDecl()->getIntegerType());
503
Daniel Dunbar1d751182008-11-08 05:48:37 +0000504 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000505 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
506 Width = Layout.getSize();
507 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000508 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000509 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000510
511 case Type::TemplateSpecialization:
512 assert(false && "Dependent types have no size");
513 break;
Chris Lattner71763312008-04-06 22:05:18 +0000514 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000515
Chris Lattner464175b2007-07-18 17:52:12 +0000516 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000517 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000518}
519
Chris Lattner34ebde42009-01-27 18:08:34 +0000520/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
521/// type for the current target in bits. This can be different than the ABI
522/// alignment in cases where it is beneficial for performance to overalign
523/// a data type.
524unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
525 unsigned ABIAlign = getTypeAlign(T);
526
527 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000528 if (T->isSpecificBuiltinType(BuiltinType::Double))
529 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000530
531 return ABIAlign;
532}
533
534
Devang Patel8b277042008-06-04 21:22:16 +0000535/// LayoutField - Field layout.
536void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000537 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000538 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000539 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000540 uint64_t FieldOffset = IsUnion ? 0 : Size;
541 uint64_t FieldSize;
542 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000543
544 // FIXME: Should this override struct packing? Probably we want to
545 // take the minimum?
546 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
547 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000548
549 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
550 // TODO: Need to check this algorithm on other targets!
551 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000552 FieldSize =
553 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000554
555 std::pair<uint64_t, unsigned> FieldInfo =
556 Context.getTypeInfo(FD->getType());
557 uint64_t TypeSize = FieldInfo.first;
558
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000559 // Determine the alignment of this bitfield. The packing
560 // attributes define a maximum and the alignment attribute defines
561 // a minimum.
562 // FIXME: What is the right behavior when the specified alignment
563 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000564 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000565 if (FieldPacking)
566 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000567 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
568 FieldAlign = std::max(FieldAlign, AA->getAlignment());
569
570 // Check if we need to add padding to give the field the correct
571 // alignment.
572 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
573 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
574
575 // Padding members don't affect overall alignment
576 if (!FD->getIdentifier())
577 FieldAlign = 1;
578 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000579 if (FD->getType()->isIncompleteArrayType()) {
580 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000581 // query getTypeInfo about these, so we figure it out here.
582 // Flexible array members don't have any size, but they
583 // have to be aligned appropriately for their element type.
584 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000585 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000586 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson2f1169f2009-04-10 05:31:15 +0000587 } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
588 unsigned AS = RT->getPointeeType().getAddressSpace();
589 FieldSize = Context.Target.getPointerWidth(AS);
590 FieldAlign = Context.Target.getPointerAlign(AS);
Devang Patel8b277042008-06-04 21:22:16 +0000591 } else {
592 std::pair<uint64_t, unsigned> FieldInfo =
593 Context.getTypeInfo(FD->getType());
594 FieldSize = FieldInfo.first;
595 FieldAlign = FieldInfo.second;
596 }
597
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000598 // Determine the alignment of this bitfield. The packing
599 // attributes define a maximum and the alignment attribute defines
600 // a minimum. Additionally, the packing alignment must be at least
601 // a byte for non-bitfields.
602 //
603 // FIXME: What is the right behavior when the specified alignment
604 // is smaller than the specified packing?
605 if (FieldPacking)
606 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000607 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
608 FieldAlign = std::max(FieldAlign, AA->getAlignment());
609
610 // Round up the current record size to the field's alignment boundary.
611 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
612 }
613
614 // Place this field at the current location.
615 FieldOffsets[FieldNo] = FieldOffset;
616
617 // Reserve space for this field.
618 if (IsUnion) {
619 Size = std::max(Size, FieldSize);
620 } else {
621 Size = FieldOffset + FieldSize;
622 }
623
624 // Remember max struct/class alignment.
625 Alignment = std::max(Alignment, FieldAlign);
626}
627
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000628static void CollectLocalObjCIvars(ASTContext *Ctx,
629 const ObjCInterfaceDecl *OI,
630 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000631 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
632 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000633 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000634 if (!IVDecl->isInvalidDecl())
635 Fields.push_back(cast<FieldDecl>(IVDecl));
636 }
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +0000637 // look into properties.
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000638 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*Ctx),
639 E = OI->prop_end(*Ctx); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000640 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +0000641 Fields.push_back(cast<FieldDecl>(IV));
642 }
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000643}
644
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000645void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
646 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
647 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
648 CollectObjCIvars(SuperClass, Fields);
649 CollectLocalObjCIvars(this, OI, Fields);
650}
651
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000652/// addRecordToClass - produces record info. for the class for its
653/// ivars and all those inherited.
654///
Chris Lattnerf1690852009-03-31 08:48:01 +0000655const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D) {
Daniel Dunbar75da6742009-04-22 10:56:29 +0000656 assert(!D->isForwardDecl() && "Invalid decl!");
657
Chris Lattner23499252009-03-31 09:24:30 +0000658 RecordDecl *&RD = ASTRecordForInterface[D];
Daniel Dunbar75da6742009-04-22 10:56:29 +0000659 if (RD)
660 return RD;
Chris Lattnerf1690852009-03-31 08:48:01 +0000661
662 llvm::SmallVector<FieldDecl*, 32> RecFields;
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000663 CollectLocalObjCIvars(this, D, RecFields);
Chris Lattner23499252009-03-31 09:24:30 +0000664
Daniel Dunbar75da6742009-04-22 10:56:29 +0000665 RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, D->getLocation(),
666 D->getIdentifier());
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000667 const RecordDecl *SRD;
668 if (const ObjCInterfaceDecl *SuperClass = D->getSuperClass()) {
669 SRD = addRecordToClass(SuperClass);
670 } else {
671 SRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, SourceLocation(), 0);
672 const_cast<RecordDecl*>(SRD)->completeDefinition(*this);
673 }
674
675 RD->addDecl(*this,
676 FieldDecl::Create(*this, RD,
677 SourceLocation(),
678 0,
679 getTagDeclType(const_cast<RecordDecl*>(SRD)),
680 0, false));
681
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000682 /// FIXME! Can do collection of ivars and adding to the record while
683 /// doing it.
Chris Lattner16ff7052009-03-31 08:58:42 +0000684 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000685 RD->addDecl(*this,
686 FieldDecl::Create(*this, RD,
Chris Lattner23499252009-03-31 09:24:30 +0000687 RecFields[i]->getLocation(),
688 RecFields[i]->getIdentifier(),
689 RecFields[i]->getType(),
690 RecFields[i]->getBitWidth(), false));
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000691 }
Chris Lattnerf1690852009-03-31 08:48:01 +0000692
Chris Lattner23499252009-03-31 09:24:30 +0000693 RD->completeDefinition(*this);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000694 return RD;
695}
Devang Patel44a3dde2008-06-04 21:54:36 +0000696
Chris Lattner61710852008-10-05 17:34:18 +0000697/// getASTObjcInterfaceLayout - Get or compute information about the layout of
698/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000699/// position information.
700const ASTRecordLayout &
701ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
702 // Look up this layout, if already laid out, return what we have.
703 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
704 if (Entry) return *Entry;
705
706 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
707 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000708 ASTRecordLayout *NewEntry = NULL;
Fariborz Jahanian0c7ce5b2009-04-08 21:54:52 +0000709 // FIXME. Add actual count of synthesized ivars, instead of count
710 // of properties which is the upper bound, but is safe.
Daniel Dunbar4af44122009-04-08 20:18:15 +0000711 unsigned FieldCount =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000712 D->ivar_size() + std::distance(D->prop_begin(*this), D->prop_end(*this));
Devang Patel6a5a34c2008-06-06 02:14:01 +0000713 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
714 FieldCount++;
715 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
716 unsigned Alignment = SL.getAlignment();
717 uint64_t Size = SL.getSize();
718 NewEntry = new ASTRecordLayout(Size, Alignment);
719 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000720 // Super class is at the beginning of the layout.
721 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000722 } else {
723 NewEntry = new ASTRecordLayout();
724 NewEntry->InitializeLayout(FieldCount);
725 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000726 Entry = NewEntry;
727
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000728 unsigned StructPacking = 0;
729 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
730 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000731
732 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
733 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
734 AA->getAlignment()));
735
736 // Layout each ivar sequentially.
737 unsigned i = 0;
738 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
739 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
740 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000741 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000742 }
Fariborz Jahanian18191882009-03-31 18:11:23 +0000743 // Also synthesized ivars
Douglas Gregor6ab35242009-04-09 21:40:53 +0000744 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
745 E = D->prop_end(*this); I != E; ++I) {
Fariborz Jahanian18191882009-03-31 18:11:23 +0000746 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
747 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
748 }
Fariborz Jahanian99eee362009-04-01 19:37:34 +0000749
Devang Patel44a3dde2008-06-04 21:54:36 +0000750 // Finally, round the size of the total struct up to the alignment of the
751 // struct itself.
752 NewEntry->FinalizeLayout();
753 return *NewEntry;
754}
755
Devang Patel88a981b2007-11-01 19:11:01 +0000756/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000757/// specified record (struct/union/class), which indicates its size and field
758/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000759const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000760 D = D->getDefinition(*this);
761 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000762
Chris Lattner464175b2007-07-18 17:52:12 +0000763 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000764 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000765 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000766
Devang Patel88a981b2007-11-01 19:11:01 +0000767 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
768 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
769 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000770 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000771
Douglas Gregore267ff32008-12-11 20:41:00 +0000772 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000773 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
774 D->field_end(*this)));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000775 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000776
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000777 unsigned StructPacking = 0;
778 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
779 StructPacking = PA->getAlignment();
780
Eli Friedman4bd998b2008-05-30 09:31:38 +0000781 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000782 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
783 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000784
Eli Friedman4bd998b2008-05-30 09:31:38 +0000785 // Layout each field, for now, just sequentially, respecting alignment. In
786 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000787 unsigned FieldIdx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000788 for (RecordDecl::field_iterator Field = D->field_begin(*this),
789 FieldEnd = D->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000790 Field != FieldEnd; (void)++Field, ++FieldIdx)
791 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000792
793 // Finally, round the size of the total struct up to the alignment of the
794 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000795 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000796 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000797}
798
Chris Lattnera7674d82007-07-13 22:13:22 +0000799//===----------------------------------------------------------------------===//
800// Type creation/memoization methods
801//===----------------------------------------------------------------------===//
802
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000803QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000804 QualType CanT = getCanonicalType(T);
805 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000806 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000807
808 // If we are composing extended qualifiers together, merge together into one
809 // ExtQualType node.
810 unsigned CVRQuals = T.getCVRQualifiers();
811 QualType::GCAttrTypes GCAttr = QualType::GCNone;
812 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000813
Chris Lattnerb7d25532009-02-18 22:53:11 +0000814 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
815 // If this type already has an address space specified, it cannot get
816 // another one.
817 assert(EQT->getAddressSpace() == 0 &&
818 "Type cannot be in multiple addr spaces!");
819 GCAttr = EQT->getObjCGCAttr();
820 TypeNode = EQT->getBaseType();
821 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000822
Chris Lattnerb7d25532009-02-18 22:53:11 +0000823 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000824 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000825 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000826 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000827 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000828 return QualType(EXTQy, CVRQuals);
829
Christopher Lambebb97e92008-02-04 02:31:56 +0000830 // If the base type isn't canonical, this won't be a canonical type either,
831 // so fill in the canonical type field.
832 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000833 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000834 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000835
Chris Lattnerb7d25532009-02-18 22:53:11 +0000836 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000837 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000838 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000839 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000840 ExtQualType *New =
841 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000842 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000843 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000844 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000845}
846
Chris Lattnerb7d25532009-02-18 22:53:11 +0000847QualType ASTContext::getObjCGCQualType(QualType T,
848 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000849 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000850 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000851 return T;
852
Chris Lattnerb7d25532009-02-18 22:53:11 +0000853 // If we are composing extended qualifiers together, merge together into one
854 // ExtQualType node.
855 unsigned CVRQuals = T.getCVRQualifiers();
856 Type *TypeNode = T.getTypePtr();
857 unsigned AddressSpace = 0;
858
859 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
860 // If this type already has an address space specified, it cannot get
861 // another one.
862 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
863 "Type cannot be in multiple addr spaces!");
864 AddressSpace = EQT->getAddressSpace();
865 TypeNode = EQT->getBaseType();
866 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000867
868 // Check if we've already instantiated an gc qual'd type of this type.
869 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000870 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000871 void *InsertPos = 0;
872 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000873 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000874
875 // If the base type isn't canonical, this won't be a canonical type either,
876 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000877 // FIXME: Isn't this also not canonical if the base type is a array
878 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000879 QualType Canonical;
880 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000881 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000882
Chris Lattnerb7d25532009-02-18 22:53:11 +0000883 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000884 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
885 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
886 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000887 ExtQualType *New =
888 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000889 ExtQualTypes.InsertNode(New, InsertPos);
890 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000891 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000892}
Chris Lattnera7674d82007-07-13 22:13:22 +0000893
Reid Spencer5f016e22007-07-11 17:01:13 +0000894/// getComplexType - Return the uniqued reference to the type for a complex
895/// number with the specified element type.
896QualType ASTContext::getComplexType(QualType T) {
897 // Unique pointers, to guarantee there is only one pointer of a particular
898 // structure.
899 llvm::FoldingSetNodeID ID;
900 ComplexType::Profile(ID, T);
901
902 void *InsertPos = 0;
903 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
904 return QualType(CT, 0);
905
906 // If the pointee type isn't canonical, this won't be a canonical type either,
907 // so fill in the canonical type field.
908 QualType Canonical;
909 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000910 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000911
912 // Get the new insert position for the node we care about.
913 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000914 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000915 }
Steve Narofff83820b2009-01-27 22:08:43 +0000916 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000917 Types.push_back(New);
918 ComplexTypes.InsertNode(New, InsertPos);
919 return QualType(New, 0);
920}
921
Eli Friedmanf98aba32009-02-13 02:31:07 +0000922QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
923 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
924 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
925 FixedWidthIntType *&Entry = Map[Width];
926 if (!Entry)
927 Entry = new FixedWidthIntType(Width, Signed);
928 return QualType(Entry, 0);
929}
Reid Spencer5f016e22007-07-11 17:01:13 +0000930
931/// getPointerType - Return the uniqued reference to the type for a pointer to
932/// the specified type.
933QualType ASTContext::getPointerType(QualType T) {
934 // Unique pointers, to guarantee there is only one pointer of a particular
935 // structure.
936 llvm::FoldingSetNodeID ID;
937 PointerType::Profile(ID, T);
938
939 void *InsertPos = 0;
940 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
941 return QualType(PT, 0);
942
943 // If the pointee type isn't canonical, this won't be a canonical type either,
944 // so fill in the canonical type field.
945 QualType Canonical;
946 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000947 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000948
949 // Get the new insert position for the node we care about.
950 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000951 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000952 }
Steve Narofff83820b2009-01-27 22:08:43 +0000953 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000954 Types.push_back(New);
955 PointerTypes.InsertNode(New, InsertPos);
956 return QualType(New, 0);
957}
958
Steve Naroff5618bd42008-08-27 16:04:49 +0000959/// getBlockPointerType - Return the uniqued reference to the type for
960/// a pointer to the specified block.
961QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000962 assert(T->isFunctionType() && "block of function types only");
963 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000964 // structure.
965 llvm::FoldingSetNodeID ID;
966 BlockPointerType::Profile(ID, T);
967
968 void *InsertPos = 0;
969 if (BlockPointerType *PT =
970 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
971 return QualType(PT, 0);
972
Steve Naroff296e8d52008-08-28 19:20:44 +0000973 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000974 // type either so fill in the canonical type field.
975 QualType Canonical;
976 if (!T->isCanonical()) {
977 Canonical = getBlockPointerType(getCanonicalType(T));
978
979 // Get the new insert position for the node we care about.
980 BlockPointerType *NewIP =
981 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000982 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000983 }
Steve Narofff83820b2009-01-27 22:08:43 +0000984 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000985 Types.push_back(New);
986 BlockPointerTypes.InsertNode(New, InsertPos);
987 return QualType(New, 0);
988}
989
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000990/// getLValueReferenceType - Return the uniqued reference to the type for an
991/// lvalue reference to the specified type.
992QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 // Unique pointers, to guarantee there is only one pointer of a particular
994 // structure.
995 llvm::FoldingSetNodeID ID;
996 ReferenceType::Profile(ID, T);
997
998 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000999 if (LValueReferenceType *RT =
1000 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001001 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001002
Reid Spencer5f016e22007-07-11 17:01:13 +00001003 // If the referencee type isn't canonical, this won't be a canonical type
1004 // either, so fill in the canonical type field.
1005 QualType Canonical;
1006 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001007 Canonical = getLValueReferenceType(getCanonicalType(T));
1008
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001010 LValueReferenceType *NewIP =
1011 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001012 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 }
1014
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001015 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001016 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001017 LValueReferenceTypes.InsertNode(New, InsertPos);
1018 return QualType(New, 0);
1019}
1020
1021/// getRValueReferenceType - Return the uniqued reference to the type for an
1022/// rvalue reference to the specified type.
1023QualType ASTContext::getRValueReferenceType(QualType T) {
1024 // Unique pointers, to guarantee there is only one pointer of a particular
1025 // structure.
1026 llvm::FoldingSetNodeID ID;
1027 ReferenceType::Profile(ID, T);
1028
1029 void *InsertPos = 0;
1030 if (RValueReferenceType *RT =
1031 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1032 return QualType(RT, 0);
1033
1034 // If the referencee type isn't canonical, this won't be a canonical type
1035 // either, so fill in the canonical type field.
1036 QualType Canonical;
1037 if (!T->isCanonical()) {
1038 Canonical = getRValueReferenceType(getCanonicalType(T));
1039
1040 // Get the new insert position for the node we care about.
1041 RValueReferenceType *NewIP =
1042 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1043 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1044 }
1045
1046 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1047 Types.push_back(New);
1048 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001049 return QualType(New, 0);
1050}
1051
Sebastian Redlf30208a2009-01-24 21:16:55 +00001052/// getMemberPointerType - Return the uniqued reference to the type for a
1053/// member pointer to the specified type, in the specified class.
1054QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1055{
1056 // Unique pointers, to guarantee there is only one pointer of a particular
1057 // structure.
1058 llvm::FoldingSetNodeID ID;
1059 MemberPointerType::Profile(ID, T, Cls);
1060
1061 void *InsertPos = 0;
1062 if (MemberPointerType *PT =
1063 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1064 return QualType(PT, 0);
1065
1066 // If the pointee or class type isn't canonical, this won't be a canonical
1067 // type either, so fill in the canonical type field.
1068 QualType Canonical;
1069 if (!T->isCanonical()) {
1070 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1071
1072 // Get the new insert position for the node we care about.
1073 MemberPointerType *NewIP =
1074 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1075 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1076 }
Steve Narofff83820b2009-01-27 22:08:43 +00001077 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001078 Types.push_back(New);
1079 MemberPointerTypes.InsertNode(New, InsertPos);
1080 return QualType(New, 0);
1081}
1082
Steve Narofffb22d962007-08-30 01:06:46 +00001083/// getConstantArrayType - Return the unique reference to the type for an
1084/// array of the specified element type.
1085QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +00001086 const llvm::APInt &ArySize,
1087 ArrayType::ArraySizeModifier ASM,
1088 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001089 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001090 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001091
1092 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001093 if (ConstantArrayType *ATP =
1094 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001095 return QualType(ATP, 0);
1096
1097 // If the element type isn't canonical, this won't be a canonical type either,
1098 // so fill in the canonical type field.
1099 QualType Canonical;
1100 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001101 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001102 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001103 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001104 ConstantArrayType *NewIP =
1105 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001106 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001107 }
1108
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001109 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001110 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001111 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001112 Types.push_back(New);
1113 return QualType(New, 0);
1114}
1115
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001116/// getVariableArrayType - Returns a non-unique reference to the type for a
1117/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001118QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1119 ArrayType::ArraySizeModifier ASM,
1120 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001121 // Since we don't unique expressions, it isn't possible to unique VLA's
1122 // that have an expression provided for their size.
1123
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001124 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001125 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001126
1127 VariableArrayTypes.push_back(New);
1128 Types.push_back(New);
1129 return QualType(New, 0);
1130}
1131
Douglas Gregor898574e2008-12-05 23:32:09 +00001132/// getDependentSizedArrayType - Returns a non-unique reference to
1133/// the type for a dependently-sized array of the specified element
1134/// type. FIXME: We will need these to be uniqued, or at least
1135/// comparable, at some point.
1136QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1137 ArrayType::ArraySizeModifier ASM,
1138 unsigned EltTypeQuals) {
1139 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1140 "Size must be type- or value-dependent!");
1141
1142 // Since we don't unique expressions, it isn't possible to unique
1143 // dependently-sized array types.
1144
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001145 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001146 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1147 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001148
1149 DependentSizedArrayTypes.push_back(New);
1150 Types.push_back(New);
1151 return QualType(New, 0);
1152}
1153
Eli Friedmanc5773c42008-02-15 18:16:39 +00001154QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1155 ArrayType::ArraySizeModifier ASM,
1156 unsigned EltTypeQuals) {
1157 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001158 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001159
1160 void *InsertPos = 0;
1161 if (IncompleteArrayType *ATP =
1162 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1163 return QualType(ATP, 0);
1164
1165 // If the element type isn't canonical, this won't be a canonical type
1166 // either, so fill in the canonical type field.
1167 QualType Canonical;
1168
1169 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001170 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001171 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001172
1173 // Get the new insert position for the node we care about.
1174 IncompleteArrayType *NewIP =
1175 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001176 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001177 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001178
Steve Narofff83820b2009-01-27 22:08:43 +00001179 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001180 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001181
1182 IncompleteArrayTypes.InsertNode(New, InsertPos);
1183 Types.push_back(New);
1184 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001185}
1186
Steve Naroff73322922007-07-18 18:00:27 +00001187/// getVectorType - Return the unique reference to a vector type of
1188/// the specified element type and size. VectorType must be a built-in type.
1189QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001190 BuiltinType *baseType;
1191
Chris Lattnerf52ab252008-04-06 22:59:24 +00001192 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001193 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001194
1195 // Check if we've already instantiated a vector of this type.
1196 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001197 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 void *InsertPos = 0;
1199 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1200 return QualType(VTP, 0);
1201
1202 // If the element type isn't canonical, this won't be a canonical type either,
1203 // so fill in the canonical type field.
1204 QualType Canonical;
1205 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001206 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001207
1208 // Get the new insert position for the node we care about.
1209 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001210 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001211 }
Steve Narofff83820b2009-01-27 22:08:43 +00001212 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001213 VectorTypes.InsertNode(New, InsertPos);
1214 Types.push_back(New);
1215 return QualType(New, 0);
1216}
1217
Nate Begeman213541a2008-04-18 23:10:10 +00001218/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001219/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001220QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001221 BuiltinType *baseType;
1222
Chris Lattnerf52ab252008-04-06 22:59:24 +00001223 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001224 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001225
1226 // Check if we've already instantiated a vector of this type.
1227 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001228 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001229 void *InsertPos = 0;
1230 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1231 return QualType(VTP, 0);
1232
1233 // If the element type isn't canonical, this won't be a canonical type either,
1234 // so fill in the canonical type field.
1235 QualType Canonical;
1236 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001237 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001238
1239 // Get the new insert position for the node we care about.
1240 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001241 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001242 }
Steve Narofff83820b2009-01-27 22:08:43 +00001243 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001244 VectorTypes.InsertNode(New, InsertPos);
1245 Types.push_back(New);
1246 return QualType(New, 0);
1247}
1248
Douglas Gregor72564e72009-02-26 23:50:07 +00001249/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001250///
Douglas Gregor72564e72009-02-26 23:50:07 +00001251QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 // Unique functions, to guarantee there is only one function of a particular
1253 // structure.
1254 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001255 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001256
1257 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001258 if (FunctionNoProtoType *FT =
1259 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001260 return QualType(FT, 0);
1261
1262 QualType Canonical;
1263 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001264 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001265
1266 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001267 FunctionNoProtoType *NewIP =
1268 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001269 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 }
1271
Douglas Gregor72564e72009-02-26 23:50:07 +00001272 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001273 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001274 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001275 return QualType(New, 0);
1276}
1277
1278/// getFunctionType - Return a normal function type with a typed argument
1279/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001280QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001281 unsigned NumArgs, bool isVariadic,
1282 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 // Unique functions, to guarantee there is only one function of a particular
1284 // structure.
1285 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001286 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001287 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001288
1289 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001290 if (FunctionProtoType *FTP =
1291 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001292 return QualType(FTP, 0);
1293
1294 // Determine whether the type being created is already canonical or not.
1295 bool isCanonical = ResultTy->isCanonical();
1296 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1297 if (!ArgArray[i]->isCanonical())
1298 isCanonical = false;
1299
1300 // If this type isn't canonical, get the canonical version of it.
1301 QualType Canonical;
1302 if (!isCanonical) {
1303 llvm::SmallVector<QualType, 16> CanonicalArgs;
1304 CanonicalArgs.reserve(NumArgs);
1305 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001306 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001307
Chris Lattnerf52ab252008-04-06 22:59:24 +00001308 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001309 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001310 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001311
1312 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001313 FunctionProtoType *NewIP =
1314 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001315 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001316 }
1317
Douglas Gregor72564e72009-02-26 23:50:07 +00001318 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001319 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001320 FunctionProtoType *FTP =
1321 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001322 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001323 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001324 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001325 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001326 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001327 return QualType(FTP, 0);
1328}
1329
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001330/// getTypeDeclType - Return the unique reference to the type for the
1331/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001332QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001333 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001334 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1335
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001336 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001337 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001338 else if (isa<TemplateTypeParmDecl>(Decl)) {
1339 assert(false && "Template type parameter types are always available.");
1340 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001341 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001342
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001343 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001344 if (PrevDecl)
1345 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001346 else
1347 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001348 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001349 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1350 if (PrevDecl)
1351 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001352 else
1353 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001354 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001355 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001356 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001357
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001358 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001359 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001360}
1361
Reid Spencer5f016e22007-07-11 17:01:13 +00001362/// getTypedefType - Return the unique reference to the type for the
1363/// specified typename decl.
1364QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1365 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1366
Chris Lattnerf52ab252008-04-06 22:59:24 +00001367 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001368 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001369 Types.push_back(Decl->TypeForDecl);
1370 return QualType(Decl->TypeForDecl, 0);
1371}
1372
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001373/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001374/// specified ObjC interface decl.
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001375QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001376 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1377
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001378 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1379 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff3536b442007-09-06 21:24:23 +00001380 Types.push_back(Decl->TypeForDecl);
1381 return QualType(Decl->TypeForDecl, 0);
1382}
1383
Douglas Gregorfab9d672009-02-05 23:33:38 +00001384/// \brief Retrieve the template type parameter type for a template
1385/// parameter with the given depth, index, and (optionally) name.
1386QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1387 IdentifierInfo *Name) {
1388 llvm::FoldingSetNodeID ID;
1389 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1390 void *InsertPos = 0;
1391 TemplateTypeParmType *TypeParm
1392 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1393
1394 if (TypeParm)
1395 return QualType(TypeParm, 0);
1396
1397 if (Name)
1398 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1399 getTemplateTypeParmType(Depth, Index));
1400 else
1401 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1402
1403 Types.push_back(TypeParm);
1404 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1405
1406 return QualType(TypeParm, 0);
1407}
1408
Douglas Gregor55f6b142009-02-09 18:46:07 +00001409QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001410ASTContext::getTemplateSpecializationType(TemplateName Template,
1411 const TemplateArgument *Args,
1412 unsigned NumArgs,
1413 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001414 if (!Canon.isNull())
1415 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001416
Douglas Gregor55f6b142009-02-09 18:46:07 +00001417 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001418 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001419
Douglas Gregor55f6b142009-02-09 18:46:07 +00001420 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001421 TemplateSpecializationType *Spec
1422 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001423
1424 if (Spec)
1425 return QualType(Spec, 0);
1426
Douglas Gregor7532dc62009-03-30 22:58:21 +00001427 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001428 sizeof(TemplateArgument) * NumArgs),
1429 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001430 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001431 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001432 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001433
1434 return QualType(Spec, 0);
1435}
1436
Douglas Gregore4e5b052009-03-19 00:18:19 +00001437QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001438ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001439 QualType NamedType) {
1440 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001441 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001442
1443 void *InsertPos = 0;
1444 QualifiedNameType *T
1445 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1446 if (T)
1447 return QualType(T, 0);
1448
Douglas Gregorab452ba2009-03-26 23:50:42 +00001449 T = new (*this) QualifiedNameType(NNS, NamedType,
1450 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001451 Types.push_back(T);
1452 QualifiedNameTypes.InsertNode(T, InsertPos);
1453 return QualType(T, 0);
1454}
1455
Douglas Gregord57959a2009-03-27 23:10:48 +00001456QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1457 const IdentifierInfo *Name,
1458 QualType Canon) {
1459 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1460
1461 if (Canon.isNull()) {
1462 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1463 if (CanonNNS != NNS)
1464 Canon = getTypenameType(CanonNNS, Name);
1465 }
1466
1467 llvm::FoldingSetNodeID ID;
1468 TypenameType::Profile(ID, NNS, Name);
1469
1470 void *InsertPos = 0;
1471 TypenameType *T
1472 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1473 if (T)
1474 return QualType(T, 0);
1475
1476 T = new (*this) TypenameType(NNS, Name, Canon);
1477 Types.push_back(T);
1478 TypenameTypes.InsertNode(T, InsertPos);
1479 return QualType(T, 0);
1480}
1481
Douglas Gregor17343172009-04-01 00:28:59 +00001482QualType
1483ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1484 const TemplateSpecializationType *TemplateId,
1485 QualType Canon) {
1486 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1487
1488 if (Canon.isNull()) {
1489 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1490 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1491 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1492 const TemplateSpecializationType *CanonTemplateId
1493 = CanonType->getAsTemplateSpecializationType();
1494 assert(CanonTemplateId &&
1495 "Canonical type must also be a template specialization type");
1496 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1497 }
1498 }
1499
1500 llvm::FoldingSetNodeID ID;
1501 TypenameType::Profile(ID, NNS, TemplateId);
1502
1503 void *InsertPos = 0;
1504 TypenameType *T
1505 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1506 if (T)
1507 return QualType(T, 0);
1508
1509 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1510 Types.push_back(T);
1511 TypenameTypes.InsertNode(T, InsertPos);
1512 return QualType(T, 0);
1513}
1514
Chris Lattner88cb27a2008-04-07 04:56:42 +00001515/// CmpProtocolNames - Comparison predicate for sorting protocols
1516/// alphabetically.
1517static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1518 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001519 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001520}
1521
1522static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1523 unsigned &NumProtocols) {
1524 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1525
1526 // Sort protocols, keyed by name.
1527 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1528
1529 // Remove duplicates.
1530 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1531 NumProtocols = ProtocolsEnd-Protocols;
1532}
1533
1534
Chris Lattner065f0d72008-04-07 04:44:08 +00001535/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1536/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001537QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1538 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001539 // Sort the protocol list alphabetically to canonicalize it.
1540 SortAndUniqueProtocols(Protocols, NumProtocols);
1541
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001542 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001543 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001544
1545 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001546 if (ObjCQualifiedInterfaceType *QT =
1547 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001548 return QualType(QT, 0);
1549
1550 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001551 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001552 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001553
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001554 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001555 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001556 return QualType(QType, 0);
1557}
1558
Chris Lattner88cb27a2008-04-07 04:56:42 +00001559/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1560/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001561QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001562 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001563 // Sort the protocol list alphabetically to canonicalize it.
1564 SortAndUniqueProtocols(Protocols, NumProtocols);
1565
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001566 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001567 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001568
1569 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001570 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001571 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001572 return QualType(QT, 0);
1573
1574 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001575 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001576 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001577 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001578 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001579 return QualType(QType, 0);
1580}
1581
Douglas Gregor72564e72009-02-26 23:50:07 +00001582/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1583/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001584/// multiple declarations that refer to "typeof(x)" all contain different
1585/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1586/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001587QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001588 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001589 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001590 Types.push_back(toe);
1591 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001592}
1593
Steve Naroff9752f252007-08-01 18:02:17 +00001594/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1595/// TypeOfType AST's. The only motivation to unique these nodes would be
1596/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1597/// an issue. This doesn't effect the type checker, since it operates
1598/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001599QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001600 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001601 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001602 Types.push_back(tot);
1603 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001604}
1605
Reid Spencer5f016e22007-07-11 17:01:13 +00001606/// getTagDeclType - Return the unique reference to the type for the
1607/// specified TagDecl (struct/union/class/enum) decl.
1608QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001609 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001610 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001611}
1612
1613/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1614/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1615/// needs to agree with the definition in <stddef.h>.
1616QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001617 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001618}
1619
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001620/// getSignedWCharType - Return the type of "signed wchar_t".
1621/// Used when in C++, as a GCC extension.
1622QualType ASTContext::getSignedWCharType() const {
1623 // FIXME: derive from "Target" ?
1624 return WCharTy;
1625}
1626
1627/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1628/// Used when in C++, as a GCC extension.
1629QualType ASTContext::getUnsignedWCharType() const {
1630 // FIXME: derive from "Target" ?
1631 return UnsignedIntTy;
1632}
1633
Chris Lattner8b9023b2007-07-13 03:05:23 +00001634/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1635/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1636QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001637 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001638}
1639
Chris Lattnere6327742008-04-02 05:18:44 +00001640//===----------------------------------------------------------------------===//
1641// Type Operators
1642//===----------------------------------------------------------------------===//
1643
Chris Lattner77c96472008-04-06 22:41:35 +00001644/// getCanonicalType - Return the canonical (structural) type corresponding to
1645/// the specified potentially non-canonical type. The non-canonical version
1646/// of a type may have many "decorated" versions of types. Decorators can
1647/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1648/// to be free of any of these, allowing two canonical types to be compared
1649/// for exact equality with a simple pointer comparison.
1650QualType ASTContext::getCanonicalType(QualType T) {
1651 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001652
1653 // If the result has type qualifiers, make sure to canonicalize them as well.
1654 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1655 if (TypeQuals == 0) return CanType;
1656
1657 // If the type qualifiers are on an array type, get the canonical type of the
1658 // array with the qualifiers applied to the element type.
1659 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1660 if (!AT)
1661 return CanType.getQualifiedType(TypeQuals);
1662
1663 // Get the canonical version of the element with the extra qualifiers on it.
1664 // This can recursively sink qualifiers through multiple levels of arrays.
1665 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1666 NewEltTy = getCanonicalType(NewEltTy);
1667
1668 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1669 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1670 CAT->getIndexTypeQualifier());
1671 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1672 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1673 IAT->getIndexTypeQualifier());
1674
Douglas Gregor898574e2008-12-05 23:32:09 +00001675 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1676 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1677 DSAT->getSizeModifier(),
1678 DSAT->getIndexTypeQualifier());
1679
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001680 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1681 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1682 VAT->getSizeModifier(),
1683 VAT->getIndexTypeQualifier());
1684}
1685
Douglas Gregord57959a2009-03-27 23:10:48 +00001686NestedNameSpecifier *
1687ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1688 if (!NNS)
1689 return 0;
1690
1691 switch (NNS->getKind()) {
1692 case NestedNameSpecifier::Identifier:
1693 // Canonicalize the prefix but keep the identifier the same.
1694 return NestedNameSpecifier::Create(*this,
1695 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1696 NNS->getAsIdentifier());
1697
1698 case NestedNameSpecifier::Namespace:
1699 // A namespace is canonical; build a nested-name-specifier with
1700 // this namespace and no prefix.
1701 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1702
1703 case NestedNameSpecifier::TypeSpec:
1704 case NestedNameSpecifier::TypeSpecWithTemplate: {
1705 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1706 NestedNameSpecifier *Prefix = 0;
1707
1708 // FIXME: This isn't the right check!
1709 if (T->isDependentType())
1710 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1711
1712 return NestedNameSpecifier::Create(*this, Prefix,
1713 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1714 T.getTypePtr());
1715 }
1716
1717 case NestedNameSpecifier::Global:
1718 // The global specifier is canonical and unique.
1719 return NNS;
1720 }
1721
1722 // Required to silence a GCC warning
1723 return 0;
1724}
1725
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001726
1727const ArrayType *ASTContext::getAsArrayType(QualType T) {
1728 // Handle the non-qualified case efficiently.
1729 if (T.getCVRQualifiers() == 0) {
1730 // Handle the common positive case fast.
1731 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1732 return AT;
1733 }
1734
1735 // Handle the common negative case fast, ignoring CVR qualifiers.
1736 QualType CType = T->getCanonicalTypeInternal();
1737
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001738 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001739 // test.
1740 if (!isa<ArrayType>(CType) &&
1741 !isa<ArrayType>(CType.getUnqualifiedType()))
1742 return 0;
1743
1744 // Apply any CVR qualifiers from the array type to the element type. This
1745 // implements C99 6.7.3p8: "If the specification of an array type includes
1746 // any type qualifiers, the element type is so qualified, not the array type."
1747
1748 // If we get here, we either have type qualifiers on the type, or we have
1749 // sugar such as a typedef in the way. If we have type qualifiers on the type
1750 // we must propagate them down into the elemeng type.
1751 unsigned CVRQuals = T.getCVRQualifiers();
1752 unsigned AddrSpace = 0;
1753 Type *Ty = T.getTypePtr();
1754
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001755 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001756 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001757 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1758 AddrSpace = EXTQT->getAddressSpace();
1759 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001760 } else {
1761 T = Ty->getDesugaredType();
1762 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1763 break;
1764 CVRQuals |= T.getCVRQualifiers();
1765 Ty = T.getTypePtr();
1766 }
1767 }
1768
1769 // If we have a simple case, just return now.
1770 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1771 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1772 return ATy;
1773
1774 // Otherwise, we have an array and we have qualifiers on it. Push the
1775 // qualifiers into the array element type and return a new array type.
1776 // Get the canonical version of the element with the extra qualifiers on it.
1777 // This can recursively sink qualifiers through multiple levels of arrays.
1778 QualType NewEltTy = ATy->getElementType();
1779 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001780 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001781 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1782
1783 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1784 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1785 CAT->getSizeModifier(),
1786 CAT->getIndexTypeQualifier()));
1787 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1788 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1789 IAT->getSizeModifier(),
1790 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001791
Douglas Gregor898574e2008-12-05 23:32:09 +00001792 if (const DependentSizedArrayType *DSAT
1793 = dyn_cast<DependentSizedArrayType>(ATy))
1794 return cast<ArrayType>(
1795 getDependentSizedArrayType(NewEltTy,
1796 DSAT->getSizeExpr(),
1797 DSAT->getSizeModifier(),
1798 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001799
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001800 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1801 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1802 VAT->getSizeModifier(),
1803 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001804}
1805
1806
Chris Lattnere6327742008-04-02 05:18:44 +00001807/// getArrayDecayedType - Return the properly qualified result of decaying the
1808/// specified array type to a pointer. This operation is non-trivial when
1809/// handling typedefs etc. The canonical type of "T" must be an array type,
1810/// this returns a pointer to a properly qualified element of the array.
1811///
1812/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1813QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001814 // Get the element type with 'getAsArrayType' so that we don't lose any
1815 // typedefs in the element type of the array. This also handles propagation
1816 // of type qualifiers from the array type into the element type if present
1817 // (C99 6.7.3p8).
1818 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1819 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001820
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001821 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001822
1823 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001824 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001825}
1826
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001827QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001828 QualType ElemTy = VAT->getElementType();
1829
1830 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1831 return getBaseElementType(VAT);
1832
1833 return ElemTy;
1834}
1835
Reid Spencer5f016e22007-07-11 17:01:13 +00001836/// getFloatingRank - Return a relative rank for floating point types.
1837/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001838static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001839 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001840 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001841
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001842 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001843 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001844 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001845 case BuiltinType::Float: return FloatRank;
1846 case BuiltinType::Double: return DoubleRank;
1847 case BuiltinType::LongDouble: return LongDoubleRank;
1848 }
1849}
1850
Steve Naroff716c7302007-08-27 01:41:48 +00001851/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1852/// point or a complex type (based on typeDomain/typeSize).
1853/// 'typeDomain' is a real floating point or complex type.
1854/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001855QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1856 QualType Domain) const {
1857 FloatingRank EltRank = getFloatingRank(Size);
1858 if (Domain->isComplexType()) {
1859 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001860 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001861 case FloatRank: return FloatComplexTy;
1862 case DoubleRank: return DoubleComplexTy;
1863 case LongDoubleRank: return LongDoubleComplexTy;
1864 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001865 }
Chris Lattner1361b112008-04-06 23:58:54 +00001866
1867 assert(Domain->isRealFloatingType() && "Unknown domain!");
1868 switch (EltRank) {
1869 default: assert(0 && "getFloatingRank(): illegal value for rank");
1870 case FloatRank: return FloatTy;
1871 case DoubleRank: return DoubleTy;
1872 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001873 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001874}
1875
Chris Lattner7cfeb082008-04-06 23:55:33 +00001876/// getFloatingTypeOrder - Compare the rank of the two specified floating
1877/// point types, ignoring the domain of the type (i.e. 'double' ==
1878/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1879/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001880int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1881 FloatingRank LHSR = getFloatingRank(LHS);
1882 FloatingRank RHSR = getFloatingRank(RHS);
1883
1884 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001885 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001886 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001887 return 1;
1888 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001889}
1890
Chris Lattnerf52ab252008-04-06 22:59:24 +00001891/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1892/// routine will assert if passed a built-in type that isn't an integer or enum,
1893/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001894unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001895 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001896 if (EnumType* ET = dyn_cast<EnumType>(T))
1897 T = ET->getDecl()->getIntegerType().getTypePtr();
1898
1899 // There are two things which impact the integer rank: the width, and
1900 // the ordering of builtins. The builtin ordering is encoded in the
1901 // bottom three bits; the width is encoded in the bits above that.
1902 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1903 return FWIT->getWidth() << 3;
1904 }
1905
Chris Lattnerf52ab252008-04-06 22:59:24 +00001906 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001907 default: assert(0 && "getIntegerRank(): not a built-in integer");
1908 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001909 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001910 case BuiltinType::Char_S:
1911 case BuiltinType::Char_U:
1912 case BuiltinType::SChar:
1913 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001914 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001915 case BuiltinType::Short:
1916 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001917 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001918 case BuiltinType::Int:
1919 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001920 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001921 case BuiltinType::Long:
1922 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001923 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001924 case BuiltinType::LongLong:
1925 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001926 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001927 }
1928}
1929
Chris Lattner7cfeb082008-04-06 23:55:33 +00001930/// getIntegerTypeOrder - Returns the highest ranked integer type:
1931/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1932/// LHS < RHS, return -1.
1933int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001934 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1935 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001936 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001937
Chris Lattnerf52ab252008-04-06 22:59:24 +00001938 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1939 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001940
Chris Lattner7cfeb082008-04-06 23:55:33 +00001941 unsigned LHSRank = getIntegerRank(LHSC);
1942 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001943
Chris Lattner7cfeb082008-04-06 23:55:33 +00001944 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1945 if (LHSRank == RHSRank) return 0;
1946 return LHSRank > RHSRank ? 1 : -1;
1947 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001948
Chris Lattner7cfeb082008-04-06 23:55:33 +00001949 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1950 if (LHSUnsigned) {
1951 // If the unsigned [LHS] type is larger, return it.
1952 if (LHSRank >= RHSRank)
1953 return 1;
1954
1955 // If the signed type can represent all values of the unsigned type, it
1956 // wins. Because we are dealing with 2's complement and types that are
1957 // powers of two larger than each other, this is always safe.
1958 return -1;
1959 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001960
Chris Lattner7cfeb082008-04-06 23:55:33 +00001961 // If the unsigned [RHS] type is larger, return it.
1962 if (RHSRank >= LHSRank)
1963 return -1;
1964
1965 // If the signed type can represent all values of the unsigned type, it
1966 // wins. Because we are dealing with 2's complement and types that are
1967 // powers of two larger than each other, this is always safe.
1968 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001969}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001970
1971// getCFConstantStringType - Return the type used for constant CFStrings.
1972QualType ASTContext::getCFConstantStringType() {
1973 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001974 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001975 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001976 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001977 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001978
1979 // const int *isa;
1980 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001981 // int flags;
1982 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001983 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001984 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001985 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001986 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001987
Anders Carlsson71993dd2007-08-17 05:31:46 +00001988 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001989 for (unsigned i = 0; i < 4; ++i) {
1990 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1991 SourceLocation(), 0,
1992 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001993 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001994 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001995 }
1996
1997 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001998 }
1999
2000 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002001}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002002
Douglas Gregor319ac892009-04-23 22:29:11 +00002003void ASTContext::setCFConstantStringType(QualType T) {
2004 const RecordType *Rec = T->getAsRecordType();
2005 assert(Rec && "Invalid CFConstantStringType");
2006 CFConstantStringTypeDecl = Rec->getDecl();
2007}
2008
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002009QualType ASTContext::getObjCFastEnumerationStateType()
2010{
2011 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002012 ObjCFastEnumerationStateTypeDecl =
2013 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2014 &Idents.get("__objcFastEnumerationState"));
2015
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002016 QualType FieldTypes[] = {
2017 UnsignedLongTy,
2018 getPointerType(ObjCIdType),
2019 getPointerType(UnsignedLongTy),
2020 getConstantArrayType(UnsignedLongTy,
2021 llvm::APInt(32, 5), ArrayType::Normal, 0)
2022 };
2023
Douglas Gregor44b43212008-12-11 16:49:14 +00002024 for (size_t i = 0; i < 4; ++i) {
2025 FieldDecl *Field = FieldDecl::Create(*this,
2026 ObjCFastEnumerationStateTypeDecl,
2027 SourceLocation(), 0,
2028 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002029 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002030 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002031 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002032
Douglas Gregor44b43212008-12-11 16:49:14 +00002033 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002034 }
2035
2036 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2037}
2038
Douglas Gregor319ac892009-04-23 22:29:11 +00002039void ASTContext::setObjCFastEnumerationStateType(QualType T) {
2040 const RecordType *Rec = T->getAsRecordType();
2041 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2042 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2043}
2044
Anders Carlssone8c49532007-10-29 06:33:42 +00002045// This returns true if a type has been typedefed to BOOL:
2046// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002047static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002048 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002049 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2050 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002051
2052 return false;
2053}
2054
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002055/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002056/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002057int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002058 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002059
2060 // Make all integer and enum types at least as large as an int
2061 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002062 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002063 // Treat arrays as pointers, since that's how they're passed in.
2064 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002065 sz = getTypeSize(VoidPtrTy);
2066 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002067}
2068
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002069/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002070/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002071void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002072 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002073 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002074 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002075 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002076 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002077 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002078 // Compute size of all parameters.
2079 // Start with computing size of a pointer in number of bytes.
2080 // FIXME: There might(should) be a better way of doing this computation!
2081 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002082 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002083 // The first two arguments (self and _cmd) are pointers; account for
2084 // their size.
2085 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002086 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2087 E = Decl->param_end(); PI != E; ++PI) {
2088 QualType PType = (*PI)->getType();
2089 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002090 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002091 ParmOffset += sz;
2092 }
2093 S += llvm::utostr(ParmOffset);
2094 S += "@0:";
2095 S += llvm::utostr(PtrSize);
2096
2097 // Argument types.
2098 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002099 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2100 E = Decl->param_end(); PI != E; ++PI) {
2101 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002102 QualType PType = PVDecl->getOriginalType();
2103 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002104 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2105 // Use array's original type only if it has known number of
2106 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002107 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002108 PType = PVDecl->getType();
2109 } else if (PType->isFunctionType())
2110 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002111 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002112 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002113 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002114 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002115 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002116 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002117 }
2118}
2119
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002120/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002121/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002122/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2123/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002124/// Property attributes are stored as a comma-delimited C string. The simple
2125/// attributes readonly and bycopy are encoded as single characters. The
2126/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2127/// encoded as single characters, followed by an identifier. Property types
2128/// are also encoded as a parametrized attribute. The characters used to encode
2129/// these attributes are defined by the following enumeration:
2130/// @code
2131/// enum PropertyAttributes {
2132/// kPropertyReadOnly = 'R', // property is read-only.
2133/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2134/// kPropertyByref = '&', // property is a reference to the value last assigned
2135/// kPropertyDynamic = 'D', // property is dynamic
2136/// kPropertyGetter = 'G', // followed by getter selector name
2137/// kPropertySetter = 'S', // followed by setter selector name
2138/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2139/// kPropertyType = 't' // followed by old-style type encoding.
2140/// kPropertyWeak = 'W' // 'weak' property
2141/// kPropertyStrong = 'P' // property GC'able
2142/// kPropertyNonAtomic = 'N' // property non-atomic
2143/// };
2144/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002145void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2146 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002147 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002148 // Collect information from the property implementation decl(s).
2149 bool Dynamic = false;
2150 ObjCPropertyImplDecl *SynthesizePID = 0;
2151
2152 // FIXME: Duplicated code due to poor abstraction.
2153 if (Container) {
2154 if (const ObjCCategoryImplDecl *CID =
2155 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2156 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002157 i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2158 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002159 ObjCPropertyImplDecl *PID = *i;
2160 if (PID->getPropertyDecl() == PD) {
2161 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2162 Dynamic = true;
2163 } else {
2164 SynthesizePID = PID;
2165 }
2166 }
2167 }
2168 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002169 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002170 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002171 i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2172 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002173 ObjCPropertyImplDecl *PID = *i;
2174 if (PID->getPropertyDecl() == PD) {
2175 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2176 Dynamic = true;
2177 } else {
2178 SynthesizePID = PID;
2179 }
2180 }
2181 }
2182 }
2183 }
2184
2185 // FIXME: This is not very efficient.
2186 S = "T";
2187
2188 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002189 // GCC has some special rules regarding encoding of properties which
2190 // closely resembles encoding of ivars.
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002191 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002192 true /* outermost type */,
2193 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002194
2195 if (PD->isReadOnly()) {
2196 S += ",R";
2197 } else {
2198 switch (PD->getSetterKind()) {
2199 case ObjCPropertyDecl::Assign: break;
2200 case ObjCPropertyDecl::Copy: S += ",C"; break;
2201 case ObjCPropertyDecl::Retain: S += ",&"; break;
2202 }
2203 }
2204
2205 // It really isn't clear at all what this means, since properties
2206 // are "dynamic by default".
2207 if (Dynamic)
2208 S += ",D";
2209
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002210 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2211 S += ",N";
2212
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002213 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2214 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002215 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002216 }
2217
2218 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2219 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002220 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002221 }
2222
2223 if (SynthesizePID) {
2224 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2225 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002226 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002227 }
2228
2229 // FIXME: OBJCGC: weak & strong
2230}
2231
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002232/// getLegacyIntegralTypeEncoding -
2233/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002234/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002235/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2236///
2237void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2238 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2239 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002240 if (BT->getKind() == BuiltinType::ULong &&
2241 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002242 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002243 else
2244 if (BT->getKind() == BuiltinType::Long &&
2245 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002246 PointeeTy = IntTy;
2247 }
2248 }
2249}
2250
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002251void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002252 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002253 // We follow the behavior of gcc, expanding structures which are
2254 // directly pointed to, and expanding embedded structures. Note that
2255 // these rules are sufficient to prevent recursive encoding of the
2256 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002257 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2258 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002259}
2260
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002261static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002262 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002263 const Expr *E = FD->getBitWidth();
2264 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2265 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2266 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2267 S += 'b';
2268 S += llvm::utostr(N);
2269}
2270
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002271void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2272 bool ExpandPointedToStructures,
2273 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002274 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002275 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002276 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002277 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002278 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002279 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002280 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002281 else {
2282 char encoding;
2283 switch (BT->getKind()) {
2284 default: assert(0 && "Unhandled builtin type kind");
2285 case BuiltinType::Void: encoding = 'v'; break;
2286 case BuiltinType::Bool: encoding = 'B'; break;
2287 case BuiltinType::Char_U:
2288 case BuiltinType::UChar: encoding = 'C'; break;
2289 case BuiltinType::UShort: encoding = 'S'; break;
2290 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002291 case BuiltinType::ULong:
2292 encoding =
2293 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2294 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002295 case BuiltinType::ULongLong: encoding = 'Q'; break;
2296 case BuiltinType::Char_S:
2297 case BuiltinType::SChar: encoding = 'c'; break;
2298 case BuiltinType::Short: encoding = 's'; break;
2299 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002300 case BuiltinType::Long:
2301 encoding =
2302 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2303 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002304 case BuiltinType::LongLong: encoding = 'q'; break;
2305 case BuiltinType::Float: encoding = 'f'; break;
2306 case BuiltinType::Double: encoding = 'd'; break;
2307 case BuiltinType::LongDouble: encoding = 'd'; break;
2308 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002309
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002310 S += encoding;
2311 }
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002312 } else if (const ComplexType *CT = T->getAsComplexType()) {
2313 S += 'j';
2314 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2315 false);
2316 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002317 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2318 ExpandPointedToStructures,
2319 ExpandStructures, FD);
2320 if (FD || EncodingProperty) {
2321 // Note that we do extended encoding of protocol qualifer list
2322 // Only when doing ivar or property encoding.
2323 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2324 S += '"';
2325 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2326 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2327 S += '<';
2328 S += Proto->getNameAsString();
2329 S += '>';
2330 }
2331 S += '"';
2332 }
2333 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002334 }
2335 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002336 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002337 bool isReadOnly = false;
2338 // For historical/compatibility reasons, the read-only qualifier of the
2339 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2340 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2341 // Also, do not emit the 'r' for anything but the outermost type!
2342 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2343 if (OutermostType && T.isConstQualified()) {
2344 isReadOnly = true;
2345 S += 'r';
2346 }
2347 }
2348 else if (OutermostType) {
2349 QualType P = PointeeTy;
2350 while (P->getAsPointerType())
2351 P = P->getAsPointerType()->getPointeeType();
2352 if (P.isConstQualified()) {
2353 isReadOnly = true;
2354 S += 'r';
2355 }
2356 }
2357 if (isReadOnly) {
2358 // Another legacy compatibility encoding. Some ObjC qualifier and type
2359 // combinations need to be rearranged.
2360 // Rewrite "in const" from "nr" to "rn"
2361 const char * s = S.c_str();
2362 int len = S.length();
2363 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2364 std::string replace = "rn";
2365 S.replace(S.end()-2, S.end(), replace);
2366 }
2367 }
Steve Naroff389bf462009-02-12 17:52:19 +00002368 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002369 S += '@';
2370 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002371 }
2372 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002373 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002374 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002375 // Another historical/compatibility reason.
2376 // We encode the underlying type which comes out as
2377 // {...};
2378 S += '^';
2379 getObjCEncodingForTypeImpl(PointeeTy, S,
2380 false, ExpandPointedToStructures,
2381 NULL);
2382 return;
2383 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002384 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002385 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002386 const ObjCInterfaceType *OIT =
2387 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002388 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002389 S += '"';
2390 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002391 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2392 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2393 S += '<';
2394 S += Proto->getNameAsString();
2395 S += '>';
2396 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002397 S += '"';
2398 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002399 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002400 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002401 S += '#';
2402 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002403 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002404 S += ':';
2405 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002406 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002407
2408 if (PointeeTy->isCharType()) {
2409 // char pointer types should be encoded as '*' unless it is a
2410 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002411 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002412 S += '*';
2413 return;
2414 }
2415 }
2416
2417 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002418 getLegacyIntegralTypeEncoding(PointeeTy);
2419
2420 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002421 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002422 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002423 } else if (const ArrayType *AT =
2424 // Ignore type qualifiers etc.
2425 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002426 if (isa<IncompleteArrayType>(AT)) {
2427 // Incomplete arrays are encoded as a pointer to the array element.
2428 S += '^';
2429
2430 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2431 false, ExpandStructures, FD);
2432 } else {
2433 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002434
Anders Carlsson559a8332009-02-22 01:38:57 +00002435 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2436 S += llvm::utostr(CAT->getSize().getZExtValue());
2437 else {
2438 //Variable length arrays are encoded as a regular array with 0 elements.
2439 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2440 S += '0';
2441 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002442
Anders Carlsson559a8332009-02-22 01:38:57 +00002443 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2444 false, ExpandStructures, FD);
2445 S += ']';
2446 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002447 } else if (T->getAsFunctionType()) {
2448 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002449 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002450 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002451 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002452 // Anonymous structures print as '?'
2453 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2454 S += II->getName();
2455 } else {
2456 S += '?';
2457 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002458 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002459 S += '=';
Douglas Gregor6ab35242009-04-09 21:40:53 +00002460 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2461 FieldEnd = RDecl->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +00002462 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002463 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002464 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002465 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002466 S += '"';
2467 }
2468
2469 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002470 if (Field->isBitField()) {
2471 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2472 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002473 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002474 QualType qt = Field->getType();
2475 getLegacyIntegralTypeEncoding(qt);
2476 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002477 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002478 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002479 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002480 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002481 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002482 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002483 if (FD && FD->isBitField())
2484 EncodeBitField(this, S, FD);
2485 else
2486 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002487 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002488 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002489 } else if (T->isObjCInterfaceType()) {
2490 // @encode(class_name)
2491 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2492 S += '{';
2493 const IdentifierInfo *II = OI->getIdentifier();
2494 S += II->getName();
2495 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002496 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002497 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002498 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002499 if (RecFields[i]->isBitField())
2500 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2501 RecFields[i]);
2502 else
2503 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2504 FD);
2505 }
2506 S += '}';
2507 }
2508 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002509 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002510}
2511
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002512void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002513 std::string& S) const {
2514 if (QT & Decl::OBJC_TQ_In)
2515 S += 'n';
2516 if (QT & Decl::OBJC_TQ_Inout)
2517 S += 'N';
2518 if (QT & Decl::OBJC_TQ_Out)
2519 S += 'o';
2520 if (QT & Decl::OBJC_TQ_Bycopy)
2521 S += 'O';
2522 if (QT & Decl::OBJC_TQ_Byref)
2523 S += 'R';
2524 if (QT & Decl::OBJC_TQ_Oneway)
2525 S += 'V';
2526}
2527
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002528void ASTContext::setBuiltinVaListType(QualType T)
2529{
2530 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2531
2532 BuiltinVaListType = T;
2533}
2534
Douglas Gregor319ac892009-04-23 22:29:11 +00002535void ASTContext::setObjCIdType(QualType T)
Steve Naroff7e219e42007-10-15 14:41:52 +00002536{
Douglas Gregor319ac892009-04-23 22:29:11 +00002537 ObjCIdType = T;
2538
2539 const TypedefType *TT = T->getAsTypedefType();
2540 if (!TT)
2541 return;
2542
2543 TypedefDecl *TD = TT->getDecl();
Steve Naroff7e219e42007-10-15 14:41:52 +00002544
2545 // typedef struct objc_object *id;
2546 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002547 // User error - caller will issue diagnostics.
2548 if (!ptr)
2549 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002550 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002551 // User error - caller will issue diagnostics.
2552 if (!rec)
2553 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002554 IdStructType = rec;
2555}
2556
Douglas Gregor319ac892009-04-23 22:29:11 +00002557void ASTContext::setObjCSelType(QualType T)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002558{
Douglas Gregor319ac892009-04-23 22:29:11 +00002559 ObjCSelType = T;
2560
2561 const TypedefType *TT = T->getAsTypedefType();
2562 if (!TT)
2563 return;
2564 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002565
2566 // typedef struct objc_selector *SEL;
2567 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002568 if (!ptr)
2569 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002570 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002571 if (!rec)
2572 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002573 SelStructType = rec;
2574}
2575
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002576void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002577{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002578 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002579}
2580
Douglas Gregor319ac892009-04-23 22:29:11 +00002581void ASTContext::setObjCClassType(QualType T)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002582{
Douglas Gregor319ac892009-04-23 22:29:11 +00002583 ObjCClassType = T;
2584
2585 const TypedefType *TT = T->getAsTypedefType();
2586 if (!TT)
2587 return;
2588 TypedefDecl *TD = TT->getDecl();
Anders Carlsson8baaca52007-10-31 02:53:19 +00002589
2590 // typedef struct objc_class *Class;
2591 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2592 assert(ptr && "'Class' incorrectly typed");
2593 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2594 assert(rec && "'Class' incorrectly typed");
2595 ClassStructType = rec;
2596}
2597
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002598void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2599 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002600 "'NSConstantString' type already set!");
2601
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002602 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002603}
2604
Douglas Gregor7532dc62009-03-30 22:58:21 +00002605/// \brief Retrieve the template name that represents a qualified
2606/// template name such as \c std::vector.
2607TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2608 bool TemplateKeyword,
2609 TemplateDecl *Template) {
2610 llvm::FoldingSetNodeID ID;
2611 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2612
2613 void *InsertPos = 0;
2614 QualifiedTemplateName *QTN =
2615 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2616 if (!QTN) {
2617 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2618 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2619 }
2620
2621 return TemplateName(QTN);
2622}
2623
2624/// \brief Retrieve the template name that represents a dependent
2625/// template name such as \c MetaFun::template apply.
2626TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2627 const IdentifierInfo *Name) {
2628 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2629
2630 llvm::FoldingSetNodeID ID;
2631 DependentTemplateName::Profile(ID, NNS, Name);
2632
2633 void *InsertPos = 0;
2634 DependentTemplateName *QTN =
2635 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2636
2637 if (QTN)
2638 return TemplateName(QTN);
2639
2640 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2641 if (CanonNNS == NNS) {
2642 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2643 } else {
2644 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2645 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2646 }
2647
2648 DependentTemplateNames.InsertNode(QTN, InsertPos);
2649 return TemplateName(QTN);
2650}
2651
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002652/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002653/// TargetInfo, produce the corresponding type. The unsigned @p Type
2654/// is actually a value of type @c TargetInfo::IntType.
2655QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002656 switch (Type) {
2657 case TargetInfo::NoInt: return QualType();
2658 case TargetInfo::SignedShort: return ShortTy;
2659 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2660 case TargetInfo::SignedInt: return IntTy;
2661 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2662 case TargetInfo::SignedLong: return LongTy;
2663 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2664 case TargetInfo::SignedLongLong: return LongLongTy;
2665 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2666 }
2667
2668 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002669 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002670}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002671
2672//===----------------------------------------------------------------------===//
2673// Type Predicates.
2674//===----------------------------------------------------------------------===//
2675
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002676/// isObjCNSObjectType - Return true if this is an NSObject object using
2677/// NSObject attribute on a c-style pointer type.
2678/// FIXME - Make it work directly on types.
2679///
2680bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2681 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2682 if (TypedefDecl *TD = TDT->getDecl())
2683 if (TD->getAttr<ObjCNSObjectAttr>())
2684 return true;
2685 }
2686 return false;
2687}
2688
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002689/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2690/// to an object type. This includes "id" and "Class" (two 'special' pointers
2691/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2692/// ID type).
2693bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002694 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002695 return true;
2696
Steve Naroff6ae98502008-10-21 18:24:04 +00002697 // Blocks are objects.
2698 if (Ty->isBlockPointerType())
2699 return true;
2700
2701 // All other object types are pointers.
Chris Lattner16ede0e2009-04-12 23:51:02 +00002702 const PointerType *PT = Ty->getAsPointerType();
2703 if (PT == 0)
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002704 return false;
2705
Chris Lattner16ede0e2009-04-12 23:51:02 +00002706 // If this a pointer to an interface (e.g. NSString*), it is ok.
2707 if (PT->getPointeeType()->isObjCInterfaceType() ||
2708 // If is has NSObject attribute, OK as well.
2709 isObjCNSObjectType(Ty))
2710 return true;
2711
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002712 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2713 // pointer types. This looks for the typedef specifically, not for the
Chris Lattner16ede0e2009-04-12 23:51:02 +00002714 // underlying type. Iteratively strip off typedefs so that we can handle
2715 // typedefs of typedefs.
2716 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2717 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2718 Ty.getUnqualifiedType() == getObjCClassType())
2719 return true;
2720
2721 Ty = TDT->getDecl()->getUnderlyingType();
2722 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002723
Chris Lattner16ede0e2009-04-12 23:51:02 +00002724 return false;
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002725}
2726
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002727/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2728/// garbage collection attribute.
2729///
2730QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002731 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002732 if (getLangOptions().ObjC1 &&
2733 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002734 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002735 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002736 // (or pointers to them) be treated as though they were declared
2737 // as __strong.
2738 if (GCAttrs == QualType::GCNone) {
2739 if (isObjCObjectPointerType(Ty))
2740 GCAttrs = QualType::Strong;
2741 else if (Ty->isPointerType())
2742 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2743 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00002744 // Non-pointers have none gc'able attribute regardless of the attribute
2745 // set on them.
2746 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2747 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002748 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002749 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002750}
2751
Chris Lattner6ac46a42008-04-07 06:51:04 +00002752//===----------------------------------------------------------------------===//
2753// Type Compatibility Testing
2754//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002755
Steve Naroff1c7d0672008-09-04 15:10:53 +00002756/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002757/// block types. Types must be strictly compatible here. For example,
2758/// C unfortunately doesn't produce an error for the following:
2759///
2760/// int (*emptyArgFunc)();
2761/// int (*intArgList)(int) = emptyArgFunc;
2762///
2763/// For blocks, we will produce an error for the following (similar to C++):
2764///
2765/// int (^emptyArgBlock)();
2766/// int (^intArgBlock)(int) = emptyArgBlock;
2767///
2768/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2769///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002770bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002771 const FunctionType *lbase = lhs->getAsFunctionType();
2772 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002773 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2774 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stumpaab0f7a2009-04-01 01:17:39 +00002775 if (lproto && rproto == 0)
2776 return false;
2777 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff1c7d0672008-09-04 15:10:53 +00002778}
2779
Chris Lattner6ac46a42008-04-07 06:51:04 +00002780/// areCompatVectorTypes - Return true if the two specified vector types are
2781/// compatible.
2782static bool areCompatVectorTypes(const VectorType *LHS,
2783 const VectorType *RHS) {
2784 assert(LHS->isCanonical() && RHS->isCanonical());
2785 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002786 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002787}
2788
Eli Friedman3d815e72008-08-22 00:56:42 +00002789/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002790/// compatible for assignment from RHS to LHS. This handles validation of any
2791/// protocol qualifiers on the LHS or RHS.
2792///
Eli Friedman3d815e72008-08-22 00:56:42 +00002793bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2794 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002795 // Verify that the base decls are compatible: the RHS must be a subclass of
2796 // the LHS.
2797 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2798 return false;
2799
2800 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2801 // protocol qualified at all, then we are good.
2802 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2803 return true;
2804
2805 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2806 // isn't a superset.
2807 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2808 return true; // FIXME: should return false!
2809
2810 // Finally, we must have two protocol-qualified interfaces.
2811 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2812 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002813
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002814 // All LHS protocols must have a presence on the RHS.
2815 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002816
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002817 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2818 LHSPE = LHSP->qual_end();
2819 LHSPI != LHSPE; LHSPI++) {
2820 bool RHSImplementsProtocol = false;
2821
2822 // If the RHS doesn't implement the protocol on the left, the types
2823 // are incompatible.
2824 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2825 RHSPE = RHSP->qual_end();
2826 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2827 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2828 RHSImplementsProtocol = true;
2829 }
2830 // FIXME: For better diagnostics, consider passing back the protocol name.
2831 if (!RHSImplementsProtocol)
2832 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002833 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002834 // The RHS implements all protocols listed on the LHS.
2835 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002836}
2837
Steve Naroff389bf462009-02-12 17:52:19 +00002838bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2839 // get the "pointed to" types
2840 const PointerType *LHSPT = LHS->getAsPointerType();
2841 const PointerType *RHSPT = RHS->getAsPointerType();
2842
2843 if (!LHSPT || !RHSPT)
2844 return false;
2845
2846 QualType lhptee = LHSPT->getPointeeType();
2847 QualType rhptee = RHSPT->getPointeeType();
2848 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2849 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2850 // ID acts sort of like void* for ObjC interfaces
2851 if (LHSIface && isObjCIdStructType(rhptee))
2852 return true;
2853 if (RHSIface && isObjCIdStructType(lhptee))
2854 return true;
2855 if (!LHSIface || !RHSIface)
2856 return false;
2857 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2858 canAssignObjCInterfaces(RHSIface, LHSIface);
2859}
2860
Steve Naroffec0550f2007-10-15 20:41:53 +00002861/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2862/// both shall have the identically qualified version of a compatible type.
2863/// C99 6.2.7p1: Two types have compatible types if their types are the
2864/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002865bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2866 return !mergeTypes(LHS, RHS).isNull();
2867}
2868
2869QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2870 const FunctionType *lbase = lhs->getAsFunctionType();
2871 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002872 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2873 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002874 bool allLTypes = true;
2875 bool allRTypes = true;
2876
2877 // Check return type
2878 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2879 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002880 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2881 allLTypes = false;
2882 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2883 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002884
2885 if (lproto && rproto) { // two C99 style function prototypes
2886 unsigned lproto_nargs = lproto->getNumArgs();
2887 unsigned rproto_nargs = rproto->getNumArgs();
2888
2889 // Compatible functions must have the same number of arguments
2890 if (lproto_nargs != rproto_nargs)
2891 return QualType();
2892
2893 // Variadic and non-variadic functions aren't compatible
2894 if (lproto->isVariadic() != rproto->isVariadic())
2895 return QualType();
2896
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002897 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2898 return QualType();
2899
Eli Friedman3d815e72008-08-22 00:56:42 +00002900 // Check argument compatibility
2901 llvm::SmallVector<QualType, 10> types;
2902 for (unsigned i = 0; i < lproto_nargs; i++) {
2903 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2904 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2905 QualType argtype = mergeTypes(largtype, rargtype);
2906 if (argtype.isNull()) return QualType();
2907 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002908 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2909 allLTypes = false;
2910 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2911 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002912 }
2913 if (allLTypes) return lhs;
2914 if (allRTypes) return rhs;
2915 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002916 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002917 }
2918
2919 if (lproto) allRTypes = false;
2920 if (rproto) allLTypes = false;
2921
Douglas Gregor72564e72009-02-26 23:50:07 +00002922 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002923 if (proto) {
2924 if (proto->isVariadic()) return QualType();
2925 // Check that the types are compatible with the types that
2926 // would result from default argument promotions (C99 6.7.5.3p15).
2927 // The only types actually affected are promotable integer
2928 // types and floats, which would be passed as a different
2929 // type depending on whether the prototype is visible.
2930 unsigned proto_nargs = proto->getNumArgs();
2931 for (unsigned i = 0; i < proto_nargs; ++i) {
2932 QualType argTy = proto->getArgType(i);
2933 if (argTy->isPromotableIntegerType() ||
2934 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2935 return QualType();
2936 }
2937
2938 if (allLTypes) return lhs;
2939 if (allRTypes) return rhs;
2940 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002941 proto->getNumArgs(), lproto->isVariadic(),
2942 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002943 }
2944
2945 if (allLTypes) return lhs;
2946 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002947 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002948}
2949
2950QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002951 // C++ [expr]: If an expression initially has the type "reference to T", the
2952 // type is adjusted to "T" prior to any further analysis, the expression
2953 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002954 // expression is an lvalue unless the reference is an rvalue reference and
2955 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00002956 // FIXME: C++ shouldn't be going through here! The rules are different
2957 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002958 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2959 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00002960 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002961 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002962 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002963 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002964
Eli Friedman3d815e72008-08-22 00:56:42 +00002965 QualType LHSCan = getCanonicalType(LHS),
2966 RHSCan = getCanonicalType(RHS);
2967
2968 // If two types are identical, they are compatible.
2969 if (LHSCan == RHSCan)
2970 return LHS;
2971
2972 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002973 // Note that we handle extended qualifiers later, in the
2974 // case for ExtQualType.
2975 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00002976 return QualType();
2977
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00002978 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
2979 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00002980
Chris Lattner1adb8832008-01-14 05:45:46 +00002981 // We want to consider the two function types to be the same for these
2982 // comparisons, just force one to the other.
2983 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2984 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002985
2986 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002987 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2988 LHSClass = Type::ConstantArray;
2989 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2990 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002991
Nate Begeman213541a2008-04-18 23:10:10 +00002992 // Canonicalize ExtVector -> Vector.
2993 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2994 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002995
Chris Lattnerb0489812008-04-07 06:38:24 +00002996 // Consider qualified interfaces and interfaces the same.
2997 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2998 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002999
Chris Lattnera36a61f2008-04-07 05:43:21 +00003000 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003001 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003002 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3003 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003004
Steve Naroffd824c9c2009-04-14 15:11:46 +00003005 // 'id' and 'Class' act sort of like void* for ObjC interfaces
3006 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003007 return LHS;
Steve Naroffd824c9c2009-04-14 15:11:46 +00003008 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003009 return RHS;
3010
Steve Naroffbc76dd02008-12-10 22:14:21 +00003011 // ID is compatible with all qualified id types.
3012 if (LHS->isObjCQualifiedIdType()) {
3013 if (const PointerType *PT = RHS->getAsPointerType()) {
3014 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003015 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003016 return LHS;
3017 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3018 // Unfortunately, this API is part of Sema (which we don't have access
3019 // to. Need to refactor. The following check is insufficient, since we
3020 // need to make sure the class implements the protocol.
3021 if (pType->isObjCInterfaceType())
3022 return LHS;
3023 }
3024 }
3025 if (RHS->isObjCQualifiedIdType()) {
3026 if (const PointerType *PT = LHS->getAsPointerType()) {
3027 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003028 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003029 return RHS;
3030 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3031 // Unfortunately, this API is part of Sema (which we don't have access
3032 // to. Need to refactor. The following check is insufficient, since we
3033 // need to make sure the class implements the protocol.
3034 if (pType->isObjCInterfaceType())
3035 return RHS;
3036 }
3037 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003038 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3039 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00003040 if (const EnumType* ETy = LHS->getAsEnumType()) {
3041 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3042 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003043 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003044 if (const EnumType* ETy = RHS->getAsEnumType()) {
3045 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3046 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003047 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003048
Eli Friedman3d815e72008-08-22 00:56:42 +00003049 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003050 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003051
Steve Naroff4a746782008-01-09 22:43:08 +00003052 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003053 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003054#define TYPE(Class, Base)
3055#define ABSTRACT_TYPE(Class, Base)
3056#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3057#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3058#include "clang/AST/TypeNodes.def"
3059 assert(false && "Non-canonical and dependent types shouldn't get here");
3060 return QualType();
3061
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003062 case Type::LValueReference:
3063 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003064 case Type::MemberPointer:
3065 assert(false && "C++ should never be in mergeTypes");
3066 return QualType();
3067
3068 case Type::IncompleteArray:
3069 case Type::VariableArray:
3070 case Type::FunctionProto:
3071 case Type::ExtVector:
3072 case Type::ObjCQualifiedInterface:
3073 assert(false && "Types are eliminated above");
3074 return QualType();
3075
Chris Lattner1adb8832008-01-14 05:45:46 +00003076 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003077 {
3078 // Merge two pointer types, while trying to preserve typedef info
3079 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3080 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3081 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3082 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003083 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3084 return LHS;
3085 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3086 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003087 return getPointerType(ResultType);
3088 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003089 case Type::BlockPointer:
3090 {
3091 // Merge two block pointer types, while trying to preserve typedef info
3092 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3093 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3094 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3095 if (ResultType.isNull()) return QualType();
3096 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3097 return LHS;
3098 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3099 return RHS;
3100 return getBlockPointerType(ResultType);
3101 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003102 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003103 {
3104 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3105 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3106 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3107 return QualType();
3108
3109 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3110 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3111 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3112 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003113 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3114 return LHS;
3115 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3116 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003117 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3118 ArrayType::ArraySizeModifier(), 0);
3119 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3120 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003121 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3122 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003123 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3124 return LHS;
3125 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3126 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003127 if (LVAT) {
3128 // FIXME: This isn't correct! But tricky to implement because
3129 // the array's size has to be the size of LHS, but the type
3130 // has to be different.
3131 return LHS;
3132 }
3133 if (RVAT) {
3134 // FIXME: This isn't correct! But tricky to implement because
3135 // the array's size has to be the size of RHS, but the type
3136 // has to be different.
3137 return RHS;
3138 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003139 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3140 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003141 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003142 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003143 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003144 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003145 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003146 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003147 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003148 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3149 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003150 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003151 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003152 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003153 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003154 case Type::Complex:
3155 // Distinct complex types are incompatible.
3156 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003157 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003158 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003159 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3160 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003161 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003162 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003163 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003164 // FIXME: This should be type compatibility, e.g. whether
3165 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003166 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3167 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3168 if (LHSIface && RHSIface &&
3169 canAssignObjCInterfaces(LHSIface, RHSIface))
3170 return LHS;
3171
Eli Friedman3d815e72008-08-22 00:56:42 +00003172 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003173 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003174 case Type::ObjCQualifiedId:
3175 // Distinct qualified id's are not compatible.
3176 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003177 case Type::FixedWidthInt:
3178 // Distinct fixed-width integers are not compatible.
3179 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003180 case Type::ExtQual:
3181 // FIXME: ExtQual types can be compatible even if they're not
3182 // identical!
3183 return QualType();
3184 // First attempt at an implementation, but I'm not really sure it's
3185 // right...
3186#if 0
3187 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3188 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3189 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3190 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3191 return QualType();
3192 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3193 LHSBase = QualType(LQual->getBaseType(), 0);
3194 RHSBase = QualType(RQual->getBaseType(), 0);
3195 ResultType = mergeTypes(LHSBase, RHSBase);
3196 if (ResultType.isNull()) return QualType();
3197 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3198 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3199 return LHS;
3200 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3201 return RHS;
3202 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3203 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3204 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3205 return ResultType;
3206#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003207
3208 case Type::TemplateSpecialization:
3209 assert(false && "Dependent types have no size");
3210 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003211 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003212
3213 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003214}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003215
Chris Lattner5426bf62008-04-07 07:01:58 +00003216//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003217// Integer Predicates
3218//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003219
Eli Friedmanad74a752008-06-28 06:23:08 +00003220unsigned ASTContext::getIntWidth(QualType T) {
3221 if (T == BoolTy)
3222 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003223 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3224 return FWIT->getWidth();
3225 }
3226 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003227 return (unsigned)getTypeSize(T);
3228}
3229
3230QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3231 assert(T->isSignedIntegerType() && "Unexpected type");
3232 if (const EnumType* ETy = T->getAsEnumType())
3233 T = ETy->getDecl()->getIntegerType();
3234 const BuiltinType* BTy = T->getAsBuiltinType();
3235 assert (BTy && "Unexpected signed integer type");
3236 switch (BTy->getKind()) {
3237 case BuiltinType::Char_S:
3238 case BuiltinType::SChar:
3239 return UnsignedCharTy;
3240 case BuiltinType::Short:
3241 return UnsignedShortTy;
3242 case BuiltinType::Int:
3243 return UnsignedIntTy;
3244 case BuiltinType::Long:
3245 return UnsignedLongTy;
3246 case BuiltinType::LongLong:
3247 return UnsignedLongLongTy;
3248 default:
3249 assert(0 && "Unexpected signed integer type");
3250 return QualType();
3251 }
3252}
3253
Douglas Gregor2cf26342009-04-09 22:27:44 +00003254ExternalASTSource::~ExternalASTSource() { }
3255
3256void ExternalASTSource::PrintStats() { }