blob: e6dea7cca955182e2e5104a9e447dd18de818450 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000019#include "clang/AST/ExternalASTSource.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000021#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000023#include "llvm/ADT/StringExtras.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000025#include "llvm/Support/MemoryBuffer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner61710852008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Douglas Gregor2deaea32009-04-22 18:49:13 +000035 bool FreeMem, unsigned size_reserve,
36 bool InitializeBuiltins) :
Douglas Gregorab452ba2009-03-26 23:50:42 +000037 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
38 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Douglas Gregor2cf26342009-04-09 22:27:44 +000039 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels),
40 ExternalSource(0) {
Daniel Dunbare91593e2008-08-11 04:54:23 +000041 if (size_reserve > 0) Types.reserve(size_reserve);
42 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
Douglas Gregor7a9cbed2009-04-26 03:57:37 +000044 BuiltinInfo.InitializeTargetBuiltins(Target);
Douglas Gregor2deaea32009-04-22 18:49:13 +000045 if (InitializeBuiltins)
46 this->InitializeBuiltins(idents);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000047 PrintingPolicy.CPlusPlus = LangOpts.CPlusPlus;
Daniel Dunbare91593e2008-08-11 04:54:23 +000048}
49
Reid Spencer5f016e22007-07-11 17:01:13 +000050ASTContext::~ASTContext() {
51 // Deallocate all the types.
52 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000053 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000054 Types.pop_back();
55 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000056
Nuno Lopesb74668e2008-12-17 22:30:25 +000057 {
58 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
59 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
60 while (I != E) {
61 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
62 delete R;
63 }
64 }
65
66 {
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +000067 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
68 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopesb74668e2008-12-17 22:30:25 +000069 while (I != E) {
70 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
71 delete R;
72 }
73 }
74
Douglas Gregorab452ba2009-03-26 23:50:42 +000075 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000076 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
77 NNS = NestedNameSpecifiers.begin(),
78 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregore7dcd782009-03-27 23:25:45 +000079 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000080 /* Increment in loop */)
81 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000082
83 if (GlobalNestedNameSpecifier)
84 GlobalNestedNameSpecifier->Destroy(*this);
85
Eli Friedmanb26153c2008-05-27 03:08:09 +000086 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000087}
88
Douglas Gregor2deaea32009-04-22 18:49:13 +000089void ASTContext::InitializeBuiltins(IdentifierTable &idents) {
Douglas Gregor2deaea32009-04-22 18:49:13 +000090 BuiltinInfo.InitializeBuiltins(idents, LangOpts.NoBuiltin);
91}
92
Douglas Gregor2cf26342009-04-09 22:27:44 +000093void
94ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
95 ExternalSource.reset(Source.take());
96}
97
Reid Spencer5f016e22007-07-11 17:01:13 +000098void ASTContext::PrintStats() const {
99 fprintf(stderr, "*** AST Context Stats:\n");
100 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000101
Douglas Gregordbe833d2009-05-26 14:40:08 +0000102 unsigned counts[] = {
103#define TYPE(Name, Parent) 0,
104#define ABSTRACT_TYPE(Name, Parent)
105#include "clang/AST/TypeNodes.def"
106 0 // Extra
107 };
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000108
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
110 Type *T = Types[i];
Douglas Gregordbe833d2009-05-26 14:40:08 +0000111 counts[(unsigned)T->getTypeClass()]++;
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 }
113
Douglas Gregordbe833d2009-05-26 14:40:08 +0000114 unsigned Idx = 0;
115 unsigned TotalBytes = 0;
116#define TYPE(Name, Parent) \
117 if (counts[Idx]) \
118 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
119 TotalBytes += counts[Idx] * sizeof(Name##Type); \
120 ++Idx;
121#define ABSTRACT_TYPE(Name, Parent)
122#include "clang/AST/TypeNodes.def"
123
124 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000125
126 if (ExternalSource.get()) {
127 fprintf(stderr, "\n");
128 ExternalSource->PrintStats();
129 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000130}
131
132
133void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000134 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000135}
136
Reid Spencer5f016e22007-07-11 17:01:13 +0000137void ASTContext::InitBuiltinTypes() {
138 assert(VoidTy.isNull() && "Context reinitialized?");
139
140 // C99 6.2.5p19.
141 InitBuiltinType(VoidTy, BuiltinType::Void);
142
143 // C99 6.2.5p2.
144 InitBuiltinType(BoolTy, BuiltinType::Bool);
145 // C99 6.2.5p3.
Eli Friedman15b91762009-06-05 07:05:05 +0000146 if (LangOpts.CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 InitBuiltinType(CharTy, BuiltinType::Char_S);
148 else
149 InitBuiltinType(CharTy, BuiltinType::Char_U);
150 // C99 6.2.5p4.
151 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
152 InitBuiltinType(ShortTy, BuiltinType::Short);
153 InitBuiltinType(IntTy, BuiltinType::Int);
154 InitBuiltinType(LongTy, BuiltinType::Long);
155 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
156
157 // C99 6.2.5p6.
158 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
159 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
160 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
161 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
162 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
163
164 // C99 6.2.5p10.
165 InitBuiltinType(FloatTy, BuiltinType::Float);
166 InitBuiltinType(DoubleTy, BuiltinType::Double);
167 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000168
Chris Lattner2df9ced2009-04-30 02:43:43 +0000169 // GNU extension, 128-bit integers.
170 InitBuiltinType(Int128Ty, BuiltinType::Int128);
171 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
172
Chris Lattner3a250322009-02-26 23:43:47 +0000173 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
174 InitBuiltinType(WCharTy, BuiltinType::WChar);
175 else // C99
176 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000177
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000178 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000179 InitBuiltinType(OverloadTy, BuiltinType::Overload);
180
181 // Placeholder type for type-dependent expressions whose type is
182 // completely unknown. No code should ever check a type against
183 // DependentTy and users should never see it; however, it is here to
184 // help diagnose failures to properly check for type-dependent
185 // expressions.
186 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000187
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 // C99 6.2.5p11.
189 FloatComplexTy = getComplexType(FloatTy);
190 DoubleComplexTy = getComplexType(DoubleTy);
191 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000192
Steve Naroff7e219e42007-10-15 14:41:52 +0000193 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000194 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000195 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000196 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000197 ClassStructType = 0;
198
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000199 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000200
201 // void * type
202 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000203
204 // nullptr type (C++0x 2.14.7)
205 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000206}
207
Chris Lattner464175b2007-07-18 17:52:12 +0000208//===----------------------------------------------------------------------===//
209// Type Sizing and Analysis
210//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000211
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000212/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
213/// scalar floating point type.
214const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
215 const BuiltinType *BT = T->getAsBuiltinType();
216 assert(BT && "Not a floating point type!");
217 switch (BT->getKind()) {
218 default: assert(0 && "Not a floating point type!");
219 case BuiltinType::Float: return Target.getFloatFormat();
220 case BuiltinType::Double: return Target.getDoubleFormat();
221 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
222 }
223}
224
Chris Lattneraf707ab2009-01-24 21:53:27 +0000225/// getDeclAlign - Return a conservative estimate of the alignment of the
226/// specified decl. Note that bitfields do not have a valid alignment, so
227/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000228unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000229 unsigned Align = Target.getCharWidth();
230
231 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
232 Align = std::max(Align, AA->getAlignment());
233
Chris Lattneraf707ab2009-01-24 21:53:27 +0000234 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
235 QualType T = VD->getType();
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000236 if (const ReferenceType* RT = T->getAsReferenceType()) {
237 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000238 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000239 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
240 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000241 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
242 T = cast<ArrayType>(T)->getElementType();
243
244 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
245 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000246 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000247
248 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000249}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000250
Chris Lattnera7674d82007-07-13 22:13:22 +0000251/// getTypeSize - Return the size of the specified type, in bits. This method
252/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000253std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000254ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000255 uint64_t Width=0;
256 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000257 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000258#define TYPE(Class, Base)
259#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000260#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000261#define DEPENDENT_TYPE(Class, Base) case Type::Class:
262#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000263 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000264 break;
265
Chris Lattner692233e2007-07-13 22:27:08 +0000266 case Type::FunctionNoProto:
267 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000268 // GCC extension: alignof(function) = 32 bits
269 Width = 0;
270 Align = 32;
271 break;
272
Douglas Gregor72564e72009-02-26 23:50:07 +0000273 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000274 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000275 Width = 0;
276 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
277 break;
278
Steve Narofffb22d962007-08-30 01:06:46 +0000279 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000280 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000281
Chris Lattner98be4942008-03-05 18:54:05 +0000282 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000283 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000284 Align = EltInfo.second;
285 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000286 }
Nate Begeman213541a2008-04-18 23:10:10 +0000287 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000288 case Type::Vector: {
289 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000290 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000291 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000292 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000293 // If the alignment is not a power of 2, round up to the next power of 2.
294 // This happens for non-power-of-2 length vectors.
295 // FIXME: this should probably be a target property.
296 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000297 break;
298 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000299
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000300 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000301 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000302 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000303 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000304 // GCC extension: alignof(void) = 8 bits.
305 Width = 0;
306 Align = 8;
307 break;
308
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000309 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000310 Width = Target.getBoolWidth();
311 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000312 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000313 case BuiltinType::Char_S:
314 case BuiltinType::Char_U:
315 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000316 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000317 Width = Target.getCharWidth();
318 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000319 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000320 case BuiltinType::WChar:
321 Width = Target.getWCharWidth();
322 Align = Target.getWCharAlign();
323 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000324 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000325 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000326 Width = Target.getShortWidth();
327 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000328 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000329 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000330 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000331 Width = Target.getIntWidth();
332 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000333 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000334 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000335 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000336 Width = Target.getLongWidth();
337 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000338 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000339 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000340 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000341 Width = Target.getLongLongWidth();
342 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000343 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000344 case BuiltinType::Int128:
345 case BuiltinType::UInt128:
346 Width = 128;
347 Align = 128; // int128_t is 128-bit aligned on all targets.
348 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000349 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000350 Width = Target.getFloatWidth();
351 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000352 break;
353 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000354 Width = Target.getDoubleWidth();
355 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000356 break;
357 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000358 Width = Target.getLongDoubleWidth();
359 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000360 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000361 case BuiltinType::NullPtr:
362 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
363 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000364 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000365 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000366 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000367 case Type::FixedWidthInt:
368 // FIXME: This isn't precisely correct; the width/alignment should depend
369 // on the available types for the target
370 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000371 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000372 Align = Width;
373 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000374 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000375 // FIXME: Pointers into different addr spaces could have different sizes and
376 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000377 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000378 case Type::ObjCQualifiedId:
Douglas Gregor72564e72009-02-26 23:50:07 +0000379 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000380 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000381 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000382 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000383 case Type::BlockPointer: {
384 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
385 Width = Target.getPointerWidth(AS);
386 Align = Target.getPointerAlign(AS);
387 break;
388 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000389 case Type::Pointer: {
390 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000391 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000392 Align = Target.getPointerAlign(AS);
393 break;
394 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000395 case Type::LValueReference:
396 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000397 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000398 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000399 // FIXME: This is wrong for struct layout: a reference in a struct has
400 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000401 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000402 case Type::MemberPointer: {
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000403 // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
404 // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
405 // If we ever want to support other ABIs this needs to be abstracted.
406
Sebastian Redlf30208a2009-01-24 21:16:55 +0000407 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000408 std::pair<uint64_t, unsigned> PtrDiffInfo =
409 getTypeInfo(getPointerDiffType());
410 Width = PtrDiffInfo.first;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000411 if (Pointee->isFunctionType())
412 Width *= 2;
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000413 Align = PtrDiffInfo.second;
414 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000415 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000416 case Type::Complex: {
417 // Complex types have the same alignment as their elements, but twice the
418 // size.
419 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000420 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000421 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000422 Align = EltInfo.second;
423 break;
424 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000425 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000426 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000427 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
428 Width = Layout.getSize();
429 Align = Layout.getAlignment();
430 break;
431 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000432 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000433 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000434 const TagType *TT = cast<TagType>(T);
435
436 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000437 Width = 1;
438 Align = 1;
439 break;
440 }
441
Daniel Dunbar1d751182008-11-08 05:48:37 +0000442 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000443 return getTypeInfo(ET->getDecl()->getIntegerType());
444
Daniel Dunbar1d751182008-11-08 05:48:37 +0000445 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000446 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
447 Width = Layout.getSize();
448 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000449 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000450 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000451
Douglas Gregor18857642009-04-30 17:32:17 +0000452 case Type::Typedef: {
453 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
454 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
455 Align = Aligned->getAlignment();
456 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
457 } else
458 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000459 break;
Chris Lattner71763312008-04-06 22:05:18 +0000460 }
Douglas Gregor18857642009-04-30 17:32:17 +0000461
462 case Type::TypeOfExpr:
463 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
464 .getTypePtr());
465
466 case Type::TypeOf:
467 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
468
469 case Type::QualifiedName:
470 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
471
472 case Type::TemplateSpecialization:
473 assert(getCanonicalType(T) != T &&
474 "Cannot request the size of a dependent type");
475 // FIXME: this is likely to be wrong once we support template
476 // aliases, since a template alias could refer to a typedef that
477 // has an __aligned__ attribute on it.
478 return getTypeInfo(getCanonicalType(T));
479 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000480
Chris Lattner464175b2007-07-18 17:52:12 +0000481 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000482 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000483}
484
Chris Lattner34ebde42009-01-27 18:08:34 +0000485/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
486/// type for the current target in bits. This can be different than the ABI
487/// alignment in cases where it is beneficial for performance to overalign
488/// a data type.
489unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
490 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman1eed6022009-05-25 21:27:19 +0000491
492 // Double and long long should be naturally aligned if possible.
493 if (const ComplexType* CT = T->getAsComplexType())
494 T = CT->getElementType().getTypePtr();
495 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
496 T->isSpecificBuiltinType(BuiltinType::LongLong))
497 return std::max(ABIAlign, (unsigned)getTypeSize(T));
498
Chris Lattner34ebde42009-01-27 18:08:34 +0000499 return ABIAlign;
500}
501
502
Devang Patel8b277042008-06-04 21:22:16 +0000503/// LayoutField - Field layout.
504void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000505 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000506 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000507 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000508 uint64_t FieldOffset = IsUnion ? 0 : Size;
509 uint64_t FieldSize;
510 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000511
512 // FIXME: Should this override struct packing? Probably we want to
513 // take the minimum?
514 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
515 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000516
517 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
518 // TODO: Need to check this algorithm on other targets!
519 // (tested on Linux-X86)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000520 FieldSize = BitWidthExpr->EvaluateAsInt(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000521
522 std::pair<uint64_t, unsigned> FieldInfo =
523 Context.getTypeInfo(FD->getType());
524 uint64_t TypeSize = FieldInfo.first;
525
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000526 // Determine the alignment of this bitfield. The packing
527 // attributes define a maximum and the alignment attribute defines
528 // a minimum.
529 // FIXME: What is the right behavior when the specified alignment
530 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000531 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000532 if (FieldPacking)
533 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000534 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
535 FieldAlign = std::max(FieldAlign, AA->getAlignment());
536
537 // Check if we need to add padding to give the field the correct
538 // alignment.
539 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
540 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
541
542 // Padding members don't affect overall alignment
543 if (!FD->getIdentifier())
544 FieldAlign = 1;
545 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000546 if (FD->getType()->isIncompleteArrayType()) {
547 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000548 // query getTypeInfo about these, so we figure it out here.
549 // Flexible array members don't have any size, but they
550 // have to be aligned appropriately for their element type.
551 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000552 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000553 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson2f1169f2009-04-10 05:31:15 +0000554 } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
555 unsigned AS = RT->getPointeeType().getAddressSpace();
556 FieldSize = Context.Target.getPointerWidth(AS);
557 FieldAlign = Context.Target.getPointerAlign(AS);
Devang Patel8b277042008-06-04 21:22:16 +0000558 } else {
559 std::pair<uint64_t, unsigned> FieldInfo =
560 Context.getTypeInfo(FD->getType());
561 FieldSize = FieldInfo.first;
562 FieldAlign = FieldInfo.second;
563 }
564
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000565 // Determine the alignment of this bitfield. The packing
566 // attributes define a maximum and the alignment attribute defines
567 // a minimum. Additionally, the packing alignment must be at least
568 // a byte for non-bitfields.
569 //
570 // FIXME: What is the right behavior when the specified alignment
571 // is smaller than the specified packing?
572 if (FieldPacking)
573 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000574 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
575 FieldAlign = std::max(FieldAlign, AA->getAlignment());
576
577 // Round up the current record size to the field's alignment boundary.
578 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
579 }
580
581 // Place this field at the current location.
582 FieldOffsets[FieldNo] = FieldOffset;
583
584 // Reserve space for this field.
585 if (IsUnion) {
586 Size = std::max(Size, FieldSize);
587 } else {
588 Size = FieldOffset + FieldSize;
589 }
590
Daniel Dunbard6884a02009-05-04 05:16:21 +0000591 // Remember the next available offset.
592 NextOffset = Size;
593
Devang Patel8b277042008-06-04 21:22:16 +0000594 // Remember max struct/class alignment.
595 Alignment = std::max(Alignment, FieldAlign);
596}
597
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000598static void CollectLocalObjCIvars(ASTContext *Ctx,
599 const ObjCInterfaceDecl *OI,
600 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000601 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
602 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000603 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000604 if (!IVDecl->isInvalidDecl())
605 Fields.push_back(cast<FieldDecl>(IVDecl));
606 }
607}
608
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000609void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
610 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
611 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
612 CollectObjCIvars(SuperClass, Fields);
613 CollectLocalObjCIvars(this, OI, Fields);
614}
615
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000616/// ShallowCollectObjCIvars -
617/// Collect all ivars, including those synthesized, in the current class.
618///
619void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
620 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars,
621 bool CollectSynthesized) {
622 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
623 E = OI->ivar_end(); I != E; ++I) {
624 Ivars.push_back(*I);
625 }
626 if (CollectSynthesized)
627 CollectSynthesizedIvars(OI, Ivars);
628}
629
Fariborz Jahanian98200742009-05-12 18:14:29 +0000630void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
631 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
632 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(*this),
633 E = PD->prop_end(*this); I != E; ++I)
634 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
635 Ivars.push_back(Ivar);
636
637 // Also look into nested protocols.
638 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
639 E = PD->protocol_end(); P != E; ++P)
640 CollectProtocolSynthesizedIvars(*P, Ivars);
641}
642
643/// CollectSynthesizedIvars -
644/// This routine collect synthesized ivars for the designated class.
645///
646void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
647 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
648 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*this),
649 E = OI->prop_end(*this); I != E; ++I) {
650 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
651 Ivars.push_back(Ivar);
652 }
653 // Also look into interface's protocol list for properties declared
654 // in the protocol and whose ivars are synthesized.
655 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
656 PE = OI->protocol_end(); P != PE; ++P) {
657 ObjCProtocolDecl *PD = (*P);
658 CollectProtocolSynthesizedIvars(PD, Ivars);
659 }
660}
661
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000662unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) {
663 unsigned count = 0;
664 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(*this),
665 E = PD->prop_end(*this); I != E; ++I)
666 if ((*I)->getPropertyIvarDecl())
667 ++count;
668
669 // Also look into nested protocols.
670 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
671 E = PD->protocol_end(); P != E; ++P)
672 count += CountProtocolSynthesizedIvars(*P);
673 return count;
674}
675
676unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI)
677{
678 unsigned count = 0;
679 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*this),
680 E = OI->prop_end(*this); I != E; ++I) {
681 if ((*I)->getPropertyIvarDecl())
682 ++count;
683 }
684 // Also look into interface's protocol list for properties declared
685 // in the protocol and whose ivars are synthesized.
686 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
687 PE = OI->protocol_end(); P != PE; ++P) {
688 ObjCProtocolDecl *PD = (*P);
689 count += CountProtocolSynthesizedIvars(PD);
690 }
691 return count;
692}
693
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000694/// getInterfaceLayoutImpl - Get or compute information about the
695/// layout of the given interface.
696///
697/// \param Impl - If given, also include the layout of the interface's
698/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000699const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000700ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
701 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000702 assert(!D->isForwardDecl() && "Invalid interface decl!");
703
Devang Patel44a3dde2008-06-04 21:54:36 +0000704 // Look up this layout, if already laid out, return what we have.
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000705 ObjCContainerDecl *Key =
706 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
707 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
708 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000709
Daniel Dunbar453addb2009-05-03 11:16:44 +0000710 unsigned FieldCount = D->ivar_size();
711 // Add in synthesized ivar count if laying out an implementation.
712 if (Impl) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000713 unsigned SynthCount = CountSynthesizedIvars(D);
714 FieldCount += SynthCount;
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000715 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +0000716 // entry. Note we can't cache this because we simply free all
717 // entries later; however we shouldn't look up implementations
718 // frequently.
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000719 if (SynthCount == 0)
Daniel Dunbar453addb2009-05-03 11:16:44 +0000720 return getObjCLayout(D, 0);
721 }
722
Devang Patel6a5a34c2008-06-06 02:14:01 +0000723 ASTRecordLayout *NewEntry = NULL;
Devang Patel6a5a34c2008-06-06 02:14:01 +0000724 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Devang Patel6a5a34c2008-06-06 02:14:01 +0000725 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
726 unsigned Alignment = SL.getAlignment();
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000727
Daniel Dunbar913af352009-05-07 21:58:26 +0000728 // We start laying out ivars not at the end of the superclass
729 // structure, but at the next byte following the last field.
730 uint64_t Size = llvm::RoundUpToAlignment(SL.NextOffset, 8);
Daniel Dunbard6884a02009-05-04 05:16:21 +0000731
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000732 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout(Size, Alignment);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000733 NewEntry->InitializeLayout(FieldCount);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000734 } else {
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000735 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout();
Devang Patel6a5a34c2008-06-06 02:14:01 +0000736 NewEntry->InitializeLayout(FieldCount);
737 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000738
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000739 unsigned StructPacking = 0;
740 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
741 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000742
743 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
744 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
745 AA->getAlignment()));
746
747 // Layout each ivar sequentially.
748 unsigned i = 0;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000749 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
750 ShallowCollectObjCIvars(D, Ivars, Impl);
751 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
752 NewEntry->LayoutField(Ivars[k], i++, false, StructPacking, *this);
753
Devang Patel44a3dde2008-06-04 21:54:36 +0000754 // Finally, round the size of the total struct up to the alignment of the
755 // struct itself.
756 NewEntry->FinalizeLayout();
757 return *NewEntry;
758}
759
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000760const ASTRecordLayout &
761ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
762 return getObjCLayout(D, 0);
763}
764
765const ASTRecordLayout &
766ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
767 return getObjCLayout(D->getClassInterface(), D);
768}
769
Devang Patel88a981b2007-11-01 19:11:01 +0000770/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000771/// specified record (struct/union/class), which indicates its size and field
772/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000773const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000774 D = D->getDefinition(*this);
775 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000776
Chris Lattner464175b2007-07-18 17:52:12 +0000777 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000778 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000779 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000780
Devang Patel88a981b2007-11-01 19:11:01 +0000781 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
782 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
783 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000784 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000785
Douglas Gregore267ff32008-12-11 20:41:00 +0000786 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000787 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
788 D->field_end(*this)));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000789 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000790
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000791 unsigned StructPacking = 0;
792 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
793 StructPacking = PA->getAlignment();
794
Eli Friedman4bd998b2008-05-30 09:31:38 +0000795 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000796 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
797 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000798
Eli Friedman4bd998b2008-05-30 09:31:38 +0000799 // Layout each field, for now, just sequentially, respecting alignment. In
800 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000801 unsigned FieldIdx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000802 for (RecordDecl::field_iterator Field = D->field_begin(*this),
803 FieldEnd = D->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000804 Field != FieldEnd; (void)++Field, ++FieldIdx)
805 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000806
807 // Finally, round the size of the total struct up to the alignment of the
808 // struct itself.
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000809 NewEntry->FinalizeLayout(getLangOptions().CPlusPlus);
Chris Lattner5d2a6302007-07-18 18:26:58 +0000810 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000811}
812
Chris Lattnera7674d82007-07-13 22:13:22 +0000813//===----------------------------------------------------------------------===//
814// Type creation/memoization methods
815//===----------------------------------------------------------------------===//
816
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000817QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000818 QualType CanT = getCanonicalType(T);
819 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000820 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000821
822 // If we are composing extended qualifiers together, merge together into one
823 // ExtQualType node.
824 unsigned CVRQuals = T.getCVRQualifiers();
825 QualType::GCAttrTypes GCAttr = QualType::GCNone;
826 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000827
Chris Lattnerb7d25532009-02-18 22:53:11 +0000828 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
829 // If this type already has an address space specified, it cannot get
830 // another one.
831 assert(EQT->getAddressSpace() == 0 &&
832 "Type cannot be in multiple addr spaces!");
833 GCAttr = EQT->getObjCGCAttr();
834 TypeNode = EQT->getBaseType();
835 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000836
Chris Lattnerb7d25532009-02-18 22:53:11 +0000837 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000838 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000839 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000840 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000841 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000842 return QualType(EXTQy, CVRQuals);
843
Christopher Lambebb97e92008-02-04 02:31:56 +0000844 // If the base type isn't canonical, this won't be a canonical type either,
845 // so fill in the canonical type field.
846 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000847 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000848 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000849
Chris Lattnerb7d25532009-02-18 22:53:11 +0000850 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000851 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000852 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000853 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000854 ExtQualType *New =
855 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000856 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000857 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000858 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000859}
860
Chris Lattnerb7d25532009-02-18 22:53:11 +0000861QualType ASTContext::getObjCGCQualType(QualType T,
862 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000863 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000864 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000865 return T;
866
Fariborz Jahanian4027cd12009-06-03 17:15:17 +0000867 if (T->isPointerType()) {
868 QualType Pointee = T->getAsPointerType()->getPointeeType();
869 if (Pointee->isPointerType()) {
870 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
871 return getPointerType(ResultType);
872 }
873 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000874 // If we are composing extended qualifiers together, merge together into one
875 // ExtQualType node.
876 unsigned CVRQuals = T.getCVRQualifiers();
877 Type *TypeNode = T.getTypePtr();
878 unsigned AddressSpace = 0;
879
880 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
881 // If this type already has an address space specified, it cannot get
882 // another one.
883 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
884 "Type cannot be in multiple addr spaces!");
885 AddressSpace = EQT->getAddressSpace();
886 TypeNode = EQT->getBaseType();
887 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000888
889 // Check if we've already instantiated an gc qual'd type of this type.
890 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000891 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000892 void *InsertPos = 0;
893 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000894 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000895
896 // If the base type isn't canonical, this won't be a canonical type either,
897 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000898 // FIXME: Isn't this also not canonical if the base type is a array
899 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000900 QualType Canonical;
901 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000902 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000903
Chris Lattnerb7d25532009-02-18 22:53:11 +0000904 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000905 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
906 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
907 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000908 ExtQualType *New =
909 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000910 ExtQualTypes.InsertNode(New, InsertPos);
911 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000912 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000913}
Chris Lattnera7674d82007-07-13 22:13:22 +0000914
Reid Spencer5f016e22007-07-11 17:01:13 +0000915/// getComplexType - Return the uniqued reference to the type for a complex
916/// number with the specified element type.
917QualType ASTContext::getComplexType(QualType T) {
918 // Unique pointers, to guarantee there is only one pointer of a particular
919 // structure.
920 llvm::FoldingSetNodeID ID;
921 ComplexType::Profile(ID, T);
922
923 void *InsertPos = 0;
924 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
925 return QualType(CT, 0);
926
927 // If the pointee type isn't canonical, this won't be a canonical type either,
928 // so fill in the canonical type field.
929 QualType Canonical;
930 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000931 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000932
933 // Get the new insert position for the node we care about.
934 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000935 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000936 }
Steve Narofff83820b2009-01-27 22:08:43 +0000937 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000938 Types.push_back(New);
939 ComplexTypes.InsertNode(New, InsertPos);
940 return QualType(New, 0);
941}
942
Eli Friedmanf98aba32009-02-13 02:31:07 +0000943QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
944 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
945 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
946 FixedWidthIntType *&Entry = Map[Width];
947 if (!Entry)
948 Entry = new FixedWidthIntType(Width, Signed);
949 return QualType(Entry, 0);
950}
Reid Spencer5f016e22007-07-11 17:01:13 +0000951
952/// getPointerType - Return the uniqued reference to the type for a pointer to
953/// the specified type.
954QualType ASTContext::getPointerType(QualType T) {
955 // Unique pointers, to guarantee there is only one pointer of a particular
956 // structure.
957 llvm::FoldingSetNodeID ID;
958 PointerType::Profile(ID, T);
959
960 void *InsertPos = 0;
961 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
962 return QualType(PT, 0);
963
964 // If the pointee type isn't canonical, this won't be a canonical type either,
965 // so fill in the canonical type field.
966 QualType Canonical;
967 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000968 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000969
970 // Get the new insert position for the node we care about.
971 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000972 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000973 }
Steve Narofff83820b2009-01-27 22:08:43 +0000974 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000975 Types.push_back(New);
976 PointerTypes.InsertNode(New, InsertPos);
977 return QualType(New, 0);
978}
979
Steve Naroff5618bd42008-08-27 16:04:49 +0000980/// getBlockPointerType - Return the uniqued reference to the type for
981/// a pointer to the specified block.
982QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000983 assert(T->isFunctionType() && "block of function types only");
984 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000985 // structure.
986 llvm::FoldingSetNodeID ID;
987 BlockPointerType::Profile(ID, T);
988
989 void *InsertPos = 0;
990 if (BlockPointerType *PT =
991 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
992 return QualType(PT, 0);
993
Steve Naroff296e8d52008-08-28 19:20:44 +0000994 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000995 // type either so fill in the canonical type field.
996 QualType Canonical;
997 if (!T->isCanonical()) {
998 Canonical = getBlockPointerType(getCanonicalType(T));
999
1000 // Get the new insert position for the node we care about.
1001 BlockPointerType *NewIP =
1002 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001003 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001004 }
Steve Narofff83820b2009-01-27 22:08:43 +00001005 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001006 Types.push_back(New);
1007 BlockPointerTypes.InsertNode(New, InsertPos);
1008 return QualType(New, 0);
1009}
1010
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001011/// getLValueReferenceType - Return the uniqued reference to the type for an
1012/// lvalue reference to the specified type.
1013QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 // Unique pointers, to guarantee there is only one pointer of a particular
1015 // structure.
1016 llvm::FoldingSetNodeID ID;
1017 ReferenceType::Profile(ID, T);
1018
1019 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001020 if (LValueReferenceType *RT =
1021 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001022 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001023
Reid Spencer5f016e22007-07-11 17:01:13 +00001024 // If the referencee type isn't canonical, this won't be a canonical type
1025 // either, so fill in the canonical type field.
1026 QualType Canonical;
1027 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001028 Canonical = getLValueReferenceType(getCanonicalType(T));
1029
Reid Spencer5f016e22007-07-11 17:01:13 +00001030 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001031 LValueReferenceType *NewIP =
1032 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001033 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 }
1035
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001036 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001037 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001038 LValueReferenceTypes.InsertNode(New, InsertPos);
1039 return QualType(New, 0);
1040}
1041
1042/// getRValueReferenceType - Return the uniqued reference to the type for an
1043/// rvalue reference to the specified type.
1044QualType ASTContext::getRValueReferenceType(QualType T) {
1045 // Unique pointers, to guarantee there is only one pointer of a particular
1046 // structure.
1047 llvm::FoldingSetNodeID ID;
1048 ReferenceType::Profile(ID, T);
1049
1050 void *InsertPos = 0;
1051 if (RValueReferenceType *RT =
1052 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1053 return QualType(RT, 0);
1054
1055 // If the referencee type isn't canonical, this won't be a canonical type
1056 // either, so fill in the canonical type field.
1057 QualType Canonical;
1058 if (!T->isCanonical()) {
1059 Canonical = getRValueReferenceType(getCanonicalType(T));
1060
1061 // Get the new insert position for the node we care about.
1062 RValueReferenceType *NewIP =
1063 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1064 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1065 }
1066
1067 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1068 Types.push_back(New);
1069 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001070 return QualType(New, 0);
1071}
1072
Sebastian Redlf30208a2009-01-24 21:16:55 +00001073/// getMemberPointerType - Return the uniqued reference to the type for a
1074/// member pointer to the specified type, in the specified class.
1075QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1076{
1077 // Unique pointers, to guarantee there is only one pointer of a particular
1078 // structure.
1079 llvm::FoldingSetNodeID ID;
1080 MemberPointerType::Profile(ID, T, Cls);
1081
1082 void *InsertPos = 0;
1083 if (MemberPointerType *PT =
1084 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1085 return QualType(PT, 0);
1086
1087 // If the pointee or class type isn't canonical, this won't be a canonical
1088 // type either, so fill in the canonical type field.
1089 QualType Canonical;
1090 if (!T->isCanonical()) {
1091 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1092
1093 // Get the new insert position for the node we care about.
1094 MemberPointerType *NewIP =
1095 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1096 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1097 }
Steve Narofff83820b2009-01-27 22:08:43 +00001098 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001099 Types.push_back(New);
1100 MemberPointerTypes.InsertNode(New, InsertPos);
1101 return QualType(New, 0);
1102}
1103
Steve Narofffb22d962007-08-30 01:06:46 +00001104/// getConstantArrayType - Return the unique reference to the type for an
1105/// array of the specified element type.
1106QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001107 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001108 ArrayType::ArraySizeModifier ASM,
1109 unsigned EltTypeQuals) {
Eli Friedman587cbdf2009-05-29 20:17:55 +00001110 assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) &&
1111 "Constant array of VLAs is illegal!");
1112
Chris Lattner38aeec72009-05-13 04:12:56 +00001113 // Convert the array size into a canonical width matching the pointer size for
1114 // the target.
1115 llvm::APInt ArySize(ArySizeIn);
1116 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1117
Reid Spencer5f016e22007-07-11 17:01:13 +00001118 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001119 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001120
1121 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001122 if (ConstantArrayType *ATP =
1123 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001124 return QualType(ATP, 0);
1125
1126 // If the element type isn't canonical, this won't be a canonical type either,
1127 // so fill in the canonical type field.
1128 QualType Canonical;
1129 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001130 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001131 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001132 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001133 ConstantArrayType *NewIP =
1134 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001135 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001136 }
1137
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001138 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001139 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001140 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001141 Types.push_back(New);
1142 return QualType(New, 0);
1143}
1144
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001145/// getVariableArrayType - Returns a non-unique reference to the type for a
1146/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001147QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1148 ArrayType::ArraySizeModifier ASM,
1149 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001150 // Since we don't unique expressions, it isn't possible to unique VLA's
1151 // that have an expression provided for their size.
1152
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001153 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001154 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001155
1156 VariableArrayTypes.push_back(New);
1157 Types.push_back(New);
1158 return QualType(New, 0);
1159}
1160
Douglas Gregor898574e2008-12-05 23:32:09 +00001161/// getDependentSizedArrayType - Returns a non-unique reference to
1162/// the type for a dependently-sized array of the specified element
1163/// type. FIXME: We will need these to be uniqued, or at least
1164/// comparable, at some point.
1165QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1166 ArrayType::ArraySizeModifier ASM,
1167 unsigned EltTypeQuals) {
1168 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1169 "Size must be type- or value-dependent!");
1170
1171 // Since we don't unique expressions, it isn't possible to unique
1172 // dependently-sized array types.
1173
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001174 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001175 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1176 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001177
1178 DependentSizedArrayTypes.push_back(New);
1179 Types.push_back(New);
1180 return QualType(New, 0);
1181}
1182
Eli Friedmanc5773c42008-02-15 18:16:39 +00001183QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1184 ArrayType::ArraySizeModifier ASM,
1185 unsigned EltTypeQuals) {
1186 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001187 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001188
1189 void *InsertPos = 0;
1190 if (IncompleteArrayType *ATP =
1191 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1192 return QualType(ATP, 0);
1193
1194 // If the element type isn't canonical, this won't be a canonical type
1195 // either, so fill in the canonical type field.
1196 QualType Canonical;
1197
1198 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001199 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001200 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001201
1202 // Get the new insert position for the node we care about.
1203 IncompleteArrayType *NewIP =
1204 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001205 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001206 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001207
Steve Narofff83820b2009-01-27 22:08:43 +00001208 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001209 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001210
1211 IncompleteArrayTypes.InsertNode(New, InsertPos);
1212 Types.push_back(New);
1213 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001214}
1215
Steve Naroff73322922007-07-18 18:00:27 +00001216/// getVectorType - Return the unique reference to a vector type of
1217/// the specified element type and size. VectorType must be a built-in type.
1218QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 BuiltinType *baseType;
1220
Chris Lattnerf52ab252008-04-06 22:59:24 +00001221 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001222 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001223
1224 // Check if we've already instantiated a vector of this type.
1225 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001226 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001227 void *InsertPos = 0;
1228 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1229 return QualType(VTP, 0);
1230
1231 // If the element type isn't canonical, this won't be a canonical type either,
1232 // so fill in the canonical type field.
1233 QualType Canonical;
1234 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001235 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001236
1237 // Get the new insert position for the node we care about.
1238 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001239 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001240 }
Steve Narofff83820b2009-01-27 22:08:43 +00001241 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 VectorTypes.InsertNode(New, InsertPos);
1243 Types.push_back(New);
1244 return QualType(New, 0);
1245}
1246
Nate Begeman213541a2008-04-18 23:10:10 +00001247/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001248/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001249QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001250 BuiltinType *baseType;
1251
Chris Lattnerf52ab252008-04-06 22:59:24 +00001252 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001253 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001254
1255 // Check if we've already instantiated a vector of this type.
1256 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001257 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001258 void *InsertPos = 0;
1259 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1260 return QualType(VTP, 0);
1261
1262 // If the element type isn't canonical, this won't be a canonical type either,
1263 // so fill in the canonical type field.
1264 QualType Canonical;
1265 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001266 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001267
1268 // Get the new insert position for the node we care about.
1269 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001270 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001271 }
Steve Narofff83820b2009-01-27 22:08:43 +00001272 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001273 VectorTypes.InsertNode(New, InsertPos);
1274 Types.push_back(New);
1275 return QualType(New, 0);
1276}
1277
Douglas Gregor72564e72009-02-26 23:50:07 +00001278/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001279///
Douglas Gregor72564e72009-02-26 23:50:07 +00001280QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001281 // Unique functions, to guarantee there is only one function of a particular
1282 // structure.
1283 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001284 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001285
1286 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001287 if (FunctionNoProtoType *FT =
1288 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001289 return QualType(FT, 0);
1290
1291 QualType Canonical;
1292 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001293 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001294
1295 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001296 FunctionNoProtoType *NewIP =
1297 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001298 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001299 }
1300
Douglas Gregor72564e72009-02-26 23:50:07 +00001301 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001302 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001303 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001304 return QualType(New, 0);
1305}
1306
1307/// getFunctionType - Return a normal function type with a typed argument
1308/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001309QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001310 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001311 unsigned TypeQuals, bool hasExceptionSpec,
1312 bool hasAnyExceptionSpec, unsigned NumExs,
1313 const QualType *ExArray) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001314 // Unique functions, to guarantee there is only one function of a particular
1315 // structure.
1316 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001317 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001318 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1319 NumExs, ExArray);
Reid Spencer5f016e22007-07-11 17:01:13 +00001320
1321 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001322 if (FunctionProtoType *FTP =
1323 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001324 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001325
1326 // Determine whether the type being created is already canonical or not.
Reid Spencer5f016e22007-07-11 17:01:13 +00001327 bool isCanonical = ResultTy->isCanonical();
Sebastian Redl465226e2009-05-27 22:11:52 +00001328 if (hasExceptionSpec)
1329 isCanonical = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001330 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1331 if (!ArgArray[i]->isCanonical())
1332 isCanonical = false;
1333
1334 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001335 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001336 QualType Canonical;
1337 if (!isCanonical) {
1338 llvm::SmallVector<QualType, 16> CanonicalArgs;
1339 CanonicalArgs.reserve(NumArgs);
1340 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001341 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001342
Chris Lattnerf52ab252008-04-06 22:59:24 +00001343 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001344 CanonicalArgs.data(), NumArgs,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001345 isVariadic, TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +00001346
Reid Spencer5f016e22007-07-11 17:01:13 +00001347 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001348 FunctionProtoType *NewIP =
1349 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001350 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001351 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001352
Douglas Gregor72564e72009-02-26 23:50:07 +00001353 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001354 // for two variable size arrays (for parameter and exception types) at the
1355 // end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001356 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001357 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1358 NumArgs*sizeof(QualType) +
1359 NumExs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001360 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001361 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1362 ExArray, NumExs, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001363 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001364 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001365 return QualType(FTP, 0);
1366}
1367
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001368/// getTypeDeclType - Return the unique reference to the type for the
1369/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001370QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001371 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001372 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1373
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001374 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001375 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001376 else if (isa<TemplateTypeParmDecl>(Decl)) {
1377 assert(false && "Template type parameter types are always available.");
1378 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001379 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001380
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001381 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001382 if (PrevDecl)
1383 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001384 else
1385 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001386 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001387 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1388 if (PrevDecl)
1389 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001390 else
1391 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001392 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001393 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001394 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001395
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001396 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001397 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001398}
1399
Reid Spencer5f016e22007-07-11 17:01:13 +00001400/// getTypedefType - Return the unique reference to the type for the
1401/// specified typename decl.
1402QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1403 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1404
Chris Lattnerf52ab252008-04-06 22:59:24 +00001405 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001406 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001407 Types.push_back(Decl->TypeForDecl);
1408 return QualType(Decl->TypeForDecl, 0);
1409}
1410
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001411/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001412/// specified ObjC interface decl.
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001413QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001414 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1415
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001416 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1417 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff3536b442007-09-06 21:24:23 +00001418 Types.push_back(Decl->TypeForDecl);
1419 return QualType(Decl->TypeForDecl, 0);
1420}
1421
Douglas Gregorfab9d672009-02-05 23:33:38 +00001422/// \brief Retrieve the template type parameter type for a template
1423/// parameter with the given depth, index, and (optionally) name.
1424QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1425 IdentifierInfo *Name) {
1426 llvm::FoldingSetNodeID ID;
1427 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1428 void *InsertPos = 0;
1429 TemplateTypeParmType *TypeParm
1430 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1431
1432 if (TypeParm)
1433 return QualType(TypeParm, 0);
1434
1435 if (Name)
1436 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1437 getTemplateTypeParmType(Depth, Index));
1438 else
1439 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1440
1441 Types.push_back(TypeParm);
1442 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1443
1444 return QualType(TypeParm, 0);
1445}
1446
Douglas Gregor55f6b142009-02-09 18:46:07 +00001447QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001448ASTContext::getTemplateSpecializationType(TemplateName Template,
1449 const TemplateArgument *Args,
1450 unsigned NumArgs,
1451 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001452 if (!Canon.isNull())
1453 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001454
Douglas Gregor55f6b142009-02-09 18:46:07 +00001455 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001456 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001457
Douglas Gregor55f6b142009-02-09 18:46:07 +00001458 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001459 TemplateSpecializationType *Spec
1460 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001461
1462 if (Spec)
1463 return QualType(Spec, 0);
1464
Douglas Gregor7532dc62009-03-30 22:58:21 +00001465 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001466 sizeof(TemplateArgument) * NumArgs),
1467 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001468 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001469 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001470 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001471
1472 return QualType(Spec, 0);
1473}
1474
Douglas Gregore4e5b052009-03-19 00:18:19 +00001475QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001476ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001477 QualType NamedType) {
1478 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001479 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001480
1481 void *InsertPos = 0;
1482 QualifiedNameType *T
1483 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1484 if (T)
1485 return QualType(T, 0);
1486
Douglas Gregorab452ba2009-03-26 23:50:42 +00001487 T = new (*this) QualifiedNameType(NNS, NamedType,
1488 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001489 Types.push_back(T);
1490 QualifiedNameTypes.InsertNode(T, InsertPos);
1491 return QualType(T, 0);
1492}
1493
Douglas Gregord57959a2009-03-27 23:10:48 +00001494QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1495 const IdentifierInfo *Name,
1496 QualType Canon) {
1497 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1498
1499 if (Canon.isNull()) {
1500 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1501 if (CanonNNS != NNS)
1502 Canon = getTypenameType(CanonNNS, Name);
1503 }
1504
1505 llvm::FoldingSetNodeID ID;
1506 TypenameType::Profile(ID, NNS, Name);
1507
1508 void *InsertPos = 0;
1509 TypenameType *T
1510 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1511 if (T)
1512 return QualType(T, 0);
1513
1514 T = new (*this) TypenameType(NNS, Name, Canon);
1515 Types.push_back(T);
1516 TypenameTypes.InsertNode(T, InsertPos);
1517 return QualType(T, 0);
1518}
1519
Douglas Gregor17343172009-04-01 00:28:59 +00001520QualType
1521ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1522 const TemplateSpecializationType *TemplateId,
1523 QualType Canon) {
1524 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1525
1526 if (Canon.isNull()) {
1527 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1528 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1529 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1530 const TemplateSpecializationType *CanonTemplateId
1531 = CanonType->getAsTemplateSpecializationType();
1532 assert(CanonTemplateId &&
1533 "Canonical type must also be a template specialization type");
1534 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1535 }
1536 }
1537
1538 llvm::FoldingSetNodeID ID;
1539 TypenameType::Profile(ID, NNS, TemplateId);
1540
1541 void *InsertPos = 0;
1542 TypenameType *T
1543 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1544 if (T)
1545 return QualType(T, 0);
1546
1547 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1548 Types.push_back(T);
1549 TypenameTypes.InsertNode(T, InsertPos);
1550 return QualType(T, 0);
1551}
1552
Chris Lattner88cb27a2008-04-07 04:56:42 +00001553/// CmpProtocolNames - Comparison predicate for sorting protocols
1554/// alphabetically.
1555static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1556 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001557 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001558}
1559
1560static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1561 unsigned &NumProtocols) {
1562 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1563
1564 // Sort protocols, keyed by name.
1565 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1566
1567 // Remove duplicates.
1568 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1569 NumProtocols = ProtocolsEnd-Protocols;
1570}
1571
1572
Chris Lattner065f0d72008-04-07 04:44:08 +00001573/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1574/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001575QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1576 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001577 // Sort the protocol list alphabetically to canonicalize it.
1578 SortAndUniqueProtocols(Protocols, NumProtocols);
1579
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001580 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001581 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001582
1583 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001584 if (ObjCQualifiedInterfaceType *QT =
1585 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001586 return QualType(QT, 0);
1587
1588 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001589 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001590 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001591
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001592 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001593 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001594 return QualType(QType, 0);
1595}
1596
Chris Lattner88cb27a2008-04-07 04:56:42 +00001597/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1598/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001599QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001600 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001601 // Sort the protocol list alphabetically to canonicalize it.
1602 SortAndUniqueProtocols(Protocols, NumProtocols);
1603
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001604 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001605 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001606
1607 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001608 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001609 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001610 return QualType(QT, 0);
1611
1612 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001613 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001614 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001615 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001616 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001617 return QualType(QType, 0);
1618}
1619
Douglas Gregor72564e72009-02-26 23:50:07 +00001620/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1621/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001622/// multiple declarations that refer to "typeof(x)" all contain different
1623/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1624/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001625QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001626 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001627 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001628 Types.push_back(toe);
1629 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001630}
1631
Steve Naroff9752f252007-08-01 18:02:17 +00001632/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1633/// TypeOfType AST's. The only motivation to unique these nodes would be
1634/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1635/// an issue. This doesn't effect the type checker, since it operates
1636/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001637QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001638 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001639 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001640 Types.push_back(tot);
1641 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001642}
1643
Reid Spencer5f016e22007-07-11 17:01:13 +00001644/// getTagDeclType - Return the unique reference to the type for the
1645/// specified TagDecl (struct/union/class/enum) decl.
1646QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001647 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001648 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001649}
1650
1651/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1652/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1653/// needs to agree with the definition in <stddef.h>.
1654QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001655 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001656}
1657
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001658/// getSignedWCharType - Return the type of "signed wchar_t".
1659/// Used when in C++, as a GCC extension.
1660QualType ASTContext::getSignedWCharType() const {
1661 // FIXME: derive from "Target" ?
1662 return WCharTy;
1663}
1664
1665/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1666/// Used when in C++, as a GCC extension.
1667QualType ASTContext::getUnsignedWCharType() const {
1668 // FIXME: derive from "Target" ?
1669 return UnsignedIntTy;
1670}
1671
Chris Lattner8b9023b2007-07-13 03:05:23 +00001672/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1673/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1674QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001675 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001676}
1677
Chris Lattnere6327742008-04-02 05:18:44 +00001678//===----------------------------------------------------------------------===//
1679// Type Operators
1680//===----------------------------------------------------------------------===//
1681
Chris Lattner77c96472008-04-06 22:41:35 +00001682/// getCanonicalType - Return the canonical (structural) type corresponding to
1683/// the specified potentially non-canonical type. The non-canonical version
1684/// of a type may have many "decorated" versions of types. Decorators can
1685/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1686/// to be free of any of these, allowing two canonical types to be compared
1687/// for exact equality with a simple pointer comparison.
1688QualType ASTContext::getCanonicalType(QualType T) {
1689 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001690
1691 // If the result has type qualifiers, make sure to canonicalize them as well.
1692 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1693 if (TypeQuals == 0) return CanType;
1694
1695 // If the type qualifiers are on an array type, get the canonical type of the
1696 // array with the qualifiers applied to the element type.
1697 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1698 if (!AT)
1699 return CanType.getQualifiedType(TypeQuals);
1700
1701 // Get the canonical version of the element with the extra qualifiers on it.
1702 // This can recursively sink qualifiers through multiple levels of arrays.
1703 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1704 NewEltTy = getCanonicalType(NewEltTy);
1705
1706 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1707 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1708 CAT->getIndexTypeQualifier());
1709 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1710 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1711 IAT->getIndexTypeQualifier());
1712
Douglas Gregor898574e2008-12-05 23:32:09 +00001713 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1714 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1715 DSAT->getSizeModifier(),
1716 DSAT->getIndexTypeQualifier());
1717
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001718 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1719 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1720 VAT->getSizeModifier(),
1721 VAT->getIndexTypeQualifier());
1722}
1723
Douglas Gregor7da97d02009-05-10 22:57:19 +00001724Decl *ASTContext::getCanonicalDecl(Decl *D) {
Douglas Gregorc4ccf012009-05-10 22:59:12 +00001725 if (!D)
1726 return 0;
1727
Douglas Gregor7da97d02009-05-10 22:57:19 +00001728 if (TagDecl *Tag = dyn_cast<TagDecl>(D)) {
1729 QualType T = getTagDeclType(Tag);
1730 return cast<TagDecl>(cast<TagType>(T.getTypePtr()->CanonicalType)
1731 ->getDecl());
1732 }
1733
1734 if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(D)) {
1735 while (Template->getPreviousDeclaration())
1736 Template = Template->getPreviousDeclaration();
1737 return Template;
1738 }
1739
1740 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
1741 while (Function->getPreviousDeclaration())
1742 Function = Function->getPreviousDeclaration();
1743 return const_cast<FunctionDecl *>(Function);
1744 }
1745
1746 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
1747 while (Var->getPreviousDeclaration())
1748 Var = Var->getPreviousDeclaration();
1749 return const_cast<VarDecl *>(Var);
1750 }
1751
1752 return D;
1753}
1754
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001755TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
1756 // If this template name refers to a template, the canonical
1757 // template name merely stores the template itself.
1758 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Douglas Gregor7da97d02009-05-10 22:57:19 +00001759 return TemplateName(cast<TemplateDecl>(getCanonicalDecl(Template)));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001760
1761 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
1762 assert(DTN && "Non-dependent template names must refer to template decls.");
1763 return DTN->CanonicalTemplateName;
1764}
1765
Douglas Gregord57959a2009-03-27 23:10:48 +00001766NestedNameSpecifier *
1767ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1768 if (!NNS)
1769 return 0;
1770
1771 switch (NNS->getKind()) {
1772 case NestedNameSpecifier::Identifier:
1773 // Canonicalize the prefix but keep the identifier the same.
1774 return NestedNameSpecifier::Create(*this,
1775 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1776 NNS->getAsIdentifier());
1777
1778 case NestedNameSpecifier::Namespace:
1779 // A namespace is canonical; build a nested-name-specifier with
1780 // this namespace and no prefix.
1781 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1782
1783 case NestedNameSpecifier::TypeSpec:
1784 case NestedNameSpecifier::TypeSpecWithTemplate: {
1785 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1786 NestedNameSpecifier *Prefix = 0;
1787
1788 // FIXME: This isn't the right check!
1789 if (T->isDependentType())
1790 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1791
1792 return NestedNameSpecifier::Create(*this, Prefix,
1793 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1794 T.getTypePtr());
1795 }
1796
1797 case NestedNameSpecifier::Global:
1798 // The global specifier is canonical and unique.
1799 return NNS;
1800 }
1801
1802 // Required to silence a GCC warning
1803 return 0;
1804}
1805
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001806
1807const ArrayType *ASTContext::getAsArrayType(QualType T) {
1808 // Handle the non-qualified case efficiently.
1809 if (T.getCVRQualifiers() == 0) {
1810 // Handle the common positive case fast.
1811 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1812 return AT;
1813 }
1814
1815 // Handle the common negative case fast, ignoring CVR qualifiers.
1816 QualType CType = T->getCanonicalTypeInternal();
1817
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001818 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001819 // test.
1820 if (!isa<ArrayType>(CType) &&
1821 !isa<ArrayType>(CType.getUnqualifiedType()))
1822 return 0;
1823
1824 // Apply any CVR qualifiers from the array type to the element type. This
1825 // implements C99 6.7.3p8: "If the specification of an array type includes
1826 // any type qualifiers, the element type is so qualified, not the array type."
1827
1828 // If we get here, we either have type qualifiers on the type, or we have
1829 // sugar such as a typedef in the way. If we have type qualifiers on the type
1830 // we must propagate them down into the elemeng type.
1831 unsigned CVRQuals = T.getCVRQualifiers();
1832 unsigned AddrSpace = 0;
1833 Type *Ty = T.getTypePtr();
1834
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001835 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001836 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001837 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1838 AddrSpace = EXTQT->getAddressSpace();
1839 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001840 } else {
1841 T = Ty->getDesugaredType();
1842 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1843 break;
1844 CVRQuals |= T.getCVRQualifiers();
1845 Ty = T.getTypePtr();
1846 }
1847 }
1848
1849 // If we have a simple case, just return now.
1850 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1851 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1852 return ATy;
1853
1854 // Otherwise, we have an array and we have qualifiers on it. Push the
1855 // qualifiers into the array element type and return a new array type.
1856 // Get the canonical version of the element with the extra qualifiers on it.
1857 // This can recursively sink qualifiers through multiple levels of arrays.
1858 QualType NewEltTy = ATy->getElementType();
1859 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001860 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001861 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1862
1863 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1864 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1865 CAT->getSizeModifier(),
1866 CAT->getIndexTypeQualifier()));
1867 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1868 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1869 IAT->getSizeModifier(),
1870 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001871
Douglas Gregor898574e2008-12-05 23:32:09 +00001872 if (const DependentSizedArrayType *DSAT
1873 = dyn_cast<DependentSizedArrayType>(ATy))
1874 return cast<ArrayType>(
1875 getDependentSizedArrayType(NewEltTy,
1876 DSAT->getSizeExpr(),
1877 DSAT->getSizeModifier(),
1878 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001879
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001880 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1881 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1882 VAT->getSizeModifier(),
1883 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001884}
1885
1886
Chris Lattnere6327742008-04-02 05:18:44 +00001887/// getArrayDecayedType - Return the properly qualified result of decaying the
1888/// specified array type to a pointer. This operation is non-trivial when
1889/// handling typedefs etc. The canonical type of "T" must be an array type,
1890/// this returns a pointer to a properly qualified element of the array.
1891///
1892/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1893QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001894 // Get the element type with 'getAsArrayType' so that we don't lose any
1895 // typedefs in the element type of the array. This also handles propagation
1896 // of type qualifiers from the array type into the element type if present
1897 // (C99 6.7.3p8).
1898 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1899 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001900
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001901 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001902
1903 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001904 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001905}
1906
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001907QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001908 QualType ElemTy = VAT->getElementType();
1909
1910 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1911 return getBaseElementType(VAT);
1912
1913 return ElemTy;
1914}
1915
Reid Spencer5f016e22007-07-11 17:01:13 +00001916/// getFloatingRank - Return a relative rank for floating point types.
1917/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001918static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001919 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001920 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001921
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001922 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001923 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001924 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001925 case BuiltinType::Float: return FloatRank;
1926 case BuiltinType::Double: return DoubleRank;
1927 case BuiltinType::LongDouble: return LongDoubleRank;
1928 }
1929}
1930
Steve Naroff716c7302007-08-27 01:41:48 +00001931/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1932/// point or a complex type (based on typeDomain/typeSize).
1933/// 'typeDomain' is a real floating point or complex type.
1934/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001935QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1936 QualType Domain) const {
1937 FloatingRank EltRank = getFloatingRank(Size);
1938 if (Domain->isComplexType()) {
1939 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001940 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001941 case FloatRank: return FloatComplexTy;
1942 case DoubleRank: return DoubleComplexTy;
1943 case LongDoubleRank: return LongDoubleComplexTy;
1944 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001945 }
Chris Lattner1361b112008-04-06 23:58:54 +00001946
1947 assert(Domain->isRealFloatingType() && "Unknown domain!");
1948 switch (EltRank) {
1949 default: assert(0 && "getFloatingRank(): illegal value for rank");
1950 case FloatRank: return FloatTy;
1951 case DoubleRank: return DoubleTy;
1952 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001953 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001954}
1955
Chris Lattner7cfeb082008-04-06 23:55:33 +00001956/// getFloatingTypeOrder - Compare the rank of the two specified floating
1957/// point types, ignoring the domain of the type (i.e. 'double' ==
1958/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1959/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001960int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1961 FloatingRank LHSR = getFloatingRank(LHS);
1962 FloatingRank RHSR = getFloatingRank(RHS);
1963
1964 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001965 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001966 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001967 return 1;
1968 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001969}
1970
Chris Lattnerf52ab252008-04-06 22:59:24 +00001971/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1972/// routine will assert if passed a built-in type that isn't an integer or enum,
1973/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001974unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001975 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001976 if (EnumType* ET = dyn_cast<EnumType>(T))
1977 T = ET->getDecl()->getIntegerType().getTypePtr();
1978
1979 // There are two things which impact the integer rank: the width, and
1980 // the ordering of builtins. The builtin ordering is encoded in the
1981 // bottom three bits; the width is encoded in the bits above that.
1982 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1983 return FWIT->getWidth() << 3;
1984 }
1985
Chris Lattnerf52ab252008-04-06 22:59:24 +00001986 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001987 default: assert(0 && "getIntegerRank(): not a built-in integer");
1988 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001989 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001990 case BuiltinType::Char_S:
1991 case BuiltinType::Char_U:
1992 case BuiltinType::SChar:
1993 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001994 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001995 case BuiltinType::Short:
1996 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001997 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001998 case BuiltinType::Int:
1999 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002000 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002001 case BuiltinType::Long:
2002 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002003 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002004 case BuiltinType::LongLong:
2005 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002006 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00002007 case BuiltinType::Int128:
2008 case BuiltinType::UInt128:
2009 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00002010 }
2011}
2012
Chris Lattner7cfeb082008-04-06 23:55:33 +00002013/// getIntegerTypeOrder - Returns the highest ranked integer type:
2014/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
2015/// LHS < RHS, return -1.
2016int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002017 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2018 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00002019 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002020
Chris Lattnerf52ab252008-04-06 22:59:24 +00002021 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2022 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00002023
Chris Lattner7cfeb082008-04-06 23:55:33 +00002024 unsigned LHSRank = getIntegerRank(LHSC);
2025 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00002026
Chris Lattner7cfeb082008-04-06 23:55:33 +00002027 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2028 if (LHSRank == RHSRank) return 0;
2029 return LHSRank > RHSRank ? 1 : -1;
2030 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002031
Chris Lattner7cfeb082008-04-06 23:55:33 +00002032 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2033 if (LHSUnsigned) {
2034 // If the unsigned [LHS] type is larger, return it.
2035 if (LHSRank >= RHSRank)
2036 return 1;
2037
2038 // If the signed type can represent all values of the unsigned type, it
2039 // wins. Because we are dealing with 2's complement and types that are
2040 // powers of two larger than each other, this is always safe.
2041 return -1;
2042 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002043
Chris Lattner7cfeb082008-04-06 23:55:33 +00002044 // If the unsigned [RHS] type is larger, return it.
2045 if (RHSRank >= LHSRank)
2046 return -1;
2047
2048 // If the signed type can represent all values of the unsigned type, it
2049 // wins. Because we are dealing with 2's complement and types that are
2050 // powers of two larger than each other, this is always safe.
2051 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002052}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002053
2054// getCFConstantStringType - Return the type used for constant CFStrings.
2055QualType ASTContext::getCFConstantStringType() {
2056 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00002057 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002058 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002059 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002060 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00002061
2062 // const int *isa;
2063 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002064 // int flags;
2065 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002066 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002067 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00002068 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002069 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00002070
Anders Carlsson71993dd2007-08-17 05:31:46 +00002071 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002072 for (unsigned i = 0; i < 4; ++i) {
2073 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2074 SourceLocation(), 0,
2075 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002076 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002077 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002078 }
2079
2080 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002081 }
2082
2083 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002084}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002085
Douglas Gregor319ac892009-04-23 22:29:11 +00002086void ASTContext::setCFConstantStringType(QualType T) {
2087 const RecordType *Rec = T->getAsRecordType();
2088 assert(Rec && "Invalid CFConstantStringType");
2089 CFConstantStringTypeDecl = Rec->getDecl();
2090}
2091
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002092QualType ASTContext::getObjCFastEnumerationStateType()
2093{
2094 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002095 ObjCFastEnumerationStateTypeDecl =
2096 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2097 &Idents.get("__objcFastEnumerationState"));
2098
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002099 QualType FieldTypes[] = {
2100 UnsignedLongTy,
2101 getPointerType(ObjCIdType),
2102 getPointerType(UnsignedLongTy),
2103 getConstantArrayType(UnsignedLongTy,
2104 llvm::APInt(32, 5), ArrayType::Normal, 0)
2105 };
2106
Douglas Gregor44b43212008-12-11 16:49:14 +00002107 for (size_t i = 0; i < 4; ++i) {
2108 FieldDecl *Field = FieldDecl::Create(*this,
2109 ObjCFastEnumerationStateTypeDecl,
2110 SourceLocation(), 0,
2111 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002112 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002113 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002114 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002115
Douglas Gregor44b43212008-12-11 16:49:14 +00002116 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002117 }
2118
2119 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2120}
2121
Douglas Gregor319ac892009-04-23 22:29:11 +00002122void ASTContext::setObjCFastEnumerationStateType(QualType T) {
2123 const RecordType *Rec = T->getAsRecordType();
2124 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2125 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2126}
2127
Anders Carlssone8c49532007-10-29 06:33:42 +00002128// This returns true if a type has been typedefed to BOOL:
2129// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002130static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002131 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002132 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2133 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002134
2135 return false;
2136}
2137
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002138/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002139/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002140int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002141 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002142
2143 // Make all integer and enum types at least as large as an int
2144 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002145 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002146 // Treat arrays as pointers, since that's how they're passed in.
2147 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002148 sz = getTypeSize(VoidPtrTy);
2149 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002150}
2151
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002152/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002153/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002154void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002155 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002156 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002157 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002158 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002159 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002160 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002161 // Compute size of all parameters.
2162 // Start with computing size of a pointer in number of bytes.
2163 // FIXME: There might(should) be a better way of doing this computation!
2164 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002165 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002166 // The first two arguments (self and _cmd) are pointers; account for
2167 // their size.
2168 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002169 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2170 E = Decl->param_end(); PI != E; ++PI) {
2171 QualType PType = (*PI)->getType();
2172 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002173 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002174 ParmOffset += sz;
2175 }
2176 S += llvm::utostr(ParmOffset);
2177 S += "@0:";
2178 S += llvm::utostr(PtrSize);
2179
2180 // Argument types.
2181 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002182 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2183 E = Decl->param_end(); PI != E; ++PI) {
2184 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002185 QualType PType = PVDecl->getOriginalType();
2186 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002187 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2188 // Use array's original type only if it has known number of
2189 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002190 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002191 PType = PVDecl->getType();
2192 } else if (PType->isFunctionType())
2193 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002194 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002195 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002196 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002197 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002198 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002199 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002200 }
2201}
2202
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002203/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002204/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002205/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2206/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002207/// Property attributes are stored as a comma-delimited C string. The simple
2208/// attributes readonly and bycopy are encoded as single characters. The
2209/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2210/// encoded as single characters, followed by an identifier. Property types
2211/// are also encoded as a parametrized attribute. The characters used to encode
2212/// these attributes are defined by the following enumeration:
2213/// @code
2214/// enum PropertyAttributes {
2215/// kPropertyReadOnly = 'R', // property is read-only.
2216/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2217/// kPropertyByref = '&', // property is a reference to the value last assigned
2218/// kPropertyDynamic = 'D', // property is dynamic
2219/// kPropertyGetter = 'G', // followed by getter selector name
2220/// kPropertySetter = 'S', // followed by setter selector name
2221/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2222/// kPropertyType = 't' // followed by old-style type encoding.
2223/// kPropertyWeak = 'W' // 'weak' property
2224/// kPropertyStrong = 'P' // property GC'able
2225/// kPropertyNonAtomic = 'N' // property non-atomic
2226/// };
2227/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002228void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2229 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002230 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002231 // Collect information from the property implementation decl(s).
2232 bool Dynamic = false;
2233 ObjCPropertyImplDecl *SynthesizePID = 0;
2234
2235 // FIXME: Duplicated code due to poor abstraction.
2236 if (Container) {
2237 if (const ObjCCategoryImplDecl *CID =
2238 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2239 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002240 i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2241 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002242 ObjCPropertyImplDecl *PID = *i;
2243 if (PID->getPropertyDecl() == PD) {
2244 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2245 Dynamic = true;
2246 } else {
2247 SynthesizePID = PID;
2248 }
2249 }
2250 }
2251 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002252 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002253 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002254 i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2255 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002256 ObjCPropertyImplDecl *PID = *i;
2257 if (PID->getPropertyDecl() == PD) {
2258 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2259 Dynamic = true;
2260 } else {
2261 SynthesizePID = PID;
2262 }
2263 }
2264 }
2265 }
2266 }
2267
2268 // FIXME: This is not very efficient.
2269 S = "T";
2270
2271 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002272 // GCC has some special rules regarding encoding of properties which
2273 // closely resembles encoding of ivars.
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002274 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002275 true /* outermost type */,
2276 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002277
2278 if (PD->isReadOnly()) {
2279 S += ",R";
2280 } else {
2281 switch (PD->getSetterKind()) {
2282 case ObjCPropertyDecl::Assign: break;
2283 case ObjCPropertyDecl::Copy: S += ",C"; break;
2284 case ObjCPropertyDecl::Retain: S += ",&"; break;
2285 }
2286 }
2287
2288 // It really isn't clear at all what this means, since properties
2289 // are "dynamic by default".
2290 if (Dynamic)
2291 S += ",D";
2292
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002293 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2294 S += ",N";
2295
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002296 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2297 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002298 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002299 }
2300
2301 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2302 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002303 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002304 }
2305
2306 if (SynthesizePID) {
2307 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2308 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002309 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002310 }
2311
2312 // FIXME: OBJCGC: weak & strong
2313}
2314
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002315/// getLegacyIntegralTypeEncoding -
2316/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002317/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002318/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2319///
2320void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2321 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2322 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002323 if (BT->getKind() == BuiltinType::ULong &&
2324 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002325 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002326 else
2327 if (BT->getKind() == BuiltinType::Long &&
2328 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002329 PointeeTy = IntTy;
2330 }
2331 }
2332}
2333
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002334void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002335 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002336 // We follow the behavior of gcc, expanding structures which are
2337 // directly pointed to, and expanding embedded structures. Note that
2338 // these rules are sufficient to prevent recursive encoding of the
2339 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002340 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2341 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002342}
2343
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002344static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002345 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002346 const Expr *E = FD->getBitWidth();
2347 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2348 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002349 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002350 S += 'b';
2351 S += llvm::utostr(N);
2352}
2353
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002354void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2355 bool ExpandPointedToStructures,
2356 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002357 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002358 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002359 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002360 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002361 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002362 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002363 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002364 else {
2365 char encoding;
2366 switch (BT->getKind()) {
2367 default: assert(0 && "Unhandled builtin type kind");
2368 case BuiltinType::Void: encoding = 'v'; break;
2369 case BuiltinType::Bool: encoding = 'B'; break;
2370 case BuiltinType::Char_U:
2371 case BuiltinType::UChar: encoding = 'C'; break;
2372 case BuiltinType::UShort: encoding = 'S'; break;
2373 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002374 case BuiltinType::ULong:
2375 encoding =
2376 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2377 break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002378 case BuiltinType::UInt128: encoding = 'T'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002379 case BuiltinType::ULongLong: encoding = 'Q'; break;
2380 case BuiltinType::Char_S:
2381 case BuiltinType::SChar: encoding = 'c'; break;
2382 case BuiltinType::Short: encoding = 's'; break;
2383 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002384 case BuiltinType::Long:
2385 encoding =
2386 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2387 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002388 case BuiltinType::LongLong: encoding = 'q'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002389 case BuiltinType::Int128: encoding = 't'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002390 case BuiltinType::Float: encoding = 'f'; break;
2391 case BuiltinType::Double: encoding = 'd'; break;
2392 case BuiltinType::LongDouble: encoding = 'd'; break;
2393 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002394
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002395 S += encoding;
2396 }
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002397 } else if (const ComplexType *CT = T->getAsComplexType()) {
2398 S += 'j';
2399 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2400 false);
2401 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002402 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2403 ExpandPointedToStructures,
2404 ExpandStructures, FD);
2405 if (FD || EncodingProperty) {
2406 // Note that we do extended encoding of protocol qualifer list
2407 // Only when doing ivar or property encoding.
2408 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2409 S += '"';
Steve Naroff446ee4e2009-05-27 16:21:00 +00002410 for (ObjCQualifiedIdType::qual_iterator I = QIDT->qual_begin(),
2411 E = QIDT->qual_end(); I != E; ++I) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002412 S += '<';
Steve Naroff446ee4e2009-05-27 16:21:00 +00002413 S += (*I)->getNameAsString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002414 S += '>';
2415 }
2416 S += '"';
2417 }
2418 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002419 }
2420 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002421 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002422 bool isReadOnly = false;
2423 // For historical/compatibility reasons, the read-only qualifier of the
2424 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2425 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2426 // Also, do not emit the 'r' for anything but the outermost type!
2427 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2428 if (OutermostType && T.isConstQualified()) {
2429 isReadOnly = true;
2430 S += 'r';
2431 }
2432 }
2433 else if (OutermostType) {
2434 QualType P = PointeeTy;
2435 while (P->getAsPointerType())
2436 P = P->getAsPointerType()->getPointeeType();
2437 if (P.isConstQualified()) {
2438 isReadOnly = true;
2439 S += 'r';
2440 }
2441 }
2442 if (isReadOnly) {
2443 // Another legacy compatibility encoding. Some ObjC qualifier and type
2444 // combinations need to be rearranged.
2445 // Rewrite "in const" from "nr" to "rn"
2446 const char * s = S.c_str();
2447 int len = S.length();
2448 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2449 std::string replace = "rn";
2450 S.replace(S.end()-2, S.end(), replace);
2451 }
2452 }
Steve Naroff389bf462009-02-12 17:52:19 +00002453 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002454 S += '@';
2455 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002456 }
2457 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002458 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002459 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002460 // Another historical/compatibility reason.
2461 // We encode the underlying type which comes out as
2462 // {...};
2463 S += '^';
2464 getObjCEncodingForTypeImpl(PointeeTy, S,
2465 false, ExpandPointedToStructures,
2466 NULL);
2467 return;
2468 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002469 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002470 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002471 const ObjCInterfaceType *OIT =
2472 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002473 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002474 S += '"';
2475 S += OI->getNameAsCString();
Steve Naroff446ee4e2009-05-27 16:21:00 +00002476 for (ObjCInterfaceType::qual_iterator I = OIT->qual_begin(),
2477 E = OIT->qual_end(); I != E; ++I) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002478 S += '<';
Steve Naroff446ee4e2009-05-27 16:21:00 +00002479 S += (*I)->getNameAsString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002480 S += '>';
2481 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002482 S += '"';
2483 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002484 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002485 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002486 S += '#';
2487 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002488 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002489 S += ':';
2490 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002491 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002492
2493 if (PointeeTy->isCharType()) {
2494 // char pointer types should be encoded as '*' unless it is a
2495 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002496 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002497 S += '*';
2498 return;
2499 }
2500 }
2501
2502 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002503 getLegacyIntegralTypeEncoding(PointeeTy);
2504
2505 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002506 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002507 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002508 } else if (const ArrayType *AT =
2509 // Ignore type qualifiers etc.
2510 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002511 if (isa<IncompleteArrayType>(AT)) {
2512 // Incomplete arrays are encoded as a pointer to the array element.
2513 S += '^';
2514
2515 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2516 false, ExpandStructures, FD);
2517 } else {
2518 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002519
Anders Carlsson559a8332009-02-22 01:38:57 +00002520 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2521 S += llvm::utostr(CAT->getSize().getZExtValue());
2522 else {
2523 //Variable length arrays are encoded as a regular array with 0 elements.
2524 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2525 S += '0';
2526 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002527
Anders Carlsson559a8332009-02-22 01:38:57 +00002528 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2529 false, ExpandStructures, FD);
2530 S += ']';
2531 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002532 } else if (T->getAsFunctionType()) {
2533 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002534 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002535 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002536 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002537 // Anonymous structures print as '?'
2538 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2539 S += II->getName();
2540 } else {
2541 S += '?';
2542 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002543 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002544 S += '=';
Douglas Gregor6ab35242009-04-09 21:40:53 +00002545 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2546 FieldEnd = RDecl->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +00002547 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002548 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002549 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002550 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002551 S += '"';
2552 }
2553
2554 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002555 if (Field->isBitField()) {
2556 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2557 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002558 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002559 QualType qt = Field->getType();
2560 getLegacyIntegralTypeEncoding(qt);
2561 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002562 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002563 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002564 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002565 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002566 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002567 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002568 if (FD && FD->isBitField())
2569 EncodeBitField(this, S, FD);
2570 else
2571 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002572 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002573 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002574 } else if (T->isObjCInterfaceType()) {
2575 // @encode(class_name)
2576 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2577 S += '{';
2578 const IdentifierInfo *II = OI->getIdentifier();
2579 S += II->getName();
2580 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002581 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002582 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002583 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002584 if (RecFields[i]->isBitField())
2585 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2586 RecFields[i]);
2587 else
2588 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2589 FD);
2590 }
2591 S += '}';
2592 }
2593 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002594 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002595}
2596
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002597void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002598 std::string& S) const {
2599 if (QT & Decl::OBJC_TQ_In)
2600 S += 'n';
2601 if (QT & Decl::OBJC_TQ_Inout)
2602 S += 'N';
2603 if (QT & Decl::OBJC_TQ_Out)
2604 S += 'o';
2605 if (QT & Decl::OBJC_TQ_Bycopy)
2606 S += 'O';
2607 if (QT & Decl::OBJC_TQ_Byref)
2608 S += 'R';
2609 if (QT & Decl::OBJC_TQ_Oneway)
2610 S += 'V';
2611}
2612
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002613void ASTContext::setBuiltinVaListType(QualType T)
2614{
2615 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2616
2617 BuiltinVaListType = T;
2618}
2619
Douglas Gregor319ac892009-04-23 22:29:11 +00002620void ASTContext::setObjCIdType(QualType T)
Steve Naroff7e219e42007-10-15 14:41:52 +00002621{
Douglas Gregor319ac892009-04-23 22:29:11 +00002622 ObjCIdType = T;
2623
2624 const TypedefType *TT = T->getAsTypedefType();
2625 if (!TT)
2626 return;
2627
2628 TypedefDecl *TD = TT->getDecl();
Steve Naroff7e219e42007-10-15 14:41:52 +00002629
2630 // typedef struct objc_object *id;
2631 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002632 // User error - caller will issue diagnostics.
2633 if (!ptr)
2634 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002635 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002636 // User error - caller will issue diagnostics.
2637 if (!rec)
2638 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002639 IdStructType = rec;
2640}
2641
Douglas Gregor319ac892009-04-23 22:29:11 +00002642void ASTContext::setObjCSelType(QualType T)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002643{
Douglas Gregor319ac892009-04-23 22:29:11 +00002644 ObjCSelType = T;
2645
2646 const TypedefType *TT = T->getAsTypedefType();
2647 if (!TT)
2648 return;
2649 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002650
2651 // typedef struct objc_selector *SEL;
2652 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002653 if (!ptr)
2654 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002655 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002656 if (!rec)
2657 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002658 SelStructType = rec;
2659}
2660
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002661void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002662{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002663 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002664}
2665
Douglas Gregor319ac892009-04-23 22:29:11 +00002666void ASTContext::setObjCClassType(QualType T)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002667{
Douglas Gregor319ac892009-04-23 22:29:11 +00002668 ObjCClassType = T;
2669
2670 const TypedefType *TT = T->getAsTypedefType();
2671 if (!TT)
2672 return;
2673 TypedefDecl *TD = TT->getDecl();
Anders Carlsson8baaca52007-10-31 02:53:19 +00002674
2675 // typedef struct objc_class *Class;
2676 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2677 assert(ptr && "'Class' incorrectly typed");
2678 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2679 assert(rec && "'Class' incorrectly typed");
2680 ClassStructType = rec;
2681}
2682
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002683void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2684 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002685 "'NSConstantString' type already set!");
2686
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002687 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002688}
2689
Douglas Gregor7532dc62009-03-30 22:58:21 +00002690/// \brief Retrieve the template name that represents a qualified
2691/// template name such as \c std::vector.
2692TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2693 bool TemplateKeyword,
2694 TemplateDecl *Template) {
2695 llvm::FoldingSetNodeID ID;
2696 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2697
2698 void *InsertPos = 0;
2699 QualifiedTemplateName *QTN =
2700 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2701 if (!QTN) {
2702 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2703 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2704 }
2705
2706 return TemplateName(QTN);
2707}
2708
2709/// \brief Retrieve the template name that represents a dependent
2710/// template name such as \c MetaFun::template apply.
2711TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2712 const IdentifierInfo *Name) {
2713 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2714
2715 llvm::FoldingSetNodeID ID;
2716 DependentTemplateName::Profile(ID, NNS, Name);
2717
2718 void *InsertPos = 0;
2719 DependentTemplateName *QTN =
2720 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2721
2722 if (QTN)
2723 return TemplateName(QTN);
2724
2725 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2726 if (CanonNNS == NNS) {
2727 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2728 } else {
2729 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2730 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2731 }
2732
2733 DependentTemplateNames.InsertNode(QTN, InsertPos);
2734 return TemplateName(QTN);
2735}
2736
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002737/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002738/// TargetInfo, produce the corresponding type. The unsigned @p Type
2739/// is actually a value of type @c TargetInfo::IntType.
2740QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002741 switch (Type) {
2742 case TargetInfo::NoInt: return QualType();
2743 case TargetInfo::SignedShort: return ShortTy;
2744 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2745 case TargetInfo::SignedInt: return IntTy;
2746 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2747 case TargetInfo::SignedLong: return LongTy;
2748 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2749 case TargetInfo::SignedLongLong: return LongLongTy;
2750 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2751 }
2752
2753 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002754 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002755}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002756
2757//===----------------------------------------------------------------------===//
2758// Type Predicates.
2759//===----------------------------------------------------------------------===//
2760
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002761/// isObjCNSObjectType - Return true if this is an NSObject object using
2762/// NSObject attribute on a c-style pointer type.
2763/// FIXME - Make it work directly on types.
2764///
2765bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2766 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2767 if (TypedefDecl *TD = TDT->getDecl())
2768 if (TD->getAttr<ObjCNSObjectAttr>())
2769 return true;
2770 }
2771 return false;
2772}
2773
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002774/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2775/// to an object type. This includes "id" and "Class" (two 'special' pointers
2776/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2777/// ID type).
2778bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002779 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002780 return true;
2781
Steve Naroff6ae98502008-10-21 18:24:04 +00002782 // Blocks are objects.
2783 if (Ty->isBlockPointerType())
2784 return true;
2785
2786 // All other object types are pointers.
Chris Lattner16ede0e2009-04-12 23:51:02 +00002787 const PointerType *PT = Ty->getAsPointerType();
2788 if (PT == 0)
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002789 return false;
2790
Chris Lattner16ede0e2009-04-12 23:51:02 +00002791 // If this a pointer to an interface (e.g. NSString*), it is ok.
2792 if (PT->getPointeeType()->isObjCInterfaceType() ||
2793 // If is has NSObject attribute, OK as well.
2794 isObjCNSObjectType(Ty))
2795 return true;
2796
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002797 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2798 // pointer types. This looks for the typedef specifically, not for the
Chris Lattner16ede0e2009-04-12 23:51:02 +00002799 // underlying type. Iteratively strip off typedefs so that we can handle
2800 // typedefs of typedefs.
2801 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2802 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2803 Ty.getUnqualifiedType() == getObjCClassType())
2804 return true;
2805
2806 Ty = TDT->getDecl()->getUnderlyingType();
2807 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002808
Chris Lattner16ede0e2009-04-12 23:51:02 +00002809 return false;
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002810}
2811
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002812/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2813/// garbage collection attribute.
2814///
2815QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002816 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002817 if (getLangOptions().ObjC1 &&
2818 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002819 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002820 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002821 // (or pointers to them) be treated as though they were declared
2822 // as __strong.
2823 if (GCAttrs == QualType::GCNone) {
2824 if (isObjCObjectPointerType(Ty))
2825 GCAttrs = QualType::Strong;
2826 else if (Ty->isPointerType())
2827 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2828 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00002829 // Non-pointers have none gc'able attribute regardless of the attribute
2830 // set on them.
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00002831 else if (!Ty->isPointerType() && !isObjCObjectPointerType(Ty))
Fariborz Jahanianc2112182009-04-11 00:00:54 +00002832 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002833 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002834 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002835}
2836
Chris Lattner6ac46a42008-04-07 06:51:04 +00002837//===----------------------------------------------------------------------===//
2838// Type Compatibility Testing
2839//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002840
Steve Naroff1c7d0672008-09-04 15:10:53 +00002841/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002842/// block types. Types must be strictly compatible here. For example,
2843/// C unfortunately doesn't produce an error for the following:
2844///
2845/// int (*emptyArgFunc)();
2846/// int (*intArgList)(int) = emptyArgFunc;
2847///
2848/// For blocks, we will produce an error for the following (similar to C++):
2849///
2850/// int (^emptyArgBlock)();
2851/// int (^intArgBlock)(int) = emptyArgBlock;
2852///
2853/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2854///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002855bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002856 const FunctionType *lbase = lhs->getAsFunctionType();
2857 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002858 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2859 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stumpaab0f7a2009-04-01 01:17:39 +00002860 if (lproto && rproto == 0)
2861 return false;
2862 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff1c7d0672008-09-04 15:10:53 +00002863}
2864
Chris Lattner6ac46a42008-04-07 06:51:04 +00002865/// areCompatVectorTypes - Return true if the two specified vector types are
2866/// compatible.
2867static bool areCompatVectorTypes(const VectorType *LHS,
2868 const VectorType *RHS) {
2869 assert(LHS->isCanonical() && RHS->isCanonical());
2870 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002871 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002872}
2873
Eli Friedman3d815e72008-08-22 00:56:42 +00002874/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002875/// compatible for assignment from RHS to LHS. This handles validation of any
2876/// protocol qualifiers on the LHS or RHS.
2877///
Eli Friedman3d815e72008-08-22 00:56:42 +00002878bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2879 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002880 // Verify that the base decls are compatible: the RHS must be a subclass of
2881 // the LHS.
2882 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2883 return false;
2884
2885 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2886 // protocol qualified at all, then we are good.
2887 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2888 return true;
2889
2890 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2891 // isn't a superset.
2892 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2893 return true; // FIXME: should return false!
2894
2895 // Finally, we must have two protocol-qualified interfaces.
2896 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2897 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002898
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002899 // All LHS protocols must have a presence on the RHS.
2900 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002901
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002902 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2903 LHSPE = LHSP->qual_end();
2904 LHSPI != LHSPE; LHSPI++) {
2905 bool RHSImplementsProtocol = false;
2906
2907 // If the RHS doesn't implement the protocol on the left, the types
2908 // are incompatible.
2909 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2910 RHSPE = RHSP->qual_end();
2911 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2912 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2913 RHSImplementsProtocol = true;
2914 }
2915 // FIXME: For better diagnostics, consider passing back the protocol name.
2916 if (!RHSImplementsProtocol)
2917 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002918 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002919 // The RHS implements all protocols listed on the LHS.
2920 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002921}
2922
Steve Naroff389bf462009-02-12 17:52:19 +00002923bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2924 // get the "pointed to" types
2925 const PointerType *LHSPT = LHS->getAsPointerType();
2926 const PointerType *RHSPT = RHS->getAsPointerType();
2927
2928 if (!LHSPT || !RHSPT)
2929 return false;
2930
2931 QualType lhptee = LHSPT->getPointeeType();
2932 QualType rhptee = RHSPT->getPointeeType();
2933 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2934 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2935 // ID acts sort of like void* for ObjC interfaces
2936 if (LHSIface && isObjCIdStructType(rhptee))
2937 return true;
2938 if (RHSIface && isObjCIdStructType(lhptee))
2939 return true;
2940 if (!LHSIface || !RHSIface)
2941 return false;
2942 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2943 canAssignObjCInterfaces(RHSIface, LHSIface);
2944}
2945
Steve Naroffec0550f2007-10-15 20:41:53 +00002946/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2947/// both shall have the identically qualified version of a compatible type.
2948/// C99 6.2.7p1: Two types have compatible types if their types are the
2949/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002950bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2951 return !mergeTypes(LHS, RHS).isNull();
2952}
2953
2954QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2955 const FunctionType *lbase = lhs->getAsFunctionType();
2956 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002957 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2958 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002959 bool allLTypes = true;
2960 bool allRTypes = true;
2961
2962 // Check return type
2963 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2964 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002965 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2966 allLTypes = false;
2967 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2968 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002969
2970 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00002971 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
2972 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00002973 unsigned lproto_nargs = lproto->getNumArgs();
2974 unsigned rproto_nargs = rproto->getNumArgs();
2975
2976 // Compatible functions must have the same number of arguments
2977 if (lproto_nargs != rproto_nargs)
2978 return QualType();
2979
2980 // Variadic and non-variadic functions aren't compatible
2981 if (lproto->isVariadic() != rproto->isVariadic())
2982 return QualType();
2983
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002984 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2985 return QualType();
2986
Eli Friedman3d815e72008-08-22 00:56:42 +00002987 // Check argument compatibility
2988 llvm::SmallVector<QualType, 10> types;
2989 for (unsigned i = 0; i < lproto_nargs; i++) {
2990 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2991 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2992 QualType argtype = mergeTypes(largtype, rargtype);
2993 if (argtype.isNull()) return QualType();
2994 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002995 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2996 allLTypes = false;
2997 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2998 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002999 }
3000 if (allLTypes) return lhs;
3001 if (allRTypes) return rhs;
3002 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003003 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00003004 }
3005
3006 if (lproto) allRTypes = false;
3007 if (rproto) allLTypes = false;
3008
Douglas Gregor72564e72009-02-26 23:50:07 +00003009 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00003010 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00003011 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003012 if (proto->isVariadic()) return QualType();
3013 // Check that the types are compatible with the types that
3014 // would result from default argument promotions (C99 6.7.5.3p15).
3015 // The only types actually affected are promotable integer
3016 // types and floats, which would be passed as a different
3017 // type depending on whether the prototype is visible.
3018 unsigned proto_nargs = proto->getNumArgs();
3019 for (unsigned i = 0; i < proto_nargs; ++i) {
3020 QualType argTy = proto->getArgType(i);
3021 if (argTy->isPromotableIntegerType() ||
3022 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
3023 return QualType();
3024 }
3025
3026 if (allLTypes) return lhs;
3027 if (allRTypes) return rhs;
3028 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003029 proto->getNumArgs(), lproto->isVariadic(),
3030 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00003031 }
3032
3033 if (allLTypes) return lhs;
3034 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00003035 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00003036}
3037
3038QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00003039 // C++ [expr]: If an expression initially has the type "reference to T", the
3040 // type is adjusted to "T" prior to any further analysis, the expression
3041 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003042 // expression is an lvalue unless the reference is an rvalue reference and
3043 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00003044 // FIXME: C++ shouldn't be going through here! The rules are different
3045 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003046 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3047 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00003048 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003049 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00003050 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003051 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00003052
Eli Friedman3d815e72008-08-22 00:56:42 +00003053 QualType LHSCan = getCanonicalType(LHS),
3054 RHSCan = getCanonicalType(RHS);
3055
3056 // If two types are identical, they are compatible.
3057 if (LHSCan == RHSCan)
3058 return LHS;
3059
3060 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003061 // Note that we handle extended qualifiers later, in the
3062 // case for ExtQualType.
3063 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00003064 return QualType();
3065
Eli Friedman852d63b2009-06-01 01:22:52 +00003066 Type::TypeClass LHSClass = LHSCan->getTypeClass();
3067 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003068
Chris Lattner1adb8832008-01-14 05:45:46 +00003069 // We want to consider the two function types to be the same for these
3070 // comparisons, just force one to the other.
3071 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3072 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003073
Eli Friedman07d25872009-06-02 05:28:56 +00003074 // Strip off objc_gc attributes off the top level so they can be merged.
3075 // This is a complete mess, but the attribute itself doesn't make much sense.
Fariborz Jahanian585f7b22009-06-02 01:40:22 +00003076 if (RHSClass == Type::ExtQual) {
Eli Friedman07d25872009-06-02 05:28:56 +00003077 QualType::GCAttrTypes GCAttr = RHSCan.getObjCGCAttr();
3078 if (GCAttr != QualType::GCNone) {
Fariborz Jahanian86f43852009-06-02 20:58:58 +00003079 QualType::GCAttrTypes GCLHSAttr = LHSCan.getObjCGCAttr();
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003080 // __weak attribute must appear on both declarations.
Fariborz Jahanian86f43852009-06-02 20:58:58 +00003081 // __strong attribue is redundant if other decl is an objective-c
3082 // object pointer (or decorated with __strong attribute); otherwise
3083 // issue error.
3084 if ((GCAttr == QualType::Weak && GCLHSAttr != GCAttr) ||
3085 (GCAttr == QualType::Strong && GCLHSAttr != GCAttr &&
3086 LHSCan->isPointerType() && !isObjCObjectPointerType(LHSCan) &&
3087 !isObjCIdStructType(LHSCan->getAsPointerType()->getPointeeType())))
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003088 return QualType();
3089
Eli Friedman07d25872009-06-02 05:28:56 +00003090 RHS = QualType(cast<ExtQualType>(RHS.getDesugaredType())->getBaseType(),
3091 RHS.getCVRQualifiers());
3092 QualType Result = mergeTypes(LHS, RHS);
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003093 if (!Result.isNull()) {
3094 if (Result.getObjCGCAttr() == QualType::GCNone)
3095 Result = getObjCGCQualType(Result, GCAttr);
3096 else if (Result.getObjCGCAttr() != GCAttr)
3097 Result = QualType();
3098 }
Eli Friedman07d25872009-06-02 05:28:56 +00003099 return Result;
3100 }
Fariborz Jahanian585f7b22009-06-02 01:40:22 +00003101 }
Fariborz Jahanian585f7b22009-06-02 01:40:22 +00003102 if (LHSClass == Type::ExtQual) {
Eli Friedman07d25872009-06-02 05:28:56 +00003103 QualType::GCAttrTypes GCAttr = LHSCan.getObjCGCAttr();
3104 if (GCAttr != QualType::GCNone) {
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003105 QualType::GCAttrTypes GCRHSAttr = RHSCan.getObjCGCAttr();
3106 // __weak attribute must appear on both declarations. __strong
Fariborz Jahanian86f43852009-06-02 20:58:58 +00003107 // __strong attribue is redundant if other decl is an objective-c
3108 // object pointer (or decorated with __strong attribute); otherwise
3109 // issue error.
3110 if ((GCAttr == QualType::Weak && GCRHSAttr != GCAttr) ||
3111 (GCAttr == QualType::Strong && GCRHSAttr != GCAttr &&
3112 RHSCan->isPointerType() && !isObjCObjectPointerType(RHSCan) &&
3113 !isObjCIdStructType(RHSCan->getAsPointerType()->getPointeeType())))
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003114 return QualType();
Fariborz Jahanian86f43852009-06-02 20:58:58 +00003115
Eli Friedman07d25872009-06-02 05:28:56 +00003116 LHS = QualType(cast<ExtQualType>(LHS.getDesugaredType())->getBaseType(),
3117 LHS.getCVRQualifiers());
3118 QualType Result = mergeTypes(LHS, RHS);
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003119 if (!Result.isNull()) {
3120 if (Result.getObjCGCAttr() == QualType::GCNone)
3121 Result = getObjCGCQualType(Result, GCAttr);
3122 else if (Result.getObjCGCAttr() != GCAttr)
3123 Result = QualType();
3124 }
Eli Friedman354e53d2009-06-02 07:45:37 +00003125 return Result;
Eli Friedman07d25872009-06-02 05:28:56 +00003126 }
Fariborz Jahanian585f7b22009-06-02 01:40:22 +00003127 }
3128
Eli Friedman4c721d32008-02-12 08:23:06 +00003129 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003130 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3131 LHSClass = Type::ConstantArray;
3132 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3133 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00003134
Nate Begeman213541a2008-04-18 23:10:10 +00003135 // Canonicalize ExtVector -> Vector.
3136 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3137 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00003138
Chris Lattnerb0489812008-04-07 06:38:24 +00003139 // Consider qualified interfaces and interfaces the same.
3140 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
3141 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00003142
Chris Lattnera36a61f2008-04-07 05:43:21 +00003143 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003144 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003145 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3146 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003147
Steve Naroffd824c9c2009-04-14 15:11:46 +00003148 // 'id' and 'Class' act sort of like void* for ObjC interfaces
3149 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003150 return LHS;
Steve Naroffd824c9c2009-04-14 15:11:46 +00003151 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003152 return RHS;
3153
Steve Naroffbc76dd02008-12-10 22:14:21 +00003154 // ID is compatible with all qualified id types.
3155 if (LHS->isObjCQualifiedIdType()) {
3156 if (const PointerType *PT = RHS->getAsPointerType()) {
3157 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003158 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003159 return LHS;
3160 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3161 // Unfortunately, this API is part of Sema (which we don't have access
3162 // to. Need to refactor. The following check is insufficient, since we
3163 // need to make sure the class implements the protocol.
3164 if (pType->isObjCInterfaceType())
3165 return LHS;
3166 }
3167 }
3168 if (RHS->isObjCQualifiedIdType()) {
3169 if (const PointerType *PT = LHS->getAsPointerType()) {
3170 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003171 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003172 return RHS;
3173 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3174 // Unfortunately, this API is part of Sema (which we don't have access
3175 // to. Need to refactor. The following check is insufficient, since we
3176 // need to make sure the class implements the protocol.
3177 if (pType->isObjCInterfaceType())
3178 return RHS;
3179 }
3180 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003181 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3182 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00003183 if (const EnumType* ETy = LHS->getAsEnumType()) {
3184 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3185 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003186 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003187 if (const EnumType* ETy = RHS->getAsEnumType()) {
3188 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3189 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003190 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003191
Eli Friedman3d815e72008-08-22 00:56:42 +00003192 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003193 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003194
Steve Naroff4a746782008-01-09 22:43:08 +00003195 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003196 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003197#define TYPE(Class, Base)
3198#define ABSTRACT_TYPE(Class, Base)
3199#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3200#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3201#include "clang/AST/TypeNodes.def"
3202 assert(false && "Non-canonical and dependent types shouldn't get here");
3203 return QualType();
3204
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003205 case Type::LValueReference:
3206 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003207 case Type::MemberPointer:
3208 assert(false && "C++ should never be in mergeTypes");
3209 return QualType();
3210
3211 case Type::IncompleteArray:
3212 case Type::VariableArray:
3213 case Type::FunctionProto:
3214 case Type::ExtVector:
3215 case Type::ObjCQualifiedInterface:
3216 assert(false && "Types are eliminated above");
3217 return QualType();
3218
Chris Lattner1adb8832008-01-14 05:45:46 +00003219 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003220 {
3221 // Merge two pointer types, while trying to preserve typedef info
3222 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3223 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3224 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3225 if (ResultType.isNull()) return QualType();
Eli Friedman07d25872009-06-02 05:28:56 +00003226 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00003227 return LHS;
Eli Friedman07d25872009-06-02 05:28:56 +00003228 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00003229 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003230 return getPointerType(ResultType);
3231 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003232 case Type::BlockPointer:
3233 {
3234 // Merge two block pointer types, while trying to preserve typedef info
3235 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3236 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3237 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3238 if (ResultType.isNull()) return QualType();
3239 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3240 return LHS;
3241 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3242 return RHS;
3243 return getBlockPointerType(ResultType);
3244 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003245 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003246 {
3247 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3248 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3249 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3250 return QualType();
3251
3252 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3253 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3254 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3255 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003256 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3257 return LHS;
3258 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3259 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003260 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3261 ArrayType::ArraySizeModifier(), 0);
3262 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3263 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003264 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3265 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003266 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3267 return LHS;
3268 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3269 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003270 if (LVAT) {
3271 // FIXME: This isn't correct! But tricky to implement because
3272 // the array's size has to be the size of LHS, but the type
3273 // has to be different.
3274 return LHS;
3275 }
3276 if (RVAT) {
3277 // FIXME: This isn't correct! But tricky to implement because
3278 // the array's size has to be the size of RHS, but the type
3279 // has to be different.
3280 return RHS;
3281 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003282 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3283 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003284 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003285 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003286 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003287 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003288 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003289 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003290 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003291 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3292 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003293 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003294 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003295 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003296 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003297 case Type::Complex:
3298 // Distinct complex types are incompatible.
3299 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003300 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003301 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003302 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3303 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003304 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003305 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003306 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003307 // FIXME: This should be type compatibility, e.g. whether
3308 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003309 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3310 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3311 if (LHSIface && RHSIface &&
3312 canAssignObjCInterfaces(LHSIface, RHSIface))
3313 return LHS;
3314
Eli Friedman3d815e72008-08-22 00:56:42 +00003315 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003316 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003317 case Type::ObjCQualifiedId:
3318 // Distinct qualified id's are not compatible.
3319 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003320 case Type::FixedWidthInt:
3321 // Distinct fixed-width integers are not compatible.
3322 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003323 case Type::ExtQual:
3324 // FIXME: ExtQual types can be compatible even if they're not
3325 // identical!
3326 return QualType();
3327 // First attempt at an implementation, but I'm not really sure it's
3328 // right...
3329#if 0
3330 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3331 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3332 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3333 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3334 return QualType();
3335 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3336 LHSBase = QualType(LQual->getBaseType(), 0);
3337 RHSBase = QualType(RQual->getBaseType(), 0);
3338 ResultType = mergeTypes(LHSBase, RHSBase);
3339 if (ResultType.isNull()) return QualType();
3340 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3341 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3342 return LHS;
3343 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3344 return RHS;
3345 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3346 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3347 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3348 return ResultType;
3349#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003350
3351 case Type::TemplateSpecialization:
3352 assert(false && "Dependent types have no size");
3353 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003354 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003355
3356 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003357}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003358
Chris Lattner5426bf62008-04-07 07:01:58 +00003359//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003360// Integer Predicates
3361//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003362
Eli Friedmanad74a752008-06-28 06:23:08 +00003363unsigned ASTContext::getIntWidth(QualType T) {
3364 if (T == BoolTy)
3365 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003366 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3367 return FWIT->getWidth();
3368 }
3369 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003370 return (unsigned)getTypeSize(T);
3371}
3372
3373QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3374 assert(T->isSignedIntegerType() && "Unexpected type");
3375 if (const EnumType* ETy = T->getAsEnumType())
3376 T = ETy->getDecl()->getIntegerType();
3377 const BuiltinType* BTy = T->getAsBuiltinType();
3378 assert (BTy && "Unexpected signed integer type");
3379 switch (BTy->getKind()) {
3380 case BuiltinType::Char_S:
3381 case BuiltinType::SChar:
3382 return UnsignedCharTy;
3383 case BuiltinType::Short:
3384 return UnsignedShortTy;
3385 case BuiltinType::Int:
3386 return UnsignedIntTy;
3387 case BuiltinType::Long:
3388 return UnsignedLongTy;
3389 case BuiltinType::LongLong:
3390 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00003391 case BuiltinType::Int128:
3392 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00003393 default:
3394 assert(0 && "Unexpected signed integer type");
3395 return QualType();
3396 }
3397}
3398
Douglas Gregor2cf26342009-04-09 22:27:44 +00003399ExternalASTSource::~ExternalASTSource() { }
3400
3401void ExternalASTSource::PrintStats() { }