blob: 749655d8712ec34d6a7f347d4d2e50f9da5fb5b0 [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) :
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +000036 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
Steve Naroffc0ac4922009-01-27 23:20:32 +000037 SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
Douglas Gregor2e1cd422008-11-17 14:58:09 +000038 Idents(idents), Selectors(sels)
Daniel Dunbare91593e2008-08-11 04:54:23 +000039{
40 if (size_reserve > 0) Types.reserve(size_reserve);
41 InitBuiltinTypes();
Douglas Gregor3573c0c2009-02-14 20:49:29 +000042 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.Freestanding);
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
Eli Friedmanb26153c2008-05-27 03:08:09 +000080 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000081}
82
83void ASTContext::PrintStats() const {
84 fprintf(stderr, "*** AST Context Stats:\n");
85 fprintf(stderr, " %d types total.\n", (int)Types.size());
86 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +000087 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000088 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
Sebastian Redlf30208a2009-01-24 21:16:55 +000089 unsigned NumMemberPointer = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000090
91 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000092 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
93 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +000094 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000095
96 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
97 Type *T = Types[i];
98 if (isa<BuiltinType>(T))
99 ++NumBuiltin;
100 else if (isa<PointerType>(T))
101 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000102 else if (isa<BlockPointerType>(T))
103 ++NumBlockPointer;
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 else if (isa<ReferenceType>(T))
105 ++NumReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000106 else if (isa<MemberPointerType>(T))
107 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000108 else if (isa<ComplexType>(T))
109 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 else if (isa<ArrayType>(T))
111 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000112 else if (isa<VectorType>(T))
113 ++NumVector;
Douglas Gregor72564e72009-02-26 23:50:07 +0000114 else if (isa<FunctionNoProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 ++NumFunctionNP;
Douglas Gregor72564e72009-02-26 23:50:07 +0000116 else if (isa<FunctionProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 ++NumFunctionP;
118 else if (isa<TypedefType>(T))
119 ++NumTypeName;
120 else if (TagType *TT = dyn_cast<TagType>(T)) {
121 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000122 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000124 case TagDecl::TK_struct: ++NumTagStruct; break;
125 case TagDecl::TK_union: ++NumTagUnion; break;
126 case TagDecl::TK_class: ++NumTagClass; break;
127 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000129 } else if (isa<ObjCInterfaceType>(T))
130 ++NumObjCInterfaces;
131 else if (isa<ObjCQualifiedInterfaceType>(T))
132 ++NumObjCQualifiedInterfaces;
133 else if (isa<ObjCQualifiedIdType>(T))
134 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000135 else if (isa<TypeOfType>(T))
136 ++NumTypeOfTypes;
Douglas Gregor72564e72009-02-26 23:50:07 +0000137 else if (isa<TypeOfExprType>(T))
138 ++NumTypeOfExprTypes;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000139 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000140 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 assert(0 && "Unknown type!");
142 }
143 }
144
145 fprintf(stderr, " %d builtin types\n", NumBuiltin);
146 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000147 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 fprintf(stderr, " %d reference types\n", NumReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000149 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000150 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000152 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
154 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
155 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
156 fprintf(stderr, " %d tagged types\n", NumTagged);
157 fprintf(stderr, " %d struct types\n", NumTagStruct);
158 fprintf(stderr, " %d union types\n", NumTagUnion);
159 fprintf(stderr, " %d class types\n", NumTagClass);
160 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000161 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000162 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000163 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000164 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000165 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000166 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor72564e72009-02-26 23:50:07 +0000167 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Steve Naroff6cc18962008-05-21 15:59:22 +0000168
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
170 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000171 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000172 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000173 NumFunctionP*sizeof(FunctionProtoType)+
174 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000175 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000176 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000177}
178
179
180void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000181 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000182}
183
Reid Spencer5f016e22007-07-11 17:01:13 +0000184void ASTContext::InitBuiltinTypes() {
185 assert(VoidTy.isNull() && "Context reinitialized?");
186
187 // C99 6.2.5p19.
188 InitBuiltinType(VoidTy, BuiltinType::Void);
189
190 // C99 6.2.5p2.
191 InitBuiltinType(BoolTy, BuiltinType::Bool);
192 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000193 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 InitBuiltinType(CharTy, BuiltinType::Char_S);
195 else
196 InitBuiltinType(CharTy, BuiltinType::Char_U);
197 // C99 6.2.5p4.
198 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
199 InitBuiltinType(ShortTy, BuiltinType::Short);
200 InitBuiltinType(IntTy, BuiltinType::Int);
201 InitBuiltinType(LongTy, BuiltinType::Long);
202 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
203
204 // C99 6.2.5p6.
205 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
206 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
207 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
208 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
209 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
210
211 // C99 6.2.5p10.
212 InitBuiltinType(FloatTy, BuiltinType::Float);
213 InitBuiltinType(DoubleTy, BuiltinType::Double);
214 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000215
Chris Lattner3a250322009-02-26 23:43:47 +0000216 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
217 InitBuiltinType(WCharTy, BuiltinType::WChar);
218 else // C99
219 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000220
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000221 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000222 InitBuiltinType(OverloadTy, BuiltinType::Overload);
223
224 // Placeholder type for type-dependent expressions whose type is
225 // completely unknown. No code should ever check a type against
226 // DependentTy and users should never see it; however, it is here to
227 // help diagnose failures to properly check for type-dependent
228 // expressions.
229 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000230
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 // C99 6.2.5p11.
232 FloatComplexTy = getComplexType(FloatTy);
233 DoubleComplexTy = getComplexType(DoubleTy);
234 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000235
Steve Naroff7e219e42007-10-15 14:41:52 +0000236 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000237 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000238 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000239 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000240 ClassStructType = 0;
241
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000242 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000243
244 // void * type
245 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000246}
247
Chris Lattner464175b2007-07-18 17:52:12 +0000248//===----------------------------------------------------------------------===//
249// Type Sizing and Analysis
250//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000251
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000252/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
253/// scalar floating point type.
254const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
255 const BuiltinType *BT = T->getAsBuiltinType();
256 assert(BT && "Not a floating point type!");
257 switch (BT->getKind()) {
258 default: assert(0 && "Not a floating point type!");
259 case BuiltinType::Float: return Target.getFloatFormat();
260 case BuiltinType::Double: return Target.getDoubleFormat();
261 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
262 }
263}
264
Chris Lattneraf707ab2009-01-24 21:53:27 +0000265/// getDeclAlign - Return a conservative estimate of the alignment of the
266/// specified decl. Note that bitfields do not have a valid alignment, so
267/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000268unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000269 unsigned Align = Target.getCharWidth();
270
271 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
272 Align = std::max(Align, AA->getAlignment());
273
Chris Lattneraf707ab2009-01-24 21:53:27 +0000274 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
275 QualType T = VD->getType();
276 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000277 if (!T->isIncompleteType() && !T->isFunctionType()) {
278 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
279 T = cast<ArrayType>(T)->getElementType();
280
281 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
282 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000283 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000284
285 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000286}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000287
Chris Lattnera7674d82007-07-13 22:13:22 +0000288/// getTypeSize - Return the size of the specified type, in bits. This method
289/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000290std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000291ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000292 T = getCanonicalType(T);
Mike Stump5e301002009-02-27 18:32:39 +0000293 uint64_t Width=0;
294 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000295 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000296#define TYPE(Class, Base)
297#define ABSTRACT_TYPE(Class, Base)
298#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
299#define DEPENDENT_TYPE(Class, Base) case Type::Class:
300#include "clang/AST/TypeNodes.def"
301 assert(false && "Should not see non-canonical or dependent types");
302 break;
303
Chris Lattner692233e2007-07-13 22:27:08 +0000304 case Type::FunctionNoProto:
305 case Type::FunctionProto:
Douglas Gregor72564e72009-02-26 23:50:07 +0000306 case Type::IncompleteArray:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000307 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000308 case Type::VariableArray:
309 assert(0 && "VLAs not implemented yet!");
310 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000311 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000312
Chris Lattner98be4942008-03-05 18:54:05 +0000313 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000314 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000315 Align = EltInfo.second;
316 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000317 }
Nate Begeman213541a2008-04-18 23:10:10 +0000318 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000319 case Type::Vector: {
320 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000321 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000322 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000323 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000324 // If the alignment is not a power of 2, round up to the next power of 2.
325 // This happens for non-power-of-2 length vectors.
326 // FIXME: this should probably be a target property.
327 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000328 break;
329 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000330
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000331 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000332 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000333 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000334 case BuiltinType::Void:
335 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000336 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000337 Width = Target.getBoolWidth();
338 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000339 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000340 case BuiltinType::Char_S:
341 case BuiltinType::Char_U:
342 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000343 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000344 Width = Target.getCharWidth();
345 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000346 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000347 case BuiltinType::WChar:
348 Width = Target.getWCharWidth();
349 Align = Target.getWCharAlign();
350 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000351 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000352 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000353 Width = Target.getShortWidth();
354 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000355 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000356 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000357 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000358 Width = Target.getIntWidth();
359 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000360 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000361 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000362 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000363 Width = Target.getLongWidth();
364 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000365 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000366 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000367 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000368 Width = Target.getLongLongWidth();
369 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000370 break;
371 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000372 Width = Target.getFloatWidth();
373 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000374 break;
375 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000376 Width = Target.getDoubleWidth();
377 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000378 break;
379 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000380 Width = Target.getLongDoubleWidth();
381 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000382 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000383 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000384 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000385 case Type::FixedWidthInt:
386 // FIXME: This isn't precisely correct; the width/alignment should depend
387 // on the available types for the target
388 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000389 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000390 Align = Width;
391 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000392 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000393 // FIXME: Pointers into different addr spaces could have different sizes and
394 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000395 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000396 case Type::ObjCQualifiedId:
Eli Friedman4bdf0872009-02-22 04:02:33 +0000397 case Type::ObjCQualifiedClass:
Douglas Gregor72564e72009-02-26 23:50:07 +0000398 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000399 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000400 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000401 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000402 case Type::BlockPointer: {
403 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
404 Width = Target.getPointerWidth(AS);
405 Align = Target.getPointerAlign(AS);
406 break;
407 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000408 case Type::Pointer: {
409 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000410 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000411 Align = Target.getPointerAlign(AS);
412 break;
413 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000414 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000415 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000416 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000417 // FIXME: This is wrong for struct layout: a reference in a struct has
418 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000419 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000420 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000421 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000422 // the GCC ABI, where pointers to data are one pointer large, pointers to
423 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000424 // other compilers too, we need to delegate this completely to TargetInfo
425 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000426 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
427 unsigned AS = Pointee.getAddressSpace();
428 Width = Target.getPointerWidth(AS);
429 if (Pointee->isFunctionType())
430 Width *= 2;
431 Align = Target.getPointerAlign(AS);
432 // GCC aligns at single pointer width.
433 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000434 case Type::Complex: {
435 // Complex types have the same alignment as their elements, but twice the
436 // size.
437 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000438 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000439 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000440 Align = EltInfo.second;
441 break;
442 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000443 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000444 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000445 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
446 Width = Layout.getSize();
447 Align = Layout.getAlignment();
448 break;
449 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000450 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000451 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000452 const TagType *TT = cast<TagType>(T);
453
454 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000455 Width = 1;
456 Align = 1;
457 break;
458 }
459
Daniel Dunbar1d751182008-11-08 05:48:37 +0000460 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000461 return getTypeInfo(ET->getDecl()->getIntegerType());
462
Daniel Dunbar1d751182008-11-08 05:48:37 +0000463 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000464 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
465 Width = Layout.getSize();
466 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000467 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000468 }
Chris Lattner71763312008-04-06 22:05:18 +0000469 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000470
Chris Lattner464175b2007-07-18 17:52:12 +0000471 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000472 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000473}
474
Chris Lattner34ebde42009-01-27 18:08:34 +0000475/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
476/// type for the current target in bits. This can be different than the ABI
477/// alignment in cases where it is beneficial for performance to overalign
478/// a data type.
479unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
480 unsigned ABIAlign = getTypeAlign(T);
481
482 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000483 if (T->isSpecificBuiltinType(BuiltinType::Double))
484 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000485
486 return ABIAlign;
487}
488
489
Devang Patel8b277042008-06-04 21:22:16 +0000490/// LayoutField - Field layout.
491void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000492 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000493 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000494 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000495 uint64_t FieldOffset = IsUnion ? 0 : Size;
496 uint64_t FieldSize;
497 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000498
499 // FIXME: Should this override struct packing? Probably we want to
500 // take the minimum?
501 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
502 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000503
504 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
505 // TODO: Need to check this algorithm on other targets!
506 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000507 FieldSize =
508 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000509
510 std::pair<uint64_t, unsigned> FieldInfo =
511 Context.getTypeInfo(FD->getType());
512 uint64_t TypeSize = FieldInfo.first;
513
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000514 // Determine the alignment of this bitfield. The packing
515 // attributes define a maximum and the alignment attribute defines
516 // a minimum.
517 // FIXME: What is the right behavior when the specified alignment
518 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000519 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000520 if (FieldPacking)
521 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000522 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
523 FieldAlign = std::max(FieldAlign, AA->getAlignment());
524
525 // Check if we need to add padding to give the field the correct
526 // alignment.
527 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
528 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
529
530 // Padding members don't affect overall alignment
531 if (!FD->getIdentifier())
532 FieldAlign = 1;
533 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000534 if (FD->getType()->isIncompleteArrayType()) {
535 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000536 // query getTypeInfo about these, so we figure it out here.
537 // Flexible array members don't have any size, but they
538 // have to be aligned appropriately for their element type.
539 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000540 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000541 FieldAlign = Context.getTypeAlign(ATy->getElementType());
542 } else {
543 std::pair<uint64_t, unsigned> FieldInfo =
544 Context.getTypeInfo(FD->getType());
545 FieldSize = FieldInfo.first;
546 FieldAlign = FieldInfo.second;
547 }
548
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000549 // Determine the alignment of this bitfield. The packing
550 // attributes define a maximum and the alignment attribute defines
551 // a minimum. Additionally, the packing alignment must be at least
552 // a byte for non-bitfields.
553 //
554 // FIXME: What is the right behavior when the specified alignment
555 // is smaller than the specified packing?
556 if (FieldPacking)
557 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000558 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
559 FieldAlign = std::max(FieldAlign, AA->getAlignment());
560
561 // Round up the current record size to the field's alignment boundary.
562 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
563 }
564
565 // Place this field at the current location.
566 FieldOffsets[FieldNo] = FieldOffset;
567
568 // Reserve space for this field.
569 if (IsUnion) {
570 Size = std::max(Size, FieldSize);
571 } else {
572 Size = FieldOffset + FieldSize;
573 }
574
575 // Remember max struct/class alignment.
576 Alignment = std::max(Alignment, FieldAlign);
577}
578
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000579static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
580 std::vector<FieldDecl*> &Fields) {
581 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
582 if (SuperClass)
583 CollectObjCIvars(SuperClass, Fields);
584 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
585 E = OI->ivar_end(); I != E; ++I) {
586 ObjCIvarDecl *IVDecl = (*I);
587 if (!IVDecl->isInvalidDecl())
588 Fields.push_back(cast<FieldDecl>(IVDecl));
589 }
590}
591
592/// addRecordToClass - produces record info. for the class for its
593/// ivars and all those inherited.
594///
595const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
596{
597 const RecordDecl *&RD = ASTRecordForInterface[D];
598 if (RD)
599 return RD;
600 std::vector<FieldDecl*> RecFields;
601 CollectObjCIvars(D, RecFields);
602 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
603 D->getLocation(),
604 D->getIdentifier());
605 /// FIXME! Can do collection of ivars and adding to the record while
606 /// doing it.
607 for (unsigned int i = 0; i != RecFields.size(); i++) {
608 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
609 RecFields[i]->getLocation(),
610 RecFields[i]->getIdentifier(),
611 RecFields[i]->getType(),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000612 RecFields[i]->getBitWidth(), false);
Douglas Gregor482b77d2009-01-12 23:27:07 +0000613 NewRD->addDecl(Field);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000614 }
615 NewRD->completeDefinition(*this);
616 RD = NewRD;
617 return RD;
618}
Devang Patel44a3dde2008-06-04 21:54:36 +0000619
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000620/// setFieldDecl - maps a field for the given Ivar reference node.
621//
622void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
623 const ObjCIvarDecl *Ivar,
624 const ObjCIvarRefExpr *MRef) {
625 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
626 lookupFieldDeclForIvar(*this, Ivar);
627 ASTFieldForIvarRef[MRef] = FD;
628}
629
Chris Lattner61710852008-10-05 17:34:18 +0000630/// getASTObjcInterfaceLayout - Get or compute information about the layout of
631/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000632/// position information.
633const ASTRecordLayout &
634ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
635 // Look up this layout, if already laid out, return what we have.
636 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
637 if (Entry) return *Entry;
638
639 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
640 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000641 ASTRecordLayout *NewEntry = NULL;
642 unsigned FieldCount = D->ivar_size();
643 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
644 FieldCount++;
645 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
646 unsigned Alignment = SL.getAlignment();
647 uint64_t Size = SL.getSize();
648 NewEntry = new ASTRecordLayout(Size, Alignment);
649 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000650 // Super class is at the beginning of the layout.
651 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000652 } else {
653 NewEntry = new ASTRecordLayout();
654 NewEntry->InitializeLayout(FieldCount);
655 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000656 Entry = NewEntry;
657
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000658 unsigned StructPacking = 0;
659 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
660 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000661
662 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
663 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
664 AA->getAlignment()));
665
666 // Layout each ivar sequentially.
667 unsigned i = 0;
668 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
669 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
670 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000671 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000672 }
673
674 // Finally, round the size of the total struct up to the alignment of the
675 // struct itself.
676 NewEntry->FinalizeLayout();
677 return *NewEntry;
678}
679
Devang Patel88a981b2007-11-01 19:11:01 +0000680/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000681/// specified record (struct/union/class), which indicates its size and field
682/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000683const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000684 D = D->getDefinition(*this);
685 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000686
Chris Lattner464175b2007-07-18 17:52:12 +0000687 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000688 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000689 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000690
Devang Patel88a981b2007-11-01 19:11:01 +0000691 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
692 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
693 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000694 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000695
Douglas Gregore267ff32008-12-11 20:41:00 +0000696 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor44b43212008-12-11 16:49:14 +0000697 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000698 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000699
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000700 unsigned StructPacking = 0;
701 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
702 StructPacking = PA->getAlignment();
703
Eli Friedman4bd998b2008-05-30 09:31:38 +0000704 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000705 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
706 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000707
Eli Friedman4bd998b2008-05-30 09:31:38 +0000708 // Layout each field, for now, just sequentially, respecting alignment. In
709 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000710 unsigned FieldIdx = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000711 for (RecordDecl::field_iterator Field = D->field_begin(),
712 FieldEnd = D->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000713 Field != FieldEnd; (void)++Field, ++FieldIdx)
714 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000715
716 // Finally, round the size of the total struct up to the alignment of the
717 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000718 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000719 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000720}
721
Chris Lattnera7674d82007-07-13 22:13:22 +0000722//===----------------------------------------------------------------------===//
723// Type creation/memoization methods
724//===----------------------------------------------------------------------===//
725
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000726QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000727 QualType CanT = getCanonicalType(T);
728 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000729 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000730
731 // If we are composing extended qualifiers together, merge together into one
732 // ExtQualType node.
733 unsigned CVRQuals = T.getCVRQualifiers();
734 QualType::GCAttrTypes GCAttr = QualType::GCNone;
735 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000736
Chris Lattnerb7d25532009-02-18 22:53:11 +0000737 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
738 // If this type already has an address space specified, it cannot get
739 // another one.
740 assert(EQT->getAddressSpace() == 0 &&
741 "Type cannot be in multiple addr spaces!");
742 GCAttr = EQT->getObjCGCAttr();
743 TypeNode = EQT->getBaseType();
744 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000745
Chris Lattnerb7d25532009-02-18 22:53:11 +0000746 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000747 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000748 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000749 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000750 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000751 return QualType(EXTQy, CVRQuals);
752
Christopher Lambebb97e92008-02-04 02:31:56 +0000753 // If the base type isn't canonical, this won't be a canonical type either,
754 // so fill in the canonical type field.
755 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000756 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000757 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000758
Chris Lattnerb7d25532009-02-18 22:53:11 +0000759 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000760 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000761 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000762 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000763 ExtQualType *New =
764 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000765 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000766 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000767 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000768}
769
Chris Lattnerb7d25532009-02-18 22:53:11 +0000770QualType ASTContext::getObjCGCQualType(QualType T,
771 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000772 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000773 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000774 return T;
775
Chris Lattnerb7d25532009-02-18 22:53:11 +0000776 // If we are composing extended qualifiers together, merge together into one
777 // ExtQualType node.
778 unsigned CVRQuals = T.getCVRQualifiers();
779 Type *TypeNode = T.getTypePtr();
780 unsigned AddressSpace = 0;
781
782 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
783 // If this type already has an address space specified, it cannot get
784 // another one.
785 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
786 "Type cannot be in multiple addr spaces!");
787 AddressSpace = EQT->getAddressSpace();
788 TypeNode = EQT->getBaseType();
789 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000790
791 // Check if we've already instantiated an gc qual'd type of this type.
792 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000793 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000794 void *InsertPos = 0;
795 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000796 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000797
798 // If the base type isn't canonical, this won't be a canonical type either,
799 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000800 // FIXME: Isn't this also not canonical if the base type is a array
801 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000802 QualType Canonical;
803 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000804 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000805
Chris Lattnerb7d25532009-02-18 22:53:11 +0000806 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000807 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
808 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
809 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000810 ExtQualType *New =
811 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000812 ExtQualTypes.InsertNode(New, InsertPos);
813 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000814 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000815}
Chris Lattnera7674d82007-07-13 22:13:22 +0000816
Reid Spencer5f016e22007-07-11 17:01:13 +0000817/// getComplexType - Return the uniqued reference to the type for a complex
818/// number with the specified element type.
819QualType ASTContext::getComplexType(QualType T) {
820 // Unique pointers, to guarantee there is only one pointer of a particular
821 // structure.
822 llvm::FoldingSetNodeID ID;
823 ComplexType::Profile(ID, T);
824
825 void *InsertPos = 0;
826 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
827 return QualType(CT, 0);
828
829 // If the pointee type isn't canonical, this won't be a canonical type either,
830 // so fill in the canonical type field.
831 QualType Canonical;
832 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000833 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000834
835 // Get the new insert position for the node we care about.
836 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000837 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000838 }
Steve Narofff83820b2009-01-27 22:08:43 +0000839 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 Types.push_back(New);
841 ComplexTypes.InsertNode(New, InsertPos);
842 return QualType(New, 0);
843}
844
Eli Friedmanf98aba32009-02-13 02:31:07 +0000845QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
846 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
847 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
848 FixedWidthIntType *&Entry = Map[Width];
849 if (!Entry)
850 Entry = new FixedWidthIntType(Width, Signed);
851 return QualType(Entry, 0);
852}
Reid Spencer5f016e22007-07-11 17:01:13 +0000853
854/// getPointerType - Return the uniqued reference to the type for a pointer to
855/// the specified type.
856QualType ASTContext::getPointerType(QualType T) {
857 // Unique pointers, to guarantee there is only one pointer of a particular
858 // structure.
859 llvm::FoldingSetNodeID ID;
860 PointerType::Profile(ID, T);
861
862 void *InsertPos = 0;
863 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
864 return QualType(PT, 0);
865
866 // If the pointee type isn't canonical, this won't be a canonical type either,
867 // so fill in the canonical type field.
868 QualType Canonical;
869 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000870 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000871
872 // Get the new insert position for the node we care about.
873 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000874 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000875 }
Steve Narofff83820b2009-01-27 22:08:43 +0000876 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 Types.push_back(New);
878 PointerTypes.InsertNode(New, InsertPos);
879 return QualType(New, 0);
880}
881
Steve Naroff5618bd42008-08-27 16:04:49 +0000882/// getBlockPointerType - Return the uniqued reference to the type for
883/// a pointer to the specified block.
884QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000885 assert(T->isFunctionType() && "block of function types only");
886 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000887 // structure.
888 llvm::FoldingSetNodeID ID;
889 BlockPointerType::Profile(ID, T);
890
891 void *InsertPos = 0;
892 if (BlockPointerType *PT =
893 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
894 return QualType(PT, 0);
895
Steve Naroff296e8d52008-08-28 19:20:44 +0000896 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000897 // type either so fill in the canonical type field.
898 QualType Canonical;
899 if (!T->isCanonical()) {
900 Canonical = getBlockPointerType(getCanonicalType(T));
901
902 // Get the new insert position for the node we care about.
903 BlockPointerType *NewIP =
904 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000905 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000906 }
Steve Narofff83820b2009-01-27 22:08:43 +0000907 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000908 Types.push_back(New);
909 BlockPointerTypes.InsertNode(New, InsertPos);
910 return QualType(New, 0);
911}
912
Reid Spencer5f016e22007-07-11 17:01:13 +0000913/// getReferenceType - Return the uniqued reference to the type for a reference
914/// to the specified type.
915QualType ASTContext::getReferenceType(QualType T) {
916 // Unique pointers, to guarantee there is only one pointer of a particular
917 // structure.
918 llvm::FoldingSetNodeID ID;
919 ReferenceType::Profile(ID, T);
920
921 void *InsertPos = 0;
922 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
923 return QualType(RT, 0);
924
925 // If the referencee type isn't canonical, this won't be a canonical type
926 // either, so fill in the canonical type field.
927 QualType Canonical;
928 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000929 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000930
931 // Get the new insert position for the node we care about.
932 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000933 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000934 }
935
Steve Narofff83820b2009-01-27 22:08:43 +0000936 ReferenceType *New = new (*this,8) ReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000937 Types.push_back(New);
938 ReferenceTypes.InsertNode(New, InsertPos);
939 return QualType(New, 0);
940}
941
Sebastian Redlf30208a2009-01-24 21:16:55 +0000942/// getMemberPointerType - Return the uniqued reference to the type for a
943/// member pointer to the specified type, in the specified class.
944QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
945{
946 // Unique pointers, to guarantee there is only one pointer of a particular
947 // structure.
948 llvm::FoldingSetNodeID ID;
949 MemberPointerType::Profile(ID, T, Cls);
950
951 void *InsertPos = 0;
952 if (MemberPointerType *PT =
953 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
954 return QualType(PT, 0);
955
956 // If the pointee or class type isn't canonical, this won't be a canonical
957 // type either, so fill in the canonical type field.
958 QualType Canonical;
959 if (!T->isCanonical()) {
960 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
961
962 // Get the new insert position for the node we care about.
963 MemberPointerType *NewIP =
964 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
965 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
966 }
Steve Narofff83820b2009-01-27 22:08:43 +0000967 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000968 Types.push_back(New);
969 MemberPointerTypes.InsertNode(New, InsertPos);
970 return QualType(New, 0);
971}
972
Steve Narofffb22d962007-08-30 01:06:46 +0000973/// getConstantArrayType - Return the unique reference to the type for an
974/// array of the specified element type.
975QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000976 const llvm::APInt &ArySize,
977 ArrayType::ArraySizeModifier ASM,
978 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000979 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +0000980 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000981
982 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000983 if (ConstantArrayType *ATP =
984 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 return QualType(ATP, 0);
986
987 // If the element type isn't canonical, this won't be a canonical type either,
988 // so fill in the canonical type field.
989 QualType Canonical;
990 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000991 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000992 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000994 ConstantArrayType *NewIP =
995 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000996 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000997 }
998
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000999 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001000 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001001 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001002 Types.push_back(New);
1003 return QualType(New, 0);
1004}
1005
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001006/// getVariableArrayType - Returns a non-unique reference to the type for a
1007/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001008QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1009 ArrayType::ArraySizeModifier ASM,
1010 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001011 // Since we don't unique expressions, it isn't possible to unique VLA's
1012 // that have an expression provided for their size.
1013
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001014 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001015 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001016
1017 VariableArrayTypes.push_back(New);
1018 Types.push_back(New);
1019 return QualType(New, 0);
1020}
1021
Douglas Gregor898574e2008-12-05 23:32:09 +00001022/// getDependentSizedArrayType - Returns a non-unique reference to
1023/// the type for a dependently-sized array of the specified element
1024/// type. FIXME: We will need these to be uniqued, or at least
1025/// comparable, at some point.
1026QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1027 ArrayType::ArraySizeModifier ASM,
1028 unsigned EltTypeQuals) {
1029 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1030 "Size must be type- or value-dependent!");
1031
1032 // Since we don't unique expressions, it isn't possible to unique
1033 // dependently-sized array types.
1034
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001035 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001036 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1037 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001038
1039 DependentSizedArrayTypes.push_back(New);
1040 Types.push_back(New);
1041 return QualType(New, 0);
1042}
1043
Eli Friedmanc5773c42008-02-15 18:16:39 +00001044QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1045 ArrayType::ArraySizeModifier ASM,
1046 unsigned EltTypeQuals) {
1047 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001048 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001049
1050 void *InsertPos = 0;
1051 if (IncompleteArrayType *ATP =
1052 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1053 return QualType(ATP, 0);
1054
1055 // If the element type isn't canonical, this won't be a canonical type
1056 // either, so fill in the canonical type field.
1057 QualType Canonical;
1058
1059 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001060 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001061 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001062
1063 // Get the new insert position for the node we care about.
1064 IncompleteArrayType *NewIP =
1065 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001066 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001067 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001068
Steve Narofff83820b2009-01-27 22:08:43 +00001069 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001070 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001071
1072 IncompleteArrayTypes.InsertNode(New, InsertPos);
1073 Types.push_back(New);
1074 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001075}
1076
Steve Naroff73322922007-07-18 18:00:27 +00001077/// getVectorType - Return the unique reference to a vector type of
1078/// the specified element type and size. VectorType must be a built-in type.
1079QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001080 BuiltinType *baseType;
1081
Chris Lattnerf52ab252008-04-06 22:59:24 +00001082 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001083 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001084
1085 // Check if we've already instantiated a vector of this type.
1086 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001087 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 void *InsertPos = 0;
1089 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1090 return QualType(VTP, 0);
1091
1092 // If the element type isn't canonical, this won't be a canonical type either,
1093 // so fill in the canonical type field.
1094 QualType Canonical;
1095 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001096 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001097
1098 // Get the new insert position for the node we care about.
1099 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001100 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001101 }
Steve Narofff83820b2009-01-27 22:08:43 +00001102 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001103 VectorTypes.InsertNode(New, InsertPos);
1104 Types.push_back(New);
1105 return QualType(New, 0);
1106}
1107
Nate Begeman213541a2008-04-18 23:10:10 +00001108/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001109/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001110QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001111 BuiltinType *baseType;
1112
Chris Lattnerf52ab252008-04-06 22:59:24 +00001113 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001114 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001115
1116 // Check if we've already instantiated a vector of this type.
1117 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001118 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001119 void *InsertPos = 0;
1120 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1121 return QualType(VTP, 0);
1122
1123 // If the element type isn't canonical, this won't be a canonical type either,
1124 // so fill in the canonical type field.
1125 QualType Canonical;
1126 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001127 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001128
1129 // Get the new insert position for the node we care about.
1130 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001131 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001132 }
Steve Narofff83820b2009-01-27 22:08:43 +00001133 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001134 VectorTypes.InsertNode(New, InsertPos);
1135 Types.push_back(New);
1136 return QualType(New, 0);
1137}
1138
Douglas Gregor72564e72009-02-26 23:50:07 +00001139/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001140///
Douglas Gregor72564e72009-02-26 23:50:07 +00001141QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001142 // Unique functions, to guarantee there is only one function of a particular
1143 // structure.
1144 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001145 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001146
1147 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001148 if (FunctionNoProtoType *FT =
1149 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001150 return QualType(FT, 0);
1151
1152 QualType Canonical;
1153 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001154 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001155
1156 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001157 FunctionNoProtoType *NewIP =
1158 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001159 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001160 }
1161
Douglas Gregor72564e72009-02-26 23:50:07 +00001162 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001163 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001164 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001165 return QualType(New, 0);
1166}
1167
1168/// getFunctionType - Return a normal function type with a typed argument
1169/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001170QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001171 unsigned NumArgs, bool isVariadic,
1172 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001173 // Unique functions, to guarantee there is only one function of a particular
1174 // structure.
1175 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001176 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001177 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001178
1179 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001180 if (FunctionProtoType *FTP =
1181 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001182 return QualType(FTP, 0);
1183
1184 // Determine whether the type being created is already canonical or not.
1185 bool isCanonical = ResultTy->isCanonical();
1186 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1187 if (!ArgArray[i]->isCanonical())
1188 isCanonical = false;
1189
1190 // If this type isn't canonical, get the canonical version of it.
1191 QualType Canonical;
1192 if (!isCanonical) {
1193 llvm::SmallVector<QualType, 16> CanonicalArgs;
1194 CanonicalArgs.reserve(NumArgs);
1195 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001196 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001197
Chris Lattnerf52ab252008-04-06 22:59:24 +00001198 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001199 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001200 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001201
1202 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001203 FunctionProtoType *NewIP =
1204 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001205 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 }
1207
Douglas Gregor72564e72009-02-26 23:50:07 +00001208 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001209 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001210 FunctionProtoType *FTP =
1211 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001212 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001213 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001214 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001216 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001217 return QualType(FTP, 0);
1218}
1219
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001220/// getTypeDeclType - Return the unique reference to the type for the
1221/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001222QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001223 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001224 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1225
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001226 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001227 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001228 else if (isa<TemplateTypeParmDecl>(Decl)) {
1229 assert(false && "Template type parameter types are always available.");
1230 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001231 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001232
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001233 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001234 if (PrevDecl)
1235 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001236 else
1237 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001238 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001239 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1240 if (PrevDecl)
1241 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001242 else
1243 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001244 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001245 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001246 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001247
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001248 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001249 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001250}
1251
Reid Spencer5f016e22007-07-11 17:01:13 +00001252/// getTypedefType - Return the unique reference to the type for the
1253/// specified typename decl.
1254QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1255 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1256
Chris Lattnerf52ab252008-04-06 22:59:24 +00001257 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001258 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001259 Types.push_back(Decl->TypeForDecl);
1260 return QualType(Decl->TypeForDecl, 0);
1261}
1262
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001263/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001264/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001265QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001266 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1267
Steve Narofff83820b2009-01-27 22:08:43 +00001268 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001269 Types.push_back(Decl->TypeForDecl);
1270 return QualType(Decl->TypeForDecl, 0);
1271}
1272
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00001273/// buildObjCInterfaceType - Returns a new type for the interface
1274/// declaration, regardless. It also removes any previously built
1275/// record declaration so caller can rebuild it.
1276QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) {
1277 const RecordDecl *&RD = ASTRecordForInterface[Decl];
1278 if (RD)
1279 RD = 0;
1280 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
1281 Types.push_back(Decl->TypeForDecl);
1282 return QualType(Decl->TypeForDecl, 0);
1283}
1284
Douglas Gregorfab9d672009-02-05 23:33:38 +00001285/// \brief Retrieve the template type parameter type for a template
1286/// parameter with the given depth, index, and (optionally) name.
1287QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1288 IdentifierInfo *Name) {
1289 llvm::FoldingSetNodeID ID;
1290 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1291 void *InsertPos = 0;
1292 TemplateTypeParmType *TypeParm
1293 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1294
1295 if (TypeParm)
1296 return QualType(TypeParm, 0);
1297
1298 if (Name)
1299 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1300 getTemplateTypeParmType(Depth, Index));
1301 else
1302 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1303
1304 Types.push_back(TypeParm);
1305 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1306
1307 return QualType(TypeParm, 0);
1308}
1309
Douglas Gregor55f6b142009-02-09 18:46:07 +00001310QualType
1311ASTContext::getClassTemplateSpecializationType(TemplateDecl *Template,
1312 unsigned NumArgs,
1313 uintptr_t *Args, bool *ArgIsType,
1314 QualType Canon) {
Douglas Gregorfc705b82009-02-26 22:19:44 +00001315 Canon = getCanonicalType(Canon);
1316
Douglas Gregor55f6b142009-02-09 18:46:07 +00001317 llvm::FoldingSetNodeID ID;
1318 ClassTemplateSpecializationType::Profile(ID, Template, NumArgs, Args,
1319 ArgIsType);
1320 void *InsertPos = 0;
1321 ClassTemplateSpecializationType *Spec
1322 = ClassTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
1323
1324 if (Spec)
1325 return QualType(Spec, 0);
1326
1327 void *Mem = Allocate(sizeof(ClassTemplateSpecializationType) +
1328 (sizeof(uintptr_t) *
1329 (ClassTemplateSpecializationType::
1330 getNumPackedWords(NumArgs) +
1331 NumArgs)), 8);
1332 Spec = new (Mem) ClassTemplateSpecializationType(Template, NumArgs, Args,
1333 ArgIsType, Canon);
1334 Types.push_back(Spec);
1335 ClassTemplateSpecializationTypes.InsertNode(Spec, InsertPos);
1336
1337 return QualType(Spec, 0);
1338}
1339
Chris Lattner88cb27a2008-04-07 04:56:42 +00001340/// CmpProtocolNames - Comparison predicate for sorting protocols
1341/// alphabetically.
1342static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1343 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001344 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001345}
1346
1347static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1348 unsigned &NumProtocols) {
1349 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1350
1351 // Sort protocols, keyed by name.
1352 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1353
1354 // Remove duplicates.
1355 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1356 NumProtocols = ProtocolsEnd-Protocols;
1357}
1358
1359
Chris Lattner065f0d72008-04-07 04:44:08 +00001360/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1361/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001362QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1363 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001364 // Sort the protocol list alphabetically to canonicalize it.
1365 SortAndUniqueProtocols(Protocols, NumProtocols);
1366
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001367 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001368 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001369
1370 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001371 if (ObjCQualifiedInterfaceType *QT =
1372 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001373 return QualType(QT, 0);
1374
1375 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001376 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001377 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001378
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001379 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001380 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001381 return QualType(QType, 0);
1382}
1383
Chris Lattner88cb27a2008-04-07 04:56:42 +00001384/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1385/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001386QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001387 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001388 // Sort the protocol list alphabetically to canonicalize it.
1389 SortAndUniqueProtocols(Protocols, NumProtocols);
1390
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001391 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001392 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001393
1394 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001395 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001396 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001397 return QualType(QT, 0);
1398
1399 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001400 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001401 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001402 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001403 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001404 return QualType(QType, 0);
1405}
1406
Douglas Gregor72564e72009-02-26 23:50:07 +00001407/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1408/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001409/// multiple declarations that refer to "typeof(x)" all contain different
1410/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1411/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001412QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001413 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001414 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001415 Types.push_back(toe);
1416 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001417}
1418
Steve Naroff9752f252007-08-01 18:02:17 +00001419/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1420/// TypeOfType AST's. The only motivation to unique these nodes would be
1421/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1422/// an issue. This doesn't effect the type checker, since it operates
1423/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001424QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001425 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001426 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001427 Types.push_back(tot);
1428 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001429}
1430
Reid Spencer5f016e22007-07-11 17:01:13 +00001431/// getTagDeclType - Return the unique reference to the type for the
1432/// specified TagDecl (struct/union/class/enum) decl.
1433QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001434 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001435 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001436}
1437
1438/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1439/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1440/// needs to agree with the definition in <stddef.h>.
1441QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001442 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001443}
1444
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001445/// getSignedWCharType - Return the type of "signed wchar_t".
1446/// Used when in C++, as a GCC extension.
1447QualType ASTContext::getSignedWCharType() const {
1448 // FIXME: derive from "Target" ?
1449 return WCharTy;
1450}
1451
1452/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1453/// Used when in C++, as a GCC extension.
1454QualType ASTContext::getUnsignedWCharType() const {
1455 // FIXME: derive from "Target" ?
1456 return UnsignedIntTy;
1457}
1458
Chris Lattner8b9023b2007-07-13 03:05:23 +00001459/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1460/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1461QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001462 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001463}
1464
Chris Lattnere6327742008-04-02 05:18:44 +00001465//===----------------------------------------------------------------------===//
1466// Type Operators
1467//===----------------------------------------------------------------------===//
1468
Chris Lattner77c96472008-04-06 22:41:35 +00001469/// getCanonicalType - Return the canonical (structural) type corresponding to
1470/// the specified potentially non-canonical type. The non-canonical version
1471/// of a type may have many "decorated" versions of types. Decorators can
1472/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1473/// to be free of any of these, allowing two canonical types to be compared
1474/// for exact equality with a simple pointer comparison.
1475QualType ASTContext::getCanonicalType(QualType T) {
1476 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001477
1478 // If the result has type qualifiers, make sure to canonicalize them as well.
1479 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1480 if (TypeQuals == 0) return CanType;
1481
1482 // If the type qualifiers are on an array type, get the canonical type of the
1483 // array with the qualifiers applied to the element type.
1484 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1485 if (!AT)
1486 return CanType.getQualifiedType(TypeQuals);
1487
1488 // Get the canonical version of the element with the extra qualifiers on it.
1489 // This can recursively sink qualifiers through multiple levels of arrays.
1490 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1491 NewEltTy = getCanonicalType(NewEltTy);
1492
1493 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1494 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1495 CAT->getIndexTypeQualifier());
1496 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1497 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1498 IAT->getIndexTypeQualifier());
1499
Douglas Gregor898574e2008-12-05 23:32:09 +00001500 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1501 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1502 DSAT->getSizeModifier(),
1503 DSAT->getIndexTypeQualifier());
1504
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001505 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1506 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1507 VAT->getSizeModifier(),
1508 VAT->getIndexTypeQualifier());
1509}
1510
1511
1512const ArrayType *ASTContext::getAsArrayType(QualType T) {
1513 // Handle the non-qualified case efficiently.
1514 if (T.getCVRQualifiers() == 0) {
1515 // Handle the common positive case fast.
1516 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1517 return AT;
1518 }
1519
1520 // Handle the common negative case fast, ignoring CVR qualifiers.
1521 QualType CType = T->getCanonicalTypeInternal();
1522
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001523 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001524 // test.
1525 if (!isa<ArrayType>(CType) &&
1526 !isa<ArrayType>(CType.getUnqualifiedType()))
1527 return 0;
1528
1529 // Apply any CVR qualifiers from the array type to the element type. This
1530 // implements C99 6.7.3p8: "If the specification of an array type includes
1531 // any type qualifiers, the element type is so qualified, not the array type."
1532
1533 // If we get here, we either have type qualifiers on the type, or we have
1534 // sugar such as a typedef in the way. If we have type qualifiers on the type
1535 // we must propagate them down into the elemeng type.
1536 unsigned CVRQuals = T.getCVRQualifiers();
1537 unsigned AddrSpace = 0;
1538 Type *Ty = T.getTypePtr();
1539
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001540 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001541 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001542 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1543 AddrSpace = EXTQT->getAddressSpace();
1544 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001545 } else {
1546 T = Ty->getDesugaredType();
1547 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1548 break;
1549 CVRQuals |= T.getCVRQualifiers();
1550 Ty = T.getTypePtr();
1551 }
1552 }
1553
1554 // If we have a simple case, just return now.
1555 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1556 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1557 return ATy;
1558
1559 // Otherwise, we have an array and we have qualifiers on it. Push the
1560 // qualifiers into the array element type and return a new array type.
1561 // Get the canonical version of the element with the extra qualifiers on it.
1562 // This can recursively sink qualifiers through multiple levels of arrays.
1563 QualType NewEltTy = ATy->getElementType();
1564 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001565 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001566 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1567
1568 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1569 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1570 CAT->getSizeModifier(),
1571 CAT->getIndexTypeQualifier()));
1572 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1573 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1574 IAT->getSizeModifier(),
1575 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001576
Douglas Gregor898574e2008-12-05 23:32:09 +00001577 if (const DependentSizedArrayType *DSAT
1578 = dyn_cast<DependentSizedArrayType>(ATy))
1579 return cast<ArrayType>(
1580 getDependentSizedArrayType(NewEltTy,
1581 DSAT->getSizeExpr(),
1582 DSAT->getSizeModifier(),
1583 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001584
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001585 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1586 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1587 VAT->getSizeModifier(),
1588 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001589}
1590
1591
Chris Lattnere6327742008-04-02 05:18:44 +00001592/// getArrayDecayedType - Return the properly qualified result of decaying the
1593/// specified array type to a pointer. This operation is non-trivial when
1594/// handling typedefs etc. The canonical type of "T" must be an array type,
1595/// this returns a pointer to a properly qualified element of the array.
1596///
1597/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1598QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001599 // Get the element type with 'getAsArrayType' so that we don't lose any
1600 // typedefs in the element type of the array. This also handles propagation
1601 // of type qualifiers from the array type into the element type if present
1602 // (C99 6.7.3p8).
1603 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1604 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001605
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001606 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001607
1608 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001609 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001610}
1611
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001612QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001613 QualType ElemTy = VAT->getElementType();
1614
1615 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1616 return getBaseElementType(VAT);
1617
1618 return ElemTy;
1619}
1620
Reid Spencer5f016e22007-07-11 17:01:13 +00001621/// getFloatingRank - Return a relative rank for floating point types.
1622/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001623static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001624 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001625 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001626
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001627 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001628 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001629 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001630 case BuiltinType::Float: return FloatRank;
1631 case BuiltinType::Double: return DoubleRank;
1632 case BuiltinType::LongDouble: return LongDoubleRank;
1633 }
1634}
1635
Steve Naroff716c7302007-08-27 01:41:48 +00001636/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1637/// point or a complex type (based on typeDomain/typeSize).
1638/// 'typeDomain' is a real floating point or complex type.
1639/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001640QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1641 QualType Domain) const {
1642 FloatingRank EltRank = getFloatingRank(Size);
1643 if (Domain->isComplexType()) {
1644 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001645 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001646 case FloatRank: return FloatComplexTy;
1647 case DoubleRank: return DoubleComplexTy;
1648 case LongDoubleRank: return LongDoubleComplexTy;
1649 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001650 }
Chris Lattner1361b112008-04-06 23:58:54 +00001651
1652 assert(Domain->isRealFloatingType() && "Unknown domain!");
1653 switch (EltRank) {
1654 default: assert(0 && "getFloatingRank(): illegal value for rank");
1655 case FloatRank: return FloatTy;
1656 case DoubleRank: return DoubleTy;
1657 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001658 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001659}
1660
Chris Lattner7cfeb082008-04-06 23:55:33 +00001661/// getFloatingTypeOrder - Compare the rank of the two specified floating
1662/// point types, ignoring the domain of the type (i.e. 'double' ==
1663/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1664/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001665int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1666 FloatingRank LHSR = getFloatingRank(LHS);
1667 FloatingRank RHSR = getFloatingRank(RHS);
1668
1669 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001670 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001671 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001672 return 1;
1673 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001674}
1675
Chris Lattnerf52ab252008-04-06 22:59:24 +00001676/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1677/// routine will assert if passed a built-in type that isn't an integer or enum,
1678/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001679unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001680 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001681 if (EnumType* ET = dyn_cast<EnumType>(T))
1682 T = ET->getDecl()->getIntegerType().getTypePtr();
1683
1684 // There are two things which impact the integer rank: the width, and
1685 // the ordering of builtins. The builtin ordering is encoded in the
1686 // bottom three bits; the width is encoded in the bits above that.
1687 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1688 return FWIT->getWidth() << 3;
1689 }
1690
Chris Lattnerf52ab252008-04-06 22:59:24 +00001691 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001692 default: assert(0 && "getIntegerRank(): not a built-in integer");
1693 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001694 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001695 case BuiltinType::Char_S:
1696 case BuiltinType::Char_U:
1697 case BuiltinType::SChar:
1698 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001699 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001700 case BuiltinType::Short:
1701 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001702 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001703 case BuiltinType::Int:
1704 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001705 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001706 case BuiltinType::Long:
1707 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001708 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001709 case BuiltinType::LongLong:
1710 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001711 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001712 }
1713}
1714
Chris Lattner7cfeb082008-04-06 23:55:33 +00001715/// getIntegerTypeOrder - Returns the highest ranked integer type:
1716/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1717/// LHS < RHS, return -1.
1718int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001719 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1720 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001721 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001722
Chris Lattnerf52ab252008-04-06 22:59:24 +00001723 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1724 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001725
Chris Lattner7cfeb082008-04-06 23:55:33 +00001726 unsigned LHSRank = getIntegerRank(LHSC);
1727 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001728
Chris Lattner7cfeb082008-04-06 23:55:33 +00001729 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1730 if (LHSRank == RHSRank) return 0;
1731 return LHSRank > RHSRank ? 1 : -1;
1732 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001733
Chris Lattner7cfeb082008-04-06 23:55:33 +00001734 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1735 if (LHSUnsigned) {
1736 // If the unsigned [LHS] type is larger, return it.
1737 if (LHSRank >= RHSRank)
1738 return 1;
1739
1740 // If the signed type can represent all values of the unsigned type, it
1741 // wins. Because we are dealing with 2's complement and types that are
1742 // powers of two larger than each other, this is always safe.
1743 return -1;
1744 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001745
Chris Lattner7cfeb082008-04-06 23:55:33 +00001746 // If the unsigned [RHS] type is larger, return it.
1747 if (RHSRank >= LHSRank)
1748 return -1;
1749
1750 // If the signed type can represent all values of the unsigned type, it
1751 // wins. Because we are dealing with 2's complement and types that are
1752 // powers of two larger than each other, this is always safe.
1753 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001754}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001755
1756// getCFConstantStringType - Return the type used for constant CFStrings.
1757QualType ASTContext::getCFConstantStringType() {
1758 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001759 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001760 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001761 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001762 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001763
1764 // const int *isa;
1765 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001766 // int flags;
1767 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001768 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001769 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001770 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001771 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001772
Anders Carlsson71993dd2007-08-17 05:31:46 +00001773 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001774 for (unsigned i = 0; i < 4; ++i) {
1775 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1776 SourceLocation(), 0,
1777 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001778 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001779 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001780 }
1781
1782 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001783 }
1784
1785 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001786}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001787
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001788QualType ASTContext::getObjCFastEnumerationStateType()
1789{
1790 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001791 ObjCFastEnumerationStateTypeDecl =
1792 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1793 &Idents.get("__objcFastEnumerationState"));
1794
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001795 QualType FieldTypes[] = {
1796 UnsignedLongTy,
1797 getPointerType(ObjCIdType),
1798 getPointerType(UnsignedLongTy),
1799 getConstantArrayType(UnsignedLongTy,
1800 llvm::APInt(32, 5), ArrayType::Normal, 0)
1801 };
1802
Douglas Gregor44b43212008-12-11 16:49:14 +00001803 for (size_t i = 0; i < 4; ++i) {
1804 FieldDecl *Field = FieldDecl::Create(*this,
1805 ObjCFastEnumerationStateTypeDecl,
1806 SourceLocation(), 0,
1807 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001808 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001809 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001810 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001811
Douglas Gregor44b43212008-12-11 16:49:14 +00001812 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001813 }
1814
1815 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1816}
1817
Anders Carlssone8c49532007-10-29 06:33:42 +00001818// This returns true if a type has been typedefed to BOOL:
1819// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001820static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001821 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00001822 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1823 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001824
1825 return false;
1826}
1827
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001828/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001829/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001830int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001831 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001832
1833 // Make all integer and enum types at least as large as an int
1834 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001835 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001836 // Treat arrays as pointers, since that's how they're passed in.
1837 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001838 sz = getTypeSize(VoidPtrTy);
1839 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001840}
1841
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001842/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001843/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001844void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001845 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001846 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001847 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001848 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001849 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001850 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001851 // Compute size of all parameters.
1852 // Start with computing size of a pointer in number of bytes.
1853 // FIXME: There might(should) be a better way of doing this computation!
1854 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001855 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001856 // The first two arguments (self and _cmd) are pointers; account for
1857 // their size.
1858 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00001859 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1860 E = Decl->param_end(); PI != E; ++PI) {
1861 QualType PType = (*PI)->getType();
1862 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001863 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001864 ParmOffset += sz;
1865 }
1866 S += llvm::utostr(ParmOffset);
1867 S += "@0:";
1868 S += llvm::utostr(PtrSize);
1869
1870 // Argument types.
1871 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00001872 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1873 E = Decl->param_end(); PI != E; ++PI) {
1874 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001875 QualType PType = PVDecl->getOriginalType();
1876 if (const ArrayType *AT =
1877 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1878 // Use array's original type only if it has known number of
1879 // elements.
1880 if (!dyn_cast<ConstantArrayType>(AT))
1881 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001882 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001883 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001884 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001885 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001886 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001887 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001888 }
1889}
1890
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001891/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001892/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001893/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1894/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001895/// Property attributes are stored as a comma-delimited C string. The simple
1896/// attributes readonly and bycopy are encoded as single characters. The
1897/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1898/// encoded as single characters, followed by an identifier. Property types
1899/// are also encoded as a parametrized attribute. The characters used to encode
1900/// these attributes are defined by the following enumeration:
1901/// @code
1902/// enum PropertyAttributes {
1903/// kPropertyReadOnly = 'R', // property is read-only.
1904/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1905/// kPropertyByref = '&', // property is a reference to the value last assigned
1906/// kPropertyDynamic = 'D', // property is dynamic
1907/// kPropertyGetter = 'G', // followed by getter selector name
1908/// kPropertySetter = 'S', // followed by setter selector name
1909/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1910/// kPropertyType = 't' // followed by old-style type encoding.
1911/// kPropertyWeak = 'W' // 'weak' property
1912/// kPropertyStrong = 'P' // property GC'able
1913/// kPropertyNonAtomic = 'N' // property non-atomic
1914/// };
1915/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001916void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1917 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001918 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001919 // Collect information from the property implementation decl(s).
1920 bool Dynamic = false;
1921 ObjCPropertyImplDecl *SynthesizePID = 0;
1922
1923 // FIXME: Duplicated code due to poor abstraction.
1924 if (Container) {
1925 if (const ObjCCategoryImplDecl *CID =
1926 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1927 for (ObjCCategoryImplDecl::propimpl_iterator
1928 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1929 ObjCPropertyImplDecl *PID = *i;
1930 if (PID->getPropertyDecl() == PD) {
1931 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1932 Dynamic = true;
1933 } else {
1934 SynthesizePID = PID;
1935 }
1936 }
1937 }
1938 } else {
Chris Lattner61710852008-10-05 17:34:18 +00001939 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001940 for (ObjCCategoryImplDecl::propimpl_iterator
1941 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1942 ObjCPropertyImplDecl *PID = *i;
1943 if (PID->getPropertyDecl() == PD) {
1944 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1945 Dynamic = true;
1946 } else {
1947 SynthesizePID = PID;
1948 }
1949 }
1950 }
1951 }
1952 }
1953
1954 // FIXME: This is not very efficient.
1955 S = "T";
1956
1957 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001958 // GCC has some special rules regarding encoding of properties which
1959 // closely resembles encoding of ivars.
1960 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
1961 true /* outermost type */,
1962 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001963
1964 if (PD->isReadOnly()) {
1965 S += ",R";
1966 } else {
1967 switch (PD->getSetterKind()) {
1968 case ObjCPropertyDecl::Assign: break;
1969 case ObjCPropertyDecl::Copy: S += ",C"; break;
1970 case ObjCPropertyDecl::Retain: S += ",&"; break;
1971 }
1972 }
1973
1974 // It really isn't clear at all what this means, since properties
1975 // are "dynamic by default".
1976 if (Dynamic)
1977 S += ",D";
1978
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001979 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
1980 S += ",N";
1981
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001982 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1983 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001984 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001985 }
1986
1987 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1988 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001989 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001990 }
1991
1992 if (SynthesizePID) {
1993 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1994 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00001995 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001996 }
1997
1998 // FIXME: OBJCGC: weak & strong
1999}
2000
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002001/// getLegacyIntegralTypeEncoding -
2002/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002003/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002004/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2005///
2006void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2007 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2008 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002009 if (BT->getKind() == BuiltinType::ULong &&
2010 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002011 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002012 else
2013 if (BT->getKind() == BuiltinType::Long &&
2014 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002015 PointeeTy = IntTy;
2016 }
2017 }
2018}
2019
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002020void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002021 FieldDecl *Field) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002022 // We follow the behavior of gcc, expanding structures which are
2023 // directly pointed to, and expanding embedded structures. Note that
2024 // these rules are sufficient to prevent recursive encoding of the
2025 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002026 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2027 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002028}
2029
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002030static void EncodeBitField(const ASTContext *Context, std::string& S,
2031 FieldDecl *FD) {
2032 const Expr *E = FD->getBitWidth();
2033 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2034 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2035 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2036 S += 'b';
2037 S += llvm::utostr(N);
2038}
2039
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002040void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2041 bool ExpandPointedToStructures,
2042 bool ExpandStructures,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002043 FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002044 bool OutermostType,
2045 bool EncodingProperty) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00002046 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002047 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002048 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002049 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002050 else {
2051 char encoding;
2052 switch (BT->getKind()) {
2053 default: assert(0 && "Unhandled builtin type kind");
2054 case BuiltinType::Void: encoding = 'v'; break;
2055 case BuiltinType::Bool: encoding = 'B'; break;
2056 case BuiltinType::Char_U:
2057 case BuiltinType::UChar: encoding = 'C'; break;
2058 case BuiltinType::UShort: encoding = 'S'; break;
2059 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002060 case BuiltinType::ULong:
2061 encoding =
2062 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2063 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002064 case BuiltinType::ULongLong: encoding = 'Q'; break;
2065 case BuiltinType::Char_S:
2066 case BuiltinType::SChar: encoding = 'c'; break;
2067 case BuiltinType::Short: encoding = 's'; break;
2068 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002069 case BuiltinType::Long:
2070 encoding =
2071 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2072 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002073 case BuiltinType::LongLong: encoding = 'q'; break;
2074 case BuiltinType::Float: encoding = 'f'; break;
2075 case BuiltinType::Double: encoding = 'd'; break;
2076 case BuiltinType::LongDouble: encoding = 'd'; break;
2077 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002078
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002079 S += encoding;
2080 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002081 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002082 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002083 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2084 ExpandPointedToStructures,
2085 ExpandStructures, FD);
2086 if (FD || EncodingProperty) {
2087 // Note that we do extended encoding of protocol qualifer list
2088 // Only when doing ivar or property encoding.
2089 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2090 S += '"';
2091 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2092 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2093 S += '<';
2094 S += Proto->getNameAsString();
2095 S += '>';
2096 }
2097 S += '"';
2098 }
2099 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002100 }
2101 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002102 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002103 bool isReadOnly = false;
2104 // For historical/compatibility reasons, the read-only qualifier of the
2105 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2106 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2107 // Also, do not emit the 'r' for anything but the outermost type!
2108 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2109 if (OutermostType && T.isConstQualified()) {
2110 isReadOnly = true;
2111 S += 'r';
2112 }
2113 }
2114 else if (OutermostType) {
2115 QualType P = PointeeTy;
2116 while (P->getAsPointerType())
2117 P = P->getAsPointerType()->getPointeeType();
2118 if (P.isConstQualified()) {
2119 isReadOnly = true;
2120 S += 'r';
2121 }
2122 }
2123 if (isReadOnly) {
2124 // Another legacy compatibility encoding. Some ObjC qualifier and type
2125 // combinations need to be rearranged.
2126 // Rewrite "in const" from "nr" to "rn"
2127 const char * s = S.c_str();
2128 int len = S.length();
2129 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2130 std::string replace = "rn";
2131 S.replace(S.end()-2, S.end(), replace);
2132 }
2133 }
Steve Naroff389bf462009-02-12 17:52:19 +00002134 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002135 S += '@';
2136 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002137 }
2138 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002139 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002140 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002141 // Another historical/compatibility reason.
2142 // We encode the underlying type which comes out as
2143 // {...};
2144 S += '^';
2145 getObjCEncodingForTypeImpl(PointeeTy, S,
2146 false, ExpandPointedToStructures,
2147 NULL);
2148 return;
2149 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002150 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002151 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002152 const ObjCInterfaceType *OIT =
2153 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002154 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002155 S += '"';
2156 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002157 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2158 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2159 S += '<';
2160 S += Proto->getNameAsString();
2161 S += '>';
2162 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002163 S += '"';
2164 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002165 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002166 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002167 S += '#';
2168 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002169 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002170 S += ':';
2171 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002172 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002173
2174 if (PointeeTy->isCharType()) {
2175 // char pointer types should be encoded as '*' unless it is a
2176 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002177 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002178 S += '*';
2179 return;
2180 }
2181 }
2182
2183 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002184 getLegacyIntegralTypeEncoding(PointeeTy);
2185
2186 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002187 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002188 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002189 } else if (const ArrayType *AT =
2190 // Ignore type qualifiers etc.
2191 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002192 if (isa<IncompleteArrayType>(AT)) {
2193 // Incomplete arrays are encoded as a pointer to the array element.
2194 S += '^';
2195
2196 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2197 false, ExpandStructures, FD);
2198 } else {
2199 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002200
Anders Carlsson559a8332009-02-22 01:38:57 +00002201 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2202 S += llvm::utostr(CAT->getSize().getZExtValue());
2203 else {
2204 //Variable length arrays are encoded as a regular array with 0 elements.
2205 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2206 S += '0';
2207 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002208
Anders Carlsson559a8332009-02-22 01:38:57 +00002209 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2210 false, ExpandStructures, FD);
2211 S += ']';
2212 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002213 } else if (T->getAsFunctionType()) {
2214 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002215 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002216 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002217 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002218 // Anonymous structures print as '?'
2219 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2220 S += II->getName();
2221 } else {
2222 S += '?';
2223 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002224 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002225 S += '=';
Douglas Gregor44b43212008-12-11 16:49:14 +00002226 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2227 FieldEnd = RDecl->field_end();
2228 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002229 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002230 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002231 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002232 S += '"';
2233 }
2234
2235 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002236 if (Field->isBitField()) {
2237 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2238 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002239 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002240 QualType qt = Field->getType();
2241 getLegacyIntegralTypeEncoding(qt);
2242 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002243 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002244 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002245 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002246 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002247 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002248 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002249 if (FD && FD->isBitField())
2250 EncodeBitField(this, S, FD);
2251 else
2252 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002253 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002254 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002255 } else if (T->isObjCInterfaceType()) {
2256 // @encode(class_name)
2257 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2258 S += '{';
2259 const IdentifierInfo *II = OI->getIdentifier();
2260 S += II->getName();
2261 S += '=';
2262 std::vector<FieldDecl*> RecFields;
2263 CollectObjCIvars(OI, RecFields);
2264 for (unsigned int i = 0; i != RecFields.size(); i++) {
2265 if (RecFields[i]->isBitField())
2266 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2267 RecFields[i]);
2268 else
2269 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2270 FD);
2271 }
2272 S += '}';
2273 }
2274 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002275 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002276}
2277
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002278void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002279 std::string& S) const {
2280 if (QT & Decl::OBJC_TQ_In)
2281 S += 'n';
2282 if (QT & Decl::OBJC_TQ_Inout)
2283 S += 'N';
2284 if (QT & Decl::OBJC_TQ_Out)
2285 S += 'o';
2286 if (QT & Decl::OBJC_TQ_Bycopy)
2287 S += 'O';
2288 if (QT & Decl::OBJC_TQ_Byref)
2289 S += 'R';
2290 if (QT & Decl::OBJC_TQ_Oneway)
2291 S += 'V';
2292}
2293
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002294void ASTContext::setBuiltinVaListType(QualType T)
2295{
2296 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2297
2298 BuiltinVaListType = T;
2299}
2300
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002301void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002302{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002303 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002304
2305 // typedef struct objc_object *id;
2306 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002307 // User error - caller will issue diagnostics.
2308 if (!ptr)
2309 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002310 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002311 // User error - caller will issue diagnostics.
2312 if (!rec)
2313 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002314 IdStructType = rec;
2315}
2316
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002317void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002318{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002319 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002320
2321 // typedef struct objc_selector *SEL;
2322 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002323 if (!ptr)
2324 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002325 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002326 if (!rec)
2327 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002328 SelStructType = rec;
2329}
2330
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002331void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002332{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002333 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002334}
2335
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002336void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002337{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002338 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002339
2340 // typedef struct objc_class *Class;
2341 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2342 assert(ptr && "'Class' incorrectly typed");
2343 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2344 assert(rec && "'Class' incorrectly typed");
2345 ClassStructType = rec;
2346}
2347
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002348void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2349 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002350 "'NSConstantString' type already set!");
2351
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002352 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002353}
2354
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002355/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002356/// TargetInfo, produce the corresponding type. The unsigned @p Type
2357/// is actually a value of type @c TargetInfo::IntType.
2358QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002359 switch (Type) {
2360 case TargetInfo::NoInt: return QualType();
2361 case TargetInfo::SignedShort: return ShortTy;
2362 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2363 case TargetInfo::SignedInt: return IntTy;
2364 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2365 case TargetInfo::SignedLong: return LongTy;
2366 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2367 case TargetInfo::SignedLongLong: return LongLongTy;
2368 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2369 }
2370
2371 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002372 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002373}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002374
2375//===----------------------------------------------------------------------===//
2376// Type Predicates.
2377//===----------------------------------------------------------------------===//
2378
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002379/// isObjCNSObjectType - Return true if this is an NSObject object using
2380/// NSObject attribute on a c-style pointer type.
2381/// FIXME - Make it work directly on types.
2382///
2383bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2384 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2385 if (TypedefDecl *TD = TDT->getDecl())
2386 if (TD->getAttr<ObjCNSObjectAttr>())
2387 return true;
2388 }
2389 return false;
2390}
2391
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002392/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2393/// to an object type. This includes "id" and "Class" (two 'special' pointers
2394/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2395/// ID type).
2396bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002397 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002398 return true;
2399
Steve Naroff6ae98502008-10-21 18:24:04 +00002400 // Blocks are objects.
2401 if (Ty->isBlockPointerType())
2402 return true;
2403
2404 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002405 if (!Ty->isPointerType())
2406 return false;
2407
2408 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2409 // pointer types. This looks for the typedef specifically, not for the
2410 // underlying type.
2411 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2412 return true;
2413
2414 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002415 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2416 return true;
2417
2418 // If is has NSObject attribute, OK as well.
2419 return isObjCNSObjectType(Ty);
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002420}
2421
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002422/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2423/// garbage collection attribute.
2424///
2425QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002426 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002427 if (getLangOptions().ObjC1 &&
2428 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002429 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002430 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002431 // (or pointers to them) be treated as though they were declared
2432 // as __strong.
2433 if (GCAttrs == QualType::GCNone) {
2434 if (isObjCObjectPointerType(Ty))
2435 GCAttrs = QualType::Strong;
2436 else if (Ty->isPointerType())
2437 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2438 }
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002439 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002440 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002441}
2442
Chris Lattner6ac46a42008-04-07 06:51:04 +00002443//===----------------------------------------------------------------------===//
2444// Type Compatibility Testing
2445//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002446
Steve Naroff1c7d0672008-09-04 15:10:53 +00002447/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002448/// block types. Types must be strictly compatible here. For example,
2449/// C unfortunately doesn't produce an error for the following:
2450///
2451/// int (*emptyArgFunc)();
2452/// int (*intArgList)(int) = emptyArgFunc;
2453///
2454/// For blocks, we will produce an error for the following (similar to C++):
2455///
2456/// int (^emptyArgBlock)();
2457/// int (^intArgBlock)(int) = emptyArgBlock;
2458///
2459/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2460///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002461bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002462 const FunctionType *lbase = lhs->getAsFunctionType();
2463 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002464 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2465 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Steve Naroffc0febd52008-12-10 17:49:55 +00002466 if (lproto && rproto)
2467 return !mergeTypes(lhs, rhs).isNull();
2468 return false;
Steve Naroff1c7d0672008-09-04 15:10:53 +00002469}
2470
Chris Lattner6ac46a42008-04-07 06:51:04 +00002471/// areCompatVectorTypes - Return true if the two specified vector types are
2472/// compatible.
2473static bool areCompatVectorTypes(const VectorType *LHS,
2474 const VectorType *RHS) {
2475 assert(LHS->isCanonical() && RHS->isCanonical());
2476 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002477 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002478}
2479
Eli Friedman3d815e72008-08-22 00:56:42 +00002480/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002481/// compatible for assignment from RHS to LHS. This handles validation of any
2482/// protocol qualifiers on the LHS or RHS.
2483///
Eli Friedman3d815e72008-08-22 00:56:42 +00002484bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2485 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002486 // Verify that the base decls are compatible: the RHS must be a subclass of
2487 // the LHS.
2488 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2489 return false;
2490
2491 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2492 // protocol qualified at all, then we are good.
2493 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2494 return true;
2495
2496 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2497 // isn't a superset.
2498 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2499 return true; // FIXME: should return false!
2500
2501 // Finally, we must have two protocol-qualified interfaces.
2502 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2503 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2504 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2505 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2506 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2507 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2508
2509 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2510 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2511 // LHS in a single parallel scan until we run out of elements in LHS.
2512 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2513 ObjCProtocolDecl *LHSProto = *LHSPI;
2514
2515 while (RHSPI != RHSPE) {
2516 ObjCProtocolDecl *RHSProto = *RHSPI++;
2517 // If the RHS has a protocol that the LHS doesn't, ignore it.
2518 if (RHSProto != LHSProto)
2519 continue;
2520
2521 // Otherwise, the RHS does have this element.
2522 ++LHSPI;
2523 if (LHSPI == LHSPE)
2524 return true; // All protocols in LHS exist in RHS.
2525
2526 LHSProto = *LHSPI;
2527 }
2528
2529 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2530 return false;
2531}
2532
Steve Naroff389bf462009-02-12 17:52:19 +00002533bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2534 // get the "pointed to" types
2535 const PointerType *LHSPT = LHS->getAsPointerType();
2536 const PointerType *RHSPT = RHS->getAsPointerType();
2537
2538 if (!LHSPT || !RHSPT)
2539 return false;
2540
2541 QualType lhptee = LHSPT->getPointeeType();
2542 QualType rhptee = RHSPT->getPointeeType();
2543 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2544 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2545 // ID acts sort of like void* for ObjC interfaces
2546 if (LHSIface && isObjCIdStructType(rhptee))
2547 return true;
2548 if (RHSIface && isObjCIdStructType(lhptee))
2549 return true;
2550 if (!LHSIface || !RHSIface)
2551 return false;
2552 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2553 canAssignObjCInterfaces(RHSIface, LHSIface);
2554}
2555
Steve Naroffec0550f2007-10-15 20:41:53 +00002556/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2557/// both shall have the identically qualified version of a compatible type.
2558/// C99 6.2.7p1: Two types have compatible types if their types are the
2559/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002560bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2561 return !mergeTypes(LHS, RHS).isNull();
2562}
2563
2564QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2565 const FunctionType *lbase = lhs->getAsFunctionType();
2566 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002567 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2568 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002569 bool allLTypes = true;
2570 bool allRTypes = true;
2571
2572 // Check return type
2573 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2574 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002575 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2576 allLTypes = false;
2577 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2578 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002579
2580 if (lproto && rproto) { // two C99 style function prototypes
2581 unsigned lproto_nargs = lproto->getNumArgs();
2582 unsigned rproto_nargs = rproto->getNumArgs();
2583
2584 // Compatible functions must have the same number of arguments
2585 if (lproto_nargs != rproto_nargs)
2586 return QualType();
2587
2588 // Variadic and non-variadic functions aren't compatible
2589 if (lproto->isVariadic() != rproto->isVariadic())
2590 return QualType();
2591
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002592 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2593 return QualType();
2594
Eli Friedman3d815e72008-08-22 00:56:42 +00002595 // Check argument compatibility
2596 llvm::SmallVector<QualType, 10> types;
2597 for (unsigned i = 0; i < lproto_nargs; i++) {
2598 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2599 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2600 QualType argtype = mergeTypes(largtype, rargtype);
2601 if (argtype.isNull()) return QualType();
2602 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002603 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2604 allLTypes = false;
2605 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2606 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002607 }
2608 if (allLTypes) return lhs;
2609 if (allRTypes) return rhs;
2610 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002611 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002612 }
2613
2614 if (lproto) allRTypes = false;
2615 if (rproto) allLTypes = false;
2616
Douglas Gregor72564e72009-02-26 23:50:07 +00002617 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002618 if (proto) {
2619 if (proto->isVariadic()) return QualType();
2620 // Check that the types are compatible with the types that
2621 // would result from default argument promotions (C99 6.7.5.3p15).
2622 // The only types actually affected are promotable integer
2623 // types and floats, which would be passed as a different
2624 // type depending on whether the prototype is visible.
2625 unsigned proto_nargs = proto->getNumArgs();
2626 for (unsigned i = 0; i < proto_nargs; ++i) {
2627 QualType argTy = proto->getArgType(i);
2628 if (argTy->isPromotableIntegerType() ||
2629 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2630 return QualType();
2631 }
2632
2633 if (allLTypes) return lhs;
2634 if (allRTypes) return rhs;
2635 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002636 proto->getNumArgs(), lproto->isVariadic(),
2637 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002638 }
2639
2640 if (allLTypes) return lhs;
2641 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002642 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002643}
2644
2645QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002646 // C++ [expr]: If an expression initially has the type "reference to T", the
2647 // type is adjusted to "T" prior to any further analysis, the expression
2648 // designates the object or function denoted by the reference, and the
2649 // expression is an lvalue.
Eli Friedman3d815e72008-08-22 00:56:42 +00002650 // FIXME: C++ shouldn't be going through here! The rules are different
2651 // enough that they should be handled separately.
2652 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002653 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002654 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002655 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002656
Eli Friedman3d815e72008-08-22 00:56:42 +00002657 QualType LHSCan = getCanonicalType(LHS),
2658 RHSCan = getCanonicalType(RHS);
2659
2660 // If two types are identical, they are compatible.
2661 if (LHSCan == RHSCan)
2662 return LHS;
2663
2664 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002665 // Note that we handle extended qualifiers later, in the
2666 // case for ExtQualType.
2667 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00002668 return QualType();
2669
2670 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2671 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2672
Chris Lattner1adb8832008-01-14 05:45:46 +00002673 // We want to consider the two function types to be the same for these
2674 // comparisons, just force one to the other.
2675 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2676 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002677
2678 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002679 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2680 LHSClass = Type::ConstantArray;
2681 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2682 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002683
Nate Begeman213541a2008-04-18 23:10:10 +00002684 // Canonicalize ExtVector -> Vector.
2685 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2686 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002687
Chris Lattnerb0489812008-04-07 06:38:24 +00002688 // Consider qualified interfaces and interfaces the same.
2689 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2690 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002691
Chris Lattnera36a61f2008-04-07 05:43:21 +00002692 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002693 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002694 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2695 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2696
2697 // ID acts sort of like void* for ObjC interfaces
2698 if (LHSIface && isObjCIdStructType(RHS))
2699 return LHS;
2700 if (RHSIface && isObjCIdStructType(LHS))
2701 return RHS;
2702
Steve Naroffbc76dd02008-12-10 22:14:21 +00002703 // ID is compatible with all qualified id types.
2704 if (LHS->isObjCQualifiedIdType()) {
2705 if (const PointerType *PT = RHS->getAsPointerType()) {
2706 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002707 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002708 return LHS;
2709 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2710 // Unfortunately, this API is part of Sema (which we don't have access
2711 // to. Need to refactor. The following check is insufficient, since we
2712 // need to make sure the class implements the protocol.
2713 if (pType->isObjCInterfaceType())
2714 return LHS;
2715 }
2716 }
2717 if (RHS->isObjCQualifiedIdType()) {
2718 if (const PointerType *PT = LHS->getAsPointerType()) {
2719 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002720 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002721 return RHS;
2722 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2723 // Unfortunately, this API is part of Sema (which we don't have access
2724 // to. Need to refactor. The following check is insufficient, since we
2725 // need to make sure the class implements the protocol.
2726 if (pType->isObjCInterfaceType())
2727 return RHS;
2728 }
2729 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002730 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2731 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002732 if (const EnumType* ETy = LHS->getAsEnumType()) {
2733 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2734 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002735 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002736 if (const EnumType* ETy = RHS->getAsEnumType()) {
2737 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2738 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002739 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002740
Eli Friedman3d815e72008-08-22 00:56:42 +00002741 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002742 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002743
Steve Naroff4a746782008-01-09 22:43:08 +00002744 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002745 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00002746#define TYPE(Class, Base)
2747#define ABSTRACT_TYPE(Class, Base)
2748#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2749#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2750#include "clang/AST/TypeNodes.def"
2751 assert(false && "Non-canonical and dependent types shouldn't get here");
2752 return QualType();
2753
2754 case Type::Reference:
2755 case Type::MemberPointer:
2756 assert(false && "C++ should never be in mergeTypes");
2757 return QualType();
2758
2759 case Type::IncompleteArray:
2760 case Type::VariableArray:
2761 case Type::FunctionProto:
2762 case Type::ExtVector:
2763 case Type::ObjCQualifiedInterface:
2764 assert(false && "Types are eliminated above");
2765 return QualType();
2766
Chris Lattner1adb8832008-01-14 05:45:46 +00002767 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002768 {
2769 // Merge two pointer types, while trying to preserve typedef info
2770 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2771 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2772 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2773 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002774 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2775 return LHS;
2776 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2777 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002778 return getPointerType(ResultType);
2779 }
Steve Naroffc0febd52008-12-10 17:49:55 +00002780 case Type::BlockPointer:
2781 {
2782 // Merge two block pointer types, while trying to preserve typedef info
2783 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2784 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2785 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2786 if (ResultType.isNull()) return QualType();
2787 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2788 return LHS;
2789 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2790 return RHS;
2791 return getBlockPointerType(ResultType);
2792 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002793 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002794 {
2795 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2796 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2797 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2798 return QualType();
2799
2800 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2801 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2802 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2803 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002804 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2805 return LHS;
2806 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2807 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002808 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2809 ArrayType::ArraySizeModifier(), 0);
2810 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2811 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002812 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2813 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00002814 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2815 return LHS;
2816 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2817 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002818 if (LVAT) {
2819 // FIXME: This isn't correct! But tricky to implement because
2820 // the array's size has to be the size of LHS, but the type
2821 // has to be different.
2822 return LHS;
2823 }
2824 if (RVAT) {
2825 // FIXME: This isn't correct! But tricky to implement because
2826 // the array's size has to be the size of RHS, but the type
2827 // has to be different.
2828 return RHS;
2829 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002830 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2831 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00002832 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002833 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002834 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002835 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00002836 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00002837 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00002838 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00002839 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
2840 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002841 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002842 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002843 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002844 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00002845 case Type::Complex:
2846 // Distinct complex types are incompatible.
2847 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002848 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002849 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00002850 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2851 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00002852 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00002853 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002854 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002855 // FIXME: This should be type compatibility, e.g. whether
2856 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00002857 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2858 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2859 if (LHSIface && RHSIface &&
2860 canAssignObjCInterfaces(LHSIface, RHSIface))
2861 return LHS;
2862
Eli Friedman3d815e72008-08-22 00:56:42 +00002863 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00002864 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00002865 case Type::ObjCQualifiedId:
2866 // Distinct qualified id's are not compatible.
2867 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002868 case Type::FixedWidthInt:
2869 // Distinct fixed-width integers are not compatible.
2870 return QualType();
2871 case Type::ObjCQualifiedClass:
2872 // Distinct qualified classes are not compatible.
2873 return QualType();
2874 case Type::ExtQual:
2875 // FIXME: ExtQual types can be compatible even if they're not
2876 // identical!
2877 return QualType();
2878 // First attempt at an implementation, but I'm not really sure it's
2879 // right...
2880#if 0
2881 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
2882 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
2883 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
2884 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
2885 return QualType();
2886 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
2887 LHSBase = QualType(LQual->getBaseType(), 0);
2888 RHSBase = QualType(RQual->getBaseType(), 0);
2889 ResultType = mergeTypes(LHSBase, RHSBase);
2890 if (ResultType.isNull()) return QualType();
2891 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
2892 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
2893 return LHS;
2894 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
2895 return RHS;
2896 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
2897 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
2898 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
2899 return ResultType;
2900#endif
Steve Naroffec0550f2007-10-15 20:41:53 +00002901 }
Douglas Gregor72564e72009-02-26 23:50:07 +00002902
2903 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002904}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002905
Chris Lattner5426bf62008-04-07 07:01:58 +00002906//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00002907// Integer Predicates
2908//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00002909
Eli Friedmanad74a752008-06-28 06:23:08 +00002910unsigned ASTContext::getIntWidth(QualType T) {
2911 if (T == BoolTy)
2912 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002913 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
2914 return FWIT->getWidth();
2915 }
2916 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00002917 return (unsigned)getTypeSize(T);
2918}
2919
2920QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2921 assert(T->isSignedIntegerType() && "Unexpected type");
2922 if (const EnumType* ETy = T->getAsEnumType())
2923 T = ETy->getDecl()->getIntegerType();
2924 const BuiltinType* BTy = T->getAsBuiltinType();
2925 assert (BTy && "Unexpected signed integer type");
2926 switch (BTy->getKind()) {
2927 case BuiltinType::Char_S:
2928 case BuiltinType::SChar:
2929 return UnsignedCharTy;
2930 case BuiltinType::Short:
2931 return UnsignedShortTy;
2932 case BuiltinType::Int:
2933 return UnsignedIntTy;
2934 case BuiltinType::Long:
2935 return UnsignedLongTy;
2936 case BuiltinType::LongLong:
2937 return UnsignedLongLongTy;
2938 default:
2939 assert(0 && "Unexpected signed integer type");
2940 return QualType();
2941 }
2942}
2943
2944
2945//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00002946// Serialization Support
2947//===----------------------------------------------------------------------===//
2948
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002949/// Emit - Serialize an ASTContext object to Bitcode.
2950void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002951 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00002952 S.EmitRef(SourceMgr);
2953 S.EmitRef(Target);
2954 S.EmitRef(Idents);
2955 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002956
Ted Kremenekfee04522007-10-31 22:44:07 +00002957 // Emit the size of the type vector so that we can reserve that size
2958 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00002959 S.EmitInt(Types.size());
2960
Ted Kremenek03ed4402007-11-13 22:02:55 +00002961 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2962 I!=E;++I)
2963 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00002964
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002965 S.EmitOwnedPtr(TUDecl);
2966
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002967 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002968}
2969
Ted Kremenek0f84c002007-11-13 00:25:37 +00002970ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002971
2972 // Read the language options.
2973 LangOptions LOpts;
2974 LOpts.Read(D);
2975
Ted Kremenekfee04522007-10-31 22:44:07 +00002976 SourceManager &SM = D.ReadRef<SourceManager>();
2977 TargetInfo &t = D.ReadRef<TargetInfo>();
2978 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2979 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00002980
Ted Kremenekfee04522007-10-31 22:44:07 +00002981 unsigned size_reserve = D.ReadInt();
2982
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002983 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2984 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00002985
Ted Kremenek03ed4402007-11-13 22:02:55 +00002986 for (unsigned i = 0; i < size_reserve; ++i)
2987 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00002988
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002989 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2990
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002991 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00002992
2993 return A;
2994}