blob: 2fc6a7d5273f6230a9a992e941c95d37963afd6c [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"
19#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000021#include "llvm/ADT/StringExtras.h"
Ted Kremenek7192f8e2007-10-31 17:10:13 +000022#include "llvm/Bitcode/Serialize.h"
23#include "llvm/Bitcode/Deserialize.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000025
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,
Steve Naroffc0ac4922009-01-27 23:20:32 +000035 bool FreeMem, unsigned size_reserve) :
Douglas Gregorab452ba2009-03-26 23:50:42 +000036 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
37 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
38 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels)
Daniel Dunbare91593e2008-08-11 04:54:23 +000039{
40 if (size_reserve > 0) Types.reserve(size_reserve);
41 InitBuiltinTypes();
Chris Lattner7644f072009-03-13 22:38:49 +000042 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.NoBuiltin);
Daniel Dunbare91593e2008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
44}
45
Reid Spencer5f016e22007-07-11 17:01:13 +000046ASTContext::~ASTContext() {
47 // Deallocate all the types.
48 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000049 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000050 Types.pop_back();
51 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000052
Nuno Lopesb74668e2008-12-17 22:30:25 +000053 {
54 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
55 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
56 while (I != E) {
57 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
58 delete R;
59 }
60 }
61
62 {
63 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
64 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
65 while (I != E) {
66 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
67 delete R;
68 }
69 }
70
71 {
72 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator
73 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
74 while (I != E) {
75 RecordDecl *R = const_cast<RecordDecl*>((I++)->second);
76 R->Destroy(*this);
77 }
78 }
79
Douglas Gregorab452ba2009-03-26 23:50:42 +000080 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000081 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
82 NNS = NestedNameSpecifiers.begin(),
83 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregore7dcd782009-03-27 23:25:45 +000084 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000085 /* Increment in loop */)
86 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000087
88 if (GlobalNestedNameSpecifier)
89 GlobalNestedNameSpecifier->Destroy(*this);
90
Eli Friedmanb26153c2008-05-27 03:08:09 +000091 TUDecl->Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000092
Reid Spencer5f016e22007-07-11 17:01:13 +000093}
94
95void ASTContext::PrintStats() const {
96 fprintf(stderr, "*** AST Context Stats:\n");
97 fprintf(stderr, " %d types total.\n", (int)Types.size());
98 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +000099 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000100 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
101 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
102
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000104 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
105 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000106 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000107
108 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
109 Type *T = Types[i];
110 if (isa<BuiltinType>(T))
111 ++NumBuiltin;
112 else if (isa<PointerType>(T))
113 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000114 else if (isa<BlockPointerType>(T))
115 ++NumBlockPointer;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000116 else if (isa<LValueReferenceType>(T))
117 ++NumLValueReference;
118 else if (isa<RValueReferenceType>(T))
119 ++NumRValueReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000120 else if (isa<MemberPointerType>(T))
121 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000122 else if (isa<ComplexType>(T))
123 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 else if (isa<ArrayType>(T))
125 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000126 else if (isa<VectorType>(T))
127 ++NumVector;
Douglas Gregor72564e72009-02-26 23:50:07 +0000128 else if (isa<FunctionNoProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 ++NumFunctionNP;
Douglas Gregor72564e72009-02-26 23:50:07 +0000130 else if (isa<FunctionProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 ++NumFunctionP;
132 else if (isa<TypedefType>(T))
133 ++NumTypeName;
134 else if (TagType *TT = dyn_cast<TagType>(T)) {
135 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000136 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000138 case TagDecl::TK_struct: ++NumTagStruct; break;
139 case TagDecl::TK_union: ++NumTagUnion; break;
140 case TagDecl::TK_class: ++NumTagClass; break;
141 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000143 } else if (isa<ObjCInterfaceType>(T))
144 ++NumObjCInterfaces;
145 else if (isa<ObjCQualifiedInterfaceType>(T))
146 ++NumObjCQualifiedInterfaces;
147 else if (isa<ObjCQualifiedIdType>(T))
148 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000149 else if (isa<TypeOfType>(T))
150 ++NumTypeOfTypes;
Douglas Gregor72564e72009-02-26 23:50:07 +0000151 else if (isa<TypeOfExprType>(T))
152 ++NumTypeOfExprTypes;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000153 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000154 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 assert(0 && "Unknown type!");
156 }
157 }
158
159 fprintf(stderr, " %d builtin types\n", NumBuiltin);
160 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000161 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000162 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
163 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000164 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000165 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000167 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
169 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
170 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
171 fprintf(stderr, " %d tagged types\n", NumTagged);
172 fprintf(stderr, " %d struct types\n", NumTagStruct);
173 fprintf(stderr, " %d union types\n", NumTagUnion);
174 fprintf(stderr, " %d class types\n", NumTagClass);
175 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000176 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000177 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000178 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000179 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000180 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000181 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor72564e72009-02-26 23:50:07 +0000182 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000183
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
185 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000186 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000187 NumLValueReference*sizeof(LValueReferenceType)+
188 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000189 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000190 NumFunctionP*sizeof(FunctionProtoType)+
191 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000192 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000193 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000194}
195
196
197void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000198 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000199}
200
Reid Spencer5f016e22007-07-11 17:01:13 +0000201void ASTContext::InitBuiltinTypes() {
202 assert(VoidTy.isNull() && "Context reinitialized?");
203
204 // C99 6.2.5p19.
205 InitBuiltinType(VoidTy, BuiltinType::Void);
206
207 // C99 6.2.5p2.
208 InitBuiltinType(BoolTy, BuiltinType::Bool);
209 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000210 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 InitBuiltinType(CharTy, BuiltinType::Char_S);
212 else
213 InitBuiltinType(CharTy, BuiltinType::Char_U);
214 // C99 6.2.5p4.
215 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
216 InitBuiltinType(ShortTy, BuiltinType::Short);
217 InitBuiltinType(IntTy, BuiltinType::Int);
218 InitBuiltinType(LongTy, BuiltinType::Long);
219 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
220
221 // C99 6.2.5p6.
222 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
223 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
224 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
225 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
226 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
227
228 // C99 6.2.5p10.
229 InitBuiltinType(FloatTy, BuiltinType::Float);
230 InitBuiltinType(DoubleTy, BuiltinType::Double);
231 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000232
Chris Lattner3a250322009-02-26 23:43:47 +0000233 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
234 InitBuiltinType(WCharTy, BuiltinType::WChar);
235 else // C99
236 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000237
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000238 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000239 InitBuiltinType(OverloadTy, BuiltinType::Overload);
240
241 // Placeholder type for type-dependent expressions whose type is
242 // completely unknown. No code should ever check a type against
243 // DependentTy and users should never see it; however, it is here to
244 // help diagnose failures to properly check for type-dependent
245 // expressions.
246 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000247
Reid Spencer5f016e22007-07-11 17:01:13 +0000248 // C99 6.2.5p11.
249 FloatComplexTy = getComplexType(FloatTy);
250 DoubleComplexTy = getComplexType(DoubleTy);
251 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000252
Steve Naroff7e219e42007-10-15 14:41:52 +0000253 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000254 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000255 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000256 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000257 ClassStructType = 0;
258
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000259 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000260
261 // void * type
262 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000263}
264
Chris Lattner464175b2007-07-18 17:52:12 +0000265//===----------------------------------------------------------------------===//
266// Type Sizing and Analysis
267//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000268
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000269/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
270/// scalar floating point type.
271const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
272 const BuiltinType *BT = T->getAsBuiltinType();
273 assert(BT && "Not a floating point type!");
274 switch (BT->getKind()) {
275 default: assert(0 && "Not a floating point type!");
276 case BuiltinType::Float: return Target.getFloatFormat();
277 case BuiltinType::Double: return Target.getDoubleFormat();
278 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
279 }
280}
281
Chris Lattneraf707ab2009-01-24 21:53:27 +0000282/// getDeclAlign - Return a conservative estimate of the alignment of the
283/// specified decl. Note that bitfields do not have a valid alignment, so
284/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000285unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000286 unsigned Align = Target.getCharWidth();
287
288 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
289 Align = std::max(Align, AA->getAlignment());
290
Chris Lattneraf707ab2009-01-24 21:53:27 +0000291 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
292 QualType T = VD->getType();
293 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000294 if (!T->isIncompleteType() && !T->isFunctionType()) {
295 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
296 T = cast<ArrayType>(T)->getElementType();
297
298 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
299 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000300 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000301
302 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000303}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000304
Chris Lattnera7674d82007-07-13 22:13:22 +0000305/// getTypeSize - Return the size of the specified type, in bits. This method
306/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000307std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000308ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000309 T = getCanonicalType(T);
Mike Stump5e301002009-02-27 18:32:39 +0000310 uint64_t Width=0;
311 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000312 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000313#define TYPE(Class, Base)
314#define ABSTRACT_TYPE(Class, Base)
315#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
316#define DEPENDENT_TYPE(Class, Base) case Type::Class:
317#include "clang/AST/TypeNodes.def"
318 assert(false && "Should not see non-canonical or dependent types");
319 break;
320
Chris Lattner692233e2007-07-13 22:27:08 +0000321 case Type::FunctionNoProto:
322 case Type::FunctionProto:
Douglas Gregor72564e72009-02-26 23:50:07 +0000323 case Type::IncompleteArray:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000324 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000325 case Type::VariableArray:
326 assert(0 && "VLAs not implemented yet!");
327 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000328 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000329
Chris Lattner98be4942008-03-05 18:54:05 +0000330 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000331 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000332 Align = EltInfo.second;
333 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000334 }
Nate Begeman213541a2008-04-18 23:10:10 +0000335 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000336 case Type::Vector: {
337 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000338 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000339 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000340 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000341 // If the alignment is not a power of 2, round up to the next power of 2.
342 // This happens for non-power-of-2 length vectors.
343 // FIXME: this should probably be a target property.
344 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000345 break;
346 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000347
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000348 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000349 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000350 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000351 case BuiltinType::Void:
352 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000353 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000354 Width = Target.getBoolWidth();
355 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000356 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000357 case BuiltinType::Char_S:
358 case BuiltinType::Char_U:
359 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000360 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000361 Width = Target.getCharWidth();
362 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000363 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000364 case BuiltinType::WChar:
365 Width = Target.getWCharWidth();
366 Align = Target.getWCharAlign();
367 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000368 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000369 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000370 Width = Target.getShortWidth();
371 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000372 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000373 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000374 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000375 Width = Target.getIntWidth();
376 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000377 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000378 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000379 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000380 Width = Target.getLongWidth();
381 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000382 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000383 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000384 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000385 Width = Target.getLongLongWidth();
386 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000387 break;
388 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000389 Width = Target.getFloatWidth();
390 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000391 break;
392 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000393 Width = Target.getDoubleWidth();
394 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000395 break;
396 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000397 Width = Target.getLongDoubleWidth();
398 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000399 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000400 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000401 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000402 case Type::FixedWidthInt:
403 // FIXME: This isn't precisely correct; the width/alignment should depend
404 // on the available types for the target
405 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000406 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000407 Align = Width;
408 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000409 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000410 // FIXME: Pointers into different addr spaces could have different sizes and
411 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000412 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000413 case Type::ObjCQualifiedId:
Eli Friedman4bdf0872009-02-22 04:02:33 +0000414 case Type::ObjCQualifiedClass:
Douglas Gregor72564e72009-02-26 23:50:07 +0000415 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000416 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000417 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000418 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000419 case Type::BlockPointer: {
420 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
421 Width = Target.getPointerWidth(AS);
422 Align = Target.getPointerAlign(AS);
423 break;
424 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000425 case Type::Pointer: {
426 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000427 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000428 Align = Target.getPointerAlign(AS);
429 break;
430 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000431 case Type::LValueReference:
432 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000433 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000434 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000435 // FIXME: This is wrong for struct layout: a reference in a struct has
436 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000437 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000438 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000439 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000440 // the GCC ABI, where pointers to data are one pointer large, pointers to
441 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000442 // other compilers too, we need to delegate this completely to TargetInfo
443 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000444 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
445 unsigned AS = Pointee.getAddressSpace();
446 Width = Target.getPointerWidth(AS);
447 if (Pointee->isFunctionType())
448 Width *= 2;
449 Align = Target.getPointerAlign(AS);
450 // GCC aligns at single pointer width.
451 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000452 case Type::Complex: {
453 // Complex types have the same alignment as their elements, but twice the
454 // size.
455 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000456 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000457 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000458 Align = EltInfo.second;
459 break;
460 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000461 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000462 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000463 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
464 Width = Layout.getSize();
465 Align = Layout.getAlignment();
466 break;
467 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000468 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000469 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000470 const TagType *TT = cast<TagType>(T);
471
472 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000473 Width = 1;
474 Align = 1;
475 break;
476 }
477
Daniel Dunbar1d751182008-11-08 05:48:37 +0000478 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000479 return getTypeInfo(ET->getDecl()->getIntegerType());
480
Daniel Dunbar1d751182008-11-08 05:48:37 +0000481 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000482 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
483 Width = Layout.getSize();
484 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000485 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000486 }
Chris Lattner71763312008-04-06 22:05:18 +0000487 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000488
Chris Lattner464175b2007-07-18 17:52:12 +0000489 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000490 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000491}
492
Chris Lattner34ebde42009-01-27 18:08:34 +0000493/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
494/// type for the current target in bits. This can be different than the ABI
495/// alignment in cases where it is beneficial for performance to overalign
496/// a data type.
497unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
498 unsigned ABIAlign = getTypeAlign(T);
499
500 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000501 if (T->isSpecificBuiltinType(BuiltinType::Double))
502 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000503
504 return ABIAlign;
505}
506
507
Devang Patel8b277042008-06-04 21:22:16 +0000508/// LayoutField - Field layout.
509void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000510 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000511 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000512 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000513 uint64_t FieldOffset = IsUnion ? 0 : Size;
514 uint64_t FieldSize;
515 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000516
517 // FIXME: Should this override struct packing? Probably we want to
518 // take the minimum?
519 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
520 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000521
522 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
523 // TODO: Need to check this algorithm on other targets!
524 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000525 FieldSize =
526 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000527
528 std::pair<uint64_t, unsigned> FieldInfo =
529 Context.getTypeInfo(FD->getType());
530 uint64_t TypeSize = FieldInfo.first;
531
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000532 // Determine the alignment of this bitfield. The packing
533 // attributes define a maximum and the alignment attribute defines
534 // a minimum.
535 // FIXME: What is the right behavior when the specified alignment
536 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000537 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000538 if (FieldPacking)
539 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000540 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
541 FieldAlign = std::max(FieldAlign, AA->getAlignment());
542
543 // Check if we need to add padding to give the field the correct
544 // alignment.
545 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
546 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
547
548 // Padding members don't affect overall alignment
549 if (!FD->getIdentifier())
550 FieldAlign = 1;
551 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000552 if (FD->getType()->isIncompleteArrayType()) {
553 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000554 // query getTypeInfo about these, so we figure it out here.
555 // Flexible array members don't have any size, but they
556 // have to be aligned appropriately for their element type.
557 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000558 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000559 FieldAlign = Context.getTypeAlign(ATy->getElementType());
560 } else {
561 std::pair<uint64_t, unsigned> FieldInfo =
562 Context.getTypeInfo(FD->getType());
563 FieldSize = FieldInfo.first;
564 FieldAlign = FieldInfo.second;
565 }
566
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000567 // Determine the alignment of this bitfield. The packing
568 // attributes define a maximum and the alignment attribute defines
569 // a minimum. Additionally, the packing alignment must be at least
570 // a byte for non-bitfields.
571 //
572 // FIXME: What is the right behavior when the specified alignment
573 // is smaller than the specified packing?
574 if (FieldPacking)
575 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000576 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
577 FieldAlign = std::max(FieldAlign, AA->getAlignment());
578
579 // Round up the current record size to the field's alignment boundary.
580 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
581 }
582
583 // Place this field at the current location.
584 FieldOffsets[FieldNo] = FieldOffset;
585
586 // Reserve space for this field.
587 if (IsUnion) {
588 Size = std::max(Size, FieldSize);
589 } else {
590 Size = FieldOffset + FieldSize;
591 }
592
593 // Remember max struct/class alignment.
594 Alignment = std::max(Alignment, FieldAlign);
595}
596
Fariborz Jahanian88e469c2009-03-05 20:08:48 +0000597void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
598 std::vector<FieldDecl*> &Fields) const {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000599 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
600 if (SuperClass)
601 CollectObjCIvars(SuperClass, Fields);
602 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
603 E = OI->ivar_end(); I != E; ++I) {
604 ObjCIvarDecl *IVDecl = (*I);
605 if (!IVDecl->isInvalidDecl())
606 Fields.push_back(cast<FieldDecl>(IVDecl));
607 }
608}
609
610/// addRecordToClass - produces record info. for the class for its
611/// ivars and all those inherited.
612///
613const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
614{
615 const RecordDecl *&RD = ASTRecordForInterface[D];
616 if (RD)
617 return RD;
618 std::vector<FieldDecl*> RecFields;
619 CollectObjCIvars(D, RecFields);
620 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
621 D->getLocation(),
622 D->getIdentifier());
623 /// FIXME! Can do collection of ivars and adding to the record while
624 /// doing it.
625 for (unsigned int i = 0; i != RecFields.size(); i++) {
626 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
627 RecFields[i]->getLocation(),
628 RecFields[i]->getIdentifier(),
629 RecFields[i]->getType(),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000630 RecFields[i]->getBitWidth(), false);
Douglas Gregor482b77d2009-01-12 23:27:07 +0000631 NewRD->addDecl(Field);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000632 }
633 NewRD->completeDefinition(*this);
634 RD = NewRD;
635 return RD;
636}
Devang Patel44a3dde2008-06-04 21:54:36 +0000637
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000638/// setFieldDecl - maps a field for the given Ivar reference node.
639//
640void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
641 const ObjCIvarDecl *Ivar,
642 const ObjCIvarRefExpr *MRef) {
643 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
644 lookupFieldDeclForIvar(*this, Ivar);
645 ASTFieldForIvarRef[MRef] = FD;
646}
647
Chris Lattner61710852008-10-05 17:34:18 +0000648/// getASTObjcInterfaceLayout - Get or compute information about the layout of
649/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000650/// position information.
651const ASTRecordLayout &
652ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
653 // Look up this layout, if already laid out, return what we have.
654 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
655 if (Entry) return *Entry;
656
657 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
658 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000659 ASTRecordLayout *NewEntry = NULL;
660 unsigned FieldCount = D->ivar_size();
661 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
662 FieldCount++;
663 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
664 unsigned Alignment = SL.getAlignment();
665 uint64_t Size = SL.getSize();
666 NewEntry = new ASTRecordLayout(Size, Alignment);
667 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000668 // Super class is at the beginning of the layout.
669 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000670 } else {
671 NewEntry = new ASTRecordLayout();
672 NewEntry->InitializeLayout(FieldCount);
673 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000674 Entry = NewEntry;
675
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000676 unsigned StructPacking = 0;
677 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
678 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000679
680 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
681 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
682 AA->getAlignment()));
683
684 // Layout each ivar sequentially.
685 unsigned i = 0;
686 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
687 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
688 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000689 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000690 }
691
692 // Finally, round the size of the total struct up to the alignment of the
693 // struct itself.
694 NewEntry->FinalizeLayout();
695 return *NewEntry;
696}
697
Devang Patel88a981b2007-11-01 19:11:01 +0000698/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000699/// specified record (struct/union/class), which indicates its size and field
700/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000701const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000702 D = D->getDefinition(*this);
703 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000704
Chris Lattner464175b2007-07-18 17:52:12 +0000705 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000706 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000707 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000708
Devang Patel88a981b2007-11-01 19:11:01 +0000709 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
710 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
711 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000712 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000713
Douglas Gregore267ff32008-12-11 20:41:00 +0000714 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor44b43212008-12-11 16:49:14 +0000715 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000716 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000717
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000718 unsigned StructPacking = 0;
719 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
720 StructPacking = PA->getAlignment();
721
Eli Friedman4bd998b2008-05-30 09:31:38 +0000722 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000723 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
724 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000725
Eli Friedman4bd998b2008-05-30 09:31:38 +0000726 // Layout each field, for now, just sequentially, respecting alignment. In
727 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000728 unsigned FieldIdx = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000729 for (RecordDecl::field_iterator Field = D->field_begin(),
730 FieldEnd = D->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000731 Field != FieldEnd; (void)++Field, ++FieldIdx)
732 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000733
734 // Finally, round the size of the total struct up to the alignment of the
735 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000736 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000737 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000738}
739
Chris Lattnera7674d82007-07-13 22:13:22 +0000740//===----------------------------------------------------------------------===//
741// Type creation/memoization methods
742//===----------------------------------------------------------------------===//
743
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000744QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000745 QualType CanT = getCanonicalType(T);
746 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000747 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000748
749 // If we are composing extended qualifiers together, merge together into one
750 // ExtQualType node.
751 unsigned CVRQuals = T.getCVRQualifiers();
752 QualType::GCAttrTypes GCAttr = QualType::GCNone;
753 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000754
Chris Lattnerb7d25532009-02-18 22:53:11 +0000755 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
756 // If this type already has an address space specified, it cannot get
757 // another one.
758 assert(EQT->getAddressSpace() == 0 &&
759 "Type cannot be in multiple addr spaces!");
760 GCAttr = EQT->getObjCGCAttr();
761 TypeNode = EQT->getBaseType();
762 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000763
Chris Lattnerb7d25532009-02-18 22:53:11 +0000764 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000765 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000766 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000767 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000768 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000769 return QualType(EXTQy, CVRQuals);
770
Christopher Lambebb97e92008-02-04 02:31:56 +0000771 // If the base type isn't canonical, this won't be a canonical type either,
772 // so fill in the canonical type field.
773 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000774 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000775 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000776
Chris Lattnerb7d25532009-02-18 22:53:11 +0000777 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000778 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000779 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000780 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000781 ExtQualType *New =
782 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000783 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000784 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000785 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000786}
787
Chris Lattnerb7d25532009-02-18 22:53:11 +0000788QualType ASTContext::getObjCGCQualType(QualType T,
789 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000790 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000791 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000792 return T;
793
Chris Lattnerb7d25532009-02-18 22:53:11 +0000794 // If we are composing extended qualifiers together, merge together into one
795 // ExtQualType node.
796 unsigned CVRQuals = T.getCVRQualifiers();
797 Type *TypeNode = T.getTypePtr();
798 unsigned AddressSpace = 0;
799
800 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
801 // If this type already has an address space specified, it cannot get
802 // another one.
803 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
804 "Type cannot be in multiple addr spaces!");
805 AddressSpace = EQT->getAddressSpace();
806 TypeNode = EQT->getBaseType();
807 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000808
809 // Check if we've already instantiated an gc qual'd type of this type.
810 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000811 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000812 void *InsertPos = 0;
813 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000814 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000815
816 // If the base type isn't canonical, this won't be a canonical type either,
817 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000818 // FIXME: Isn't this also not canonical if the base type is a array
819 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000820 QualType Canonical;
821 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000822 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000823
Chris Lattnerb7d25532009-02-18 22:53:11 +0000824 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000825 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
826 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
827 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000828 ExtQualType *New =
829 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000830 ExtQualTypes.InsertNode(New, InsertPos);
831 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000832 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000833}
Chris Lattnera7674d82007-07-13 22:13:22 +0000834
Reid Spencer5f016e22007-07-11 17:01:13 +0000835/// getComplexType - Return the uniqued reference to the type for a complex
836/// number with the specified element type.
837QualType ASTContext::getComplexType(QualType T) {
838 // Unique pointers, to guarantee there is only one pointer of a particular
839 // structure.
840 llvm::FoldingSetNodeID ID;
841 ComplexType::Profile(ID, T);
842
843 void *InsertPos = 0;
844 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
845 return QualType(CT, 0);
846
847 // If the pointee type isn't canonical, this won't be a canonical type either,
848 // so fill in the canonical type field.
849 QualType Canonical;
850 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000851 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000852
853 // Get the new insert position for the node we care about.
854 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000855 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 }
Steve Narofff83820b2009-01-27 22:08:43 +0000857 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000858 Types.push_back(New);
859 ComplexTypes.InsertNode(New, InsertPos);
860 return QualType(New, 0);
861}
862
Eli Friedmanf98aba32009-02-13 02:31:07 +0000863QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
864 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
865 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
866 FixedWidthIntType *&Entry = Map[Width];
867 if (!Entry)
868 Entry = new FixedWidthIntType(Width, Signed);
869 return QualType(Entry, 0);
870}
Reid Spencer5f016e22007-07-11 17:01:13 +0000871
872/// getPointerType - Return the uniqued reference to the type for a pointer to
873/// the specified type.
874QualType ASTContext::getPointerType(QualType T) {
875 // Unique pointers, to guarantee there is only one pointer of a particular
876 // structure.
877 llvm::FoldingSetNodeID ID;
878 PointerType::Profile(ID, T);
879
880 void *InsertPos = 0;
881 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
882 return QualType(PT, 0);
883
884 // If the pointee type isn't canonical, this won't be a canonical type either,
885 // so fill in the canonical type field.
886 QualType Canonical;
887 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000888 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000889
890 // Get the new insert position for the node we care about.
891 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000892 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000893 }
Steve Narofff83820b2009-01-27 22:08:43 +0000894 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000895 Types.push_back(New);
896 PointerTypes.InsertNode(New, InsertPos);
897 return QualType(New, 0);
898}
899
Steve Naroff5618bd42008-08-27 16:04:49 +0000900/// getBlockPointerType - Return the uniqued reference to the type for
901/// a pointer to the specified block.
902QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000903 assert(T->isFunctionType() && "block of function types only");
904 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000905 // structure.
906 llvm::FoldingSetNodeID ID;
907 BlockPointerType::Profile(ID, T);
908
909 void *InsertPos = 0;
910 if (BlockPointerType *PT =
911 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
912 return QualType(PT, 0);
913
Steve Naroff296e8d52008-08-28 19:20:44 +0000914 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000915 // type either so fill in the canonical type field.
916 QualType Canonical;
917 if (!T->isCanonical()) {
918 Canonical = getBlockPointerType(getCanonicalType(T));
919
920 // Get the new insert position for the node we care about.
921 BlockPointerType *NewIP =
922 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000923 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000924 }
Steve Narofff83820b2009-01-27 22:08:43 +0000925 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000926 Types.push_back(New);
927 BlockPointerTypes.InsertNode(New, InsertPos);
928 return QualType(New, 0);
929}
930
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000931/// getLValueReferenceType - Return the uniqued reference to the type for an
932/// lvalue reference to the specified type.
933QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000934 // Unique pointers, to guarantee there is only one pointer of a particular
935 // structure.
936 llvm::FoldingSetNodeID ID;
937 ReferenceType::Profile(ID, T);
938
939 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000940 if (LValueReferenceType *RT =
941 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000943
Reid Spencer5f016e22007-07-11 17:01:13 +0000944 // If the referencee type isn't canonical, this won't be a canonical type
945 // either, so fill in the canonical type field.
946 QualType Canonical;
947 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000948 Canonical = getLValueReferenceType(getCanonicalType(T));
949
Reid Spencer5f016e22007-07-11 17:01:13 +0000950 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000951 LValueReferenceType *NewIP =
952 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000953 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000954 }
955
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000956 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000957 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000958 LValueReferenceTypes.InsertNode(New, InsertPos);
959 return QualType(New, 0);
960}
961
962/// getRValueReferenceType - Return the uniqued reference to the type for an
963/// rvalue reference to the specified type.
964QualType ASTContext::getRValueReferenceType(QualType T) {
965 // Unique pointers, to guarantee there is only one pointer of a particular
966 // structure.
967 llvm::FoldingSetNodeID ID;
968 ReferenceType::Profile(ID, T);
969
970 void *InsertPos = 0;
971 if (RValueReferenceType *RT =
972 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
973 return QualType(RT, 0);
974
975 // If the referencee type isn't canonical, this won't be a canonical type
976 // either, so fill in the canonical type field.
977 QualType Canonical;
978 if (!T->isCanonical()) {
979 Canonical = getRValueReferenceType(getCanonicalType(T));
980
981 // Get the new insert position for the node we care about.
982 RValueReferenceType *NewIP =
983 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
984 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
985 }
986
987 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
988 Types.push_back(New);
989 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000990 return QualType(New, 0);
991}
992
Sebastian Redlf30208a2009-01-24 21:16:55 +0000993/// getMemberPointerType - Return the uniqued reference to the type for a
994/// member pointer to the specified type, in the specified class.
995QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
996{
997 // Unique pointers, to guarantee there is only one pointer of a particular
998 // structure.
999 llvm::FoldingSetNodeID ID;
1000 MemberPointerType::Profile(ID, T, Cls);
1001
1002 void *InsertPos = 0;
1003 if (MemberPointerType *PT =
1004 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1005 return QualType(PT, 0);
1006
1007 // If the pointee or class type isn't canonical, this won't be a canonical
1008 // type either, so fill in the canonical type field.
1009 QualType Canonical;
1010 if (!T->isCanonical()) {
1011 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1012
1013 // Get the new insert position for the node we care about.
1014 MemberPointerType *NewIP =
1015 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1016 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1017 }
Steve Narofff83820b2009-01-27 22:08:43 +00001018 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001019 Types.push_back(New);
1020 MemberPointerTypes.InsertNode(New, InsertPos);
1021 return QualType(New, 0);
1022}
1023
Steve Narofffb22d962007-08-30 01:06:46 +00001024/// getConstantArrayType - Return the unique reference to the type for an
1025/// array of the specified element type.
1026QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +00001027 const llvm::APInt &ArySize,
1028 ArrayType::ArraySizeModifier ASM,
1029 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001030 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001031 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001032
1033 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001034 if (ConstantArrayType *ATP =
1035 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001036 return QualType(ATP, 0);
1037
1038 // If the element type isn't canonical, this won't be a canonical type either,
1039 // so fill in the canonical type field.
1040 QualType Canonical;
1041 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001042 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001043 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001044 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001045 ConstantArrayType *NewIP =
1046 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001047 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001048 }
1049
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001050 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001051 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001052 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001053 Types.push_back(New);
1054 return QualType(New, 0);
1055}
1056
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001057/// getVariableArrayType - Returns a non-unique reference to the type for a
1058/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001059QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1060 ArrayType::ArraySizeModifier ASM,
1061 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001062 // Since we don't unique expressions, it isn't possible to unique VLA's
1063 // that have an expression provided for their size.
1064
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001065 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001066 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001067
1068 VariableArrayTypes.push_back(New);
1069 Types.push_back(New);
1070 return QualType(New, 0);
1071}
1072
Douglas Gregor898574e2008-12-05 23:32:09 +00001073/// getDependentSizedArrayType - Returns a non-unique reference to
1074/// the type for a dependently-sized array of the specified element
1075/// type. FIXME: We will need these to be uniqued, or at least
1076/// comparable, at some point.
1077QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1078 ArrayType::ArraySizeModifier ASM,
1079 unsigned EltTypeQuals) {
1080 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1081 "Size must be type- or value-dependent!");
1082
1083 // Since we don't unique expressions, it isn't possible to unique
1084 // dependently-sized array types.
1085
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001086 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001087 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1088 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001089
1090 DependentSizedArrayTypes.push_back(New);
1091 Types.push_back(New);
1092 return QualType(New, 0);
1093}
1094
Eli Friedmanc5773c42008-02-15 18:16:39 +00001095QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1096 ArrayType::ArraySizeModifier ASM,
1097 unsigned EltTypeQuals) {
1098 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001099 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001100
1101 void *InsertPos = 0;
1102 if (IncompleteArrayType *ATP =
1103 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1104 return QualType(ATP, 0);
1105
1106 // If the element type isn't canonical, this won't be a canonical type
1107 // either, so fill in the canonical type field.
1108 QualType Canonical;
1109
1110 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001111 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001112 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001113
1114 // Get the new insert position for the node we care about.
1115 IncompleteArrayType *NewIP =
1116 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001117 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001118 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001119
Steve Narofff83820b2009-01-27 22:08:43 +00001120 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001121 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001122
1123 IncompleteArrayTypes.InsertNode(New, InsertPos);
1124 Types.push_back(New);
1125 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001126}
1127
Steve Naroff73322922007-07-18 18:00:27 +00001128/// getVectorType - Return the unique reference to a vector type of
1129/// the specified element type and size. VectorType must be a built-in type.
1130QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 BuiltinType *baseType;
1132
Chris Lattnerf52ab252008-04-06 22:59:24 +00001133 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001134 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001135
1136 // Check if we've already instantiated a vector of this type.
1137 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001138 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001139 void *InsertPos = 0;
1140 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1141 return QualType(VTP, 0);
1142
1143 // If the element type isn't canonical, this won't be a canonical type either,
1144 // so fill in the canonical type field.
1145 QualType Canonical;
1146 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001147 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001148
1149 // Get the new insert position for the node we care about.
1150 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001151 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 }
Steve Narofff83820b2009-01-27 22:08:43 +00001153 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001154 VectorTypes.InsertNode(New, InsertPos);
1155 Types.push_back(New);
1156 return QualType(New, 0);
1157}
1158
Nate Begeman213541a2008-04-18 23:10:10 +00001159/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001160/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001161QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001162 BuiltinType *baseType;
1163
Chris Lattnerf52ab252008-04-06 22:59:24 +00001164 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001165 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001166
1167 // Check if we've already instantiated a vector of this type.
1168 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001169 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001170 void *InsertPos = 0;
1171 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1172 return QualType(VTP, 0);
1173
1174 // If the element type isn't canonical, this won't be a canonical type either,
1175 // so fill in the canonical type field.
1176 QualType Canonical;
1177 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001178 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001179
1180 // Get the new insert position for the node we care about.
1181 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001182 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001183 }
Steve Narofff83820b2009-01-27 22:08:43 +00001184 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001185 VectorTypes.InsertNode(New, InsertPos);
1186 Types.push_back(New);
1187 return QualType(New, 0);
1188}
1189
Douglas Gregor72564e72009-02-26 23:50:07 +00001190/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001191///
Douglas Gregor72564e72009-02-26 23:50:07 +00001192QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 // Unique functions, to guarantee there is only one function of a particular
1194 // structure.
1195 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001196 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001197
1198 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001199 if (FunctionNoProtoType *FT =
1200 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001201 return QualType(FT, 0);
1202
1203 QualType Canonical;
1204 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001205 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001206
1207 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001208 FunctionNoProtoType *NewIP =
1209 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001210 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001211 }
1212
Douglas Gregor72564e72009-02-26 23:50:07 +00001213 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001215 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 return QualType(New, 0);
1217}
1218
1219/// getFunctionType - Return a normal function type with a typed argument
1220/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001221QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001222 unsigned NumArgs, bool isVariadic,
1223 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001224 // Unique functions, to guarantee there is only one function of a particular
1225 // structure.
1226 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001227 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001228 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001229
1230 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001231 if (FunctionProtoType *FTP =
1232 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001233 return QualType(FTP, 0);
1234
1235 // Determine whether the type being created is already canonical or not.
1236 bool isCanonical = ResultTy->isCanonical();
1237 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1238 if (!ArgArray[i]->isCanonical())
1239 isCanonical = false;
1240
1241 // If this type isn't canonical, get the canonical version of it.
1242 QualType Canonical;
1243 if (!isCanonical) {
1244 llvm::SmallVector<QualType, 16> CanonicalArgs;
1245 CanonicalArgs.reserve(NumArgs);
1246 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001247 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001248
Chris Lattnerf52ab252008-04-06 22:59:24 +00001249 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001251 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001252
1253 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001254 FunctionProtoType *NewIP =
1255 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001256 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001257 }
1258
Douglas Gregor72564e72009-02-26 23:50:07 +00001259 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001260 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001261 FunctionProtoType *FTP =
1262 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001263 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001264 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001265 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001266 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001267 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001268 return QualType(FTP, 0);
1269}
1270
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001271/// getTypeDeclType - Return the unique reference to the type for the
1272/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001273QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001274 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001275 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1276
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001277 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001278 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001279 else if (isa<TemplateTypeParmDecl>(Decl)) {
1280 assert(false && "Template type parameter types are always available.");
1281 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001282 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001283
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001284 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001285 if (PrevDecl)
1286 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001287 else
1288 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001289 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001290 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1291 if (PrevDecl)
1292 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001293 else
1294 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001295 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001296 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001297 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001298
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001299 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001300 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001301}
1302
Reid Spencer5f016e22007-07-11 17:01:13 +00001303/// getTypedefType - Return the unique reference to the type for the
1304/// specified typename decl.
1305QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1306 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1307
Chris Lattnerf52ab252008-04-06 22:59:24 +00001308 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001309 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001310 Types.push_back(Decl->TypeForDecl);
1311 return QualType(Decl->TypeForDecl, 0);
1312}
1313
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001314/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001315/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001316QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001317 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1318
Steve Narofff83820b2009-01-27 22:08:43 +00001319 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001320 Types.push_back(Decl->TypeForDecl);
1321 return QualType(Decl->TypeForDecl, 0);
1322}
1323
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00001324/// buildObjCInterfaceType - Returns a new type for the interface
1325/// declaration, regardless. It also removes any previously built
1326/// record declaration so caller can rebuild it.
1327QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) {
1328 const RecordDecl *&RD = ASTRecordForInterface[Decl];
1329 if (RD)
1330 RD = 0;
1331 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
1332 Types.push_back(Decl->TypeForDecl);
1333 return QualType(Decl->TypeForDecl, 0);
1334}
1335
Douglas Gregorfab9d672009-02-05 23:33:38 +00001336/// \brief Retrieve the template type parameter type for a template
1337/// parameter with the given depth, index, and (optionally) name.
1338QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1339 IdentifierInfo *Name) {
1340 llvm::FoldingSetNodeID ID;
1341 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1342 void *InsertPos = 0;
1343 TemplateTypeParmType *TypeParm
1344 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1345
1346 if (TypeParm)
1347 return QualType(TypeParm, 0);
1348
1349 if (Name)
1350 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1351 getTemplateTypeParmType(Depth, Index));
1352 else
1353 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1354
1355 Types.push_back(TypeParm);
1356 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1357
1358 return QualType(TypeParm, 0);
1359}
1360
Douglas Gregor55f6b142009-02-09 18:46:07 +00001361QualType
1362ASTContext::getClassTemplateSpecializationType(TemplateDecl *Template,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001363 const TemplateArgument *Args,
Douglas Gregor55f6b142009-02-09 18:46:07 +00001364 unsigned NumArgs,
Douglas Gregor55f6b142009-02-09 18:46:07 +00001365 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001366 if (!Canon.isNull())
1367 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001368
Douglas Gregor55f6b142009-02-09 18:46:07 +00001369 llvm::FoldingSetNodeID ID;
Douglas Gregor40808ce2009-03-09 23:48:35 +00001370 ClassTemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
1371
Douglas Gregor55f6b142009-02-09 18:46:07 +00001372 void *InsertPos = 0;
1373 ClassTemplateSpecializationType *Spec
1374 = ClassTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
1375
1376 if (Spec)
1377 return QualType(Spec, 0);
1378
Douglas Gregor40808ce2009-03-09 23:48:35 +00001379 void *Mem = Allocate((sizeof(ClassTemplateSpecializationType) +
1380 sizeof(TemplateArgument) * NumArgs),
1381 8);
1382 Spec = new (Mem) ClassTemplateSpecializationType(Template, Args, NumArgs,
1383 Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001384 Types.push_back(Spec);
1385 ClassTemplateSpecializationTypes.InsertNode(Spec, InsertPos);
1386
1387 return QualType(Spec, 0);
1388}
1389
Douglas Gregore4e5b052009-03-19 00:18:19 +00001390QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001391ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001392 QualType NamedType) {
1393 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001394 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001395
1396 void *InsertPos = 0;
1397 QualifiedNameType *T
1398 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1399 if (T)
1400 return QualType(T, 0);
1401
Douglas Gregorab452ba2009-03-26 23:50:42 +00001402 T = new (*this) QualifiedNameType(NNS, NamedType,
1403 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001404 Types.push_back(T);
1405 QualifiedNameTypes.InsertNode(T, InsertPos);
1406 return QualType(T, 0);
1407}
1408
Douglas Gregord57959a2009-03-27 23:10:48 +00001409QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1410 const IdentifierInfo *Name,
1411 QualType Canon) {
1412 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1413
1414 if (Canon.isNull()) {
1415 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1416 if (CanonNNS != NNS)
1417 Canon = getTypenameType(CanonNNS, Name);
1418 }
1419
1420 llvm::FoldingSetNodeID ID;
1421 TypenameType::Profile(ID, NNS, Name);
1422
1423 void *InsertPos = 0;
1424 TypenameType *T
1425 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1426 if (T)
1427 return QualType(T, 0);
1428
1429 T = new (*this) TypenameType(NNS, Name, Canon);
1430 Types.push_back(T);
1431 TypenameTypes.InsertNode(T, InsertPos);
1432 return QualType(T, 0);
1433}
1434
Chris Lattner88cb27a2008-04-07 04:56:42 +00001435/// CmpProtocolNames - Comparison predicate for sorting protocols
1436/// alphabetically.
1437static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1438 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001439 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001440}
1441
1442static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1443 unsigned &NumProtocols) {
1444 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1445
1446 // Sort protocols, keyed by name.
1447 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1448
1449 // Remove duplicates.
1450 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1451 NumProtocols = ProtocolsEnd-Protocols;
1452}
1453
1454
Chris Lattner065f0d72008-04-07 04:44:08 +00001455/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1456/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001457QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1458 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001459 // Sort the protocol list alphabetically to canonicalize it.
1460 SortAndUniqueProtocols(Protocols, NumProtocols);
1461
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001462 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001463 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001464
1465 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001466 if (ObjCQualifiedInterfaceType *QT =
1467 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001468 return QualType(QT, 0);
1469
1470 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001471 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001472 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001473
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001474 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001475 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001476 return QualType(QType, 0);
1477}
1478
Chris Lattner88cb27a2008-04-07 04:56:42 +00001479/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1480/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001481QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001482 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001483 // Sort the protocol list alphabetically to canonicalize it.
1484 SortAndUniqueProtocols(Protocols, NumProtocols);
1485
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001486 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001487 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001488
1489 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001490 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001491 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001492 return QualType(QT, 0);
1493
1494 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001495 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001496 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001497 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001498 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001499 return QualType(QType, 0);
1500}
1501
Douglas Gregor72564e72009-02-26 23:50:07 +00001502/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1503/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001504/// multiple declarations that refer to "typeof(x)" all contain different
1505/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1506/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001507QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001508 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001509 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001510 Types.push_back(toe);
1511 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001512}
1513
Steve Naroff9752f252007-08-01 18:02:17 +00001514/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1515/// TypeOfType AST's. The only motivation to unique these nodes would be
1516/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1517/// an issue. This doesn't effect the type checker, since it operates
1518/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001519QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001520 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001521 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001522 Types.push_back(tot);
1523 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001524}
1525
Reid Spencer5f016e22007-07-11 17:01:13 +00001526/// getTagDeclType - Return the unique reference to the type for the
1527/// specified TagDecl (struct/union/class/enum) decl.
1528QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001529 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001530 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001531}
1532
1533/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1534/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1535/// needs to agree with the definition in <stddef.h>.
1536QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001537 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001538}
1539
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001540/// getSignedWCharType - Return the type of "signed wchar_t".
1541/// Used when in C++, as a GCC extension.
1542QualType ASTContext::getSignedWCharType() const {
1543 // FIXME: derive from "Target" ?
1544 return WCharTy;
1545}
1546
1547/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1548/// Used when in C++, as a GCC extension.
1549QualType ASTContext::getUnsignedWCharType() const {
1550 // FIXME: derive from "Target" ?
1551 return UnsignedIntTy;
1552}
1553
Chris Lattner8b9023b2007-07-13 03:05:23 +00001554/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1555/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1556QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001557 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001558}
1559
Chris Lattnere6327742008-04-02 05:18:44 +00001560//===----------------------------------------------------------------------===//
1561// Type Operators
1562//===----------------------------------------------------------------------===//
1563
Chris Lattner77c96472008-04-06 22:41:35 +00001564/// getCanonicalType - Return the canonical (structural) type corresponding to
1565/// the specified potentially non-canonical type. The non-canonical version
1566/// of a type may have many "decorated" versions of types. Decorators can
1567/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1568/// to be free of any of these, allowing two canonical types to be compared
1569/// for exact equality with a simple pointer comparison.
1570QualType ASTContext::getCanonicalType(QualType T) {
1571 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001572
1573 // If the result has type qualifiers, make sure to canonicalize them as well.
1574 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1575 if (TypeQuals == 0) return CanType;
1576
1577 // If the type qualifiers are on an array type, get the canonical type of the
1578 // array with the qualifiers applied to the element type.
1579 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1580 if (!AT)
1581 return CanType.getQualifiedType(TypeQuals);
1582
1583 // Get the canonical version of the element with the extra qualifiers on it.
1584 // This can recursively sink qualifiers through multiple levels of arrays.
1585 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1586 NewEltTy = getCanonicalType(NewEltTy);
1587
1588 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1589 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1590 CAT->getIndexTypeQualifier());
1591 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1592 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1593 IAT->getIndexTypeQualifier());
1594
Douglas Gregor898574e2008-12-05 23:32:09 +00001595 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1596 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1597 DSAT->getSizeModifier(),
1598 DSAT->getIndexTypeQualifier());
1599
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001600 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1601 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1602 VAT->getSizeModifier(),
1603 VAT->getIndexTypeQualifier());
1604}
1605
Douglas Gregord57959a2009-03-27 23:10:48 +00001606NestedNameSpecifier *
1607ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1608 if (!NNS)
1609 return 0;
1610
1611 switch (NNS->getKind()) {
1612 case NestedNameSpecifier::Identifier:
1613 // Canonicalize the prefix but keep the identifier the same.
1614 return NestedNameSpecifier::Create(*this,
1615 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1616 NNS->getAsIdentifier());
1617
1618 case NestedNameSpecifier::Namespace:
1619 // A namespace is canonical; build a nested-name-specifier with
1620 // this namespace and no prefix.
1621 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1622
1623 case NestedNameSpecifier::TypeSpec:
1624 case NestedNameSpecifier::TypeSpecWithTemplate: {
1625 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1626 NestedNameSpecifier *Prefix = 0;
1627
1628 // FIXME: This isn't the right check!
1629 if (T->isDependentType())
1630 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1631
1632 return NestedNameSpecifier::Create(*this, Prefix,
1633 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1634 T.getTypePtr());
1635 }
1636
1637 case NestedNameSpecifier::Global:
1638 // The global specifier is canonical and unique.
1639 return NNS;
1640 }
1641
1642 // Required to silence a GCC warning
1643 return 0;
1644}
1645
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001646
1647const ArrayType *ASTContext::getAsArrayType(QualType T) {
1648 // Handle the non-qualified case efficiently.
1649 if (T.getCVRQualifiers() == 0) {
1650 // Handle the common positive case fast.
1651 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1652 return AT;
1653 }
1654
1655 // Handle the common negative case fast, ignoring CVR qualifiers.
1656 QualType CType = T->getCanonicalTypeInternal();
1657
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001658 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001659 // test.
1660 if (!isa<ArrayType>(CType) &&
1661 !isa<ArrayType>(CType.getUnqualifiedType()))
1662 return 0;
1663
1664 // Apply any CVR qualifiers from the array type to the element type. This
1665 // implements C99 6.7.3p8: "If the specification of an array type includes
1666 // any type qualifiers, the element type is so qualified, not the array type."
1667
1668 // If we get here, we either have type qualifiers on the type, or we have
1669 // sugar such as a typedef in the way. If we have type qualifiers on the type
1670 // we must propagate them down into the elemeng type.
1671 unsigned CVRQuals = T.getCVRQualifiers();
1672 unsigned AddrSpace = 0;
1673 Type *Ty = T.getTypePtr();
1674
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001675 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001676 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001677 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1678 AddrSpace = EXTQT->getAddressSpace();
1679 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001680 } else {
1681 T = Ty->getDesugaredType();
1682 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1683 break;
1684 CVRQuals |= T.getCVRQualifiers();
1685 Ty = T.getTypePtr();
1686 }
1687 }
1688
1689 // If we have a simple case, just return now.
1690 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1691 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1692 return ATy;
1693
1694 // Otherwise, we have an array and we have qualifiers on it. Push the
1695 // qualifiers into the array element type and return a new array type.
1696 // Get the canonical version of the element with the extra qualifiers on it.
1697 // This can recursively sink qualifiers through multiple levels of arrays.
1698 QualType NewEltTy = ATy->getElementType();
1699 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001700 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001701 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1702
1703 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1704 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1705 CAT->getSizeModifier(),
1706 CAT->getIndexTypeQualifier()));
1707 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1708 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1709 IAT->getSizeModifier(),
1710 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001711
Douglas Gregor898574e2008-12-05 23:32:09 +00001712 if (const DependentSizedArrayType *DSAT
1713 = dyn_cast<DependentSizedArrayType>(ATy))
1714 return cast<ArrayType>(
1715 getDependentSizedArrayType(NewEltTy,
1716 DSAT->getSizeExpr(),
1717 DSAT->getSizeModifier(),
1718 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001719
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001720 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1721 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1722 VAT->getSizeModifier(),
1723 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001724}
1725
1726
Chris Lattnere6327742008-04-02 05:18:44 +00001727/// getArrayDecayedType - Return the properly qualified result of decaying the
1728/// specified array type to a pointer. This operation is non-trivial when
1729/// handling typedefs etc. The canonical type of "T" must be an array type,
1730/// this returns a pointer to a properly qualified element of the array.
1731///
1732/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1733QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001734 // Get the element type with 'getAsArrayType' so that we don't lose any
1735 // typedefs in the element type of the array. This also handles propagation
1736 // of type qualifiers from the array type into the element type if present
1737 // (C99 6.7.3p8).
1738 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1739 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001740
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001741 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001742
1743 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001744 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001745}
1746
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001747QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001748 QualType ElemTy = VAT->getElementType();
1749
1750 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1751 return getBaseElementType(VAT);
1752
1753 return ElemTy;
1754}
1755
Reid Spencer5f016e22007-07-11 17:01:13 +00001756/// getFloatingRank - Return a relative rank for floating point types.
1757/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001758static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001759 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001760 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001761
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001762 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001763 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001764 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001765 case BuiltinType::Float: return FloatRank;
1766 case BuiltinType::Double: return DoubleRank;
1767 case BuiltinType::LongDouble: return LongDoubleRank;
1768 }
1769}
1770
Steve Naroff716c7302007-08-27 01:41:48 +00001771/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1772/// point or a complex type (based on typeDomain/typeSize).
1773/// 'typeDomain' is a real floating point or complex type.
1774/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001775QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1776 QualType Domain) const {
1777 FloatingRank EltRank = getFloatingRank(Size);
1778 if (Domain->isComplexType()) {
1779 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001780 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001781 case FloatRank: return FloatComplexTy;
1782 case DoubleRank: return DoubleComplexTy;
1783 case LongDoubleRank: return LongDoubleComplexTy;
1784 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001785 }
Chris Lattner1361b112008-04-06 23:58:54 +00001786
1787 assert(Domain->isRealFloatingType() && "Unknown domain!");
1788 switch (EltRank) {
1789 default: assert(0 && "getFloatingRank(): illegal value for rank");
1790 case FloatRank: return FloatTy;
1791 case DoubleRank: return DoubleTy;
1792 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001793 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001794}
1795
Chris Lattner7cfeb082008-04-06 23:55:33 +00001796/// getFloatingTypeOrder - Compare the rank of the two specified floating
1797/// point types, ignoring the domain of the type (i.e. 'double' ==
1798/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1799/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001800int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1801 FloatingRank LHSR = getFloatingRank(LHS);
1802 FloatingRank RHSR = getFloatingRank(RHS);
1803
1804 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001805 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001806 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001807 return 1;
1808 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001809}
1810
Chris Lattnerf52ab252008-04-06 22:59:24 +00001811/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1812/// routine will assert if passed a built-in type that isn't an integer or enum,
1813/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001814unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001815 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001816 if (EnumType* ET = dyn_cast<EnumType>(T))
1817 T = ET->getDecl()->getIntegerType().getTypePtr();
1818
1819 // There are two things which impact the integer rank: the width, and
1820 // the ordering of builtins. The builtin ordering is encoded in the
1821 // bottom three bits; the width is encoded in the bits above that.
1822 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1823 return FWIT->getWidth() << 3;
1824 }
1825
Chris Lattnerf52ab252008-04-06 22:59:24 +00001826 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001827 default: assert(0 && "getIntegerRank(): not a built-in integer");
1828 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001829 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001830 case BuiltinType::Char_S:
1831 case BuiltinType::Char_U:
1832 case BuiltinType::SChar:
1833 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001834 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001835 case BuiltinType::Short:
1836 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001837 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001838 case BuiltinType::Int:
1839 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001840 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001841 case BuiltinType::Long:
1842 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001843 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001844 case BuiltinType::LongLong:
1845 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001846 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001847 }
1848}
1849
Chris Lattner7cfeb082008-04-06 23:55:33 +00001850/// getIntegerTypeOrder - Returns the highest ranked integer type:
1851/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1852/// LHS < RHS, return -1.
1853int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001854 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1855 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001856 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001857
Chris Lattnerf52ab252008-04-06 22:59:24 +00001858 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1859 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001860
Chris Lattner7cfeb082008-04-06 23:55:33 +00001861 unsigned LHSRank = getIntegerRank(LHSC);
1862 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001863
Chris Lattner7cfeb082008-04-06 23:55:33 +00001864 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1865 if (LHSRank == RHSRank) return 0;
1866 return LHSRank > RHSRank ? 1 : -1;
1867 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001868
Chris Lattner7cfeb082008-04-06 23:55:33 +00001869 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1870 if (LHSUnsigned) {
1871 // If the unsigned [LHS] type is larger, return it.
1872 if (LHSRank >= RHSRank)
1873 return 1;
1874
1875 // If the signed type can represent all values of the unsigned type, it
1876 // wins. Because we are dealing with 2's complement and types that are
1877 // powers of two larger than each other, this is always safe.
1878 return -1;
1879 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001880
Chris Lattner7cfeb082008-04-06 23:55:33 +00001881 // If the unsigned [RHS] type is larger, return it.
1882 if (RHSRank >= LHSRank)
1883 return -1;
1884
1885 // If the signed type can represent all values of the unsigned type, it
1886 // wins. Because we are dealing with 2's complement and types that are
1887 // powers of two larger than each other, this is always safe.
1888 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001889}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001890
1891// getCFConstantStringType - Return the type used for constant CFStrings.
1892QualType ASTContext::getCFConstantStringType() {
1893 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001894 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001895 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001896 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001897 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001898
1899 // const int *isa;
1900 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001901 // int flags;
1902 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001903 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001904 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001905 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001906 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001907
Anders Carlsson71993dd2007-08-17 05:31:46 +00001908 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001909 for (unsigned i = 0; i < 4; ++i) {
1910 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1911 SourceLocation(), 0,
1912 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001913 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001914 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001915 }
1916
1917 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001918 }
1919
1920 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001921}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001922
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001923QualType ASTContext::getObjCFastEnumerationStateType()
1924{
1925 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001926 ObjCFastEnumerationStateTypeDecl =
1927 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1928 &Idents.get("__objcFastEnumerationState"));
1929
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001930 QualType FieldTypes[] = {
1931 UnsignedLongTy,
1932 getPointerType(ObjCIdType),
1933 getPointerType(UnsignedLongTy),
1934 getConstantArrayType(UnsignedLongTy,
1935 llvm::APInt(32, 5), ArrayType::Normal, 0)
1936 };
1937
Douglas Gregor44b43212008-12-11 16:49:14 +00001938 for (size_t i = 0; i < 4; ++i) {
1939 FieldDecl *Field = FieldDecl::Create(*this,
1940 ObjCFastEnumerationStateTypeDecl,
1941 SourceLocation(), 0,
1942 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001943 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001944 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001945 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001946
Douglas Gregor44b43212008-12-11 16:49:14 +00001947 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001948 }
1949
1950 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1951}
1952
Anders Carlssone8c49532007-10-29 06:33:42 +00001953// This returns true if a type has been typedefed to BOOL:
1954// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001955static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001956 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00001957 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1958 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001959
1960 return false;
1961}
1962
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001963/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001964/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001965int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001966 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001967
1968 // Make all integer and enum types at least as large as an int
1969 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001970 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001971 // Treat arrays as pointers, since that's how they're passed in.
1972 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001973 sz = getTypeSize(VoidPtrTy);
1974 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001975}
1976
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001977/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001978/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001979void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001980 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001981 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001982 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001983 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001984 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001985 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001986 // Compute size of all parameters.
1987 // Start with computing size of a pointer in number of bytes.
1988 // FIXME: There might(should) be a better way of doing this computation!
1989 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001990 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001991 // The first two arguments (self and _cmd) are pointers; account for
1992 // their size.
1993 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00001994 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1995 E = Decl->param_end(); PI != E; ++PI) {
1996 QualType PType = (*PI)->getType();
1997 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001998 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001999 ParmOffset += sz;
2000 }
2001 S += llvm::utostr(ParmOffset);
2002 S += "@0:";
2003 S += llvm::utostr(PtrSize);
2004
2005 // Argument types.
2006 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002007 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2008 E = Decl->param_end(); PI != E; ++PI) {
2009 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002010 QualType PType = PVDecl->getOriginalType();
2011 if (const ArrayType *AT =
2012 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
2013 // Use array's original type only if it has known number of
2014 // elements.
2015 if (!dyn_cast<ConstantArrayType>(AT))
2016 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002017 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002018 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002019 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002020 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002021 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002022 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002023 }
2024}
2025
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002026/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002027/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002028/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2029/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002030/// Property attributes are stored as a comma-delimited C string. The simple
2031/// attributes readonly and bycopy are encoded as single characters. The
2032/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2033/// encoded as single characters, followed by an identifier. Property types
2034/// are also encoded as a parametrized attribute. The characters used to encode
2035/// these attributes are defined by the following enumeration:
2036/// @code
2037/// enum PropertyAttributes {
2038/// kPropertyReadOnly = 'R', // property is read-only.
2039/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2040/// kPropertyByref = '&', // property is a reference to the value last assigned
2041/// kPropertyDynamic = 'D', // property is dynamic
2042/// kPropertyGetter = 'G', // followed by getter selector name
2043/// kPropertySetter = 'S', // followed by setter selector name
2044/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2045/// kPropertyType = 't' // followed by old-style type encoding.
2046/// kPropertyWeak = 'W' // 'weak' property
2047/// kPropertyStrong = 'P' // property GC'able
2048/// kPropertyNonAtomic = 'N' // property non-atomic
2049/// };
2050/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002051void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2052 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002053 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002054 // Collect information from the property implementation decl(s).
2055 bool Dynamic = false;
2056 ObjCPropertyImplDecl *SynthesizePID = 0;
2057
2058 // FIXME: Duplicated code due to poor abstraction.
2059 if (Container) {
2060 if (const ObjCCategoryImplDecl *CID =
2061 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2062 for (ObjCCategoryImplDecl::propimpl_iterator
2063 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2064 ObjCPropertyImplDecl *PID = *i;
2065 if (PID->getPropertyDecl() == PD) {
2066 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2067 Dynamic = true;
2068 } else {
2069 SynthesizePID = PID;
2070 }
2071 }
2072 }
2073 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002074 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002075 for (ObjCCategoryImplDecl::propimpl_iterator
2076 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2077 ObjCPropertyImplDecl *PID = *i;
2078 if (PID->getPropertyDecl() == PD) {
2079 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2080 Dynamic = true;
2081 } else {
2082 SynthesizePID = PID;
2083 }
2084 }
2085 }
2086 }
2087 }
2088
2089 // FIXME: This is not very efficient.
2090 S = "T";
2091
2092 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002093 // GCC has some special rules regarding encoding of properties which
2094 // closely resembles encoding of ivars.
2095 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
2096 true /* outermost type */,
2097 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002098
2099 if (PD->isReadOnly()) {
2100 S += ",R";
2101 } else {
2102 switch (PD->getSetterKind()) {
2103 case ObjCPropertyDecl::Assign: break;
2104 case ObjCPropertyDecl::Copy: S += ",C"; break;
2105 case ObjCPropertyDecl::Retain: S += ",&"; break;
2106 }
2107 }
2108
2109 // It really isn't clear at all what this means, since properties
2110 // are "dynamic by default".
2111 if (Dynamic)
2112 S += ",D";
2113
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002114 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2115 S += ",N";
2116
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002117 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2118 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002119 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002120 }
2121
2122 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2123 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002124 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002125 }
2126
2127 if (SynthesizePID) {
2128 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2129 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002130 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002131 }
2132
2133 // FIXME: OBJCGC: weak & strong
2134}
2135
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002136/// getLegacyIntegralTypeEncoding -
2137/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002138/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002139/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2140///
2141void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2142 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2143 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002144 if (BT->getKind() == BuiltinType::ULong &&
2145 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002146 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002147 else
2148 if (BT->getKind() == BuiltinType::Long &&
2149 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002150 PointeeTy = IntTy;
2151 }
2152 }
2153}
2154
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002155void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002156 FieldDecl *Field) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002157 // We follow the behavior of gcc, expanding structures which are
2158 // directly pointed to, and expanding embedded structures. Note that
2159 // these rules are sufficient to prevent recursive encoding of the
2160 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002161 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2162 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002163}
2164
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002165static void EncodeBitField(const ASTContext *Context, std::string& S,
2166 FieldDecl *FD) {
2167 const Expr *E = FD->getBitWidth();
2168 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2169 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2170 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2171 S += 'b';
2172 S += llvm::utostr(N);
2173}
2174
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002175void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2176 bool ExpandPointedToStructures,
2177 bool ExpandStructures,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002178 FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002179 bool OutermostType,
2180 bool EncodingProperty) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00002181 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002182 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002183 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002184 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002185 else {
2186 char encoding;
2187 switch (BT->getKind()) {
2188 default: assert(0 && "Unhandled builtin type kind");
2189 case BuiltinType::Void: encoding = 'v'; break;
2190 case BuiltinType::Bool: encoding = 'B'; break;
2191 case BuiltinType::Char_U:
2192 case BuiltinType::UChar: encoding = 'C'; break;
2193 case BuiltinType::UShort: encoding = 'S'; break;
2194 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002195 case BuiltinType::ULong:
2196 encoding =
2197 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2198 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002199 case BuiltinType::ULongLong: encoding = 'Q'; break;
2200 case BuiltinType::Char_S:
2201 case BuiltinType::SChar: encoding = 'c'; break;
2202 case BuiltinType::Short: encoding = 's'; break;
2203 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002204 case BuiltinType::Long:
2205 encoding =
2206 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2207 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002208 case BuiltinType::LongLong: encoding = 'q'; break;
2209 case BuiltinType::Float: encoding = 'f'; break;
2210 case BuiltinType::Double: encoding = 'd'; break;
2211 case BuiltinType::LongDouble: encoding = 'd'; break;
2212 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002213
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002214 S += encoding;
2215 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002216 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002217 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002218 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2219 ExpandPointedToStructures,
2220 ExpandStructures, FD);
2221 if (FD || EncodingProperty) {
2222 // Note that we do extended encoding of protocol qualifer list
2223 // Only when doing ivar or property encoding.
2224 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2225 S += '"';
2226 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2227 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2228 S += '<';
2229 S += Proto->getNameAsString();
2230 S += '>';
2231 }
2232 S += '"';
2233 }
2234 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002235 }
2236 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002237 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002238 bool isReadOnly = false;
2239 // For historical/compatibility reasons, the read-only qualifier of the
2240 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2241 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2242 // Also, do not emit the 'r' for anything but the outermost type!
2243 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2244 if (OutermostType && T.isConstQualified()) {
2245 isReadOnly = true;
2246 S += 'r';
2247 }
2248 }
2249 else if (OutermostType) {
2250 QualType P = PointeeTy;
2251 while (P->getAsPointerType())
2252 P = P->getAsPointerType()->getPointeeType();
2253 if (P.isConstQualified()) {
2254 isReadOnly = true;
2255 S += 'r';
2256 }
2257 }
2258 if (isReadOnly) {
2259 // Another legacy compatibility encoding. Some ObjC qualifier and type
2260 // combinations need to be rearranged.
2261 // Rewrite "in const" from "nr" to "rn"
2262 const char * s = S.c_str();
2263 int len = S.length();
2264 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2265 std::string replace = "rn";
2266 S.replace(S.end()-2, S.end(), replace);
2267 }
2268 }
Steve Naroff389bf462009-02-12 17:52:19 +00002269 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002270 S += '@';
2271 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002272 }
2273 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002274 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002275 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002276 // Another historical/compatibility reason.
2277 // We encode the underlying type which comes out as
2278 // {...};
2279 S += '^';
2280 getObjCEncodingForTypeImpl(PointeeTy, S,
2281 false, ExpandPointedToStructures,
2282 NULL);
2283 return;
2284 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002285 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002286 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002287 const ObjCInterfaceType *OIT =
2288 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002289 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002290 S += '"';
2291 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002292 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2293 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2294 S += '<';
2295 S += Proto->getNameAsString();
2296 S += '>';
2297 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002298 S += '"';
2299 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002300 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002301 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002302 S += '#';
2303 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002304 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002305 S += ':';
2306 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002307 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002308
2309 if (PointeeTy->isCharType()) {
2310 // char pointer types should be encoded as '*' unless it is a
2311 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002312 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002313 S += '*';
2314 return;
2315 }
2316 }
2317
2318 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002319 getLegacyIntegralTypeEncoding(PointeeTy);
2320
2321 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002322 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002323 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002324 } else if (const ArrayType *AT =
2325 // Ignore type qualifiers etc.
2326 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002327 if (isa<IncompleteArrayType>(AT)) {
2328 // Incomplete arrays are encoded as a pointer to the array element.
2329 S += '^';
2330
2331 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2332 false, ExpandStructures, FD);
2333 } else {
2334 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002335
Anders Carlsson559a8332009-02-22 01:38:57 +00002336 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2337 S += llvm::utostr(CAT->getSize().getZExtValue());
2338 else {
2339 //Variable length arrays are encoded as a regular array with 0 elements.
2340 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2341 S += '0';
2342 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002343
Anders Carlsson559a8332009-02-22 01:38:57 +00002344 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2345 false, ExpandStructures, FD);
2346 S += ']';
2347 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002348 } else if (T->getAsFunctionType()) {
2349 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002350 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002351 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002352 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002353 // Anonymous structures print as '?'
2354 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2355 S += II->getName();
2356 } else {
2357 S += '?';
2358 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002359 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002360 S += '=';
Douglas Gregor44b43212008-12-11 16:49:14 +00002361 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2362 FieldEnd = RDecl->field_end();
2363 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002364 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002365 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002366 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002367 S += '"';
2368 }
2369
2370 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002371 if (Field->isBitField()) {
2372 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2373 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002374 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002375 QualType qt = Field->getType();
2376 getLegacyIntegralTypeEncoding(qt);
2377 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002378 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002379 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002380 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002381 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002382 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002383 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002384 if (FD && FD->isBitField())
2385 EncodeBitField(this, S, FD);
2386 else
2387 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002388 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002389 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002390 } else if (T->isObjCInterfaceType()) {
2391 // @encode(class_name)
2392 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2393 S += '{';
2394 const IdentifierInfo *II = OI->getIdentifier();
2395 S += II->getName();
2396 S += '=';
2397 std::vector<FieldDecl*> RecFields;
2398 CollectObjCIvars(OI, RecFields);
2399 for (unsigned int i = 0; i != RecFields.size(); i++) {
2400 if (RecFields[i]->isBitField())
2401 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2402 RecFields[i]);
2403 else
2404 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2405 FD);
2406 }
2407 S += '}';
2408 }
2409 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002410 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002411}
2412
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002413void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002414 std::string& S) const {
2415 if (QT & Decl::OBJC_TQ_In)
2416 S += 'n';
2417 if (QT & Decl::OBJC_TQ_Inout)
2418 S += 'N';
2419 if (QT & Decl::OBJC_TQ_Out)
2420 S += 'o';
2421 if (QT & Decl::OBJC_TQ_Bycopy)
2422 S += 'O';
2423 if (QT & Decl::OBJC_TQ_Byref)
2424 S += 'R';
2425 if (QT & Decl::OBJC_TQ_Oneway)
2426 S += 'V';
2427}
2428
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002429void ASTContext::setBuiltinVaListType(QualType T)
2430{
2431 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2432
2433 BuiltinVaListType = T;
2434}
2435
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002436void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002437{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002438 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002439
2440 // typedef struct objc_object *id;
2441 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002442 // User error - caller will issue diagnostics.
2443 if (!ptr)
2444 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002445 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002446 // User error - caller will issue diagnostics.
2447 if (!rec)
2448 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002449 IdStructType = rec;
2450}
2451
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002452void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002453{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002454 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002455
2456 // typedef struct objc_selector *SEL;
2457 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002458 if (!ptr)
2459 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002460 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002461 if (!rec)
2462 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002463 SelStructType = rec;
2464}
2465
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002466void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002467{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002468 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002469}
2470
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002471void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002472{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002473 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002474
2475 // typedef struct objc_class *Class;
2476 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2477 assert(ptr && "'Class' incorrectly typed");
2478 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2479 assert(rec && "'Class' incorrectly typed");
2480 ClassStructType = rec;
2481}
2482
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002483void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2484 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002485 "'NSConstantString' type already set!");
2486
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002487 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002488}
2489
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002490/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002491/// TargetInfo, produce the corresponding type. The unsigned @p Type
2492/// is actually a value of type @c TargetInfo::IntType.
2493QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002494 switch (Type) {
2495 case TargetInfo::NoInt: return QualType();
2496 case TargetInfo::SignedShort: return ShortTy;
2497 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2498 case TargetInfo::SignedInt: return IntTy;
2499 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2500 case TargetInfo::SignedLong: return LongTy;
2501 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2502 case TargetInfo::SignedLongLong: return LongLongTy;
2503 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2504 }
2505
2506 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002507 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002508}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002509
2510//===----------------------------------------------------------------------===//
2511// Type Predicates.
2512//===----------------------------------------------------------------------===//
2513
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002514/// isObjCNSObjectType - Return true if this is an NSObject object using
2515/// NSObject attribute on a c-style pointer type.
2516/// FIXME - Make it work directly on types.
2517///
2518bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2519 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2520 if (TypedefDecl *TD = TDT->getDecl())
2521 if (TD->getAttr<ObjCNSObjectAttr>())
2522 return true;
2523 }
2524 return false;
2525}
2526
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002527/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2528/// to an object type. This includes "id" and "Class" (two 'special' pointers
2529/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2530/// ID type).
2531bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002532 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002533 return true;
2534
Steve Naroff6ae98502008-10-21 18:24:04 +00002535 // Blocks are objects.
2536 if (Ty->isBlockPointerType())
2537 return true;
2538
2539 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002540 if (!Ty->isPointerType())
2541 return false;
2542
2543 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2544 // pointer types. This looks for the typedef specifically, not for the
2545 // underlying type.
Eli Friedman5fdeae12009-03-22 23:00:19 +00002546 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2547 Ty.getUnqualifiedType() == getObjCClassType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002548 return true;
2549
2550 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002551 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2552 return true;
2553
2554 // If is has NSObject attribute, OK as well.
2555 return isObjCNSObjectType(Ty);
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002556}
2557
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002558/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2559/// garbage collection attribute.
2560///
2561QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002562 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002563 if (getLangOptions().ObjC1 &&
2564 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002565 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002566 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002567 // (or pointers to them) be treated as though they were declared
2568 // as __strong.
2569 if (GCAttrs == QualType::GCNone) {
2570 if (isObjCObjectPointerType(Ty))
2571 GCAttrs = QualType::Strong;
2572 else if (Ty->isPointerType())
2573 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2574 }
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002575 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002576 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002577}
2578
Chris Lattner6ac46a42008-04-07 06:51:04 +00002579//===----------------------------------------------------------------------===//
2580// Type Compatibility Testing
2581//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002582
Steve Naroff1c7d0672008-09-04 15:10:53 +00002583/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002584/// block types. Types must be strictly compatible here. For example,
2585/// C unfortunately doesn't produce an error for the following:
2586///
2587/// int (*emptyArgFunc)();
2588/// int (*intArgList)(int) = emptyArgFunc;
2589///
2590/// For blocks, we will produce an error for the following (similar to C++):
2591///
2592/// int (^emptyArgBlock)();
2593/// int (^intArgBlock)(int) = emptyArgBlock;
2594///
2595/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2596///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002597bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002598 const FunctionType *lbase = lhs->getAsFunctionType();
2599 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002600 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2601 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Steve Naroffc0febd52008-12-10 17:49:55 +00002602 if (lproto && rproto)
2603 return !mergeTypes(lhs, rhs).isNull();
2604 return false;
Steve Naroff1c7d0672008-09-04 15:10:53 +00002605}
2606
Chris Lattner6ac46a42008-04-07 06:51:04 +00002607/// areCompatVectorTypes - Return true if the two specified vector types are
2608/// compatible.
2609static bool areCompatVectorTypes(const VectorType *LHS,
2610 const VectorType *RHS) {
2611 assert(LHS->isCanonical() && RHS->isCanonical());
2612 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002613 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002614}
2615
Eli Friedman3d815e72008-08-22 00:56:42 +00002616/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002617/// compatible for assignment from RHS to LHS. This handles validation of any
2618/// protocol qualifiers on the LHS or RHS.
2619///
Eli Friedman3d815e72008-08-22 00:56:42 +00002620bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2621 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002622 // Verify that the base decls are compatible: the RHS must be a subclass of
2623 // the LHS.
2624 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2625 return false;
2626
2627 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2628 // protocol qualified at all, then we are good.
2629 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2630 return true;
2631
2632 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2633 // isn't a superset.
2634 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2635 return true; // FIXME: should return false!
2636
2637 // Finally, we must have two protocol-qualified interfaces.
2638 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2639 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002640
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002641 // All LHS protocols must have a presence on the RHS.
2642 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002643
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002644 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2645 LHSPE = LHSP->qual_end();
2646 LHSPI != LHSPE; LHSPI++) {
2647 bool RHSImplementsProtocol = false;
2648
2649 // If the RHS doesn't implement the protocol on the left, the types
2650 // are incompatible.
2651 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2652 RHSPE = RHSP->qual_end();
2653 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2654 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2655 RHSImplementsProtocol = true;
2656 }
2657 // FIXME: For better diagnostics, consider passing back the protocol name.
2658 if (!RHSImplementsProtocol)
2659 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002660 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002661 // The RHS implements all protocols listed on the LHS.
2662 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002663}
2664
Steve Naroff389bf462009-02-12 17:52:19 +00002665bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2666 // get the "pointed to" types
2667 const PointerType *LHSPT = LHS->getAsPointerType();
2668 const PointerType *RHSPT = RHS->getAsPointerType();
2669
2670 if (!LHSPT || !RHSPT)
2671 return false;
2672
2673 QualType lhptee = LHSPT->getPointeeType();
2674 QualType rhptee = RHSPT->getPointeeType();
2675 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2676 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2677 // ID acts sort of like void* for ObjC interfaces
2678 if (LHSIface && isObjCIdStructType(rhptee))
2679 return true;
2680 if (RHSIface && isObjCIdStructType(lhptee))
2681 return true;
2682 if (!LHSIface || !RHSIface)
2683 return false;
2684 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2685 canAssignObjCInterfaces(RHSIface, LHSIface);
2686}
2687
Steve Naroffec0550f2007-10-15 20:41:53 +00002688/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2689/// both shall have the identically qualified version of a compatible type.
2690/// C99 6.2.7p1: Two types have compatible types if their types are the
2691/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002692bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2693 return !mergeTypes(LHS, RHS).isNull();
2694}
2695
2696QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2697 const FunctionType *lbase = lhs->getAsFunctionType();
2698 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002699 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2700 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002701 bool allLTypes = true;
2702 bool allRTypes = true;
2703
2704 // Check return type
2705 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2706 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002707 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2708 allLTypes = false;
2709 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2710 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002711
2712 if (lproto && rproto) { // two C99 style function prototypes
2713 unsigned lproto_nargs = lproto->getNumArgs();
2714 unsigned rproto_nargs = rproto->getNumArgs();
2715
2716 // Compatible functions must have the same number of arguments
2717 if (lproto_nargs != rproto_nargs)
2718 return QualType();
2719
2720 // Variadic and non-variadic functions aren't compatible
2721 if (lproto->isVariadic() != rproto->isVariadic())
2722 return QualType();
2723
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002724 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2725 return QualType();
2726
Eli Friedman3d815e72008-08-22 00:56:42 +00002727 // Check argument compatibility
2728 llvm::SmallVector<QualType, 10> types;
2729 for (unsigned i = 0; i < lproto_nargs; i++) {
2730 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2731 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2732 QualType argtype = mergeTypes(largtype, rargtype);
2733 if (argtype.isNull()) return QualType();
2734 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002735 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2736 allLTypes = false;
2737 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2738 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002739 }
2740 if (allLTypes) return lhs;
2741 if (allRTypes) return rhs;
2742 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002743 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002744 }
2745
2746 if (lproto) allRTypes = false;
2747 if (rproto) allLTypes = false;
2748
Douglas Gregor72564e72009-02-26 23:50:07 +00002749 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002750 if (proto) {
2751 if (proto->isVariadic()) return QualType();
2752 // Check that the types are compatible with the types that
2753 // would result from default argument promotions (C99 6.7.5.3p15).
2754 // The only types actually affected are promotable integer
2755 // types and floats, which would be passed as a different
2756 // type depending on whether the prototype is visible.
2757 unsigned proto_nargs = proto->getNumArgs();
2758 for (unsigned i = 0; i < proto_nargs; ++i) {
2759 QualType argTy = proto->getArgType(i);
2760 if (argTy->isPromotableIntegerType() ||
2761 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2762 return QualType();
2763 }
2764
2765 if (allLTypes) return lhs;
2766 if (allRTypes) return rhs;
2767 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002768 proto->getNumArgs(), lproto->isVariadic(),
2769 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002770 }
2771
2772 if (allLTypes) return lhs;
2773 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002774 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002775}
2776
2777QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002778 // C++ [expr]: If an expression initially has the type "reference to T", the
2779 // type is adjusted to "T" prior to any further analysis, the expression
2780 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002781 // expression is an lvalue unless the reference is an rvalue reference and
2782 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00002783 // FIXME: C++ shouldn't be going through here! The rules are different
2784 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002785 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2786 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00002787 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002788 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002789 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002790 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002791
Eli Friedman3d815e72008-08-22 00:56:42 +00002792 QualType LHSCan = getCanonicalType(LHS),
2793 RHSCan = getCanonicalType(RHS);
2794
2795 // If two types are identical, they are compatible.
2796 if (LHSCan == RHSCan)
2797 return LHS;
2798
2799 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002800 // Note that we handle extended qualifiers later, in the
2801 // case for ExtQualType.
2802 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00002803 return QualType();
2804
2805 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2806 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2807
Chris Lattner1adb8832008-01-14 05:45:46 +00002808 // We want to consider the two function types to be the same for these
2809 // comparisons, just force one to the other.
2810 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2811 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002812
2813 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002814 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2815 LHSClass = Type::ConstantArray;
2816 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2817 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002818
Nate Begeman213541a2008-04-18 23:10:10 +00002819 // Canonicalize ExtVector -> Vector.
2820 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2821 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002822
Chris Lattnerb0489812008-04-07 06:38:24 +00002823 // Consider qualified interfaces and interfaces the same.
2824 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2825 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002826
Chris Lattnera36a61f2008-04-07 05:43:21 +00002827 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002828 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002829 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2830 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2831
2832 // ID acts sort of like void* for ObjC interfaces
2833 if (LHSIface && isObjCIdStructType(RHS))
2834 return LHS;
2835 if (RHSIface && isObjCIdStructType(LHS))
2836 return RHS;
2837
Steve Naroffbc76dd02008-12-10 22:14:21 +00002838 // ID is compatible with all qualified id types.
2839 if (LHS->isObjCQualifiedIdType()) {
2840 if (const PointerType *PT = RHS->getAsPointerType()) {
2841 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002842 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002843 return LHS;
2844 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2845 // Unfortunately, this API is part of Sema (which we don't have access
2846 // to. Need to refactor. The following check is insufficient, since we
2847 // need to make sure the class implements the protocol.
2848 if (pType->isObjCInterfaceType())
2849 return LHS;
2850 }
2851 }
2852 if (RHS->isObjCQualifiedIdType()) {
2853 if (const PointerType *PT = LHS->getAsPointerType()) {
2854 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002855 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002856 return RHS;
2857 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2858 // Unfortunately, this API is part of Sema (which we don't have access
2859 // to. Need to refactor. The following check is insufficient, since we
2860 // need to make sure the class implements the protocol.
2861 if (pType->isObjCInterfaceType())
2862 return RHS;
2863 }
2864 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002865 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2866 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002867 if (const EnumType* ETy = LHS->getAsEnumType()) {
2868 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2869 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002870 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002871 if (const EnumType* ETy = RHS->getAsEnumType()) {
2872 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2873 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002874 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002875
Eli Friedman3d815e72008-08-22 00:56:42 +00002876 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002877 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002878
Steve Naroff4a746782008-01-09 22:43:08 +00002879 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002880 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00002881#define TYPE(Class, Base)
2882#define ABSTRACT_TYPE(Class, Base)
2883#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2884#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2885#include "clang/AST/TypeNodes.def"
2886 assert(false && "Non-canonical and dependent types shouldn't get here");
2887 return QualType();
2888
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002889 case Type::LValueReference:
2890 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00002891 case Type::MemberPointer:
2892 assert(false && "C++ should never be in mergeTypes");
2893 return QualType();
2894
2895 case Type::IncompleteArray:
2896 case Type::VariableArray:
2897 case Type::FunctionProto:
2898 case Type::ExtVector:
2899 case Type::ObjCQualifiedInterface:
2900 assert(false && "Types are eliminated above");
2901 return QualType();
2902
Chris Lattner1adb8832008-01-14 05:45:46 +00002903 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002904 {
2905 // Merge two pointer types, while trying to preserve typedef info
2906 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2907 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2908 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2909 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002910 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2911 return LHS;
2912 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2913 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002914 return getPointerType(ResultType);
2915 }
Steve Naroffc0febd52008-12-10 17:49:55 +00002916 case Type::BlockPointer:
2917 {
2918 // Merge two block pointer types, while trying to preserve typedef info
2919 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2920 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2921 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2922 if (ResultType.isNull()) return QualType();
2923 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2924 return LHS;
2925 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2926 return RHS;
2927 return getBlockPointerType(ResultType);
2928 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002929 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002930 {
2931 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2932 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2933 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2934 return QualType();
2935
2936 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2937 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2938 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2939 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002940 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2941 return LHS;
2942 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2943 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002944 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2945 ArrayType::ArraySizeModifier(), 0);
2946 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2947 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002948 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2949 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00002950 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2951 return LHS;
2952 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2953 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002954 if (LVAT) {
2955 // FIXME: This isn't correct! But tricky to implement because
2956 // the array's size has to be the size of LHS, but the type
2957 // has to be different.
2958 return LHS;
2959 }
2960 if (RVAT) {
2961 // FIXME: This isn't correct! But tricky to implement because
2962 // the array's size has to be the size of RHS, but the type
2963 // has to be different.
2964 return RHS;
2965 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002966 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2967 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00002968 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002969 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002970 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002971 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00002972 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00002973 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00002974 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00002975 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
2976 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002977 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002978 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002979 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002980 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00002981 case Type::Complex:
2982 // Distinct complex types are incompatible.
2983 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002984 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002985 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00002986 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2987 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00002988 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00002989 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002990 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002991 // FIXME: This should be type compatibility, e.g. whether
2992 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00002993 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2994 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2995 if (LHSIface && RHSIface &&
2996 canAssignObjCInterfaces(LHSIface, RHSIface))
2997 return LHS;
2998
Eli Friedman3d815e72008-08-22 00:56:42 +00002999 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003000 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003001 case Type::ObjCQualifiedId:
3002 // Distinct qualified id's are not compatible.
3003 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003004 case Type::FixedWidthInt:
3005 // Distinct fixed-width integers are not compatible.
3006 return QualType();
3007 case Type::ObjCQualifiedClass:
3008 // Distinct qualified classes are not compatible.
3009 return QualType();
3010 case Type::ExtQual:
3011 // FIXME: ExtQual types can be compatible even if they're not
3012 // identical!
3013 return QualType();
3014 // First attempt at an implementation, but I'm not really sure it's
3015 // right...
3016#if 0
3017 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3018 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3019 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3020 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3021 return QualType();
3022 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3023 LHSBase = QualType(LQual->getBaseType(), 0);
3024 RHSBase = QualType(RQual->getBaseType(), 0);
3025 ResultType = mergeTypes(LHSBase, RHSBase);
3026 if (ResultType.isNull()) return QualType();
3027 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3028 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3029 return LHS;
3030 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3031 return RHS;
3032 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3033 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3034 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3035 return ResultType;
3036#endif
Steve Naroffec0550f2007-10-15 20:41:53 +00003037 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003038
3039 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003040}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003041
Chris Lattner5426bf62008-04-07 07:01:58 +00003042//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003043// Integer Predicates
3044//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003045
Eli Friedmanad74a752008-06-28 06:23:08 +00003046unsigned ASTContext::getIntWidth(QualType T) {
3047 if (T == BoolTy)
3048 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003049 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3050 return FWIT->getWidth();
3051 }
3052 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003053 return (unsigned)getTypeSize(T);
3054}
3055
3056QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3057 assert(T->isSignedIntegerType() && "Unexpected type");
3058 if (const EnumType* ETy = T->getAsEnumType())
3059 T = ETy->getDecl()->getIntegerType();
3060 const BuiltinType* BTy = T->getAsBuiltinType();
3061 assert (BTy && "Unexpected signed integer type");
3062 switch (BTy->getKind()) {
3063 case BuiltinType::Char_S:
3064 case BuiltinType::SChar:
3065 return UnsignedCharTy;
3066 case BuiltinType::Short:
3067 return UnsignedShortTy;
3068 case BuiltinType::Int:
3069 return UnsignedIntTy;
3070 case BuiltinType::Long:
3071 return UnsignedLongTy;
3072 case BuiltinType::LongLong:
3073 return UnsignedLongLongTy;
3074 default:
3075 assert(0 && "Unexpected signed integer type");
3076 return QualType();
3077 }
3078}
3079
3080
3081//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00003082// Serialization Support
3083//===----------------------------------------------------------------------===//
3084
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003085/// Emit - Serialize an ASTContext object to Bitcode.
3086void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003087 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00003088 S.EmitRef(SourceMgr);
3089 S.EmitRef(Target);
3090 S.EmitRef(Idents);
3091 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003092
Ted Kremenekfee04522007-10-31 22:44:07 +00003093 // Emit the size of the type vector so that we can reserve that size
3094 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00003095 S.EmitInt(Types.size());
3096
Ted Kremenek03ed4402007-11-13 22:02:55 +00003097 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3098 I!=E;++I)
3099 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00003100
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003101 S.EmitOwnedPtr(TUDecl);
3102
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003103 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003104}
3105
Ted Kremenek0f84c002007-11-13 00:25:37 +00003106ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003107
3108 // Read the language options.
3109 LangOptions LOpts;
3110 LOpts.Read(D);
3111
Ted Kremenekfee04522007-10-31 22:44:07 +00003112 SourceManager &SM = D.ReadRef<SourceManager>();
3113 TargetInfo &t = D.ReadRef<TargetInfo>();
3114 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3115 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00003116
Ted Kremenekfee04522007-10-31 22:44:07 +00003117 unsigned size_reserve = D.ReadInt();
3118
Douglas Gregor2e1cd422008-11-17 14:58:09 +00003119 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3120 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00003121
Ted Kremenek03ed4402007-11-13 22:02:55 +00003122 for (unsigned i = 0; i < size_reserve; ++i)
3123 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00003124
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003125 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3126
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003127 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00003128
3129 return A;
3130}