blob: b36d1f3dbfa37011201039e1fd18c5a830bf378f [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000019#include "clang/AST/ExternalASTSource.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000021#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000023#include "llvm/ADT/StringExtras.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000025#include "llvm/Support/MemoryBuffer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner61710852008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Douglas Gregor2deaea32009-04-22 18:49:13 +000035 bool FreeMem, unsigned size_reserve,
36 bool InitializeBuiltins) :
Douglas Gregorab452ba2009-03-26 23:50:42 +000037 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
38 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Douglas Gregor2cf26342009-04-09 22:27:44 +000039 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels),
40 ExternalSource(0) {
Daniel Dunbare91593e2008-08-11 04:54:23 +000041 if (size_reserve > 0) Types.reserve(size_reserve);
42 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
Douglas Gregor7a9cbed2009-04-26 03:57:37 +000044 BuiltinInfo.InitializeTargetBuiltins(Target);
Douglas Gregor2deaea32009-04-22 18:49:13 +000045 if (InitializeBuiltins)
46 this->InitializeBuiltins(idents);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000047 PrintingPolicy.CPlusPlus = LangOpts.CPlusPlus;
Daniel Dunbare91593e2008-08-11 04:54:23 +000048}
49
Reid Spencer5f016e22007-07-11 17:01:13 +000050ASTContext::~ASTContext() {
51 // Deallocate all the types.
52 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000053 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000054 Types.pop_back();
55 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000056
Nuno Lopesb74668e2008-12-17 22:30:25 +000057 {
58 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
59 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
60 while (I != E) {
61 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
62 delete R;
63 }
64 }
65
66 {
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +000067 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
68 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopesb74668e2008-12-17 22:30:25 +000069 while (I != E) {
70 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
71 delete R;
72 }
73 }
74
Douglas Gregorab452ba2009-03-26 23:50:42 +000075 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000076 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
77 NNS = NestedNameSpecifiers.begin(),
78 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregore7dcd782009-03-27 23:25:45 +000079 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000080 /* Increment in loop */)
81 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000082
83 if (GlobalNestedNameSpecifier)
84 GlobalNestedNameSpecifier->Destroy(*this);
85
Eli Friedmanb26153c2008-05-27 03:08:09 +000086 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000087}
88
Douglas Gregor2deaea32009-04-22 18:49:13 +000089void ASTContext::InitializeBuiltins(IdentifierTable &idents) {
Douglas Gregor2deaea32009-04-22 18:49:13 +000090 BuiltinInfo.InitializeBuiltins(idents, LangOpts.NoBuiltin);
91}
92
Douglas Gregor2cf26342009-04-09 22:27:44 +000093void
94ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
95 ExternalSource.reset(Source.take());
96}
97
Reid Spencer5f016e22007-07-11 17:01:13 +000098void ASTContext::PrintStats() const {
99 fprintf(stderr, "*** AST Context Stats:\n");
100 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000101
Douglas Gregordbe833d2009-05-26 14:40:08 +0000102 unsigned counts[] = {
103#define TYPE(Name, Parent) 0,
104#define ABSTRACT_TYPE(Name, Parent)
105#include "clang/AST/TypeNodes.def"
106 0 // Extra
107 };
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000108
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
110 Type *T = Types[i];
Douglas Gregordbe833d2009-05-26 14:40:08 +0000111 counts[(unsigned)T->getTypeClass()]++;
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 }
113
Douglas Gregordbe833d2009-05-26 14:40:08 +0000114 unsigned Idx = 0;
115 unsigned TotalBytes = 0;
116#define TYPE(Name, Parent) \
117 if (counts[Idx]) \
118 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
119 TotalBytes += counts[Idx] * sizeof(Name##Type); \
120 ++Idx;
121#define ABSTRACT_TYPE(Name, Parent)
122#include "clang/AST/TypeNodes.def"
123
124 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000125
126 if (ExternalSource.get()) {
127 fprintf(stderr, "\n");
128 ExternalSource->PrintStats();
129 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000130}
131
132
133void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000134 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000135}
136
Reid Spencer5f016e22007-07-11 17:01:13 +0000137void ASTContext::InitBuiltinTypes() {
138 assert(VoidTy.isNull() && "Context reinitialized?");
139
140 // C99 6.2.5p19.
141 InitBuiltinType(VoidTy, BuiltinType::Void);
142
143 // C99 6.2.5p2.
144 InitBuiltinType(BoolTy, BuiltinType::Bool);
145 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000146 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 InitBuiltinType(CharTy, BuiltinType::Char_S);
148 else
149 InitBuiltinType(CharTy, BuiltinType::Char_U);
150 // C99 6.2.5p4.
151 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
152 InitBuiltinType(ShortTy, BuiltinType::Short);
153 InitBuiltinType(IntTy, BuiltinType::Int);
154 InitBuiltinType(LongTy, BuiltinType::Long);
155 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
156
157 // C99 6.2.5p6.
158 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
159 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
160 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
161 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
162 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
163
164 // C99 6.2.5p10.
165 InitBuiltinType(FloatTy, BuiltinType::Float);
166 InitBuiltinType(DoubleTy, BuiltinType::Double);
167 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000168
Chris Lattner2df9ced2009-04-30 02:43:43 +0000169 // GNU extension, 128-bit integers.
170 InitBuiltinType(Int128Ty, BuiltinType::Int128);
171 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
172
Chris Lattner3a250322009-02-26 23:43:47 +0000173 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
174 InitBuiltinType(WCharTy, BuiltinType::WChar);
175 else // C99
176 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000177
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000178 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000179 InitBuiltinType(OverloadTy, BuiltinType::Overload);
180
181 // Placeholder type for type-dependent expressions whose type is
182 // completely unknown. No code should ever check a type against
183 // DependentTy and users should never see it; however, it is here to
184 // help diagnose failures to properly check for type-dependent
185 // expressions.
186 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000187
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 // C99 6.2.5p11.
189 FloatComplexTy = getComplexType(FloatTy);
190 DoubleComplexTy = getComplexType(DoubleTy);
191 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000192
Steve Naroff7e219e42007-10-15 14:41:52 +0000193 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000194 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000195 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000196 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000197 ClassStructType = 0;
198
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000199 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000200
201 // void * type
202 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000203
204 // nullptr type (C++0x 2.14.7)
205 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000206}
207
Chris Lattner464175b2007-07-18 17:52:12 +0000208//===----------------------------------------------------------------------===//
209// Type Sizing and Analysis
210//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000211
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000212/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
213/// scalar floating point type.
214const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
215 const BuiltinType *BT = T->getAsBuiltinType();
216 assert(BT && "Not a floating point type!");
217 switch (BT->getKind()) {
218 default: assert(0 && "Not a floating point type!");
219 case BuiltinType::Float: return Target.getFloatFormat();
220 case BuiltinType::Double: return Target.getDoubleFormat();
221 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
222 }
223}
224
Chris Lattneraf707ab2009-01-24 21:53:27 +0000225/// getDeclAlign - Return a conservative estimate of the alignment of the
226/// specified decl. Note that bitfields do not have a valid alignment, so
227/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000228unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000229 unsigned Align = Target.getCharWidth();
230
231 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
232 Align = std::max(Align, AA->getAlignment());
233
Chris Lattneraf707ab2009-01-24 21:53:27 +0000234 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
235 QualType T = VD->getType();
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000236 if (const ReferenceType* RT = T->getAsReferenceType()) {
237 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000238 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000239 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
240 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000241 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
242 T = cast<ArrayType>(T)->getElementType();
243
244 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
245 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000246 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000247
248 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000249}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000250
Chris Lattnera7674d82007-07-13 22:13:22 +0000251/// getTypeSize - Return the size of the specified type, in bits. This method
252/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000253std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000254ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000255 uint64_t Width=0;
256 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000257 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000258#define TYPE(Class, Base)
259#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000260#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000261#define DEPENDENT_TYPE(Class, Base) case Type::Class:
262#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000263 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000264 break;
265
Chris Lattner692233e2007-07-13 22:27:08 +0000266 case Type::FunctionNoProto:
267 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000268 // GCC extension: alignof(function) = 32 bits
269 Width = 0;
270 Align = 32;
271 break;
272
Douglas Gregor72564e72009-02-26 23:50:07 +0000273 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000274 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000275 Width = 0;
276 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
277 break;
278
Steve Narofffb22d962007-08-30 01:06:46 +0000279 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000280 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000281
Chris Lattner98be4942008-03-05 18:54:05 +0000282 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000283 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000284 Align = EltInfo.second;
285 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000286 }
Nate Begeman213541a2008-04-18 23:10:10 +0000287 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000288 case Type::Vector: {
289 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000290 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000291 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000292 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000293 // If the alignment is not a power of 2, round up to the next power of 2.
294 // This happens for non-power-of-2 length vectors.
295 // FIXME: this should probably be a target property.
296 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000297 break;
298 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000299
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000300 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000301 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000302 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000303 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000304 // GCC extension: alignof(void) = 8 bits.
305 Width = 0;
306 Align = 8;
307 break;
308
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000309 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000310 Width = Target.getBoolWidth();
311 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000312 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000313 case BuiltinType::Char_S:
314 case BuiltinType::Char_U:
315 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000316 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000317 Width = Target.getCharWidth();
318 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000319 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000320 case BuiltinType::WChar:
321 Width = Target.getWCharWidth();
322 Align = Target.getWCharAlign();
323 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000324 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000325 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000326 Width = Target.getShortWidth();
327 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000328 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000329 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000330 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000331 Width = Target.getIntWidth();
332 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000333 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000334 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000335 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000336 Width = Target.getLongWidth();
337 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000338 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000339 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000340 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000341 Width = Target.getLongLongWidth();
342 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000343 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000344 case BuiltinType::Int128:
345 case BuiltinType::UInt128:
346 Width = 128;
347 Align = 128; // int128_t is 128-bit aligned on all targets.
348 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000349 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000350 Width = Target.getFloatWidth();
351 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000352 break;
353 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000354 Width = Target.getDoubleWidth();
355 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000356 break;
357 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000358 Width = Target.getLongDoubleWidth();
359 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000360 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000361 case BuiltinType::NullPtr:
362 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
363 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000364 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000365 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000366 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000367 case Type::FixedWidthInt:
368 // FIXME: This isn't precisely correct; the width/alignment should depend
369 // on the available types for the target
370 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000371 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000372 Align = Width;
373 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000374 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000375 // FIXME: Pointers into different addr spaces could have different sizes and
376 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000377 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000378 case Type::ObjCQualifiedId:
Douglas Gregor72564e72009-02-26 23:50:07 +0000379 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000380 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000381 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000382 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000383 case Type::BlockPointer: {
384 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
385 Width = Target.getPointerWidth(AS);
386 Align = Target.getPointerAlign(AS);
387 break;
388 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000389 case Type::Pointer: {
390 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000391 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000392 Align = Target.getPointerAlign(AS);
393 break;
394 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000395 case Type::LValueReference:
396 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000397 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000398 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000399 // FIXME: This is wrong for struct layout: a reference in a struct has
400 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000401 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000402 case Type::MemberPointer: {
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000403 // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
404 // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
405 // If we ever want to support other ABIs this needs to be abstracted.
406
Sebastian Redlf30208a2009-01-24 21:16:55 +0000407 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000408 std::pair<uint64_t, unsigned> PtrDiffInfo =
409 getTypeInfo(getPointerDiffType());
410 Width = PtrDiffInfo.first;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000411 if (Pointee->isFunctionType())
412 Width *= 2;
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000413 Align = PtrDiffInfo.second;
414 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000415 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000416 case Type::Complex: {
417 // Complex types have the same alignment as their elements, but twice the
418 // size.
419 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000420 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000421 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000422 Align = EltInfo.second;
423 break;
424 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000425 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000426 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000427 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
428 Width = Layout.getSize();
429 Align = Layout.getAlignment();
430 break;
431 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000432 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000433 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000434 const TagType *TT = cast<TagType>(T);
435
436 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000437 Width = 1;
438 Align = 1;
439 break;
440 }
441
Daniel Dunbar1d751182008-11-08 05:48:37 +0000442 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000443 return getTypeInfo(ET->getDecl()->getIntegerType());
444
Daniel Dunbar1d751182008-11-08 05:48:37 +0000445 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000446 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
447 Width = Layout.getSize();
448 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000449 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000450 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000451
Douglas Gregor18857642009-04-30 17:32:17 +0000452 case Type::Typedef: {
453 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
454 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
455 Align = Aligned->getAlignment();
456 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
457 } else
458 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000459 break;
Chris Lattner71763312008-04-06 22:05:18 +0000460 }
Douglas Gregor18857642009-04-30 17:32:17 +0000461
462 case Type::TypeOfExpr:
463 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
464 .getTypePtr());
465
466 case Type::TypeOf:
467 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
468
469 case Type::QualifiedName:
470 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
471
472 case Type::TemplateSpecialization:
473 assert(getCanonicalType(T) != T &&
474 "Cannot request the size of a dependent type");
475 // FIXME: this is likely to be wrong once we support template
476 // aliases, since a template alias could refer to a typedef that
477 // has an __aligned__ attribute on it.
478 return getTypeInfo(getCanonicalType(T));
479 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000480
Chris Lattner464175b2007-07-18 17:52:12 +0000481 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000482 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000483}
484
Chris Lattner34ebde42009-01-27 18:08:34 +0000485/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
486/// type for the current target in bits. This can be different than the ABI
487/// alignment in cases where it is beneficial for performance to overalign
488/// a data type.
489unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
490 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman1eed6022009-05-25 21:27:19 +0000491
492 // Double and long long should be naturally aligned if possible.
493 if (const ComplexType* CT = T->getAsComplexType())
494 T = CT->getElementType().getTypePtr();
495 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
496 T->isSpecificBuiltinType(BuiltinType::LongLong))
497 return std::max(ABIAlign, (unsigned)getTypeSize(T));
498
Chris Lattner34ebde42009-01-27 18:08:34 +0000499 return ABIAlign;
500}
501
502
Devang Patel8b277042008-06-04 21:22:16 +0000503/// LayoutField - Field layout.
504void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000505 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000506 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000507 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000508 uint64_t FieldOffset = IsUnion ? 0 : Size;
509 uint64_t FieldSize;
510 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000511
512 // FIXME: Should this override struct packing? Probably we want to
513 // take the minimum?
514 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
515 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000516
517 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
518 // TODO: Need to check this algorithm on other targets!
519 // (tested on Linux-X86)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000520 FieldSize = BitWidthExpr->EvaluateAsInt(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000521
522 std::pair<uint64_t, unsigned> FieldInfo =
523 Context.getTypeInfo(FD->getType());
524 uint64_t TypeSize = FieldInfo.first;
525
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000526 // Determine the alignment of this bitfield. The packing
527 // attributes define a maximum and the alignment attribute defines
528 // a minimum.
529 // FIXME: What is the right behavior when the specified alignment
530 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000531 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000532 if (FieldPacking)
533 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000534 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
535 FieldAlign = std::max(FieldAlign, AA->getAlignment());
536
537 // Check if we need to add padding to give the field the correct
538 // alignment.
539 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
540 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
541
542 // Padding members don't affect overall alignment
543 if (!FD->getIdentifier())
544 FieldAlign = 1;
545 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000546 if (FD->getType()->isIncompleteArrayType()) {
547 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000548 // query getTypeInfo about these, so we figure it out here.
549 // Flexible array members don't have any size, but they
550 // have to be aligned appropriately for their element type.
551 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000552 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000553 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson2f1169f2009-04-10 05:31:15 +0000554 } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
555 unsigned AS = RT->getPointeeType().getAddressSpace();
556 FieldSize = Context.Target.getPointerWidth(AS);
557 FieldAlign = Context.Target.getPointerAlign(AS);
Devang Patel8b277042008-06-04 21:22:16 +0000558 } else {
559 std::pair<uint64_t, unsigned> FieldInfo =
560 Context.getTypeInfo(FD->getType());
561 FieldSize = FieldInfo.first;
562 FieldAlign = FieldInfo.second;
563 }
564
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000565 // Determine the alignment of this bitfield. The packing
566 // attributes define a maximum and the alignment attribute defines
567 // a minimum. Additionally, the packing alignment must be at least
568 // a byte for non-bitfields.
569 //
570 // FIXME: What is the right behavior when the specified alignment
571 // is smaller than the specified packing?
572 if (FieldPacking)
573 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000574 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
575 FieldAlign = std::max(FieldAlign, AA->getAlignment());
576
577 // Round up the current record size to the field's alignment boundary.
578 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
579 }
580
581 // Place this field at the current location.
582 FieldOffsets[FieldNo] = FieldOffset;
583
584 // Reserve space for this field.
585 if (IsUnion) {
586 Size = std::max(Size, FieldSize);
587 } else {
588 Size = FieldOffset + FieldSize;
589 }
590
Daniel Dunbard6884a02009-05-04 05:16:21 +0000591 // Remember the next available offset.
592 NextOffset = Size;
593
Devang Patel8b277042008-06-04 21:22:16 +0000594 // Remember max struct/class alignment.
595 Alignment = std::max(Alignment, FieldAlign);
596}
597
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000598static void CollectLocalObjCIvars(ASTContext *Ctx,
599 const ObjCInterfaceDecl *OI,
600 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000601 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
602 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000603 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000604 if (!IVDecl->isInvalidDecl())
605 Fields.push_back(cast<FieldDecl>(IVDecl));
606 }
607}
608
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000609void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
610 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
611 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
612 CollectObjCIvars(SuperClass, Fields);
613 CollectLocalObjCIvars(this, OI, Fields);
614}
615
Fariborz Jahanian98200742009-05-12 18:14:29 +0000616void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
617 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
618 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(*this),
619 E = PD->prop_end(*this); I != E; ++I)
620 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
621 Ivars.push_back(Ivar);
622
623 // Also look into nested protocols.
624 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
625 E = PD->protocol_end(); P != E; ++P)
626 CollectProtocolSynthesizedIvars(*P, Ivars);
627}
628
629/// CollectSynthesizedIvars -
630/// This routine collect synthesized ivars for the designated class.
631///
632void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
633 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
634 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*this),
635 E = OI->prop_end(*this); I != E; ++I) {
636 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
637 Ivars.push_back(Ivar);
638 }
639 // Also look into interface's protocol list for properties declared
640 // in the protocol and whose ivars are synthesized.
641 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
642 PE = OI->protocol_end(); P != PE; ++P) {
643 ObjCProtocolDecl *PD = (*P);
644 CollectProtocolSynthesizedIvars(PD, Ivars);
645 }
646}
647
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000648/// getInterfaceLayoutImpl - Get or compute information about the
649/// layout of the given interface.
650///
651/// \param Impl - If given, also include the layout of the interface's
652/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000653const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000654ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
655 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000656 assert(!D->isForwardDecl() && "Invalid interface decl!");
657
Devang Patel44a3dde2008-06-04 21:54:36 +0000658 // Look up this layout, if already laid out, return what we have.
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000659 ObjCContainerDecl *Key =
660 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
661 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
662 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000663
Daniel Dunbar453addb2009-05-03 11:16:44 +0000664 unsigned FieldCount = D->ivar_size();
665 // Add in synthesized ivar count if laying out an implementation.
666 if (Impl) {
Fariborz Jahanian98200742009-05-12 18:14:29 +0000667 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
668 CollectSynthesizedIvars(D, Ivars);
669 FieldCount += Ivars.size();
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000670 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +0000671 // entry. Note we can't cache this because we simply free all
672 // entries later; however we shouldn't look up implementations
673 // frequently.
674 if (FieldCount == D->ivar_size())
675 return getObjCLayout(D, 0);
676 }
677
Devang Patel6a5a34c2008-06-06 02:14:01 +0000678 ASTRecordLayout *NewEntry = NULL;
Devang Patel6a5a34c2008-06-06 02:14:01 +0000679 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Devang Patel6a5a34c2008-06-06 02:14:01 +0000680 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
681 unsigned Alignment = SL.getAlignment();
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000682
Daniel Dunbar913af352009-05-07 21:58:26 +0000683 // We start laying out ivars not at the end of the superclass
684 // structure, but at the next byte following the last field.
685 uint64_t Size = llvm::RoundUpToAlignment(SL.NextOffset, 8);
Daniel Dunbard6884a02009-05-04 05:16:21 +0000686
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000687 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout(Size, Alignment);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000688 NewEntry->InitializeLayout(FieldCount);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000689 } else {
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000690 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout();
Devang Patel6a5a34c2008-06-06 02:14:01 +0000691 NewEntry->InitializeLayout(FieldCount);
692 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000693
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000694 unsigned StructPacking = 0;
695 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
696 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000697
698 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
699 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
700 AA->getAlignment()));
701
702 // Layout each ivar sequentially.
703 unsigned i = 0;
704 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
705 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
706 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000707 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000708 }
Daniel Dunbar453addb2009-05-03 11:16:44 +0000709 // And synthesized ivars, if this is an implementation.
710 if (Impl) {
Fariborz Jahanian98200742009-05-12 18:14:29 +0000711 // FIXME. Do we need to colltect twice?
712 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
713 CollectSynthesizedIvars(D, Ivars);
714 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
715 NewEntry->LayoutField(Ivars[k], i++, false, StructPacking, *this);
Fariborz Jahanian18191882009-03-31 18:11:23 +0000716 }
Fariborz Jahanian99eee362009-04-01 19:37:34 +0000717
Devang Patel44a3dde2008-06-04 21:54:36 +0000718 // Finally, round the size of the total struct up to the alignment of the
719 // struct itself.
720 NewEntry->FinalizeLayout();
721 return *NewEntry;
722}
723
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000724const ASTRecordLayout &
725ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
726 return getObjCLayout(D, 0);
727}
728
729const ASTRecordLayout &
730ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
731 return getObjCLayout(D->getClassInterface(), D);
732}
733
Devang Patel88a981b2007-11-01 19:11:01 +0000734/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000735/// specified record (struct/union/class), which indicates its size and field
736/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000737const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000738 D = D->getDefinition(*this);
739 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000740
Chris Lattner464175b2007-07-18 17:52:12 +0000741 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000742 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000743 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000744
Devang Patel88a981b2007-11-01 19:11:01 +0000745 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
746 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
747 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000748 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000749
Douglas Gregore267ff32008-12-11 20:41:00 +0000750 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000751 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
752 D->field_end(*this)));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000753 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000754
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000755 unsigned StructPacking = 0;
756 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
757 StructPacking = PA->getAlignment();
758
Eli Friedman4bd998b2008-05-30 09:31:38 +0000759 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000760 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
761 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000762
Eli Friedman4bd998b2008-05-30 09:31:38 +0000763 // Layout each field, for now, just sequentially, respecting alignment. In
764 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000765 unsigned FieldIdx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000766 for (RecordDecl::field_iterator Field = D->field_begin(*this),
767 FieldEnd = D->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000768 Field != FieldEnd; (void)++Field, ++FieldIdx)
769 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000770
771 // Finally, round the size of the total struct up to the alignment of the
772 // struct itself.
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000773 NewEntry->FinalizeLayout(getLangOptions().CPlusPlus);
Chris Lattner5d2a6302007-07-18 18:26:58 +0000774 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000775}
776
Chris Lattnera7674d82007-07-13 22:13:22 +0000777//===----------------------------------------------------------------------===//
778// Type creation/memoization methods
779//===----------------------------------------------------------------------===//
780
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000781QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000782 QualType CanT = getCanonicalType(T);
783 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000784 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000785
786 // If we are composing extended qualifiers together, merge together into one
787 // ExtQualType node.
788 unsigned CVRQuals = T.getCVRQualifiers();
789 QualType::GCAttrTypes GCAttr = QualType::GCNone;
790 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000791
Chris Lattnerb7d25532009-02-18 22:53:11 +0000792 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
793 // If this type already has an address space specified, it cannot get
794 // another one.
795 assert(EQT->getAddressSpace() == 0 &&
796 "Type cannot be in multiple addr spaces!");
797 GCAttr = EQT->getObjCGCAttr();
798 TypeNode = EQT->getBaseType();
799 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000800
Chris Lattnerb7d25532009-02-18 22:53:11 +0000801 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000802 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000803 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000804 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000805 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000806 return QualType(EXTQy, CVRQuals);
807
Christopher Lambebb97e92008-02-04 02:31:56 +0000808 // If the base type isn't canonical, this won't be a canonical type either,
809 // so fill in the canonical type field.
810 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000811 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000812 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000813
Chris Lattnerb7d25532009-02-18 22:53:11 +0000814 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000815 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000816 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000817 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000818 ExtQualType *New =
819 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000820 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000821 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000822 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000823}
824
Chris Lattnerb7d25532009-02-18 22:53:11 +0000825QualType ASTContext::getObjCGCQualType(QualType T,
826 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000827 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000828 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000829 return T;
830
Fariborz Jahanian4027cd12009-06-03 17:15:17 +0000831 if (T->isPointerType()) {
832 QualType Pointee = T->getAsPointerType()->getPointeeType();
833 if (Pointee->isPointerType()) {
834 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
835 return getPointerType(ResultType);
836 }
837 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000838 // If we are composing extended qualifiers together, merge together into one
839 // ExtQualType node.
840 unsigned CVRQuals = T.getCVRQualifiers();
841 Type *TypeNode = T.getTypePtr();
842 unsigned AddressSpace = 0;
843
844 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
845 // If this type already has an address space specified, it cannot get
846 // another one.
847 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
848 "Type cannot be in multiple addr spaces!");
849 AddressSpace = EQT->getAddressSpace();
850 TypeNode = EQT->getBaseType();
851 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000852
853 // Check if we've already instantiated an gc qual'd type of this type.
854 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000855 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000856 void *InsertPos = 0;
857 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000858 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000859
860 // If the base type isn't canonical, this won't be a canonical type either,
861 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000862 // FIXME: Isn't this also not canonical if the base type is a array
863 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000864 QualType Canonical;
865 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000866 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000867
Chris Lattnerb7d25532009-02-18 22:53:11 +0000868 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000869 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
870 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
871 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000872 ExtQualType *New =
873 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000874 ExtQualTypes.InsertNode(New, InsertPos);
875 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000876 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000877}
Chris Lattnera7674d82007-07-13 22:13:22 +0000878
Reid Spencer5f016e22007-07-11 17:01:13 +0000879/// getComplexType - Return the uniqued reference to the type for a complex
880/// number with the specified element type.
881QualType ASTContext::getComplexType(QualType T) {
882 // Unique pointers, to guarantee there is only one pointer of a particular
883 // structure.
884 llvm::FoldingSetNodeID ID;
885 ComplexType::Profile(ID, T);
886
887 void *InsertPos = 0;
888 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
889 return QualType(CT, 0);
890
891 // If the pointee type isn't canonical, this won't be a canonical type either,
892 // so fill in the canonical type field.
893 QualType Canonical;
894 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000895 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000896
897 // Get the new insert position for the node we care about.
898 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000899 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000900 }
Steve Narofff83820b2009-01-27 22:08:43 +0000901 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000902 Types.push_back(New);
903 ComplexTypes.InsertNode(New, InsertPos);
904 return QualType(New, 0);
905}
906
Eli Friedmanf98aba32009-02-13 02:31:07 +0000907QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
908 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
909 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
910 FixedWidthIntType *&Entry = Map[Width];
911 if (!Entry)
912 Entry = new FixedWidthIntType(Width, Signed);
913 return QualType(Entry, 0);
914}
Reid Spencer5f016e22007-07-11 17:01:13 +0000915
916/// getPointerType - Return the uniqued reference to the type for a pointer to
917/// the specified type.
918QualType ASTContext::getPointerType(QualType T) {
919 // Unique pointers, to guarantee there is only one pointer of a particular
920 // structure.
921 llvm::FoldingSetNodeID ID;
922 PointerType::Profile(ID, T);
923
924 void *InsertPos = 0;
925 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
926 return QualType(PT, 0);
927
928 // If the pointee type isn't canonical, this won't be a canonical type either,
929 // so fill in the canonical type field.
930 QualType Canonical;
931 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000932 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000933
934 // Get the new insert position for the node we care about.
935 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000936 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000937 }
Steve Narofff83820b2009-01-27 22:08:43 +0000938 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000939 Types.push_back(New);
940 PointerTypes.InsertNode(New, InsertPos);
941 return QualType(New, 0);
942}
943
Steve Naroff5618bd42008-08-27 16:04:49 +0000944/// getBlockPointerType - Return the uniqued reference to the type for
945/// a pointer to the specified block.
946QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000947 assert(T->isFunctionType() && "block of function types only");
948 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000949 // structure.
950 llvm::FoldingSetNodeID ID;
951 BlockPointerType::Profile(ID, T);
952
953 void *InsertPos = 0;
954 if (BlockPointerType *PT =
955 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
956 return QualType(PT, 0);
957
Steve Naroff296e8d52008-08-28 19:20:44 +0000958 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000959 // type either so fill in the canonical type field.
960 QualType Canonical;
961 if (!T->isCanonical()) {
962 Canonical = getBlockPointerType(getCanonicalType(T));
963
964 // Get the new insert position for the node we care about.
965 BlockPointerType *NewIP =
966 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000967 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000968 }
Steve Narofff83820b2009-01-27 22:08:43 +0000969 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000970 Types.push_back(New);
971 BlockPointerTypes.InsertNode(New, InsertPos);
972 return QualType(New, 0);
973}
974
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000975/// getLValueReferenceType - Return the uniqued reference to the type for an
976/// lvalue reference to the specified type.
977QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000978 // Unique pointers, to guarantee there is only one pointer of a particular
979 // structure.
980 llvm::FoldingSetNodeID ID;
981 ReferenceType::Profile(ID, T);
982
983 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000984 if (LValueReferenceType *RT =
985 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000986 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000987
Reid Spencer5f016e22007-07-11 17:01:13 +0000988 // If the referencee type isn't canonical, this won't be a canonical type
989 // either, so fill in the canonical type field.
990 QualType Canonical;
991 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000992 Canonical = getLValueReferenceType(getCanonicalType(T));
993
Reid Spencer5f016e22007-07-11 17:01:13 +0000994 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000995 LValueReferenceType *NewIP =
996 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000997 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000998 }
999
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001000 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001001 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001002 LValueReferenceTypes.InsertNode(New, InsertPos);
1003 return QualType(New, 0);
1004}
1005
1006/// getRValueReferenceType - Return the uniqued reference to the type for an
1007/// rvalue reference to the specified type.
1008QualType ASTContext::getRValueReferenceType(QualType T) {
1009 // Unique pointers, to guarantee there is only one pointer of a particular
1010 // structure.
1011 llvm::FoldingSetNodeID ID;
1012 ReferenceType::Profile(ID, T);
1013
1014 void *InsertPos = 0;
1015 if (RValueReferenceType *RT =
1016 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1017 return QualType(RT, 0);
1018
1019 // If the referencee type isn't canonical, this won't be a canonical type
1020 // either, so fill in the canonical type field.
1021 QualType Canonical;
1022 if (!T->isCanonical()) {
1023 Canonical = getRValueReferenceType(getCanonicalType(T));
1024
1025 // Get the new insert position for the node we care about.
1026 RValueReferenceType *NewIP =
1027 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1028 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1029 }
1030
1031 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1032 Types.push_back(New);
1033 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 return QualType(New, 0);
1035}
1036
Sebastian Redlf30208a2009-01-24 21:16:55 +00001037/// getMemberPointerType - Return the uniqued reference to the type for a
1038/// member pointer to the specified type, in the specified class.
1039QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1040{
1041 // Unique pointers, to guarantee there is only one pointer of a particular
1042 // structure.
1043 llvm::FoldingSetNodeID ID;
1044 MemberPointerType::Profile(ID, T, Cls);
1045
1046 void *InsertPos = 0;
1047 if (MemberPointerType *PT =
1048 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1049 return QualType(PT, 0);
1050
1051 // If the pointee or class type isn't canonical, this won't be a canonical
1052 // type either, so fill in the canonical type field.
1053 QualType Canonical;
1054 if (!T->isCanonical()) {
1055 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1056
1057 // Get the new insert position for the node we care about.
1058 MemberPointerType *NewIP =
1059 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1060 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1061 }
Steve Narofff83820b2009-01-27 22:08:43 +00001062 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001063 Types.push_back(New);
1064 MemberPointerTypes.InsertNode(New, InsertPos);
1065 return QualType(New, 0);
1066}
1067
Steve Narofffb22d962007-08-30 01:06:46 +00001068/// getConstantArrayType - Return the unique reference to the type for an
1069/// array of the specified element type.
1070QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001071 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001072 ArrayType::ArraySizeModifier ASM,
1073 unsigned EltTypeQuals) {
Eli Friedman587cbdf2009-05-29 20:17:55 +00001074 assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) &&
1075 "Constant array of VLAs is illegal!");
1076
Chris Lattner38aeec72009-05-13 04:12:56 +00001077 // Convert the array size into a canonical width matching the pointer size for
1078 // the target.
1079 llvm::APInt ArySize(ArySizeIn);
1080 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1081
Reid Spencer5f016e22007-07-11 17:01:13 +00001082 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001083 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001084
1085 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001086 if (ConstantArrayType *ATP =
1087 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 return QualType(ATP, 0);
1089
1090 // If the element type isn't canonical, this won't be a canonical type either,
1091 // so fill in the canonical type field.
1092 QualType Canonical;
1093 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001094 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001095 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001096 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001097 ConstantArrayType *NewIP =
1098 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001099 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 }
1101
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001102 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001103 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001104 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001105 Types.push_back(New);
1106 return QualType(New, 0);
1107}
1108
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001109/// getVariableArrayType - Returns a non-unique reference to the type for a
1110/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001111QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1112 ArrayType::ArraySizeModifier ASM,
1113 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001114 // Since we don't unique expressions, it isn't possible to unique VLA's
1115 // that have an expression provided for their size.
1116
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001117 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001118 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001119
1120 VariableArrayTypes.push_back(New);
1121 Types.push_back(New);
1122 return QualType(New, 0);
1123}
1124
Douglas Gregor898574e2008-12-05 23:32:09 +00001125/// getDependentSizedArrayType - Returns a non-unique reference to
1126/// the type for a dependently-sized array of the specified element
1127/// type. FIXME: We will need these to be uniqued, or at least
1128/// comparable, at some point.
1129QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1130 ArrayType::ArraySizeModifier ASM,
1131 unsigned EltTypeQuals) {
1132 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1133 "Size must be type- or value-dependent!");
1134
1135 // Since we don't unique expressions, it isn't possible to unique
1136 // dependently-sized array types.
1137
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001138 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001139 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1140 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001141
1142 DependentSizedArrayTypes.push_back(New);
1143 Types.push_back(New);
1144 return QualType(New, 0);
1145}
1146
Eli Friedmanc5773c42008-02-15 18:16:39 +00001147QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1148 ArrayType::ArraySizeModifier ASM,
1149 unsigned EltTypeQuals) {
1150 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001151 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001152
1153 void *InsertPos = 0;
1154 if (IncompleteArrayType *ATP =
1155 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1156 return QualType(ATP, 0);
1157
1158 // If the element type isn't canonical, this won't be a canonical type
1159 // either, so fill in the canonical type field.
1160 QualType Canonical;
1161
1162 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001163 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001164 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001165
1166 // Get the new insert position for the node we care about.
1167 IncompleteArrayType *NewIP =
1168 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001169 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001170 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001171
Steve Narofff83820b2009-01-27 22:08:43 +00001172 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001173 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001174
1175 IncompleteArrayTypes.InsertNode(New, InsertPos);
1176 Types.push_back(New);
1177 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001178}
1179
Steve Naroff73322922007-07-18 18:00:27 +00001180/// getVectorType - Return the unique reference to a vector type of
1181/// the specified element type and size. VectorType must be a built-in type.
1182QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 BuiltinType *baseType;
1184
Chris Lattnerf52ab252008-04-06 22:59:24 +00001185 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001186 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001187
1188 // Check if we've already instantiated a vector of this type.
1189 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001190 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 void *InsertPos = 0;
1192 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1193 return QualType(VTP, 0);
1194
1195 // If the element type isn't canonical, this won't be a canonical type either,
1196 // so fill in the canonical type field.
1197 QualType Canonical;
1198 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001199 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001200
1201 // Get the new insert position for the node we care about.
1202 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001203 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 }
Steve Narofff83820b2009-01-27 22:08:43 +00001205 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 VectorTypes.InsertNode(New, InsertPos);
1207 Types.push_back(New);
1208 return QualType(New, 0);
1209}
1210
Nate Begeman213541a2008-04-18 23:10:10 +00001211/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001212/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001213QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001214 BuiltinType *baseType;
1215
Chris Lattnerf52ab252008-04-06 22:59:24 +00001216 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001217 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001218
1219 // Check if we've already instantiated a vector of this type.
1220 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001221 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001222 void *InsertPos = 0;
1223 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1224 return QualType(VTP, 0);
1225
1226 // If the element type isn't canonical, this won't be a canonical type either,
1227 // so fill in the canonical type field.
1228 QualType Canonical;
1229 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001230 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001231
1232 // Get the new insert position for the node we care about.
1233 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001234 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001235 }
Steve Narofff83820b2009-01-27 22:08:43 +00001236 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001237 VectorTypes.InsertNode(New, InsertPos);
1238 Types.push_back(New);
1239 return QualType(New, 0);
1240}
1241
Douglas Gregor72564e72009-02-26 23:50:07 +00001242/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001243///
Douglas Gregor72564e72009-02-26 23:50:07 +00001244QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 // Unique functions, to guarantee there is only one function of a particular
1246 // structure.
1247 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001248 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001249
1250 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001251 if (FunctionNoProtoType *FT =
1252 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 return QualType(FT, 0);
1254
1255 QualType Canonical;
1256 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001257 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001258
1259 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001260 FunctionNoProtoType *NewIP =
1261 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001262 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 }
1264
Douglas Gregor72564e72009-02-26 23:50:07 +00001265 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001266 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001267 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001268 return QualType(New, 0);
1269}
1270
1271/// getFunctionType - Return a normal function type with a typed argument
1272/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001273QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001274 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001275 unsigned TypeQuals, bool hasExceptionSpec,
1276 bool hasAnyExceptionSpec, unsigned NumExs,
1277 const QualType *ExArray) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001278 // Unique functions, to guarantee there is only one function of a particular
1279 // structure.
1280 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001281 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001282 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1283 NumExs, ExArray);
Reid Spencer5f016e22007-07-11 17:01:13 +00001284
1285 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001286 if (FunctionProtoType *FTP =
1287 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001289
1290 // Determine whether the type being created is already canonical or not.
Reid Spencer5f016e22007-07-11 17:01:13 +00001291 bool isCanonical = ResultTy->isCanonical();
Sebastian Redl465226e2009-05-27 22:11:52 +00001292 if (hasExceptionSpec)
1293 isCanonical = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001294 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1295 if (!ArgArray[i]->isCanonical())
1296 isCanonical = false;
1297
1298 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001299 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001300 QualType Canonical;
1301 if (!isCanonical) {
1302 llvm::SmallVector<QualType, 16> CanonicalArgs;
1303 CanonicalArgs.reserve(NumArgs);
1304 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001305 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001306
Chris Lattnerf52ab252008-04-06 22:59:24 +00001307 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001308 CanonicalArgs.data(), NumArgs,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001309 isVariadic, TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +00001310
Reid Spencer5f016e22007-07-11 17:01:13 +00001311 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001312 FunctionProtoType *NewIP =
1313 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001314 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001315 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001316
Douglas Gregor72564e72009-02-26 23:50:07 +00001317 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001318 // for two variable size arrays (for parameter and exception types) at the
1319 // end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001320 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001321 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1322 NumArgs*sizeof(QualType) +
1323 NumExs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001324 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001325 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1326 ExArray, NumExs, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001327 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001328 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001329 return QualType(FTP, 0);
1330}
1331
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001332/// getTypeDeclType - Return the unique reference to the type for the
1333/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001334QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001335 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001336 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1337
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001338 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001339 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001340 else if (isa<TemplateTypeParmDecl>(Decl)) {
1341 assert(false && "Template type parameter types are always available.");
1342 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001343 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001344
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001345 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001346 if (PrevDecl)
1347 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001348 else
1349 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001350 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001351 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1352 if (PrevDecl)
1353 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001354 else
1355 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001356 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001357 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001358 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001359
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001360 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001361 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001362}
1363
Reid Spencer5f016e22007-07-11 17:01:13 +00001364/// getTypedefType - Return the unique reference to the type for the
1365/// specified typename decl.
1366QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1367 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1368
Chris Lattnerf52ab252008-04-06 22:59:24 +00001369 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001370 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001371 Types.push_back(Decl->TypeForDecl);
1372 return QualType(Decl->TypeForDecl, 0);
1373}
1374
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001375/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001376/// specified ObjC interface decl.
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001377QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001378 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1379
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001380 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1381 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff3536b442007-09-06 21:24:23 +00001382 Types.push_back(Decl->TypeForDecl);
1383 return QualType(Decl->TypeForDecl, 0);
1384}
1385
Douglas Gregorfab9d672009-02-05 23:33:38 +00001386/// \brief Retrieve the template type parameter type for a template
1387/// parameter with the given depth, index, and (optionally) name.
1388QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1389 IdentifierInfo *Name) {
1390 llvm::FoldingSetNodeID ID;
1391 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1392 void *InsertPos = 0;
1393 TemplateTypeParmType *TypeParm
1394 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1395
1396 if (TypeParm)
1397 return QualType(TypeParm, 0);
1398
1399 if (Name)
1400 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1401 getTemplateTypeParmType(Depth, Index));
1402 else
1403 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1404
1405 Types.push_back(TypeParm);
1406 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1407
1408 return QualType(TypeParm, 0);
1409}
1410
Douglas Gregor55f6b142009-02-09 18:46:07 +00001411QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001412ASTContext::getTemplateSpecializationType(TemplateName Template,
1413 const TemplateArgument *Args,
1414 unsigned NumArgs,
1415 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001416 if (!Canon.isNull())
1417 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001418
Douglas Gregor55f6b142009-02-09 18:46:07 +00001419 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001420 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001421
Douglas Gregor55f6b142009-02-09 18:46:07 +00001422 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001423 TemplateSpecializationType *Spec
1424 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001425
1426 if (Spec)
1427 return QualType(Spec, 0);
1428
Douglas Gregor7532dc62009-03-30 22:58:21 +00001429 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001430 sizeof(TemplateArgument) * NumArgs),
1431 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001432 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001433 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001434 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001435
1436 return QualType(Spec, 0);
1437}
1438
Douglas Gregore4e5b052009-03-19 00:18:19 +00001439QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001440ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001441 QualType NamedType) {
1442 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001443 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001444
1445 void *InsertPos = 0;
1446 QualifiedNameType *T
1447 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1448 if (T)
1449 return QualType(T, 0);
1450
Douglas Gregorab452ba2009-03-26 23:50:42 +00001451 T = new (*this) QualifiedNameType(NNS, NamedType,
1452 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001453 Types.push_back(T);
1454 QualifiedNameTypes.InsertNode(T, InsertPos);
1455 return QualType(T, 0);
1456}
1457
Douglas Gregord57959a2009-03-27 23:10:48 +00001458QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1459 const IdentifierInfo *Name,
1460 QualType Canon) {
1461 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1462
1463 if (Canon.isNull()) {
1464 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1465 if (CanonNNS != NNS)
1466 Canon = getTypenameType(CanonNNS, Name);
1467 }
1468
1469 llvm::FoldingSetNodeID ID;
1470 TypenameType::Profile(ID, NNS, Name);
1471
1472 void *InsertPos = 0;
1473 TypenameType *T
1474 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1475 if (T)
1476 return QualType(T, 0);
1477
1478 T = new (*this) TypenameType(NNS, Name, Canon);
1479 Types.push_back(T);
1480 TypenameTypes.InsertNode(T, InsertPos);
1481 return QualType(T, 0);
1482}
1483
Douglas Gregor17343172009-04-01 00:28:59 +00001484QualType
1485ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1486 const TemplateSpecializationType *TemplateId,
1487 QualType Canon) {
1488 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1489
1490 if (Canon.isNull()) {
1491 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1492 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1493 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1494 const TemplateSpecializationType *CanonTemplateId
1495 = CanonType->getAsTemplateSpecializationType();
1496 assert(CanonTemplateId &&
1497 "Canonical type must also be a template specialization type");
1498 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1499 }
1500 }
1501
1502 llvm::FoldingSetNodeID ID;
1503 TypenameType::Profile(ID, NNS, TemplateId);
1504
1505 void *InsertPos = 0;
1506 TypenameType *T
1507 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1508 if (T)
1509 return QualType(T, 0);
1510
1511 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1512 Types.push_back(T);
1513 TypenameTypes.InsertNode(T, InsertPos);
1514 return QualType(T, 0);
1515}
1516
Chris Lattner88cb27a2008-04-07 04:56:42 +00001517/// CmpProtocolNames - Comparison predicate for sorting protocols
1518/// alphabetically.
1519static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1520 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001521 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001522}
1523
1524static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1525 unsigned &NumProtocols) {
1526 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1527
1528 // Sort protocols, keyed by name.
1529 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1530
1531 // Remove duplicates.
1532 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1533 NumProtocols = ProtocolsEnd-Protocols;
1534}
1535
1536
Chris Lattner065f0d72008-04-07 04:44:08 +00001537/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1538/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001539QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1540 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001541 // Sort the protocol list alphabetically to canonicalize it.
1542 SortAndUniqueProtocols(Protocols, NumProtocols);
1543
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001544 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001545 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001546
1547 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001548 if (ObjCQualifiedInterfaceType *QT =
1549 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001550 return QualType(QT, 0);
1551
1552 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001553 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001554 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001555
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001556 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001557 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001558 return QualType(QType, 0);
1559}
1560
Chris Lattner88cb27a2008-04-07 04:56:42 +00001561/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1562/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001563QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001564 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001565 // Sort the protocol list alphabetically to canonicalize it.
1566 SortAndUniqueProtocols(Protocols, NumProtocols);
1567
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001568 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001569 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001570
1571 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001572 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001573 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001574 return QualType(QT, 0);
1575
1576 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001577 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001578 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001579 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001580 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001581 return QualType(QType, 0);
1582}
1583
Douglas Gregor72564e72009-02-26 23:50:07 +00001584/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1585/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001586/// multiple declarations that refer to "typeof(x)" all contain different
1587/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1588/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001589QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001590 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001591 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001592 Types.push_back(toe);
1593 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001594}
1595
Steve Naroff9752f252007-08-01 18:02:17 +00001596/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1597/// TypeOfType AST's. The only motivation to unique these nodes would be
1598/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1599/// an issue. This doesn't effect the type checker, since it operates
1600/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001601QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001602 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001603 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001604 Types.push_back(tot);
1605 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001606}
1607
Reid Spencer5f016e22007-07-11 17:01:13 +00001608/// getTagDeclType - Return the unique reference to the type for the
1609/// specified TagDecl (struct/union/class/enum) decl.
1610QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001611 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001612 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001613}
1614
1615/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1616/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1617/// needs to agree with the definition in <stddef.h>.
1618QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001619 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001620}
1621
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001622/// getSignedWCharType - Return the type of "signed wchar_t".
1623/// Used when in C++, as a GCC extension.
1624QualType ASTContext::getSignedWCharType() const {
1625 // FIXME: derive from "Target" ?
1626 return WCharTy;
1627}
1628
1629/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1630/// Used when in C++, as a GCC extension.
1631QualType ASTContext::getUnsignedWCharType() const {
1632 // FIXME: derive from "Target" ?
1633 return UnsignedIntTy;
1634}
1635
Chris Lattner8b9023b2007-07-13 03:05:23 +00001636/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1637/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1638QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001639 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001640}
1641
Chris Lattnere6327742008-04-02 05:18:44 +00001642//===----------------------------------------------------------------------===//
1643// Type Operators
1644//===----------------------------------------------------------------------===//
1645
Chris Lattner77c96472008-04-06 22:41:35 +00001646/// getCanonicalType - Return the canonical (structural) type corresponding to
1647/// the specified potentially non-canonical type. The non-canonical version
1648/// of a type may have many "decorated" versions of types. Decorators can
1649/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1650/// to be free of any of these, allowing two canonical types to be compared
1651/// for exact equality with a simple pointer comparison.
1652QualType ASTContext::getCanonicalType(QualType T) {
1653 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001654
1655 // If the result has type qualifiers, make sure to canonicalize them as well.
1656 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1657 if (TypeQuals == 0) return CanType;
1658
1659 // If the type qualifiers are on an array type, get the canonical type of the
1660 // array with the qualifiers applied to the element type.
1661 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1662 if (!AT)
1663 return CanType.getQualifiedType(TypeQuals);
1664
1665 // Get the canonical version of the element with the extra qualifiers on it.
1666 // This can recursively sink qualifiers through multiple levels of arrays.
1667 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1668 NewEltTy = getCanonicalType(NewEltTy);
1669
1670 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1671 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1672 CAT->getIndexTypeQualifier());
1673 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1674 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1675 IAT->getIndexTypeQualifier());
1676
Douglas Gregor898574e2008-12-05 23:32:09 +00001677 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1678 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1679 DSAT->getSizeModifier(),
1680 DSAT->getIndexTypeQualifier());
1681
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001682 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1683 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1684 VAT->getSizeModifier(),
1685 VAT->getIndexTypeQualifier());
1686}
1687
Douglas Gregor7da97d02009-05-10 22:57:19 +00001688Decl *ASTContext::getCanonicalDecl(Decl *D) {
Douglas Gregorc4ccf012009-05-10 22:59:12 +00001689 if (!D)
1690 return 0;
1691
Douglas Gregor7da97d02009-05-10 22:57:19 +00001692 if (TagDecl *Tag = dyn_cast<TagDecl>(D)) {
1693 QualType T = getTagDeclType(Tag);
1694 return cast<TagDecl>(cast<TagType>(T.getTypePtr()->CanonicalType)
1695 ->getDecl());
1696 }
1697
1698 if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(D)) {
1699 while (Template->getPreviousDeclaration())
1700 Template = Template->getPreviousDeclaration();
1701 return Template;
1702 }
1703
1704 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
1705 while (Function->getPreviousDeclaration())
1706 Function = Function->getPreviousDeclaration();
1707 return const_cast<FunctionDecl *>(Function);
1708 }
1709
1710 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
1711 while (Var->getPreviousDeclaration())
1712 Var = Var->getPreviousDeclaration();
1713 return const_cast<VarDecl *>(Var);
1714 }
1715
1716 return D;
1717}
1718
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001719TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
1720 // If this template name refers to a template, the canonical
1721 // template name merely stores the template itself.
1722 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Douglas Gregor7da97d02009-05-10 22:57:19 +00001723 return TemplateName(cast<TemplateDecl>(getCanonicalDecl(Template)));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001724
1725 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
1726 assert(DTN && "Non-dependent template names must refer to template decls.");
1727 return DTN->CanonicalTemplateName;
1728}
1729
Douglas Gregord57959a2009-03-27 23:10:48 +00001730NestedNameSpecifier *
1731ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1732 if (!NNS)
1733 return 0;
1734
1735 switch (NNS->getKind()) {
1736 case NestedNameSpecifier::Identifier:
1737 // Canonicalize the prefix but keep the identifier the same.
1738 return NestedNameSpecifier::Create(*this,
1739 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1740 NNS->getAsIdentifier());
1741
1742 case NestedNameSpecifier::Namespace:
1743 // A namespace is canonical; build a nested-name-specifier with
1744 // this namespace and no prefix.
1745 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1746
1747 case NestedNameSpecifier::TypeSpec:
1748 case NestedNameSpecifier::TypeSpecWithTemplate: {
1749 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1750 NestedNameSpecifier *Prefix = 0;
1751
1752 // FIXME: This isn't the right check!
1753 if (T->isDependentType())
1754 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1755
1756 return NestedNameSpecifier::Create(*this, Prefix,
1757 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1758 T.getTypePtr());
1759 }
1760
1761 case NestedNameSpecifier::Global:
1762 // The global specifier is canonical and unique.
1763 return NNS;
1764 }
1765
1766 // Required to silence a GCC warning
1767 return 0;
1768}
1769
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001770
1771const ArrayType *ASTContext::getAsArrayType(QualType T) {
1772 // Handle the non-qualified case efficiently.
1773 if (T.getCVRQualifiers() == 0) {
1774 // Handle the common positive case fast.
1775 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1776 return AT;
1777 }
1778
1779 // Handle the common negative case fast, ignoring CVR qualifiers.
1780 QualType CType = T->getCanonicalTypeInternal();
1781
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001782 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001783 // test.
1784 if (!isa<ArrayType>(CType) &&
1785 !isa<ArrayType>(CType.getUnqualifiedType()))
1786 return 0;
1787
1788 // Apply any CVR qualifiers from the array type to the element type. This
1789 // implements C99 6.7.3p8: "If the specification of an array type includes
1790 // any type qualifiers, the element type is so qualified, not the array type."
1791
1792 // If we get here, we either have type qualifiers on the type, or we have
1793 // sugar such as a typedef in the way. If we have type qualifiers on the type
1794 // we must propagate them down into the elemeng type.
1795 unsigned CVRQuals = T.getCVRQualifiers();
1796 unsigned AddrSpace = 0;
1797 Type *Ty = T.getTypePtr();
1798
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001799 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001800 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001801 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1802 AddrSpace = EXTQT->getAddressSpace();
1803 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001804 } else {
1805 T = Ty->getDesugaredType();
1806 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1807 break;
1808 CVRQuals |= T.getCVRQualifiers();
1809 Ty = T.getTypePtr();
1810 }
1811 }
1812
1813 // If we have a simple case, just return now.
1814 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1815 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1816 return ATy;
1817
1818 // Otherwise, we have an array and we have qualifiers on it. Push the
1819 // qualifiers into the array element type and return a new array type.
1820 // Get the canonical version of the element with the extra qualifiers on it.
1821 // This can recursively sink qualifiers through multiple levels of arrays.
1822 QualType NewEltTy = ATy->getElementType();
1823 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001824 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001825 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1826
1827 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1828 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1829 CAT->getSizeModifier(),
1830 CAT->getIndexTypeQualifier()));
1831 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1832 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1833 IAT->getSizeModifier(),
1834 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001835
Douglas Gregor898574e2008-12-05 23:32:09 +00001836 if (const DependentSizedArrayType *DSAT
1837 = dyn_cast<DependentSizedArrayType>(ATy))
1838 return cast<ArrayType>(
1839 getDependentSizedArrayType(NewEltTy,
1840 DSAT->getSizeExpr(),
1841 DSAT->getSizeModifier(),
1842 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001843
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001844 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1845 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1846 VAT->getSizeModifier(),
1847 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001848}
1849
1850
Chris Lattnere6327742008-04-02 05:18:44 +00001851/// getArrayDecayedType - Return the properly qualified result of decaying the
1852/// specified array type to a pointer. This operation is non-trivial when
1853/// handling typedefs etc. The canonical type of "T" must be an array type,
1854/// this returns a pointer to a properly qualified element of the array.
1855///
1856/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1857QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001858 // Get the element type with 'getAsArrayType' so that we don't lose any
1859 // typedefs in the element type of the array. This also handles propagation
1860 // of type qualifiers from the array type into the element type if present
1861 // (C99 6.7.3p8).
1862 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1863 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001864
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001865 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001866
1867 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001868 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001869}
1870
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001871QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001872 QualType ElemTy = VAT->getElementType();
1873
1874 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1875 return getBaseElementType(VAT);
1876
1877 return ElemTy;
1878}
1879
Reid Spencer5f016e22007-07-11 17:01:13 +00001880/// getFloatingRank - Return a relative rank for floating point types.
1881/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001882static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001883 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001884 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001885
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001886 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001887 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001888 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001889 case BuiltinType::Float: return FloatRank;
1890 case BuiltinType::Double: return DoubleRank;
1891 case BuiltinType::LongDouble: return LongDoubleRank;
1892 }
1893}
1894
Steve Naroff716c7302007-08-27 01:41:48 +00001895/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1896/// point or a complex type (based on typeDomain/typeSize).
1897/// 'typeDomain' is a real floating point or complex type.
1898/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001899QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1900 QualType Domain) const {
1901 FloatingRank EltRank = getFloatingRank(Size);
1902 if (Domain->isComplexType()) {
1903 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001904 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001905 case FloatRank: return FloatComplexTy;
1906 case DoubleRank: return DoubleComplexTy;
1907 case LongDoubleRank: return LongDoubleComplexTy;
1908 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001909 }
Chris Lattner1361b112008-04-06 23:58:54 +00001910
1911 assert(Domain->isRealFloatingType() && "Unknown domain!");
1912 switch (EltRank) {
1913 default: assert(0 && "getFloatingRank(): illegal value for rank");
1914 case FloatRank: return FloatTy;
1915 case DoubleRank: return DoubleTy;
1916 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001917 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001918}
1919
Chris Lattner7cfeb082008-04-06 23:55:33 +00001920/// getFloatingTypeOrder - Compare the rank of the two specified floating
1921/// point types, ignoring the domain of the type (i.e. 'double' ==
1922/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1923/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001924int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1925 FloatingRank LHSR = getFloatingRank(LHS);
1926 FloatingRank RHSR = getFloatingRank(RHS);
1927
1928 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001929 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001930 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001931 return 1;
1932 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001933}
1934
Chris Lattnerf52ab252008-04-06 22:59:24 +00001935/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1936/// routine will assert if passed a built-in type that isn't an integer or enum,
1937/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001938unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001939 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001940 if (EnumType* ET = dyn_cast<EnumType>(T))
1941 T = ET->getDecl()->getIntegerType().getTypePtr();
1942
1943 // There are two things which impact the integer rank: the width, and
1944 // the ordering of builtins. The builtin ordering is encoded in the
1945 // bottom three bits; the width is encoded in the bits above that.
1946 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1947 return FWIT->getWidth() << 3;
1948 }
1949
Chris Lattnerf52ab252008-04-06 22:59:24 +00001950 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001951 default: assert(0 && "getIntegerRank(): not a built-in integer");
1952 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001953 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001954 case BuiltinType::Char_S:
1955 case BuiltinType::Char_U:
1956 case BuiltinType::SChar:
1957 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001958 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001959 case BuiltinType::Short:
1960 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001961 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001962 case BuiltinType::Int:
1963 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001964 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001965 case BuiltinType::Long:
1966 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001967 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001968 case BuiltinType::LongLong:
1969 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001970 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00001971 case BuiltinType::Int128:
1972 case BuiltinType::UInt128:
1973 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001974 }
1975}
1976
Chris Lattner7cfeb082008-04-06 23:55:33 +00001977/// getIntegerTypeOrder - Returns the highest ranked integer type:
1978/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1979/// LHS < RHS, return -1.
1980int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001981 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1982 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001983 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001984
Chris Lattnerf52ab252008-04-06 22:59:24 +00001985 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1986 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001987
Chris Lattner7cfeb082008-04-06 23:55:33 +00001988 unsigned LHSRank = getIntegerRank(LHSC);
1989 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001990
Chris Lattner7cfeb082008-04-06 23:55:33 +00001991 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1992 if (LHSRank == RHSRank) return 0;
1993 return LHSRank > RHSRank ? 1 : -1;
1994 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001995
Chris Lattner7cfeb082008-04-06 23:55:33 +00001996 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1997 if (LHSUnsigned) {
1998 // If the unsigned [LHS] type is larger, return it.
1999 if (LHSRank >= RHSRank)
2000 return 1;
2001
2002 // If the signed type can represent all values of the unsigned type, it
2003 // wins. Because we are dealing with 2's complement and types that are
2004 // powers of two larger than each other, this is always safe.
2005 return -1;
2006 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002007
Chris Lattner7cfeb082008-04-06 23:55:33 +00002008 // If the unsigned [RHS] type is larger, return it.
2009 if (RHSRank >= LHSRank)
2010 return -1;
2011
2012 // If the signed type can represent all values of the unsigned type, it
2013 // wins. Because we are dealing with 2's complement and types that are
2014 // powers of two larger than each other, this is always safe.
2015 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002016}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002017
2018// getCFConstantStringType - Return the type used for constant CFStrings.
2019QualType ASTContext::getCFConstantStringType() {
2020 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00002021 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002022 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002023 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002024 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00002025
2026 // const int *isa;
2027 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002028 // int flags;
2029 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002030 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002031 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00002032 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002033 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00002034
Anders Carlsson71993dd2007-08-17 05:31:46 +00002035 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002036 for (unsigned i = 0; i < 4; ++i) {
2037 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2038 SourceLocation(), 0,
2039 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002040 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002041 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002042 }
2043
2044 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002045 }
2046
2047 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002048}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002049
Douglas Gregor319ac892009-04-23 22:29:11 +00002050void ASTContext::setCFConstantStringType(QualType T) {
2051 const RecordType *Rec = T->getAsRecordType();
2052 assert(Rec && "Invalid CFConstantStringType");
2053 CFConstantStringTypeDecl = Rec->getDecl();
2054}
2055
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002056QualType ASTContext::getObjCFastEnumerationStateType()
2057{
2058 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002059 ObjCFastEnumerationStateTypeDecl =
2060 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2061 &Idents.get("__objcFastEnumerationState"));
2062
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002063 QualType FieldTypes[] = {
2064 UnsignedLongTy,
2065 getPointerType(ObjCIdType),
2066 getPointerType(UnsignedLongTy),
2067 getConstantArrayType(UnsignedLongTy,
2068 llvm::APInt(32, 5), ArrayType::Normal, 0)
2069 };
2070
Douglas Gregor44b43212008-12-11 16:49:14 +00002071 for (size_t i = 0; i < 4; ++i) {
2072 FieldDecl *Field = FieldDecl::Create(*this,
2073 ObjCFastEnumerationStateTypeDecl,
2074 SourceLocation(), 0,
2075 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002076 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002077 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002078 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002079
Douglas Gregor44b43212008-12-11 16:49:14 +00002080 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002081 }
2082
2083 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2084}
2085
Douglas Gregor319ac892009-04-23 22:29:11 +00002086void ASTContext::setObjCFastEnumerationStateType(QualType T) {
2087 const RecordType *Rec = T->getAsRecordType();
2088 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2089 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2090}
2091
Anders Carlssone8c49532007-10-29 06:33:42 +00002092// This returns true if a type has been typedefed to BOOL:
2093// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002094static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002095 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002096 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2097 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002098
2099 return false;
2100}
2101
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002102/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002103/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002104int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002105 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002106
2107 // Make all integer and enum types at least as large as an int
2108 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002109 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002110 // Treat arrays as pointers, since that's how they're passed in.
2111 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002112 sz = getTypeSize(VoidPtrTy);
2113 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002114}
2115
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002116/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002117/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002118void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002119 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002120 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002121 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002122 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002123 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002124 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002125 // Compute size of all parameters.
2126 // Start with computing size of a pointer in number of bytes.
2127 // FIXME: There might(should) be a better way of doing this computation!
2128 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002129 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002130 // The first two arguments (self and _cmd) are pointers; account for
2131 // their size.
2132 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002133 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2134 E = Decl->param_end(); PI != E; ++PI) {
2135 QualType PType = (*PI)->getType();
2136 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002137 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002138 ParmOffset += sz;
2139 }
2140 S += llvm::utostr(ParmOffset);
2141 S += "@0:";
2142 S += llvm::utostr(PtrSize);
2143
2144 // Argument types.
2145 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002146 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2147 E = Decl->param_end(); PI != E; ++PI) {
2148 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002149 QualType PType = PVDecl->getOriginalType();
2150 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002151 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2152 // Use array's original type only if it has known number of
2153 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002154 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002155 PType = PVDecl->getType();
2156 } else if (PType->isFunctionType())
2157 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002158 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002159 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002160 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002161 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002162 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002163 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002164 }
2165}
2166
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002167/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002168/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002169/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2170/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002171/// Property attributes are stored as a comma-delimited C string. The simple
2172/// attributes readonly and bycopy are encoded as single characters. The
2173/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2174/// encoded as single characters, followed by an identifier. Property types
2175/// are also encoded as a parametrized attribute. The characters used to encode
2176/// these attributes are defined by the following enumeration:
2177/// @code
2178/// enum PropertyAttributes {
2179/// kPropertyReadOnly = 'R', // property is read-only.
2180/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2181/// kPropertyByref = '&', // property is a reference to the value last assigned
2182/// kPropertyDynamic = 'D', // property is dynamic
2183/// kPropertyGetter = 'G', // followed by getter selector name
2184/// kPropertySetter = 'S', // followed by setter selector name
2185/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2186/// kPropertyType = 't' // followed by old-style type encoding.
2187/// kPropertyWeak = 'W' // 'weak' property
2188/// kPropertyStrong = 'P' // property GC'able
2189/// kPropertyNonAtomic = 'N' // property non-atomic
2190/// };
2191/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002192void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2193 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002194 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002195 // Collect information from the property implementation decl(s).
2196 bool Dynamic = false;
2197 ObjCPropertyImplDecl *SynthesizePID = 0;
2198
2199 // FIXME: Duplicated code due to poor abstraction.
2200 if (Container) {
2201 if (const ObjCCategoryImplDecl *CID =
2202 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2203 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002204 i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2205 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002206 ObjCPropertyImplDecl *PID = *i;
2207 if (PID->getPropertyDecl() == PD) {
2208 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2209 Dynamic = true;
2210 } else {
2211 SynthesizePID = PID;
2212 }
2213 }
2214 }
2215 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002216 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002217 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002218 i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2219 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002220 ObjCPropertyImplDecl *PID = *i;
2221 if (PID->getPropertyDecl() == PD) {
2222 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2223 Dynamic = true;
2224 } else {
2225 SynthesizePID = PID;
2226 }
2227 }
2228 }
2229 }
2230 }
2231
2232 // FIXME: This is not very efficient.
2233 S = "T";
2234
2235 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002236 // GCC has some special rules regarding encoding of properties which
2237 // closely resembles encoding of ivars.
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002238 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002239 true /* outermost type */,
2240 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002241
2242 if (PD->isReadOnly()) {
2243 S += ",R";
2244 } else {
2245 switch (PD->getSetterKind()) {
2246 case ObjCPropertyDecl::Assign: break;
2247 case ObjCPropertyDecl::Copy: S += ",C"; break;
2248 case ObjCPropertyDecl::Retain: S += ",&"; break;
2249 }
2250 }
2251
2252 // It really isn't clear at all what this means, since properties
2253 // are "dynamic by default".
2254 if (Dynamic)
2255 S += ",D";
2256
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002257 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2258 S += ",N";
2259
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002260 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2261 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002262 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002263 }
2264
2265 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2266 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002267 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002268 }
2269
2270 if (SynthesizePID) {
2271 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2272 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002273 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002274 }
2275
2276 // FIXME: OBJCGC: weak & strong
2277}
2278
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002279/// getLegacyIntegralTypeEncoding -
2280/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002281/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002282/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2283///
2284void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2285 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2286 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002287 if (BT->getKind() == BuiltinType::ULong &&
2288 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002289 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002290 else
2291 if (BT->getKind() == BuiltinType::Long &&
2292 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002293 PointeeTy = IntTy;
2294 }
2295 }
2296}
2297
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002298void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002299 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002300 // We follow the behavior of gcc, expanding structures which are
2301 // directly pointed to, and expanding embedded structures. Note that
2302 // these rules are sufficient to prevent recursive encoding of the
2303 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002304 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2305 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002306}
2307
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002308static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002309 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002310 const Expr *E = FD->getBitWidth();
2311 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2312 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002313 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002314 S += 'b';
2315 S += llvm::utostr(N);
2316}
2317
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002318void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2319 bool ExpandPointedToStructures,
2320 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002321 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002322 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002323 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002324 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002325 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002326 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002327 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002328 else {
2329 char encoding;
2330 switch (BT->getKind()) {
2331 default: assert(0 && "Unhandled builtin type kind");
2332 case BuiltinType::Void: encoding = 'v'; break;
2333 case BuiltinType::Bool: encoding = 'B'; break;
2334 case BuiltinType::Char_U:
2335 case BuiltinType::UChar: encoding = 'C'; break;
2336 case BuiltinType::UShort: encoding = 'S'; break;
2337 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002338 case BuiltinType::ULong:
2339 encoding =
2340 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2341 break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002342 case BuiltinType::UInt128: encoding = 'T'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002343 case BuiltinType::ULongLong: encoding = 'Q'; break;
2344 case BuiltinType::Char_S:
2345 case BuiltinType::SChar: encoding = 'c'; break;
2346 case BuiltinType::Short: encoding = 's'; break;
2347 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002348 case BuiltinType::Long:
2349 encoding =
2350 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2351 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002352 case BuiltinType::LongLong: encoding = 'q'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002353 case BuiltinType::Int128: encoding = 't'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002354 case BuiltinType::Float: encoding = 'f'; break;
2355 case BuiltinType::Double: encoding = 'd'; break;
2356 case BuiltinType::LongDouble: encoding = 'd'; break;
2357 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002358
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002359 S += encoding;
2360 }
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002361 } else if (const ComplexType *CT = T->getAsComplexType()) {
2362 S += 'j';
2363 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2364 false);
2365 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002366 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2367 ExpandPointedToStructures,
2368 ExpandStructures, FD);
2369 if (FD || EncodingProperty) {
2370 // Note that we do extended encoding of protocol qualifer list
2371 // Only when doing ivar or property encoding.
2372 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2373 S += '"';
Steve Naroff446ee4e2009-05-27 16:21:00 +00002374 for (ObjCQualifiedIdType::qual_iterator I = QIDT->qual_begin(),
2375 E = QIDT->qual_end(); I != E; ++I) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002376 S += '<';
Steve Naroff446ee4e2009-05-27 16:21:00 +00002377 S += (*I)->getNameAsString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002378 S += '>';
2379 }
2380 S += '"';
2381 }
2382 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002383 }
2384 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002385 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002386 bool isReadOnly = false;
2387 // For historical/compatibility reasons, the read-only qualifier of the
2388 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2389 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2390 // Also, do not emit the 'r' for anything but the outermost type!
2391 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2392 if (OutermostType && T.isConstQualified()) {
2393 isReadOnly = true;
2394 S += 'r';
2395 }
2396 }
2397 else if (OutermostType) {
2398 QualType P = PointeeTy;
2399 while (P->getAsPointerType())
2400 P = P->getAsPointerType()->getPointeeType();
2401 if (P.isConstQualified()) {
2402 isReadOnly = true;
2403 S += 'r';
2404 }
2405 }
2406 if (isReadOnly) {
2407 // Another legacy compatibility encoding. Some ObjC qualifier and type
2408 // combinations need to be rearranged.
2409 // Rewrite "in const" from "nr" to "rn"
2410 const char * s = S.c_str();
2411 int len = S.length();
2412 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2413 std::string replace = "rn";
2414 S.replace(S.end()-2, S.end(), replace);
2415 }
2416 }
Steve Naroff389bf462009-02-12 17:52:19 +00002417 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002418 S += '@';
2419 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002420 }
2421 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002422 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002423 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002424 // Another historical/compatibility reason.
2425 // We encode the underlying type which comes out as
2426 // {...};
2427 S += '^';
2428 getObjCEncodingForTypeImpl(PointeeTy, S,
2429 false, ExpandPointedToStructures,
2430 NULL);
2431 return;
2432 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002433 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002434 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002435 const ObjCInterfaceType *OIT =
2436 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002437 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002438 S += '"';
2439 S += OI->getNameAsCString();
Steve Naroff446ee4e2009-05-27 16:21:00 +00002440 for (ObjCInterfaceType::qual_iterator I = OIT->qual_begin(),
2441 E = OIT->qual_end(); I != E; ++I) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002442 S += '<';
Steve Naroff446ee4e2009-05-27 16:21:00 +00002443 S += (*I)->getNameAsString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002444 S += '>';
2445 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002446 S += '"';
2447 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002448 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002449 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002450 S += '#';
2451 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002452 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002453 S += ':';
2454 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002455 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002456
2457 if (PointeeTy->isCharType()) {
2458 // char pointer types should be encoded as '*' unless it is a
2459 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002460 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002461 S += '*';
2462 return;
2463 }
2464 }
2465
2466 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002467 getLegacyIntegralTypeEncoding(PointeeTy);
2468
2469 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002470 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002471 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002472 } else if (const ArrayType *AT =
2473 // Ignore type qualifiers etc.
2474 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002475 if (isa<IncompleteArrayType>(AT)) {
2476 // Incomplete arrays are encoded as a pointer to the array element.
2477 S += '^';
2478
2479 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2480 false, ExpandStructures, FD);
2481 } else {
2482 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002483
Anders Carlsson559a8332009-02-22 01:38:57 +00002484 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2485 S += llvm::utostr(CAT->getSize().getZExtValue());
2486 else {
2487 //Variable length arrays are encoded as a regular array with 0 elements.
2488 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2489 S += '0';
2490 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002491
Anders Carlsson559a8332009-02-22 01:38:57 +00002492 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2493 false, ExpandStructures, FD);
2494 S += ']';
2495 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002496 } else if (T->getAsFunctionType()) {
2497 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002498 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002499 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002500 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002501 // Anonymous structures print as '?'
2502 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2503 S += II->getName();
2504 } else {
2505 S += '?';
2506 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002507 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002508 S += '=';
Douglas Gregor6ab35242009-04-09 21:40:53 +00002509 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2510 FieldEnd = RDecl->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +00002511 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002512 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002513 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002514 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002515 S += '"';
2516 }
2517
2518 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002519 if (Field->isBitField()) {
2520 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2521 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002522 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002523 QualType qt = Field->getType();
2524 getLegacyIntegralTypeEncoding(qt);
2525 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002526 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002527 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002528 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002529 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002530 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002531 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002532 if (FD && FD->isBitField())
2533 EncodeBitField(this, S, FD);
2534 else
2535 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002536 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002537 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002538 } else if (T->isObjCInterfaceType()) {
2539 // @encode(class_name)
2540 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2541 S += '{';
2542 const IdentifierInfo *II = OI->getIdentifier();
2543 S += II->getName();
2544 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002545 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002546 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002547 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002548 if (RecFields[i]->isBitField())
2549 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2550 RecFields[i]);
2551 else
2552 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2553 FD);
2554 }
2555 S += '}';
2556 }
2557 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002558 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002559}
2560
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002561void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002562 std::string& S) const {
2563 if (QT & Decl::OBJC_TQ_In)
2564 S += 'n';
2565 if (QT & Decl::OBJC_TQ_Inout)
2566 S += 'N';
2567 if (QT & Decl::OBJC_TQ_Out)
2568 S += 'o';
2569 if (QT & Decl::OBJC_TQ_Bycopy)
2570 S += 'O';
2571 if (QT & Decl::OBJC_TQ_Byref)
2572 S += 'R';
2573 if (QT & Decl::OBJC_TQ_Oneway)
2574 S += 'V';
2575}
2576
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002577void ASTContext::setBuiltinVaListType(QualType T)
2578{
2579 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2580
2581 BuiltinVaListType = T;
2582}
2583
Douglas Gregor319ac892009-04-23 22:29:11 +00002584void ASTContext::setObjCIdType(QualType T)
Steve Naroff7e219e42007-10-15 14:41:52 +00002585{
Douglas Gregor319ac892009-04-23 22:29:11 +00002586 ObjCIdType = T;
2587
2588 const TypedefType *TT = T->getAsTypedefType();
2589 if (!TT)
2590 return;
2591
2592 TypedefDecl *TD = TT->getDecl();
Steve Naroff7e219e42007-10-15 14:41:52 +00002593
2594 // typedef struct objc_object *id;
2595 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002596 // User error - caller will issue diagnostics.
2597 if (!ptr)
2598 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002599 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002600 // User error - caller will issue diagnostics.
2601 if (!rec)
2602 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002603 IdStructType = rec;
2604}
2605
Douglas Gregor319ac892009-04-23 22:29:11 +00002606void ASTContext::setObjCSelType(QualType T)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002607{
Douglas Gregor319ac892009-04-23 22:29:11 +00002608 ObjCSelType = T;
2609
2610 const TypedefType *TT = T->getAsTypedefType();
2611 if (!TT)
2612 return;
2613 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002614
2615 // typedef struct objc_selector *SEL;
2616 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002617 if (!ptr)
2618 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002619 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002620 if (!rec)
2621 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002622 SelStructType = rec;
2623}
2624
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002625void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002626{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002627 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002628}
2629
Douglas Gregor319ac892009-04-23 22:29:11 +00002630void ASTContext::setObjCClassType(QualType T)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002631{
Douglas Gregor319ac892009-04-23 22:29:11 +00002632 ObjCClassType = T;
2633
2634 const TypedefType *TT = T->getAsTypedefType();
2635 if (!TT)
2636 return;
2637 TypedefDecl *TD = TT->getDecl();
Anders Carlsson8baaca52007-10-31 02:53:19 +00002638
2639 // typedef struct objc_class *Class;
2640 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2641 assert(ptr && "'Class' incorrectly typed");
2642 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2643 assert(rec && "'Class' incorrectly typed");
2644 ClassStructType = rec;
2645}
2646
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002647void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2648 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002649 "'NSConstantString' type already set!");
2650
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002651 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002652}
2653
Douglas Gregor7532dc62009-03-30 22:58:21 +00002654/// \brief Retrieve the template name that represents a qualified
2655/// template name such as \c std::vector.
2656TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2657 bool TemplateKeyword,
2658 TemplateDecl *Template) {
2659 llvm::FoldingSetNodeID ID;
2660 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2661
2662 void *InsertPos = 0;
2663 QualifiedTemplateName *QTN =
2664 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2665 if (!QTN) {
2666 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2667 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2668 }
2669
2670 return TemplateName(QTN);
2671}
2672
2673/// \brief Retrieve the template name that represents a dependent
2674/// template name such as \c MetaFun::template apply.
2675TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2676 const IdentifierInfo *Name) {
2677 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2678
2679 llvm::FoldingSetNodeID ID;
2680 DependentTemplateName::Profile(ID, NNS, Name);
2681
2682 void *InsertPos = 0;
2683 DependentTemplateName *QTN =
2684 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2685
2686 if (QTN)
2687 return TemplateName(QTN);
2688
2689 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2690 if (CanonNNS == NNS) {
2691 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2692 } else {
2693 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2694 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2695 }
2696
2697 DependentTemplateNames.InsertNode(QTN, InsertPos);
2698 return TemplateName(QTN);
2699}
2700
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002701/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002702/// TargetInfo, produce the corresponding type. The unsigned @p Type
2703/// is actually a value of type @c TargetInfo::IntType.
2704QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002705 switch (Type) {
2706 case TargetInfo::NoInt: return QualType();
2707 case TargetInfo::SignedShort: return ShortTy;
2708 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2709 case TargetInfo::SignedInt: return IntTy;
2710 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2711 case TargetInfo::SignedLong: return LongTy;
2712 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2713 case TargetInfo::SignedLongLong: return LongLongTy;
2714 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2715 }
2716
2717 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002718 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002719}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002720
2721//===----------------------------------------------------------------------===//
2722// Type Predicates.
2723//===----------------------------------------------------------------------===//
2724
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002725/// isObjCNSObjectType - Return true if this is an NSObject object using
2726/// NSObject attribute on a c-style pointer type.
2727/// FIXME - Make it work directly on types.
2728///
2729bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2730 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2731 if (TypedefDecl *TD = TDT->getDecl())
2732 if (TD->getAttr<ObjCNSObjectAttr>())
2733 return true;
2734 }
2735 return false;
2736}
2737
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002738/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2739/// to an object type. This includes "id" and "Class" (two 'special' pointers
2740/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2741/// ID type).
2742bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002743 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002744 return true;
2745
Steve Naroff6ae98502008-10-21 18:24:04 +00002746 // Blocks are objects.
2747 if (Ty->isBlockPointerType())
2748 return true;
2749
2750 // All other object types are pointers.
Chris Lattner16ede0e2009-04-12 23:51:02 +00002751 const PointerType *PT = Ty->getAsPointerType();
2752 if (PT == 0)
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002753 return false;
2754
Chris Lattner16ede0e2009-04-12 23:51:02 +00002755 // If this a pointer to an interface (e.g. NSString*), it is ok.
2756 if (PT->getPointeeType()->isObjCInterfaceType() ||
2757 // If is has NSObject attribute, OK as well.
2758 isObjCNSObjectType(Ty))
2759 return true;
2760
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002761 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2762 // pointer types. This looks for the typedef specifically, not for the
Chris Lattner16ede0e2009-04-12 23:51:02 +00002763 // underlying type. Iteratively strip off typedefs so that we can handle
2764 // typedefs of typedefs.
2765 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2766 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2767 Ty.getUnqualifiedType() == getObjCClassType())
2768 return true;
2769
2770 Ty = TDT->getDecl()->getUnderlyingType();
2771 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002772
Chris Lattner16ede0e2009-04-12 23:51:02 +00002773 return false;
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002774}
2775
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002776/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2777/// garbage collection attribute.
2778///
2779QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002780 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002781 if (getLangOptions().ObjC1 &&
2782 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002783 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002784 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002785 // (or pointers to them) be treated as though they were declared
2786 // as __strong.
2787 if (GCAttrs == QualType::GCNone) {
2788 if (isObjCObjectPointerType(Ty))
2789 GCAttrs = QualType::Strong;
2790 else if (Ty->isPointerType())
2791 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2792 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00002793 // Non-pointers have none gc'able attribute regardless of the attribute
2794 // set on them.
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00002795 else if (!Ty->isPointerType() && !isObjCObjectPointerType(Ty))
Fariborz Jahanianc2112182009-04-11 00:00:54 +00002796 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002797 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002798 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002799}
2800
Chris Lattner6ac46a42008-04-07 06:51:04 +00002801//===----------------------------------------------------------------------===//
2802// Type Compatibility Testing
2803//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002804
Steve Naroff1c7d0672008-09-04 15:10:53 +00002805/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002806/// block types. Types must be strictly compatible here. For example,
2807/// C unfortunately doesn't produce an error for the following:
2808///
2809/// int (*emptyArgFunc)();
2810/// int (*intArgList)(int) = emptyArgFunc;
2811///
2812/// For blocks, we will produce an error for the following (similar to C++):
2813///
2814/// int (^emptyArgBlock)();
2815/// int (^intArgBlock)(int) = emptyArgBlock;
2816///
2817/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2818///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002819bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002820 const FunctionType *lbase = lhs->getAsFunctionType();
2821 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002822 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2823 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stumpaab0f7a2009-04-01 01:17:39 +00002824 if (lproto && rproto == 0)
2825 return false;
2826 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff1c7d0672008-09-04 15:10:53 +00002827}
2828
Chris Lattner6ac46a42008-04-07 06:51:04 +00002829/// areCompatVectorTypes - Return true if the two specified vector types are
2830/// compatible.
2831static bool areCompatVectorTypes(const VectorType *LHS,
2832 const VectorType *RHS) {
2833 assert(LHS->isCanonical() && RHS->isCanonical());
2834 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002835 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002836}
2837
Eli Friedman3d815e72008-08-22 00:56:42 +00002838/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002839/// compatible for assignment from RHS to LHS. This handles validation of any
2840/// protocol qualifiers on the LHS or RHS.
2841///
Eli Friedman3d815e72008-08-22 00:56:42 +00002842bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2843 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002844 // Verify that the base decls are compatible: the RHS must be a subclass of
2845 // the LHS.
2846 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2847 return false;
2848
2849 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2850 // protocol qualified at all, then we are good.
2851 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2852 return true;
2853
2854 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2855 // isn't a superset.
2856 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2857 return true; // FIXME: should return false!
2858
2859 // Finally, we must have two protocol-qualified interfaces.
2860 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2861 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002862
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002863 // All LHS protocols must have a presence on the RHS.
2864 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002865
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002866 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2867 LHSPE = LHSP->qual_end();
2868 LHSPI != LHSPE; LHSPI++) {
2869 bool RHSImplementsProtocol = false;
2870
2871 // If the RHS doesn't implement the protocol on the left, the types
2872 // are incompatible.
2873 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2874 RHSPE = RHSP->qual_end();
2875 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2876 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2877 RHSImplementsProtocol = true;
2878 }
2879 // FIXME: For better diagnostics, consider passing back the protocol name.
2880 if (!RHSImplementsProtocol)
2881 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002882 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002883 // The RHS implements all protocols listed on the LHS.
2884 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002885}
2886
Steve Naroff389bf462009-02-12 17:52:19 +00002887bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2888 // get the "pointed to" types
2889 const PointerType *LHSPT = LHS->getAsPointerType();
2890 const PointerType *RHSPT = RHS->getAsPointerType();
2891
2892 if (!LHSPT || !RHSPT)
2893 return false;
2894
2895 QualType lhptee = LHSPT->getPointeeType();
2896 QualType rhptee = RHSPT->getPointeeType();
2897 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2898 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2899 // ID acts sort of like void* for ObjC interfaces
2900 if (LHSIface && isObjCIdStructType(rhptee))
2901 return true;
2902 if (RHSIface && isObjCIdStructType(lhptee))
2903 return true;
2904 if (!LHSIface || !RHSIface)
2905 return false;
2906 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2907 canAssignObjCInterfaces(RHSIface, LHSIface);
2908}
2909
Steve Naroffec0550f2007-10-15 20:41:53 +00002910/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2911/// both shall have the identically qualified version of a compatible type.
2912/// C99 6.2.7p1: Two types have compatible types if their types are the
2913/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002914bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2915 return !mergeTypes(LHS, RHS).isNull();
2916}
2917
2918QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2919 const FunctionType *lbase = lhs->getAsFunctionType();
2920 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002921 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2922 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002923 bool allLTypes = true;
2924 bool allRTypes = true;
2925
2926 // Check return type
2927 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2928 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002929 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2930 allLTypes = false;
2931 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2932 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002933
2934 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00002935 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
2936 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00002937 unsigned lproto_nargs = lproto->getNumArgs();
2938 unsigned rproto_nargs = rproto->getNumArgs();
2939
2940 // Compatible functions must have the same number of arguments
2941 if (lproto_nargs != rproto_nargs)
2942 return QualType();
2943
2944 // Variadic and non-variadic functions aren't compatible
2945 if (lproto->isVariadic() != rproto->isVariadic())
2946 return QualType();
2947
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002948 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2949 return QualType();
2950
Eli Friedman3d815e72008-08-22 00:56:42 +00002951 // Check argument compatibility
2952 llvm::SmallVector<QualType, 10> types;
2953 for (unsigned i = 0; i < lproto_nargs; i++) {
2954 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2955 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2956 QualType argtype = mergeTypes(largtype, rargtype);
2957 if (argtype.isNull()) return QualType();
2958 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002959 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2960 allLTypes = false;
2961 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2962 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002963 }
2964 if (allLTypes) return lhs;
2965 if (allRTypes) return rhs;
2966 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002967 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002968 }
2969
2970 if (lproto) allRTypes = false;
2971 if (rproto) allLTypes = false;
2972
Douglas Gregor72564e72009-02-26 23:50:07 +00002973 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002974 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00002975 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00002976 if (proto->isVariadic()) return QualType();
2977 // Check that the types are compatible with the types that
2978 // would result from default argument promotions (C99 6.7.5.3p15).
2979 // The only types actually affected are promotable integer
2980 // types and floats, which would be passed as a different
2981 // type depending on whether the prototype is visible.
2982 unsigned proto_nargs = proto->getNumArgs();
2983 for (unsigned i = 0; i < proto_nargs; ++i) {
2984 QualType argTy = proto->getArgType(i);
2985 if (argTy->isPromotableIntegerType() ||
2986 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2987 return QualType();
2988 }
2989
2990 if (allLTypes) return lhs;
2991 if (allRTypes) return rhs;
2992 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002993 proto->getNumArgs(), lproto->isVariadic(),
2994 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002995 }
2996
2997 if (allLTypes) return lhs;
2998 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002999 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00003000}
3001
3002QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00003003 // C++ [expr]: If an expression initially has the type "reference to T", the
3004 // type is adjusted to "T" prior to any further analysis, the expression
3005 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003006 // expression is an lvalue unless the reference is an rvalue reference and
3007 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00003008 // FIXME: C++ shouldn't be going through here! The rules are different
3009 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003010 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3011 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00003012 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003013 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00003014 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003015 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00003016
Eli Friedman3d815e72008-08-22 00:56:42 +00003017 QualType LHSCan = getCanonicalType(LHS),
3018 RHSCan = getCanonicalType(RHS);
3019
3020 // If two types are identical, they are compatible.
3021 if (LHSCan == RHSCan)
3022 return LHS;
3023
3024 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003025 // Note that we handle extended qualifiers later, in the
3026 // case for ExtQualType.
3027 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00003028 return QualType();
3029
Eli Friedman852d63b2009-06-01 01:22:52 +00003030 Type::TypeClass LHSClass = LHSCan->getTypeClass();
3031 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003032
Chris Lattner1adb8832008-01-14 05:45:46 +00003033 // We want to consider the two function types to be the same for these
3034 // comparisons, just force one to the other.
3035 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3036 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003037
Eli Friedman07d25872009-06-02 05:28:56 +00003038 // Strip off objc_gc attributes off the top level so they can be merged.
3039 // This is a complete mess, but the attribute itself doesn't make much sense.
Fariborz Jahanian585f7b22009-06-02 01:40:22 +00003040 if (RHSClass == Type::ExtQual) {
Eli Friedman07d25872009-06-02 05:28:56 +00003041 QualType::GCAttrTypes GCAttr = RHSCan.getObjCGCAttr();
3042 if (GCAttr != QualType::GCNone) {
Fariborz Jahanian86f43852009-06-02 20:58:58 +00003043 QualType::GCAttrTypes GCLHSAttr = LHSCan.getObjCGCAttr();
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003044 // __weak attribute must appear on both declarations.
Fariborz Jahanian86f43852009-06-02 20:58:58 +00003045 // __strong attribue is redundant if other decl is an objective-c
3046 // object pointer (or decorated with __strong attribute); otherwise
3047 // issue error.
3048 if ((GCAttr == QualType::Weak && GCLHSAttr != GCAttr) ||
3049 (GCAttr == QualType::Strong && GCLHSAttr != GCAttr &&
3050 LHSCan->isPointerType() && !isObjCObjectPointerType(LHSCan) &&
3051 !isObjCIdStructType(LHSCan->getAsPointerType()->getPointeeType())))
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003052 return QualType();
3053
Eli Friedman07d25872009-06-02 05:28:56 +00003054 RHS = QualType(cast<ExtQualType>(RHS.getDesugaredType())->getBaseType(),
3055 RHS.getCVRQualifiers());
3056 QualType Result = mergeTypes(LHS, RHS);
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003057 if (!Result.isNull()) {
3058 if (Result.getObjCGCAttr() == QualType::GCNone)
3059 Result = getObjCGCQualType(Result, GCAttr);
3060 else if (Result.getObjCGCAttr() != GCAttr)
3061 Result = QualType();
3062 }
Eli Friedman07d25872009-06-02 05:28:56 +00003063 return Result;
3064 }
Fariborz Jahanian585f7b22009-06-02 01:40:22 +00003065 }
Fariborz Jahanian585f7b22009-06-02 01:40:22 +00003066 if (LHSClass == Type::ExtQual) {
Eli Friedman07d25872009-06-02 05:28:56 +00003067 QualType::GCAttrTypes GCAttr = LHSCan.getObjCGCAttr();
3068 if (GCAttr != QualType::GCNone) {
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003069 QualType::GCAttrTypes GCRHSAttr = RHSCan.getObjCGCAttr();
3070 // __weak attribute must appear on both declarations. __strong
Fariborz Jahanian86f43852009-06-02 20:58:58 +00003071 // __strong attribue is redundant if other decl is an objective-c
3072 // object pointer (or decorated with __strong attribute); otherwise
3073 // issue error.
3074 if ((GCAttr == QualType::Weak && GCRHSAttr != GCAttr) ||
3075 (GCAttr == QualType::Strong && GCRHSAttr != GCAttr &&
3076 RHSCan->isPointerType() && !isObjCObjectPointerType(RHSCan) &&
3077 !isObjCIdStructType(RHSCan->getAsPointerType()->getPointeeType())))
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003078 return QualType();
Fariborz Jahanian86f43852009-06-02 20:58:58 +00003079
Eli Friedman07d25872009-06-02 05:28:56 +00003080 LHS = QualType(cast<ExtQualType>(LHS.getDesugaredType())->getBaseType(),
3081 LHS.getCVRQualifiers());
3082 QualType Result = mergeTypes(LHS, RHS);
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003083 if (!Result.isNull()) {
3084 if (Result.getObjCGCAttr() == QualType::GCNone)
3085 Result = getObjCGCQualType(Result, GCAttr);
3086 else if (Result.getObjCGCAttr() != GCAttr)
3087 Result = QualType();
3088 }
Eli Friedman354e53d2009-06-02 07:45:37 +00003089 return Result;
Eli Friedman07d25872009-06-02 05:28:56 +00003090 }
Fariborz Jahanian585f7b22009-06-02 01:40:22 +00003091 }
3092
Eli Friedman4c721d32008-02-12 08:23:06 +00003093 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003094 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3095 LHSClass = Type::ConstantArray;
3096 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3097 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00003098
Nate Begeman213541a2008-04-18 23:10:10 +00003099 // Canonicalize ExtVector -> Vector.
3100 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3101 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00003102
Chris Lattnerb0489812008-04-07 06:38:24 +00003103 // Consider qualified interfaces and interfaces the same.
3104 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
3105 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00003106
Chris Lattnera36a61f2008-04-07 05:43:21 +00003107 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003108 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003109 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3110 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003111
Steve Naroffd824c9c2009-04-14 15:11:46 +00003112 // 'id' and 'Class' act sort of like void* for ObjC interfaces
3113 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003114 return LHS;
Steve Naroffd824c9c2009-04-14 15:11:46 +00003115 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003116 return RHS;
3117
Steve Naroffbc76dd02008-12-10 22:14:21 +00003118 // ID is compatible with all qualified id types.
3119 if (LHS->isObjCQualifiedIdType()) {
3120 if (const PointerType *PT = RHS->getAsPointerType()) {
3121 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003122 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003123 return LHS;
3124 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3125 // Unfortunately, this API is part of Sema (which we don't have access
3126 // to. Need to refactor. The following check is insufficient, since we
3127 // need to make sure the class implements the protocol.
3128 if (pType->isObjCInterfaceType())
3129 return LHS;
3130 }
3131 }
3132 if (RHS->isObjCQualifiedIdType()) {
3133 if (const PointerType *PT = LHS->getAsPointerType()) {
3134 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003135 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003136 return RHS;
3137 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3138 // Unfortunately, this API is part of Sema (which we don't have access
3139 // to. Need to refactor. The following check is insufficient, since we
3140 // need to make sure the class implements the protocol.
3141 if (pType->isObjCInterfaceType())
3142 return RHS;
3143 }
3144 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003145 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3146 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00003147 if (const EnumType* ETy = LHS->getAsEnumType()) {
3148 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3149 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003150 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003151 if (const EnumType* ETy = RHS->getAsEnumType()) {
3152 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3153 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003154 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003155
Eli Friedman3d815e72008-08-22 00:56:42 +00003156 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003157 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003158
Steve Naroff4a746782008-01-09 22:43:08 +00003159 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003160 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003161#define TYPE(Class, Base)
3162#define ABSTRACT_TYPE(Class, Base)
3163#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3164#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3165#include "clang/AST/TypeNodes.def"
3166 assert(false && "Non-canonical and dependent types shouldn't get here");
3167 return QualType();
3168
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003169 case Type::LValueReference:
3170 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003171 case Type::MemberPointer:
3172 assert(false && "C++ should never be in mergeTypes");
3173 return QualType();
3174
3175 case Type::IncompleteArray:
3176 case Type::VariableArray:
3177 case Type::FunctionProto:
3178 case Type::ExtVector:
3179 case Type::ObjCQualifiedInterface:
3180 assert(false && "Types are eliminated above");
3181 return QualType();
3182
Chris Lattner1adb8832008-01-14 05:45:46 +00003183 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003184 {
3185 // Merge two pointer types, while trying to preserve typedef info
3186 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3187 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3188 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3189 if (ResultType.isNull()) return QualType();
Eli Friedman07d25872009-06-02 05:28:56 +00003190 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00003191 return LHS;
Eli Friedman07d25872009-06-02 05:28:56 +00003192 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00003193 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003194 return getPointerType(ResultType);
3195 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003196 case Type::BlockPointer:
3197 {
3198 // Merge two block pointer types, while trying to preserve typedef info
3199 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3200 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3201 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3202 if (ResultType.isNull()) return QualType();
3203 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3204 return LHS;
3205 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3206 return RHS;
3207 return getBlockPointerType(ResultType);
3208 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003209 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003210 {
3211 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3212 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3213 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3214 return QualType();
3215
3216 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3217 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3218 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3219 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003220 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3221 return LHS;
3222 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3223 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003224 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3225 ArrayType::ArraySizeModifier(), 0);
3226 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3227 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003228 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3229 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003230 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3231 return LHS;
3232 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3233 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003234 if (LVAT) {
3235 // FIXME: This isn't correct! But tricky to implement because
3236 // the array's size has to be the size of LHS, but the type
3237 // has to be different.
3238 return LHS;
3239 }
3240 if (RVAT) {
3241 // FIXME: This isn't correct! But tricky to implement because
3242 // the array's size has to be the size of RHS, but the type
3243 // has to be different.
3244 return RHS;
3245 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003246 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3247 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003248 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003249 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003250 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003251 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003252 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003253 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003254 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003255 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3256 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003257 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003258 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003259 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003260 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003261 case Type::Complex:
3262 // Distinct complex types are incompatible.
3263 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003264 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003265 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003266 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3267 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003268 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003269 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003270 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003271 // FIXME: This should be type compatibility, e.g. whether
3272 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003273 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3274 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3275 if (LHSIface && RHSIface &&
3276 canAssignObjCInterfaces(LHSIface, RHSIface))
3277 return LHS;
3278
Eli Friedman3d815e72008-08-22 00:56:42 +00003279 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003280 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003281 case Type::ObjCQualifiedId:
3282 // Distinct qualified id's are not compatible.
3283 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003284 case Type::FixedWidthInt:
3285 // Distinct fixed-width integers are not compatible.
3286 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003287 case Type::ExtQual:
3288 // FIXME: ExtQual types can be compatible even if they're not
3289 // identical!
3290 return QualType();
3291 // First attempt at an implementation, but I'm not really sure it's
3292 // right...
3293#if 0
3294 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3295 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3296 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3297 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3298 return QualType();
3299 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3300 LHSBase = QualType(LQual->getBaseType(), 0);
3301 RHSBase = QualType(RQual->getBaseType(), 0);
3302 ResultType = mergeTypes(LHSBase, RHSBase);
3303 if (ResultType.isNull()) return QualType();
3304 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3305 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3306 return LHS;
3307 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3308 return RHS;
3309 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3310 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3311 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3312 return ResultType;
3313#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003314
3315 case Type::TemplateSpecialization:
3316 assert(false && "Dependent types have no size");
3317 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003318 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003319
3320 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003321}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003322
Chris Lattner5426bf62008-04-07 07:01:58 +00003323//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003324// Integer Predicates
3325//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003326
Eli Friedmanad74a752008-06-28 06:23:08 +00003327unsigned ASTContext::getIntWidth(QualType T) {
3328 if (T == BoolTy)
3329 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003330 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3331 return FWIT->getWidth();
3332 }
3333 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003334 return (unsigned)getTypeSize(T);
3335}
3336
3337QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3338 assert(T->isSignedIntegerType() && "Unexpected type");
3339 if (const EnumType* ETy = T->getAsEnumType())
3340 T = ETy->getDecl()->getIntegerType();
3341 const BuiltinType* BTy = T->getAsBuiltinType();
3342 assert (BTy && "Unexpected signed integer type");
3343 switch (BTy->getKind()) {
3344 case BuiltinType::Char_S:
3345 case BuiltinType::SChar:
3346 return UnsignedCharTy;
3347 case BuiltinType::Short:
3348 return UnsignedShortTy;
3349 case BuiltinType::Int:
3350 return UnsignedIntTy;
3351 case BuiltinType::Long:
3352 return UnsignedLongTy;
3353 case BuiltinType::LongLong:
3354 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00003355 case BuiltinType::Int128:
3356 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00003357 default:
3358 assert(0 && "Unexpected signed integer type");
3359 return QualType();
3360 }
3361}
3362
Douglas Gregor2cf26342009-04-09 22:27:44 +00003363ExternalASTSource::~ExternalASTSource() { }
3364
3365void ExternalASTSource::PrintStats() { }