blob: 00eaa361a3f9122d742d92465ec8491eb3ce6480 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000019#include "clang/AST/ExternalASTSource.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000021#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000023#include "llvm/ADT/StringExtras.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000025#include "llvm/Support/MemoryBuffer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner61710852008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Douglas Gregor2deaea32009-04-22 18:49:13 +000035 bool FreeMem, unsigned size_reserve,
36 bool InitializeBuiltins) :
Douglas Gregorab452ba2009-03-26 23:50:42 +000037 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
38 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Douglas Gregor2cf26342009-04-09 22:27:44 +000039 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels),
40 ExternalSource(0) {
Daniel Dunbare91593e2008-08-11 04:54:23 +000041 if (size_reserve > 0) Types.reserve(size_reserve);
42 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
Douglas Gregor7a9cbed2009-04-26 03:57:37 +000044 BuiltinInfo.InitializeTargetBuiltins(Target);
Douglas Gregor2deaea32009-04-22 18:49:13 +000045 if (InitializeBuiltins)
46 this->InitializeBuiltins(idents);
Daniel Dunbare91593e2008-08-11 04:54:23 +000047}
48
Reid Spencer5f016e22007-07-11 17:01:13 +000049ASTContext::~ASTContext() {
50 // Deallocate all the types.
51 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000052 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000053 Types.pop_back();
54 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000055
Nuno Lopesb74668e2008-12-17 22:30:25 +000056 {
57 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
58 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
59 while (I != E) {
60 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
61 delete R;
62 }
63 }
64
65 {
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +000066 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
67 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopesb74668e2008-12-17 22:30:25 +000068 while (I != E) {
69 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
70 delete R;
71 }
72 }
73
Douglas Gregorab452ba2009-03-26 23:50:42 +000074 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000075 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
76 NNS = NestedNameSpecifiers.begin(),
77 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregore7dcd782009-03-27 23:25:45 +000078 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000079 /* Increment in loop */)
80 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000081
82 if (GlobalNestedNameSpecifier)
83 GlobalNestedNameSpecifier->Destroy(*this);
84
Eli Friedmanb26153c2008-05-27 03:08:09 +000085 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000086}
87
Douglas Gregor2deaea32009-04-22 18:49:13 +000088void ASTContext::InitializeBuiltins(IdentifierTable &idents) {
Douglas Gregor2deaea32009-04-22 18:49:13 +000089 BuiltinInfo.InitializeBuiltins(idents, LangOpts.NoBuiltin);
90}
91
Douglas Gregor2cf26342009-04-09 22:27:44 +000092void
93ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
94 ExternalSource.reset(Source.take());
95}
96
Reid Spencer5f016e22007-07-11 17:01:13 +000097void ASTContext::PrintStats() const {
98 fprintf(stderr, "*** AST Context Stats:\n");
99 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000100
Douglas Gregordbe833d2009-05-26 14:40:08 +0000101 unsigned counts[] = {
102#define TYPE(Name, Parent) 0,
103#define ABSTRACT_TYPE(Name, Parent)
104#include "clang/AST/TypeNodes.def"
105 0 // Extra
106 };
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000107
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
109 Type *T = Types[i];
Douglas Gregordbe833d2009-05-26 14:40:08 +0000110 counts[(unsigned)T->getTypeClass()]++;
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 }
112
Douglas Gregordbe833d2009-05-26 14:40:08 +0000113 unsigned Idx = 0;
114 unsigned TotalBytes = 0;
115#define TYPE(Name, Parent) \
116 if (counts[Idx]) \
117 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
118 TotalBytes += counts[Idx] * sizeof(Name##Type); \
119 ++Idx;
120#define ABSTRACT_TYPE(Name, Parent)
121#include "clang/AST/TypeNodes.def"
122
123 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000124
125 if (ExternalSource.get()) {
126 fprintf(stderr, "\n");
127 ExternalSource->PrintStats();
128 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000129}
130
131
132void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000133 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000134}
135
Reid Spencer5f016e22007-07-11 17:01:13 +0000136void ASTContext::InitBuiltinTypes() {
137 assert(VoidTy.isNull() && "Context reinitialized?");
138
139 // C99 6.2.5p19.
140 InitBuiltinType(VoidTy, BuiltinType::Void);
141
142 // C99 6.2.5p2.
143 InitBuiltinType(BoolTy, BuiltinType::Bool);
144 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000145 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 InitBuiltinType(CharTy, BuiltinType::Char_S);
147 else
148 InitBuiltinType(CharTy, BuiltinType::Char_U);
149 // C99 6.2.5p4.
150 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
151 InitBuiltinType(ShortTy, BuiltinType::Short);
152 InitBuiltinType(IntTy, BuiltinType::Int);
153 InitBuiltinType(LongTy, BuiltinType::Long);
154 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
155
156 // C99 6.2.5p6.
157 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
158 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
159 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
160 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
161 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
162
163 // C99 6.2.5p10.
164 InitBuiltinType(FloatTy, BuiltinType::Float);
165 InitBuiltinType(DoubleTy, BuiltinType::Double);
166 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000167
Chris Lattner2df9ced2009-04-30 02:43:43 +0000168 // GNU extension, 128-bit integers.
169 InitBuiltinType(Int128Ty, BuiltinType::Int128);
170 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
171
Chris Lattner3a250322009-02-26 23:43:47 +0000172 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
173 InitBuiltinType(WCharTy, BuiltinType::WChar);
174 else // C99
175 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000176
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000177 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000178 InitBuiltinType(OverloadTy, BuiltinType::Overload);
179
180 // Placeholder type for type-dependent expressions whose type is
181 // completely unknown. No code should ever check a type against
182 // DependentTy and users should never see it; however, it is here to
183 // help diagnose failures to properly check for type-dependent
184 // expressions.
185 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000186
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 // C99 6.2.5p11.
188 FloatComplexTy = getComplexType(FloatTy);
189 DoubleComplexTy = getComplexType(DoubleTy);
190 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000191
Steve Naroff7e219e42007-10-15 14:41:52 +0000192 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000193 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000194 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000195 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000196 ClassStructType = 0;
197
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000198 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000199
200 // void * type
201 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000202
203 // nullptr type (C++0x 2.14.7)
204 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000205}
206
Chris Lattner464175b2007-07-18 17:52:12 +0000207//===----------------------------------------------------------------------===//
208// Type Sizing and Analysis
209//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000210
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000211/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
212/// scalar floating point type.
213const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
214 const BuiltinType *BT = T->getAsBuiltinType();
215 assert(BT && "Not a floating point type!");
216 switch (BT->getKind()) {
217 default: assert(0 && "Not a floating point type!");
218 case BuiltinType::Float: return Target.getFloatFormat();
219 case BuiltinType::Double: return Target.getDoubleFormat();
220 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
221 }
222}
223
Chris Lattneraf707ab2009-01-24 21:53:27 +0000224/// getDeclAlign - Return a conservative estimate of the alignment of the
225/// specified decl. Note that bitfields do not have a valid alignment, so
226/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000227unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000228 unsigned Align = Target.getCharWidth();
229
230 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
231 Align = std::max(Align, AA->getAlignment());
232
Chris Lattneraf707ab2009-01-24 21:53:27 +0000233 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
234 QualType T = VD->getType();
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000235 if (const ReferenceType* RT = T->getAsReferenceType()) {
236 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000237 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000238 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
239 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000240 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
241 T = cast<ArrayType>(T)->getElementType();
242
243 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
244 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000245 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000246
247 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000248}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000249
Chris Lattnera7674d82007-07-13 22:13:22 +0000250/// getTypeSize - Return the size of the specified type, in bits. This method
251/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000252std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000253ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000254 uint64_t Width=0;
255 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000256 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000257#define TYPE(Class, Base)
258#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000259#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000260#define DEPENDENT_TYPE(Class, Base) case Type::Class:
261#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000262 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000263 break;
264
Chris Lattner692233e2007-07-13 22:27:08 +0000265 case Type::FunctionNoProto:
266 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000267 // GCC extension: alignof(function) = 32 bits
268 Width = 0;
269 Align = 32;
270 break;
271
Douglas Gregor72564e72009-02-26 23:50:07 +0000272 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000273 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000274 Width = 0;
275 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
276 break;
277
Steve Narofffb22d962007-08-30 01:06:46 +0000278 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000279 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000280
Chris Lattner98be4942008-03-05 18:54:05 +0000281 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000282 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000283 Align = EltInfo.second;
284 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000285 }
Nate Begeman213541a2008-04-18 23:10:10 +0000286 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000287 case Type::Vector: {
288 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000289 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000290 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000291 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000292 // If the alignment is not a power of 2, round up to the next power of 2.
293 // This happens for non-power-of-2 length vectors.
294 // FIXME: this should probably be a target property.
295 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000296 break;
297 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000298
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000299 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000300 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000301 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000302 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000303 // GCC extension: alignof(void) = 8 bits.
304 Width = 0;
305 Align = 8;
306 break;
307
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000308 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000309 Width = Target.getBoolWidth();
310 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000311 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000312 case BuiltinType::Char_S:
313 case BuiltinType::Char_U:
314 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000315 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000316 Width = Target.getCharWidth();
317 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000318 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000319 case BuiltinType::WChar:
320 Width = Target.getWCharWidth();
321 Align = Target.getWCharAlign();
322 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000323 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000324 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000325 Width = Target.getShortWidth();
326 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000327 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000328 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000329 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000330 Width = Target.getIntWidth();
331 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000332 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000333 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000334 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000335 Width = Target.getLongWidth();
336 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000337 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000338 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000339 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000340 Width = Target.getLongLongWidth();
341 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000342 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000343 case BuiltinType::Int128:
344 case BuiltinType::UInt128:
345 Width = 128;
346 Align = 128; // int128_t is 128-bit aligned on all targets.
347 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000348 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000349 Width = Target.getFloatWidth();
350 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000351 break;
352 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000353 Width = Target.getDoubleWidth();
354 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000355 break;
356 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000357 Width = Target.getLongDoubleWidth();
358 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000359 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000360 case BuiltinType::NullPtr:
361 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
362 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000363 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000364 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000365 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000366 case Type::FixedWidthInt:
367 // FIXME: This isn't precisely correct; the width/alignment should depend
368 // on the available types for the target
369 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000370 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000371 Align = Width;
372 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000373 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000374 // FIXME: Pointers into different addr spaces could have different sizes and
375 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000376 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000377 case Type::ObjCQualifiedId:
Douglas Gregor72564e72009-02-26 23:50:07 +0000378 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000379 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000380 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000381 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000382 case Type::BlockPointer: {
383 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
384 Width = Target.getPointerWidth(AS);
385 Align = Target.getPointerAlign(AS);
386 break;
387 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000388 case Type::Pointer: {
389 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000390 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000391 Align = Target.getPointerAlign(AS);
392 break;
393 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000394 case Type::LValueReference:
395 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000396 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000397 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000398 // FIXME: This is wrong for struct layout: a reference in a struct has
399 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000400 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000401 case Type::MemberPointer: {
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000402 // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
403 // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
404 // If we ever want to support other ABIs this needs to be abstracted.
405
Sebastian Redlf30208a2009-01-24 21:16:55 +0000406 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000407 std::pair<uint64_t, unsigned> PtrDiffInfo =
408 getTypeInfo(getPointerDiffType());
409 Width = PtrDiffInfo.first;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000410 if (Pointee->isFunctionType())
411 Width *= 2;
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000412 Align = PtrDiffInfo.second;
413 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000414 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000415 case Type::Complex: {
416 // Complex types have the same alignment as their elements, but twice the
417 // size.
418 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000419 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000420 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000421 Align = EltInfo.second;
422 break;
423 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000424 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000425 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000426 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
427 Width = Layout.getSize();
428 Align = Layout.getAlignment();
429 break;
430 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000431 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000432 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000433 const TagType *TT = cast<TagType>(T);
434
435 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000436 Width = 1;
437 Align = 1;
438 break;
439 }
440
Daniel Dunbar1d751182008-11-08 05:48:37 +0000441 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000442 return getTypeInfo(ET->getDecl()->getIntegerType());
443
Daniel Dunbar1d751182008-11-08 05:48:37 +0000444 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000445 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
446 Width = Layout.getSize();
447 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000448 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000449 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000450
Douglas Gregor18857642009-04-30 17:32:17 +0000451 case Type::Typedef: {
452 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
453 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
454 Align = Aligned->getAlignment();
455 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
456 } else
457 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000458 break;
Chris Lattner71763312008-04-06 22:05:18 +0000459 }
Douglas Gregor18857642009-04-30 17:32:17 +0000460
461 case Type::TypeOfExpr:
462 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
463 .getTypePtr());
464
465 case Type::TypeOf:
466 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
467
468 case Type::QualifiedName:
469 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
470
471 case Type::TemplateSpecialization:
472 assert(getCanonicalType(T) != T &&
473 "Cannot request the size of a dependent type");
474 // FIXME: this is likely to be wrong once we support template
475 // aliases, since a template alias could refer to a typedef that
476 // has an __aligned__ attribute on it.
477 return getTypeInfo(getCanonicalType(T));
478 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000479
Chris Lattner464175b2007-07-18 17:52:12 +0000480 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000481 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000482}
483
Chris Lattner34ebde42009-01-27 18:08:34 +0000484/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
485/// type for the current target in bits. This can be different than the ABI
486/// alignment in cases where it is beneficial for performance to overalign
487/// a data type.
488unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
489 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman1eed6022009-05-25 21:27:19 +0000490
491 // Double and long long should be naturally aligned if possible.
492 if (const ComplexType* CT = T->getAsComplexType())
493 T = CT->getElementType().getTypePtr();
494 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
495 T->isSpecificBuiltinType(BuiltinType::LongLong))
496 return std::max(ABIAlign, (unsigned)getTypeSize(T));
497
Chris Lattner34ebde42009-01-27 18:08:34 +0000498 return ABIAlign;
499}
500
501
Devang Patel8b277042008-06-04 21:22:16 +0000502/// LayoutField - Field layout.
503void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000504 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000505 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000506 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000507 uint64_t FieldOffset = IsUnion ? 0 : Size;
508 uint64_t FieldSize;
509 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000510
511 // FIXME: Should this override struct packing? Probably we want to
512 // take the minimum?
513 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
514 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000515
516 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
517 // TODO: Need to check this algorithm on other targets!
518 // (tested on Linux-X86)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000519 FieldSize = BitWidthExpr->EvaluateAsInt(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000520
521 std::pair<uint64_t, unsigned> FieldInfo =
522 Context.getTypeInfo(FD->getType());
523 uint64_t TypeSize = FieldInfo.first;
524
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000525 // Determine the alignment of this bitfield. The packing
526 // attributes define a maximum and the alignment attribute defines
527 // a minimum.
528 // FIXME: What is the right behavior when the specified alignment
529 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000530 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000531 if (FieldPacking)
532 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000533 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
534 FieldAlign = std::max(FieldAlign, AA->getAlignment());
535
536 // Check if we need to add padding to give the field the correct
537 // alignment.
538 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
539 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
540
541 // Padding members don't affect overall alignment
542 if (!FD->getIdentifier())
543 FieldAlign = 1;
544 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000545 if (FD->getType()->isIncompleteArrayType()) {
546 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000547 // query getTypeInfo about these, so we figure it out here.
548 // Flexible array members don't have any size, but they
549 // have to be aligned appropriately for their element type.
550 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000551 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000552 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson2f1169f2009-04-10 05:31:15 +0000553 } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
554 unsigned AS = RT->getPointeeType().getAddressSpace();
555 FieldSize = Context.Target.getPointerWidth(AS);
556 FieldAlign = Context.Target.getPointerAlign(AS);
Devang Patel8b277042008-06-04 21:22:16 +0000557 } else {
558 std::pair<uint64_t, unsigned> FieldInfo =
559 Context.getTypeInfo(FD->getType());
560 FieldSize = FieldInfo.first;
561 FieldAlign = FieldInfo.second;
562 }
563
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000564 // Determine the alignment of this bitfield. The packing
565 // attributes define a maximum and the alignment attribute defines
566 // a minimum. Additionally, the packing alignment must be at least
567 // a byte for non-bitfields.
568 //
569 // FIXME: What is the right behavior when the specified alignment
570 // is smaller than the specified packing?
571 if (FieldPacking)
572 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000573 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
574 FieldAlign = std::max(FieldAlign, AA->getAlignment());
575
576 // Round up the current record size to the field's alignment boundary.
577 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
578 }
579
580 // Place this field at the current location.
581 FieldOffsets[FieldNo] = FieldOffset;
582
583 // Reserve space for this field.
584 if (IsUnion) {
585 Size = std::max(Size, FieldSize);
586 } else {
587 Size = FieldOffset + FieldSize;
588 }
589
Daniel Dunbard6884a02009-05-04 05:16:21 +0000590 // Remember the next available offset.
591 NextOffset = Size;
592
Devang Patel8b277042008-06-04 21:22:16 +0000593 // Remember max struct/class alignment.
594 Alignment = std::max(Alignment, FieldAlign);
595}
596
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000597static void CollectLocalObjCIvars(ASTContext *Ctx,
598 const ObjCInterfaceDecl *OI,
599 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000600 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
601 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000602 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000603 if (!IVDecl->isInvalidDecl())
604 Fields.push_back(cast<FieldDecl>(IVDecl));
605 }
606}
607
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000608void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
609 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
610 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
611 CollectObjCIvars(SuperClass, Fields);
612 CollectLocalObjCIvars(this, OI, Fields);
613}
614
Fariborz Jahanian98200742009-05-12 18:14:29 +0000615void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
616 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
617 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(*this),
618 E = PD->prop_end(*this); I != E; ++I)
619 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
620 Ivars.push_back(Ivar);
621
622 // Also look into nested protocols.
623 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
624 E = PD->protocol_end(); P != E; ++P)
625 CollectProtocolSynthesizedIvars(*P, Ivars);
626}
627
628/// CollectSynthesizedIvars -
629/// This routine collect synthesized ivars for the designated class.
630///
631void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
632 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
633 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*this),
634 E = OI->prop_end(*this); I != E; ++I) {
635 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
636 Ivars.push_back(Ivar);
637 }
638 // Also look into interface's protocol list for properties declared
639 // in the protocol and whose ivars are synthesized.
640 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
641 PE = OI->protocol_end(); P != PE; ++P) {
642 ObjCProtocolDecl *PD = (*P);
643 CollectProtocolSynthesizedIvars(PD, Ivars);
644 }
645}
646
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000647/// getInterfaceLayoutImpl - Get or compute information about the
648/// layout of the given interface.
649///
650/// \param Impl - If given, also include the layout of the interface's
651/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000652const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000653ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
654 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000655 assert(!D->isForwardDecl() && "Invalid interface decl!");
656
Devang Patel44a3dde2008-06-04 21:54:36 +0000657 // Look up this layout, if already laid out, return what we have.
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000658 ObjCContainerDecl *Key =
659 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
660 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
661 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000662
Daniel Dunbar453addb2009-05-03 11:16:44 +0000663 unsigned FieldCount = D->ivar_size();
664 // Add in synthesized ivar count if laying out an implementation.
665 if (Impl) {
Fariborz Jahanian98200742009-05-12 18:14:29 +0000666 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
667 CollectSynthesizedIvars(D, Ivars);
668 FieldCount += Ivars.size();
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000669 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +0000670 // entry. Note we can't cache this because we simply free all
671 // entries later; however we shouldn't look up implementations
672 // frequently.
673 if (FieldCount == D->ivar_size())
674 return getObjCLayout(D, 0);
675 }
676
Devang Patel6a5a34c2008-06-06 02:14:01 +0000677 ASTRecordLayout *NewEntry = NULL;
Devang Patel6a5a34c2008-06-06 02:14:01 +0000678 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Devang Patel6a5a34c2008-06-06 02:14:01 +0000679 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
680 unsigned Alignment = SL.getAlignment();
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000681
Daniel Dunbar913af352009-05-07 21:58:26 +0000682 // We start laying out ivars not at the end of the superclass
683 // structure, but at the next byte following the last field.
684 uint64_t Size = llvm::RoundUpToAlignment(SL.NextOffset, 8);
Daniel Dunbard6884a02009-05-04 05:16:21 +0000685
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000686 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout(Size, Alignment);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000687 NewEntry->InitializeLayout(FieldCount);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000688 } else {
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000689 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout();
Devang Patel6a5a34c2008-06-06 02:14:01 +0000690 NewEntry->InitializeLayout(FieldCount);
691 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000692
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000693 unsigned StructPacking = 0;
694 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
695 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000696
697 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
698 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
699 AA->getAlignment()));
700
701 // Layout each ivar sequentially.
702 unsigned i = 0;
703 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
704 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
705 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000706 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000707 }
Daniel Dunbar453addb2009-05-03 11:16:44 +0000708 // And synthesized ivars, if this is an implementation.
709 if (Impl) {
Fariborz Jahanian98200742009-05-12 18:14:29 +0000710 // FIXME. Do we need to colltect twice?
711 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
712 CollectSynthesizedIvars(D, Ivars);
713 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
714 NewEntry->LayoutField(Ivars[k], i++, false, StructPacking, *this);
Fariborz Jahanian18191882009-03-31 18:11:23 +0000715 }
Fariborz Jahanian99eee362009-04-01 19:37:34 +0000716
Devang Patel44a3dde2008-06-04 21:54:36 +0000717 // Finally, round the size of the total struct up to the alignment of the
718 // struct itself.
719 NewEntry->FinalizeLayout();
720 return *NewEntry;
721}
722
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000723const ASTRecordLayout &
724ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
725 return getObjCLayout(D, 0);
726}
727
728const ASTRecordLayout &
729ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
730 return getObjCLayout(D->getClassInterface(), D);
731}
732
Devang Patel88a981b2007-11-01 19:11:01 +0000733/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000734/// specified record (struct/union/class), which indicates its size and field
735/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000736const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000737 D = D->getDefinition(*this);
738 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000739
Chris Lattner464175b2007-07-18 17:52:12 +0000740 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000741 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000742 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000743
Devang Patel88a981b2007-11-01 19:11:01 +0000744 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
745 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
746 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000747 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000748
Douglas Gregore267ff32008-12-11 20:41:00 +0000749 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000750 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
751 D->field_end(*this)));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000752 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000753
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000754 unsigned StructPacking = 0;
755 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
756 StructPacking = PA->getAlignment();
757
Eli Friedman4bd998b2008-05-30 09:31:38 +0000758 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000759 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
760 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000761
Eli Friedman4bd998b2008-05-30 09:31:38 +0000762 // Layout each field, for now, just sequentially, respecting alignment. In
763 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000764 unsigned FieldIdx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000765 for (RecordDecl::field_iterator Field = D->field_begin(*this),
766 FieldEnd = D->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000767 Field != FieldEnd; (void)++Field, ++FieldIdx)
768 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000769
770 // Finally, round the size of the total struct up to the alignment of the
771 // struct itself.
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000772 NewEntry->FinalizeLayout(getLangOptions().CPlusPlus);
Chris Lattner5d2a6302007-07-18 18:26:58 +0000773 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000774}
775
Chris Lattnera7674d82007-07-13 22:13:22 +0000776//===----------------------------------------------------------------------===//
777// Type creation/memoization methods
778//===----------------------------------------------------------------------===//
779
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000780QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000781 QualType CanT = getCanonicalType(T);
782 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000783 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000784
785 // If we are composing extended qualifiers together, merge together into one
786 // ExtQualType node.
787 unsigned CVRQuals = T.getCVRQualifiers();
788 QualType::GCAttrTypes GCAttr = QualType::GCNone;
789 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000790
Chris Lattnerb7d25532009-02-18 22:53:11 +0000791 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
792 // If this type already has an address space specified, it cannot get
793 // another one.
794 assert(EQT->getAddressSpace() == 0 &&
795 "Type cannot be in multiple addr spaces!");
796 GCAttr = EQT->getObjCGCAttr();
797 TypeNode = EQT->getBaseType();
798 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000799
Chris Lattnerb7d25532009-02-18 22:53:11 +0000800 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000801 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000802 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000803 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000804 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000805 return QualType(EXTQy, CVRQuals);
806
Christopher Lambebb97e92008-02-04 02:31:56 +0000807 // If the base type isn't canonical, this won't be a canonical type either,
808 // so fill in the canonical type field.
809 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000810 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000811 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000812
Chris Lattnerb7d25532009-02-18 22:53:11 +0000813 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000814 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000815 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000816 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000817 ExtQualType *New =
818 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000819 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000820 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000821 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000822}
823
Chris Lattnerb7d25532009-02-18 22:53:11 +0000824QualType ASTContext::getObjCGCQualType(QualType T,
825 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000826 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000827 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000828 return T;
829
Chris Lattnerb7d25532009-02-18 22:53:11 +0000830 // If we are composing extended qualifiers together, merge together into one
831 // ExtQualType node.
832 unsigned CVRQuals = T.getCVRQualifiers();
833 Type *TypeNode = T.getTypePtr();
834 unsigned AddressSpace = 0;
835
836 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
837 // If this type already has an address space specified, it cannot get
838 // another one.
839 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
840 "Type cannot be in multiple addr spaces!");
841 AddressSpace = EQT->getAddressSpace();
842 TypeNode = EQT->getBaseType();
843 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000844
845 // Check if we've already instantiated an gc qual'd type of this type.
846 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000847 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000848 void *InsertPos = 0;
849 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000850 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000851
852 // If the base type isn't canonical, this won't be a canonical type either,
853 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000854 // FIXME: Isn't this also not canonical if the base type is a array
855 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000856 QualType Canonical;
857 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000858 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000859
Chris Lattnerb7d25532009-02-18 22:53:11 +0000860 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000861 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
862 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
863 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000864 ExtQualType *New =
865 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000866 ExtQualTypes.InsertNode(New, InsertPos);
867 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000868 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000869}
Chris Lattnera7674d82007-07-13 22:13:22 +0000870
Reid Spencer5f016e22007-07-11 17:01:13 +0000871/// getComplexType - Return the uniqued reference to the type for a complex
872/// number with the specified element type.
873QualType ASTContext::getComplexType(QualType T) {
874 // Unique pointers, to guarantee there is only one pointer of a particular
875 // structure.
876 llvm::FoldingSetNodeID ID;
877 ComplexType::Profile(ID, T);
878
879 void *InsertPos = 0;
880 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
881 return QualType(CT, 0);
882
883 // If the pointee type isn't canonical, this won't be a canonical type either,
884 // so fill in the canonical type field.
885 QualType Canonical;
886 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000887 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000888
889 // Get the new insert position for the node we care about.
890 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000891 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000892 }
Steve Narofff83820b2009-01-27 22:08:43 +0000893 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000894 Types.push_back(New);
895 ComplexTypes.InsertNode(New, InsertPos);
896 return QualType(New, 0);
897}
898
Eli Friedmanf98aba32009-02-13 02:31:07 +0000899QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
900 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
901 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
902 FixedWidthIntType *&Entry = Map[Width];
903 if (!Entry)
904 Entry = new FixedWidthIntType(Width, Signed);
905 return QualType(Entry, 0);
906}
Reid Spencer5f016e22007-07-11 17:01:13 +0000907
908/// getPointerType - Return the uniqued reference to the type for a pointer to
909/// the specified type.
910QualType ASTContext::getPointerType(QualType T) {
911 // Unique pointers, to guarantee there is only one pointer of a particular
912 // structure.
913 llvm::FoldingSetNodeID ID;
914 PointerType::Profile(ID, T);
915
916 void *InsertPos = 0;
917 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
918 return QualType(PT, 0);
919
920 // If the pointee type isn't canonical, this won't be a canonical type either,
921 // so fill in the canonical type field.
922 QualType Canonical;
923 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000924 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000925
926 // Get the new insert position for the node we care about.
927 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000928 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000929 }
Steve Narofff83820b2009-01-27 22:08:43 +0000930 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000931 Types.push_back(New);
932 PointerTypes.InsertNode(New, InsertPos);
933 return QualType(New, 0);
934}
935
Steve Naroff5618bd42008-08-27 16:04:49 +0000936/// getBlockPointerType - Return the uniqued reference to the type for
937/// a pointer to the specified block.
938QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000939 assert(T->isFunctionType() && "block of function types only");
940 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000941 // structure.
942 llvm::FoldingSetNodeID ID;
943 BlockPointerType::Profile(ID, T);
944
945 void *InsertPos = 0;
946 if (BlockPointerType *PT =
947 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
948 return QualType(PT, 0);
949
Steve Naroff296e8d52008-08-28 19:20:44 +0000950 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000951 // type either so fill in the canonical type field.
952 QualType Canonical;
953 if (!T->isCanonical()) {
954 Canonical = getBlockPointerType(getCanonicalType(T));
955
956 // Get the new insert position for the node we care about.
957 BlockPointerType *NewIP =
958 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000959 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000960 }
Steve Narofff83820b2009-01-27 22:08:43 +0000961 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000962 Types.push_back(New);
963 BlockPointerTypes.InsertNode(New, InsertPos);
964 return QualType(New, 0);
965}
966
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000967/// getLValueReferenceType - Return the uniqued reference to the type for an
968/// lvalue reference to the specified type.
969QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000970 // Unique pointers, to guarantee there is only one pointer of a particular
971 // structure.
972 llvm::FoldingSetNodeID ID;
973 ReferenceType::Profile(ID, T);
974
975 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000976 if (LValueReferenceType *RT =
977 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000978 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000979
Reid Spencer5f016e22007-07-11 17:01:13 +0000980 // If the referencee type isn't canonical, this won't be a canonical type
981 // either, so fill in the canonical type field.
982 QualType Canonical;
983 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000984 Canonical = getLValueReferenceType(getCanonicalType(T));
985
Reid Spencer5f016e22007-07-11 17:01:13 +0000986 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000987 LValueReferenceType *NewIP =
988 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000989 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000990 }
991
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000992 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000994 LValueReferenceTypes.InsertNode(New, InsertPos);
995 return QualType(New, 0);
996}
997
998/// getRValueReferenceType - Return the uniqued reference to the type for an
999/// rvalue reference to the specified type.
1000QualType ASTContext::getRValueReferenceType(QualType T) {
1001 // Unique pointers, to guarantee there is only one pointer of a particular
1002 // structure.
1003 llvm::FoldingSetNodeID ID;
1004 ReferenceType::Profile(ID, T);
1005
1006 void *InsertPos = 0;
1007 if (RValueReferenceType *RT =
1008 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1009 return QualType(RT, 0);
1010
1011 // If the referencee type isn't canonical, this won't be a canonical type
1012 // either, so fill in the canonical type field.
1013 QualType Canonical;
1014 if (!T->isCanonical()) {
1015 Canonical = getRValueReferenceType(getCanonicalType(T));
1016
1017 // Get the new insert position for the node we care about.
1018 RValueReferenceType *NewIP =
1019 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1020 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1021 }
1022
1023 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1024 Types.push_back(New);
1025 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001026 return QualType(New, 0);
1027}
1028
Sebastian Redlf30208a2009-01-24 21:16:55 +00001029/// getMemberPointerType - Return the uniqued reference to the type for a
1030/// member pointer to the specified type, in the specified class.
1031QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1032{
1033 // Unique pointers, to guarantee there is only one pointer of a particular
1034 // structure.
1035 llvm::FoldingSetNodeID ID;
1036 MemberPointerType::Profile(ID, T, Cls);
1037
1038 void *InsertPos = 0;
1039 if (MemberPointerType *PT =
1040 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1041 return QualType(PT, 0);
1042
1043 // If the pointee or class type isn't canonical, this won't be a canonical
1044 // type either, so fill in the canonical type field.
1045 QualType Canonical;
1046 if (!T->isCanonical()) {
1047 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1048
1049 // Get the new insert position for the node we care about.
1050 MemberPointerType *NewIP =
1051 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1052 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1053 }
Steve Narofff83820b2009-01-27 22:08:43 +00001054 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001055 Types.push_back(New);
1056 MemberPointerTypes.InsertNode(New, InsertPos);
1057 return QualType(New, 0);
1058}
1059
Steve Narofffb22d962007-08-30 01:06:46 +00001060/// getConstantArrayType - Return the unique reference to the type for an
1061/// array of the specified element type.
1062QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001063 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001064 ArrayType::ArraySizeModifier ASM,
1065 unsigned EltTypeQuals) {
Eli Friedman587cbdf2009-05-29 20:17:55 +00001066 assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) &&
1067 "Constant array of VLAs is illegal!");
1068
Chris Lattner38aeec72009-05-13 04:12:56 +00001069 // Convert the array size into a canonical width matching the pointer size for
1070 // the target.
1071 llvm::APInt ArySize(ArySizeIn);
1072 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1073
Reid Spencer5f016e22007-07-11 17:01:13 +00001074 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001075 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001076
1077 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001078 if (ConstantArrayType *ATP =
1079 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001080 return QualType(ATP, 0);
1081
1082 // If the element type isn't canonical, this won't be a canonical type either,
1083 // so fill in the canonical type field.
1084 QualType Canonical;
1085 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001086 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001087 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001089 ConstantArrayType *NewIP =
1090 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001091 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001092 }
1093
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001094 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001095 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001096 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001097 Types.push_back(New);
1098 return QualType(New, 0);
1099}
1100
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001101/// getVariableArrayType - Returns a non-unique reference to the type for a
1102/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001103QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1104 ArrayType::ArraySizeModifier ASM,
1105 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001106 // Since we don't unique expressions, it isn't possible to unique VLA's
1107 // that have an expression provided for their size.
1108
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001109 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001110 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001111
1112 VariableArrayTypes.push_back(New);
1113 Types.push_back(New);
1114 return QualType(New, 0);
1115}
1116
Douglas Gregor898574e2008-12-05 23:32:09 +00001117/// getDependentSizedArrayType - Returns a non-unique reference to
1118/// the type for a dependently-sized array of the specified element
1119/// type. FIXME: We will need these to be uniqued, or at least
1120/// comparable, at some point.
1121QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1122 ArrayType::ArraySizeModifier ASM,
1123 unsigned EltTypeQuals) {
1124 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1125 "Size must be type- or value-dependent!");
1126
1127 // Since we don't unique expressions, it isn't possible to unique
1128 // dependently-sized array types.
1129
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001130 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001131 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1132 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001133
1134 DependentSizedArrayTypes.push_back(New);
1135 Types.push_back(New);
1136 return QualType(New, 0);
1137}
1138
Eli Friedmanc5773c42008-02-15 18:16:39 +00001139QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1140 ArrayType::ArraySizeModifier ASM,
1141 unsigned EltTypeQuals) {
1142 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001143 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001144
1145 void *InsertPos = 0;
1146 if (IncompleteArrayType *ATP =
1147 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1148 return QualType(ATP, 0);
1149
1150 // If the element type isn't canonical, this won't be a canonical type
1151 // either, so fill in the canonical type field.
1152 QualType Canonical;
1153
1154 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001155 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001156 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001157
1158 // Get the new insert position for the node we care about.
1159 IncompleteArrayType *NewIP =
1160 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001161 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001162 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001163
Steve Narofff83820b2009-01-27 22:08:43 +00001164 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001165 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001166
1167 IncompleteArrayTypes.InsertNode(New, InsertPos);
1168 Types.push_back(New);
1169 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001170}
1171
Steve Naroff73322922007-07-18 18:00:27 +00001172/// getVectorType - Return the unique reference to a vector type of
1173/// the specified element type and size. VectorType must be a built-in type.
1174QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 BuiltinType *baseType;
1176
Chris Lattnerf52ab252008-04-06 22:59:24 +00001177 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001178 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001179
1180 // Check if we've already instantiated a vector of this type.
1181 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001182 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 void *InsertPos = 0;
1184 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1185 return QualType(VTP, 0);
1186
1187 // If the element type isn't canonical, this won't be a canonical type either,
1188 // so fill in the canonical type field.
1189 QualType Canonical;
1190 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001191 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001192
1193 // Get the new insert position for the node we care about.
1194 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001195 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001196 }
Steve Narofff83820b2009-01-27 22:08:43 +00001197 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 VectorTypes.InsertNode(New, InsertPos);
1199 Types.push_back(New);
1200 return QualType(New, 0);
1201}
1202
Nate Begeman213541a2008-04-18 23:10:10 +00001203/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001204/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001205QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001206 BuiltinType *baseType;
1207
Chris Lattnerf52ab252008-04-06 22:59:24 +00001208 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001209 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001210
1211 // Check if we've already instantiated a vector of this type.
1212 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001213 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001214 void *InsertPos = 0;
1215 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1216 return QualType(VTP, 0);
1217
1218 // If the element type isn't canonical, this won't be a canonical type either,
1219 // so fill in the canonical type field.
1220 QualType Canonical;
1221 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001222 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001223
1224 // Get the new insert position for the node we care about.
1225 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001226 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001227 }
Steve Narofff83820b2009-01-27 22:08:43 +00001228 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001229 VectorTypes.InsertNode(New, InsertPos);
1230 Types.push_back(New);
1231 return QualType(New, 0);
1232}
1233
Douglas Gregor72564e72009-02-26 23:50:07 +00001234/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001235///
Douglas Gregor72564e72009-02-26 23:50:07 +00001236QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001237 // Unique functions, to guarantee there is only one function of a particular
1238 // structure.
1239 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001240 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001241
1242 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001243 if (FunctionNoProtoType *FT =
1244 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 return QualType(FT, 0);
1246
1247 QualType Canonical;
1248 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001249 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001250
1251 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001252 FunctionNoProtoType *NewIP =
1253 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001254 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 }
1256
Douglas Gregor72564e72009-02-26 23:50:07 +00001257 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001258 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001259 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001260 return QualType(New, 0);
1261}
1262
1263/// getFunctionType - Return a normal function type with a typed argument
1264/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001265QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001266 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001267 unsigned TypeQuals, bool hasExceptionSpec,
1268 bool hasAnyExceptionSpec, unsigned NumExs,
1269 const QualType *ExArray) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 // Unique functions, to guarantee there is only one function of a particular
1271 // structure.
1272 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001273 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001274 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1275 NumExs, ExArray);
Reid Spencer5f016e22007-07-11 17:01:13 +00001276
1277 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001278 if (FunctionProtoType *FTP =
1279 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001281
1282 // Determine whether the type being created is already canonical or not.
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 bool isCanonical = ResultTy->isCanonical();
Sebastian Redl465226e2009-05-27 22:11:52 +00001284 if (hasExceptionSpec)
1285 isCanonical = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001286 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1287 if (!ArgArray[i]->isCanonical())
1288 isCanonical = false;
1289
1290 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001291 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001292 QualType Canonical;
1293 if (!isCanonical) {
1294 llvm::SmallVector<QualType, 16> CanonicalArgs;
1295 CanonicalArgs.reserve(NumArgs);
1296 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001297 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001298
Chris Lattnerf52ab252008-04-06 22:59:24 +00001299 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001300 CanonicalArgs.data(), NumArgs,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001301 isVariadic, TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +00001302
Reid Spencer5f016e22007-07-11 17:01:13 +00001303 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001304 FunctionProtoType *NewIP =
1305 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001306 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001307 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001308
Douglas Gregor72564e72009-02-26 23:50:07 +00001309 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001310 // for two variable size arrays (for parameter and exception types) at the
1311 // end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001312 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001313 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1314 NumArgs*sizeof(QualType) +
1315 NumExs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001316 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001317 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1318 ExArray, NumExs, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001319 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001320 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001321 return QualType(FTP, 0);
1322}
1323
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001324/// getTypeDeclType - Return the unique reference to the type for the
1325/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001326QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001327 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001328 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1329
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001330 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001331 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001332 else if (isa<TemplateTypeParmDecl>(Decl)) {
1333 assert(false && "Template type parameter types are always available.");
1334 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001335 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001336
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001337 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001338 if (PrevDecl)
1339 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001340 else
1341 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001342 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001343 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1344 if (PrevDecl)
1345 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001346 else
1347 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001348 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001349 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001350 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001351
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001352 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001353 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001354}
1355
Reid Spencer5f016e22007-07-11 17:01:13 +00001356/// getTypedefType - Return the unique reference to the type for the
1357/// specified typename decl.
1358QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1359 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1360
Chris Lattnerf52ab252008-04-06 22:59:24 +00001361 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001362 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001363 Types.push_back(Decl->TypeForDecl);
1364 return QualType(Decl->TypeForDecl, 0);
1365}
1366
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001367/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001368/// specified ObjC interface decl.
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001369QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001370 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1371
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001372 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1373 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff3536b442007-09-06 21:24:23 +00001374 Types.push_back(Decl->TypeForDecl);
1375 return QualType(Decl->TypeForDecl, 0);
1376}
1377
Douglas Gregorfab9d672009-02-05 23:33:38 +00001378/// \brief Retrieve the template type parameter type for a template
1379/// parameter with the given depth, index, and (optionally) name.
1380QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1381 IdentifierInfo *Name) {
1382 llvm::FoldingSetNodeID ID;
1383 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1384 void *InsertPos = 0;
1385 TemplateTypeParmType *TypeParm
1386 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1387
1388 if (TypeParm)
1389 return QualType(TypeParm, 0);
1390
1391 if (Name)
1392 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1393 getTemplateTypeParmType(Depth, Index));
1394 else
1395 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1396
1397 Types.push_back(TypeParm);
1398 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1399
1400 return QualType(TypeParm, 0);
1401}
1402
Douglas Gregor55f6b142009-02-09 18:46:07 +00001403QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001404ASTContext::getTemplateSpecializationType(TemplateName Template,
1405 const TemplateArgument *Args,
1406 unsigned NumArgs,
1407 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001408 if (!Canon.isNull())
1409 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001410
Douglas Gregor55f6b142009-02-09 18:46:07 +00001411 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001412 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001413
Douglas Gregor55f6b142009-02-09 18:46:07 +00001414 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001415 TemplateSpecializationType *Spec
1416 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001417
1418 if (Spec)
1419 return QualType(Spec, 0);
1420
Douglas Gregor7532dc62009-03-30 22:58:21 +00001421 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001422 sizeof(TemplateArgument) * NumArgs),
1423 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001424 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001425 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001426 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001427
1428 return QualType(Spec, 0);
1429}
1430
Douglas Gregore4e5b052009-03-19 00:18:19 +00001431QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001432ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001433 QualType NamedType) {
1434 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001435 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001436
1437 void *InsertPos = 0;
1438 QualifiedNameType *T
1439 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1440 if (T)
1441 return QualType(T, 0);
1442
Douglas Gregorab452ba2009-03-26 23:50:42 +00001443 T = new (*this) QualifiedNameType(NNS, NamedType,
1444 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001445 Types.push_back(T);
1446 QualifiedNameTypes.InsertNode(T, InsertPos);
1447 return QualType(T, 0);
1448}
1449
Douglas Gregord57959a2009-03-27 23:10:48 +00001450QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1451 const IdentifierInfo *Name,
1452 QualType Canon) {
1453 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1454
1455 if (Canon.isNull()) {
1456 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1457 if (CanonNNS != NNS)
1458 Canon = getTypenameType(CanonNNS, Name);
1459 }
1460
1461 llvm::FoldingSetNodeID ID;
1462 TypenameType::Profile(ID, NNS, Name);
1463
1464 void *InsertPos = 0;
1465 TypenameType *T
1466 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1467 if (T)
1468 return QualType(T, 0);
1469
1470 T = new (*this) TypenameType(NNS, Name, Canon);
1471 Types.push_back(T);
1472 TypenameTypes.InsertNode(T, InsertPos);
1473 return QualType(T, 0);
1474}
1475
Douglas Gregor17343172009-04-01 00:28:59 +00001476QualType
1477ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1478 const TemplateSpecializationType *TemplateId,
1479 QualType Canon) {
1480 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1481
1482 if (Canon.isNull()) {
1483 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1484 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1485 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1486 const TemplateSpecializationType *CanonTemplateId
1487 = CanonType->getAsTemplateSpecializationType();
1488 assert(CanonTemplateId &&
1489 "Canonical type must also be a template specialization type");
1490 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1491 }
1492 }
1493
1494 llvm::FoldingSetNodeID ID;
1495 TypenameType::Profile(ID, NNS, TemplateId);
1496
1497 void *InsertPos = 0;
1498 TypenameType *T
1499 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1500 if (T)
1501 return QualType(T, 0);
1502
1503 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1504 Types.push_back(T);
1505 TypenameTypes.InsertNode(T, InsertPos);
1506 return QualType(T, 0);
1507}
1508
Chris Lattner88cb27a2008-04-07 04:56:42 +00001509/// CmpProtocolNames - Comparison predicate for sorting protocols
1510/// alphabetically.
1511static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1512 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001513 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001514}
1515
1516static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1517 unsigned &NumProtocols) {
1518 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1519
1520 // Sort protocols, keyed by name.
1521 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1522
1523 // Remove duplicates.
1524 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1525 NumProtocols = ProtocolsEnd-Protocols;
1526}
1527
1528
Chris Lattner065f0d72008-04-07 04:44:08 +00001529/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1530/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001531QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1532 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001533 // Sort the protocol list alphabetically to canonicalize it.
1534 SortAndUniqueProtocols(Protocols, NumProtocols);
1535
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001536 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001537 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001538
1539 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001540 if (ObjCQualifiedInterfaceType *QT =
1541 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001542 return QualType(QT, 0);
1543
1544 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001545 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001546 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001547
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001548 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001549 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001550 return QualType(QType, 0);
1551}
1552
Chris Lattner88cb27a2008-04-07 04:56:42 +00001553/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1554/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001555QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001556 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001557 // Sort the protocol list alphabetically to canonicalize it.
1558 SortAndUniqueProtocols(Protocols, NumProtocols);
1559
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001560 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001561 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001562
1563 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001564 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001565 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001566 return QualType(QT, 0);
1567
1568 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001569 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001570 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001571 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001572 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001573 return QualType(QType, 0);
1574}
1575
Douglas Gregor72564e72009-02-26 23:50:07 +00001576/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1577/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001578/// multiple declarations that refer to "typeof(x)" all contain different
1579/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1580/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001581QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001582 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001583 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001584 Types.push_back(toe);
1585 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001586}
1587
Steve Naroff9752f252007-08-01 18:02:17 +00001588/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1589/// TypeOfType AST's. The only motivation to unique these nodes would be
1590/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1591/// an issue. This doesn't effect the type checker, since it operates
1592/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001593QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001594 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001595 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001596 Types.push_back(tot);
1597 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001598}
1599
Reid Spencer5f016e22007-07-11 17:01:13 +00001600/// getTagDeclType - Return the unique reference to the type for the
1601/// specified TagDecl (struct/union/class/enum) decl.
1602QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001603 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001604 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001605}
1606
1607/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1608/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1609/// needs to agree with the definition in <stddef.h>.
1610QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001611 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001612}
1613
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001614/// getSignedWCharType - Return the type of "signed wchar_t".
1615/// Used when in C++, as a GCC extension.
1616QualType ASTContext::getSignedWCharType() const {
1617 // FIXME: derive from "Target" ?
1618 return WCharTy;
1619}
1620
1621/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1622/// Used when in C++, as a GCC extension.
1623QualType ASTContext::getUnsignedWCharType() const {
1624 // FIXME: derive from "Target" ?
1625 return UnsignedIntTy;
1626}
1627
Chris Lattner8b9023b2007-07-13 03:05:23 +00001628/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1629/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1630QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001631 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001632}
1633
Chris Lattnere6327742008-04-02 05:18:44 +00001634//===----------------------------------------------------------------------===//
1635// Type Operators
1636//===----------------------------------------------------------------------===//
1637
Chris Lattner77c96472008-04-06 22:41:35 +00001638/// getCanonicalType - Return the canonical (structural) type corresponding to
1639/// the specified potentially non-canonical type. The non-canonical version
1640/// of a type may have many "decorated" versions of types. Decorators can
1641/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1642/// to be free of any of these, allowing two canonical types to be compared
1643/// for exact equality with a simple pointer comparison.
1644QualType ASTContext::getCanonicalType(QualType T) {
1645 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001646
1647 // If the result has type qualifiers, make sure to canonicalize them as well.
1648 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1649 if (TypeQuals == 0) return CanType;
1650
1651 // If the type qualifiers are on an array type, get the canonical type of the
1652 // array with the qualifiers applied to the element type.
1653 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1654 if (!AT)
1655 return CanType.getQualifiedType(TypeQuals);
1656
1657 // Get the canonical version of the element with the extra qualifiers on it.
1658 // This can recursively sink qualifiers through multiple levels of arrays.
1659 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1660 NewEltTy = getCanonicalType(NewEltTy);
1661
1662 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1663 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1664 CAT->getIndexTypeQualifier());
1665 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1666 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1667 IAT->getIndexTypeQualifier());
1668
Douglas Gregor898574e2008-12-05 23:32:09 +00001669 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1670 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1671 DSAT->getSizeModifier(),
1672 DSAT->getIndexTypeQualifier());
1673
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001674 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1675 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1676 VAT->getSizeModifier(),
1677 VAT->getIndexTypeQualifier());
1678}
1679
Douglas Gregor7da97d02009-05-10 22:57:19 +00001680Decl *ASTContext::getCanonicalDecl(Decl *D) {
Douglas Gregorc4ccf012009-05-10 22:59:12 +00001681 if (!D)
1682 return 0;
1683
Douglas Gregor7da97d02009-05-10 22:57:19 +00001684 if (TagDecl *Tag = dyn_cast<TagDecl>(D)) {
1685 QualType T = getTagDeclType(Tag);
1686 return cast<TagDecl>(cast<TagType>(T.getTypePtr()->CanonicalType)
1687 ->getDecl());
1688 }
1689
1690 if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(D)) {
1691 while (Template->getPreviousDeclaration())
1692 Template = Template->getPreviousDeclaration();
1693 return Template;
1694 }
1695
1696 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
1697 while (Function->getPreviousDeclaration())
1698 Function = Function->getPreviousDeclaration();
1699 return const_cast<FunctionDecl *>(Function);
1700 }
1701
1702 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
1703 while (Var->getPreviousDeclaration())
1704 Var = Var->getPreviousDeclaration();
1705 return const_cast<VarDecl *>(Var);
1706 }
1707
1708 return D;
1709}
1710
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001711TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
1712 // If this template name refers to a template, the canonical
1713 // template name merely stores the template itself.
1714 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Douglas Gregor7da97d02009-05-10 22:57:19 +00001715 return TemplateName(cast<TemplateDecl>(getCanonicalDecl(Template)));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001716
1717 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
1718 assert(DTN && "Non-dependent template names must refer to template decls.");
1719 return DTN->CanonicalTemplateName;
1720}
1721
Douglas Gregord57959a2009-03-27 23:10:48 +00001722NestedNameSpecifier *
1723ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1724 if (!NNS)
1725 return 0;
1726
1727 switch (NNS->getKind()) {
1728 case NestedNameSpecifier::Identifier:
1729 // Canonicalize the prefix but keep the identifier the same.
1730 return NestedNameSpecifier::Create(*this,
1731 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1732 NNS->getAsIdentifier());
1733
1734 case NestedNameSpecifier::Namespace:
1735 // A namespace is canonical; build a nested-name-specifier with
1736 // this namespace and no prefix.
1737 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1738
1739 case NestedNameSpecifier::TypeSpec:
1740 case NestedNameSpecifier::TypeSpecWithTemplate: {
1741 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1742 NestedNameSpecifier *Prefix = 0;
1743
1744 // FIXME: This isn't the right check!
1745 if (T->isDependentType())
1746 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1747
1748 return NestedNameSpecifier::Create(*this, Prefix,
1749 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1750 T.getTypePtr());
1751 }
1752
1753 case NestedNameSpecifier::Global:
1754 // The global specifier is canonical and unique.
1755 return NNS;
1756 }
1757
1758 // Required to silence a GCC warning
1759 return 0;
1760}
1761
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001762
1763const ArrayType *ASTContext::getAsArrayType(QualType T) {
1764 // Handle the non-qualified case efficiently.
1765 if (T.getCVRQualifiers() == 0) {
1766 // Handle the common positive case fast.
1767 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1768 return AT;
1769 }
1770
1771 // Handle the common negative case fast, ignoring CVR qualifiers.
1772 QualType CType = T->getCanonicalTypeInternal();
1773
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001774 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001775 // test.
1776 if (!isa<ArrayType>(CType) &&
1777 !isa<ArrayType>(CType.getUnqualifiedType()))
1778 return 0;
1779
1780 // Apply any CVR qualifiers from the array type to the element type. This
1781 // implements C99 6.7.3p8: "If the specification of an array type includes
1782 // any type qualifiers, the element type is so qualified, not the array type."
1783
1784 // If we get here, we either have type qualifiers on the type, or we have
1785 // sugar such as a typedef in the way. If we have type qualifiers on the type
1786 // we must propagate them down into the elemeng type.
1787 unsigned CVRQuals = T.getCVRQualifiers();
1788 unsigned AddrSpace = 0;
1789 Type *Ty = T.getTypePtr();
1790
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001791 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001792 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001793 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1794 AddrSpace = EXTQT->getAddressSpace();
1795 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001796 } else {
1797 T = Ty->getDesugaredType();
1798 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1799 break;
1800 CVRQuals |= T.getCVRQualifiers();
1801 Ty = T.getTypePtr();
1802 }
1803 }
1804
1805 // If we have a simple case, just return now.
1806 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1807 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1808 return ATy;
1809
1810 // Otherwise, we have an array and we have qualifiers on it. Push the
1811 // qualifiers into the array element type and return a new array type.
1812 // Get the canonical version of the element with the extra qualifiers on it.
1813 // This can recursively sink qualifiers through multiple levels of arrays.
1814 QualType NewEltTy = ATy->getElementType();
1815 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001816 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001817 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1818
1819 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1820 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1821 CAT->getSizeModifier(),
1822 CAT->getIndexTypeQualifier()));
1823 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1824 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1825 IAT->getSizeModifier(),
1826 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001827
Douglas Gregor898574e2008-12-05 23:32:09 +00001828 if (const DependentSizedArrayType *DSAT
1829 = dyn_cast<DependentSizedArrayType>(ATy))
1830 return cast<ArrayType>(
1831 getDependentSizedArrayType(NewEltTy,
1832 DSAT->getSizeExpr(),
1833 DSAT->getSizeModifier(),
1834 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001835
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001836 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1837 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1838 VAT->getSizeModifier(),
1839 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001840}
1841
1842
Chris Lattnere6327742008-04-02 05:18:44 +00001843/// getArrayDecayedType - Return the properly qualified result of decaying the
1844/// specified array type to a pointer. This operation is non-trivial when
1845/// handling typedefs etc. The canonical type of "T" must be an array type,
1846/// this returns a pointer to a properly qualified element of the array.
1847///
1848/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1849QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001850 // Get the element type with 'getAsArrayType' so that we don't lose any
1851 // typedefs in the element type of the array. This also handles propagation
1852 // of type qualifiers from the array type into the element type if present
1853 // (C99 6.7.3p8).
1854 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1855 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001856
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001857 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001858
1859 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001860 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001861}
1862
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001863QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001864 QualType ElemTy = VAT->getElementType();
1865
1866 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1867 return getBaseElementType(VAT);
1868
1869 return ElemTy;
1870}
1871
Reid Spencer5f016e22007-07-11 17:01:13 +00001872/// getFloatingRank - Return a relative rank for floating point types.
1873/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001874static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001875 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001876 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001877
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001878 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001879 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001880 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001881 case BuiltinType::Float: return FloatRank;
1882 case BuiltinType::Double: return DoubleRank;
1883 case BuiltinType::LongDouble: return LongDoubleRank;
1884 }
1885}
1886
Steve Naroff716c7302007-08-27 01:41:48 +00001887/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1888/// point or a complex type (based on typeDomain/typeSize).
1889/// 'typeDomain' is a real floating point or complex type.
1890/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001891QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1892 QualType Domain) const {
1893 FloatingRank EltRank = getFloatingRank(Size);
1894 if (Domain->isComplexType()) {
1895 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001896 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001897 case FloatRank: return FloatComplexTy;
1898 case DoubleRank: return DoubleComplexTy;
1899 case LongDoubleRank: return LongDoubleComplexTy;
1900 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001901 }
Chris Lattner1361b112008-04-06 23:58:54 +00001902
1903 assert(Domain->isRealFloatingType() && "Unknown domain!");
1904 switch (EltRank) {
1905 default: assert(0 && "getFloatingRank(): illegal value for rank");
1906 case FloatRank: return FloatTy;
1907 case DoubleRank: return DoubleTy;
1908 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001909 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001910}
1911
Chris Lattner7cfeb082008-04-06 23:55:33 +00001912/// getFloatingTypeOrder - Compare the rank of the two specified floating
1913/// point types, ignoring the domain of the type (i.e. 'double' ==
1914/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1915/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001916int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1917 FloatingRank LHSR = getFloatingRank(LHS);
1918 FloatingRank RHSR = getFloatingRank(RHS);
1919
1920 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001921 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001922 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001923 return 1;
1924 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001925}
1926
Chris Lattnerf52ab252008-04-06 22:59:24 +00001927/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1928/// routine will assert if passed a built-in type that isn't an integer or enum,
1929/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001930unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001931 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001932 if (EnumType* ET = dyn_cast<EnumType>(T))
1933 T = ET->getDecl()->getIntegerType().getTypePtr();
1934
1935 // There are two things which impact the integer rank: the width, and
1936 // the ordering of builtins. The builtin ordering is encoded in the
1937 // bottom three bits; the width is encoded in the bits above that.
1938 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1939 return FWIT->getWidth() << 3;
1940 }
1941
Chris Lattnerf52ab252008-04-06 22:59:24 +00001942 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001943 default: assert(0 && "getIntegerRank(): not a built-in integer");
1944 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001945 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001946 case BuiltinType::Char_S:
1947 case BuiltinType::Char_U:
1948 case BuiltinType::SChar:
1949 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001950 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001951 case BuiltinType::Short:
1952 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001953 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001954 case BuiltinType::Int:
1955 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001956 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001957 case BuiltinType::Long:
1958 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001959 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001960 case BuiltinType::LongLong:
1961 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001962 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00001963 case BuiltinType::Int128:
1964 case BuiltinType::UInt128:
1965 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001966 }
1967}
1968
Chris Lattner7cfeb082008-04-06 23:55:33 +00001969/// getIntegerTypeOrder - Returns the highest ranked integer type:
1970/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1971/// LHS < RHS, return -1.
1972int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001973 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1974 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001975 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001976
Chris Lattnerf52ab252008-04-06 22:59:24 +00001977 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1978 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001979
Chris Lattner7cfeb082008-04-06 23:55:33 +00001980 unsigned LHSRank = getIntegerRank(LHSC);
1981 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001982
Chris Lattner7cfeb082008-04-06 23:55:33 +00001983 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1984 if (LHSRank == RHSRank) return 0;
1985 return LHSRank > RHSRank ? 1 : -1;
1986 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001987
Chris Lattner7cfeb082008-04-06 23:55:33 +00001988 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1989 if (LHSUnsigned) {
1990 // If the unsigned [LHS] type is larger, return it.
1991 if (LHSRank >= RHSRank)
1992 return 1;
1993
1994 // If the signed type can represent all values of the unsigned type, it
1995 // wins. Because we are dealing with 2's complement and types that are
1996 // powers of two larger than each other, this is always safe.
1997 return -1;
1998 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001999
Chris Lattner7cfeb082008-04-06 23:55:33 +00002000 // If the unsigned [RHS] type is larger, return it.
2001 if (RHSRank >= LHSRank)
2002 return -1;
2003
2004 // If the signed type can represent all values of the unsigned type, it
2005 // wins. Because we are dealing with 2's complement and types that are
2006 // powers of two larger than each other, this is always safe.
2007 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002008}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002009
2010// getCFConstantStringType - Return the type used for constant CFStrings.
2011QualType ASTContext::getCFConstantStringType() {
2012 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00002013 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002014 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002015 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002016 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00002017
2018 // const int *isa;
2019 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002020 // int flags;
2021 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002022 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002023 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00002024 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002025 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00002026
Anders Carlsson71993dd2007-08-17 05:31:46 +00002027 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002028 for (unsigned i = 0; i < 4; ++i) {
2029 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2030 SourceLocation(), 0,
2031 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002032 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002033 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002034 }
2035
2036 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002037 }
2038
2039 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002040}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002041
Douglas Gregor319ac892009-04-23 22:29:11 +00002042void ASTContext::setCFConstantStringType(QualType T) {
2043 const RecordType *Rec = T->getAsRecordType();
2044 assert(Rec && "Invalid CFConstantStringType");
2045 CFConstantStringTypeDecl = Rec->getDecl();
2046}
2047
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002048QualType ASTContext::getObjCFastEnumerationStateType()
2049{
2050 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002051 ObjCFastEnumerationStateTypeDecl =
2052 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2053 &Idents.get("__objcFastEnumerationState"));
2054
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002055 QualType FieldTypes[] = {
2056 UnsignedLongTy,
2057 getPointerType(ObjCIdType),
2058 getPointerType(UnsignedLongTy),
2059 getConstantArrayType(UnsignedLongTy,
2060 llvm::APInt(32, 5), ArrayType::Normal, 0)
2061 };
2062
Douglas Gregor44b43212008-12-11 16:49:14 +00002063 for (size_t i = 0; i < 4; ++i) {
2064 FieldDecl *Field = FieldDecl::Create(*this,
2065 ObjCFastEnumerationStateTypeDecl,
2066 SourceLocation(), 0,
2067 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002068 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002069 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002070 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002071
Douglas Gregor44b43212008-12-11 16:49:14 +00002072 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002073 }
2074
2075 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2076}
2077
Douglas Gregor319ac892009-04-23 22:29:11 +00002078void ASTContext::setObjCFastEnumerationStateType(QualType T) {
2079 const RecordType *Rec = T->getAsRecordType();
2080 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2081 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2082}
2083
Anders Carlssone8c49532007-10-29 06:33:42 +00002084// This returns true if a type has been typedefed to BOOL:
2085// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002086static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002087 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002088 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2089 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002090
2091 return false;
2092}
2093
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002094/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002095/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002096int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002097 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002098
2099 // Make all integer and enum types at least as large as an int
2100 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002101 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002102 // Treat arrays as pointers, since that's how they're passed in.
2103 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002104 sz = getTypeSize(VoidPtrTy);
2105 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002106}
2107
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002108/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002109/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002110void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002111 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002112 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002113 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002114 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002115 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002116 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002117 // Compute size of all parameters.
2118 // Start with computing size of a pointer in number of bytes.
2119 // FIXME: There might(should) be a better way of doing this computation!
2120 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002121 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002122 // The first two arguments (self and _cmd) are pointers; account for
2123 // their size.
2124 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002125 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2126 E = Decl->param_end(); PI != E; ++PI) {
2127 QualType PType = (*PI)->getType();
2128 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002129 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002130 ParmOffset += sz;
2131 }
2132 S += llvm::utostr(ParmOffset);
2133 S += "@0:";
2134 S += llvm::utostr(PtrSize);
2135
2136 // Argument types.
2137 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002138 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2139 E = Decl->param_end(); PI != E; ++PI) {
2140 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002141 QualType PType = PVDecl->getOriginalType();
2142 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002143 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2144 // Use array's original type only if it has known number of
2145 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002146 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002147 PType = PVDecl->getType();
2148 } else if (PType->isFunctionType())
2149 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002150 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002151 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002152 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002153 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002154 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002155 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002156 }
2157}
2158
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002159/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002160/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002161/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2162/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002163/// Property attributes are stored as a comma-delimited C string. The simple
2164/// attributes readonly and bycopy are encoded as single characters. The
2165/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2166/// encoded as single characters, followed by an identifier. Property types
2167/// are also encoded as a parametrized attribute. The characters used to encode
2168/// these attributes are defined by the following enumeration:
2169/// @code
2170/// enum PropertyAttributes {
2171/// kPropertyReadOnly = 'R', // property is read-only.
2172/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2173/// kPropertyByref = '&', // property is a reference to the value last assigned
2174/// kPropertyDynamic = 'D', // property is dynamic
2175/// kPropertyGetter = 'G', // followed by getter selector name
2176/// kPropertySetter = 'S', // followed by setter selector name
2177/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2178/// kPropertyType = 't' // followed by old-style type encoding.
2179/// kPropertyWeak = 'W' // 'weak' property
2180/// kPropertyStrong = 'P' // property GC'able
2181/// kPropertyNonAtomic = 'N' // property non-atomic
2182/// };
2183/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002184void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2185 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002186 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002187 // Collect information from the property implementation decl(s).
2188 bool Dynamic = false;
2189 ObjCPropertyImplDecl *SynthesizePID = 0;
2190
2191 // FIXME: Duplicated code due to poor abstraction.
2192 if (Container) {
2193 if (const ObjCCategoryImplDecl *CID =
2194 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2195 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002196 i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2197 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002198 ObjCPropertyImplDecl *PID = *i;
2199 if (PID->getPropertyDecl() == PD) {
2200 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2201 Dynamic = true;
2202 } else {
2203 SynthesizePID = PID;
2204 }
2205 }
2206 }
2207 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002208 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002209 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002210 i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2211 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002212 ObjCPropertyImplDecl *PID = *i;
2213 if (PID->getPropertyDecl() == PD) {
2214 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2215 Dynamic = true;
2216 } else {
2217 SynthesizePID = PID;
2218 }
2219 }
2220 }
2221 }
2222 }
2223
2224 // FIXME: This is not very efficient.
2225 S = "T";
2226
2227 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002228 // GCC has some special rules regarding encoding of properties which
2229 // closely resembles encoding of ivars.
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002230 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002231 true /* outermost type */,
2232 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002233
2234 if (PD->isReadOnly()) {
2235 S += ",R";
2236 } else {
2237 switch (PD->getSetterKind()) {
2238 case ObjCPropertyDecl::Assign: break;
2239 case ObjCPropertyDecl::Copy: S += ",C"; break;
2240 case ObjCPropertyDecl::Retain: S += ",&"; break;
2241 }
2242 }
2243
2244 // It really isn't clear at all what this means, since properties
2245 // are "dynamic by default".
2246 if (Dynamic)
2247 S += ",D";
2248
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002249 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2250 S += ",N";
2251
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002252 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2253 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002254 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002255 }
2256
2257 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2258 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002259 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002260 }
2261
2262 if (SynthesizePID) {
2263 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2264 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002265 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002266 }
2267
2268 // FIXME: OBJCGC: weak & strong
2269}
2270
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002271/// getLegacyIntegralTypeEncoding -
2272/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002273/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002274/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2275///
2276void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2277 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2278 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002279 if (BT->getKind() == BuiltinType::ULong &&
2280 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002281 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002282 else
2283 if (BT->getKind() == BuiltinType::Long &&
2284 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002285 PointeeTy = IntTy;
2286 }
2287 }
2288}
2289
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002290void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002291 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002292 // We follow the behavior of gcc, expanding structures which are
2293 // directly pointed to, and expanding embedded structures. Note that
2294 // these rules are sufficient to prevent recursive encoding of the
2295 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002296 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2297 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002298}
2299
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002300static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002301 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002302 const Expr *E = FD->getBitWidth();
2303 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2304 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002305 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002306 S += 'b';
2307 S += llvm::utostr(N);
2308}
2309
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002310void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2311 bool ExpandPointedToStructures,
2312 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002313 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002314 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002315 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002316 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002317 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002318 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002319 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002320 else {
2321 char encoding;
2322 switch (BT->getKind()) {
2323 default: assert(0 && "Unhandled builtin type kind");
2324 case BuiltinType::Void: encoding = 'v'; break;
2325 case BuiltinType::Bool: encoding = 'B'; break;
2326 case BuiltinType::Char_U:
2327 case BuiltinType::UChar: encoding = 'C'; break;
2328 case BuiltinType::UShort: encoding = 'S'; break;
2329 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002330 case BuiltinType::ULong:
2331 encoding =
2332 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2333 break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002334 case BuiltinType::UInt128: encoding = 'T'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002335 case BuiltinType::ULongLong: encoding = 'Q'; break;
2336 case BuiltinType::Char_S:
2337 case BuiltinType::SChar: encoding = 'c'; break;
2338 case BuiltinType::Short: encoding = 's'; break;
2339 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002340 case BuiltinType::Long:
2341 encoding =
2342 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2343 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002344 case BuiltinType::LongLong: encoding = 'q'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002345 case BuiltinType::Int128: encoding = 't'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002346 case BuiltinType::Float: encoding = 'f'; break;
2347 case BuiltinType::Double: encoding = 'd'; break;
2348 case BuiltinType::LongDouble: encoding = 'd'; break;
2349 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002350
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002351 S += encoding;
2352 }
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002353 } else if (const ComplexType *CT = T->getAsComplexType()) {
2354 S += 'j';
2355 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2356 false);
2357 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002358 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2359 ExpandPointedToStructures,
2360 ExpandStructures, FD);
2361 if (FD || EncodingProperty) {
2362 // Note that we do extended encoding of protocol qualifer list
2363 // Only when doing ivar or property encoding.
2364 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2365 S += '"';
Steve Naroff446ee4e2009-05-27 16:21:00 +00002366 for (ObjCQualifiedIdType::qual_iterator I = QIDT->qual_begin(),
2367 E = QIDT->qual_end(); I != E; ++I) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002368 S += '<';
Steve Naroff446ee4e2009-05-27 16:21:00 +00002369 S += (*I)->getNameAsString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002370 S += '>';
2371 }
2372 S += '"';
2373 }
2374 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002375 }
2376 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002377 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002378 bool isReadOnly = false;
2379 // For historical/compatibility reasons, the read-only qualifier of the
2380 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2381 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2382 // Also, do not emit the 'r' for anything but the outermost type!
2383 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2384 if (OutermostType && T.isConstQualified()) {
2385 isReadOnly = true;
2386 S += 'r';
2387 }
2388 }
2389 else if (OutermostType) {
2390 QualType P = PointeeTy;
2391 while (P->getAsPointerType())
2392 P = P->getAsPointerType()->getPointeeType();
2393 if (P.isConstQualified()) {
2394 isReadOnly = true;
2395 S += 'r';
2396 }
2397 }
2398 if (isReadOnly) {
2399 // Another legacy compatibility encoding. Some ObjC qualifier and type
2400 // combinations need to be rearranged.
2401 // Rewrite "in const" from "nr" to "rn"
2402 const char * s = S.c_str();
2403 int len = S.length();
2404 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2405 std::string replace = "rn";
2406 S.replace(S.end()-2, S.end(), replace);
2407 }
2408 }
Steve Naroff389bf462009-02-12 17:52:19 +00002409 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002410 S += '@';
2411 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002412 }
2413 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002414 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002415 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002416 // Another historical/compatibility reason.
2417 // We encode the underlying type which comes out as
2418 // {...};
2419 S += '^';
2420 getObjCEncodingForTypeImpl(PointeeTy, S,
2421 false, ExpandPointedToStructures,
2422 NULL);
2423 return;
2424 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002425 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002426 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002427 const ObjCInterfaceType *OIT =
2428 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002429 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002430 S += '"';
2431 S += OI->getNameAsCString();
Steve Naroff446ee4e2009-05-27 16:21:00 +00002432 for (ObjCInterfaceType::qual_iterator I = OIT->qual_begin(),
2433 E = OIT->qual_end(); I != E; ++I) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002434 S += '<';
Steve Naroff446ee4e2009-05-27 16:21:00 +00002435 S += (*I)->getNameAsString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002436 S += '>';
2437 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002438 S += '"';
2439 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002440 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002441 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002442 S += '#';
2443 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002444 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002445 S += ':';
2446 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002447 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002448
2449 if (PointeeTy->isCharType()) {
2450 // char pointer types should be encoded as '*' unless it is a
2451 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002452 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002453 S += '*';
2454 return;
2455 }
2456 }
2457
2458 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002459 getLegacyIntegralTypeEncoding(PointeeTy);
2460
2461 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002462 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002463 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002464 } else if (const ArrayType *AT =
2465 // Ignore type qualifiers etc.
2466 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002467 if (isa<IncompleteArrayType>(AT)) {
2468 // Incomplete arrays are encoded as a pointer to the array element.
2469 S += '^';
2470
2471 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2472 false, ExpandStructures, FD);
2473 } else {
2474 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002475
Anders Carlsson559a8332009-02-22 01:38:57 +00002476 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2477 S += llvm::utostr(CAT->getSize().getZExtValue());
2478 else {
2479 //Variable length arrays are encoded as a regular array with 0 elements.
2480 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2481 S += '0';
2482 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002483
Anders Carlsson559a8332009-02-22 01:38:57 +00002484 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2485 false, ExpandStructures, FD);
2486 S += ']';
2487 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002488 } else if (T->getAsFunctionType()) {
2489 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002490 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002491 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002492 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002493 // Anonymous structures print as '?'
2494 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2495 S += II->getName();
2496 } else {
2497 S += '?';
2498 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002499 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002500 S += '=';
Douglas Gregor6ab35242009-04-09 21:40:53 +00002501 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2502 FieldEnd = RDecl->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +00002503 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002504 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002505 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002506 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002507 S += '"';
2508 }
2509
2510 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002511 if (Field->isBitField()) {
2512 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2513 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002514 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002515 QualType qt = Field->getType();
2516 getLegacyIntegralTypeEncoding(qt);
2517 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002518 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002519 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002520 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002521 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002522 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002523 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002524 if (FD && FD->isBitField())
2525 EncodeBitField(this, S, FD);
2526 else
2527 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002528 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002529 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002530 } else if (T->isObjCInterfaceType()) {
2531 // @encode(class_name)
2532 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2533 S += '{';
2534 const IdentifierInfo *II = OI->getIdentifier();
2535 S += II->getName();
2536 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002537 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002538 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002539 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002540 if (RecFields[i]->isBitField())
2541 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2542 RecFields[i]);
2543 else
2544 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2545 FD);
2546 }
2547 S += '}';
2548 }
2549 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002550 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002551}
2552
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002553void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002554 std::string& S) const {
2555 if (QT & Decl::OBJC_TQ_In)
2556 S += 'n';
2557 if (QT & Decl::OBJC_TQ_Inout)
2558 S += 'N';
2559 if (QT & Decl::OBJC_TQ_Out)
2560 S += 'o';
2561 if (QT & Decl::OBJC_TQ_Bycopy)
2562 S += 'O';
2563 if (QT & Decl::OBJC_TQ_Byref)
2564 S += 'R';
2565 if (QT & Decl::OBJC_TQ_Oneway)
2566 S += 'V';
2567}
2568
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002569void ASTContext::setBuiltinVaListType(QualType T)
2570{
2571 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2572
2573 BuiltinVaListType = T;
2574}
2575
Douglas Gregor319ac892009-04-23 22:29:11 +00002576void ASTContext::setObjCIdType(QualType T)
Steve Naroff7e219e42007-10-15 14:41:52 +00002577{
Douglas Gregor319ac892009-04-23 22:29:11 +00002578 ObjCIdType = T;
2579
2580 const TypedefType *TT = T->getAsTypedefType();
2581 if (!TT)
2582 return;
2583
2584 TypedefDecl *TD = TT->getDecl();
Steve Naroff7e219e42007-10-15 14:41:52 +00002585
2586 // typedef struct objc_object *id;
2587 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002588 // User error - caller will issue diagnostics.
2589 if (!ptr)
2590 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002591 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002592 // User error - caller will issue diagnostics.
2593 if (!rec)
2594 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002595 IdStructType = rec;
2596}
2597
Douglas Gregor319ac892009-04-23 22:29:11 +00002598void ASTContext::setObjCSelType(QualType T)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002599{
Douglas Gregor319ac892009-04-23 22:29:11 +00002600 ObjCSelType = T;
2601
2602 const TypedefType *TT = T->getAsTypedefType();
2603 if (!TT)
2604 return;
2605 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002606
2607 // typedef struct objc_selector *SEL;
2608 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002609 if (!ptr)
2610 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002611 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002612 if (!rec)
2613 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002614 SelStructType = rec;
2615}
2616
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002617void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002618{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002619 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002620}
2621
Douglas Gregor319ac892009-04-23 22:29:11 +00002622void ASTContext::setObjCClassType(QualType T)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002623{
Douglas Gregor319ac892009-04-23 22:29:11 +00002624 ObjCClassType = T;
2625
2626 const TypedefType *TT = T->getAsTypedefType();
2627 if (!TT)
2628 return;
2629 TypedefDecl *TD = TT->getDecl();
Anders Carlsson8baaca52007-10-31 02:53:19 +00002630
2631 // typedef struct objc_class *Class;
2632 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2633 assert(ptr && "'Class' incorrectly typed");
2634 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2635 assert(rec && "'Class' incorrectly typed");
2636 ClassStructType = rec;
2637}
2638
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002639void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2640 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002641 "'NSConstantString' type already set!");
2642
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002643 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002644}
2645
Douglas Gregor7532dc62009-03-30 22:58:21 +00002646/// \brief Retrieve the template name that represents a qualified
2647/// template name such as \c std::vector.
2648TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2649 bool TemplateKeyword,
2650 TemplateDecl *Template) {
2651 llvm::FoldingSetNodeID ID;
2652 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2653
2654 void *InsertPos = 0;
2655 QualifiedTemplateName *QTN =
2656 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2657 if (!QTN) {
2658 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2659 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2660 }
2661
2662 return TemplateName(QTN);
2663}
2664
2665/// \brief Retrieve the template name that represents a dependent
2666/// template name such as \c MetaFun::template apply.
2667TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2668 const IdentifierInfo *Name) {
2669 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2670
2671 llvm::FoldingSetNodeID ID;
2672 DependentTemplateName::Profile(ID, NNS, Name);
2673
2674 void *InsertPos = 0;
2675 DependentTemplateName *QTN =
2676 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2677
2678 if (QTN)
2679 return TemplateName(QTN);
2680
2681 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2682 if (CanonNNS == NNS) {
2683 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2684 } else {
2685 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2686 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2687 }
2688
2689 DependentTemplateNames.InsertNode(QTN, InsertPos);
2690 return TemplateName(QTN);
2691}
2692
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002693/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002694/// TargetInfo, produce the corresponding type. The unsigned @p Type
2695/// is actually a value of type @c TargetInfo::IntType.
2696QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002697 switch (Type) {
2698 case TargetInfo::NoInt: return QualType();
2699 case TargetInfo::SignedShort: return ShortTy;
2700 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2701 case TargetInfo::SignedInt: return IntTy;
2702 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2703 case TargetInfo::SignedLong: return LongTy;
2704 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2705 case TargetInfo::SignedLongLong: return LongLongTy;
2706 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2707 }
2708
2709 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002710 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002711}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002712
2713//===----------------------------------------------------------------------===//
2714// Type Predicates.
2715//===----------------------------------------------------------------------===//
2716
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002717/// isObjCNSObjectType - Return true if this is an NSObject object using
2718/// NSObject attribute on a c-style pointer type.
2719/// FIXME - Make it work directly on types.
2720///
2721bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2722 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2723 if (TypedefDecl *TD = TDT->getDecl())
2724 if (TD->getAttr<ObjCNSObjectAttr>())
2725 return true;
2726 }
2727 return false;
2728}
2729
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002730/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2731/// to an object type. This includes "id" and "Class" (two 'special' pointers
2732/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2733/// ID type).
2734bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002735 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002736 return true;
2737
Steve Naroff6ae98502008-10-21 18:24:04 +00002738 // Blocks are objects.
2739 if (Ty->isBlockPointerType())
2740 return true;
2741
2742 // All other object types are pointers.
Chris Lattner16ede0e2009-04-12 23:51:02 +00002743 const PointerType *PT = Ty->getAsPointerType();
2744 if (PT == 0)
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002745 return false;
2746
Chris Lattner16ede0e2009-04-12 23:51:02 +00002747 // If this a pointer to an interface (e.g. NSString*), it is ok.
2748 if (PT->getPointeeType()->isObjCInterfaceType() ||
2749 // If is has NSObject attribute, OK as well.
2750 isObjCNSObjectType(Ty))
2751 return true;
2752
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002753 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2754 // pointer types. This looks for the typedef specifically, not for the
Chris Lattner16ede0e2009-04-12 23:51:02 +00002755 // underlying type. Iteratively strip off typedefs so that we can handle
2756 // typedefs of typedefs.
2757 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2758 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2759 Ty.getUnqualifiedType() == getObjCClassType())
2760 return true;
2761
2762 Ty = TDT->getDecl()->getUnderlyingType();
2763 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002764
Chris Lattner16ede0e2009-04-12 23:51:02 +00002765 return false;
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002766}
2767
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002768/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2769/// garbage collection attribute.
2770///
2771QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002772 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002773 if (getLangOptions().ObjC1 &&
2774 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002775 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002776 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002777 // (or pointers to them) be treated as though they were declared
2778 // as __strong.
2779 if (GCAttrs == QualType::GCNone) {
2780 if (isObjCObjectPointerType(Ty))
2781 GCAttrs = QualType::Strong;
2782 else if (Ty->isPointerType())
2783 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2784 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00002785 // Non-pointers have none gc'able attribute regardless of the attribute
2786 // set on them.
2787 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2788 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002789 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002790 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002791}
2792
Chris Lattner6ac46a42008-04-07 06:51:04 +00002793//===----------------------------------------------------------------------===//
2794// Type Compatibility Testing
2795//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002796
Steve Naroff1c7d0672008-09-04 15:10:53 +00002797/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002798/// block types. Types must be strictly compatible here. For example,
2799/// C unfortunately doesn't produce an error for the following:
2800///
2801/// int (*emptyArgFunc)();
2802/// int (*intArgList)(int) = emptyArgFunc;
2803///
2804/// For blocks, we will produce an error for the following (similar to C++):
2805///
2806/// int (^emptyArgBlock)();
2807/// int (^intArgBlock)(int) = emptyArgBlock;
2808///
2809/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2810///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002811bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002812 const FunctionType *lbase = lhs->getAsFunctionType();
2813 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002814 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2815 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stumpaab0f7a2009-04-01 01:17:39 +00002816 if (lproto && rproto == 0)
2817 return false;
2818 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff1c7d0672008-09-04 15:10:53 +00002819}
2820
Chris Lattner6ac46a42008-04-07 06:51:04 +00002821/// areCompatVectorTypes - Return true if the two specified vector types are
2822/// compatible.
2823static bool areCompatVectorTypes(const VectorType *LHS,
2824 const VectorType *RHS) {
2825 assert(LHS->isCanonical() && RHS->isCanonical());
2826 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002827 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002828}
2829
Eli Friedman3d815e72008-08-22 00:56:42 +00002830/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002831/// compatible for assignment from RHS to LHS. This handles validation of any
2832/// protocol qualifiers on the LHS or RHS.
2833///
Eli Friedman3d815e72008-08-22 00:56:42 +00002834bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2835 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002836 // Verify that the base decls are compatible: the RHS must be a subclass of
2837 // the LHS.
2838 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2839 return false;
2840
2841 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2842 // protocol qualified at all, then we are good.
2843 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2844 return true;
2845
2846 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2847 // isn't a superset.
2848 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2849 return true; // FIXME: should return false!
2850
2851 // Finally, we must have two protocol-qualified interfaces.
2852 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2853 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002854
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002855 // All LHS protocols must have a presence on the RHS.
2856 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002857
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002858 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2859 LHSPE = LHSP->qual_end();
2860 LHSPI != LHSPE; LHSPI++) {
2861 bool RHSImplementsProtocol = false;
2862
2863 // If the RHS doesn't implement the protocol on the left, the types
2864 // are incompatible.
2865 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2866 RHSPE = RHSP->qual_end();
2867 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2868 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2869 RHSImplementsProtocol = true;
2870 }
2871 // FIXME: For better diagnostics, consider passing back the protocol name.
2872 if (!RHSImplementsProtocol)
2873 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002874 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002875 // The RHS implements all protocols listed on the LHS.
2876 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002877}
2878
Steve Naroff389bf462009-02-12 17:52:19 +00002879bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2880 // get the "pointed to" types
2881 const PointerType *LHSPT = LHS->getAsPointerType();
2882 const PointerType *RHSPT = RHS->getAsPointerType();
2883
2884 if (!LHSPT || !RHSPT)
2885 return false;
2886
2887 QualType lhptee = LHSPT->getPointeeType();
2888 QualType rhptee = RHSPT->getPointeeType();
2889 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2890 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2891 // ID acts sort of like void* for ObjC interfaces
2892 if (LHSIface && isObjCIdStructType(rhptee))
2893 return true;
2894 if (RHSIface && isObjCIdStructType(lhptee))
2895 return true;
2896 if (!LHSIface || !RHSIface)
2897 return false;
2898 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2899 canAssignObjCInterfaces(RHSIface, LHSIface);
2900}
2901
Steve Naroffec0550f2007-10-15 20:41:53 +00002902/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2903/// both shall have the identically qualified version of a compatible type.
2904/// C99 6.2.7p1: Two types have compatible types if their types are the
2905/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002906bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2907 return !mergeTypes(LHS, RHS).isNull();
2908}
2909
2910QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2911 const FunctionType *lbase = lhs->getAsFunctionType();
2912 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002913 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2914 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002915 bool allLTypes = true;
2916 bool allRTypes = true;
2917
2918 // Check return type
2919 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2920 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002921 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2922 allLTypes = false;
2923 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2924 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002925
2926 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00002927 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
2928 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00002929 unsigned lproto_nargs = lproto->getNumArgs();
2930 unsigned rproto_nargs = rproto->getNumArgs();
2931
2932 // Compatible functions must have the same number of arguments
2933 if (lproto_nargs != rproto_nargs)
2934 return QualType();
2935
2936 // Variadic and non-variadic functions aren't compatible
2937 if (lproto->isVariadic() != rproto->isVariadic())
2938 return QualType();
2939
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002940 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2941 return QualType();
2942
Eli Friedman3d815e72008-08-22 00:56:42 +00002943 // Check argument compatibility
2944 llvm::SmallVector<QualType, 10> types;
2945 for (unsigned i = 0; i < lproto_nargs; i++) {
2946 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2947 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2948 QualType argtype = mergeTypes(largtype, rargtype);
2949 if (argtype.isNull()) return QualType();
2950 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002951 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2952 allLTypes = false;
2953 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2954 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002955 }
2956 if (allLTypes) return lhs;
2957 if (allRTypes) return rhs;
2958 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002959 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002960 }
2961
2962 if (lproto) allRTypes = false;
2963 if (rproto) allLTypes = false;
2964
Douglas Gregor72564e72009-02-26 23:50:07 +00002965 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002966 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00002967 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00002968 if (proto->isVariadic()) return QualType();
2969 // Check that the types are compatible with the types that
2970 // would result from default argument promotions (C99 6.7.5.3p15).
2971 // The only types actually affected are promotable integer
2972 // types and floats, which would be passed as a different
2973 // type depending on whether the prototype is visible.
2974 unsigned proto_nargs = proto->getNumArgs();
2975 for (unsigned i = 0; i < proto_nargs; ++i) {
2976 QualType argTy = proto->getArgType(i);
2977 if (argTy->isPromotableIntegerType() ||
2978 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2979 return QualType();
2980 }
2981
2982 if (allLTypes) return lhs;
2983 if (allRTypes) return rhs;
2984 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002985 proto->getNumArgs(), lproto->isVariadic(),
2986 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002987 }
2988
2989 if (allLTypes) return lhs;
2990 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002991 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002992}
2993
2994QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002995 // C++ [expr]: If an expression initially has the type "reference to T", the
2996 // type is adjusted to "T" prior to any further analysis, the expression
2997 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002998 // expression is an lvalue unless the reference is an rvalue reference and
2999 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00003000 // FIXME: C++ shouldn't be going through here! The rules are different
3001 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003002 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3003 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00003004 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003005 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00003006 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003007 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00003008
Eli Friedman3d815e72008-08-22 00:56:42 +00003009 QualType LHSCan = getCanonicalType(LHS),
3010 RHSCan = getCanonicalType(RHS);
3011
3012 // If two types are identical, they are compatible.
3013 if (LHSCan == RHSCan)
3014 return LHS;
3015
3016 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003017 // Note that we handle extended qualifiers later, in the
3018 // case for ExtQualType.
3019 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00003020 return QualType();
3021
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003022 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
3023 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003024
Chris Lattner1adb8832008-01-14 05:45:46 +00003025 // We want to consider the two function types to be the same for these
3026 // comparisons, just force one to the other.
3027 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3028 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003029
3030 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003031 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3032 LHSClass = Type::ConstantArray;
3033 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3034 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00003035
Nate Begeman213541a2008-04-18 23:10:10 +00003036 // Canonicalize ExtVector -> Vector.
3037 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3038 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00003039
Chris Lattnerb0489812008-04-07 06:38:24 +00003040 // Consider qualified interfaces and interfaces the same.
3041 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
3042 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00003043
Chris Lattnera36a61f2008-04-07 05:43:21 +00003044 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003045 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003046 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3047 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003048
Steve Naroffd824c9c2009-04-14 15:11:46 +00003049 // 'id' and 'Class' act sort of like void* for ObjC interfaces
3050 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003051 return LHS;
Steve Naroffd824c9c2009-04-14 15:11:46 +00003052 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003053 return RHS;
3054
Steve Naroffbc76dd02008-12-10 22:14:21 +00003055 // ID is compatible with all qualified id types.
3056 if (LHS->isObjCQualifiedIdType()) {
3057 if (const PointerType *PT = RHS->getAsPointerType()) {
3058 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003059 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003060 return LHS;
3061 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3062 // Unfortunately, this API is part of Sema (which we don't have access
3063 // to. Need to refactor. The following check is insufficient, since we
3064 // need to make sure the class implements the protocol.
3065 if (pType->isObjCInterfaceType())
3066 return LHS;
3067 }
3068 }
3069 if (RHS->isObjCQualifiedIdType()) {
3070 if (const PointerType *PT = LHS->getAsPointerType()) {
3071 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003072 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003073 return RHS;
3074 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3075 // Unfortunately, this API is part of Sema (which we don't have access
3076 // to. Need to refactor. The following check is insufficient, since we
3077 // need to make sure the class implements the protocol.
3078 if (pType->isObjCInterfaceType())
3079 return RHS;
3080 }
3081 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003082 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3083 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00003084 if (const EnumType* ETy = LHS->getAsEnumType()) {
3085 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3086 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003087 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003088 if (const EnumType* ETy = RHS->getAsEnumType()) {
3089 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3090 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003091 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003092
Eli Friedman3d815e72008-08-22 00:56:42 +00003093 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003094 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003095
Steve Naroff4a746782008-01-09 22:43:08 +00003096 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003097 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003098#define TYPE(Class, Base)
3099#define ABSTRACT_TYPE(Class, Base)
3100#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3101#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3102#include "clang/AST/TypeNodes.def"
3103 assert(false && "Non-canonical and dependent types shouldn't get here");
3104 return QualType();
3105
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003106 case Type::LValueReference:
3107 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003108 case Type::MemberPointer:
3109 assert(false && "C++ should never be in mergeTypes");
3110 return QualType();
3111
3112 case Type::IncompleteArray:
3113 case Type::VariableArray:
3114 case Type::FunctionProto:
3115 case Type::ExtVector:
3116 case Type::ObjCQualifiedInterface:
3117 assert(false && "Types are eliminated above");
3118 return QualType();
3119
Chris Lattner1adb8832008-01-14 05:45:46 +00003120 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003121 {
3122 // Merge two pointer types, while trying to preserve typedef info
3123 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3124 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3125 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3126 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003127 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3128 return LHS;
3129 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3130 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003131 return getPointerType(ResultType);
3132 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003133 case Type::BlockPointer:
3134 {
3135 // Merge two block pointer types, while trying to preserve typedef info
3136 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3137 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3138 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3139 if (ResultType.isNull()) return QualType();
3140 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3141 return LHS;
3142 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3143 return RHS;
3144 return getBlockPointerType(ResultType);
3145 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003146 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003147 {
3148 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3149 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3150 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3151 return QualType();
3152
3153 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3154 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3155 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3156 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003157 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3158 return LHS;
3159 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3160 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003161 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3162 ArrayType::ArraySizeModifier(), 0);
3163 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3164 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003165 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3166 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003167 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3168 return LHS;
3169 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3170 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003171 if (LVAT) {
3172 // FIXME: This isn't correct! But tricky to implement because
3173 // the array's size has to be the size of LHS, but the type
3174 // has to be different.
3175 return LHS;
3176 }
3177 if (RVAT) {
3178 // FIXME: This isn't correct! But tricky to implement because
3179 // the array's size has to be the size of RHS, but the type
3180 // has to be different.
3181 return RHS;
3182 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003183 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3184 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003185 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003186 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003187 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003188 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003189 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003190 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003191 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003192 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3193 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003194 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003195 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003196 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003197 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003198 case Type::Complex:
3199 // Distinct complex types are incompatible.
3200 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003201 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003202 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003203 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3204 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003205 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003206 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003207 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003208 // FIXME: This should be type compatibility, e.g. whether
3209 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003210 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3211 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3212 if (LHSIface && RHSIface &&
3213 canAssignObjCInterfaces(LHSIface, RHSIface))
3214 return LHS;
3215
Eli Friedman3d815e72008-08-22 00:56:42 +00003216 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003217 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003218 case Type::ObjCQualifiedId:
3219 // Distinct qualified id's are not compatible.
3220 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003221 case Type::FixedWidthInt:
3222 // Distinct fixed-width integers are not compatible.
3223 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003224 case Type::ExtQual:
3225 // FIXME: ExtQual types can be compatible even if they're not
3226 // identical!
3227 return QualType();
3228 // First attempt at an implementation, but I'm not really sure it's
3229 // right...
3230#if 0
3231 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3232 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3233 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3234 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3235 return QualType();
3236 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3237 LHSBase = QualType(LQual->getBaseType(), 0);
3238 RHSBase = QualType(RQual->getBaseType(), 0);
3239 ResultType = mergeTypes(LHSBase, RHSBase);
3240 if (ResultType.isNull()) return QualType();
3241 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3242 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3243 return LHS;
3244 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3245 return RHS;
3246 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3247 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3248 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3249 return ResultType;
3250#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003251
3252 case Type::TemplateSpecialization:
3253 assert(false && "Dependent types have no size");
3254 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003255 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003256
3257 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003258}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003259
Chris Lattner5426bf62008-04-07 07:01:58 +00003260//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003261// Integer Predicates
3262//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003263
Eli Friedmanad74a752008-06-28 06:23:08 +00003264unsigned ASTContext::getIntWidth(QualType T) {
3265 if (T == BoolTy)
3266 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003267 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3268 return FWIT->getWidth();
3269 }
3270 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003271 return (unsigned)getTypeSize(T);
3272}
3273
3274QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3275 assert(T->isSignedIntegerType() && "Unexpected type");
3276 if (const EnumType* ETy = T->getAsEnumType())
3277 T = ETy->getDecl()->getIntegerType();
3278 const BuiltinType* BTy = T->getAsBuiltinType();
3279 assert (BTy && "Unexpected signed integer type");
3280 switch (BTy->getKind()) {
3281 case BuiltinType::Char_S:
3282 case BuiltinType::SChar:
3283 return UnsignedCharTy;
3284 case BuiltinType::Short:
3285 return UnsignedShortTy;
3286 case BuiltinType::Int:
3287 return UnsignedIntTy;
3288 case BuiltinType::Long:
3289 return UnsignedLongTy;
3290 case BuiltinType::LongLong:
3291 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00003292 case BuiltinType::Int128:
3293 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00003294 default:
3295 assert(0 && "Unexpected signed integer type");
3296 return QualType();
3297 }
3298}
3299
Douglas Gregor2cf26342009-04-09 22:27:44 +00003300ExternalASTSource::~ExternalASTSource() { }
3301
3302void ExternalASTSource::PrintStats() { }