blob: 660b30a5e25872cc42293b87a40fb749d548f813 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000019#include "clang/AST/ExternalASTSource.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattnerc46fcdd2009-06-14 01:54:56 +000021#include "clang/Basic/Builtins.h"
Chris Lattnerb09b31d2009-03-28 03:45:20 +000022#include "clang/Basic/SourceManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000023#include "clang/Basic/TargetInfo.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000024#include "llvm/ADT/StringExtras.h"
Nate Begeman7903d052009-01-18 06:42:49 +000025#include "llvm/Support/MathExtras.h"
Chris Lattnerf4fbc442009-03-28 04:27:18 +000026#include "llvm/Support/MemoryBuffer.h"
Chris Lattner4b009652007-07-25 00:24:17 +000027using namespace clang;
28
29enum FloatingRank {
30 FloatRank, DoubleRank, LongDoubleRank
31};
32
Chris Lattner2fda0ed2008-10-05 17:34:18 +000033ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
34 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000035 IdentifierTable &idents, SelectorTable &sels,
Chris Lattnerc46fcdd2009-06-14 01:54:56 +000036 Builtin::Context &builtins,
37 bool FreeMem, unsigned size_reserve) :
Douglas Gregor1e589cc2009-03-26 23:50:42 +000038 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
39 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Douglas Gregorc34897d2009-04-09 22:27:44 +000040 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels),
Chris Lattnerc46fcdd2009-06-14 01:54:56 +000041 BuiltinInfo(builtins), ExternalSource(0) {
Daniel Dunbarde300732008-08-11 04:54:23 +000042 if (size_reserve > 0) Types.reserve(size_reserve);
43 InitBuiltinTypes();
Daniel Dunbarde300732008-08-11 04:54:23 +000044 TUDecl = TranslationUnitDecl::Create(*this);
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +000045 PrintingPolicy.CPlusPlus = LangOpts.CPlusPlus;
Daniel Dunbarde300732008-08-11 04:54:23 +000046}
47
Chris Lattner4b009652007-07-25 00:24:17 +000048ASTContext::~ASTContext() {
49 // Deallocate all the types.
50 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000051 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000052 Types.pop_back();
53 }
Eli Friedman65489b72008-05-27 03:08:09 +000054
Nuno Lopes355a8682008-12-17 22:30:25 +000055 {
56 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
57 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
58 while (I != E) {
59 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
60 delete R;
61 }
62 }
63
64 {
Daniel Dunbar1fbaef12009-05-03 10:38:35 +000065 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
66 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopes355a8682008-12-17 22:30:25 +000067 while (I != E) {
68 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
69 delete R;
70 }
71 }
72
Douglas Gregor1e589cc2009-03-26 23:50:42 +000073 // Destroy nested-name-specifiers.
Douglas Gregor3c4eae52009-03-27 23:54:10 +000074 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
75 NNS = NestedNameSpecifiers.begin(),
76 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregorbccd97c2009-03-27 23:25:45 +000077 NNS != NNSEnd;
Douglas Gregor3c4eae52009-03-27 23:54:10 +000078 /* Increment in loop */)
79 (*NNS++).Destroy(*this);
Douglas Gregor1e589cc2009-03-26 23:50:42 +000080
81 if (GlobalNestedNameSpecifier)
82 GlobalNestedNameSpecifier->Destroy(*this);
83
Eli Friedman65489b72008-05-27 03:08:09 +000084 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000085}
86
Douglas Gregorc34897d2009-04-09 22:27:44 +000087void
88ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
89 ExternalSource.reset(Source.take());
90}
91
Chris Lattner4b009652007-07-25 00:24:17 +000092void ASTContext::PrintStats() const {
93 fprintf(stderr, "*** AST Context Stats:\n");
94 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redlce6fff02009-03-16 23:22:08 +000095
Douglas Gregore6609442009-05-26 14:40:08 +000096 unsigned counts[] = {
97#define TYPE(Name, Parent) 0,
98#define ABSTRACT_TYPE(Name, Parent)
99#include "clang/AST/TypeNodes.def"
100 0 // Extra
101 };
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000102
Chris Lattner4b009652007-07-25 00:24:17 +0000103 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
104 Type *T = Types[i];
Douglas Gregore6609442009-05-26 14:40:08 +0000105 counts[(unsigned)T->getTypeClass()]++;
Chris Lattner4b009652007-07-25 00:24:17 +0000106 }
107
Douglas Gregore6609442009-05-26 14:40:08 +0000108 unsigned Idx = 0;
109 unsigned TotalBytes = 0;
110#define TYPE(Name, Parent) \
111 if (counts[Idx]) \
112 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
113 TotalBytes += counts[Idx] * sizeof(Name##Type); \
114 ++Idx;
115#define ABSTRACT_TYPE(Name, Parent)
116#include "clang/AST/TypeNodes.def"
117
118 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregorc34897d2009-04-09 22:27:44 +0000119
120 if (ExternalSource.get()) {
121 fprintf(stderr, "\n");
122 ExternalSource->PrintStats();
123 }
Chris Lattner4b009652007-07-25 00:24:17 +0000124}
125
126
127void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroff93fd2112009-01-27 22:08:43 +0000128 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000129}
130
Chris Lattner4b009652007-07-25 00:24:17 +0000131void ASTContext::InitBuiltinTypes() {
132 assert(VoidTy.isNull() && "Context reinitialized?");
133
134 // C99 6.2.5p19.
135 InitBuiltinType(VoidTy, BuiltinType::Void);
136
137 // C99 6.2.5p2.
138 InitBuiltinType(BoolTy, BuiltinType::Bool);
139 // C99 6.2.5p3.
Eli Friedmand9389be2009-06-05 07:05:05 +0000140 if (LangOpts.CharIsSigned)
Chris Lattner4b009652007-07-25 00:24:17 +0000141 InitBuiltinType(CharTy, BuiltinType::Char_S);
142 else
143 InitBuiltinType(CharTy, BuiltinType::Char_U);
144 // C99 6.2.5p4.
145 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
146 InitBuiltinType(ShortTy, BuiltinType::Short);
147 InitBuiltinType(IntTy, BuiltinType::Int);
148 InitBuiltinType(LongTy, BuiltinType::Long);
149 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
150
151 // C99 6.2.5p6.
152 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
153 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
154 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
155 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
156 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
157
158 // C99 6.2.5p10.
159 InitBuiltinType(FloatTy, BuiltinType::Float);
160 InitBuiltinType(DoubleTy, BuiltinType::Double);
161 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000162
Chris Lattner6cc7e412009-04-30 02:43:43 +0000163 // GNU extension, 128-bit integers.
164 InitBuiltinType(Int128Ty, BuiltinType::Int128);
165 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
166
Chris Lattnere1dafe72009-02-26 23:43:47 +0000167 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
168 InitBuiltinType(WCharTy, BuiltinType::WChar);
169 else // C99
170 WCharTy = getFromTargetType(Target.getWCharType());
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000171
Douglas Gregord2baafd2008-10-21 16:13:35 +0000172 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000173 InitBuiltinType(OverloadTy, BuiltinType::Overload);
174
175 // Placeholder type for type-dependent expressions whose type is
176 // completely unknown. No code should ever check a type against
177 // DependentTy and users should never see it; however, it is here to
178 // help diagnose failures to properly check for type-dependent
179 // expressions.
180 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000181
Chris Lattner4b009652007-07-25 00:24:17 +0000182 // C99 6.2.5p11.
183 FloatComplexTy = getComplexType(FloatTy);
184 DoubleComplexTy = getComplexType(DoubleTy);
185 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000186
Steve Naroff9d12c902007-10-15 14:41:52 +0000187 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000188 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000189 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000190 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000191 ClassStructType = 0;
192
Ted Kremenek42730c52008-01-07 19:49:32 +0000193 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000194
195 // void * type
196 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl5d0ead72009-05-10 18:38:11 +0000197
198 // nullptr type (C++0x 2.14.7)
199 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Chris Lattner4b009652007-07-25 00:24:17 +0000200}
201
202//===----------------------------------------------------------------------===//
203// Type Sizing and Analysis
204//===----------------------------------------------------------------------===//
205
Chris Lattner2a674dc2008-06-30 18:32:54 +0000206/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
207/// scalar floating point type.
208const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
209 const BuiltinType *BT = T->getAsBuiltinType();
210 assert(BT && "Not a floating point type!");
211 switch (BT->getKind()) {
212 default: assert(0 && "Not a floating point type!");
213 case BuiltinType::Float: return Target.getFloatFormat();
214 case BuiltinType::Double: return Target.getDoubleFormat();
215 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
216 }
217}
218
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000219/// getDeclAlign - Return a conservative estimate of the alignment of the
220/// specified decl. Note that bitfields do not have a valid alignment, so
221/// this method will assert on them.
Daniel Dunbar96d1f1b2009-02-17 22:16:19 +0000222unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedman0ee57322009-02-22 02:56:25 +0000223 unsigned Align = Target.getCharWidth();
224
225 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
226 Align = std::max(Align, AA->getAlignment());
227
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000228 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
229 QualType T = VD->getType();
Anders Carlssonaa0783b2009-04-10 04:47:03 +0000230 if (const ReferenceType* RT = T->getAsReferenceType()) {
231 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssoneeaeda32009-04-10 04:52:36 +0000232 Align = Target.getPointerAlign(AS);
Anders Carlssonaa0783b2009-04-10 04:47:03 +0000233 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
234 // Incomplete or function types default to 1.
Eli Friedman0ee57322009-02-22 02:56:25 +0000235 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
236 T = cast<ArrayType>(T)->getElementType();
237
238 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
239 }
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000240 }
Eli Friedman0ee57322009-02-22 02:56:25 +0000241
242 return Align / Target.getCharWidth();
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000243}
Chris Lattner2a674dc2008-06-30 18:32:54 +0000244
Chris Lattner4b009652007-07-25 00:24:17 +0000245/// getTypeSize - Return the size of the specified type, in bits. This method
246/// does not work on incomplete types.
247std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000248ASTContext::getTypeInfo(const Type *T) {
Mike Stump44d1f402009-02-27 18:32:39 +0000249 uint64_t Width=0;
250 unsigned Align=8;
Chris Lattner4b009652007-07-25 00:24:17 +0000251 switch (T->getTypeClass()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000252#define TYPE(Class, Base)
253#define ABSTRACT_TYPE(Class, Base)
Douglas Gregorab380272009-04-30 17:32:17 +0000254#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor4fa58902009-02-26 23:50:07 +0000255#define DEPENDENT_TYPE(Class, Base) case Type::Class:
256#include "clang/AST/TypeNodes.def"
Douglas Gregorab380272009-04-30 17:32:17 +0000257 assert(false && "Should not see dependent types");
Douglas Gregor4fa58902009-02-26 23:50:07 +0000258 break;
259
Chris Lattner4b009652007-07-25 00:24:17 +0000260 case Type::FunctionNoProto:
261 case Type::FunctionProto:
Douglas Gregorab380272009-04-30 17:32:17 +0000262 // GCC extension: alignof(function) = 32 bits
263 Width = 0;
264 Align = 32;
265 break;
266
Douglas Gregor4fa58902009-02-26 23:50:07 +0000267 case Type::IncompleteArray:
Steve Naroff83c13012007-08-30 01:06:46 +0000268 case Type::VariableArray:
Douglas Gregorab380272009-04-30 17:32:17 +0000269 Width = 0;
270 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
271 break;
272
Steve Naroff83c13012007-08-30 01:06:46 +0000273 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000274 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000275
Chris Lattner8cd0e932008-03-05 18:54:05 +0000276 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000277 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000278 Align = EltInfo.second;
279 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000280 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000281 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000282 case Type::Vector: {
283 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000284 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000285 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000286 Align = Width;
Nate Begeman7903d052009-01-18 06:42:49 +0000287 // If the alignment is not a power of 2, round up to the next power of 2.
288 // This happens for non-power-of-2 length vectors.
289 // FIXME: this should probably be a target property.
290 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000291 break;
292 }
293
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000294 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000295 switch (cast<BuiltinType>(T)->getKind()) {
296 default: assert(0 && "Unknown builtin type!");
297 case BuiltinType::Void:
Douglas Gregorab380272009-04-30 17:32:17 +0000298 // GCC extension: alignof(void) = 8 bits.
299 Width = 0;
300 Align = 8;
301 break;
302
Chris Lattnerb66237b2007-12-19 19:23:28 +0000303 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000304 Width = Target.getBoolWidth();
305 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000306 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000307 case BuiltinType::Char_S:
308 case BuiltinType::Char_U:
309 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000310 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000311 Width = Target.getCharWidth();
312 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000313 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000314 case BuiltinType::WChar:
315 Width = Target.getWCharWidth();
316 Align = Target.getWCharAlign();
317 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000318 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000319 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000320 Width = Target.getShortWidth();
321 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000322 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000323 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000324 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000325 Width = Target.getIntWidth();
326 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000327 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000328 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000329 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000330 Width = Target.getLongWidth();
331 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000332 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000333 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000334 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000335 Width = Target.getLongLongWidth();
336 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000337 break;
Chris Lattner4b11cc22009-04-30 02:55:13 +0000338 case BuiltinType::Int128:
339 case BuiltinType::UInt128:
340 Width = 128;
341 Align = 128; // int128_t is 128-bit aligned on all targets.
342 break;
Chris Lattnerb66237b2007-12-19 19:23:28 +0000343 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000344 Width = Target.getFloatWidth();
345 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000346 break;
347 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000348 Width = Target.getDoubleWidth();
349 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000350 break;
351 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000352 Width = Target.getLongDoubleWidth();
353 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000354 break;
Sebastian Redl5d0ead72009-05-10 18:38:11 +0000355 case BuiltinType::NullPtr:
356 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
357 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redlc4cce782009-05-27 19:34:06 +0000358 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000359 }
360 break;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000361 case Type::FixedWidthInt:
362 // FIXME: This isn't precisely correct; the width/alignment should depend
363 // on the available types for the target
364 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattnere9174982009-02-15 21:20:13 +0000365 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000366 Align = Width;
367 break;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000368 case Type::ExtQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000369 // FIXME: Pointers into different addr spaces could have different sizes and
370 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000371 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000372 case Type::ObjCQualifiedId:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000373 case Type::ObjCQualifiedInterface:
Chris Lattner1d78a862008-04-07 07:01:58 +0000374 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000375 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000376 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000377 case Type::BlockPointer: {
378 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
379 Width = Target.getPointerWidth(AS);
380 Align = Target.getPointerAlign(AS);
381 break;
382 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000383 case Type::Pointer: {
384 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000385 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000386 Align = Target.getPointerAlign(AS);
387 break;
388 }
Sebastian Redlce6fff02009-03-16 23:22:08 +0000389 case Type::LValueReference:
390 case Type::RValueReference:
Chris Lattner4b009652007-07-25 00:24:17 +0000391 // "When applied to a reference or a reference type, the result is the size
392 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000393 // FIXME: This is wrong for struct layout: a reference in a struct has
394 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000395 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl75555032009-01-24 21:16:55 +0000396 case Type::MemberPointer: {
Anders Carlsson86cf4ac2009-05-17 02:06:04 +0000397 // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
398 // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
399 // If we ever want to support other ABIs this needs to be abstracted.
400
Sebastian Redl75555032009-01-24 21:16:55 +0000401 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Anders Carlsson86cf4ac2009-05-17 02:06:04 +0000402 std::pair<uint64_t, unsigned> PtrDiffInfo =
403 getTypeInfo(getPointerDiffType());
404 Width = PtrDiffInfo.first;
Sebastian Redl75555032009-01-24 21:16:55 +0000405 if (Pointee->isFunctionType())
406 Width *= 2;
Anders Carlsson86cf4ac2009-05-17 02:06:04 +0000407 Align = PtrDiffInfo.second;
408 break;
Sebastian Redl75555032009-01-24 21:16:55 +0000409 }
Chris Lattner4b009652007-07-25 00:24:17 +0000410 case Type::Complex: {
411 // Complex types have the same alignment as their elements, but twice the
412 // size.
413 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000414 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000415 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000416 Align = EltInfo.second;
417 break;
418 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000419 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000420 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000421 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
422 Width = Layout.getSize();
423 Align = Layout.getAlignment();
424 break;
425 }
Douglas Gregor4fa58902009-02-26 23:50:07 +0000426 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000427 case Type::Enum: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000428 const TagType *TT = cast<TagType>(T);
429
430 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000431 Width = 1;
432 Align = 1;
433 break;
434 }
435
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000436 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000437 return getTypeInfo(ET->getDecl()->getIntegerType());
438
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000439 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000440 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
441 Width = Layout.getSize();
442 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000443 break;
444 }
Douglas Gregordd13e842009-03-30 22:58:21 +0000445
Douglas Gregorab380272009-04-30 17:32:17 +0000446 case Type::Typedef: {
447 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
448 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
449 Align = Aligned->getAlignment();
450 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
451 } else
452 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregordd13e842009-03-30 22:58:21 +0000453 break;
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000454 }
Douglas Gregorab380272009-04-30 17:32:17 +0000455
456 case Type::TypeOfExpr:
457 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
458 .getTypePtr());
459
460 case Type::TypeOf:
461 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
462
463 case Type::QualifiedName:
464 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
465
466 case Type::TemplateSpecialization:
467 assert(getCanonicalType(T) != T &&
468 "Cannot request the size of a dependent type");
469 // FIXME: this is likely to be wrong once we support template
470 // aliases, since a template alias could refer to a typedef that
471 // has an __aligned__ attribute on it.
472 return getTypeInfo(getCanonicalType(T));
473 }
Chris Lattner4b009652007-07-25 00:24:17 +0000474
475 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000476 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000477}
478
Chris Lattner83165b52009-01-27 18:08:34 +0000479/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
480/// type for the current target in bits. This can be different than the ABI
481/// alignment in cases where it is beneficial for performance to overalign
482/// a data type.
483unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
484 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman66c9edf2009-05-25 21:27:19 +0000485
486 // Double and long long should be naturally aligned if possible.
487 if (const ComplexType* CT = T->getAsComplexType())
488 T = CT->getElementType().getTypePtr();
489 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
490 T->isSpecificBuiltinType(BuiltinType::LongLong))
491 return std::max(ABIAlign, (unsigned)getTypeSize(T));
492
Chris Lattner83165b52009-01-27 18:08:34 +0000493 return ABIAlign;
494}
495
496
Devang Patelbfe323c2008-06-04 21:22:16 +0000497/// LayoutField - Field layout.
498void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000499 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000500 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000501 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000502 uint64_t FieldOffset = IsUnion ? 0 : Size;
503 uint64_t FieldSize;
504 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000505
506 // FIXME: Should this override struct packing? Probably we want to
507 // take the minimum?
508 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
509 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000510
511 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
512 // TODO: Need to check this algorithm on other targets!
513 // (tested on Linux-X86)
Eli Friedman5255e7a2009-04-26 19:19:15 +0000514 FieldSize = BitWidthExpr->EvaluateAsInt(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000515
516 std::pair<uint64_t, unsigned> FieldInfo =
517 Context.getTypeInfo(FD->getType());
518 uint64_t TypeSize = FieldInfo.first;
519
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000520 // Determine the alignment of this bitfield. The packing
521 // attributes define a maximum and the alignment attribute defines
522 // a minimum.
523 // FIXME: What is the right behavior when the specified alignment
524 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000525 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000526 if (FieldPacking)
527 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000528 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
529 FieldAlign = std::max(FieldAlign, AA->getAlignment());
530
531 // Check if we need to add padding to give the field the correct
532 // alignment.
533 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
534 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
535
536 // Padding members don't affect overall alignment
537 if (!FD->getIdentifier())
538 FieldAlign = 1;
539 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000540 if (FD->getType()->isIncompleteArrayType()) {
541 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000542 // query getTypeInfo about these, so we figure it out here.
543 // Flexible array members don't have any size, but they
544 // have to be aligned appropriately for their element type.
545 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000546 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000547 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson0843ea52009-04-10 05:31:15 +0000548 } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
549 unsigned AS = RT->getPointeeType().getAddressSpace();
550 FieldSize = Context.Target.getPointerWidth(AS);
551 FieldAlign = Context.Target.getPointerAlign(AS);
Devang Patelbfe323c2008-06-04 21:22:16 +0000552 } else {
553 std::pair<uint64_t, unsigned> FieldInfo =
554 Context.getTypeInfo(FD->getType());
555 FieldSize = FieldInfo.first;
556 FieldAlign = FieldInfo.second;
557 }
558
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000559 // Determine the alignment of this bitfield. The packing
560 // attributes define a maximum and the alignment attribute defines
561 // a minimum. Additionally, the packing alignment must be at least
562 // a byte for non-bitfields.
563 //
564 // FIXME: What is the right behavior when the specified alignment
565 // is smaller than the specified packing?
566 if (FieldPacking)
567 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000568 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
569 FieldAlign = std::max(FieldAlign, AA->getAlignment());
570
571 // Round up the current record size to the field's alignment boundary.
572 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
573 }
574
575 // Place this field at the current location.
576 FieldOffsets[FieldNo] = FieldOffset;
577
578 // Reserve space for this field.
579 if (IsUnion) {
580 Size = std::max(Size, FieldSize);
581 } else {
582 Size = FieldOffset + FieldSize;
583 }
584
Daniel Dunbar5523e862009-05-04 05:16:21 +0000585 // Remember the next available offset.
586 NextOffset = Size;
587
Devang Patelbfe323c2008-06-04 21:22:16 +0000588 // Remember max struct/class alignment.
589 Alignment = std::max(Alignment, FieldAlign);
590}
591
Daniel Dunbar1af336e2009-04-22 17:43:55 +0000592static void CollectLocalObjCIvars(ASTContext *Ctx,
593 const ObjCInterfaceDecl *OI,
594 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000595 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
596 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner9329cf52009-03-31 08:48:01 +0000597 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000598 if (!IVDecl->isInvalidDecl())
599 Fields.push_back(cast<FieldDecl>(IVDecl));
600 }
601}
602
Daniel Dunbar1af336e2009-04-22 17:43:55 +0000603void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
604 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
605 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
606 CollectObjCIvars(SuperClass, Fields);
607 CollectLocalObjCIvars(this, OI, Fields);
608}
609
Fariborz Jahanianb290be02009-06-04 01:19:09 +0000610/// ShallowCollectObjCIvars -
611/// Collect all ivars, including those synthesized, in the current class.
612///
613void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
614 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars,
615 bool CollectSynthesized) {
616 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
617 E = OI->ivar_end(); I != E; ++I) {
618 Ivars.push_back(*I);
619 }
620 if (CollectSynthesized)
621 CollectSynthesizedIvars(OI, Ivars);
622}
623
Fariborz Jahanian02ebfa82009-05-12 18:14:29 +0000624void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
625 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
626 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(*this),
627 E = PD->prop_end(*this); I != E; ++I)
628 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
629 Ivars.push_back(Ivar);
630
631 // Also look into nested protocols.
632 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
633 E = PD->protocol_end(); P != E; ++P)
634 CollectProtocolSynthesizedIvars(*P, Ivars);
635}
636
637/// CollectSynthesizedIvars -
638/// This routine collect synthesized ivars for the designated class.
639///
640void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
641 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
642 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*this),
643 E = OI->prop_end(*this); I != E; ++I) {
644 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
645 Ivars.push_back(Ivar);
646 }
647 // Also look into interface's protocol list for properties declared
648 // in the protocol and whose ivars are synthesized.
649 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
650 PE = OI->protocol_end(); P != PE; ++P) {
651 ObjCProtocolDecl *PD = (*P);
652 CollectProtocolSynthesizedIvars(PD, Ivars);
653 }
654}
655
Fariborz Jahanianb290be02009-06-04 01:19:09 +0000656unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) {
657 unsigned count = 0;
658 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(*this),
659 E = PD->prop_end(*this); I != E; ++I)
660 if ((*I)->getPropertyIvarDecl())
661 ++count;
662
663 // Also look into nested protocols.
664 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
665 E = PD->protocol_end(); P != E; ++P)
666 count += CountProtocolSynthesizedIvars(*P);
667 return count;
668}
669
670unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI)
671{
672 unsigned count = 0;
673 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*this),
674 E = OI->prop_end(*this); I != E; ++I) {
675 if ((*I)->getPropertyIvarDecl())
676 ++count;
677 }
678 // Also look into interface's protocol list for properties declared
679 // in the protocol and whose ivars are synthesized.
680 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
681 PE = OI->protocol_end(); P != PE; ++P) {
682 ObjCProtocolDecl *PD = (*P);
683 count += CountProtocolSynthesizedIvars(PD);
684 }
685 return count;
686}
687
Daniel Dunbar1fbaef12009-05-03 10:38:35 +0000688/// getInterfaceLayoutImpl - Get or compute information about the
689/// layout of the given interface.
690///
691/// \param Impl - If given, also include the layout of the interface's
692/// implementation. This may differ by including synthesized ivars.
Devang Patel4b6bf702008-06-04 21:54:36 +0000693const ASTRecordLayout &
Daniel Dunbar1fbaef12009-05-03 10:38:35 +0000694ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
695 const ObjCImplementationDecl *Impl) {
Daniel Dunbar94d2ede2009-05-03 13:15:50 +0000696 assert(!D->isForwardDecl() && "Invalid interface decl!");
697
Devang Patel4b6bf702008-06-04 21:54:36 +0000698 // Look up this layout, if already laid out, return what we have.
Daniel Dunbarb3170af2009-05-03 11:41:43 +0000699 ObjCContainerDecl *Key =
700 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
701 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
702 return *Entry;
Devang Patel4b6bf702008-06-04 21:54:36 +0000703
Daniel Dunbar5b9332f2009-05-03 11:16:44 +0000704 unsigned FieldCount = D->ivar_size();
705 // Add in synthesized ivar count if laying out an implementation.
706 if (Impl) {
Fariborz Jahanianb290be02009-06-04 01:19:09 +0000707 unsigned SynthCount = CountSynthesizedIvars(D);
708 FieldCount += SynthCount;
Daniel Dunbarb3170af2009-05-03 11:41:43 +0000709 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar5b9332f2009-05-03 11:16:44 +0000710 // entry. Note we can't cache this because we simply free all
711 // entries later; however we shouldn't look up implementations
712 // frequently.
Fariborz Jahanianb290be02009-06-04 01:19:09 +0000713 if (SynthCount == 0)
Daniel Dunbar5b9332f2009-05-03 11:16:44 +0000714 return getObjCLayout(D, 0);
715 }
716
Devang Patel8682d882008-06-06 02:14:01 +0000717 ASTRecordLayout *NewEntry = NULL;
Devang Patel8682d882008-06-06 02:14:01 +0000718 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Devang Patel8682d882008-06-06 02:14:01 +0000719 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
720 unsigned Alignment = SL.getAlignment();
Daniel Dunbarb3170af2009-05-03 11:41:43 +0000721
Daniel Dunbarb5dc2942009-05-07 21:58:26 +0000722 // We start laying out ivars not at the end of the superclass
723 // structure, but at the next byte following the last field.
724 uint64_t Size = llvm::RoundUpToAlignment(SL.NextOffset, 8);
Daniel Dunbar5523e862009-05-04 05:16:21 +0000725
Daniel Dunbarb3170af2009-05-03 11:41:43 +0000726 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout(Size, Alignment);
Devang Patel8682d882008-06-06 02:14:01 +0000727 NewEntry->InitializeLayout(FieldCount);
Devang Patel8682d882008-06-06 02:14:01 +0000728 } else {
Daniel Dunbarb3170af2009-05-03 11:41:43 +0000729 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout();
Devang Patel8682d882008-06-06 02:14:01 +0000730 NewEntry->InitializeLayout(FieldCount);
731 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000732
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000733 unsigned StructPacking = 0;
734 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
735 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000736
737 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
738 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
739 AA->getAlignment()));
740
741 // Layout each ivar sequentially.
742 unsigned i = 0;
Fariborz Jahanianb290be02009-06-04 01:19:09 +0000743 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
744 ShallowCollectObjCIvars(D, Ivars, Impl);
745 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
746 NewEntry->LayoutField(Ivars[k], i++, false, StructPacking, *this);
747
Devang Patel4b6bf702008-06-04 21:54:36 +0000748 // Finally, round the size of the total struct up to the alignment of the
749 // struct itself.
750 NewEntry->FinalizeLayout();
751 return *NewEntry;
752}
753
Daniel Dunbar1fbaef12009-05-03 10:38:35 +0000754const ASTRecordLayout &
755ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
756 return getObjCLayout(D, 0);
757}
758
759const ASTRecordLayout &
760ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
761 return getObjCLayout(D->getClassInterface(), D);
762}
763
Devang Patel7a78e432007-11-01 19:11:01 +0000764/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000765/// specified record (struct/union/class), which indicates its size and field
766/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000767const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000768 D = D->getDefinition(*this);
769 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000770
Chris Lattner4b009652007-07-25 00:24:17 +0000771 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000772 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000773 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000774
Devang Patel7a78e432007-11-01 19:11:01 +0000775 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
776 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
777 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000778 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000779
Douglas Gregor39677622008-12-11 20:41:00 +0000780 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000781 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
782 D->field_end(*this)));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000783 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000784
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000785 unsigned StructPacking = 0;
786 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
787 StructPacking = PA->getAlignment();
788
Eli Friedman5949a022008-05-30 09:31:38 +0000789 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000790 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
791 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000792
Eli Friedman5949a022008-05-30 09:31:38 +0000793 // Layout each field, for now, just sequentially, respecting alignment. In
794 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000795 unsigned FieldIdx = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000796 for (RecordDecl::field_iterator Field = D->field_begin(*this),
797 FieldEnd = D->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000798 Field != FieldEnd; (void)++Field, ++FieldIdx)
799 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000800
801 // Finally, round the size of the total struct up to the alignment of the
802 // struct itself.
Sebastian Redlc4cce782009-05-27 19:34:06 +0000803 NewEntry->FinalizeLayout(getLangOptions().CPlusPlus);
Chris Lattner4b009652007-07-25 00:24:17 +0000804 return *NewEntry;
805}
806
Chris Lattner4b009652007-07-25 00:24:17 +0000807//===----------------------------------------------------------------------===//
808// Type creation/memoization methods
809//===----------------------------------------------------------------------===//
810
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000811QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000812 QualType CanT = getCanonicalType(T);
813 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000814 return T;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000815
816 // If we are composing extended qualifiers together, merge together into one
817 // ExtQualType node.
818 unsigned CVRQuals = T.getCVRQualifiers();
819 QualType::GCAttrTypes GCAttr = QualType::GCNone;
820 Type *TypeNode = T.getTypePtr();
Chris Lattner35fef522008-02-20 20:55:12 +0000821
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000822 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
823 // If this type already has an address space specified, it cannot get
824 // another one.
825 assert(EQT->getAddressSpace() == 0 &&
826 "Type cannot be in multiple addr spaces!");
827 GCAttr = EQT->getObjCGCAttr();
828 TypeNode = EQT->getBaseType();
829 }
Chris Lattner35fef522008-02-20 20:55:12 +0000830
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000831 // Check if we've already instantiated this type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000832 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000833 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000834 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000835 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000836 return QualType(EXTQy, CVRQuals);
837
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000838 // If the base type isn't canonical, this won't be a canonical type either,
839 // so fill in the canonical type field.
840 QualType Canonical;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000841 if (!TypeNode->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000842 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000843
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000844 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000845 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000846 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000847 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000848 ExtQualType *New =
849 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000850 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000851 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000852 return QualType(New, CVRQuals);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000853}
854
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000855QualType ASTContext::getObjCGCQualType(QualType T,
856 QualType::GCAttrTypes GCAttr) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000857 QualType CanT = getCanonicalType(T);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000858 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000859 return T;
860
Fariborz Jahanian143b0082009-06-03 17:15:17 +0000861 if (T->isPointerType()) {
862 QualType Pointee = T->getAsPointerType()->getPointeeType();
863 if (Pointee->isPointerType()) {
864 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
865 return getPointerType(ResultType);
866 }
867 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000868 // If we are composing extended qualifiers together, merge together into one
869 // ExtQualType node.
870 unsigned CVRQuals = T.getCVRQualifiers();
871 Type *TypeNode = T.getTypePtr();
872 unsigned AddressSpace = 0;
873
874 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
875 // If this type already has an address space specified, it cannot get
876 // another one.
877 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
878 "Type cannot be in multiple addr spaces!");
879 AddressSpace = EQT->getAddressSpace();
880 TypeNode = EQT->getBaseType();
881 }
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000882
883 // Check if we've already instantiated an gc qual'd type of this type.
884 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000885 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000886 void *InsertPos = 0;
887 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000888 return QualType(EXTQy, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000889
890 // If the base type isn't canonical, this won't be a canonical type either,
891 // so fill in the canonical type field.
Eli Friedman94fcc9a2009-02-27 23:04:43 +0000892 // FIXME: Isn't this also not canonical if the base type is a array
893 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000894 QualType Canonical;
895 if (!T->isCanonical()) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000896 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000897
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000898 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000899 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
900 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
901 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000902 ExtQualType *New =
903 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000904 ExtQualTypes.InsertNode(New, InsertPos);
905 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000906 return QualType(New, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000907}
Chris Lattner4b009652007-07-25 00:24:17 +0000908
909/// getComplexType - Return the uniqued reference to the type for a complex
910/// number with the specified element type.
911QualType ASTContext::getComplexType(QualType T) {
912 // Unique pointers, to guarantee there is only one pointer of a particular
913 // structure.
914 llvm::FoldingSetNodeID ID;
915 ComplexType::Profile(ID, T);
916
917 void *InsertPos = 0;
918 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
919 return QualType(CT, 0);
920
921 // If the pointee type isn't canonical, this won't be a canonical type either,
922 // so fill in the canonical type field.
923 QualType Canonical;
924 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000925 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000926
927 // Get the new insert position for the node we care about.
928 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000929 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000930 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000931 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000932 Types.push_back(New);
933 ComplexTypes.InsertNode(New, InsertPos);
934 return QualType(New, 0);
935}
936
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000937QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
938 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
939 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
940 FixedWidthIntType *&Entry = Map[Width];
941 if (!Entry)
942 Entry = new FixedWidthIntType(Width, Signed);
943 return QualType(Entry, 0);
944}
Chris Lattner4b009652007-07-25 00:24:17 +0000945
946/// getPointerType - Return the uniqued reference to the type for a pointer to
947/// the specified type.
948QualType ASTContext::getPointerType(QualType T) {
949 // Unique pointers, to guarantee there is only one pointer of a particular
950 // structure.
951 llvm::FoldingSetNodeID ID;
952 PointerType::Profile(ID, T);
953
954 void *InsertPos = 0;
955 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
956 return QualType(PT, 0);
957
958 // If the pointee type isn't canonical, this won't be a canonical type either,
959 // so fill in the canonical type field.
960 QualType Canonical;
961 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000962 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000963
964 // Get the new insert position for the node we care about.
965 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000966 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000967 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000968 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000969 Types.push_back(New);
970 PointerTypes.InsertNode(New, InsertPos);
971 return QualType(New, 0);
972}
973
Steve Naroff7aa54752008-08-27 16:04:49 +0000974/// getBlockPointerType - Return the uniqued reference to the type for
975/// a pointer to the specified block.
976QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000977 assert(T->isFunctionType() && "block of function types only");
978 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000979 // structure.
980 llvm::FoldingSetNodeID ID;
981 BlockPointerType::Profile(ID, T);
982
983 void *InsertPos = 0;
984 if (BlockPointerType *PT =
985 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
986 return QualType(PT, 0);
987
Steve Narofffd5b19d2008-08-28 19:20:44 +0000988 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000989 // type either so fill in the canonical type field.
990 QualType Canonical;
991 if (!T->isCanonical()) {
992 Canonical = getBlockPointerType(getCanonicalType(T));
993
994 // Get the new insert position for the node we care about.
995 BlockPointerType *NewIP =
996 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000997 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000998 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000999 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +00001000 Types.push_back(New);
1001 BlockPointerTypes.InsertNode(New, InsertPos);
1002 return QualType(New, 0);
1003}
1004
Sebastian Redlce6fff02009-03-16 23:22:08 +00001005/// getLValueReferenceType - Return the uniqued reference to the type for an
1006/// lvalue reference to the specified type.
1007QualType ASTContext::getLValueReferenceType(QualType T) {
Chris Lattner4b009652007-07-25 00:24:17 +00001008 // Unique pointers, to guarantee there is only one pointer of a particular
1009 // structure.
1010 llvm::FoldingSetNodeID ID;
1011 ReferenceType::Profile(ID, T);
1012
1013 void *InsertPos = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +00001014 if (LValueReferenceType *RT =
1015 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001016 return QualType(RT, 0);
Sebastian Redlce6fff02009-03-16 23:22:08 +00001017
Chris Lattner4b009652007-07-25 00:24:17 +00001018 // If the referencee type isn't canonical, this won't be a canonical type
1019 // either, so fill in the canonical type field.
1020 QualType Canonical;
1021 if (!T->isCanonical()) {
Sebastian Redlce6fff02009-03-16 23:22:08 +00001022 Canonical = getLValueReferenceType(getCanonicalType(T));
1023
Chris Lattner4b009652007-07-25 00:24:17 +00001024 // Get the new insert position for the node we care about.
Sebastian Redlce6fff02009-03-16 23:22:08 +00001025 LValueReferenceType *NewIP =
1026 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001027 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001028 }
1029
Sebastian Redlce6fff02009-03-16 23:22:08 +00001030 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001031 Types.push_back(New);
Sebastian Redlce6fff02009-03-16 23:22:08 +00001032 LValueReferenceTypes.InsertNode(New, InsertPos);
1033 return QualType(New, 0);
1034}
1035
1036/// getRValueReferenceType - Return the uniqued reference to the type for an
1037/// rvalue reference to the specified type.
1038QualType ASTContext::getRValueReferenceType(QualType T) {
1039 // Unique pointers, to guarantee there is only one pointer of a particular
1040 // structure.
1041 llvm::FoldingSetNodeID ID;
1042 ReferenceType::Profile(ID, T);
1043
1044 void *InsertPos = 0;
1045 if (RValueReferenceType *RT =
1046 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1047 return QualType(RT, 0);
1048
1049 // If the referencee type isn't canonical, this won't be a canonical type
1050 // either, so fill in the canonical type field.
1051 QualType Canonical;
1052 if (!T->isCanonical()) {
1053 Canonical = getRValueReferenceType(getCanonicalType(T));
1054
1055 // Get the new insert position for the node we care about.
1056 RValueReferenceType *NewIP =
1057 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1058 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1059 }
1060
1061 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1062 Types.push_back(New);
1063 RValueReferenceTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001064 return QualType(New, 0);
1065}
1066
Sebastian Redl75555032009-01-24 21:16:55 +00001067/// getMemberPointerType - Return the uniqued reference to the type for a
1068/// member pointer to the specified type, in the specified class.
1069QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1070{
1071 // Unique pointers, to guarantee there is only one pointer of a particular
1072 // structure.
1073 llvm::FoldingSetNodeID ID;
1074 MemberPointerType::Profile(ID, T, Cls);
1075
1076 void *InsertPos = 0;
1077 if (MemberPointerType *PT =
1078 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1079 return QualType(PT, 0);
1080
1081 // If the pointee or class type isn't canonical, this won't be a canonical
1082 // type either, so fill in the canonical type field.
1083 QualType Canonical;
1084 if (!T->isCanonical()) {
1085 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1086
1087 // Get the new insert position for the node we care about.
1088 MemberPointerType *NewIP =
1089 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1090 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1091 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001092 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +00001093 Types.push_back(New);
1094 MemberPointerTypes.InsertNode(New, InsertPos);
1095 return QualType(New, 0);
1096}
1097
Steve Naroff83c13012007-08-30 01:06:46 +00001098/// getConstantArrayType - Return the unique reference to the type for an
1099/// array of the specified element type.
1100QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner08bea472009-05-13 04:12:56 +00001101 const llvm::APInt &ArySizeIn,
Steve Naroff24c9b982007-08-30 18:10:14 +00001102 ArrayType::ArraySizeModifier ASM,
1103 unsigned EltTypeQuals) {
Eli Friedmanb4c71b32009-05-29 20:17:55 +00001104 assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) &&
1105 "Constant array of VLAs is illegal!");
1106
Chris Lattner08bea472009-05-13 04:12:56 +00001107 // Convert the array size into a canonical width matching the pointer size for
1108 // the target.
1109 llvm::APInt ArySize(ArySizeIn);
1110 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1111
Chris Lattner4b009652007-07-25 00:24:17 +00001112 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001113 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001114
1115 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +00001116 if (ConstantArrayType *ATP =
1117 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001118 return QualType(ATP, 0);
1119
1120 // If the element type isn't canonical, this won't be a canonical type either,
1121 // so fill in the canonical type field.
1122 QualType Canonical;
1123 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001124 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +00001125 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001126 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +00001127 ConstantArrayType *NewIP =
1128 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001129 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001130 }
1131
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001132 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001133 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001134 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001135 Types.push_back(New);
1136 return QualType(New, 0);
1137}
1138
Steve Naroffe2579e32007-08-30 18:14:25 +00001139/// getVariableArrayType - Returns a non-unique reference to the type for a
1140/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +00001141QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1142 ArrayType::ArraySizeModifier ASM,
1143 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +00001144 // Since we don't unique expressions, it isn't possible to unique VLA's
1145 // that have an expression provided for their size.
1146
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001147 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001148 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001149
1150 VariableArrayTypes.push_back(New);
1151 Types.push_back(New);
1152 return QualType(New, 0);
1153}
1154
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001155/// getDependentSizedArrayType - Returns a non-unique reference to
1156/// the type for a dependently-sized array of the specified element
1157/// type. FIXME: We will need these to be uniqued, or at least
1158/// comparable, at some point.
1159QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1160 ArrayType::ArraySizeModifier ASM,
1161 unsigned EltTypeQuals) {
1162 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1163 "Size must be type- or value-dependent!");
1164
1165 // Since we don't unique expressions, it isn't possible to unique
1166 // dependently-sized array types.
1167
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001168 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001169 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1170 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001171
1172 DependentSizedArrayTypes.push_back(New);
1173 Types.push_back(New);
1174 return QualType(New, 0);
1175}
1176
Eli Friedman8ff07782008-02-15 18:16:39 +00001177QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1178 ArrayType::ArraySizeModifier ASM,
1179 unsigned EltTypeQuals) {
1180 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001181 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001182
1183 void *InsertPos = 0;
1184 if (IncompleteArrayType *ATP =
1185 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1186 return QualType(ATP, 0);
1187
1188 // If the element type isn't canonical, this won't be a canonical type
1189 // either, so fill in the canonical type field.
1190 QualType Canonical;
1191
1192 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001193 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001194 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001195
1196 // Get the new insert position for the node we care about.
1197 IncompleteArrayType *NewIP =
1198 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001199 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001200 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001201
Steve Naroff93fd2112009-01-27 22:08:43 +00001202 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001203 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001204
1205 IncompleteArrayTypes.InsertNode(New, InsertPos);
1206 Types.push_back(New);
1207 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001208}
1209
Chris Lattner4b009652007-07-25 00:24:17 +00001210/// getVectorType - Return the unique reference to a vector type of
1211/// the specified element type and size. VectorType must be a built-in type.
1212QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1213 BuiltinType *baseType;
1214
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001215 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001216 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1217
1218 // Check if we've already instantiated a vector of this type.
1219 llvm::FoldingSetNodeID ID;
1220 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1221 void *InsertPos = 0;
1222 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1223 return QualType(VTP, 0);
1224
1225 // If the element type isn't canonical, this won't be a canonical type either,
1226 // so fill in the canonical type field.
1227 QualType Canonical;
1228 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001229 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001230
1231 // Get the new insert position for the node we care about.
1232 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001233 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001234 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001235 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001236 VectorTypes.InsertNode(New, InsertPos);
1237 Types.push_back(New);
1238 return QualType(New, 0);
1239}
1240
Nate Begemanaf6ed502008-04-18 23:10:10 +00001241/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001242/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001243QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001244 BuiltinType *baseType;
1245
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001246 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001247 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001248
1249 // Check if we've already instantiated a vector of this type.
1250 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001251 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001252 void *InsertPos = 0;
1253 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1254 return QualType(VTP, 0);
1255
1256 // If the element type isn't canonical, this won't be a canonical type either,
1257 // so fill in the canonical type field.
1258 QualType Canonical;
1259 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001260 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001261
1262 // Get the new insert position for the node we care about.
1263 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001264 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001265 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001266 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001267 VectorTypes.InsertNode(New, InsertPos);
1268 Types.push_back(New);
1269 return QualType(New, 0);
1270}
1271
Douglas Gregor2a2e0402009-06-17 21:51:59 +00001272QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
1273 Expr *SizeExpr,
1274 SourceLocation AttrLoc) {
1275 DependentSizedExtVectorType *New =
1276 new (*this,8) DependentSizedExtVectorType(vecType, QualType(),
1277 SizeExpr, AttrLoc);
1278
1279 DependentSizedExtVectorTypes.push_back(New);
1280 Types.push_back(New);
1281 return QualType(New, 0);
1282}
1283
Douglas Gregor4fa58902009-02-26 23:50:07 +00001284/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001285///
Douglas Gregor4fa58902009-02-26 23:50:07 +00001286QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Chris Lattner4b009652007-07-25 00:24:17 +00001287 // Unique functions, to guarantee there is only one function of a particular
1288 // structure.
1289 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001290 FunctionNoProtoType::Profile(ID, ResultTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001291
1292 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001293 if (FunctionNoProtoType *FT =
1294 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001295 return QualType(FT, 0);
1296
1297 QualType Canonical;
1298 if (!ResultTy->isCanonical()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00001299 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001300
1301 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001302 FunctionNoProtoType *NewIP =
1303 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001304 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001305 }
1306
Douglas Gregor4fa58902009-02-26 23:50:07 +00001307 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001308 Types.push_back(New);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001309 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001310 return QualType(New, 0);
1311}
1312
1313/// getFunctionType - Return a normal function type with a typed argument
1314/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001315QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001316 unsigned NumArgs, bool isVariadic,
Sebastian Redl2767d882009-05-27 22:11:52 +00001317 unsigned TypeQuals, bool hasExceptionSpec,
1318 bool hasAnyExceptionSpec, unsigned NumExs,
1319 const QualType *ExArray) {
Chris Lattner4b009652007-07-25 00:24:17 +00001320 // Unique functions, to guarantee there is only one function of a particular
1321 // structure.
1322 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001323 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl2767d882009-05-27 22:11:52 +00001324 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1325 NumExs, ExArray);
Chris Lattner4b009652007-07-25 00:24:17 +00001326
1327 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001328 if (FunctionProtoType *FTP =
1329 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001330 return QualType(FTP, 0);
Sebastian Redl2767d882009-05-27 22:11:52 +00001331
1332 // Determine whether the type being created is already canonical or not.
Chris Lattner4b009652007-07-25 00:24:17 +00001333 bool isCanonical = ResultTy->isCanonical();
Sebastian Redl2767d882009-05-27 22:11:52 +00001334 if (hasExceptionSpec)
1335 isCanonical = false;
Chris Lattner4b009652007-07-25 00:24:17 +00001336 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1337 if (!ArgArray[i]->isCanonical())
1338 isCanonical = false;
1339
1340 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl2767d882009-05-27 22:11:52 +00001341 // The exception spec is not part of the canonical type.
Chris Lattner4b009652007-07-25 00:24:17 +00001342 QualType Canonical;
1343 if (!isCanonical) {
1344 llvm::SmallVector<QualType, 16> CanonicalArgs;
1345 CanonicalArgs.reserve(NumArgs);
1346 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001347 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redl2767d882009-05-27 22:11:52 +00001348
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001349 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foad9e6bef42009-05-21 09:52:38 +00001350 CanonicalArgs.data(), NumArgs,
Sebastian Redlba9a3712009-05-06 23:27:55 +00001351 isVariadic, TypeQuals);
Sebastian Redl2767d882009-05-27 22:11:52 +00001352
Chris Lattner4b009652007-07-25 00:24:17 +00001353 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001354 FunctionProtoType *NewIP =
1355 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001356 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001357 }
Sebastian Redl2767d882009-05-27 22:11:52 +00001358
Douglas Gregor4fa58902009-02-26 23:50:07 +00001359 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl2767d882009-05-27 22:11:52 +00001360 // for two variable size arrays (for parameter and exception types) at the
1361 // end of them.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001362 FunctionProtoType *FTP =
Sebastian Redl2767d882009-05-27 22:11:52 +00001363 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1364 NumArgs*sizeof(QualType) +
1365 NumExs*sizeof(QualType), 8);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001366 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl2767d882009-05-27 22:11:52 +00001367 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1368 ExArray, NumExs, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001369 Types.push_back(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001370 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001371 return QualType(FTP, 0);
1372}
1373
Douglas Gregor1d661552008-04-13 21:07:44 +00001374/// getTypeDeclType - Return the unique reference to the type for the
1375/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001376QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001377 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001378 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1379
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001380 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001381 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001382 else if (isa<TemplateTypeParmDecl>(Decl)) {
1383 assert(false && "Template type parameter types are always available.");
1384 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001385 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001386
Douglas Gregor2e047592009-02-28 01:32:25 +00001387 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001388 if (PrevDecl)
1389 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001390 else
1391 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001392 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001393 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1394 if (PrevDecl)
1395 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001396 else
1397 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001398 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001399 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001400 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001401
Ted Kremenek46a837c2008-09-05 17:16:31 +00001402 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001403 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001404}
1405
Chris Lattner4b009652007-07-25 00:24:17 +00001406/// getTypedefType - Return the unique reference to the type for the
1407/// specified typename decl.
1408QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1409 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1410
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001411 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001412 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001413 Types.push_back(Decl->TypeForDecl);
1414 return QualType(Decl->TypeForDecl, 0);
1415}
1416
Ted Kremenek42730c52008-01-07 19:49:32 +00001417/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001418/// specified ObjC interface decl.
Daniel Dunbarbe1ff272009-04-22 04:34:53 +00001419QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001420 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1421
Daniel Dunbarbe1ff272009-04-22 04:34:53 +00001422 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1423 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001424 Types.push_back(Decl->TypeForDecl);
1425 return QualType(Decl->TypeForDecl, 0);
1426}
1427
Douglas Gregora4918772009-02-05 23:33:38 +00001428/// \brief Retrieve the template type parameter type for a template
Anders Carlsson4e3d3552009-06-16 00:30:48 +00001429/// parameter or parameter pack with the given depth, index, and (optionally)
1430/// name.
Douglas Gregora4918772009-02-05 23:33:38 +00001431QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson4e3d3552009-06-16 00:30:48 +00001432 bool ParameterPack,
Douglas Gregora4918772009-02-05 23:33:38 +00001433 IdentifierInfo *Name) {
1434 llvm::FoldingSetNodeID ID;
Anders Carlsson4e3d3552009-06-16 00:30:48 +00001435 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregora4918772009-02-05 23:33:38 +00001436 void *InsertPos = 0;
1437 TemplateTypeParmType *TypeParm
1438 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1439
1440 if (TypeParm)
1441 return QualType(TypeParm, 0);
1442
Anders Carlsson4e3d3552009-06-16 00:30:48 +00001443 if (Name) {
1444 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
1445 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack,
1446 Name, Canon);
1447 } else
1448 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregora4918772009-02-05 23:33:38 +00001449
1450 Types.push_back(TypeParm);
1451 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1452
1453 return QualType(TypeParm, 0);
1454}
1455
Douglas Gregor8e458f42009-02-09 18:46:07 +00001456QualType
Douglas Gregordd13e842009-03-30 22:58:21 +00001457ASTContext::getTemplateSpecializationType(TemplateName Template,
1458 const TemplateArgument *Args,
1459 unsigned NumArgs,
1460 QualType Canon) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001461 if (!Canon.isNull())
1462 Canon = getCanonicalType(Canon);
Douglas Gregor9c7825b2009-02-26 22:19:44 +00001463
Douglas Gregor8e458f42009-02-09 18:46:07 +00001464 llvm::FoldingSetNodeID ID;
Douglas Gregordd13e842009-03-30 22:58:21 +00001465 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001466
Douglas Gregor8e458f42009-02-09 18:46:07 +00001467 void *InsertPos = 0;
Douglas Gregordd13e842009-03-30 22:58:21 +00001468 TemplateSpecializationType *Spec
1469 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001470
1471 if (Spec)
1472 return QualType(Spec, 0);
1473
Douglas Gregordd13e842009-03-30 22:58:21 +00001474 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001475 sizeof(TemplateArgument) * NumArgs),
1476 8);
Douglas Gregordd13e842009-03-30 22:58:21 +00001477 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001478 Types.push_back(Spec);
Douglas Gregordd13e842009-03-30 22:58:21 +00001479 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001480
1481 return QualType(Spec, 0);
1482}
1483
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001484QualType
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001485ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001486 QualType NamedType) {
1487 llvm::FoldingSetNodeID ID;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001488 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001489
1490 void *InsertPos = 0;
1491 QualifiedNameType *T
1492 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1493 if (T)
1494 return QualType(T, 0);
1495
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001496 T = new (*this) QualifiedNameType(NNS, NamedType,
1497 getCanonicalType(NamedType));
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001498 Types.push_back(T);
1499 QualifiedNameTypes.InsertNode(T, InsertPos);
1500 return QualType(T, 0);
1501}
1502
Douglas Gregord3022602009-03-27 23:10:48 +00001503QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1504 const IdentifierInfo *Name,
1505 QualType Canon) {
1506 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1507
1508 if (Canon.isNull()) {
1509 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1510 if (CanonNNS != NNS)
1511 Canon = getTypenameType(CanonNNS, Name);
1512 }
1513
1514 llvm::FoldingSetNodeID ID;
1515 TypenameType::Profile(ID, NNS, Name);
1516
1517 void *InsertPos = 0;
1518 TypenameType *T
1519 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1520 if (T)
1521 return QualType(T, 0);
1522
1523 T = new (*this) TypenameType(NNS, Name, Canon);
1524 Types.push_back(T);
1525 TypenameTypes.InsertNode(T, InsertPos);
1526 return QualType(T, 0);
1527}
1528
Douglas Gregor77da5802009-04-01 00:28:59 +00001529QualType
1530ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1531 const TemplateSpecializationType *TemplateId,
1532 QualType Canon) {
1533 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1534
1535 if (Canon.isNull()) {
1536 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1537 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1538 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1539 const TemplateSpecializationType *CanonTemplateId
1540 = CanonType->getAsTemplateSpecializationType();
1541 assert(CanonTemplateId &&
1542 "Canonical type must also be a template specialization type");
1543 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1544 }
1545 }
1546
1547 llvm::FoldingSetNodeID ID;
1548 TypenameType::Profile(ID, NNS, TemplateId);
1549
1550 void *InsertPos = 0;
1551 TypenameType *T
1552 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1553 if (T)
1554 return QualType(T, 0);
1555
1556 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1557 Types.push_back(T);
1558 TypenameTypes.InsertNode(T, InsertPos);
1559 return QualType(T, 0);
1560}
1561
Chris Lattnere1352302008-04-07 04:56:42 +00001562/// CmpProtocolNames - Comparison predicate for sorting protocols
1563/// alphabetically.
1564static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1565 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001566 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001567}
1568
1569static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1570 unsigned &NumProtocols) {
1571 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1572
1573 // Sort protocols, keyed by name.
1574 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1575
1576 // Remove duplicates.
1577 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1578 NumProtocols = ProtocolsEnd-Protocols;
1579}
1580
1581
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001582/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1583/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001584QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1585 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001586 // Sort the protocol list alphabetically to canonicalize it.
1587 SortAndUniqueProtocols(Protocols, NumProtocols);
1588
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001589 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001590 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001591
1592 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001593 if (ObjCQualifiedInterfaceType *QT =
1594 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001595 return QualType(QT, 0);
1596
1597 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001598 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001599 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001600
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001601 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001602 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001603 return QualType(QType, 0);
1604}
1605
Chris Lattnere1352302008-04-07 04:56:42 +00001606/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1607/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001608QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001609 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001610 // Sort the protocol list alphabetically to canonicalize it.
1611 SortAndUniqueProtocols(Protocols, NumProtocols);
1612
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001613 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001614 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001615
1616 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001617 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001618 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001619 return QualType(QT, 0);
1620
1621 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001622 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001623 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001624 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001625 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001626 return QualType(QType, 0);
1627}
1628
Douglas Gregor4fa58902009-02-26 23:50:07 +00001629/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1630/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff0604dd92007-08-01 18:02:17 +00001631/// multiple declarations that refer to "typeof(x)" all contain different
1632/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1633/// on canonical type's (which are always unique).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001634QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001635 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001636 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001637 Types.push_back(toe);
1638 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001639}
1640
Steve Naroff0604dd92007-08-01 18:02:17 +00001641/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1642/// TypeOfType AST's. The only motivation to unique these nodes would be
1643/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1644/// an issue. This doesn't effect the type checker, since it operates
1645/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001646QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001647 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001648 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001649 Types.push_back(tot);
1650 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001651}
1652
Chris Lattner4b009652007-07-25 00:24:17 +00001653/// getTagDeclType - Return the unique reference to the type for the
1654/// specified TagDecl (struct/union/class/enum) decl.
1655QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001656 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001657 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001658}
1659
1660/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1661/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1662/// needs to agree with the definition in <stddef.h>.
1663QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001664 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001665}
1666
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001667/// getSignedWCharType - Return the type of "signed wchar_t".
1668/// Used when in C++, as a GCC extension.
1669QualType ASTContext::getSignedWCharType() const {
1670 // FIXME: derive from "Target" ?
1671 return WCharTy;
1672}
1673
1674/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1675/// Used when in C++, as a GCC extension.
1676QualType ASTContext::getUnsignedWCharType() const {
1677 // FIXME: derive from "Target" ?
1678 return UnsignedIntTy;
1679}
1680
Chris Lattner4b009652007-07-25 00:24:17 +00001681/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1682/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1683QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001684 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001685}
1686
Chris Lattner19eb97e2008-04-02 05:18:44 +00001687//===----------------------------------------------------------------------===//
1688// Type Operators
1689//===----------------------------------------------------------------------===//
1690
Chris Lattner3dae6f42008-04-06 22:41:35 +00001691/// getCanonicalType - Return the canonical (structural) type corresponding to
1692/// the specified potentially non-canonical type. The non-canonical version
1693/// of a type may have many "decorated" versions of types. Decorators can
1694/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1695/// to be free of any of these, allowing two canonical types to be compared
1696/// for exact equality with a simple pointer comparison.
1697QualType ASTContext::getCanonicalType(QualType T) {
1698 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001699
1700 // If the result has type qualifiers, make sure to canonicalize them as well.
1701 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1702 if (TypeQuals == 0) return CanType;
1703
1704 // If the type qualifiers are on an array type, get the canonical type of the
1705 // array with the qualifiers applied to the element type.
1706 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1707 if (!AT)
1708 return CanType.getQualifiedType(TypeQuals);
1709
1710 // Get the canonical version of the element with the extra qualifiers on it.
1711 // This can recursively sink qualifiers through multiple levels of arrays.
1712 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1713 NewEltTy = getCanonicalType(NewEltTy);
1714
1715 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1716 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1717 CAT->getIndexTypeQualifier());
1718 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1719 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1720 IAT->getIndexTypeQualifier());
1721
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001722 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1723 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1724 DSAT->getSizeModifier(),
1725 DSAT->getIndexTypeQualifier());
1726
Chris Lattnera1923f62008-08-04 07:31:14 +00001727 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1728 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1729 VAT->getSizeModifier(),
1730 VAT->getIndexTypeQualifier());
1731}
1732
Douglas Gregor9054f982009-05-10 22:57:19 +00001733Decl *ASTContext::getCanonicalDecl(Decl *D) {
Douglas Gregordf3e9572009-05-10 22:59:12 +00001734 if (!D)
1735 return 0;
1736
Douglas Gregor9054f982009-05-10 22:57:19 +00001737 if (TagDecl *Tag = dyn_cast<TagDecl>(D)) {
1738 QualType T = getTagDeclType(Tag);
1739 return cast<TagDecl>(cast<TagType>(T.getTypePtr()->CanonicalType)
1740 ->getDecl());
1741 }
1742
1743 if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(D)) {
1744 while (Template->getPreviousDeclaration())
1745 Template = Template->getPreviousDeclaration();
1746 return Template;
1747 }
1748
1749 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
1750 while (Function->getPreviousDeclaration())
1751 Function = Function->getPreviousDeclaration();
1752 return const_cast<FunctionDecl *>(Function);
1753 }
1754
1755 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
1756 while (Var->getPreviousDeclaration())
1757 Var = Var->getPreviousDeclaration();
1758 return const_cast<VarDecl *>(Var);
1759 }
1760
1761 return D;
1762}
1763
Douglas Gregorb88ba412009-05-07 06:41:52 +00001764TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
1765 // If this template name refers to a template, the canonical
1766 // template name merely stores the template itself.
1767 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Douglas Gregor9054f982009-05-10 22:57:19 +00001768 return TemplateName(cast<TemplateDecl>(getCanonicalDecl(Template)));
Douglas Gregorb88ba412009-05-07 06:41:52 +00001769
1770 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
1771 assert(DTN && "Non-dependent template names must refer to template decls.");
1772 return DTN->CanonicalTemplateName;
1773}
1774
Douglas Gregord3022602009-03-27 23:10:48 +00001775NestedNameSpecifier *
1776ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1777 if (!NNS)
1778 return 0;
1779
1780 switch (NNS->getKind()) {
1781 case NestedNameSpecifier::Identifier:
1782 // Canonicalize the prefix but keep the identifier the same.
1783 return NestedNameSpecifier::Create(*this,
1784 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1785 NNS->getAsIdentifier());
1786
1787 case NestedNameSpecifier::Namespace:
1788 // A namespace is canonical; build a nested-name-specifier with
1789 // this namespace and no prefix.
1790 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1791
1792 case NestedNameSpecifier::TypeSpec:
1793 case NestedNameSpecifier::TypeSpecWithTemplate: {
1794 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1795 NestedNameSpecifier *Prefix = 0;
1796
1797 // FIXME: This isn't the right check!
1798 if (T->isDependentType())
1799 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1800
1801 return NestedNameSpecifier::Create(*this, Prefix,
1802 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1803 T.getTypePtr());
1804 }
1805
1806 case NestedNameSpecifier::Global:
1807 // The global specifier is canonical and unique.
1808 return NNS;
1809 }
1810
1811 // Required to silence a GCC warning
1812 return 0;
1813}
1814
Chris Lattnera1923f62008-08-04 07:31:14 +00001815
1816const ArrayType *ASTContext::getAsArrayType(QualType T) {
1817 // Handle the non-qualified case efficiently.
1818 if (T.getCVRQualifiers() == 0) {
1819 // Handle the common positive case fast.
1820 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1821 return AT;
1822 }
1823
1824 // Handle the common negative case fast, ignoring CVR qualifiers.
1825 QualType CType = T->getCanonicalTypeInternal();
1826
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001827 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001828 // test.
1829 if (!isa<ArrayType>(CType) &&
1830 !isa<ArrayType>(CType.getUnqualifiedType()))
1831 return 0;
1832
1833 // Apply any CVR qualifiers from the array type to the element type. This
1834 // implements C99 6.7.3p8: "If the specification of an array type includes
1835 // any type qualifiers, the element type is so qualified, not the array type."
1836
1837 // If we get here, we either have type qualifiers on the type, or we have
1838 // sugar such as a typedef in the way. If we have type qualifiers on the type
1839 // we must propagate them down into the elemeng type.
1840 unsigned CVRQuals = T.getCVRQualifiers();
1841 unsigned AddrSpace = 0;
1842 Type *Ty = T.getTypePtr();
1843
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001844 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001845 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001846 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1847 AddrSpace = EXTQT->getAddressSpace();
1848 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001849 } else {
1850 T = Ty->getDesugaredType();
1851 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1852 break;
1853 CVRQuals |= T.getCVRQualifiers();
1854 Ty = T.getTypePtr();
1855 }
1856 }
1857
1858 // If we have a simple case, just return now.
1859 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1860 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1861 return ATy;
1862
1863 // Otherwise, we have an array and we have qualifiers on it. Push the
1864 // qualifiers into the array element type and return a new array type.
1865 // Get the canonical version of the element with the extra qualifiers on it.
1866 // This can recursively sink qualifiers through multiple levels of arrays.
1867 QualType NewEltTy = ATy->getElementType();
1868 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001869 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001870 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1871
1872 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1873 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1874 CAT->getSizeModifier(),
1875 CAT->getIndexTypeQualifier()));
1876 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1877 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1878 IAT->getSizeModifier(),
1879 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001880
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001881 if (const DependentSizedArrayType *DSAT
1882 = dyn_cast<DependentSizedArrayType>(ATy))
1883 return cast<ArrayType>(
1884 getDependentSizedArrayType(NewEltTy,
1885 DSAT->getSizeExpr(),
1886 DSAT->getSizeModifier(),
1887 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001888
Chris Lattnera1923f62008-08-04 07:31:14 +00001889 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1890 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1891 VAT->getSizeModifier(),
1892 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001893}
1894
1895
Chris Lattner19eb97e2008-04-02 05:18:44 +00001896/// getArrayDecayedType - Return the properly qualified result of decaying the
1897/// specified array type to a pointer. This operation is non-trivial when
1898/// handling typedefs etc. The canonical type of "T" must be an array type,
1899/// this returns a pointer to a properly qualified element of the array.
1900///
1901/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1902QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001903 // Get the element type with 'getAsArrayType' so that we don't lose any
1904 // typedefs in the element type of the array. This also handles propagation
1905 // of type qualifiers from the array type into the element type if present
1906 // (C99 6.7.3p8).
1907 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1908 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001909
Chris Lattnera1923f62008-08-04 07:31:14 +00001910 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001911
1912 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001913 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001914}
1915
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001916QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001917 QualType ElemTy = VAT->getElementType();
1918
1919 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1920 return getBaseElementType(VAT);
1921
1922 return ElemTy;
1923}
1924
Chris Lattner4b009652007-07-25 00:24:17 +00001925/// getFloatingRank - Return a relative rank for floating point types.
1926/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001927static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001928 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001929 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001930
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001931 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001932 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001933 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001934 case BuiltinType::Float: return FloatRank;
1935 case BuiltinType::Double: return DoubleRank;
1936 case BuiltinType::LongDouble: return LongDoubleRank;
1937 }
1938}
1939
Steve Narofffa0c4532007-08-27 01:41:48 +00001940/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1941/// point or a complex type (based on typeDomain/typeSize).
1942/// 'typeDomain' is a real floating point or complex type.
1943/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001944QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1945 QualType Domain) const {
1946 FloatingRank EltRank = getFloatingRank(Size);
1947 if (Domain->isComplexType()) {
1948 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001949 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001950 case FloatRank: return FloatComplexTy;
1951 case DoubleRank: return DoubleComplexTy;
1952 case LongDoubleRank: return LongDoubleComplexTy;
1953 }
Chris Lattner4b009652007-07-25 00:24:17 +00001954 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001955
1956 assert(Domain->isRealFloatingType() && "Unknown domain!");
1957 switch (EltRank) {
1958 default: assert(0 && "getFloatingRank(): illegal value for rank");
1959 case FloatRank: return FloatTy;
1960 case DoubleRank: return DoubleTy;
1961 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001962 }
Chris Lattner4b009652007-07-25 00:24:17 +00001963}
1964
Chris Lattner51285d82008-04-06 23:55:33 +00001965/// getFloatingTypeOrder - Compare the rank of the two specified floating
1966/// point types, ignoring the domain of the type (i.e. 'double' ==
1967/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1968/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001969int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1970 FloatingRank LHSR = getFloatingRank(LHS);
1971 FloatingRank RHSR = getFloatingRank(RHS);
1972
1973 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001974 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001975 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001976 return 1;
1977 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001978}
1979
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001980/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1981/// routine will assert if passed a built-in type that isn't an integer or enum,
1982/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001983unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001984 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001985 if (EnumType* ET = dyn_cast<EnumType>(T))
1986 T = ET->getDecl()->getIntegerType().getTypePtr();
1987
1988 // There are two things which impact the integer rank: the width, and
1989 // the ordering of builtins. The builtin ordering is encoded in the
1990 // bottom three bits; the width is encoded in the bits above that.
Chris Lattnerc46fcdd2009-06-14 01:54:56 +00001991 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T))
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001992 return FWIT->getWidth() << 3;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001993
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001994 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001995 default: assert(0 && "getIntegerRank(): not a built-in integer");
1996 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001997 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001998 case BuiltinType::Char_S:
1999 case BuiltinType::Char_U:
2000 case BuiltinType::SChar:
2001 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002002 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00002003 case BuiltinType::Short:
2004 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002005 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00002006 case BuiltinType::Int:
2007 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002008 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00002009 case BuiltinType::Long:
2010 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002011 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00002012 case BuiltinType::LongLong:
2013 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002014 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner6cc7e412009-04-30 02:43:43 +00002015 case BuiltinType::Int128:
2016 case BuiltinType::UInt128:
2017 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00002018 }
2019}
2020
Chris Lattner51285d82008-04-06 23:55:33 +00002021/// getIntegerTypeOrder - Returns the highest ranked integer type:
2022/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
2023/// LHS < RHS, return -1.
2024int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00002025 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2026 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00002027 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00002028
Chris Lattnerc1b68db2008-04-06 22:59:24 +00002029 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2030 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00002031
Chris Lattner51285d82008-04-06 23:55:33 +00002032 unsigned LHSRank = getIntegerRank(LHSC);
2033 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00002034
Chris Lattner51285d82008-04-06 23:55:33 +00002035 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2036 if (LHSRank == RHSRank) return 0;
2037 return LHSRank > RHSRank ? 1 : -1;
2038 }
Chris Lattner4b009652007-07-25 00:24:17 +00002039
Chris Lattner51285d82008-04-06 23:55:33 +00002040 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2041 if (LHSUnsigned) {
2042 // If the unsigned [LHS] type is larger, return it.
2043 if (LHSRank >= RHSRank)
2044 return 1;
2045
2046 // If the signed type can represent all values of the unsigned type, it
2047 // wins. Because we are dealing with 2's complement and types that are
2048 // powers of two larger than each other, this is always safe.
2049 return -1;
2050 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00002051
Chris Lattner51285d82008-04-06 23:55:33 +00002052 // If the unsigned [RHS] type is larger, return it.
2053 if (RHSRank >= LHSRank)
2054 return -1;
2055
2056 // If the signed type can represent all values of the unsigned type, it
2057 // wins. Because we are dealing with 2's complement and types that are
2058 // powers of two larger than each other, this is always safe.
2059 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00002060}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002061
2062// getCFConstantStringType - Return the type used for constant CFStrings.
2063QualType ASTContext::getCFConstantStringType() {
2064 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00002065 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00002066 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00002067 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002068 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002069
2070 // const int *isa;
2071 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002072 // int flags;
2073 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002074 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002075 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002076 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002077 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00002078
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002079 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00002080 for (unsigned i = 0; i < 4; ++i) {
2081 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2082 SourceLocation(), 0,
2083 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002084 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002085 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002086 }
2087
2088 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002089 }
2090
2091 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00002092}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002093
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002094void ASTContext::setCFConstantStringType(QualType T) {
2095 const RecordType *Rec = T->getAsRecordType();
2096 assert(Rec && "Invalid CFConstantStringType");
2097 CFConstantStringTypeDecl = Rec->getDecl();
2098}
2099
Anders Carlssonf58cac72008-08-30 19:34:46 +00002100QualType ASTContext::getObjCFastEnumerationStateType()
2101{
2102 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00002103 ObjCFastEnumerationStateTypeDecl =
2104 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2105 &Idents.get("__objcFastEnumerationState"));
2106
Anders Carlssonf58cac72008-08-30 19:34:46 +00002107 QualType FieldTypes[] = {
2108 UnsignedLongTy,
2109 getPointerType(ObjCIdType),
2110 getPointerType(UnsignedLongTy),
2111 getConstantArrayType(UnsignedLongTy,
2112 llvm::APInt(32, 5), ArrayType::Normal, 0)
2113 };
2114
Douglas Gregor8acb7272008-12-11 16:49:14 +00002115 for (size_t i = 0; i < 4; ++i) {
2116 FieldDecl *Field = FieldDecl::Create(*this,
2117 ObjCFastEnumerationStateTypeDecl,
2118 SourceLocation(), 0,
2119 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002120 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002121 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002122 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00002123
Douglas Gregor8acb7272008-12-11 16:49:14 +00002124 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00002125 }
2126
2127 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2128}
2129
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002130void ASTContext::setObjCFastEnumerationStateType(QualType T) {
2131 const RecordType *Rec = T->getAsRecordType();
2132 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2133 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2134}
2135
Anders Carlssone3f02572007-10-29 06:33:42 +00002136// This returns true if a type has been typedefed to BOOL:
2137// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00002138static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002139 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00002140 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2141 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002142
2143 return false;
2144}
2145
Ted Kremenek42730c52008-01-07 19:49:32 +00002146/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002147/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00002148int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00002149 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002150
2151 // Make all integer and enum types at least as large as an int
2152 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002153 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002154 // Treat arrays as pointers, since that's how they're passed in.
2155 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002156 sz = getTypeSize(VoidPtrTy);
2157 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002158}
2159
Ted Kremenek42730c52008-01-07 19:49:32 +00002160/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002161/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002162void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00002163 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002164 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002165 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00002166 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002167 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002168 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002169 // Compute size of all parameters.
2170 // Start with computing size of a pointer in number of bytes.
2171 // FIXME: There might(should) be a better way of doing this computation!
2172 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00002173 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002174 // The first two arguments (self and _cmd) are pointers; account for
2175 // their size.
2176 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002177 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2178 E = Decl->param_end(); PI != E; ++PI) {
2179 QualType PType = (*PI)->getType();
2180 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00002181 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002182 ParmOffset += sz;
2183 }
2184 S += llvm::utostr(ParmOffset);
2185 S += "@0:";
2186 S += llvm::utostr(PtrSize);
2187
2188 // Argument types.
2189 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002190 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2191 E = Decl->param_end(); PI != E; ++PI) {
2192 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002193 QualType PType = PVDecl->getOriginalType();
2194 if (const ArrayType *AT =
Steve Naroff78380fb2009-04-14 00:03:58 +00002195 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2196 // Use array's original type only if it has known number of
2197 // elements.
Steve Naroff6777bf32009-04-14 00:40:09 +00002198 if (!isa<ConstantArrayType>(AT))
Steve Naroff78380fb2009-04-14 00:03:58 +00002199 PType = PVDecl->getType();
2200 } else if (PType->isFunctionType())
2201 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002202 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002203 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002204 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002205 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002206 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00002207 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002208 }
2209}
2210
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002211/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002212/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002213/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2214/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002215/// Property attributes are stored as a comma-delimited C string. The simple
2216/// attributes readonly and bycopy are encoded as single characters. The
2217/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2218/// encoded as single characters, followed by an identifier. Property types
2219/// are also encoded as a parametrized attribute. The characters used to encode
2220/// these attributes are defined by the following enumeration:
2221/// @code
2222/// enum PropertyAttributes {
2223/// kPropertyReadOnly = 'R', // property is read-only.
2224/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2225/// kPropertyByref = '&', // property is a reference to the value last assigned
2226/// kPropertyDynamic = 'D', // property is dynamic
2227/// kPropertyGetter = 'G', // followed by getter selector name
2228/// kPropertySetter = 'S', // followed by setter selector name
2229/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2230/// kPropertyType = 't' // followed by old-style type encoding.
2231/// kPropertyWeak = 'W' // 'weak' property
2232/// kPropertyStrong = 'P' // property GC'able
2233/// kPropertyNonAtomic = 'N' // property non-atomic
2234/// };
2235/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002236void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2237 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00002238 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002239 // Collect information from the property implementation decl(s).
2240 bool Dynamic = false;
2241 ObjCPropertyImplDecl *SynthesizePID = 0;
2242
2243 // FIXME: Duplicated code due to poor abstraction.
2244 if (Container) {
2245 if (const ObjCCategoryImplDecl *CID =
2246 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2247 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregorcd19b572009-04-23 01:02:12 +00002248 i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2249 i != e; ++i) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002250 ObjCPropertyImplDecl *PID = *i;
2251 if (PID->getPropertyDecl() == PD) {
2252 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2253 Dynamic = true;
2254 } else {
2255 SynthesizePID = PID;
2256 }
2257 }
2258 }
2259 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002260 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002261 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregorcd19b572009-04-23 01:02:12 +00002262 i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2263 i != e; ++i) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002264 ObjCPropertyImplDecl *PID = *i;
2265 if (PID->getPropertyDecl() == PD) {
2266 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2267 Dynamic = true;
2268 } else {
2269 SynthesizePID = PID;
2270 }
2271 }
2272 }
2273 }
2274 }
2275
2276 // FIXME: This is not very efficient.
2277 S = "T";
2278
2279 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002280 // GCC has some special rules regarding encoding of properties which
2281 // closely resembles encoding of ivars.
Daniel Dunbar701c8502009-04-20 06:37:24 +00002282 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002283 true /* outermost type */,
2284 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002285
2286 if (PD->isReadOnly()) {
2287 S += ",R";
2288 } else {
2289 switch (PD->getSetterKind()) {
2290 case ObjCPropertyDecl::Assign: break;
2291 case ObjCPropertyDecl::Copy: S += ",C"; break;
2292 case ObjCPropertyDecl::Retain: S += ",&"; break;
2293 }
2294 }
2295
2296 // It really isn't clear at all what this means, since properties
2297 // are "dynamic by default".
2298 if (Dynamic)
2299 S += ",D";
2300
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002301 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2302 S += ",N";
2303
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002304 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2305 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002306 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002307 }
2308
2309 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2310 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002311 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002312 }
2313
2314 if (SynthesizePID) {
2315 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2316 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002317 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002318 }
2319
2320 // FIXME: OBJCGC: weak & strong
2321}
2322
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002323/// getLegacyIntegralTypeEncoding -
2324/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002325/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002326/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2327///
2328void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2329 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2330 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002331 if (BT->getKind() == BuiltinType::ULong &&
2332 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002333 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002334 else
2335 if (BT->getKind() == BuiltinType::Long &&
2336 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002337 PointeeTy = IntTy;
2338 }
2339 }
2340}
2341
Fariborz Jahanian248db262008-01-22 22:44:46 +00002342void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002343 const FieldDecl *Field) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002344 // We follow the behavior of gcc, expanding structures which are
2345 // directly pointed to, and expanding embedded structures. Note that
2346 // these rules are sufficient to prevent recursive encoding of the
2347 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002348 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2349 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002350}
2351
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002352static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002353 const FieldDecl *FD) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002354 const Expr *E = FD->getBitWidth();
2355 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2356 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman5255e7a2009-04-26 19:19:15 +00002357 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002358 S += 'b';
2359 S += llvm::utostr(N);
2360}
2361
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002362void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2363 bool ExpandPointedToStructures,
2364 bool ExpandStructures,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002365 const FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002366 bool OutermostType,
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002367 bool EncodingProperty) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002368 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002369 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002370 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002371 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002372 else {
2373 char encoding;
2374 switch (BT->getKind()) {
2375 default: assert(0 && "Unhandled builtin type kind");
2376 case BuiltinType::Void: encoding = 'v'; break;
2377 case BuiltinType::Bool: encoding = 'B'; break;
2378 case BuiltinType::Char_U:
2379 case BuiltinType::UChar: encoding = 'C'; break;
2380 case BuiltinType::UShort: encoding = 'S'; break;
2381 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002382 case BuiltinType::ULong:
2383 encoding =
2384 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2385 break;
Chris Lattner6cc7e412009-04-30 02:43:43 +00002386 case BuiltinType::UInt128: encoding = 'T'; break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002387 case BuiltinType::ULongLong: encoding = 'Q'; break;
2388 case BuiltinType::Char_S:
2389 case BuiltinType::SChar: encoding = 'c'; break;
2390 case BuiltinType::Short: encoding = 's'; break;
2391 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002392 case BuiltinType::Long:
2393 encoding =
2394 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2395 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002396 case BuiltinType::LongLong: encoding = 'q'; break;
Chris Lattner6cc7e412009-04-30 02:43:43 +00002397 case BuiltinType::Int128: encoding = 't'; break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002398 case BuiltinType::Float: encoding = 'f'; break;
2399 case BuiltinType::Double: encoding = 'd'; break;
2400 case BuiltinType::LongDouble: encoding = 'd'; break;
2401 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002402
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002403 S += encoding;
2404 }
Anders Carlsson70e16dd2009-04-09 21:55:45 +00002405 } else if (const ComplexType *CT = T->getAsComplexType()) {
2406 S += 'j';
2407 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2408 false);
2409 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002410 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2411 ExpandPointedToStructures,
2412 ExpandStructures, FD);
2413 if (FD || EncodingProperty) {
2414 // Note that we do extended encoding of protocol qualifer list
2415 // Only when doing ivar or property encoding.
2416 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2417 S += '"';
Steve Naroff83418522009-05-27 16:21:00 +00002418 for (ObjCQualifiedIdType::qual_iterator I = QIDT->qual_begin(),
2419 E = QIDT->qual_end(); I != E; ++I) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002420 S += '<';
Steve Naroff83418522009-05-27 16:21:00 +00002421 S += (*I)->getNameAsString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002422 S += '>';
2423 }
2424 S += '"';
2425 }
2426 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002427 }
2428 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002429 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002430 bool isReadOnly = false;
2431 // For historical/compatibility reasons, the read-only qualifier of the
2432 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2433 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2434 // Also, do not emit the 'r' for anything but the outermost type!
2435 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2436 if (OutermostType && T.isConstQualified()) {
2437 isReadOnly = true;
2438 S += 'r';
2439 }
2440 }
2441 else if (OutermostType) {
2442 QualType P = PointeeTy;
2443 while (P->getAsPointerType())
2444 P = P->getAsPointerType()->getPointeeType();
2445 if (P.isConstQualified()) {
2446 isReadOnly = true;
2447 S += 'r';
2448 }
2449 }
2450 if (isReadOnly) {
2451 // Another legacy compatibility encoding. Some ObjC qualifier and type
2452 // combinations need to be rearranged.
2453 // Rewrite "in const" from "nr" to "rn"
2454 const char * s = S.c_str();
2455 int len = S.length();
2456 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2457 std::string replace = "rn";
2458 S.replace(S.end()-2, S.end(), replace);
2459 }
2460 }
Steve Naroff17c03822009-02-12 17:52:19 +00002461 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002462 S += '@';
2463 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002464 }
2465 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002466 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002467 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002468 // Another historical/compatibility reason.
2469 // We encode the underlying type which comes out as
2470 // {...};
2471 S += '^';
2472 getObjCEncodingForTypeImpl(PointeeTy, S,
2473 false, ExpandPointedToStructures,
2474 NULL);
2475 return;
2476 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002477 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002478 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002479 const ObjCInterfaceType *OIT =
2480 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002481 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002482 S += '"';
2483 S += OI->getNameAsCString();
Steve Naroff83418522009-05-27 16:21:00 +00002484 for (ObjCInterfaceType::qual_iterator I = OIT->qual_begin(),
2485 E = OIT->qual_end(); I != E; ++I) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002486 S += '<';
Steve Naroff83418522009-05-27 16:21:00 +00002487 S += (*I)->getNameAsString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002488 S += '>';
2489 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002490 S += '"';
2491 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002492 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002493 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002494 S += '#';
2495 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002496 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002497 S += ':';
2498 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002499 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002500
2501 if (PointeeTy->isCharType()) {
2502 // char pointer types should be encoded as '*' unless it is a
2503 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002504 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002505 S += '*';
2506 return;
2507 }
2508 }
2509
2510 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002511 getLegacyIntegralTypeEncoding(PointeeTy);
2512
2513 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002514 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002515 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002516 } else if (const ArrayType *AT =
2517 // Ignore type qualifiers etc.
2518 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002519 if (isa<IncompleteArrayType>(AT)) {
2520 // Incomplete arrays are encoded as a pointer to the array element.
2521 S += '^';
2522
2523 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2524 false, ExpandStructures, FD);
2525 } else {
2526 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002527
Anders Carlsson858c64d2009-02-22 01:38:57 +00002528 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2529 S += llvm::utostr(CAT->getSize().getZExtValue());
2530 else {
2531 //Variable length arrays are encoded as a regular array with 0 elements.
2532 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2533 S += '0';
2534 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002535
Anders Carlsson858c64d2009-02-22 01:38:57 +00002536 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2537 false, ExpandStructures, FD);
2538 S += ']';
2539 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002540 } else if (T->getAsFunctionType()) {
2541 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002542 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002543 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002544 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002545 // Anonymous structures print as '?'
2546 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2547 S += II->getName();
2548 } else {
2549 S += '?';
2550 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002551 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002552 S += '=';
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002553 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2554 FieldEnd = RDecl->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002555 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002556 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002557 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002558 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002559 S += '"';
2560 }
2561
2562 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002563 if (Field->isBitField()) {
2564 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2565 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002566 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002567 QualType qt = Field->getType();
2568 getLegacyIntegralTypeEncoding(qt);
2569 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002570 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002571 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002572 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002573 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002574 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002575 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002576 if (FD && FD->isBitField())
2577 EncodeBitField(this, S, FD);
2578 else
2579 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002580 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002581 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002582 } else if (T->isObjCInterfaceType()) {
2583 // @encode(class_name)
2584 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2585 S += '{';
2586 const IdentifierInfo *II = OI->getIdentifier();
2587 S += II->getName();
2588 S += '=';
Chris Lattner9329cf52009-03-31 08:48:01 +00002589 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002590 CollectObjCIvars(OI, RecFields);
Chris Lattner9329cf52009-03-31 08:48:01 +00002591 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002592 if (RecFields[i]->isBitField())
2593 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2594 RecFields[i]);
2595 else
2596 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2597 FD);
2598 }
2599 S += '}';
2600 }
2601 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002602 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002603}
2604
Ted Kremenek42730c52008-01-07 19:49:32 +00002605void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002606 std::string& S) const {
2607 if (QT & Decl::OBJC_TQ_In)
2608 S += 'n';
2609 if (QT & Decl::OBJC_TQ_Inout)
2610 S += 'N';
2611 if (QT & Decl::OBJC_TQ_Out)
2612 S += 'o';
2613 if (QT & Decl::OBJC_TQ_Bycopy)
2614 S += 'O';
2615 if (QT & Decl::OBJC_TQ_Byref)
2616 S += 'R';
2617 if (QT & Decl::OBJC_TQ_Oneway)
2618 S += 'V';
2619}
2620
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002621void ASTContext::setBuiltinVaListType(QualType T)
2622{
2623 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2624
2625 BuiltinVaListType = T;
2626}
2627
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002628void ASTContext::setObjCIdType(QualType T)
Steve Naroff9d12c902007-10-15 14:41:52 +00002629{
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002630 ObjCIdType = T;
2631
2632 const TypedefType *TT = T->getAsTypedefType();
2633 if (!TT)
2634 return;
2635
2636 TypedefDecl *TD = TT->getDecl();
Steve Naroff9d12c902007-10-15 14:41:52 +00002637
2638 // typedef struct objc_object *id;
2639 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002640 // User error - caller will issue diagnostics.
2641 if (!ptr)
2642 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002643 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002644 // User error - caller will issue diagnostics.
2645 if (!rec)
2646 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002647 IdStructType = rec;
2648}
2649
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002650void ASTContext::setObjCSelType(QualType T)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002651{
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002652 ObjCSelType = T;
2653
2654 const TypedefType *TT = T->getAsTypedefType();
2655 if (!TT)
2656 return;
2657 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002658
2659 // typedef struct objc_selector *SEL;
2660 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002661 if (!ptr)
2662 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002663 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002664 if (!rec)
2665 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002666 SelStructType = rec;
2667}
2668
Ted Kremenek42730c52008-01-07 19:49:32 +00002669void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002670{
Ted Kremenek42730c52008-01-07 19:49:32 +00002671 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002672}
2673
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002674void ASTContext::setObjCClassType(QualType T)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002675{
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002676 ObjCClassType = T;
2677
2678 const TypedefType *TT = T->getAsTypedefType();
2679 if (!TT)
2680 return;
2681 TypedefDecl *TD = TT->getDecl();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002682
2683 // typedef struct objc_class *Class;
2684 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2685 assert(ptr && "'Class' incorrectly typed");
2686 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2687 assert(rec && "'Class' incorrectly typed");
2688 ClassStructType = rec;
2689}
2690
Ted Kremenek42730c52008-01-07 19:49:32 +00002691void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2692 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002693 "'NSConstantString' type already set!");
2694
Ted Kremenek42730c52008-01-07 19:49:32 +00002695 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002696}
2697
Douglas Gregordd13e842009-03-30 22:58:21 +00002698/// \brief Retrieve the template name that represents a qualified
2699/// template name such as \c std::vector.
2700TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2701 bool TemplateKeyword,
2702 TemplateDecl *Template) {
2703 llvm::FoldingSetNodeID ID;
2704 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2705
2706 void *InsertPos = 0;
2707 QualifiedTemplateName *QTN =
2708 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2709 if (!QTN) {
2710 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2711 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2712 }
2713
2714 return TemplateName(QTN);
2715}
2716
2717/// \brief Retrieve the template name that represents a dependent
2718/// template name such as \c MetaFun::template apply.
2719TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2720 const IdentifierInfo *Name) {
2721 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2722
2723 llvm::FoldingSetNodeID ID;
2724 DependentTemplateName::Profile(ID, NNS, Name);
2725
2726 void *InsertPos = 0;
2727 DependentTemplateName *QTN =
2728 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2729
2730 if (QTN)
2731 return TemplateName(QTN);
2732
2733 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2734 if (CanonNNS == NNS) {
2735 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2736 } else {
2737 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2738 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2739 }
2740
2741 DependentTemplateNames.InsertNode(QTN, InsertPos);
2742 return TemplateName(QTN);
2743}
2744
Douglas Gregorc6507e42008-11-03 14:12:49 +00002745/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002746/// TargetInfo, produce the corresponding type. The unsigned @p Type
2747/// is actually a value of type @c TargetInfo::IntType.
2748QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002749 switch (Type) {
2750 case TargetInfo::NoInt: return QualType();
2751 case TargetInfo::SignedShort: return ShortTy;
2752 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2753 case TargetInfo::SignedInt: return IntTy;
2754 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2755 case TargetInfo::SignedLong: return LongTy;
2756 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2757 case TargetInfo::SignedLongLong: return LongLongTy;
2758 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2759 }
2760
2761 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002762 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002763}
Ted Kremenek118930e2008-07-24 23:58:27 +00002764
2765//===----------------------------------------------------------------------===//
2766// Type Predicates.
2767//===----------------------------------------------------------------------===//
2768
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002769/// isObjCNSObjectType - Return true if this is an NSObject object using
2770/// NSObject attribute on a c-style pointer type.
2771/// FIXME - Make it work directly on types.
2772///
2773bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2774 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2775 if (TypedefDecl *TD = TDT->getDecl())
2776 if (TD->getAttr<ObjCNSObjectAttr>())
2777 return true;
2778 }
2779 return false;
2780}
2781
Ted Kremenek118930e2008-07-24 23:58:27 +00002782/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2783/// to an object type. This includes "id" and "Class" (two 'special' pointers
2784/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2785/// ID type).
2786bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroff6805fc42009-02-23 18:36:16 +00002787 if (Ty->isObjCQualifiedIdType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002788 return true;
2789
Steve Naroffd9e00802008-10-21 18:24:04 +00002790 // Blocks are objects.
2791 if (Ty->isBlockPointerType())
2792 return true;
2793
2794 // All other object types are pointers.
Chris Lattnera008d172009-04-12 23:51:02 +00002795 const PointerType *PT = Ty->getAsPointerType();
2796 if (PT == 0)
Ted Kremenek118930e2008-07-24 23:58:27 +00002797 return false;
2798
Chris Lattnera008d172009-04-12 23:51:02 +00002799 // If this a pointer to an interface (e.g. NSString*), it is ok.
2800 if (PT->getPointeeType()->isObjCInterfaceType() ||
2801 // If is has NSObject attribute, OK as well.
2802 isObjCNSObjectType(Ty))
2803 return true;
2804
Ted Kremenek118930e2008-07-24 23:58:27 +00002805 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2806 // pointer types. This looks for the typedef specifically, not for the
Chris Lattnera008d172009-04-12 23:51:02 +00002807 // underlying type. Iteratively strip off typedefs so that we can handle
2808 // typedefs of typedefs.
2809 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2810 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2811 Ty.getUnqualifiedType() == getObjCClassType())
2812 return true;
2813
2814 Ty = TDT->getDecl()->getUnderlyingType();
2815 }
Ted Kremenek118930e2008-07-24 23:58:27 +00002816
Chris Lattnera008d172009-04-12 23:51:02 +00002817 return false;
Ted Kremenek118930e2008-07-24 23:58:27 +00002818}
2819
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002820/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2821/// garbage collection attribute.
2822///
2823QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002824 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002825 if (getLangOptions().ObjC1 &&
2826 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002827 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002828 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002829 // (or pointers to them) be treated as though they were declared
2830 // as __strong.
2831 if (GCAttrs == QualType::GCNone) {
2832 if (isObjCObjectPointerType(Ty))
2833 GCAttrs = QualType::Strong;
2834 else if (Ty->isPointerType())
2835 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2836 }
Fariborz Jahaniand7b01972009-04-11 00:00:54 +00002837 // Non-pointers have none gc'able attribute regardless of the attribute
2838 // set on them.
Fariborz Jahanian717e4fa2009-06-02 18:32:00 +00002839 else if (!Ty->isPointerType() && !isObjCObjectPointerType(Ty))
Fariborz Jahaniand7b01972009-04-11 00:00:54 +00002840 return QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002841 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002842 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002843}
2844
Chris Lattner6ff358b2008-04-07 06:51:04 +00002845//===----------------------------------------------------------------------===//
2846// Type Compatibility Testing
2847//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002848
Chris Lattner6ff358b2008-04-07 06:51:04 +00002849/// areCompatVectorTypes - Return true if the two specified vector types are
2850/// compatible.
2851static bool areCompatVectorTypes(const VectorType *LHS,
2852 const VectorType *RHS) {
2853 assert(LHS->isCanonical() && RHS->isCanonical());
2854 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002855 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002856}
2857
Eli Friedman0d9549b2008-08-22 00:56:42 +00002858/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002859/// compatible for assignment from RHS to LHS. This handles validation of any
2860/// protocol qualifiers on the LHS or RHS.
2861///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002862bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2863 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002864 // Verify that the base decls are compatible: the RHS must be a subclass of
2865 // the LHS.
2866 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2867 return false;
2868
2869 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2870 // protocol qualified at all, then we are good.
2871 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2872 return true;
2873
2874 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2875 // isn't a superset.
2876 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2877 return true; // FIXME: should return false!
2878
2879 // Finally, we must have two protocol-qualified interfaces.
2880 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2881 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ff358b2008-04-07 06:51:04 +00002882
Steve Naroff98e71b82009-03-01 16:12:44 +00002883 // All LHS protocols must have a presence on the RHS.
2884 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ff358b2008-04-07 06:51:04 +00002885
Steve Naroff98e71b82009-03-01 16:12:44 +00002886 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2887 LHSPE = LHSP->qual_end();
2888 LHSPI != LHSPE; LHSPI++) {
2889 bool RHSImplementsProtocol = false;
2890
2891 // If the RHS doesn't implement the protocol on the left, the types
2892 // are incompatible.
2893 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2894 RHSPE = RHSP->qual_end();
2895 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2896 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2897 RHSImplementsProtocol = true;
2898 }
2899 // FIXME: For better diagnostics, consider passing back the protocol name.
2900 if (!RHSImplementsProtocol)
2901 return false;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002902 }
Steve Naroff98e71b82009-03-01 16:12:44 +00002903 // The RHS implements all protocols listed on the LHS.
2904 return true;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002905}
2906
Steve Naroff17c03822009-02-12 17:52:19 +00002907bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2908 // get the "pointed to" types
2909 const PointerType *LHSPT = LHS->getAsPointerType();
2910 const PointerType *RHSPT = RHS->getAsPointerType();
2911
2912 if (!LHSPT || !RHSPT)
2913 return false;
2914
2915 QualType lhptee = LHSPT->getPointeeType();
2916 QualType rhptee = RHSPT->getPointeeType();
2917 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2918 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2919 // ID acts sort of like void* for ObjC interfaces
2920 if (LHSIface && isObjCIdStructType(rhptee))
2921 return true;
2922 if (RHSIface && isObjCIdStructType(lhptee))
2923 return true;
2924 if (!LHSIface || !RHSIface)
2925 return false;
2926 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2927 canAssignObjCInterfaces(RHSIface, LHSIface);
2928}
2929
Steve Naroff85f0dc52007-10-15 20:41:53 +00002930/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2931/// both shall have the identically qualified version of a compatible type.
2932/// C99 6.2.7p1: Two types have compatible types if their types are the
2933/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002934bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2935 return !mergeTypes(LHS, RHS).isNull();
2936}
2937
2938QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2939 const FunctionType *lbase = lhs->getAsFunctionType();
2940 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002941 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2942 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002943 bool allLTypes = true;
2944 bool allRTypes = true;
2945
2946 // Check return type
2947 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2948 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002949 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2950 allLTypes = false;
2951 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2952 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002953
2954 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl2767d882009-05-27 22:11:52 +00002955 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
2956 "C++ shouldn't be here");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002957 unsigned lproto_nargs = lproto->getNumArgs();
2958 unsigned rproto_nargs = rproto->getNumArgs();
2959
2960 // Compatible functions must have the same number of arguments
2961 if (lproto_nargs != rproto_nargs)
2962 return QualType();
2963
2964 // Variadic and non-variadic functions aren't compatible
2965 if (lproto->isVariadic() != rproto->isVariadic())
2966 return QualType();
2967
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002968 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2969 return QualType();
2970
Eli Friedman0d9549b2008-08-22 00:56:42 +00002971 // Check argument compatibility
2972 llvm::SmallVector<QualType, 10> types;
2973 for (unsigned i = 0; i < lproto_nargs; i++) {
2974 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2975 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2976 QualType argtype = mergeTypes(largtype, rargtype);
2977 if (argtype.isNull()) return QualType();
2978 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002979 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2980 allLTypes = false;
2981 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2982 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002983 }
2984 if (allLTypes) return lhs;
2985 if (allRTypes) return rhs;
2986 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002987 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002988 }
2989
2990 if (lproto) allRTypes = false;
2991 if (rproto) allLTypes = false;
2992
Douglas Gregor4fa58902009-02-26 23:50:07 +00002993 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002994 if (proto) {
Sebastian Redl2767d882009-05-27 22:11:52 +00002995 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002996 if (proto->isVariadic()) return QualType();
2997 // Check that the types are compatible with the types that
2998 // would result from default argument promotions (C99 6.7.5.3p15).
2999 // The only types actually affected are promotable integer
3000 // types and floats, which would be passed as a different
3001 // type depending on whether the prototype is visible.
3002 unsigned proto_nargs = proto->getNumArgs();
3003 for (unsigned i = 0; i < proto_nargs; ++i) {
3004 QualType argTy = proto->getArgType(i);
3005 if (argTy->isPromotableIntegerType() ||
3006 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
3007 return QualType();
3008 }
3009
3010 if (allLTypes) return lhs;
3011 if (allRTypes) return rhs;
3012 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00003013 proto->getNumArgs(), lproto->isVariadic(),
3014 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00003015 }
3016
3017 if (allLTypes) return lhs;
3018 if (allRTypes) return rhs;
Douglas Gregor4fa58902009-02-26 23:50:07 +00003019 return getFunctionNoProtoType(retType);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003020}
3021
3022QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00003023 // C++ [expr]: If an expression initially has the type "reference to T", the
3024 // type is adjusted to "T" prior to any further analysis, the expression
3025 // designates the object or function denoted by the reference, and the
Sebastian Redlce6fff02009-03-16 23:22:08 +00003026 // expression is an lvalue unless the reference is an rvalue reference and
3027 // the expression is a function call (possibly inside parentheses).
Eli Friedman0d9549b2008-08-22 00:56:42 +00003028 // FIXME: C++ shouldn't be going through here! The rules are different
3029 // enough that they should be handled separately.
Sebastian Redlce6fff02009-03-16 23:22:08 +00003030 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3031 // shouldn't be going through here!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003032 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00003033 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00003034 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00003035 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00003036
Eli Friedman0d9549b2008-08-22 00:56:42 +00003037 QualType LHSCan = getCanonicalType(LHS),
3038 RHSCan = getCanonicalType(RHS);
3039
3040 // If two types are identical, they are compatible.
3041 if (LHSCan == RHSCan)
3042 return LHS;
3043
3044 // If the qualifiers are different, the types aren't compatible
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003045 // Note that we handle extended qualifiers later, in the
3046 // case for ExtQualType.
3047 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman0d9549b2008-08-22 00:56:42 +00003048 return QualType();
3049
Eli Friedmanaeae1ce2009-06-01 01:22:52 +00003050 Type::TypeClass LHSClass = LHSCan->getTypeClass();
3051 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman0d9549b2008-08-22 00:56:42 +00003052
Chris Lattnerc38d4522008-01-14 05:45:46 +00003053 // We want to consider the two function types to be the same for these
3054 // comparisons, just force one to the other.
3055 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3056 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00003057
Eli Friedmande43bf62009-06-02 05:28:56 +00003058 // Strip off objc_gc attributes off the top level so they can be merged.
3059 // This is a complete mess, but the attribute itself doesn't make much sense.
Fariborz Jahanian85d91262009-06-02 01:40:22 +00003060 if (RHSClass == Type::ExtQual) {
Eli Friedmande43bf62009-06-02 05:28:56 +00003061 QualType::GCAttrTypes GCAttr = RHSCan.getObjCGCAttr();
3062 if (GCAttr != QualType::GCNone) {
Fariborz Jahanian562d0ee2009-06-02 20:58:58 +00003063 QualType::GCAttrTypes GCLHSAttr = LHSCan.getObjCGCAttr();
Fariborz Jahanian717e4fa2009-06-02 18:32:00 +00003064 // __weak attribute must appear on both declarations.
Fariborz Jahanian562d0ee2009-06-02 20:58:58 +00003065 // __strong attribue is redundant if other decl is an objective-c
3066 // object pointer (or decorated with __strong attribute); otherwise
3067 // issue error.
3068 if ((GCAttr == QualType::Weak && GCLHSAttr != GCAttr) ||
3069 (GCAttr == QualType::Strong && GCLHSAttr != GCAttr &&
3070 LHSCan->isPointerType() && !isObjCObjectPointerType(LHSCan) &&
3071 !isObjCIdStructType(LHSCan->getAsPointerType()->getPointeeType())))
Fariborz Jahanian717e4fa2009-06-02 18:32:00 +00003072 return QualType();
3073
Eli Friedmande43bf62009-06-02 05:28:56 +00003074 RHS = QualType(cast<ExtQualType>(RHS.getDesugaredType())->getBaseType(),
3075 RHS.getCVRQualifiers());
3076 QualType Result = mergeTypes(LHS, RHS);
Fariborz Jahanian717e4fa2009-06-02 18:32:00 +00003077 if (!Result.isNull()) {
3078 if (Result.getObjCGCAttr() == QualType::GCNone)
3079 Result = getObjCGCQualType(Result, GCAttr);
3080 else if (Result.getObjCGCAttr() != GCAttr)
3081 Result = QualType();
3082 }
Eli Friedmande43bf62009-06-02 05:28:56 +00003083 return Result;
3084 }
Fariborz Jahanian85d91262009-06-02 01:40:22 +00003085 }
Fariborz Jahanian85d91262009-06-02 01:40:22 +00003086 if (LHSClass == Type::ExtQual) {
Eli Friedmande43bf62009-06-02 05:28:56 +00003087 QualType::GCAttrTypes GCAttr = LHSCan.getObjCGCAttr();
3088 if (GCAttr != QualType::GCNone) {
Fariborz Jahanian717e4fa2009-06-02 18:32:00 +00003089 QualType::GCAttrTypes GCRHSAttr = RHSCan.getObjCGCAttr();
3090 // __weak attribute must appear on both declarations. __strong
Fariborz Jahanian562d0ee2009-06-02 20:58:58 +00003091 // __strong attribue is redundant if other decl is an objective-c
3092 // object pointer (or decorated with __strong attribute); otherwise
3093 // issue error.
3094 if ((GCAttr == QualType::Weak && GCRHSAttr != GCAttr) ||
3095 (GCAttr == QualType::Strong && GCRHSAttr != GCAttr &&
3096 RHSCan->isPointerType() && !isObjCObjectPointerType(RHSCan) &&
3097 !isObjCIdStructType(RHSCan->getAsPointerType()->getPointeeType())))
Fariborz Jahanian717e4fa2009-06-02 18:32:00 +00003098 return QualType();
Fariborz Jahanian562d0ee2009-06-02 20:58:58 +00003099
Eli Friedmande43bf62009-06-02 05:28:56 +00003100 LHS = QualType(cast<ExtQualType>(LHS.getDesugaredType())->getBaseType(),
3101 LHS.getCVRQualifiers());
3102 QualType Result = mergeTypes(LHS, RHS);
Fariborz Jahanian717e4fa2009-06-02 18:32:00 +00003103 if (!Result.isNull()) {
3104 if (Result.getObjCGCAttr() == QualType::GCNone)
3105 Result = getObjCGCQualType(Result, GCAttr);
3106 else if (Result.getObjCGCAttr() != GCAttr)
3107 Result = QualType();
3108 }
Eli Friedman430d9f12009-06-02 07:45:37 +00003109 return Result;
Eli Friedmande43bf62009-06-02 05:28:56 +00003110 }
Fariborz Jahanian85d91262009-06-02 01:40:22 +00003111 }
3112
Eli Friedman398837e2008-02-12 08:23:06 +00003113 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00003114 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3115 LHSClass = Type::ConstantArray;
3116 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3117 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003118
Nate Begemanaf6ed502008-04-18 23:10:10 +00003119 // Canonicalize ExtVector -> Vector.
3120 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3121 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00003122
Chris Lattner7cdcb252008-04-07 06:38:24 +00003123 // Consider qualified interfaces and interfaces the same.
3124 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
3125 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003126
Chris Lattnerb5709e22008-04-07 05:43:21 +00003127 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00003128 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003129 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3130 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanian0dc684e2009-04-15 21:54:48 +00003131
Steve Naroff0773c582009-04-14 15:11:46 +00003132 // 'id' and 'Class' act sort of like void* for ObjC interfaces
3133 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00003134 return LHS;
Steve Naroff0773c582009-04-14 15:11:46 +00003135 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00003136 return RHS;
3137
Steve Naroff28ceff72008-12-10 22:14:21 +00003138 // ID is compatible with all qualified id types.
3139 if (LHS->isObjCQualifiedIdType()) {
3140 if (const PointerType *PT = RHS->getAsPointerType()) {
3141 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00003142 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00003143 return LHS;
3144 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3145 // Unfortunately, this API is part of Sema (which we don't have access
3146 // to. Need to refactor. The following check is insufficient, since we
3147 // need to make sure the class implements the protocol.
3148 if (pType->isObjCInterfaceType())
3149 return LHS;
3150 }
3151 }
3152 if (RHS->isObjCQualifiedIdType()) {
3153 if (const PointerType *PT = LHS->getAsPointerType()) {
3154 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00003155 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00003156 return RHS;
3157 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3158 // Unfortunately, this API is part of Sema (which we don't have access
3159 // to. Need to refactor. The following check is insufficient, since we
3160 // need to make sure the class implements the protocol.
3161 if (pType->isObjCInterfaceType())
3162 return RHS;
3163 }
3164 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003165 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3166 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003167 if (const EnumType* ETy = LHS->getAsEnumType()) {
3168 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3169 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00003170 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003171 if (const EnumType* ETy = RHS->getAsEnumType()) {
3172 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3173 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00003174 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003175
Eli Friedman0d9549b2008-08-22 00:56:42 +00003176 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003177 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003178
Steve Naroffc88babe2008-01-09 22:43:08 +00003179 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00003180 switch (LHSClass) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00003181#define TYPE(Class, Base)
3182#define ABSTRACT_TYPE(Class, Base)
3183#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3184#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3185#include "clang/AST/TypeNodes.def"
3186 assert(false && "Non-canonical and dependent types shouldn't get here");
3187 return QualType();
3188
Sebastian Redlce6fff02009-03-16 23:22:08 +00003189 case Type::LValueReference:
3190 case Type::RValueReference:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003191 case Type::MemberPointer:
3192 assert(false && "C++ should never be in mergeTypes");
3193 return QualType();
3194
3195 case Type::IncompleteArray:
3196 case Type::VariableArray:
3197 case Type::FunctionProto:
3198 case Type::ExtVector:
3199 case Type::ObjCQualifiedInterface:
3200 assert(false && "Types are eliminated above");
3201 return QualType();
3202
Chris Lattnerc38d4522008-01-14 05:45:46 +00003203 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003204 {
3205 // Merge two pointer types, while trying to preserve typedef info
3206 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3207 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3208 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3209 if (ResultType.isNull()) return QualType();
Eli Friedmande43bf62009-06-02 05:28:56 +00003210 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003211 return LHS;
Eli Friedmande43bf62009-06-02 05:28:56 +00003212 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003213 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003214 return getPointerType(ResultType);
3215 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00003216 case Type::BlockPointer:
3217 {
3218 // Merge two block pointer types, while trying to preserve typedef info
3219 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3220 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3221 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3222 if (ResultType.isNull()) return QualType();
3223 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3224 return LHS;
3225 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3226 return RHS;
3227 return getBlockPointerType(ResultType);
3228 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003229 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003230 {
3231 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3232 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3233 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3234 return QualType();
3235
3236 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3237 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3238 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3239 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003240 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3241 return LHS;
3242 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3243 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003244 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3245 ArrayType::ArraySizeModifier(), 0);
3246 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3247 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003248 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3249 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003250 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3251 return LHS;
3252 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3253 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003254 if (LVAT) {
3255 // FIXME: This isn't correct! But tricky to implement because
3256 // the array's size has to be the size of LHS, but the type
3257 // has to be different.
3258 return LHS;
3259 }
3260 if (RVAT) {
3261 // FIXME: This isn't correct! But tricky to implement because
3262 // the array's size has to be the size of RHS, but the type
3263 // has to be different.
3264 return RHS;
3265 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003266 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3267 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003268 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003269 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003270 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003271 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor4fa58902009-02-26 23:50:07 +00003272 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003273 case Type::Enum:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003274 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00003275 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3276 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003277 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00003278 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003279 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003280 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00003281 case Type::Complex:
3282 // Distinct complex types are incompatible.
3283 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003284 case Type::Vector:
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003285 // FIXME: The merged type should be an ExtVector!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003286 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3287 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003288 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003289 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003290 // Check if the interfaces are assignment compatible.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003291 // FIXME: This should be type compatibility, e.g. whether
3292 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff0bbc1352009-02-21 16:18:07 +00003293 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3294 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3295 if (LHSIface && RHSIface &&
3296 canAssignObjCInterfaces(LHSIface, RHSIface))
3297 return LHS;
3298
Eli Friedman0d9549b2008-08-22 00:56:42 +00003299 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003300 }
Steve Naroff28ceff72008-12-10 22:14:21 +00003301 case Type::ObjCQualifiedId:
3302 // Distinct qualified id's are not compatible.
3303 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003304 case Type::FixedWidthInt:
3305 // Distinct fixed-width integers are not compatible.
3306 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003307 case Type::ExtQual:
3308 // FIXME: ExtQual types can be compatible even if they're not
3309 // identical!
3310 return QualType();
3311 // First attempt at an implementation, but I'm not really sure it's
3312 // right...
3313#if 0
3314 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3315 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3316 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3317 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3318 return QualType();
3319 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3320 LHSBase = QualType(LQual->getBaseType(), 0);
3321 RHSBase = QualType(RQual->getBaseType(), 0);
3322 ResultType = mergeTypes(LHSBase, RHSBase);
3323 if (ResultType.isNull()) return QualType();
3324 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3325 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3326 return LHS;
3327 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3328 return RHS;
3329 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3330 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3331 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3332 return ResultType;
3333#endif
Douglas Gregordd13e842009-03-30 22:58:21 +00003334
3335 case Type::TemplateSpecialization:
3336 assert(false && "Dependent types have no size");
3337 break;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003338 }
Douglas Gregor4fa58902009-02-26 23:50:07 +00003339
3340 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003341}
Ted Kremenek738e6c02007-10-31 17:10:13 +00003342
Chris Lattner1d78a862008-04-07 07:01:58 +00003343//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00003344// Integer Predicates
3345//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00003346
Eli Friedman0832dbc2008-06-28 06:23:08 +00003347unsigned ASTContext::getIntWidth(QualType T) {
3348 if (T == BoolTy)
3349 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00003350 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3351 return FWIT->getWidth();
3352 }
3353 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00003354 return (unsigned)getTypeSize(T);
3355}
3356
3357QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3358 assert(T->isSignedIntegerType() && "Unexpected type");
3359 if (const EnumType* ETy = T->getAsEnumType())
3360 T = ETy->getDecl()->getIntegerType();
3361 const BuiltinType* BTy = T->getAsBuiltinType();
3362 assert (BTy && "Unexpected signed integer type");
3363 switch (BTy->getKind()) {
3364 case BuiltinType::Char_S:
3365 case BuiltinType::SChar:
3366 return UnsignedCharTy;
3367 case BuiltinType::Short:
3368 return UnsignedShortTy;
3369 case BuiltinType::Int:
3370 return UnsignedIntTy;
3371 case BuiltinType::Long:
3372 return UnsignedLongTy;
3373 case BuiltinType::LongLong:
3374 return UnsignedLongLongTy;
Chris Lattner6cc7e412009-04-30 02:43:43 +00003375 case BuiltinType::Int128:
3376 return UnsignedInt128Ty;
Eli Friedman0832dbc2008-06-28 06:23:08 +00003377 default:
3378 assert(0 && "Unexpected signed integer type");
3379 return QualType();
3380 }
3381}
3382
Douglas Gregorc34897d2009-04-09 22:27:44 +00003383ExternalASTSource::~ExternalASTSource() { }
3384
3385void ExternalASTSource::PrintStats() { }
Chris Lattner260ad502009-06-14 00:45:47 +00003386
3387
3388//===----------------------------------------------------------------------===//
3389// Builtin Type Computation
3390//===----------------------------------------------------------------------===//
3391
3392/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
3393/// pointer over the consumed characters. This returns the resultant type.
3394static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
3395 ASTContext::GetBuiltinTypeError &Error,
3396 bool AllowTypeModifiers = true) {
3397 // Modifiers.
3398 int HowLong = 0;
3399 bool Signed = false, Unsigned = false;
3400
3401 // Read the modifiers first.
3402 bool Done = false;
3403 while (!Done) {
3404 switch (*Str++) {
3405 default: Done = true; --Str; break;
3406 case 'S':
3407 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
3408 assert(!Signed && "Can't use 'S' modifier multiple times!");
3409 Signed = true;
3410 break;
3411 case 'U':
3412 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
3413 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
3414 Unsigned = true;
3415 break;
3416 case 'L':
3417 assert(HowLong <= 2 && "Can't have LLLL modifier");
3418 ++HowLong;
3419 break;
3420 }
3421 }
3422
3423 QualType Type;
3424
3425 // Read the base type.
3426 switch (*Str++) {
3427 default: assert(0 && "Unknown builtin type letter!");
3428 case 'v':
3429 assert(HowLong == 0 && !Signed && !Unsigned &&
3430 "Bad modifiers used with 'v'!");
3431 Type = Context.VoidTy;
3432 break;
3433 case 'f':
3434 assert(HowLong == 0 && !Signed && !Unsigned &&
3435 "Bad modifiers used with 'f'!");
3436 Type = Context.FloatTy;
3437 break;
3438 case 'd':
3439 assert(HowLong < 2 && !Signed && !Unsigned &&
3440 "Bad modifiers used with 'd'!");
3441 if (HowLong)
3442 Type = Context.LongDoubleTy;
3443 else
3444 Type = Context.DoubleTy;
3445 break;
3446 case 's':
3447 assert(HowLong == 0 && "Bad modifiers used with 's'!");
3448 if (Unsigned)
3449 Type = Context.UnsignedShortTy;
3450 else
3451 Type = Context.ShortTy;
3452 break;
3453 case 'i':
3454 if (HowLong == 3)
3455 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
3456 else if (HowLong == 2)
3457 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
3458 else if (HowLong == 1)
3459 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
3460 else
3461 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
3462 break;
3463 case 'c':
3464 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
3465 if (Signed)
3466 Type = Context.SignedCharTy;
3467 else if (Unsigned)
3468 Type = Context.UnsignedCharTy;
3469 else
3470 Type = Context.CharTy;
3471 break;
3472 case 'b': // boolean
3473 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
3474 Type = Context.BoolTy;
3475 break;
3476 case 'z': // size_t.
3477 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
3478 Type = Context.getSizeType();
3479 break;
3480 case 'F':
3481 Type = Context.getCFConstantStringType();
3482 break;
3483 case 'a':
3484 Type = Context.getBuiltinVaListType();
3485 assert(!Type.isNull() && "builtin va list type not initialized!");
3486 break;
3487 case 'A':
3488 // This is a "reference" to a va_list; however, what exactly
3489 // this means depends on how va_list is defined. There are two
3490 // different kinds of va_list: ones passed by value, and ones
3491 // passed by reference. An example of a by-value va_list is
3492 // x86, where va_list is a char*. An example of by-ref va_list
3493 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
3494 // we want this argument to be a char*&; for x86-64, we want
3495 // it to be a __va_list_tag*.
3496 Type = Context.getBuiltinVaListType();
3497 assert(!Type.isNull() && "builtin va list type not initialized!");
3498 if (Type->isArrayType()) {
3499 Type = Context.getArrayDecayedType(Type);
3500 } else {
3501 Type = Context.getLValueReferenceType(Type);
3502 }
3503 break;
3504 case 'V': {
3505 char *End;
3506
3507 unsigned NumElements = strtoul(Str, &End, 10);
3508 assert(End != Str && "Missing vector size");
3509
3510 Str = End;
3511
3512 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
3513 Type = Context.getVectorType(ElementType, NumElements);
3514 break;
3515 }
3516 case 'P': {
3517 IdentifierInfo *II = &Context.Idents.get("FILE");
3518 DeclContext::lookup_result Lookup
3519 = Context.getTranslationUnitDecl()->lookup(Context, II);
3520 if (Lookup.first != Lookup.second && isa<TypeDecl>(*Lookup.first)) {
3521 Type = Context.getTypeDeclType(cast<TypeDecl>(*Lookup.first));
3522 break;
3523 }
3524 else {
3525 Error = ASTContext::GE_Missing_FILE;
3526 return QualType();
3527 }
3528 }
3529 }
3530
3531 if (!AllowTypeModifiers)
3532 return Type;
3533
3534 Done = false;
3535 while (!Done) {
3536 switch (*Str++) {
3537 default: Done = true; --Str; break;
3538 case '*':
3539 Type = Context.getPointerType(Type);
3540 break;
3541 case '&':
3542 Type = Context.getLValueReferenceType(Type);
3543 break;
3544 // FIXME: There's no way to have a built-in with an rvalue ref arg.
3545 case 'C':
3546 Type = Type.getQualifiedType(QualType::Const);
3547 break;
3548 }
3549 }
3550
3551 return Type;
3552}
3553
3554/// GetBuiltinType - Return the type for the specified builtin.
3555QualType ASTContext::GetBuiltinType(unsigned id,
3556 GetBuiltinTypeError &Error) {
3557 const char *TypeStr = BuiltinInfo.GetTypeString(id);
3558
3559 llvm::SmallVector<QualType, 8> ArgTypes;
3560
3561 Error = GE_None;
3562 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
3563 if (Error != GE_None)
3564 return QualType();
3565 while (TypeStr[0] && TypeStr[0] != '.') {
3566 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
3567 if (Error != GE_None)
3568 return QualType();
3569
3570 // Do array -> pointer decay. The builtin should use the decayed type.
3571 if (Ty->isArrayType())
3572 Ty = getArrayDecayedType(Ty);
3573
3574 ArgTypes.push_back(Ty);
3575 }
3576
3577 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
3578 "'.' should only occur at end of builtin type list!");
3579
3580 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
3581 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
3582 return getFunctionNoProtoType(ResType);
3583 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
3584 TypeStr[0] == '.', 0);
3585}