blob: b230edb93d3c8eb417529d37442d34ffd9bf48c5 [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) {
Chris Lattner38aeec72009-05-13 04:12:56 +00001066 // Convert the array size into a canonical width matching the pointer size for
1067 // the target.
1068 llvm::APInt ArySize(ArySizeIn);
1069 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1070
Reid Spencer5f016e22007-07-11 17:01:13 +00001071 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001072 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001073
1074 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001075 if (ConstantArrayType *ATP =
1076 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001077 return QualType(ATP, 0);
1078
1079 // If the element type isn't canonical, this won't be a canonical type either,
1080 // so fill in the canonical type field.
1081 QualType Canonical;
1082 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001083 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001084 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001085 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001086 ConstantArrayType *NewIP =
1087 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001088 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001089 }
1090
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001091 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001092 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001093 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001094 Types.push_back(New);
1095 return QualType(New, 0);
1096}
1097
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001098/// getVariableArrayType - Returns a non-unique reference to the type for a
1099/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001100QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1101 ArrayType::ArraySizeModifier ASM,
1102 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001103 // Since we don't unique expressions, it isn't possible to unique VLA's
1104 // that have an expression provided for their size.
1105
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001106 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001107 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001108
1109 VariableArrayTypes.push_back(New);
1110 Types.push_back(New);
1111 return QualType(New, 0);
1112}
1113
Douglas Gregor898574e2008-12-05 23:32:09 +00001114/// getDependentSizedArrayType - Returns a non-unique reference to
1115/// the type for a dependently-sized array of the specified element
1116/// type. FIXME: We will need these to be uniqued, or at least
1117/// comparable, at some point.
1118QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1119 ArrayType::ArraySizeModifier ASM,
1120 unsigned EltTypeQuals) {
1121 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1122 "Size must be type- or value-dependent!");
1123
1124 // Since we don't unique expressions, it isn't possible to unique
1125 // dependently-sized array types.
1126
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001127 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001128 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1129 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001130
1131 DependentSizedArrayTypes.push_back(New);
1132 Types.push_back(New);
1133 return QualType(New, 0);
1134}
1135
Eli Friedmanc5773c42008-02-15 18:16:39 +00001136QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1137 ArrayType::ArraySizeModifier ASM,
1138 unsigned EltTypeQuals) {
1139 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001140 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001141
1142 void *InsertPos = 0;
1143 if (IncompleteArrayType *ATP =
1144 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1145 return QualType(ATP, 0);
1146
1147 // If the element type isn't canonical, this won't be a canonical type
1148 // either, so fill in the canonical type field.
1149 QualType Canonical;
1150
1151 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001152 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001153 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001154
1155 // Get the new insert position for the node we care about.
1156 IncompleteArrayType *NewIP =
1157 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001158 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001159 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001160
Steve Narofff83820b2009-01-27 22:08:43 +00001161 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001162 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001163
1164 IncompleteArrayTypes.InsertNode(New, InsertPos);
1165 Types.push_back(New);
1166 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001167}
1168
Steve Naroff73322922007-07-18 18:00:27 +00001169/// getVectorType - Return the unique reference to a vector type of
1170/// the specified element type and size. VectorType must be a built-in type.
1171QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001172 BuiltinType *baseType;
1173
Chris Lattnerf52ab252008-04-06 22:59:24 +00001174 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001175 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001176
1177 // Check if we've already instantiated a vector of this type.
1178 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001179 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 void *InsertPos = 0;
1181 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1182 return QualType(VTP, 0);
1183
1184 // If the element type isn't canonical, this won't be a canonical type either,
1185 // so fill in the canonical type field.
1186 QualType Canonical;
1187 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001188 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001189
1190 // Get the new insert position for the node we care about.
1191 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001192 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 }
Steve Narofff83820b2009-01-27 22:08:43 +00001194 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 VectorTypes.InsertNode(New, InsertPos);
1196 Types.push_back(New);
1197 return QualType(New, 0);
1198}
1199
Nate Begeman213541a2008-04-18 23:10:10 +00001200/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001201/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001202QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001203 BuiltinType *baseType;
1204
Chris Lattnerf52ab252008-04-06 22:59:24 +00001205 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001206 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001207
1208 // Check if we've already instantiated a vector of this type.
1209 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001210 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001211 void *InsertPos = 0;
1212 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1213 return QualType(VTP, 0);
1214
1215 // If the element type isn't canonical, this won't be a canonical type either,
1216 // so fill in the canonical type field.
1217 QualType Canonical;
1218 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001219 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001220
1221 // Get the new insert position for the node we care about.
1222 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001223 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001224 }
Steve Narofff83820b2009-01-27 22:08:43 +00001225 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001226 VectorTypes.InsertNode(New, InsertPos);
1227 Types.push_back(New);
1228 return QualType(New, 0);
1229}
1230
Douglas Gregor72564e72009-02-26 23:50:07 +00001231/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001232///
Douglas Gregor72564e72009-02-26 23:50:07 +00001233QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 // Unique functions, to guarantee there is only one function of a particular
1235 // structure.
1236 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001237 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001238
1239 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001240 if (FunctionNoProtoType *FT =
1241 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 return QualType(FT, 0);
1243
1244 QualType Canonical;
1245 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001246 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001247
1248 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001249 FunctionNoProtoType *NewIP =
1250 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001251 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 }
1253
Douglas Gregor72564e72009-02-26 23:50:07 +00001254 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001256 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001257 return QualType(New, 0);
1258}
1259
1260/// getFunctionType - Return a normal function type with a typed argument
1261/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001262QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001263 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001264 unsigned TypeQuals, bool hasExceptionSpec,
1265 bool hasAnyExceptionSpec, unsigned NumExs,
1266 const QualType *ExArray) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001267 // Unique functions, to guarantee there is only one function of a particular
1268 // structure.
1269 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001270 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001271 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1272 NumExs, ExArray);
Reid Spencer5f016e22007-07-11 17:01:13 +00001273
1274 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001275 if (FunctionProtoType *FTP =
1276 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001277 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001278
1279 // Determine whether the type being created is already canonical or not.
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 bool isCanonical = ResultTy->isCanonical();
Sebastian Redl465226e2009-05-27 22:11:52 +00001281 if (hasExceptionSpec)
1282 isCanonical = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1284 if (!ArgArray[i]->isCanonical())
1285 isCanonical = false;
1286
1287 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001288 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001289 QualType Canonical;
1290 if (!isCanonical) {
1291 llvm::SmallVector<QualType, 16> CanonicalArgs;
1292 CanonicalArgs.reserve(NumArgs);
1293 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001294 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001295
Chris Lattnerf52ab252008-04-06 22:59:24 +00001296 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001297 CanonicalArgs.data(), NumArgs,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001298 isVariadic, TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +00001299
Reid Spencer5f016e22007-07-11 17:01:13 +00001300 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001301 FunctionProtoType *NewIP =
1302 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001303 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001304 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001305
Douglas Gregor72564e72009-02-26 23:50:07 +00001306 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001307 // for two variable size arrays (for parameter and exception types) at the
1308 // end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001309 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001310 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1311 NumArgs*sizeof(QualType) +
1312 NumExs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001313 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001314 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1315 ExArray, NumExs, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001316 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001317 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001318 return QualType(FTP, 0);
1319}
1320
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001321/// getTypeDeclType - Return the unique reference to the type for the
1322/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001323QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001324 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001325 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1326
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001327 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001328 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001329 else if (isa<TemplateTypeParmDecl>(Decl)) {
1330 assert(false && "Template type parameter types are always available.");
1331 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001332 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001333
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001334 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001335 if (PrevDecl)
1336 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001337 else
1338 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001339 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001340 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1341 if (PrevDecl)
1342 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001343 else
1344 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001345 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001346 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001347 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001348
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001349 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001350 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001351}
1352
Reid Spencer5f016e22007-07-11 17:01:13 +00001353/// getTypedefType - Return the unique reference to the type for the
1354/// specified typename decl.
1355QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1356 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1357
Chris Lattnerf52ab252008-04-06 22:59:24 +00001358 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001359 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001360 Types.push_back(Decl->TypeForDecl);
1361 return QualType(Decl->TypeForDecl, 0);
1362}
1363
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001364/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001365/// specified ObjC interface decl.
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001366QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001367 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1368
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001369 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1370 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff3536b442007-09-06 21:24:23 +00001371 Types.push_back(Decl->TypeForDecl);
1372 return QualType(Decl->TypeForDecl, 0);
1373}
1374
Douglas Gregorfab9d672009-02-05 23:33:38 +00001375/// \brief Retrieve the template type parameter type for a template
1376/// parameter with the given depth, index, and (optionally) name.
1377QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1378 IdentifierInfo *Name) {
1379 llvm::FoldingSetNodeID ID;
1380 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1381 void *InsertPos = 0;
1382 TemplateTypeParmType *TypeParm
1383 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1384
1385 if (TypeParm)
1386 return QualType(TypeParm, 0);
1387
1388 if (Name)
1389 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1390 getTemplateTypeParmType(Depth, Index));
1391 else
1392 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1393
1394 Types.push_back(TypeParm);
1395 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1396
1397 return QualType(TypeParm, 0);
1398}
1399
Douglas Gregor55f6b142009-02-09 18:46:07 +00001400QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001401ASTContext::getTemplateSpecializationType(TemplateName Template,
1402 const TemplateArgument *Args,
1403 unsigned NumArgs,
1404 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001405 if (!Canon.isNull())
1406 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001407
Douglas Gregor55f6b142009-02-09 18:46:07 +00001408 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001409 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001410
Douglas Gregor55f6b142009-02-09 18:46:07 +00001411 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001412 TemplateSpecializationType *Spec
1413 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001414
1415 if (Spec)
1416 return QualType(Spec, 0);
1417
Douglas Gregor7532dc62009-03-30 22:58:21 +00001418 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001419 sizeof(TemplateArgument) * NumArgs),
1420 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001421 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001422 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001423 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001424
1425 return QualType(Spec, 0);
1426}
1427
Douglas Gregore4e5b052009-03-19 00:18:19 +00001428QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001429ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001430 QualType NamedType) {
1431 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001432 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001433
1434 void *InsertPos = 0;
1435 QualifiedNameType *T
1436 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1437 if (T)
1438 return QualType(T, 0);
1439
Douglas Gregorab452ba2009-03-26 23:50:42 +00001440 T = new (*this) QualifiedNameType(NNS, NamedType,
1441 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001442 Types.push_back(T);
1443 QualifiedNameTypes.InsertNode(T, InsertPos);
1444 return QualType(T, 0);
1445}
1446
Douglas Gregord57959a2009-03-27 23:10:48 +00001447QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1448 const IdentifierInfo *Name,
1449 QualType Canon) {
1450 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1451
1452 if (Canon.isNull()) {
1453 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1454 if (CanonNNS != NNS)
1455 Canon = getTypenameType(CanonNNS, Name);
1456 }
1457
1458 llvm::FoldingSetNodeID ID;
1459 TypenameType::Profile(ID, NNS, Name);
1460
1461 void *InsertPos = 0;
1462 TypenameType *T
1463 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1464 if (T)
1465 return QualType(T, 0);
1466
1467 T = new (*this) TypenameType(NNS, Name, Canon);
1468 Types.push_back(T);
1469 TypenameTypes.InsertNode(T, InsertPos);
1470 return QualType(T, 0);
1471}
1472
Douglas Gregor17343172009-04-01 00:28:59 +00001473QualType
1474ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1475 const TemplateSpecializationType *TemplateId,
1476 QualType Canon) {
1477 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1478
1479 if (Canon.isNull()) {
1480 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1481 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1482 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1483 const TemplateSpecializationType *CanonTemplateId
1484 = CanonType->getAsTemplateSpecializationType();
1485 assert(CanonTemplateId &&
1486 "Canonical type must also be a template specialization type");
1487 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1488 }
1489 }
1490
1491 llvm::FoldingSetNodeID ID;
1492 TypenameType::Profile(ID, NNS, TemplateId);
1493
1494 void *InsertPos = 0;
1495 TypenameType *T
1496 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1497 if (T)
1498 return QualType(T, 0);
1499
1500 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1501 Types.push_back(T);
1502 TypenameTypes.InsertNode(T, InsertPos);
1503 return QualType(T, 0);
1504}
1505
Chris Lattner88cb27a2008-04-07 04:56:42 +00001506/// CmpProtocolNames - Comparison predicate for sorting protocols
1507/// alphabetically.
1508static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1509 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001510 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001511}
1512
1513static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1514 unsigned &NumProtocols) {
1515 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1516
1517 // Sort protocols, keyed by name.
1518 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1519
1520 // Remove duplicates.
1521 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1522 NumProtocols = ProtocolsEnd-Protocols;
1523}
1524
1525
Chris Lattner065f0d72008-04-07 04:44:08 +00001526/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1527/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001528QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1529 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001530 // Sort the protocol list alphabetically to canonicalize it.
1531 SortAndUniqueProtocols(Protocols, NumProtocols);
1532
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001533 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001534 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001535
1536 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001537 if (ObjCQualifiedInterfaceType *QT =
1538 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001539 return QualType(QT, 0);
1540
1541 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001542 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001543 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001544
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001545 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001546 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001547 return QualType(QType, 0);
1548}
1549
Chris Lattner88cb27a2008-04-07 04:56:42 +00001550/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1551/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001552QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001553 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001554 // Sort the protocol list alphabetically to canonicalize it.
1555 SortAndUniqueProtocols(Protocols, NumProtocols);
1556
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001557 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001558 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001559
1560 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001561 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001562 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001563 return QualType(QT, 0);
1564
1565 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001566 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001567 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001568 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001569 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001570 return QualType(QType, 0);
1571}
1572
Douglas Gregor72564e72009-02-26 23:50:07 +00001573/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1574/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001575/// multiple declarations that refer to "typeof(x)" all contain different
1576/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1577/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001578QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001579 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001580 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001581 Types.push_back(toe);
1582 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001583}
1584
Steve Naroff9752f252007-08-01 18:02:17 +00001585/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1586/// TypeOfType AST's. The only motivation to unique these nodes would be
1587/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1588/// an issue. This doesn't effect the type checker, since it operates
1589/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001590QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001591 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001592 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001593 Types.push_back(tot);
1594 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001595}
1596
Reid Spencer5f016e22007-07-11 17:01:13 +00001597/// getTagDeclType - Return the unique reference to the type for the
1598/// specified TagDecl (struct/union/class/enum) decl.
1599QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001600 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001601 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001602}
1603
1604/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1605/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1606/// needs to agree with the definition in <stddef.h>.
1607QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001608 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001609}
1610
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001611/// getSignedWCharType - Return the type of "signed wchar_t".
1612/// Used when in C++, as a GCC extension.
1613QualType ASTContext::getSignedWCharType() const {
1614 // FIXME: derive from "Target" ?
1615 return WCharTy;
1616}
1617
1618/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1619/// Used when in C++, as a GCC extension.
1620QualType ASTContext::getUnsignedWCharType() const {
1621 // FIXME: derive from "Target" ?
1622 return UnsignedIntTy;
1623}
1624
Chris Lattner8b9023b2007-07-13 03:05:23 +00001625/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1626/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1627QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001628 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001629}
1630
Chris Lattnere6327742008-04-02 05:18:44 +00001631//===----------------------------------------------------------------------===//
1632// Type Operators
1633//===----------------------------------------------------------------------===//
1634
Chris Lattner77c96472008-04-06 22:41:35 +00001635/// getCanonicalType - Return the canonical (structural) type corresponding to
1636/// the specified potentially non-canonical type. The non-canonical version
1637/// of a type may have many "decorated" versions of types. Decorators can
1638/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1639/// to be free of any of these, allowing two canonical types to be compared
1640/// for exact equality with a simple pointer comparison.
1641QualType ASTContext::getCanonicalType(QualType T) {
1642 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001643
1644 // If the result has type qualifiers, make sure to canonicalize them as well.
1645 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1646 if (TypeQuals == 0) return CanType;
1647
1648 // If the type qualifiers are on an array type, get the canonical type of the
1649 // array with the qualifiers applied to the element type.
1650 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1651 if (!AT)
1652 return CanType.getQualifiedType(TypeQuals);
1653
1654 // Get the canonical version of the element with the extra qualifiers on it.
1655 // This can recursively sink qualifiers through multiple levels of arrays.
1656 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1657 NewEltTy = getCanonicalType(NewEltTy);
1658
1659 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1660 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1661 CAT->getIndexTypeQualifier());
1662 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1663 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1664 IAT->getIndexTypeQualifier());
1665
Douglas Gregor898574e2008-12-05 23:32:09 +00001666 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1667 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1668 DSAT->getSizeModifier(),
1669 DSAT->getIndexTypeQualifier());
1670
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001671 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1672 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1673 VAT->getSizeModifier(),
1674 VAT->getIndexTypeQualifier());
1675}
1676
Douglas Gregor7da97d02009-05-10 22:57:19 +00001677Decl *ASTContext::getCanonicalDecl(Decl *D) {
Douglas Gregorc4ccf012009-05-10 22:59:12 +00001678 if (!D)
1679 return 0;
1680
Douglas Gregor7da97d02009-05-10 22:57:19 +00001681 if (TagDecl *Tag = dyn_cast<TagDecl>(D)) {
1682 QualType T = getTagDeclType(Tag);
1683 return cast<TagDecl>(cast<TagType>(T.getTypePtr()->CanonicalType)
1684 ->getDecl());
1685 }
1686
1687 if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(D)) {
1688 while (Template->getPreviousDeclaration())
1689 Template = Template->getPreviousDeclaration();
1690 return Template;
1691 }
1692
1693 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
1694 while (Function->getPreviousDeclaration())
1695 Function = Function->getPreviousDeclaration();
1696 return const_cast<FunctionDecl *>(Function);
1697 }
1698
1699 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
1700 while (Var->getPreviousDeclaration())
1701 Var = Var->getPreviousDeclaration();
1702 return const_cast<VarDecl *>(Var);
1703 }
1704
1705 return D;
1706}
1707
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001708TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
1709 // If this template name refers to a template, the canonical
1710 // template name merely stores the template itself.
1711 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Douglas Gregor7da97d02009-05-10 22:57:19 +00001712 return TemplateName(cast<TemplateDecl>(getCanonicalDecl(Template)));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001713
1714 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
1715 assert(DTN && "Non-dependent template names must refer to template decls.");
1716 return DTN->CanonicalTemplateName;
1717}
1718
Douglas Gregord57959a2009-03-27 23:10:48 +00001719NestedNameSpecifier *
1720ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1721 if (!NNS)
1722 return 0;
1723
1724 switch (NNS->getKind()) {
1725 case NestedNameSpecifier::Identifier:
1726 // Canonicalize the prefix but keep the identifier the same.
1727 return NestedNameSpecifier::Create(*this,
1728 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1729 NNS->getAsIdentifier());
1730
1731 case NestedNameSpecifier::Namespace:
1732 // A namespace is canonical; build a nested-name-specifier with
1733 // this namespace and no prefix.
1734 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1735
1736 case NestedNameSpecifier::TypeSpec:
1737 case NestedNameSpecifier::TypeSpecWithTemplate: {
1738 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1739 NestedNameSpecifier *Prefix = 0;
1740
1741 // FIXME: This isn't the right check!
1742 if (T->isDependentType())
1743 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1744
1745 return NestedNameSpecifier::Create(*this, Prefix,
1746 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1747 T.getTypePtr());
1748 }
1749
1750 case NestedNameSpecifier::Global:
1751 // The global specifier is canonical and unique.
1752 return NNS;
1753 }
1754
1755 // Required to silence a GCC warning
1756 return 0;
1757}
1758
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001759
1760const ArrayType *ASTContext::getAsArrayType(QualType T) {
1761 // Handle the non-qualified case efficiently.
1762 if (T.getCVRQualifiers() == 0) {
1763 // Handle the common positive case fast.
1764 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1765 return AT;
1766 }
1767
1768 // Handle the common negative case fast, ignoring CVR qualifiers.
1769 QualType CType = T->getCanonicalTypeInternal();
1770
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001771 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001772 // test.
1773 if (!isa<ArrayType>(CType) &&
1774 !isa<ArrayType>(CType.getUnqualifiedType()))
1775 return 0;
1776
1777 // Apply any CVR qualifiers from the array type to the element type. This
1778 // implements C99 6.7.3p8: "If the specification of an array type includes
1779 // any type qualifiers, the element type is so qualified, not the array type."
1780
1781 // If we get here, we either have type qualifiers on the type, or we have
1782 // sugar such as a typedef in the way. If we have type qualifiers on the type
1783 // we must propagate them down into the elemeng type.
1784 unsigned CVRQuals = T.getCVRQualifiers();
1785 unsigned AddrSpace = 0;
1786 Type *Ty = T.getTypePtr();
1787
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001788 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001789 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001790 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1791 AddrSpace = EXTQT->getAddressSpace();
1792 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001793 } else {
1794 T = Ty->getDesugaredType();
1795 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1796 break;
1797 CVRQuals |= T.getCVRQualifiers();
1798 Ty = T.getTypePtr();
1799 }
1800 }
1801
1802 // If we have a simple case, just return now.
1803 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1804 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1805 return ATy;
1806
1807 // Otherwise, we have an array and we have qualifiers on it. Push the
1808 // qualifiers into the array element type and return a new array type.
1809 // Get the canonical version of the element with the extra qualifiers on it.
1810 // This can recursively sink qualifiers through multiple levels of arrays.
1811 QualType NewEltTy = ATy->getElementType();
1812 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001813 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001814 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1815
1816 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1817 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1818 CAT->getSizeModifier(),
1819 CAT->getIndexTypeQualifier()));
1820 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1821 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1822 IAT->getSizeModifier(),
1823 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001824
Douglas Gregor898574e2008-12-05 23:32:09 +00001825 if (const DependentSizedArrayType *DSAT
1826 = dyn_cast<DependentSizedArrayType>(ATy))
1827 return cast<ArrayType>(
1828 getDependentSizedArrayType(NewEltTy,
1829 DSAT->getSizeExpr(),
1830 DSAT->getSizeModifier(),
1831 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001832
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001833 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1834 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1835 VAT->getSizeModifier(),
1836 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001837}
1838
1839
Chris Lattnere6327742008-04-02 05:18:44 +00001840/// getArrayDecayedType - Return the properly qualified result of decaying the
1841/// specified array type to a pointer. This operation is non-trivial when
1842/// handling typedefs etc. The canonical type of "T" must be an array type,
1843/// this returns a pointer to a properly qualified element of the array.
1844///
1845/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1846QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001847 // Get the element type with 'getAsArrayType' so that we don't lose any
1848 // typedefs in the element type of the array. This also handles propagation
1849 // of type qualifiers from the array type into the element type if present
1850 // (C99 6.7.3p8).
1851 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1852 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001853
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001854 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001855
1856 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001857 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001858}
1859
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001860QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001861 QualType ElemTy = VAT->getElementType();
1862
1863 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1864 return getBaseElementType(VAT);
1865
1866 return ElemTy;
1867}
1868
Reid Spencer5f016e22007-07-11 17:01:13 +00001869/// getFloatingRank - Return a relative rank for floating point types.
1870/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001871static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001872 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001873 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001874
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001875 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001876 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001877 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001878 case BuiltinType::Float: return FloatRank;
1879 case BuiltinType::Double: return DoubleRank;
1880 case BuiltinType::LongDouble: return LongDoubleRank;
1881 }
1882}
1883
Steve Naroff716c7302007-08-27 01:41:48 +00001884/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1885/// point or a complex type (based on typeDomain/typeSize).
1886/// 'typeDomain' is a real floating point or complex type.
1887/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001888QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1889 QualType Domain) const {
1890 FloatingRank EltRank = getFloatingRank(Size);
1891 if (Domain->isComplexType()) {
1892 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001893 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001894 case FloatRank: return FloatComplexTy;
1895 case DoubleRank: return DoubleComplexTy;
1896 case LongDoubleRank: return LongDoubleComplexTy;
1897 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001898 }
Chris Lattner1361b112008-04-06 23:58:54 +00001899
1900 assert(Domain->isRealFloatingType() && "Unknown domain!");
1901 switch (EltRank) {
1902 default: assert(0 && "getFloatingRank(): illegal value for rank");
1903 case FloatRank: return FloatTy;
1904 case DoubleRank: return DoubleTy;
1905 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001906 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001907}
1908
Chris Lattner7cfeb082008-04-06 23:55:33 +00001909/// getFloatingTypeOrder - Compare the rank of the two specified floating
1910/// point types, ignoring the domain of the type (i.e. 'double' ==
1911/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1912/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001913int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1914 FloatingRank LHSR = getFloatingRank(LHS);
1915 FloatingRank RHSR = getFloatingRank(RHS);
1916
1917 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001918 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001919 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001920 return 1;
1921 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001922}
1923
Chris Lattnerf52ab252008-04-06 22:59:24 +00001924/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1925/// routine will assert if passed a built-in type that isn't an integer or enum,
1926/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001927unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001928 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001929 if (EnumType* ET = dyn_cast<EnumType>(T))
1930 T = ET->getDecl()->getIntegerType().getTypePtr();
1931
1932 // There are two things which impact the integer rank: the width, and
1933 // the ordering of builtins. The builtin ordering is encoded in the
1934 // bottom three bits; the width is encoded in the bits above that.
1935 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1936 return FWIT->getWidth() << 3;
1937 }
1938
Chris Lattnerf52ab252008-04-06 22:59:24 +00001939 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001940 default: assert(0 && "getIntegerRank(): not a built-in integer");
1941 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001942 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001943 case BuiltinType::Char_S:
1944 case BuiltinType::Char_U:
1945 case BuiltinType::SChar:
1946 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001947 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001948 case BuiltinType::Short:
1949 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001950 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001951 case BuiltinType::Int:
1952 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001953 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001954 case BuiltinType::Long:
1955 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001956 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001957 case BuiltinType::LongLong:
1958 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001959 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00001960 case BuiltinType::Int128:
1961 case BuiltinType::UInt128:
1962 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001963 }
1964}
1965
Chris Lattner7cfeb082008-04-06 23:55:33 +00001966/// getIntegerTypeOrder - Returns the highest ranked integer type:
1967/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1968/// LHS < RHS, return -1.
1969int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001970 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1971 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001972 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001973
Chris Lattnerf52ab252008-04-06 22:59:24 +00001974 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1975 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001976
Chris Lattner7cfeb082008-04-06 23:55:33 +00001977 unsigned LHSRank = getIntegerRank(LHSC);
1978 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001979
Chris Lattner7cfeb082008-04-06 23:55:33 +00001980 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1981 if (LHSRank == RHSRank) return 0;
1982 return LHSRank > RHSRank ? 1 : -1;
1983 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001984
Chris Lattner7cfeb082008-04-06 23:55:33 +00001985 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1986 if (LHSUnsigned) {
1987 // If the unsigned [LHS] type is larger, return it.
1988 if (LHSRank >= RHSRank)
1989 return 1;
1990
1991 // If the signed type can represent all values of the unsigned type, it
1992 // wins. Because we are dealing with 2's complement and types that are
1993 // powers of two larger than each other, this is always safe.
1994 return -1;
1995 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001996
Chris Lattner7cfeb082008-04-06 23:55:33 +00001997 // If the unsigned [RHS] type is larger, return it.
1998 if (RHSRank >= LHSRank)
1999 return -1;
2000
2001 // If the signed type can represent all values of the unsigned type, it
2002 // wins. Because we are dealing with 2's complement and types that are
2003 // powers of two larger than each other, this is always safe.
2004 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002005}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002006
2007// getCFConstantStringType - Return the type used for constant CFStrings.
2008QualType ASTContext::getCFConstantStringType() {
2009 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00002010 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002011 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002012 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002013 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00002014
2015 // const int *isa;
2016 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002017 // int flags;
2018 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002019 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002020 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00002021 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002022 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00002023
Anders Carlsson71993dd2007-08-17 05:31:46 +00002024 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002025 for (unsigned i = 0; i < 4; ++i) {
2026 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2027 SourceLocation(), 0,
2028 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002029 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002030 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002031 }
2032
2033 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002034 }
2035
2036 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002037}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002038
Douglas Gregor319ac892009-04-23 22:29:11 +00002039void ASTContext::setCFConstantStringType(QualType T) {
2040 const RecordType *Rec = T->getAsRecordType();
2041 assert(Rec && "Invalid CFConstantStringType");
2042 CFConstantStringTypeDecl = Rec->getDecl();
2043}
2044
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002045QualType ASTContext::getObjCFastEnumerationStateType()
2046{
2047 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002048 ObjCFastEnumerationStateTypeDecl =
2049 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2050 &Idents.get("__objcFastEnumerationState"));
2051
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002052 QualType FieldTypes[] = {
2053 UnsignedLongTy,
2054 getPointerType(ObjCIdType),
2055 getPointerType(UnsignedLongTy),
2056 getConstantArrayType(UnsignedLongTy,
2057 llvm::APInt(32, 5), ArrayType::Normal, 0)
2058 };
2059
Douglas Gregor44b43212008-12-11 16:49:14 +00002060 for (size_t i = 0; i < 4; ++i) {
2061 FieldDecl *Field = FieldDecl::Create(*this,
2062 ObjCFastEnumerationStateTypeDecl,
2063 SourceLocation(), 0,
2064 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002065 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002066 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002067 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002068
Douglas Gregor44b43212008-12-11 16:49:14 +00002069 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002070 }
2071
2072 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2073}
2074
Douglas Gregor319ac892009-04-23 22:29:11 +00002075void ASTContext::setObjCFastEnumerationStateType(QualType T) {
2076 const RecordType *Rec = T->getAsRecordType();
2077 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2078 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2079}
2080
Anders Carlssone8c49532007-10-29 06:33:42 +00002081// This returns true if a type has been typedefed to BOOL:
2082// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002083static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002084 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002085 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2086 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002087
2088 return false;
2089}
2090
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002091/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002092/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002093int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002094 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002095
2096 // Make all integer and enum types at least as large as an int
2097 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002098 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002099 // Treat arrays as pointers, since that's how they're passed in.
2100 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002101 sz = getTypeSize(VoidPtrTy);
2102 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002103}
2104
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002105/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002106/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002107void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002108 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002109 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002110 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002111 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002112 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002113 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002114 // Compute size of all parameters.
2115 // Start with computing size of a pointer in number of bytes.
2116 // FIXME: There might(should) be a better way of doing this computation!
2117 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002118 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002119 // The first two arguments (self and _cmd) are pointers; account for
2120 // their size.
2121 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002122 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2123 E = Decl->param_end(); PI != E; ++PI) {
2124 QualType PType = (*PI)->getType();
2125 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002126 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002127 ParmOffset += sz;
2128 }
2129 S += llvm::utostr(ParmOffset);
2130 S += "@0:";
2131 S += llvm::utostr(PtrSize);
2132
2133 // Argument types.
2134 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002135 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2136 E = Decl->param_end(); PI != E; ++PI) {
2137 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002138 QualType PType = PVDecl->getOriginalType();
2139 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002140 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2141 // Use array's original type only if it has known number of
2142 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002143 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002144 PType = PVDecl->getType();
2145 } else if (PType->isFunctionType())
2146 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002147 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002148 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002149 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002150 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002151 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002152 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002153 }
2154}
2155
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002156/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002157/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002158/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2159/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002160/// Property attributes are stored as a comma-delimited C string. The simple
2161/// attributes readonly and bycopy are encoded as single characters. The
2162/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2163/// encoded as single characters, followed by an identifier. Property types
2164/// are also encoded as a parametrized attribute. The characters used to encode
2165/// these attributes are defined by the following enumeration:
2166/// @code
2167/// enum PropertyAttributes {
2168/// kPropertyReadOnly = 'R', // property is read-only.
2169/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2170/// kPropertyByref = '&', // property is a reference to the value last assigned
2171/// kPropertyDynamic = 'D', // property is dynamic
2172/// kPropertyGetter = 'G', // followed by getter selector name
2173/// kPropertySetter = 'S', // followed by setter selector name
2174/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2175/// kPropertyType = 't' // followed by old-style type encoding.
2176/// kPropertyWeak = 'W' // 'weak' property
2177/// kPropertyStrong = 'P' // property GC'able
2178/// kPropertyNonAtomic = 'N' // property non-atomic
2179/// };
2180/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002181void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2182 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002183 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002184 // Collect information from the property implementation decl(s).
2185 bool Dynamic = false;
2186 ObjCPropertyImplDecl *SynthesizePID = 0;
2187
2188 // FIXME: Duplicated code due to poor abstraction.
2189 if (Container) {
2190 if (const ObjCCategoryImplDecl *CID =
2191 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2192 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002193 i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2194 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002195 ObjCPropertyImplDecl *PID = *i;
2196 if (PID->getPropertyDecl() == PD) {
2197 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2198 Dynamic = true;
2199 } else {
2200 SynthesizePID = PID;
2201 }
2202 }
2203 }
2204 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002205 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002206 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002207 i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2208 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002209 ObjCPropertyImplDecl *PID = *i;
2210 if (PID->getPropertyDecl() == PD) {
2211 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2212 Dynamic = true;
2213 } else {
2214 SynthesizePID = PID;
2215 }
2216 }
2217 }
2218 }
2219 }
2220
2221 // FIXME: This is not very efficient.
2222 S = "T";
2223
2224 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002225 // GCC has some special rules regarding encoding of properties which
2226 // closely resembles encoding of ivars.
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002227 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002228 true /* outermost type */,
2229 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002230
2231 if (PD->isReadOnly()) {
2232 S += ",R";
2233 } else {
2234 switch (PD->getSetterKind()) {
2235 case ObjCPropertyDecl::Assign: break;
2236 case ObjCPropertyDecl::Copy: S += ",C"; break;
2237 case ObjCPropertyDecl::Retain: S += ",&"; break;
2238 }
2239 }
2240
2241 // It really isn't clear at all what this means, since properties
2242 // are "dynamic by default".
2243 if (Dynamic)
2244 S += ",D";
2245
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002246 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2247 S += ",N";
2248
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002249 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2250 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002251 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002252 }
2253
2254 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2255 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002256 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002257 }
2258
2259 if (SynthesizePID) {
2260 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2261 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002262 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002263 }
2264
2265 // FIXME: OBJCGC: weak & strong
2266}
2267
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002268/// getLegacyIntegralTypeEncoding -
2269/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002270/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002271/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2272///
2273void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2274 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2275 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002276 if (BT->getKind() == BuiltinType::ULong &&
2277 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002278 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002279 else
2280 if (BT->getKind() == BuiltinType::Long &&
2281 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002282 PointeeTy = IntTy;
2283 }
2284 }
2285}
2286
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002287void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002288 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002289 // We follow the behavior of gcc, expanding structures which are
2290 // directly pointed to, and expanding embedded structures. Note that
2291 // these rules are sufficient to prevent recursive encoding of the
2292 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002293 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2294 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002295}
2296
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002297static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002298 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002299 const Expr *E = FD->getBitWidth();
2300 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2301 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002302 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002303 S += 'b';
2304 S += llvm::utostr(N);
2305}
2306
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002307void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2308 bool ExpandPointedToStructures,
2309 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002310 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002311 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002312 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002313 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002314 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002315 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002316 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002317 else {
2318 char encoding;
2319 switch (BT->getKind()) {
2320 default: assert(0 && "Unhandled builtin type kind");
2321 case BuiltinType::Void: encoding = 'v'; break;
2322 case BuiltinType::Bool: encoding = 'B'; break;
2323 case BuiltinType::Char_U:
2324 case BuiltinType::UChar: encoding = 'C'; break;
2325 case BuiltinType::UShort: encoding = 'S'; break;
2326 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002327 case BuiltinType::ULong:
2328 encoding =
2329 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2330 break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002331 case BuiltinType::UInt128: encoding = 'T'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002332 case BuiltinType::ULongLong: encoding = 'Q'; break;
2333 case BuiltinType::Char_S:
2334 case BuiltinType::SChar: encoding = 'c'; break;
2335 case BuiltinType::Short: encoding = 's'; break;
2336 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002337 case BuiltinType::Long:
2338 encoding =
2339 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2340 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002341 case BuiltinType::LongLong: encoding = 'q'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002342 case BuiltinType::Int128: encoding = 't'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002343 case BuiltinType::Float: encoding = 'f'; break;
2344 case BuiltinType::Double: encoding = 'd'; break;
2345 case BuiltinType::LongDouble: encoding = 'd'; break;
2346 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002347
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002348 S += encoding;
2349 }
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002350 } else if (const ComplexType *CT = T->getAsComplexType()) {
2351 S += 'j';
2352 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2353 false);
2354 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002355 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2356 ExpandPointedToStructures,
2357 ExpandStructures, FD);
2358 if (FD || EncodingProperty) {
2359 // Note that we do extended encoding of protocol qualifer list
2360 // Only when doing ivar or property encoding.
2361 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2362 S += '"';
Steve Naroff446ee4e2009-05-27 16:21:00 +00002363 for (ObjCQualifiedIdType::qual_iterator I = QIDT->qual_begin(),
2364 E = QIDT->qual_end(); I != E; ++I) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002365 S += '<';
Steve Naroff446ee4e2009-05-27 16:21:00 +00002366 S += (*I)->getNameAsString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002367 S += '>';
2368 }
2369 S += '"';
2370 }
2371 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002372 }
2373 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002374 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002375 bool isReadOnly = false;
2376 // For historical/compatibility reasons, the read-only qualifier of the
2377 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2378 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2379 // Also, do not emit the 'r' for anything but the outermost type!
2380 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2381 if (OutermostType && T.isConstQualified()) {
2382 isReadOnly = true;
2383 S += 'r';
2384 }
2385 }
2386 else if (OutermostType) {
2387 QualType P = PointeeTy;
2388 while (P->getAsPointerType())
2389 P = P->getAsPointerType()->getPointeeType();
2390 if (P.isConstQualified()) {
2391 isReadOnly = true;
2392 S += 'r';
2393 }
2394 }
2395 if (isReadOnly) {
2396 // Another legacy compatibility encoding. Some ObjC qualifier and type
2397 // combinations need to be rearranged.
2398 // Rewrite "in const" from "nr" to "rn"
2399 const char * s = S.c_str();
2400 int len = S.length();
2401 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2402 std::string replace = "rn";
2403 S.replace(S.end()-2, S.end(), replace);
2404 }
2405 }
Steve Naroff389bf462009-02-12 17:52:19 +00002406 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002407 S += '@';
2408 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002409 }
2410 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002411 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002412 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002413 // Another historical/compatibility reason.
2414 // We encode the underlying type which comes out as
2415 // {...};
2416 S += '^';
2417 getObjCEncodingForTypeImpl(PointeeTy, S,
2418 false, ExpandPointedToStructures,
2419 NULL);
2420 return;
2421 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002422 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002423 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002424 const ObjCInterfaceType *OIT =
2425 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002426 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002427 S += '"';
2428 S += OI->getNameAsCString();
Steve Naroff446ee4e2009-05-27 16:21:00 +00002429 for (ObjCInterfaceType::qual_iterator I = OIT->qual_begin(),
2430 E = OIT->qual_end(); I != E; ++I) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002431 S += '<';
Steve Naroff446ee4e2009-05-27 16:21:00 +00002432 S += (*I)->getNameAsString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002433 S += '>';
2434 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002435 S += '"';
2436 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002437 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002438 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002439 S += '#';
2440 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002441 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002442 S += ':';
2443 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002444 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002445
2446 if (PointeeTy->isCharType()) {
2447 // char pointer types should be encoded as '*' unless it is a
2448 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002449 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002450 S += '*';
2451 return;
2452 }
2453 }
2454
2455 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002456 getLegacyIntegralTypeEncoding(PointeeTy);
2457
2458 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002459 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002460 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002461 } else if (const ArrayType *AT =
2462 // Ignore type qualifiers etc.
2463 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002464 if (isa<IncompleteArrayType>(AT)) {
2465 // Incomplete arrays are encoded as a pointer to the array element.
2466 S += '^';
2467
2468 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2469 false, ExpandStructures, FD);
2470 } else {
2471 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002472
Anders Carlsson559a8332009-02-22 01:38:57 +00002473 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2474 S += llvm::utostr(CAT->getSize().getZExtValue());
2475 else {
2476 //Variable length arrays are encoded as a regular array with 0 elements.
2477 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2478 S += '0';
2479 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002480
Anders Carlsson559a8332009-02-22 01:38:57 +00002481 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2482 false, ExpandStructures, FD);
2483 S += ']';
2484 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002485 } else if (T->getAsFunctionType()) {
2486 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002487 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002488 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002489 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002490 // Anonymous structures print as '?'
2491 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2492 S += II->getName();
2493 } else {
2494 S += '?';
2495 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002496 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002497 S += '=';
Douglas Gregor6ab35242009-04-09 21:40:53 +00002498 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2499 FieldEnd = RDecl->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +00002500 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002501 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002502 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002503 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002504 S += '"';
2505 }
2506
2507 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002508 if (Field->isBitField()) {
2509 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2510 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002511 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002512 QualType qt = Field->getType();
2513 getLegacyIntegralTypeEncoding(qt);
2514 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002515 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002516 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002517 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002518 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002519 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002520 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002521 if (FD && FD->isBitField())
2522 EncodeBitField(this, S, FD);
2523 else
2524 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002525 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002526 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002527 } else if (T->isObjCInterfaceType()) {
2528 // @encode(class_name)
2529 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2530 S += '{';
2531 const IdentifierInfo *II = OI->getIdentifier();
2532 S += II->getName();
2533 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002534 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002535 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002536 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002537 if (RecFields[i]->isBitField())
2538 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2539 RecFields[i]);
2540 else
2541 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2542 FD);
2543 }
2544 S += '}';
2545 }
2546 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002547 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002548}
2549
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002550void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002551 std::string& S) const {
2552 if (QT & Decl::OBJC_TQ_In)
2553 S += 'n';
2554 if (QT & Decl::OBJC_TQ_Inout)
2555 S += 'N';
2556 if (QT & Decl::OBJC_TQ_Out)
2557 S += 'o';
2558 if (QT & Decl::OBJC_TQ_Bycopy)
2559 S += 'O';
2560 if (QT & Decl::OBJC_TQ_Byref)
2561 S += 'R';
2562 if (QT & Decl::OBJC_TQ_Oneway)
2563 S += 'V';
2564}
2565
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002566void ASTContext::setBuiltinVaListType(QualType T)
2567{
2568 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2569
2570 BuiltinVaListType = T;
2571}
2572
Douglas Gregor319ac892009-04-23 22:29:11 +00002573void ASTContext::setObjCIdType(QualType T)
Steve Naroff7e219e42007-10-15 14:41:52 +00002574{
Douglas Gregor319ac892009-04-23 22:29:11 +00002575 ObjCIdType = T;
2576
2577 const TypedefType *TT = T->getAsTypedefType();
2578 if (!TT)
2579 return;
2580
2581 TypedefDecl *TD = TT->getDecl();
Steve Naroff7e219e42007-10-15 14:41:52 +00002582
2583 // typedef struct objc_object *id;
2584 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002585 // User error - caller will issue diagnostics.
2586 if (!ptr)
2587 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002588 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002589 // User error - caller will issue diagnostics.
2590 if (!rec)
2591 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002592 IdStructType = rec;
2593}
2594
Douglas Gregor319ac892009-04-23 22:29:11 +00002595void ASTContext::setObjCSelType(QualType T)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002596{
Douglas Gregor319ac892009-04-23 22:29:11 +00002597 ObjCSelType = T;
2598
2599 const TypedefType *TT = T->getAsTypedefType();
2600 if (!TT)
2601 return;
2602 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002603
2604 // typedef struct objc_selector *SEL;
2605 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002606 if (!ptr)
2607 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002608 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002609 if (!rec)
2610 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002611 SelStructType = rec;
2612}
2613
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002614void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002615{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002616 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002617}
2618
Douglas Gregor319ac892009-04-23 22:29:11 +00002619void ASTContext::setObjCClassType(QualType T)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002620{
Douglas Gregor319ac892009-04-23 22:29:11 +00002621 ObjCClassType = T;
2622
2623 const TypedefType *TT = T->getAsTypedefType();
2624 if (!TT)
2625 return;
2626 TypedefDecl *TD = TT->getDecl();
Anders Carlsson8baaca52007-10-31 02:53:19 +00002627
2628 // typedef struct objc_class *Class;
2629 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2630 assert(ptr && "'Class' incorrectly typed");
2631 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2632 assert(rec && "'Class' incorrectly typed");
2633 ClassStructType = rec;
2634}
2635
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002636void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2637 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002638 "'NSConstantString' type already set!");
2639
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002640 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002641}
2642
Douglas Gregor7532dc62009-03-30 22:58:21 +00002643/// \brief Retrieve the template name that represents a qualified
2644/// template name such as \c std::vector.
2645TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2646 bool TemplateKeyword,
2647 TemplateDecl *Template) {
2648 llvm::FoldingSetNodeID ID;
2649 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2650
2651 void *InsertPos = 0;
2652 QualifiedTemplateName *QTN =
2653 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2654 if (!QTN) {
2655 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2656 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2657 }
2658
2659 return TemplateName(QTN);
2660}
2661
2662/// \brief Retrieve the template name that represents a dependent
2663/// template name such as \c MetaFun::template apply.
2664TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2665 const IdentifierInfo *Name) {
2666 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2667
2668 llvm::FoldingSetNodeID ID;
2669 DependentTemplateName::Profile(ID, NNS, Name);
2670
2671 void *InsertPos = 0;
2672 DependentTemplateName *QTN =
2673 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2674
2675 if (QTN)
2676 return TemplateName(QTN);
2677
2678 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2679 if (CanonNNS == NNS) {
2680 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2681 } else {
2682 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2683 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2684 }
2685
2686 DependentTemplateNames.InsertNode(QTN, InsertPos);
2687 return TemplateName(QTN);
2688}
2689
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002690/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002691/// TargetInfo, produce the corresponding type. The unsigned @p Type
2692/// is actually a value of type @c TargetInfo::IntType.
2693QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002694 switch (Type) {
2695 case TargetInfo::NoInt: return QualType();
2696 case TargetInfo::SignedShort: return ShortTy;
2697 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2698 case TargetInfo::SignedInt: return IntTy;
2699 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2700 case TargetInfo::SignedLong: return LongTy;
2701 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2702 case TargetInfo::SignedLongLong: return LongLongTy;
2703 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2704 }
2705
2706 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002707 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002708}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002709
2710//===----------------------------------------------------------------------===//
2711// Type Predicates.
2712//===----------------------------------------------------------------------===//
2713
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002714/// isObjCNSObjectType - Return true if this is an NSObject object using
2715/// NSObject attribute on a c-style pointer type.
2716/// FIXME - Make it work directly on types.
2717///
2718bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2719 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2720 if (TypedefDecl *TD = TDT->getDecl())
2721 if (TD->getAttr<ObjCNSObjectAttr>())
2722 return true;
2723 }
2724 return false;
2725}
2726
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002727/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2728/// to an object type. This includes "id" and "Class" (two 'special' pointers
2729/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2730/// ID type).
2731bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002732 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002733 return true;
2734
Steve Naroff6ae98502008-10-21 18:24:04 +00002735 // Blocks are objects.
2736 if (Ty->isBlockPointerType())
2737 return true;
2738
2739 // All other object types are pointers.
Chris Lattner16ede0e2009-04-12 23:51:02 +00002740 const PointerType *PT = Ty->getAsPointerType();
2741 if (PT == 0)
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002742 return false;
2743
Chris Lattner16ede0e2009-04-12 23:51:02 +00002744 // If this a pointer to an interface (e.g. NSString*), it is ok.
2745 if (PT->getPointeeType()->isObjCInterfaceType() ||
2746 // If is has NSObject attribute, OK as well.
2747 isObjCNSObjectType(Ty))
2748 return true;
2749
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002750 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2751 // pointer types. This looks for the typedef specifically, not for the
Chris Lattner16ede0e2009-04-12 23:51:02 +00002752 // underlying type. Iteratively strip off typedefs so that we can handle
2753 // typedefs of typedefs.
2754 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2755 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2756 Ty.getUnqualifiedType() == getObjCClassType())
2757 return true;
2758
2759 Ty = TDT->getDecl()->getUnderlyingType();
2760 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002761
Chris Lattner16ede0e2009-04-12 23:51:02 +00002762 return false;
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002763}
2764
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002765/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2766/// garbage collection attribute.
2767///
2768QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002769 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002770 if (getLangOptions().ObjC1 &&
2771 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002772 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002773 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002774 // (or pointers to them) be treated as though they were declared
2775 // as __strong.
2776 if (GCAttrs == QualType::GCNone) {
2777 if (isObjCObjectPointerType(Ty))
2778 GCAttrs = QualType::Strong;
2779 else if (Ty->isPointerType())
2780 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2781 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00002782 // Non-pointers have none gc'able attribute regardless of the attribute
2783 // set on them.
2784 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2785 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002786 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002787 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002788}
2789
Chris Lattner6ac46a42008-04-07 06:51:04 +00002790//===----------------------------------------------------------------------===//
2791// Type Compatibility Testing
2792//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002793
Steve Naroff1c7d0672008-09-04 15:10:53 +00002794/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002795/// block types. Types must be strictly compatible here. For example,
2796/// C unfortunately doesn't produce an error for the following:
2797///
2798/// int (*emptyArgFunc)();
2799/// int (*intArgList)(int) = emptyArgFunc;
2800///
2801/// For blocks, we will produce an error for the following (similar to C++):
2802///
2803/// int (^emptyArgBlock)();
2804/// int (^intArgBlock)(int) = emptyArgBlock;
2805///
2806/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2807///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002808bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002809 const FunctionType *lbase = lhs->getAsFunctionType();
2810 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002811 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2812 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stumpaab0f7a2009-04-01 01:17:39 +00002813 if (lproto && rproto == 0)
2814 return false;
2815 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff1c7d0672008-09-04 15:10:53 +00002816}
2817
Chris Lattner6ac46a42008-04-07 06:51:04 +00002818/// areCompatVectorTypes - Return true if the two specified vector types are
2819/// compatible.
2820static bool areCompatVectorTypes(const VectorType *LHS,
2821 const VectorType *RHS) {
2822 assert(LHS->isCanonical() && RHS->isCanonical());
2823 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002824 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002825}
2826
Eli Friedman3d815e72008-08-22 00:56:42 +00002827/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002828/// compatible for assignment from RHS to LHS. This handles validation of any
2829/// protocol qualifiers on the LHS or RHS.
2830///
Eli Friedman3d815e72008-08-22 00:56:42 +00002831bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2832 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002833 // Verify that the base decls are compatible: the RHS must be a subclass of
2834 // the LHS.
2835 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2836 return false;
2837
2838 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2839 // protocol qualified at all, then we are good.
2840 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2841 return true;
2842
2843 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2844 // isn't a superset.
2845 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2846 return true; // FIXME: should return false!
2847
2848 // Finally, we must have two protocol-qualified interfaces.
2849 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2850 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002851
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002852 // All LHS protocols must have a presence on the RHS.
2853 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002854
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002855 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2856 LHSPE = LHSP->qual_end();
2857 LHSPI != LHSPE; LHSPI++) {
2858 bool RHSImplementsProtocol = false;
2859
2860 // If the RHS doesn't implement the protocol on the left, the types
2861 // are incompatible.
2862 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2863 RHSPE = RHSP->qual_end();
2864 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2865 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2866 RHSImplementsProtocol = true;
2867 }
2868 // FIXME: For better diagnostics, consider passing back the protocol name.
2869 if (!RHSImplementsProtocol)
2870 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002871 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002872 // The RHS implements all protocols listed on the LHS.
2873 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002874}
2875
Steve Naroff389bf462009-02-12 17:52:19 +00002876bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2877 // get the "pointed to" types
2878 const PointerType *LHSPT = LHS->getAsPointerType();
2879 const PointerType *RHSPT = RHS->getAsPointerType();
2880
2881 if (!LHSPT || !RHSPT)
2882 return false;
2883
2884 QualType lhptee = LHSPT->getPointeeType();
2885 QualType rhptee = RHSPT->getPointeeType();
2886 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2887 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2888 // ID acts sort of like void* for ObjC interfaces
2889 if (LHSIface && isObjCIdStructType(rhptee))
2890 return true;
2891 if (RHSIface && isObjCIdStructType(lhptee))
2892 return true;
2893 if (!LHSIface || !RHSIface)
2894 return false;
2895 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2896 canAssignObjCInterfaces(RHSIface, LHSIface);
2897}
2898
Steve Naroffec0550f2007-10-15 20:41:53 +00002899/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2900/// both shall have the identically qualified version of a compatible type.
2901/// C99 6.2.7p1: Two types have compatible types if their types are the
2902/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002903bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2904 return !mergeTypes(LHS, RHS).isNull();
2905}
2906
2907QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2908 const FunctionType *lbase = lhs->getAsFunctionType();
2909 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002910 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2911 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002912 bool allLTypes = true;
2913 bool allRTypes = true;
2914
2915 // Check return type
2916 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2917 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002918 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2919 allLTypes = false;
2920 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2921 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002922
2923 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00002924 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
2925 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00002926 unsigned lproto_nargs = lproto->getNumArgs();
2927 unsigned rproto_nargs = rproto->getNumArgs();
2928
2929 // Compatible functions must have the same number of arguments
2930 if (lproto_nargs != rproto_nargs)
2931 return QualType();
2932
2933 // Variadic and non-variadic functions aren't compatible
2934 if (lproto->isVariadic() != rproto->isVariadic())
2935 return QualType();
2936
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002937 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2938 return QualType();
2939
Eli Friedman3d815e72008-08-22 00:56:42 +00002940 // Check argument compatibility
2941 llvm::SmallVector<QualType, 10> types;
2942 for (unsigned i = 0; i < lproto_nargs; i++) {
2943 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2944 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2945 QualType argtype = mergeTypes(largtype, rargtype);
2946 if (argtype.isNull()) return QualType();
2947 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002948 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2949 allLTypes = false;
2950 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2951 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002952 }
2953 if (allLTypes) return lhs;
2954 if (allRTypes) return rhs;
2955 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002956 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002957 }
2958
2959 if (lproto) allRTypes = false;
2960 if (rproto) allLTypes = false;
2961
Douglas Gregor72564e72009-02-26 23:50:07 +00002962 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002963 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00002964 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00002965 if (proto->isVariadic()) return QualType();
2966 // Check that the types are compatible with the types that
2967 // would result from default argument promotions (C99 6.7.5.3p15).
2968 // The only types actually affected are promotable integer
2969 // types and floats, which would be passed as a different
2970 // type depending on whether the prototype is visible.
2971 unsigned proto_nargs = proto->getNumArgs();
2972 for (unsigned i = 0; i < proto_nargs; ++i) {
2973 QualType argTy = proto->getArgType(i);
2974 if (argTy->isPromotableIntegerType() ||
2975 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2976 return QualType();
2977 }
2978
2979 if (allLTypes) return lhs;
2980 if (allRTypes) return rhs;
2981 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002982 proto->getNumArgs(), lproto->isVariadic(),
2983 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002984 }
2985
2986 if (allLTypes) return lhs;
2987 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002988 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002989}
2990
2991QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002992 // C++ [expr]: If an expression initially has the type "reference to T", the
2993 // type is adjusted to "T" prior to any further analysis, the expression
2994 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002995 // expression is an lvalue unless the reference is an rvalue reference and
2996 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00002997 // FIXME: C++ shouldn't be going through here! The rules are different
2998 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002999 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3000 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00003001 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003002 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00003003 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003004 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00003005
Eli Friedman3d815e72008-08-22 00:56:42 +00003006 QualType LHSCan = getCanonicalType(LHS),
3007 RHSCan = getCanonicalType(RHS);
3008
3009 // If two types are identical, they are compatible.
3010 if (LHSCan == RHSCan)
3011 return LHS;
3012
3013 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003014 // Note that we handle extended qualifiers later, in the
3015 // case for ExtQualType.
3016 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00003017 return QualType();
3018
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003019 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
3020 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003021
Chris Lattner1adb8832008-01-14 05:45:46 +00003022 // We want to consider the two function types to be the same for these
3023 // comparisons, just force one to the other.
3024 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3025 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003026
3027 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003028 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3029 LHSClass = Type::ConstantArray;
3030 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3031 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00003032
Nate Begeman213541a2008-04-18 23:10:10 +00003033 // Canonicalize ExtVector -> Vector.
3034 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3035 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00003036
Chris Lattnerb0489812008-04-07 06:38:24 +00003037 // Consider qualified interfaces and interfaces the same.
3038 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
3039 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00003040
Chris Lattnera36a61f2008-04-07 05:43:21 +00003041 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003042 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003043 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3044 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003045
Steve Naroffd824c9c2009-04-14 15:11:46 +00003046 // 'id' and 'Class' act sort of like void* for ObjC interfaces
3047 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003048 return LHS;
Steve Naroffd824c9c2009-04-14 15:11:46 +00003049 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003050 return RHS;
3051
Steve Naroffbc76dd02008-12-10 22:14:21 +00003052 // ID is compatible with all qualified id types.
3053 if (LHS->isObjCQualifiedIdType()) {
3054 if (const PointerType *PT = RHS->getAsPointerType()) {
3055 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003056 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003057 return LHS;
3058 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3059 // Unfortunately, this API is part of Sema (which we don't have access
3060 // to. Need to refactor. The following check is insufficient, since we
3061 // need to make sure the class implements the protocol.
3062 if (pType->isObjCInterfaceType())
3063 return LHS;
3064 }
3065 }
3066 if (RHS->isObjCQualifiedIdType()) {
3067 if (const PointerType *PT = LHS->getAsPointerType()) {
3068 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003069 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003070 return RHS;
3071 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3072 // Unfortunately, this API is part of Sema (which we don't have access
3073 // to. Need to refactor. The following check is insufficient, since we
3074 // need to make sure the class implements the protocol.
3075 if (pType->isObjCInterfaceType())
3076 return RHS;
3077 }
3078 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003079 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3080 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00003081 if (const EnumType* ETy = LHS->getAsEnumType()) {
3082 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3083 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003084 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003085 if (const EnumType* ETy = RHS->getAsEnumType()) {
3086 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3087 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003088 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003089
Eli Friedman3d815e72008-08-22 00:56:42 +00003090 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003091 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003092
Steve Naroff4a746782008-01-09 22:43:08 +00003093 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003094 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003095#define TYPE(Class, Base)
3096#define ABSTRACT_TYPE(Class, Base)
3097#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3098#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3099#include "clang/AST/TypeNodes.def"
3100 assert(false && "Non-canonical and dependent types shouldn't get here");
3101 return QualType();
3102
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003103 case Type::LValueReference:
3104 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003105 case Type::MemberPointer:
3106 assert(false && "C++ should never be in mergeTypes");
3107 return QualType();
3108
3109 case Type::IncompleteArray:
3110 case Type::VariableArray:
3111 case Type::FunctionProto:
3112 case Type::ExtVector:
3113 case Type::ObjCQualifiedInterface:
3114 assert(false && "Types are eliminated above");
3115 return QualType();
3116
Chris Lattner1adb8832008-01-14 05:45:46 +00003117 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003118 {
3119 // Merge two pointer types, while trying to preserve typedef info
3120 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3121 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3122 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3123 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003124 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3125 return LHS;
3126 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3127 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003128 return getPointerType(ResultType);
3129 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003130 case Type::BlockPointer:
3131 {
3132 // Merge two block pointer types, while trying to preserve typedef info
3133 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3134 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3135 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3136 if (ResultType.isNull()) return QualType();
3137 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3138 return LHS;
3139 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3140 return RHS;
3141 return getBlockPointerType(ResultType);
3142 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003143 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003144 {
3145 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3146 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3147 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3148 return QualType();
3149
3150 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3151 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3152 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3153 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003154 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3155 return LHS;
3156 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3157 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003158 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3159 ArrayType::ArraySizeModifier(), 0);
3160 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3161 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003162 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3163 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003164 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3165 return LHS;
3166 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3167 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003168 if (LVAT) {
3169 // FIXME: This isn't correct! But tricky to implement because
3170 // the array's size has to be the size of LHS, but the type
3171 // has to be different.
3172 return LHS;
3173 }
3174 if (RVAT) {
3175 // FIXME: This isn't correct! But tricky to implement because
3176 // the array's size has to be the size of RHS, but the type
3177 // has to be different.
3178 return RHS;
3179 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003180 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3181 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003182 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003183 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003184 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003185 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003186 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003187 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003188 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003189 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3190 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003191 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003192 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003193 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003194 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003195 case Type::Complex:
3196 // Distinct complex types are incompatible.
3197 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003198 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003199 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003200 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3201 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003202 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003203 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003204 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003205 // FIXME: This should be type compatibility, e.g. whether
3206 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003207 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3208 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3209 if (LHSIface && RHSIface &&
3210 canAssignObjCInterfaces(LHSIface, RHSIface))
3211 return LHS;
3212
Eli Friedman3d815e72008-08-22 00:56:42 +00003213 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003214 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003215 case Type::ObjCQualifiedId:
3216 // Distinct qualified id's are not compatible.
3217 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003218 case Type::FixedWidthInt:
3219 // Distinct fixed-width integers are not compatible.
3220 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003221 case Type::ExtQual:
3222 // FIXME: ExtQual types can be compatible even if they're not
3223 // identical!
3224 return QualType();
3225 // First attempt at an implementation, but I'm not really sure it's
3226 // right...
3227#if 0
3228 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3229 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3230 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3231 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3232 return QualType();
3233 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3234 LHSBase = QualType(LQual->getBaseType(), 0);
3235 RHSBase = QualType(RQual->getBaseType(), 0);
3236 ResultType = mergeTypes(LHSBase, RHSBase);
3237 if (ResultType.isNull()) return QualType();
3238 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3239 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3240 return LHS;
3241 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3242 return RHS;
3243 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3244 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3245 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3246 return ResultType;
3247#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003248
3249 case Type::TemplateSpecialization:
3250 assert(false && "Dependent types have no size");
3251 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003252 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003253
3254 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003255}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003256
Chris Lattner5426bf62008-04-07 07:01:58 +00003257//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003258// Integer Predicates
3259//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003260
Eli Friedmanad74a752008-06-28 06:23:08 +00003261unsigned ASTContext::getIntWidth(QualType T) {
3262 if (T == BoolTy)
3263 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003264 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3265 return FWIT->getWidth();
3266 }
3267 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003268 return (unsigned)getTypeSize(T);
3269}
3270
3271QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3272 assert(T->isSignedIntegerType() && "Unexpected type");
3273 if (const EnumType* ETy = T->getAsEnumType())
3274 T = ETy->getDecl()->getIntegerType();
3275 const BuiltinType* BTy = T->getAsBuiltinType();
3276 assert (BTy && "Unexpected signed integer type");
3277 switch (BTy->getKind()) {
3278 case BuiltinType::Char_S:
3279 case BuiltinType::SChar:
3280 return UnsignedCharTy;
3281 case BuiltinType::Short:
3282 return UnsignedShortTy;
3283 case BuiltinType::Int:
3284 return UnsignedIntTy;
3285 case BuiltinType::Long:
3286 return UnsignedLongTy;
3287 case BuiltinType::LongLong:
3288 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00003289 case BuiltinType::Int128:
3290 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00003291 default:
3292 assert(0 && "Unexpected signed integer type");
3293 return QualType();
3294 }
3295}
3296
Douglas Gregor2cf26342009-04-09 22:27:44 +00003297ExternalASTSource::~ExternalASTSource() { }
3298
3299void ExternalASTSource::PrintStats() { }