blob: 84976a029449d8c0a732104772c6f960cd55036c [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.
81 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
82 NNS = NestedNameSpecifiers.begin(),
83 NNSEnd = NestedNameSpecifiers.end();
84 NNS != NNSEnd; ++NNS)
85 NNS->Destroy(*this);
86
87 if (GlobalNestedNameSpecifier)
88 GlobalNestedNameSpecifier->Destroy(*this);
89
Eli Friedmanb26153c2008-05-27 03:08:09 +000090 TUDecl->Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000091
Reid Spencer5f016e22007-07-11 17:01:13 +000092}
93
94void ASTContext::PrintStats() const {
95 fprintf(stderr, "*** AST Context Stats:\n");
96 fprintf(stderr, " %d types total.\n", (int)Types.size());
97 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +000098 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +000099 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
100 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
101
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000103 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
104 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000105 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000106
107 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
108 Type *T = Types[i];
109 if (isa<BuiltinType>(T))
110 ++NumBuiltin;
111 else if (isa<PointerType>(T))
112 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000113 else if (isa<BlockPointerType>(T))
114 ++NumBlockPointer;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000115 else if (isa<LValueReferenceType>(T))
116 ++NumLValueReference;
117 else if (isa<RValueReferenceType>(T))
118 ++NumRValueReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000119 else if (isa<MemberPointerType>(T))
120 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000121 else if (isa<ComplexType>(T))
122 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 else if (isa<ArrayType>(T))
124 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000125 else if (isa<VectorType>(T))
126 ++NumVector;
Douglas Gregor72564e72009-02-26 23:50:07 +0000127 else if (isa<FunctionNoProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 ++NumFunctionNP;
Douglas Gregor72564e72009-02-26 23:50:07 +0000129 else if (isa<FunctionProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 ++NumFunctionP;
131 else if (isa<TypedefType>(T))
132 ++NumTypeName;
133 else if (TagType *TT = dyn_cast<TagType>(T)) {
134 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000135 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000137 case TagDecl::TK_struct: ++NumTagStruct; break;
138 case TagDecl::TK_union: ++NumTagUnion; break;
139 case TagDecl::TK_class: ++NumTagClass; break;
140 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000142 } else if (isa<ObjCInterfaceType>(T))
143 ++NumObjCInterfaces;
144 else if (isa<ObjCQualifiedInterfaceType>(T))
145 ++NumObjCQualifiedInterfaces;
146 else if (isa<ObjCQualifiedIdType>(T))
147 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000148 else if (isa<TypeOfType>(T))
149 ++NumTypeOfTypes;
Douglas Gregor72564e72009-02-26 23:50:07 +0000150 else if (isa<TypeOfExprType>(T))
151 ++NumTypeOfExprTypes;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000152 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000153 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 assert(0 && "Unknown type!");
155 }
156 }
157
158 fprintf(stderr, " %d builtin types\n", NumBuiltin);
159 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000160 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000161 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
162 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000163 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000164 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000166 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
168 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
169 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
170 fprintf(stderr, " %d tagged types\n", NumTagged);
171 fprintf(stderr, " %d struct types\n", NumTagStruct);
172 fprintf(stderr, " %d union types\n", NumTagUnion);
173 fprintf(stderr, " %d class types\n", NumTagClass);
174 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000175 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000176 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000177 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000178 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000179 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000180 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor72564e72009-02-26 23:50:07 +0000181 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000182
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
184 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000185 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000186 NumLValueReference*sizeof(LValueReferenceType)+
187 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000188 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000189 NumFunctionP*sizeof(FunctionProtoType)+
190 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000191 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000192 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000193}
194
195
196void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000197 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000198}
199
Reid Spencer5f016e22007-07-11 17:01:13 +0000200void ASTContext::InitBuiltinTypes() {
201 assert(VoidTy.isNull() && "Context reinitialized?");
202
203 // C99 6.2.5p19.
204 InitBuiltinType(VoidTy, BuiltinType::Void);
205
206 // C99 6.2.5p2.
207 InitBuiltinType(BoolTy, BuiltinType::Bool);
208 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000209 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 InitBuiltinType(CharTy, BuiltinType::Char_S);
211 else
212 InitBuiltinType(CharTy, BuiltinType::Char_U);
213 // C99 6.2.5p4.
214 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
215 InitBuiltinType(ShortTy, BuiltinType::Short);
216 InitBuiltinType(IntTy, BuiltinType::Int);
217 InitBuiltinType(LongTy, BuiltinType::Long);
218 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
219
220 // C99 6.2.5p6.
221 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
222 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
223 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
224 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
225 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
226
227 // C99 6.2.5p10.
228 InitBuiltinType(FloatTy, BuiltinType::Float);
229 InitBuiltinType(DoubleTy, BuiltinType::Double);
230 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000231
Chris Lattner3a250322009-02-26 23:43:47 +0000232 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
233 InitBuiltinType(WCharTy, BuiltinType::WChar);
234 else // C99
235 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000236
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000237 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000238 InitBuiltinType(OverloadTy, BuiltinType::Overload);
239
240 // Placeholder type for type-dependent expressions whose type is
241 // completely unknown. No code should ever check a type against
242 // DependentTy and users should never see it; however, it is here to
243 // help diagnose failures to properly check for type-dependent
244 // expressions.
245 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000246
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 // C99 6.2.5p11.
248 FloatComplexTy = getComplexType(FloatTy);
249 DoubleComplexTy = getComplexType(DoubleTy);
250 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000251
Steve Naroff7e219e42007-10-15 14:41:52 +0000252 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000253 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000254 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000255 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000256 ClassStructType = 0;
257
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000258 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000259
260 // void * type
261 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000262}
263
Chris Lattner464175b2007-07-18 17:52:12 +0000264//===----------------------------------------------------------------------===//
265// Type Sizing and Analysis
266//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000267
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000268/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
269/// scalar floating point type.
270const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
271 const BuiltinType *BT = T->getAsBuiltinType();
272 assert(BT && "Not a floating point type!");
273 switch (BT->getKind()) {
274 default: assert(0 && "Not a floating point type!");
275 case BuiltinType::Float: return Target.getFloatFormat();
276 case BuiltinType::Double: return Target.getDoubleFormat();
277 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
278 }
279}
280
Chris Lattneraf707ab2009-01-24 21:53:27 +0000281/// getDeclAlign - Return a conservative estimate of the alignment of the
282/// specified decl. Note that bitfields do not have a valid alignment, so
283/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000284unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000285 unsigned Align = Target.getCharWidth();
286
287 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
288 Align = std::max(Align, AA->getAlignment());
289
Chris Lattneraf707ab2009-01-24 21:53:27 +0000290 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
291 QualType T = VD->getType();
292 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000293 if (!T->isIncompleteType() && !T->isFunctionType()) {
294 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
295 T = cast<ArrayType>(T)->getElementType();
296
297 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
298 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000299 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000300
301 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000302}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000303
Chris Lattnera7674d82007-07-13 22:13:22 +0000304/// getTypeSize - Return the size of the specified type, in bits. This method
305/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000306std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000307ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000308 T = getCanonicalType(T);
Mike Stump5e301002009-02-27 18:32:39 +0000309 uint64_t Width=0;
310 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000311 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000312#define TYPE(Class, Base)
313#define ABSTRACT_TYPE(Class, Base)
314#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
315#define DEPENDENT_TYPE(Class, Base) case Type::Class:
316#include "clang/AST/TypeNodes.def"
317 assert(false && "Should not see non-canonical or dependent types");
318 break;
319
Chris Lattner692233e2007-07-13 22:27:08 +0000320 case Type::FunctionNoProto:
321 case Type::FunctionProto:
Douglas Gregor72564e72009-02-26 23:50:07 +0000322 case Type::IncompleteArray:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000323 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000324 case Type::VariableArray:
325 assert(0 && "VLAs not implemented yet!");
326 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000327 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000328
Chris Lattner98be4942008-03-05 18:54:05 +0000329 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000330 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000331 Align = EltInfo.second;
332 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000333 }
Nate Begeman213541a2008-04-18 23:10:10 +0000334 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000335 case Type::Vector: {
336 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000337 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000338 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000339 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000340 // If the alignment is not a power of 2, round up to the next power of 2.
341 // This happens for non-power-of-2 length vectors.
342 // FIXME: this should probably be a target property.
343 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000344 break;
345 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000346
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000347 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000348 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000349 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000350 case BuiltinType::Void:
351 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000352 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000353 Width = Target.getBoolWidth();
354 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000355 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000356 case BuiltinType::Char_S:
357 case BuiltinType::Char_U:
358 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000359 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000360 Width = Target.getCharWidth();
361 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000362 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000363 case BuiltinType::WChar:
364 Width = Target.getWCharWidth();
365 Align = Target.getWCharAlign();
366 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000367 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000368 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000369 Width = Target.getShortWidth();
370 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000371 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000372 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000373 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000374 Width = Target.getIntWidth();
375 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000376 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000377 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000378 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000379 Width = Target.getLongWidth();
380 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000381 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000382 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000383 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000384 Width = Target.getLongLongWidth();
385 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000386 break;
387 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000388 Width = Target.getFloatWidth();
389 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000390 break;
391 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000392 Width = Target.getDoubleWidth();
393 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000394 break;
395 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000396 Width = Target.getLongDoubleWidth();
397 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000398 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000399 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000400 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000401 case Type::FixedWidthInt:
402 // FIXME: This isn't precisely correct; the width/alignment should depend
403 // on the available types for the target
404 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000405 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000406 Align = Width;
407 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000408 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000409 // FIXME: Pointers into different addr spaces could have different sizes and
410 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000411 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000412 case Type::ObjCQualifiedId:
Eli Friedman4bdf0872009-02-22 04:02:33 +0000413 case Type::ObjCQualifiedClass:
Douglas Gregor72564e72009-02-26 23:50:07 +0000414 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000415 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000416 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000417 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000418 case Type::BlockPointer: {
419 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
420 Width = Target.getPointerWidth(AS);
421 Align = Target.getPointerAlign(AS);
422 break;
423 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000424 case Type::Pointer: {
425 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000426 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000427 Align = Target.getPointerAlign(AS);
428 break;
429 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000430 case Type::LValueReference:
431 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000432 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000433 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000434 // FIXME: This is wrong for struct layout: a reference in a struct has
435 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000436 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000437 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000438 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000439 // the GCC ABI, where pointers to data are one pointer large, pointers to
440 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000441 // other compilers too, we need to delegate this completely to TargetInfo
442 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000443 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
444 unsigned AS = Pointee.getAddressSpace();
445 Width = Target.getPointerWidth(AS);
446 if (Pointee->isFunctionType())
447 Width *= 2;
448 Align = Target.getPointerAlign(AS);
449 // GCC aligns at single pointer width.
450 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000451 case Type::Complex: {
452 // Complex types have the same alignment as their elements, but twice the
453 // size.
454 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000455 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000456 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000457 Align = EltInfo.second;
458 break;
459 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000460 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000461 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000462 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
463 Width = Layout.getSize();
464 Align = Layout.getAlignment();
465 break;
466 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000467 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000468 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000469 const TagType *TT = cast<TagType>(T);
470
471 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000472 Width = 1;
473 Align = 1;
474 break;
475 }
476
Daniel Dunbar1d751182008-11-08 05:48:37 +0000477 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000478 return getTypeInfo(ET->getDecl()->getIntegerType());
479
Daniel Dunbar1d751182008-11-08 05:48:37 +0000480 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000481 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
482 Width = Layout.getSize();
483 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000484 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000485 }
Chris Lattner71763312008-04-06 22:05:18 +0000486 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000487
Chris Lattner464175b2007-07-18 17:52:12 +0000488 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000489 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000490}
491
Chris Lattner34ebde42009-01-27 18:08:34 +0000492/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
493/// type for the current target in bits. This can be different than the ABI
494/// alignment in cases where it is beneficial for performance to overalign
495/// a data type.
496unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
497 unsigned ABIAlign = getTypeAlign(T);
498
499 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000500 if (T->isSpecificBuiltinType(BuiltinType::Double))
501 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000502
503 return ABIAlign;
504}
505
506
Devang Patel8b277042008-06-04 21:22:16 +0000507/// LayoutField - Field layout.
508void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000509 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000510 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000511 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000512 uint64_t FieldOffset = IsUnion ? 0 : Size;
513 uint64_t FieldSize;
514 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000515
516 // FIXME: Should this override struct packing? Probably we want to
517 // take the minimum?
518 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
519 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000520
521 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
522 // TODO: Need to check this algorithm on other targets!
523 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000524 FieldSize =
525 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000526
527 std::pair<uint64_t, unsigned> FieldInfo =
528 Context.getTypeInfo(FD->getType());
529 uint64_t TypeSize = FieldInfo.first;
530
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000531 // Determine the alignment of this bitfield. The packing
532 // attributes define a maximum and the alignment attribute defines
533 // a minimum.
534 // FIXME: What is the right behavior when the specified alignment
535 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000536 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000537 if (FieldPacking)
538 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000539 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
540 FieldAlign = std::max(FieldAlign, AA->getAlignment());
541
542 // Check if we need to add padding to give the field the correct
543 // alignment.
544 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
545 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
546
547 // Padding members don't affect overall alignment
548 if (!FD->getIdentifier())
549 FieldAlign = 1;
550 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000551 if (FD->getType()->isIncompleteArrayType()) {
552 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000553 // query getTypeInfo about these, so we figure it out here.
554 // Flexible array members don't have any size, but they
555 // have to be aligned appropriately for their element type.
556 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000557 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000558 FieldAlign = Context.getTypeAlign(ATy->getElementType());
559 } else {
560 std::pair<uint64_t, unsigned> FieldInfo =
561 Context.getTypeInfo(FD->getType());
562 FieldSize = FieldInfo.first;
563 FieldAlign = FieldInfo.second;
564 }
565
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000566 // Determine the alignment of this bitfield. The packing
567 // attributes define a maximum and the alignment attribute defines
568 // a minimum. Additionally, the packing alignment must be at least
569 // a byte for non-bitfields.
570 //
571 // FIXME: What is the right behavior when the specified alignment
572 // is smaller than the specified packing?
573 if (FieldPacking)
574 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000575 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
576 FieldAlign = std::max(FieldAlign, AA->getAlignment());
577
578 // Round up the current record size to the field's alignment boundary.
579 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
580 }
581
582 // Place this field at the current location.
583 FieldOffsets[FieldNo] = FieldOffset;
584
585 // Reserve space for this field.
586 if (IsUnion) {
587 Size = std::max(Size, FieldSize);
588 } else {
589 Size = FieldOffset + FieldSize;
590 }
591
592 // Remember max struct/class alignment.
593 Alignment = std::max(Alignment, FieldAlign);
594}
595
Fariborz Jahanian88e469c2009-03-05 20:08:48 +0000596void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
597 std::vector<FieldDecl*> &Fields) const {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000598 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
599 if (SuperClass)
600 CollectObjCIvars(SuperClass, Fields);
601 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
602 E = OI->ivar_end(); I != E; ++I) {
603 ObjCIvarDecl *IVDecl = (*I);
604 if (!IVDecl->isInvalidDecl())
605 Fields.push_back(cast<FieldDecl>(IVDecl));
606 }
607}
608
609/// addRecordToClass - produces record info. for the class for its
610/// ivars and all those inherited.
611///
612const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
613{
614 const RecordDecl *&RD = ASTRecordForInterface[D];
615 if (RD)
616 return RD;
617 std::vector<FieldDecl*> RecFields;
618 CollectObjCIvars(D, RecFields);
619 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
620 D->getLocation(),
621 D->getIdentifier());
622 /// FIXME! Can do collection of ivars and adding to the record while
623 /// doing it.
624 for (unsigned int i = 0; i != RecFields.size(); i++) {
625 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
626 RecFields[i]->getLocation(),
627 RecFields[i]->getIdentifier(),
628 RecFields[i]->getType(),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000629 RecFields[i]->getBitWidth(), false);
Douglas Gregor482b77d2009-01-12 23:27:07 +0000630 NewRD->addDecl(Field);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000631 }
632 NewRD->completeDefinition(*this);
633 RD = NewRD;
634 return RD;
635}
Devang Patel44a3dde2008-06-04 21:54:36 +0000636
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000637/// setFieldDecl - maps a field for the given Ivar reference node.
638//
639void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
640 const ObjCIvarDecl *Ivar,
641 const ObjCIvarRefExpr *MRef) {
642 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
643 lookupFieldDeclForIvar(*this, Ivar);
644 ASTFieldForIvarRef[MRef] = FD;
645}
646
Chris Lattner61710852008-10-05 17:34:18 +0000647/// getASTObjcInterfaceLayout - Get or compute information about the layout of
648/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000649/// position information.
650const ASTRecordLayout &
651ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
652 // Look up this layout, if already laid out, return what we have.
653 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
654 if (Entry) return *Entry;
655
656 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
657 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000658 ASTRecordLayout *NewEntry = NULL;
659 unsigned FieldCount = D->ivar_size();
660 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
661 FieldCount++;
662 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
663 unsigned Alignment = SL.getAlignment();
664 uint64_t Size = SL.getSize();
665 NewEntry = new ASTRecordLayout(Size, Alignment);
666 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000667 // Super class is at the beginning of the layout.
668 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000669 } else {
670 NewEntry = new ASTRecordLayout();
671 NewEntry->InitializeLayout(FieldCount);
672 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000673 Entry = NewEntry;
674
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000675 unsigned StructPacking = 0;
676 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
677 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000678
679 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
680 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
681 AA->getAlignment()));
682
683 // Layout each ivar sequentially.
684 unsigned i = 0;
685 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
686 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
687 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000688 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000689 }
690
691 // Finally, round the size of the total struct up to the alignment of the
692 // struct itself.
693 NewEntry->FinalizeLayout();
694 return *NewEntry;
695}
696
Devang Patel88a981b2007-11-01 19:11:01 +0000697/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000698/// specified record (struct/union/class), which indicates its size and field
699/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000700const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000701 D = D->getDefinition(*this);
702 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000703
Chris Lattner464175b2007-07-18 17:52:12 +0000704 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000705 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000706 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000707
Devang Patel88a981b2007-11-01 19:11:01 +0000708 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
709 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
710 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000711 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000712
Douglas Gregore267ff32008-12-11 20:41:00 +0000713 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor44b43212008-12-11 16:49:14 +0000714 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000715 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000716
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000717 unsigned StructPacking = 0;
718 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
719 StructPacking = PA->getAlignment();
720
Eli Friedman4bd998b2008-05-30 09:31:38 +0000721 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000722 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
723 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000724
Eli Friedman4bd998b2008-05-30 09:31:38 +0000725 // Layout each field, for now, just sequentially, respecting alignment. In
726 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000727 unsigned FieldIdx = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000728 for (RecordDecl::field_iterator Field = D->field_begin(),
729 FieldEnd = D->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000730 Field != FieldEnd; (void)++Field, ++FieldIdx)
731 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000732
733 // Finally, round the size of the total struct up to the alignment of the
734 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000735 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000736 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000737}
738
Chris Lattnera7674d82007-07-13 22:13:22 +0000739//===----------------------------------------------------------------------===//
740// Type creation/memoization methods
741//===----------------------------------------------------------------------===//
742
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000743QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000744 QualType CanT = getCanonicalType(T);
745 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000746 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000747
748 // If we are composing extended qualifiers together, merge together into one
749 // ExtQualType node.
750 unsigned CVRQuals = T.getCVRQualifiers();
751 QualType::GCAttrTypes GCAttr = QualType::GCNone;
752 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000753
Chris Lattnerb7d25532009-02-18 22:53:11 +0000754 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
755 // If this type already has an address space specified, it cannot get
756 // another one.
757 assert(EQT->getAddressSpace() == 0 &&
758 "Type cannot be in multiple addr spaces!");
759 GCAttr = EQT->getObjCGCAttr();
760 TypeNode = EQT->getBaseType();
761 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000762
Chris Lattnerb7d25532009-02-18 22:53:11 +0000763 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000764 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000765 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000766 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000767 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000768 return QualType(EXTQy, CVRQuals);
769
Christopher Lambebb97e92008-02-04 02:31:56 +0000770 // If the base type isn't canonical, this won't be a canonical type either,
771 // so fill in the canonical type field.
772 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000773 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000774 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000775
Chris Lattnerb7d25532009-02-18 22:53:11 +0000776 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000777 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000778 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000779 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000780 ExtQualType *New =
781 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000782 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000783 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000784 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000785}
786
Chris Lattnerb7d25532009-02-18 22:53:11 +0000787QualType ASTContext::getObjCGCQualType(QualType T,
788 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000789 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000790 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000791 return T;
792
Chris Lattnerb7d25532009-02-18 22:53:11 +0000793 // If we are composing extended qualifiers together, merge together into one
794 // ExtQualType node.
795 unsigned CVRQuals = T.getCVRQualifiers();
796 Type *TypeNode = T.getTypePtr();
797 unsigned AddressSpace = 0;
798
799 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
800 // If this type already has an address space specified, it cannot get
801 // another one.
802 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
803 "Type cannot be in multiple addr spaces!");
804 AddressSpace = EQT->getAddressSpace();
805 TypeNode = EQT->getBaseType();
806 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000807
808 // Check if we've already instantiated an gc qual'd type of this type.
809 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000810 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000811 void *InsertPos = 0;
812 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000813 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000814
815 // If the base type isn't canonical, this won't be a canonical type either,
816 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000817 // FIXME: Isn't this also not canonical if the base type is a array
818 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000819 QualType Canonical;
820 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000821 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000822
Chris Lattnerb7d25532009-02-18 22:53:11 +0000823 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000824 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
825 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
826 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000827 ExtQualType *New =
828 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000829 ExtQualTypes.InsertNode(New, InsertPos);
830 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000831 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000832}
Chris Lattnera7674d82007-07-13 22:13:22 +0000833
Reid Spencer5f016e22007-07-11 17:01:13 +0000834/// getComplexType - Return the uniqued reference to the type for a complex
835/// number with the specified element type.
836QualType ASTContext::getComplexType(QualType T) {
837 // Unique pointers, to guarantee there is only one pointer of a particular
838 // structure.
839 llvm::FoldingSetNodeID ID;
840 ComplexType::Profile(ID, T);
841
842 void *InsertPos = 0;
843 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
844 return QualType(CT, 0);
845
846 // If the pointee type isn't canonical, this won't be a canonical type either,
847 // so fill in the canonical type field.
848 QualType Canonical;
849 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000850 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000851
852 // Get the new insert position for the node we care about.
853 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000854 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000855 }
Steve Narofff83820b2009-01-27 22:08:43 +0000856 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 Types.push_back(New);
858 ComplexTypes.InsertNode(New, InsertPos);
859 return QualType(New, 0);
860}
861
Eli Friedmanf98aba32009-02-13 02:31:07 +0000862QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
863 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
864 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
865 FixedWidthIntType *&Entry = Map[Width];
866 if (!Entry)
867 Entry = new FixedWidthIntType(Width, Signed);
868 return QualType(Entry, 0);
869}
Reid Spencer5f016e22007-07-11 17:01:13 +0000870
871/// getPointerType - Return the uniqued reference to the type for a pointer to
872/// the specified type.
873QualType ASTContext::getPointerType(QualType T) {
874 // Unique pointers, to guarantee there is only one pointer of a particular
875 // structure.
876 llvm::FoldingSetNodeID ID;
877 PointerType::Profile(ID, T);
878
879 void *InsertPos = 0;
880 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
881 return QualType(PT, 0);
882
883 // If the pointee type isn't canonical, this won't be a canonical type either,
884 // so fill in the canonical type field.
885 QualType Canonical;
886 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000887 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000888
889 // Get the new insert position for the node we care about.
890 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000891 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000892 }
Steve Narofff83820b2009-01-27 22:08:43 +0000893 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000894 Types.push_back(New);
895 PointerTypes.InsertNode(New, InsertPos);
896 return QualType(New, 0);
897}
898
Steve Naroff5618bd42008-08-27 16:04:49 +0000899/// getBlockPointerType - Return the uniqued reference to the type for
900/// a pointer to the specified block.
901QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000902 assert(T->isFunctionType() && "block of function types only");
903 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000904 // structure.
905 llvm::FoldingSetNodeID ID;
906 BlockPointerType::Profile(ID, T);
907
908 void *InsertPos = 0;
909 if (BlockPointerType *PT =
910 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
911 return QualType(PT, 0);
912
Steve Naroff296e8d52008-08-28 19:20:44 +0000913 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000914 // type either so fill in the canonical type field.
915 QualType Canonical;
916 if (!T->isCanonical()) {
917 Canonical = getBlockPointerType(getCanonicalType(T));
918
919 // Get the new insert position for the node we care about.
920 BlockPointerType *NewIP =
921 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000922 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000923 }
Steve Narofff83820b2009-01-27 22:08:43 +0000924 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000925 Types.push_back(New);
926 BlockPointerTypes.InsertNode(New, InsertPos);
927 return QualType(New, 0);
928}
929
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000930/// getLValueReferenceType - Return the uniqued reference to the type for an
931/// lvalue reference to the specified type.
932QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000933 // Unique pointers, to guarantee there is only one pointer of a particular
934 // structure.
935 llvm::FoldingSetNodeID ID;
936 ReferenceType::Profile(ID, T);
937
938 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000939 if (LValueReferenceType *RT =
940 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000941 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000942
Reid Spencer5f016e22007-07-11 17:01:13 +0000943 // If the referencee type isn't canonical, this won't be a canonical type
944 // either, so fill in the canonical type field.
945 QualType Canonical;
946 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000947 Canonical = getLValueReferenceType(getCanonicalType(T));
948
Reid Spencer5f016e22007-07-11 17:01:13 +0000949 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000950 LValueReferenceType *NewIP =
951 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000952 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000953 }
954
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000955 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000956 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000957 LValueReferenceTypes.InsertNode(New, InsertPos);
958 return QualType(New, 0);
959}
960
961/// getRValueReferenceType - Return the uniqued reference to the type for an
962/// rvalue reference to the specified type.
963QualType ASTContext::getRValueReferenceType(QualType T) {
964 // Unique pointers, to guarantee there is only one pointer of a particular
965 // structure.
966 llvm::FoldingSetNodeID ID;
967 ReferenceType::Profile(ID, T);
968
969 void *InsertPos = 0;
970 if (RValueReferenceType *RT =
971 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
972 return QualType(RT, 0);
973
974 // If the referencee type isn't canonical, this won't be a canonical type
975 // either, so fill in the canonical type field.
976 QualType Canonical;
977 if (!T->isCanonical()) {
978 Canonical = getRValueReferenceType(getCanonicalType(T));
979
980 // Get the new insert position for the node we care about.
981 RValueReferenceType *NewIP =
982 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
983 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
984 }
985
986 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
987 Types.push_back(New);
988 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000989 return QualType(New, 0);
990}
991
Sebastian Redlf30208a2009-01-24 21:16:55 +0000992/// getMemberPointerType - Return the uniqued reference to the type for a
993/// member pointer to the specified type, in the specified class.
994QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
995{
996 // Unique pointers, to guarantee there is only one pointer of a particular
997 // structure.
998 llvm::FoldingSetNodeID ID;
999 MemberPointerType::Profile(ID, T, Cls);
1000
1001 void *InsertPos = 0;
1002 if (MemberPointerType *PT =
1003 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1004 return QualType(PT, 0);
1005
1006 // If the pointee or class type isn't canonical, this won't be a canonical
1007 // type either, so fill in the canonical type field.
1008 QualType Canonical;
1009 if (!T->isCanonical()) {
1010 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1011
1012 // Get the new insert position for the node we care about.
1013 MemberPointerType *NewIP =
1014 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1015 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1016 }
Steve Narofff83820b2009-01-27 22:08:43 +00001017 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001018 Types.push_back(New);
1019 MemberPointerTypes.InsertNode(New, InsertPos);
1020 return QualType(New, 0);
1021}
1022
Steve Narofffb22d962007-08-30 01:06:46 +00001023/// getConstantArrayType - Return the unique reference to the type for an
1024/// array of the specified element type.
1025QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +00001026 const llvm::APInt &ArySize,
1027 ArrayType::ArraySizeModifier ASM,
1028 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001030 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001031
1032 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001033 if (ConstantArrayType *ATP =
1034 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001035 return QualType(ATP, 0);
1036
1037 // If the element type isn't canonical, this won't be a canonical type either,
1038 // so fill in the canonical type field.
1039 QualType Canonical;
1040 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001041 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001042 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001043 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001044 ConstantArrayType *NewIP =
1045 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001046 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001047 }
1048
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001049 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001050 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001051 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001052 Types.push_back(New);
1053 return QualType(New, 0);
1054}
1055
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001056/// getVariableArrayType - Returns a non-unique reference to the type for a
1057/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001058QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1059 ArrayType::ArraySizeModifier ASM,
1060 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001061 // Since we don't unique expressions, it isn't possible to unique VLA's
1062 // that have an expression provided for their size.
1063
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001064 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001065 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001066
1067 VariableArrayTypes.push_back(New);
1068 Types.push_back(New);
1069 return QualType(New, 0);
1070}
1071
Douglas Gregor898574e2008-12-05 23:32:09 +00001072/// getDependentSizedArrayType - Returns a non-unique reference to
1073/// the type for a dependently-sized array of the specified element
1074/// type. FIXME: We will need these to be uniqued, or at least
1075/// comparable, at some point.
1076QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1077 ArrayType::ArraySizeModifier ASM,
1078 unsigned EltTypeQuals) {
1079 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1080 "Size must be type- or value-dependent!");
1081
1082 // Since we don't unique expressions, it isn't possible to unique
1083 // dependently-sized array types.
1084
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001085 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001086 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1087 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001088
1089 DependentSizedArrayTypes.push_back(New);
1090 Types.push_back(New);
1091 return QualType(New, 0);
1092}
1093
Eli Friedmanc5773c42008-02-15 18:16:39 +00001094QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1095 ArrayType::ArraySizeModifier ASM,
1096 unsigned EltTypeQuals) {
1097 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001098 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001099
1100 void *InsertPos = 0;
1101 if (IncompleteArrayType *ATP =
1102 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1103 return QualType(ATP, 0);
1104
1105 // If the element type isn't canonical, this won't be a canonical type
1106 // either, so fill in the canonical type field.
1107 QualType Canonical;
1108
1109 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001110 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001111 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001112
1113 // Get the new insert position for the node we care about.
1114 IncompleteArrayType *NewIP =
1115 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001116 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001117 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001118
Steve Narofff83820b2009-01-27 22:08:43 +00001119 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001120 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001121
1122 IncompleteArrayTypes.InsertNode(New, InsertPos);
1123 Types.push_back(New);
1124 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001125}
1126
Steve Naroff73322922007-07-18 18:00:27 +00001127/// getVectorType - Return the unique reference to a vector type of
1128/// the specified element type and size. VectorType must be a built-in type.
1129QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001130 BuiltinType *baseType;
1131
Chris Lattnerf52ab252008-04-06 22:59:24 +00001132 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001133 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001134
1135 // Check if we've already instantiated a vector of this type.
1136 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001137 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001138 void *InsertPos = 0;
1139 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1140 return QualType(VTP, 0);
1141
1142 // If the element type isn't canonical, this won't be a canonical type either,
1143 // so fill in the canonical type field.
1144 QualType Canonical;
1145 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001146 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001147
1148 // Get the new insert position for the node we care about.
1149 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001150 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001151 }
Steve Narofff83820b2009-01-27 22:08:43 +00001152 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 VectorTypes.InsertNode(New, InsertPos);
1154 Types.push_back(New);
1155 return QualType(New, 0);
1156}
1157
Nate Begeman213541a2008-04-18 23:10:10 +00001158/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001159/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001160QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001161 BuiltinType *baseType;
1162
Chris Lattnerf52ab252008-04-06 22:59:24 +00001163 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001164 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001165
1166 // Check if we've already instantiated a vector of this type.
1167 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001168 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001169 void *InsertPos = 0;
1170 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1171 return QualType(VTP, 0);
1172
1173 // If the element type isn't canonical, this won't be a canonical type either,
1174 // so fill in the canonical type field.
1175 QualType Canonical;
1176 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001177 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001178
1179 // Get the new insert position for the node we care about.
1180 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001181 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001182 }
Steve Narofff83820b2009-01-27 22:08:43 +00001183 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001184 VectorTypes.InsertNode(New, InsertPos);
1185 Types.push_back(New);
1186 return QualType(New, 0);
1187}
1188
Douglas Gregor72564e72009-02-26 23:50:07 +00001189/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001190///
Douglas Gregor72564e72009-02-26 23:50:07 +00001191QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001192 // Unique functions, to guarantee there is only one function of a particular
1193 // structure.
1194 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001195 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001196
1197 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001198 if (FunctionNoProtoType *FT =
1199 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 return QualType(FT, 0);
1201
1202 QualType Canonical;
1203 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001204 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001205
1206 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001207 FunctionNoProtoType *NewIP =
1208 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001209 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 }
1211
Douglas Gregor72564e72009-02-26 23:50:07 +00001212 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001213 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001214 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 return QualType(New, 0);
1216}
1217
1218/// getFunctionType - Return a normal function type with a typed argument
1219/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001220QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001221 unsigned NumArgs, bool isVariadic,
1222 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001223 // Unique functions, to guarantee there is only one function of a particular
1224 // structure.
1225 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001226 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001227 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001228
1229 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001230 if (FunctionProtoType *FTP =
1231 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 return QualType(FTP, 0);
1233
1234 // Determine whether the type being created is already canonical or not.
1235 bool isCanonical = ResultTy->isCanonical();
1236 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1237 if (!ArgArray[i]->isCanonical())
1238 isCanonical = false;
1239
1240 // If this type isn't canonical, get the canonical version of it.
1241 QualType Canonical;
1242 if (!isCanonical) {
1243 llvm::SmallVector<QualType, 16> CanonicalArgs;
1244 CanonicalArgs.reserve(NumArgs);
1245 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001246 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001247
Chris Lattnerf52ab252008-04-06 22:59:24 +00001248 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001250 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001251
1252 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001253 FunctionProtoType *NewIP =
1254 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001255 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 }
1257
Douglas Gregor72564e72009-02-26 23:50:07 +00001258 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001259 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001260 FunctionProtoType *FTP =
1261 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001262 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001263 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001264 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001266 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001267 return QualType(FTP, 0);
1268}
1269
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001270/// getTypeDeclType - Return the unique reference to the type for the
1271/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001272QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001273 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001274 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1275
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001276 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001277 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001278 else if (isa<TemplateTypeParmDecl>(Decl)) {
1279 assert(false && "Template type parameter types are always available.");
1280 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001281 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001282
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001283 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001284 if (PrevDecl)
1285 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001286 else
1287 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001288 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001289 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1290 if (PrevDecl)
1291 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001292 else
1293 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001294 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001295 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001296 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001297
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001298 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001299 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001300}
1301
Reid Spencer5f016e22007-07-11 17:01:13 +00001302/// getTypedefType - Return the unique reference to the type for the
1303/// specified typename decl.
1304QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1305 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1306
Chris Lattnerf52ab252008-04-06 22:59:24 +00001307 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001308 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001309 Types.push_back(Decl->TypeForDecl);
1310 return QualType(Decl->TypeForDecl, 0);
1311}
1312
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001313/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001314/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001315QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001316 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1317
Steve Narofff83820b2009-01-27 22:08:43 +00001318 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001319 Types.push_back(Decl->TypeForDecl);
1320 return QualType(Decl->TypeForDecl, 0);
1321}
1322
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00001323/// buildObjCInterfaceType - Returns a new type for the interface
1324/// declaration, regardless. It also removes any previously built
1325/// record declaration so caller can rebuild it.
1326QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) {
1327 const RecordDecl *&RD = ASTRecordForInterface[Decl];
1328 if (RD)
1329 RD = 0;
1330 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
1331 Types.push_back(Decl->TypeForDecl);
1332 return QualType(Decl->TypeForDecl, 0);
1333}
1334
Douglas Gregorfab9d672009-02-05 23:33:38 +00001335/// \brief Retrieve the template type parameter type for a template
1336/// parameter with the given depth, index, and (optionally) name.
1337QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1338 IdentifierInfo *Name) {
1339 llvm::FoldingSetNodeID ID;
1340 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1341 void *InsertPos = 0;
1342 TemplateTypeParmType *TypeParm
1343 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1344
1345 if (TypeParm)
1346 return QualType(TypeParm, 0);
1347
1348 if (Name)
1349 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1350 getTemplateTypeParmType(Depth, Index));
1351 else
1352 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1353
1354 Types.push_back(TypeParm);
1355 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1356
1357 return QualType(TypeParm, 0);
1358}
1359
Douglas Gregor55f6b142009-02-09 18:46:07 +00001360QualType
1361ASTContext::getClassTemplateSpecializationType(TemplateDecl *Template,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001362 const TemplateArgument *Args,
Douglas Gregor55f6b142009-02-09 18:46:07 +00001363 unsigned NumArgs,
Douglas Gregor55f6b142009-02-09 18:46:07 +00001364 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001365 if (!Canon.isNull())
1366 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001367
Douglas Gregor55f6b142009-02-09 18:46:07 +00001368 llvm::FoldingSetNodeID ID;
Douglas Gregor40808ce2009-03-09 23:48:35 +00001369 ClassTemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
1370
Douglas Gregor55f6b142009-02-09 18:46:07 +00001371 void *InsertPos = 0;
1372 ClassTemplateSpecializationType *Spec
1373 = ClassTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
1374
1375 if (Spec)
1376 return QualType(Spec, 0);
1377
Douglas Gregor40808ce2009-03-09 23:48:35 +00001378 void *Mem = Allocate((sizeof(ClassTemplateSpecializationType) +
1379 sizeof(TemplateArgument) * NumArgs),
1380 8);
1381 Spec = new (Mem) ClassTemplateSpecializationType(Template, Args, NumArgs,
1382 Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001383 Types.push_back(Spec);
1384 ClassTemplateSpecializationTypes.InsertNode(Spec, InsertPos);
1385
1386 return QualType(Spec, 0);
1387}
1388
Douglas Gregore4e5b052009-03-19 00:18:19 +00001389QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001390ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001391 QualType NamedType) {
1392 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001393 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001394
1395 void *InsertPos = 0;
1396 QualifiedNameType *T
1397 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1398 if (T)
1399 return QualType(T, 0);
1400
Douglas Gregorab452ba2009-03-26 23:50:42 +00001401 T = new (*this) QualifiedNameType(NNS, NamedType,
1402 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001403 Types.push_back(T);
1404 QualifiedNameTypes.InsertNode(T, InsertPos);
1405 return QualType(T, 0);
1406}
1407
Chris Lattner88cb27a2008-04-07 04:56:42 +00001408/// CmpProtocolNames - Comparison predicate for sorting protocols
1409/// alphabetically.
1410static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1411 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001412 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001413}
1414
1415static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1416 unsigned &NumProtocols) {
1417 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1418
1419 // Sort protocols, keyed by name.
1420 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1421
1422 // Remove duplicates.
1423 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1424 NumProtocols = ProtocolsEnd-Protocols;
1425}
1426
1427
Chris Lattner065f0d72008-04-07 04:44:08 +00001428/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1429/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001430QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1431 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001432 // Sort the protocol list alphabetically to canonicalize it.
1433 SortAndUniqueProtocols(Protocols, NumProtocols);
1434
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001435 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001436 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001437
1438 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001439 if (ObjCQualifiedInterfaceType *QT =
1440 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001441 return QualType(QT, 0);
1442
1443 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001444 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001445 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001446
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001447 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001448 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001449 return QualType(QType, 0);
1450}
1451
Chris Lattner88cb27a2008-04-07 04:56:42 +00001452/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1453/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001454QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001455 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001456 // Sort the protocol list alphabetically to canonicalize it.
1457 SortAndUniqueProtocols(Protocols, NumProtocols);
1458
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001459 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001460 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001461
1462 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001463 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001464 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001465 return QualType(QT, 0);
1466
1467 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001468 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001469 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001470 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001471 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001472 return QualType(QType, 0);
1473}
1474
Douglas Gregor72564e72009-02-26 23:50:07 +00001475/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1476/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001477/// multiple declarations that refer to "typeof(x)" all contain different
1478/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1479/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001480QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001481 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001482 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001483 Types.push_back(toe);
1484 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001485}
1486
Steve Naroff9752f252007-08-01 18:02:17 +00001487/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1488/// TypeOfType AST's. The only motivation to unique these nodes would be
1489/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1490/// an issue. This doesn't effect the type checker, since it operates
1491/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001492QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001493 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001494 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001495 Types.push_back(tot);
1496 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001497}
1498
Reid Spencer5f016e22007-07-11 17:01:13 +00001499/// getTagDeclType - Return the unique reference to the type for the
1500/// specified TagDecl (struct/union/class/enum) decl.
1501QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001502 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001503 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001504}
1505
1506/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1507/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1508/// needs to agree with the definition in <stddef.h>.
1509QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001510 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001511}
1512
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001513/// getSignedWCharType - Return the type of "signed wchar_t".
1514/// Used when in C++, as a GCC extension.
1515QualType ASTContext::getSignedWCharType() const {
1516 // FIXME: derive from "Target" ?
1517 return WCharTy;
1518}
1519
1520/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1521/// Used when in C++, as a GCC extension.
1522QualType ASTContext::getUnsignedWCharType() const {
1523 // FIXME: derive from "Target" ?
1524 return UnsignedIntTy;
1525}
1526
Chris Lattner8b9023b2007-07-13 03:05:23 +00001527/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1528/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1529QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001530 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001531}
1532
Chris Lattnere6327742008-04-02 05:18:44 +00001533//===----------------------------------------------------------------------===//
1534// Type Operators
1535//===----------------------------------------------------------------------===//
1536
Chris Lattner77c96472008-04-06 22:41:35 +00001537/// getCanonicalType - Return the canonical (structural) type corresponding to
1538/// the specified potentially non-canonical type. The non-canonical version
1539/// of a type may have many "decorated" versions of types. Decorators can
1540/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1541/// to be free of any of these, allowing two canonical types to be compared
1542/// for exact equality with a simple pointer comparison.
1543QualType ASTContext::getCanonicalType(QualType T) {
1544 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001545
1546 // If the result has type qualifiers, make sure to canonicalize them as well.
1547 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1548 if (TypeQuals == 0) return CanType;
1549
1550 // If the type qualifiers are on an array type, get the canonical type of the
1551 // array with the qualifiers applied to the element type.
1552 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1553 if (!AT)
1554 return CanType.getQualifiedType(TypeQuals);
1555
1556 // Get the canonical version of the element with the extra qualifiers on it.
1557 // This can recursively sink qualifiers through multiple levels of arrays.
1558 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1559 NewEltTy = getCanonicalType(NewEltTy);
1560
1561 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1562 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1563 CAT->getIndexTypeQualifier());
1564 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1565 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1566 IAT->getIndexTypeQualifier());
1567
Douglas Gregor898574e2008-12-05 23:32:09 +00001568 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1569 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1570 DSAT->getSizeModifier(),
1571 DSAT->getIndexTypeQualifier());
1572
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001573 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1574 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1575 VAT->getSizeModifier(),
1576 VAT->getIndexTypeQualifier());
1577}
1578
1579
1580const ArrayType *ASTContext::getAsArrayType(QualType T) {
1581 // Handle the non-qualified case efficiently.
1582 if (T.getCVRQualifiers() == 0) {
1583 // Handle the common positive case fast.
1584 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1585 return AT;
1586 }
1587
1588 // Handle the common negative case fast, ignoring CVR qualifiers.
1589 QualType CType = T->getCanonicalTypeInternal();
1590
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001591 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001592 // test.
1593 if (!isa<ArrayType>(CType) &&
1594 !isa<ArrayType>(CType.getUnqualifiedType()))
1595 return 0;
1596
1597 // Apply any CVR qualifiers from the array type to the element type. This
1598 // implements C99 6.7.3p8: "If the specification of an array type includes
1599 // any type qualifiers, the element type is so qualified, not the array type."
1600
1601 // If we get here, we either have type qualifiers on the type, or we have
1602 // sugar such as a typedef in the way. If we have type qualifiers on the type
1603 // we must propagate them down into the elemeng type.
1604 unsigned CVRQuals = T.getCVRQualifiers();
1605 unsigned AddrSpace = 0;
1606 Type *Ty = T.getTypePtr();
1607
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001608 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001609 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001610 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1611 AddrSpace = EXTQT->getAddressSpace();
1612 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001613 } else {
1614 T = Ty->getDesugaredType();
1615 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1616 break;
1617 CVRQuals |= T.getCVRQualifiers();
1618 Ty = T.getTypePtr();
1619 }
1620 }
1621
1622 // If we have a simple case, just return now.
1623 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1624 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1625 return ATy;
1626
1627 // Otherwise, we have an array and we have qualifiers on it. Push the
1628 // qualifiers into the array element type and return a new array type.
1629 // Get the canonical version of the element with the extra qualifiers on it.
1630 // This can recursively sink qualifiers through multiple levels of arrays.
1631 QualType NewEltTy = ATy->getElementType();
1632 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001633 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001634 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1635
1636 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1637 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1638 CAT->getSizeModifier(),
1639 CAT->getIndexTypeQualifier()));
1640 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1641 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1642 IAT->getSizeModifier(),
1643 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001644
Douglas Gregor898574e2008-12-05 23:32:09 +00001645 if (const DependentSizedArrayType *DSAT
1646 = dyn_cast<DependentSizedArrayType>(ATy))
1647 return cast<ArrayType>(
1648 getDependentSizedArrayType(NewEltTy,
1649 DSAT->getSizeExpr(),
1650 DSAT->getSizeModifier(),
1651 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001652
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001653 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1654 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1655 VAT->getSizeModifier(),
1656 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001657}
1658
1659
Chris Lattnere6327742008-04-02 05:18:44 +00001660/// getArrayDecayedType - Return the properly qualified result of decaying the
1661/// specified array type to a pointer. This operation is non-trivial when
1662/// handling typedefs etc. The canonical type of "T" must be an array type,
1663/// this returns a pointer to a properly qualified element of the array.
1664///
1665/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1666QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001667 // Get the element type with 'getAsArrayType' so that we don't lose any
1668 // typedefs in the element type of the array. This also handles propagation
1669 // of type qualifiers from the array type into the element type if present
1670 // (C99 6.7.3p8).
1671 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1672 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001673
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001674 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001675
1676 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001677 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001678}
1679
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001680QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001681 QualType ElemTy = VAT->getElementType();
1682
1683 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1684 return getBaseElementType(VAT);
1685
1686 return ElemTy;
1687}
1688
Reid Spencer5f016e22007-07-11 17:01:13 +00001689/// getFloatingRank - Return a relative rank for floating point types.
1690/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001691static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001692 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001693 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001694
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001695 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001696 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001697 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001698 case BuiltinType::Float: return FloatRank;
1699 case BuiltinType::Double: return DoubleRank;
1700 case BuiltinType::LongDouble: return LongDoubleRank;
1701 }
1702}
1703
Steve Naroff716c7302007-08-27 01:41:48 +00001704/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1705/// point or a complex type (based on typeDomain/typeSize).
1706/// 'typeDomain' is a real floating point or complex type.
1707/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001708QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1709 QualType Domain) const {
1710 FloatingRank EltRank = getFloatingRank(Size);
1711 if (Domain->isComplexType()) {
1712 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001713 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001714 case FloatRank: return FloatComplexTy;
1715 case DoubleRank: return DoubleComplexTy;
1716 case LongDoubleRank: return LongDoubleComplexTy;
1717 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001718 }
Chris Lattner1361b112008-04-06 23:58:54 +00001719
1720 assert(Domain->isRealFloatingType() && "Unknown domain!");
1721 switch (EltRank) {
1722 default: assert(0 && "getFloatingRank(): illegal value for rank");
1723 case FloatRank: return FloatTy;
1724 case DoubleRank: return DoubleTy;
1725 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001726 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001727}
1728
Chris Lattner7cfeb082008-04-06 23:55:33 +00001729/// getFloatingTypeOrder - Compare the rank of the two specified floating
1730/// point types, ignoring the domain of the type (i.e. 'double' ==
1731/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1732/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001733int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1734 FloatingRank LHSR = getFloatingRank(LHS);
1735 FloatingRank RHSR = getFloatingRank(RHS);
1736
1737 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001738 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001739 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001740 return 1;
1741 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001742}
1743
Chris Lattnerf52ab252008-04-06 22:59:24 +00001744/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1745/// routine will assert if passed a built-in type that isn't an integer or enum,
1746/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001747unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001748 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001749 if (EnumType* ET = dyn_cast<EnumType>(T))
1750 T = ET->getDecl()->getIntegerType().getTypePtr();
1751
1752 // There are two things which impact the integer rank: the width, and
1753 // the ordering of builtins. The builtin ordering is encoded in the
1754 // bottom three bits; the width is encoded in the bits above that.
1755 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1756 return FWIT->getWidth() << 3;
1757 }
1758
Chris Lattnerf52ab252008-04-06 22:59:24 +00001759 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001760 default: assert(0 && "getIntegerRank(): not a built-in integer");
1761 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001762 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001763 case BuiltinType::Char_S:
1764 case BuiltinType::Char_U:
1765 case BuiltinType::SChar:
1766 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001767 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001768 case BuiltinType::Short:
1769 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001770 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001771 case BuiltinType::Int:
1772 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001773 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001774 case BuiltinType::Long:
1775 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001776 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001777 case BuiltinType::LongLong:
1778 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001779 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001780 }
1781}
1782
Chris Lattner7cfeb082008-04-06 23:55:33 +00001783/// getIntegerTypeOrder - Returns the highest ranked integer type:
1784/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1785/// LHS < RHS, return -1.
1786int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001787 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1788 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001789 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001790
Chris Lattnerf52ab252008-04-06 22:59:24 +00001791 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1792 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001793
Chris Lattner7cfeb082008-04-06 23:55:33 +00001794 unsigned LHSRank = getIntegerRank(LHSC);
1795 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001796
Chris Lattner7cfeb082008-04-06 23:55:33 +00001797 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1798 if (LHSRank == RHSRank) return 0;
1799 return LHSRank > RHSRank ? 1 : -1;
1800 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001801
Chris Lattner7cfeb082008-04-06 23:55:33 +00001802 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1803 if (LHSUnsigned) {
1804 // If the unsigned [LHS] type is larger, return it.
1805 if (LHSRank >= RHSRank)
1806 return 1;
1807
1808 // If the signed type can represent all values of the unsigned type, it
1809 // wins. Because we are dealing with 2's complement and types that are
1810 // powers of two larger than each other, this is always safe.
1811 return -1;
1812 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001813
Chris Lattner7cfeb082008-04-06 23:55:33 +00001814 // If the unsigned [RHS] type is larger, return it.
1815 if (RHSRank >= LHSRank)
1816 return -1;
1817
1818 // If the signed type can represent all values of the unsigned type, it
1819 // wins. Because we are dealing with 2's complement and types that are
1820 // powers of two larger than each other, this is always safe.
1821 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001822}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001823
1824// getCFConstantStringType - Return the type used for constant CFStrings.
1825QualType ASTContext::getCFConstantStringType() {
1826 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001827 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001828 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001829 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001830 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001831
1832 // const int *isa;
1833 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001834 // int flags;
1835 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001836 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001837 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001838 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001839 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001840
Anders Carlsson71993dd2007-08-17 05:31:46 +00001841 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001842 for (unsigned i = 0; i < 4; ++i) {
1843 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1844 SourceLocation(), 0,
1845 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001846 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001847 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001848 }
1849
1850 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001851 }
1852
1853 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001854}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001855
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001856QualType ASTContext::getObjCFastEnumerationStateType()
1857{
1858 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001859 ObjCFastEnumerationStateTypeDecl =
1860 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1861 &Idents.get("__objcFastEnumerationState"));
1862
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001863 QualType FieldTypes[] = {
1864 UnsignedLongTy,
1865 getPointerType(ObjCIdType),
1866 getPointerType(UnsignedLongTy),
1867 getConstantArrayType(UnsignedLongTy,
1868 llvm::APInt(32, 5), ArrayType::Normal, 0)
1869 };
1870
Douglas Gregor44b43212008-12-11 16:49:14 +00001871 for (size_t i = 0; i < 4; ++i) {
1872 FieldDecl *Field = FieldDecl::Create(*this,
1873 ObjCFastEnumerationStateTypeDecl,
1874 SourceLocation(), 0,
1875 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001876 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001877 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001878 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001879
Douglas Gregor44b43212008-12-11 16:49:14 +00001880 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001881 }
1882
1883 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1884}
1885
Anders Carlssone8c49532007-10-29 06:33:42 +00001886// This returns true if a type has been typedefed to BOOL:
1887// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001888static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001889 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00001890 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1891 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001892
1893 return false;
1894}
1895
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001896/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001897/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001898int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001899 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001900
1901 // Make all integer and enum types at least as large as an int
1902 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001903 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001904 // Treat arrays as pointers, since that's how they're passed in.
1905 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001906 sz = getTypeSize(VoidPtrTy);
1907 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001908}
1909
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001910/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001911/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001912void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001913 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001914 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001915 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001916 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001917 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001918 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001919 // Compute size of all parameters.
1920 // Start with computing size of a pointer in number of bytes.
1921 // FIXME: There might(should) be a better way of doing this computation!
1922 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001923 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001924 // The first two arguments (self and _cmd) are pointers; account for
1925 // their size.
1926 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00001927 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1928 E = Decl->param_end(); PI != E; ++PI) {
1929 QualType PType = (*PI)->getType();
1930 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001931 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001932 ParmOffset += sz;
1933 }
1934 S += llvm::utostr(ParmOffset);
1935 S += "@0:";
1936 S += llvm::utostr(PtrSize);
1937
1938 // Argument types.
1939 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00001940 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1941 E = Decl->param_end(); PI != E; ++PI) {
1942 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001943 QualType PType = PVDecl->getOriginalType();
1944 if (const ArrayType *AT =
1945 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1946 // Use array's original type only if it has known number of
1947 // elements.
1948 if (!dyn_cast<ConstantArrayType>(AT))
1949 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001950 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001951 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001952 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001953 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001954 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001955 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001956 }
1957}
1958
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001959/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001960/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001961/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1962/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001963/// Property attributes are stored as a comma-delimited C string. The simple
1964/// attributes readonly and bycopy are encoded as single characters. The
1965/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1966/// encoded as single characters, followed by an identifier. Property types
1967/// are also encoded as a parametrized attribute. The characters used to encode
1968/// these attributes are defined by the following enumeration:
1969/// @code
1970/// enum PropertyAttributes {
1971/// kPropertyReadOnly = 'R', // property is read-only.
1972/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1973/// kPropertyByref = '&', // property is a reference to the value last assigned
1974/// kPropertyDynamic = 'D', // property is dynamic
1975/// kPropertyGetter = 'G', // followed by getter selector name
1976/// kPropertySetter = 'S', // followed by setter selector name
1977/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1978/// kPropertyType = 't' // followed by old-style type encoding.
1979/// kPropertyWeak = 'W' // 'weak' property
1980/// kPropertyStrong = 'P' // property GC'able
1981/// kPropertyNonAtomic = 'N' // property non-atomic
1982/// };
1983/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001984void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1985 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001986 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001987 // Collect information from the property implementation decl(s).
1988 bool Dynamic = false;
1989 ObjCPropertyImplDecl *SynthesizePID = 0;
1990
1991 // FIXME: Duplicated code due to poor abstraction.
1992 if (Container) {
1993 if (const ObjCCategoryImplDecl *CID =
1994 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1995 for (ObjCCategoryImplDecl::propimpl_iterator
1996 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1997 ObjCPropertyImplDecl *PID = *i;
1998 if (PID->getPropertyDecl() == PD) {
1999 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2000 Dynamic = true;
2001 } else {
2002 SynthesizePID = PID;
2003 }
2004 }
2005 }
2006 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002007 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002008 for (ObjCCategoryImplDecl::propimpl_iterator
2009 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2010 ObjCPropertyImplDecl *PID = *i;
2011 if (PID->getPropertyDecl() == PD) {
2012 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2013 Dynamic = true;
2014 } else {
2015 SynthesizePID = PID;
2016 }
2017 }
2018 }
2019 }
2020 }
2021
2022 // FIXME: This is not very efficient.
2023 S = "T";
2024
2025 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002026 // GCC has some special rules regarding encoding of properties which
2027 // closely resembles encoding of ivars.
2028 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
2029 true /* outermost type */,
2030 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002031
2032 if (PD->isReadOnly()) {
2033 S += ",R";
2034 } else {
2035 switch (PD->getSetterKind()) {
2036 case ObjCPropertyDecl::Assign: break;
2037 case ObjCPropertyDecl::Copy: S += ",C"; break;
2038 case ObjCPropertyDecl::Retain: S += ",&"; break;
2039 }
2040 }
2041
2042 // It really isn't clear at all what this means, since properties
2043 // are "dynamic by default".
2044 if (Dynamic)
2045 S += ",D";
2046
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002047 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2048 S += ",N";
2049
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002050 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2051 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002052 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002053 }
2054
2055 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2056 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002057 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002058 }
2059
2060 if (SynthesizePID) {
2061 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2062 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002063 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002064 }
2065
2066 // FIXME: OBJCGC: weak & strong
2067}
2068
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002069/// getLegacyIntegralTypeEncoding -
2070/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002071/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002072/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2073///
2074void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2075 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2076 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002077 if (BT->getKind() == BuiltinType::ULong &&
2078 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002079 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002080 else
2081 if (BT->getKind() == BuiltinType::Long &&
2082 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002083 PointeeTy = IntTy;
2084 }
2085 }
2086}
2087
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002088void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002089 FieldDecl *Field) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002090 // We follow the behavior of gcc, expanding structures which are
2091 // directly pointed to, and expanding embedded structures. Note that
2092 // these rules are sufficient to prevent recursive encoding of the
2093 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002094 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2095 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002096}
2097
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002098static void EncodeBitField(const ASTContext *Context, std::string& S,
2099 FieldDecl *FD) {
2100 const Expr *E = FD->getBitWidth();
2101 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2102 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2103 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2104 S += 'b';
2105 S += llvm::utostr(N);
2106}
2107
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002108void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2109 bool ExpandPointedToStructures,
2110 bool ExpandStructures,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002111 FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002112 bool OutermostType,
2113 bool EncodingProperty) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00002114 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002115 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002116 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002117 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002118 else {
2119 char encoding;
2120 switch (BT->getKind()) {
2121 default: assert(0 && "Unhandled builtin type kind");
2122 case BuiltinType::Void: encoding = 'v'; break;
2123 case BuiltinType::Bool: encoding = 'B'; break;
2124 case BuiltinType::Char_U:
2125 case BuiltinType::UChar: encoding = 'C'; break;
2126 case BuiltinType::UShort: encoding = 'S'; break;
2127 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002128 case BuiltinType::ULong:
2129 encoding =
2130 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2131 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002132 case BuiltinType::ULongLong: encoding = 'Q'; break;
2133 case BuiltinType::Char_S:
2134 case BuiltinType::SChar: encoding = 'c'; break;
2135 case BuiltinType::Short: encoding = 's'; break;
2136 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002137 case BuiltinType::Long:
2138 encoding =
2139 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2140 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002141 case BuiltinType::LongLong: encoding = 'q'; break;
2142 case BuiltinType::Float: encoding = 'f'; break;
2143 case BuiltinType::Double: encoding = 'd'; break;
2144 case BuiltinType::LongDouble: encoding = 'd'; break;
2145 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002146
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002147 S += encoding;
2148 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002149 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002150 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002151 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2152 ExpandPointedToStructures,
2153 ExpandStructures, FD);
2154 if (FD || EncodingProperty) {
2155 // Note that we do extended encoding of protocol qualifer list
2156 // Only when doing ivar or property encoding.
2157 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2158 S += '"';
2159 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2160 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2161 S += '<';
2162 S += Proto->getNameAsString();
2163 S += '>';
2164 }
2165 S += '"';
2166 }
2167 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002168 }
2169 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002170 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002171 bool isReadOnly = false;
2172 // For historical/compatibility reasons, the read-only qualifier of the
2173 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2174 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2175 // Also, do not emit the 'r' for anything but the outermost type!
2176 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2177 if (OutermostType && T.isConstQualified()) {
2178 isReadOnly = true;
2179 S += 'r';
2180 }
2181 }
2182 else if (OutermostType) {
2183 QualType P = PointeeTy;
2184 while (P->getAsPointerType())
2185 P = P->getAsPointerType()->getPointeeType();
2186 if (P.isConstQualified()) {
2187 isReadOnly = true;
2188 S += 'r';
2189 }
2190 }
2191 if (isReadOnly) {
2192 // Another legacy compatibility encoding. Some ObjC qualifier and type
2193 // combinations need to be rearranged.
2194 // Rewrite "in const" from "nr" to "rn"
2195 const char * s = S.c_str();
2196 int len = S.length();
2197 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2198 std::string replace = "rn";
2199 S.replace(S.end()-2, S.end(), replace);
2200 }
2201 }
Steve Naroff389bf462009-02-12 17:52:19 +00002202 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002203 S += '@';
2204 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002205 }
2206 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002207 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002208 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002209 // Another historical/compatibility reason.
2210 // We encode the underlying type which comes out as
2211 // {...};
2212 S += '^';
2213 getObjCEncodingForTypeImpl(PointeeTy, S,
2214 false, ExpandPointedToStructures,
2215 NULL);
2216 return;
2217 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002218 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002219 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002220 const ObjCInterfaceType *OIT =
2221 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002222 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002223 S += '"';
2224 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002225 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2226 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2227 S += '<';
2228 S += Proto->getNameAsString();
2229 S += '>';
2230 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002231 S += '"';
2232 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002233 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002234 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002235 S += '#';
2236 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002237 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002238 S += ':';
2239 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002240 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002241
2242 if (PointeeTy->isCharType()) {
2243 // char pointer types should be encoded as '*' unless it is a
2244 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002245 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002246 S += '*';
2247 return;
2248 }
2249 }
2250
2251 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002252 getLegacyIntegralTypeEncoding(PointeeTy);
2253
2254 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002255 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002256 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002257 } else if (const ArrayType *AT =
2258 // Ignore type qualifiers etc.
2259 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002260 if (isa<IncompleteArrayType>(AT)) {
2261 // Incomplete arrays are encoded as a pointer to the array element.
2262 S += '^';
2263
2264 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2265 false, ExpandStructures, FD);
2266 } else {
2267 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002268
Anders Carlsson559a8332009-02-22 01:38:57 +00002269 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2270 S += llvm::utostr(CAT->getSize().getZExtValue());
2271 else {
2272 //Variable length arrays are encoded as a regular array with 0 elements.
2273 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2274 S += '0';
2275 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002276
Anders Carlsson559a8332009-02-22 01:38:57 +00002277 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2278 false, ExpandStructures, FD);
2279 S += ']';
2280 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002281 } else if (T->getAsFunctionType()) {
2282 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002283 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002284 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002285 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002286 // Anonymous structures print as '?'
2287 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2288 S += II->getName();
2289 } else {
2290 S += '?';
2291 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002292 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002293 S += '=';
Douglas Gregor44b43212008-12-11 16:49:14 +00002294 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2295 FieldEnd = RDecl->field_end();
2296 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002297 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002298 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002299 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002300 S += '"';
2301 }
2302
2303 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002304 if (Field->isBitField()) {
2305 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2306 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002307 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002308 QualType qt = Field->getType();
2309 getLegacyIntegralTypeEncoding(qt);
2310 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002311 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002312 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002313 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002314 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002315 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002316 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002317 if (FD && FD->isBitField())
2318 EncodeBitField(this, S, FD);
2319 else
2320 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002321 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002322 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002323 } else if (T->isObjCInterfaceType()) {
2324 // @encode(class_name)
2325 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2326 S += '{';
2327 const IdentifierInfo *II = OI->getIdentifier();
2328 S += II->getName();
2329 S += '=';
2330 std::vector<FieldDecl*> RecFields;
2331 CollectObjCIvars(OI, RecFields);
2332 for (unsigned int i = 0; i != RecFields.size(); i++) {
2333 if (RecFields[i]->isBitField())
2334 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2335 RecFields[i]);
2336 else
2337 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2338 FD);
2339 }
2340 S += '}';
2341 }
2342 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002343 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002344}
2345
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002346void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002347 std::string& S) const {
2348 if (QT & Decl::OBJC_TQ_In)
2349 S += 'n';
2350 if (QT & Decl::OBJC_TQ_Inout)
2351 S += 'N';
2352 if (QT & Decl::OBJC_TQ_Out)
2353 S += 'o';
2354 if (QT & Decl::OBJC_TQ_Bycopy)
2355 S += 'O';
2356 if (QT & Decl::OBJC_TQ_Byref)
2357 S += 'R';
2358 if (QT & Decl::OBJC_TQ_Oneway)
2359 S += 'V';
2360}
2361
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002362void ASTContext::setBuiltinVaListType(QualType T)
2363{
2364 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2365
2366 BuiltinVaListType = T;
2367}
2368
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002369void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002370{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002371 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002372
2373 // typedef struct objc_object *id;
2374 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002375 // User error - caller will issue diagnostics.
2376 if (!ptr)
2377 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002378 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002379 // User error - caller will issue diagnostics.
2380 if (!rec)
2381 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002382 IdStructType = rec;
2383}
2384
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002385void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002386{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002387 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002388
2389 // typedef struct objc_selector *SEL;
2390 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002391 if (!ptr)
2392 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002393 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002394 if (!rec)
2395 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002396 SelStructType = rec;
2397}
2398
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002399void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002400{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002401 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002402}
2403
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002404void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002405{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002406 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002407
2408 // typedef struct objc_class *Class;
2409 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2410 assert(ptr && "'Class' incorrectly typed");
2411 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2412 assert(rec && "'Class' incorrectly typed");
2413 ClassStructType = rec;
2414}
2415
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002416void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2417 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002418 "'NSConstantString' type already set!");
2419
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002420 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002421}
2422
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002423/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002424/// TargetInfo, produce the corresponding type. The unsigned @p Type
2425/// is actually a value of type @c TargetInfo::IntType.
2426QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002427 switch (Type) {
2428 case TargetInfo::NoInt: return QualType();
2429 case TargetInfo::SignedShort: return ShortTy;
2430 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2431 case TargetInfo::SignedInt: return IntTy;
2432 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2433 case TargetInfo::SignedLong: return LongTy;
2434 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2435 case TargetInfo::SignedLongLong: return LongLongTy;
2436 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2437 }
2438
2439 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002440 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002441}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002442
2443//===----------------------------------------------------------------------===//
2444// Type Predicates.
2445//===----------------------------------------------------------------------===//
2446
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002447/// isObjCNSObjectType - Return true if this is an NSObject object using
2448/// NSObject attribute on a c-style pointer type.
2449/// FIXME - Make it work directly on types.
2450///
2451bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2452 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2453 if (TypedefDecl *TD = TDT->getDecl())
2454 if (TD->getAttr<ObjCNSObjectAttr>())
2455 return true;
2456 }
2457 return false;
2458}
2459
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002460/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2461/// to an object type. This includes "id" and "Class" (two 'special' pointers
2462/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2463/// ID type).
2464bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002465 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002466 return true;
2467
Steve Naroff6ae98502008-10-21 18:24:04 +00002468 // Blocks are objects.
2469 if (Ty->isBlockPointerType())
2470 return true;
2471
2472 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002473 if (!Ty->isPointerType())
2474 return false;
2475
2476 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2477 // pointer types. This looks for the typedef specifically, not for the
2478 // underlying type.
Eli Friedman5fdeae12009-03-22 23:00:19 +00002479 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2480 Ty.getUnqualifiedType() == getObjCClassType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002481 return true;
2482
2483 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002484 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2485 return true;
2486
2487 // If is has NSObject attribute, OK as well.
2488 return isObjCNSObjectType(Ty);
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002489}
2490
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002491/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2492/// garbage collection attribute.
2493///
2494QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002495 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002496 if (getLangOptions().ObjC1 &&
2497 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002498 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002499 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002500 // (or pointers to them) be treated as though they were declared
2501 // as __strong.
2502 if (GCAttrs == QualType::GCNone) {
2503 if (isObjCObjectPointerType(Ty))
2504 GCAttrs = QualType::Strong;
2505 else if (Ty->isPointerType())
2506 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2507 }
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002508 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002509 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002510}
2511
Chris Lattner6ac46a42008-04-07 06:51:04 +00002512//===----------------------------------------------------------------------===//
2513// Type Compatibility Testing
2514//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002515
Steve Naroff1c7d0672008-09-04 15:10:53 +00002516/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002517/// block types. Types must be strictly compatible here. For example,
2518/// C unfortunately doesn't produce an error for the following:
2519///
2520/// int (*emptyArgFunc)();
2521/// int (*intArgList)(int) = emptyArgFunc;
2522///
2523/// For blocks, we will produce an error for the following (similar to C++):
2524///
2525/// int (^emptyArgBlock)();
2526/// int (^intArgBlock)(int) = emptyArgBlock;
2527///
2528/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2529///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002530bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002531 const FunctionType *lbase = lhs->getAsFunctionType();
2532 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002533 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2534 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Steve Naroffc0febd52008-12-10 17:49:55 +00002535 if (lproto && rproto)
2536 return !mergeTypes(lhs, rhs).isNull();
2537 return false;
Steve Naroff1c7d0672008-09-04 15:10:53 +00002538}
2539
Chris Lattner6ac46a42008-04-07 06:51:04 +00002540/// areCompatVectorTypes - Return true if the two specified vector types are
2541/// compatible.
2542static bool areCompatVectorTypes(const VectorType *LHS,
2543 const VectorType *RHS) {
2544 assert(LHS->isCanonical() && RHS->isCanonical());
2545 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002546 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002547}
2548
Eli Friedman3d815e72008-08-22 00:56:42 +00002549/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002550/// compatible for assignment from RHS to LHS. This handles validation of any
2551/// protocol qualifiers on the LHS or RHS.
2552///
Eli Friedman3d815e72008-08-22 00:56:42 +00002553bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2554 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002555 // Verify that the base decls are compatible: the RHS must be a subclass of
2556 // the LHS.
2557 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2558 return false;
2559
2560 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2561 // protocol qualified at all, then we are good.
2562 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2563 return true;
2564
2565 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2566 // isn't a superset.
2567 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2568 return true; // FIXME: should return false!
2569
2570 // Finally, we must have two protocol-qualified interfaces.
2571 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2572 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002573
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002574 // All LHS protocols must have a presence on the RHS.
2575 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002576
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002577 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2578 LHSPE = LHSP->qual_end();
2579 LHSPI != LHSPE; LHSPI++) {
2580 bool RHSImplementsProtocol = false;
2581
2582 // If the RHS doesn't implement the protocol on the left, the types
2583 // are incompatible.
2584 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2585 RHSPE = RHSP->qual_end();
2586 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2587 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2588 RHSImplementsProtocol = true;
2589 }
2590 // FIXME: For better diagnostics, consider passing back the protocol name.
2591 if (!RHSImplementsProtocol)
2592 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002593 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002594 // The RHS implements all protocols listed on the LHS.
2595 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002596}
2597
Steve Naroff389bf462009-02-12 17:52:19 +00002598bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2599 // get the "pointed to" types
2600 const PointerType *LHSPT = LHS->getAsPointerType();
2601 const PointerType *RHSPT = RHS->getAsPointerType();
2602
2603 if (!LHSPT || !RHSPT)
2604 return false;
2605
2606 QualType lhptee = LHSPT->getPointeeType();
2607 QualType rhptee = RHSPT->getPointeeType();
2608 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2609 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2610 // ID acts sort of like void* for ObjC interfaces
2611 if (LHSIface && isObjCIdStructType(rhptee))
2612 return true;
2613 if (RHSIface && isObjCIdStructType(lhptee))
2614 return true;
2615 if (!LHSIface || !RHSIface)
2616 return false;
2617 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2618 canAssignObjCInterfaces(RHSIface, LHSIface);
2619}
2620
Steve Naroffec0550f2007-10-15 20:41:53 +00002621/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2622/// both shall have the identically qualified version of a compatible type.
2623/// C99 6.2.7p1: Two types have compatible types if their types are the
2624/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002625bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2626 return !mergeTypes(LHS, RHS).isNull();
2627}
2628
2629QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2630 const FunctionType *lbase = lhs->getAsFunctionType();
2631 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002632 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2633 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002634 bool allLTypes = true;
2635 bool allRTypes = true;
2636
2637 // Check return type
2638 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2639 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002640 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2641 allLTypes = false;
2642 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2643 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002644
2645 if (lproto && rproto) { // two C99 style function prototypes
2646 unsigned lproto_nargs = lproto->getNumArgs();
2647 unsigned rproto_nargs = rproto->getNumArgs();
2648
2649 // Compatible functions must have the same number of arguments
2650 if (lproto_nargs != rproto_nargs)
2651 return QualType();
2652
2653 // Variadic and non-variadic functions aren't compatible
2654 if (lproto->isVariadic() != rproto->isVariadic())
2655 return QualType();
2656
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002657 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2658 return QualType();
2659
Eli Friedman3d815e72008-08-22 00:56:42 +00002660 // Check argument compatibility
2661 llvm::SmallVector<QualType, 10> types;
2662 for (unsigned i = 0; i < lproto_nargs; i++) {
2663 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2664 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2665 QualType argtype = mergeTypes(largtype, rargtype);
2666 if (argtype.isNull()) return QualType();
2667 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002668 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2669 allLTypes = false;
2670 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2671 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002672 }
2673 if (allLTypes) return lhs;
2674 if (allRTypes) return rhs;
2675 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002676 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002677 }
2678
2679 if (lproto) allRTypes = false;
2680 if (rproto) allLTypes = false;
2681
Douglas Gregor72564e72009-02-26 23:50:07 +00002682 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002683 if (proto) {
2684 if (proto->isVariadic()) return QualType();
2685 // Check that the types are compatible with the types that
2686 // would result from default argument promotions (C99 6.7.5.3p15).
2687 // The only types actually affected are promotable integer
2688 // types and floats, which would be passed as a different
2689 // type depending on whether the prototype is visible.
2690 unsigned proto_nargs = proto->getNumArgs();
2691 for (unsigned i = 0; i < proto_nargs; ++i) {
2692 QualType argTy = proto->getArgType(i);
2693 if (argTy->isPromotableIntegerType() ||
2694 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2695 return QualType();
2696 }
2697
2698 if (allLTypes) return lhs;
2699 if (allRTypes) return rhs;
2700 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002701 proto->getNumArgs(), lproto->isVariadic(),
2702 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002703 }
2704
2705 if (allLTypes) return lhs;
2706 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002707 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002708}
2709
2710QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002711 // C++ [expr]: If an expression initially has the type "reference to T", the
2712 // type is adjusted to "T" prior to any further analysis, the expression
2713 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002714 // expression is an lvalue unless the reference is an rvalue reference and
2715 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00002716 // FIXME: C++ shouldn't be going through here! The rules are different
2717 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002718 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2719 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00002720 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002721 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002722 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002723 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002724
Eli Friedman3d815e72008-08-22 00:56:42 +00002725 QualType LHSCan = getCanonicalType(LHS),
2726 RHSCan = getCanonicalType(RHS);
2727
2728 // If two types are identical, they are compatible.
2729 if (LHSCan == RHSCan)
2730 return LHS;
2731
2732 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002733 // Note that we handle extended qualifiers later, in the
2734 // case for ExtQualType.
2735 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00002736 return QualType();
2737
2738 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2739 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2740
Chris Lattner1adb8832008-01-14 05:45:46 +00002741 // We want to consider the two function types to be the same for these
2742 // comparisons, just force one to the other.
2743 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2744 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002745
2746 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002747 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2748 LHSClass = Type::ConstantArray;
2749 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2750 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002751
Nate Begeman213541a2008-04-18 23:10:10 +00002752 // Canonicalize ExtVector -> Vector.
2753 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2754 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002755
Chris Lattnerb0489812008-04-07 06:38:24 +00002756 // Consider qualified interfaces and interfaces the same.
2757 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2758 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002759
Chris Lattnera36a61f2008-04-07 05:43:21 +00002760 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002761 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002762 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2763 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2764
2765 // ID acts sort of like void* for ObjC interfaces
2766 if (LHSIface && isObjCIdStructType(RHS))
2767 return LHS;
2768 if (RHSIface && isObjCIdStructType(LHS))
2769 return RHS;
2770
Steve Naroffbc76dd02008-12-10 22:14:21 +00002771 // ID is compatible with all qualified id types.
2772 if (LHS->isObjCQualifiedIdType()) {
2773 if (const PointerType *PT = RHS->getAsPointerType()) {
2774 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002775 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002776 return LHS;
2777 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2778 // Unfortunately, this API is part of Sema (which we don't have access
2779 // to. Need to refactor. The following check is insufficient, since we
2780 // need to make sure the class implements the protocol.
2781 if (pType->isObjCInterfaceType())
2782 return LHS;
2783 }
2784 }
2785 if (RHS->isObjCQualifiedIdType()) {
2786 if (const PointerType *PT = LHS->getAsPointerType()) {
2787 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002788 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002789 return RHS;
2790 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2791 // Unfortunately, this API is part of Sema (which we don't have access
2792 // to. Need to refactor. The following check is insufficient, since we
2793 // need to make sure the class implements the protocol.
2794 if (pType->isObjCInterfaceType())
2795 return RHS;
2796 }
2797 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002798 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2799 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002800 if (const EnumType* ETy = LHS->getAsEnumType()) {
2801 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2802 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002803 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002804 if (const EnumType* ETy = RHS->getAsEnumType()) {
2805 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2806 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002807 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002808
Eli Friedman3d815e72008-08-22 00:56:42 +00002809 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002810 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002811
Steve Naroff4a746782008-01-09 22:43:08 +00002812 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002813 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00002814#define TYPE(Class, Base)
2815#define ABSTRACT_TYPE(Class, Base)
2816#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2817#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2818#include "clang/AST/TypeNodes.def"
2819 assert(false && "Non-canonical and dependent types shouldn't get here");
2820 return QualType();
2821
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002822 case Type::LValueReference:
2823 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00002824 case Type::MemberPointer:
2825 assert(false && "C++ should never be in mergeTypes");
2826 return QualType();
2827
2828 case Type::IncompleteArray:
2829 case Type::VariableArray:
2830 case Type::FunctionProto:
2831 case Type::ExtVector:
2832 case Type::ObjCQualifiedInterface:
2833 assert(false && "Types are eliminated above");
2834 return QualType();
2835
Chris Lattner1adb8832008-01-14 05:45:46 +00002836 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002837 {
2838 // Merge two pointer types, while trying to preserve typedef info
2839 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2840 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2841 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2842 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002843 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2844 return LHS;
2845 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2846 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002847 return getPointerType(ResultType);
2848 }
Steve Naroffc0febd52008-12-10 17:49:55 +00002849 case Type::BlockPointer:
2850 {
2851 // Merge two block pointer types, while trying to preserve typedef info
2852 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2853 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2854 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2855 if (ResultType.isNull()) return QualType();
2856 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2857 return LHS;
2858 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2859 return RHS;
2860 return getBlockPointerType(ResultType);
2861 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002862 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002863 {
2864 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2865 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2866 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2867 return QualType();
2868
2869 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2870 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2871 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2872 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002873 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2874 return LHS;
2875 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2876 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002877 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2878 ArrayType::ArraySizeModifier(), 0);
2879 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2880 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002881 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2882 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00002883 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2884 return LHS;
2885 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2886 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002887 if (LVAT) {
2888 // FIXME: This isn't correct! But tricky to implement because
2889 // the array's size has to be the size of LHS, but the type
2890 // has to be different.
2891 return LHS;
2892 }
2893 if (RVAT) {
2894 // FIXME: This isn't correct! But tricky to implement because
2895 // the array's size has to be the size of RHS, but the type
2896 // has to be different.
2897 return RHS;
2898 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002899 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2900 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00002901 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002902 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002903 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002904 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00002905 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00002906 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00002907 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00002908 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
2909 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002910 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002911 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002912 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002913 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00002914 case Type::Complex:
2915 // Distinct complex types are incompatible.
2916 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002917 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002918 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00002919 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2920 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00002921 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00002922 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002923 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002924 // FIXME: This should be type compatibility, e.g. whether
2925 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00002926 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2927 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2928 if (LHSIface && RHSIface &&
2929 canAssignObjCInterfaces(LHSIface, RHSIface))
2930 return LHS;
2931
Eli Friedman3d815e72008-08-22 00:56:42 +00002932 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00002933 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00002934 case Type::ObjCQualifiedId:
2935 // Distinct qualified id's are not compatible.
2936 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002937 case Type::FixedWidthInt:
2938 // Distinct fixed-width integers are not compatible.
2939 return QualType();
2940 case Type::ObjCQualifiedClass:
2941 // Distinct qualified classes are not compatible.
2942 return QualType();
2943 case Type::ExtQual:
2944 // FIXME: ExtQual types can be compatible even if they're not
2945 // identical!
2946 return QualType();
2947 // First attempt at an implementation, but I'm not really sure it's
2948 // right...
2949#if 0
2950 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
2951 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
2952 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
2953 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
2954 return QualType();
2955 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
2956 LHSBase = QualType(LQual->getBaseType(), 0);
2957 RHSBase = QualType(RQual->getBaseType(), 0);
2958 ResultType = mergeTypes(LHSBase, RHSBase);
2959 if (ResultType.isNull()) return QualType();
2960 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
2961 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
2962 return LHS;
2963 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
2964 return RHS;
2965 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
2966 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
2967 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
2968 return ResultType;
2969#endif
Steve Naroffec0550f2007-10-15 20:41:53 +00002970 }
Douglas Gregor72564e72009-02-26 23:50:07 +00002971
2972 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002973}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002974
Chris Lattner5426bf62008-04-07 07:01:58 +00002975//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00002976// Integer Predicates
2977//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00002978
Eli Friedmanad74a752008-06-28 06:23:08 +00002979unsigned ASTContext::getIntWidth(QualType T) {
2980 if (T == BoolTy)
2981 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002982 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
2983 return FWIT->getWidth();
2984 }
2985 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00002986 return (unsigned)getTypeSize(T);
2987}
2988
2989QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2990 assert(T->isSignedIntegerType() && "Unexpected type");
2991 if (const EnumType* ETy = T->getAsEnumType())
2992 T = ETy->getDecl()->getIntegerType();
2993 const BuiltinType* BTy = T->getAsBuiltinType();
2994 assert (BTy && "Unexpected signed integer type");
2995 switch (BTy->getKind()) {
2996 case BuiltinType::Char_S:
2997 case BuiltinType::SChar:
2998 return UnsignedCharTy;
2999 case BuiltinType::Short:
3000 return UnsignedShortTy;
3001 case BuiltinType::Int:
3002 return UnsignedIntTy;
3003 case BuiltinType::Long:
3004 return UnsignedLongTy;
3005 case BuiltinType::LongLong:
3006 return UnsignedLongLongTy;
3007 default:
3008 assert(0 && "Unexpected signed integer type");
3009 return QualType();
3010 }
3011}
3012
3013
3014//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00003015// Serialization Support
3016//===----------------------------------------------------------------------===//
3017
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003018/// Emit - Serialize an ASTContext object to Bitcode.
3019void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003020 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00003021 S.EmitRef(SourceMgr);
3022 S.EmitRef(Target);
3023 S.EmitRef(Idents);
3024 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003025
Ted Kremenekfee04522007-10-31 22:44:07 +00003026 // Emit the size of the type vector so that we can reserve that size
3027 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00003028 S.EmitInt(Types.size());
3029
Ted Kremenek03ed4402007-11-13 22:02:55 +00003030 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3031 I!=E;++I)
3032 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00003033
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003034 S.EmitOwnedPtr(TUDecl);
3035
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003036 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003037}
3038
Ted Kremenek0f84c002007-11-13 00:25:37 +00003039ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003040
3041 // Read the language options.
3042 LangOptions LOpts;
3043 LOpts.Read(D);
3044
Ted Kremenekfee04522007-10-31 22:44:07 +00003045 SourceManager &SM = D.ReadRef<SourceManager>();
3046 TargetInfo &t = D.ReadRef<TargetInfo>();
3047 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3048 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00003049
Ted Kremenekfee04522007-10-31 22:44:07 +00003050 unsigned size_reserve = D.ReadInt();
3051
Douglas Gregor2e1cd422008-11-17 14:58:09 +00003052 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3053 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00003054
Ted Kremenek03ed4402007-11-13 22:02:55 +00003055 for (unsigned i = 0; i < size_reserve; ++i)
3056 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00003057
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003058 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3059
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003060 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00003061
3062 return A;
3063}