blob: e4347d53dbaf07e6b00680e62998bb81904b5260 [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:
451 case Type::CXXRecord:
452 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000453 const TagType *TT = cast<TagType>(T);
454
455 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000456 Width = 1;
457 Align = 1;
458 break;
459 }
460
Daniel Dunbar1d751182008-11-08 05:48:37 +0000461 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000462 return getTypeInfo(ET->getDecl()->getIntegerType());
463
Daniel Dunbar1d751182008-11-08 05:48:37 +0000464 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000465 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
466 Width = Layout.getSize();
467 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000468 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000469 }
Chris Lattner71763312008-04-06 22:05:18 +0000470 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000471
Chris Lattner464175b2007-07-18 17:52:12 +0000472 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000473 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000474}
475
Chris Lattner34ebde42009-01-27 18:08:34 +0000476/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
477/// type for the current target in bits. This can be different than the ABI
478/// alignment in cases where it is beneficial for performance to overalign
479/// a data type.
480unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
481 unsigned ABIAlign = getTypeAlign(T);
482
483 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000484 if (T->isSpecificBuiltinType(BuiltinType::Double))
485 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000486
487 return ABIAlign;
488}
489
490
Devang Patel8b277042008-06-04 21:22:16 +0000491/// LayoutField - Field layout.
492void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000493 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000494 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000495 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000496 uint64_t FieldOffset = IsUnion ? 0 : Size;
497 uint64_t FieldSize;
498 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000499
500 // FIXME: Should this override struct packing? Probably we want to
501 // take the minimum?
502 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
503 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000504
505 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
506 // TODO: Need to check this algorithm on other targets!
507 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000508 FieldSize =
509 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000510
511 std::pair<uint64_t, unsigned> FieldInfo =
512 Context.getTypeInfo(FD->getType());
513 uint64_t TypeSize = FieldInfo.first;
514
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000515 // Determine the alignment of this bitfield. The packing
516 // attributes define a maximum and the alignment attribute defines
517 // a minimum.
518 // FIXME: What is the right behavior when the specified alignment
519 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000520 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000521 if (FieldPacking)
522 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000523 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
524 FieldAlign = std::max(FieldAlign, AA->getAlignment());
525
526 // Check if we need to add padding to give the field the correct
527 // alignment.
528 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
529 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
530
531 // Padding members don't affect overall alignment
532 if (!FD->getIdentifier())
533 FieldAlign = 1;
534 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000535 if (FD->getType()->isIncompleteArrayType()) {
536 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000537 // query getTypeInfo about these, so we figure it out here.
538 // Flexible array members don't have any size, but they
539 // have to be aligned appropriately for their element type.
540 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000541 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000542 FieldAlign = Context.getTypeAlign(ATy->getElementType());
543 } else {
544 std::pair<uint64_t, unsigned> FieldInfo =
545 Context.getTypeInfo(FD->getType());
546 FieldSize = FieldInfo.first;
547 FieldAlign = FieldInfo.second;
548 }
549
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000550 // Determine the alignment of this bitfield. The packing
551 // attributes define a maximum and the alignment attribute defines
552 // a minimum. Additionally, the packing alignment must be at least
553 // a byte for non-bitfields.
554 //
555 // FIXME: What is the right behavior when the specified alignment
556 // is smaller than the specified packing?
557 if (FieldPacking)
558 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000559 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
560 FieldAlign = std::max(FieldAlign, AA->getAlignment());
561
562 // Round up the current record size to the field's alignment boundary.
563 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
564 }
565
566 // Place this field at the current location.
567 FieldOffsets[FieldNo] = FieldOffset;
568
569 // Reserve space for this field.
570 if (IsUnion) {
571 Size = std::max(Size, FieldSize);
572 } else {
573 Size = FieldOffset + FieldSize;
574 }
575
576 // Remember max struct/class alignment.
577 Alignment = std::max(Alignment, FieldAlign);
578}
579
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000580static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
581 std::vector<FieldDecl*> &Fields) {
582 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
583 if (SuperClass)
584 CollectObjCIvars(SuperClass, Fields);
585 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
586 E = OI->ivar_end(); I != E; ++I) {
587 ObjCIvarDecl *IVDecl = (*I);
588 if (!IVDecl->isInvalidDecl())
589 Fields.push_back(cast<FieldDecl>(IVDecl));
590 }
591}
592
593/// addRecordToClass - produces record info. for the class for its
594/// ivars and all those inherited.
595///
596const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
597{
598 const RecordDecl *&RD = ASTRecordForInterface[D];
599 if (RD)
600 return RD;
601 std::vector<FieldDecl*> RecFields;
602 CollectObjCIvars(D, RecFields);
603 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
604 D->getLocation(),
605 D->getIdentifier());
606 /// FIXME! Can do collection of ivars and adding to the record while
607 /// doing it.
608 for (unsigned int i = 0; i != RecFields.size(); i++) {
609 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
610 RecFields[i]->getLocation(),
611 RecFields[i]->getIdentifier(),
612 RecFields[i]->getType(),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000613 RecFields[i]->getBitWidth(), false);
Douglas Gregor482b77d2009-01-12 23:27:07 +0000614 NewRD->addDecl(Field);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000615 }
616 NewRD->completeDefinition(*this);
617 RD = NewRD;
618 return RD;
619}
Devang Patel44a3dde2008-06-04 21:54:36 +0000620
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000621/// setFieldDecl - maps a field for the given Ivar reference node.
622//
623void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
624 const ObjCIvarDecl *Ivar,
625 const ObjCIvarRefExpr *MRef) {
626 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
627 lookupFieldDeclForIvar(*this, Ivar);
628 ASTFieldForIvarRef[MRef] = FD;
629}
630
Chris Lattner61710852008-10-05 17:34:18 +0000631/// getASTObjcInterfaceLayout - Get or compute information about the layout of
632/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000633/// position information.
634const ASTRecordLayout &
635ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
636 // Look up this layout, if already laid out, return what we have.
637 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
638 if (Entry) return *Entry;
639
640 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
641 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000642 ASTRecordLayout *NewEntry = NULL;
643 unsigned FieldCount = D->ivar_size();
644 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
645 FieldCount++;
646 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
647 unsigned Alignment = SL.getAlignment();
648 uint64_t Size = SL.getSize();
649 NewEntry = new ASTRecordLayout(Size, Alignment);
650 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000651 // Super class is at the beginning of the layout.
652 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000653 } else {
654 NewEntry = new ASTRecordLayout();
655 NewEntry->InitializeLayout(FieldCount);
656 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000657 Entry = NewEntry;
658
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000659 unsigned StructPacking = 0;
660 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
661 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000662
663 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
664 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
665 AA->getAlignment()));
666
667 // Layout each ivar sequentially.
668 unsigned i = 0;
669 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
670 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
671 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000672 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000673 }
674
675 // Finally, round the size of the total struct up to the alignment of the
676 // struct itself.
677 NewEntry->FinalizeLayout();
678 return *NewEntry;
679}
680
Devang Patel88a981b2007-11-01 19:11:01 +0000681/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000682/// specified record (struct/union/class), which indicates its size and field
683/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000684const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000685 D = D->getDefinition(*this);
686 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000687
Chris Lattner464175b2007-07-18 17:52:12 +0000688 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000689 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000690 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000691
Devang Patel88a981b2007-11-01 19:11:01 +0000692 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
693 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
694 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000695 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000696
Douglas Gregore267ff32008-12-11 20:41:00 +0000697 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor44b43212008-12-11 16:49:14 +0000698 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000699 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000700
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000701 unsigned StructPacking = 0;
702 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
703 StructPacking = PA->getAlignment();
704
Eli Friedman4bd998b2008-05-30 09:31:38 +0000705 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000706 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
707 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000708
Eli Friedman4bd998b2008-05-30 09:31:38 +0000709 // Layout each field, for now, just sequentially, respecting alignment. In
710 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000711 unsigned FieldIdx = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000712 for (RecordDecl::field_iterator Field = D->field_begin(),
713 FieldEnd = D->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000714 Field != FieldEnd; (void)++Field, ++FieldIdx)
715 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000716
717 // Finally, round the size of the total struct up to the alignment of the
718 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000719 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000720 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000721}
722
Chris Lattnera7674d82007-07-13 22:13:22 +0000723//===----------------------------------------------------------------------===//
724// Type creation/memoization methods
725//===----------------------------------------------------------------------===//
726
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000727QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000728 QualType CanT = getCanonicalType(T);
729 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000730 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000731
732 // If we are composing extended qualifiers together, merge together into one
733 // ExtQualType node.
734 unsigned CVRQuals = T.getCVRQualifiers();
735 QualType::GCAttrTypes GCAttr = QualType::GCNone;
736 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000737
Chris Lattnerb7d25532009-02-18 22:53:11 +0000738 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
739 // If this type already has an address space specified, it cannot get
740 // another one.
741 assert(EQT->getAddressSpace() == 0 &&
742 "Type cannot be in multiple addr spaces!");
743 GCAttr = EQT->getObjCGCAttr();
744 TypeNode = EQT->getBaseType();
745 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000746
Chris Lattnerb7d25532009-02-18 22:53:11 +0000747 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000748 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000749 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000750 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000751 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000752 return QualType(EXTQy, CVRQuals);
753
Christopher Lambebb97e92008-02-04 02:31:56 +0000754 // If the base type isn't canonical, this won't be a canonical type either,
755 // so fill in the canonical type field.
756 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000757 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000758 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000759
Chris Lattnerb7d25532009-02-18 22:53:11 +0000760 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000761 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000762 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000763 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000764 ExtQualType *New =
765 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000766 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000767 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000768 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000769}
770
Chris Lattnerb7d25532009-02-18 22:53:11 +0000771QualType ASTContext::getObjCGCQualType(QualType T,
772 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000773 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000774 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000775 return T;
776
Chris Lattnerb7d25532009-02-18 22:53:11 +0000777 // If we are composing extended qualifiers together, merge together into one
778 // ExtQualType node.
779 unsigned CVRQuals = T.getCVRQualifiers();
780 Type *TypeNode = T.getTypePtr();
781 unsigned AddressSpace = 0;
782
783 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
784 // If this type already has an address space specified, it cannot get
785 // another one.
786 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
787 "Type cannot be in multiple addr spaces!");
788 AddressSpace = EQT->getAddressSpace();
789 TypeNode = EQT->getBaseType();
790 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000791
792 // Check if we've already instantiated an gc qual'd type of this type.
793 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000794 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000795 void *InsertPos = 0;
796 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000797 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000798
799 // If the base type isn't canonical, this won't be a canonical type either,
800 // so fill in the canonical type field.
801 QualType Canonical;
802 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000803 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000804
Chris Lattnerb7d25532009-02-18 22:53:11 +0000805 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000806 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
807 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
808 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000809 ExtQualType *New =
810 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000811 ExtQualTypes.InsertNode(New, InsertPos);
812 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000813 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000814}
Chris Lattnera7674d82007-07-13 22:13:22 +0000815
Reid Spencer5f016e22007-07-11 17:01:13 +0000816/// getComplexType - Return the uniqued reference to the type for a complex
817/// number with the specified element type.
818QualType ASTContext::getComplexType(QualType T) {
819 // Unique pointers, to guarantee there is only one pointer of a particular
820 // structure.
821 llvm::FoldingSetNodeID ID;
822 ComplexType::Profile(ID, T);
823
824 void *InsertPos = 0;
825 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
826 return QualType(CT, 0);
827
828 // If the pointee type isn't canonical, this won't be a canonical type either,
829 // so fill in the canonical type field.
830 QualType Canonical;
831 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000832 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000833
834 // Get the new insert position for the node we care about.
835 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000836 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 }
Steve Narofff83820b2009-01-27 22:08:43 +0000838 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000839 Types.push_back(New);
840 ComplexTypes.InsertNode(New, InsertPos);
841 return QualType(New, 0);
842}
843
Eli Friedmanf98aba32009-02-13 02:31:07 +0000844QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
845 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
846 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
847 FixedWidthIntType *&Entry = Map[Width];
848 if (!Entry)
849 Entry = new FixedWidthIntType(Width, Signed);
850 return QualType(Entry, 0);
851}
Reid Spencer5f016e22007-07-11 17:01:13 +0000852
853/// getPointerType - Return the uniqued reference to the type for a pointer to
854/// the specified type.
855QualType ASTContext::getPointerType(QualType T) {
856 // Unique pointers, to guarantee there is only one pointer of a particular
857 // structure.
858 llvm::FoldingSetNodeID ID;
859 PointerType::Profile(ID, T);
860
861 void *InsertPos = 0;
862 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
863 return QualType(PT, 0);
864
865 // If the pointee type isn't canonical, this won't be a canonical type either,
866 // so fill in the canonical type field.
867 QualType Canonical;
868 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000869 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000870
871 // Get the new insert position for the node we care about.
872 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000873 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000874 }
Steve Narofff83820b2009-01-27 22:08:43 +0000875 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000876 Types.push_back(New);
877 PointerTypes.InsertNode(New, InsertPos);
878 return QualType(New, 0);
879}
880
Steve Naroff5618bd42008-08-27 16:04:49 +0000881/// getBlockPointerType - Return the uniqued reference to the type for
882/// a pointer to the specified block.
883QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000884 assert(T->isFunctionType() && "block of function types only");
885 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000886 // structure.
887 llvm::FoldingSetNodeID ID;
888 BlockPointerType::Profile(ID, T);
889
890 void *InsertPos = 0;
891 if (BlockPointerType *PT =
892 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
893 return QualType(PT, 0);
894
Steve Naroff296e8d52008-08-28 19:20:44 +0000895 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000896 // type either so fill in the canonical type field.
897 QualType Canonical;
898 if (!T->isCanonical()) {
899 Canonical = getBlockPointerType(getCanonicalType(T));
900
901 // Get the new insert position for the node we care about.
902 BlockPointerType *NewIP =
903 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000904 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000905 }
Steve Narofff83820b2009-01-27 22:08:43 +0000906 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000907 Types.push_back(New);
908 BlockPointerTypes.InsertNode(New, InsertPos);
909 return QualType(New, 0);
910}
911
Reid Spencer5f016e22007-07-11 17:01:13 +0000912/// getReferenceType - Return the uniqued reference to the type for a reference
913/// to the specified type.
914QualType ASTContext::getReferenceType(QualType T) {
915 // Unique pointers, to guarantee there is only one pointer of a particular
916 // structure.
917 llvm::FoldingSetNodeID ID;
918 ReferenceType::Profile(ID, T);
919
920 void *InsertPos = 0;
921 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
922 return QualType(RT, 0);
923
924 // If the referencee type isn't canonical, this won't be a canonical type
925 // either, so fill in the canonical type field.
926 QualType Canonical;
927 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000928 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000929
930 // Get the new insert position for the node we care about.
931 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000932 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000933 }
934
Steve Narofff83820b2009-01-27 22:08:43 +0000935 ReferenceType *New = new (*this,8) ReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000936 Types.push_back(New);
937 ReferenceTypes.InsertNode(New, InsertPos);
938 return QualType(New, 0);
939}
940
Sebastian Redlf30208a2009-01-24 21:16:55 +0000941/// getMemberPointerType - Return the uniqued reference to the type for a
942/// member pointer to the specified type, in the specified class.
943QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
944{
945 // Unique pointers, to guarantee there is only one pointer of a particular
946 // structure.
947 llvm::FoldingSetNodeID ID;
948 MemberPointerType::Profile(ID, T, Cls);
949
950 void *InsertPos = 0;
951 if (MemberPointerType *PT =
952 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
953 return QualType(PT, 0);
954
955 // If the pointee or class type isn't canonical, this won't be a canonical
956 // type either, so fill in the canonical type field.
957 QualType Canonical;
958 if (!T->isCanonical()) {
959 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
960
961 // Get the new insert position for the node we care about.
962 MemberPointerType *NewIP =
963 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
964 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
965 }
Steve Narofff83820b2009-01-27 22:08:43 +0000966 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000967 Types.push_back(New);
968 MemberPointerTypes.InsertNode(New, InsertPos);
969 return QualType(New, 0);
970}
971
Steve Narofffb22d962007-08-30 01:06:46 +0000972/// getConstantArrayType - Return the unique reference to the type for an
973/// array of the specified element type.
974QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000975 const llvm::APInt &ArySize,
976 ArrayType::ArraySizeModifier ASM,
977 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000978 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +0000979 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000980
981 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000982 if (ConstantArrayType *ATP =
983 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000984 return QualType(ATP, 0);
985
986 // If the element type isn't canonical, this won't be a canonical type either,
987 // so fill in the canonical type field.
988 QualType Canonical;
989 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000990 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000991 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000992 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000993 ConstantArrayType *NewIP =
994 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000995 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 }
997
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000998 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +0000999 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001000 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001001 Types.push_back(New);
1002 return QualType(New, 0);
1003}
1004
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001005/// getVariableArrayType - Returns a non-unique reference to the type for a
1006/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001007QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1008 ArrayType::ArraySizeModifier ASM,
1009 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001010 // Since we don't unique expressions, it isn't possible to unique VLA's
1011 // that have an expression provided for their size.
1012
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001013 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001014 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001015
1016 VariableArrayTypes.push_back(New);
1017 Types.push_back(New);
1018 return QualType(New, 0);
1019}
1020
Douglas Gregor898574e2008-12-05 23:32:09 +00001021/// getDependentSizedArrayType - Returns a non-unique reference to
1022/// the type for a dependently-sized array of the specified element
1023/// type. FIXME: We will need these to be uniqued, or at least
1024/// comparable, at some point.
1025QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1026 ArrayType::ArraySizeModifier ASM,
1027 unsigned EltTypeQuals) {
1028 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1029 "Size must be type- or value-dependent!");
1030
1031 // Since we don't unique expressions, it isn't possible to unique
1032 // dependently-sized array types.
1033
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001034 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001035 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1036 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001037
1038 DependentSizedArrayTypes.push_back(New);
1039 Types.push_back(New);
1040 return QualType(New, 0);
1041}
1042
Eli Friedmanc5773c42008-02-15 18:16:39 +00001043QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1044 ArrayType::ArraySizeModifier ASM,
1045 unsigned EltTypeQuals) {
1046 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001047 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001048
1049 void *InsertPos = 0;
1050 if (IncompleteArrayType *ATP =
1051 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1052 return QualType(ATP, 0);
1053
1054 // If the element type isn't canonical, this won't be a canonical type
1055 // either, so fill in the canonical type field.
1056 QualType Canonical;
1057
1058 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001059 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001060 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001061
1062 // Get the new insert position for the node we care about.
1063 IncompleteArrayType *NewIP =
1064 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001065 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001066 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001067
Steve Narofff83820b2009-01-27 22:08:43 +00001068 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001069 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001070
1071 IncompleteArrayTypes.InsertNode(New, InsertPos);
1072 Types.push_back(New);
1073 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001074}
1075
Steve Naroff73322922007-07-18 18:00:27 +00001076/// getVectorType - Return the unique reference to a vector type of
1077/// the specified element type and size. VectorType must be a built-in type.
1078QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001079 BuiltinType *baseType;
1080
Chris Lattnerf52ab252008-04-06 22:59:24 +00001081 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001082 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001083
1084 // Check if we've already instantiated a vector of this type.
1085 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001086 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 void *InsertPos = 0;
1088 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1089 return QualType(VTP, 0);
1090
1091 // If the element type isn't canonical, this won't be a canonical type either,
1092 // so fill in the canonical type field.
1093 QualType Canonical;
1094 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001095 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001096
1097 // Get the new insert position for the node we care about.
1098 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001099 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 }
Steve Narofff83820b2009-01-27 22:08:43 +00001101 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001102 VectorTypes.InsertNode(New, InsertPos);
1103 Types.push_back(New);
1104 return QualType(New, 0);
1105}
1106
Nate Begeman213541a2008-04-18 23:10:10 +00001107/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001108/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001109QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001110 BuiltinType *baseType;
1111
Chris Lattnerf52ab252008-04-06 22:59:24 +00001112 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001113 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001114
1115 // Check if we've already instantiated a vector of this type.
1116 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001117 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001118 void *InsertPos = 0;
1119 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1120 return QualType(VTP, 0);
1121
1122 // If the element type isn't canonical, this won't be a canonical type either,
1123 // so fill in the canonical type field.
1124 QualType Canonical;
1125 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001126 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001127
1128 // Get the new insert position for the node we care about.
1129 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001130 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001131 }
Steve Narofff83820b2009-01-27 22:08:43 +00001132 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001133 VectorTypes.InsertNode(New, InsertPos);
1134 Types.push_back(New);
1135 return QualType(New, 0);
1136}
1137
Douglas Gregor72564e72009-02-26 23:50:07 +00001138/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001139///
Douglas Gregor72564e72009-02-26 23:50:07 +00001140QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001141 // Unique functions, to guarantee there is only one function of a particular
1142 // structure.
1143 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001144 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001145
1146 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001147 if (FunctionNoProtoType *FT =
1148 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001149 return QualType(FT, 0);
1150
1151 QualType Canonical;
1152 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001153 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001154
1155 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001156 FunctionNoProtoType *NewIP =
1157 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001158 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 }
1160
Douglas Gregor72564e72009-02-26 23:50:07 +00001161 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001162 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001163 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 return QualType(New, 0);
1165}
1166
1167/// getFunctionType - Return a normal function type with a typed argument
1168/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001169QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001170 unsigned NumArgs, bool isVariadic,
1171 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001172 // Unique functions, to guarantee there is only one function of a particular
1173 // structure.
1174 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001175 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001176 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001177
1178 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001179 if (FunctionProtoType *FTP =
1180 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 return QualType(FTP, 0);
1182
1183 // Determine whether the type being created is already canonical or not.
1184 bool isCanonical = ResultTy->isCanonical();
1185 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1186 if (!ArgArray[i]->isCanonical())
1187 isCanonical = false;
1188
1189 // If this type isn't canonical, get the canonical version of it.
1190 QualType Canonical;
1191 if (!isCanonical) {
1192 llvm::SmallVector<QualType, 16> CanonicalArgs;
1193 CanonicalArgs.reserve(NumArgs);
1194 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001195 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001196
Chris Lattnerf52ab252008-04-06 22:59:24 +00001197 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001199 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001200
1201 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001202 FunctionProtoType *NewIP =
1203 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001204 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001205 }
1206
Douglas Gregor72564e72009-02-26 23:50:07 +00001207 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001208 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001209 FunctionProtoType *FTP =
1210 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001211 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001212 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001213 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001215 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 return QualType(FTP, 0);
1217}
1218
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001219/// getTypeDeclType - Return the unique reference to the type for the
1220/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001221QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001222 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001223 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1224
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001225 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001226 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001227 else if (isa<TemplateTypeParmDecl>(Decl)) {
1228 assert(false && "Template type parameter types are always available.");
1229 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001230 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001231
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001232 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001233 if (PrevDecl)
1234 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001235 else
1236 Decl->TypeForDecl = new (*this,8) CXXRecordType(CXXRecord);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001237 }
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001238 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001239 if (PrevDecl)
1240 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001241 else
1242 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001243 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001244 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1245 if (PrevDecl)
1246 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001247 else
1248 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001249 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001250 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001251 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001252
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001253 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001254 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001255}
1256
Reid Spencer5f016e22007-07-11 17:01:13 +00001257/// getTypedefType - Return the unique reference to the type for the
1258/// specified typename decl.
1259QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1260 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1261
Chris Lattnerf52ab252008-04-06 22:59:24 +00001262 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001263 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001264 Types.push_back(Decl->TypeForDecl);
1265 return QualType(Decl->TypeForDecl, 0);
1266}
1267
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001268/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001269/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001270QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001271 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1272
Steve Narofff83820b2009-01-27 22:08:43 +00001273 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001274 Types.push_back(Decl->TypeForDecl);
1275 return QualType(Decl->TypeForDecl, 0);
1276}
1277
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00001278/// buildObjCInterfaceType - Returns a new type for the interface
1279/// declaration, regardless. It also removes any previously built
1280/// record declaration so caller can rebuild it.
1281QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) {
1282 const RecordDecl *&RD = ASTRecordForInterface[Decl];
1283 if (RD)
1284 RD = 0;
1285 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
1286 Types.push_back(Decl->TypeForDecl);
1287 return QualType(Decl->TypeForDecl, 0);
1288}
1289
Douglas Gregorfab9d672009-02-05 23:33:38 +00001290/// \brief Retrieve the template type parameter type for a template
1291/// parameter with the given depth, index, and (optionally) name.
1292QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1293 IdentifierInfo *Name) {
1294 llvm::FoldingSetNodeID ID;
1295 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1296 void *InsertPos = 0;
1297 TemplateTypeParmType *TypeParm
1298 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1299
1300 if (TypeParm)
1301 return QualType(TypeParm, 0);
1302
1303 if (Name)
1304 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1305 getTemplateTypeParmType(Depth, Index));
1306 else
1307 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1308
1309 Types.push_back(TypeParm);
1310 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1311
1312 return QualType(TypeParm, 0);
1313}
1314
Douglas Gregor55f6b142009-02-09 18:46:07 +00001315QualType
1316ASTContext::getClassTemplateSpecializationType(TemplateDecl *Template,
1317 unsigned NumArgs,
1318 uintptr_t *Args, bool *ArgIsType,
1319 QualType Canon) {
Douglas Gregorfc705b82009-02-26 22:19:44 +00001320 Canon = getCanonicalType(Canon);
1321
Douglas Gregor55f6b142009-02-09 18:46:07 +00001322 llvm::FoldingSetNodeID ID;
1323 ClassTemplateSpecializationType::Profile(ID, Template, NumArgs, Args,
1324 ArgIsType);
1325 void *InsertPos = 0;
1326 ClassTemplateSpecializationType *Spec
1327 = ClassTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
1328
1329 if (Spec)
1330 return QualType(Spec, 0);
1331
1332 void *Mem = Allocate(sizeof(ClassTemplateSpecializationType) +
1333 (sizeof(uintptr_t) *
1334 (ClassTemplateSpecializationType::
1335 getNumPackedWords(NumArgs) +
1336 NumArgs)), 8);
1337 Spec = new (Mem) ClassTemplateSpecializationType(Template, NumArgs, Args,
1338 ArgIsType, Canon);
1339 Types.push_back(Spec);
1340 ClassTemplateSpecializationTypes.InsertNode(Spec, InsertPos);
1341
1342 return QualType(Spec, 0);
1343}
1344
Chris Lattner88cb27a2008-04-07 04:56:42 +00001345/// CmpProtocolNames - Comparison predicate for sorting protocols
1346/// alphabetically.
1347static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1348 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001349 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001350}
1351
1352static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1353 unsigned &NumProtocols) {
1354 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1355
1356 // Sort protocols, keyed by name.
1357 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1358
1359 // Remove duplicates.
1360 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1361 NumProtocols = ProtocolsEnd-Protocols;
1362}
1363
1364
Chris Lattner065f0d72008-04-07 04:44:08 +00001365/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1366/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001367QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1368 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001369 // Sort the protocol list alphabetically to canonicalize it.
1370 SortAndUniqueProtocols(Protocols, NumProtocols);
1371
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001372 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001373 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001374
1375 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001376 if (ObjCQualifiedInterfaceType *QT =
1377 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001378 return QualType(QT, 0);
1379
1380 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001381 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001382 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001383
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001384 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001385 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001386 return QualType(QType, 0);
1387}
1388
Chris Lattner88cb27a2008-04-07 04:56:42 +00001389/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1390/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001391QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001392 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001393 // Sort the protocol list alphabetically to canonicalize it.
1394 SortAndUniqueProtocols(Protocols, NumProtocols);
1395
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001396 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001397 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001398
1399 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001400 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001401 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001402 return QualType(QT, 0);
1403
1404 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001405 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001406 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001407 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001408 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001409 return QualType(QType, 0);
1410}
1411
Douglas Gregor72564e72009-02-26 23:50:07 +00001412/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1413/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001414/// multiple declarations that refer to "typeof(x)" all contain different
1415/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1416/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001417QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001418 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001419 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001420 Types.push_back(toe);
1421 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001422}
1423
Steve Naroff9752f252007-08-01 18:02:17 +00001424/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1425/// TypeOfType AST's. The only motivation to unique these nodes would be
1426/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1427/// an issue. This doesn't effect the type checker, since it operates
1428/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001429QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001430 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001431 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001432 Types.push_back(tot);
1433 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001434}
1435
Reid Spencer5f016e22007-07-11 17:01:13 +00001436/// getTagDeclType - Return the unique reference to the type for the
1437/// specified TagDecl (struct/union/class/enum) decl.
1438QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001439 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001440 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001441}
1442
1443/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1444/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1445/// needs to agree with the definition in <stddef.h>.
1446QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001447 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001448}
1449
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001450/// getSignedWCharType - Return the type of "signed wchar_t".
1451/// Used when in C++, as a GCC extension.
1452QualType ASTContext::getSignedWCharType() const {
1453 // FIXME: derive from "Target" ?
1454 return WCharTy;
1455}
1456
1457/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1458/// Used when in C++, as a GCC extension.
1459QualType ASTContext::getUnsignedWCharType() const {
1460 // FIXME: derive from "Target" ?
1461 return UnsignedIntTy;
1462}
1463
Chris Lattner8b9023b2007-07-13 03:05:23 +00001464/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1465/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1466QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001467 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001468}
1469
Chris Lattnere6327742008-04-02 05:18:44 +00001470//===----------------------------------------------------------------------===//
1471// Type Operators
1472//===----------------------------------------------------------------------===//
1473
Chris Lattner77c96472008-04-06 22:41:35 +00001474/// getCanonicalType - Return the canonical (structural) type corresponding to
1475/// the specified potentially non-canonical type. The non-canonical version
1476/// of a type may have many "decorated" versions of types. Decorators can
1477/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1478/// to be free of any of these, allowing two canonical types to be compared
1479/// for exact equality with a simple pointer comparison.
1480QualType ASTContext::getCanonicalType(QualType T) {
1481 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001482
1483 // If the result has type qualifiers, make sure to canonicalize them as well.
1484 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1485 if (TypeQuals == 0) return CanType;
1486
1487 // If the type qualifiers are on an array type, get the canonical type of the
1488 // array with the qualifiers applied to the element type.
1489 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1490 if (!AT)
1491 return CanType.getQualifiedType(TypeQuals);
1492
1493 // Get the canonical version of the element with the extra qualifiers on it.
1494 // This can recursively sink qualifiers through multiple levels of arrays.
1495 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1496 NewEltTy = getCanonicalType(NewEltTy);
1497
1498 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1499 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1500 CAT->getIndexTypeQualifier());
1501 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1502 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1503 IAT->getIndexTypeQualifier());
1504
Douglas Gregor898574e2008-12-05 23:32:09 +00001505 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1506 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1507 DSAT->getSizeModifier(),
1508 DSAT->getIndexTypeQualifier());
1509
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001510 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1511 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1512 VAT->getSizeModifier(),
1513 VAT->getIndexTypeQualifier());
1514}
1515
1516
1517const ArrayType *ASTContext::getAsArrayType(QualType T) {
1518 // Handle the non-qualified case efficiently.
1519 if (T.getCVRQualifiers() == 0) {
1520 // Handle the common positive case fast.
1521 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1522 return AT;
1523 }
1524
1525 // Handle the common negative case fast, ignoring CVR qualifiers.
1526 QualType CType = T->getCanonicalTypeInternal();
1527
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001528 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001529 // test.
1530 if (!isa<ArrayType>(CType) &&
1531 !isa<ArrayType>(CType.getUnqualifiedType()))
1532 return 0;
1533
1534 // Apply any CVR qualifiers from the array type to the element type. This
1535 // implements C99 6.7.3p8: "If the specification of an array type includes
1536 // any type qualifiers, the element type is so qualified, not the array type."
1537
1538 // If we get here, we either have type qualifiers on the type, or we have
1539 // sugar such as a typedef in the way. If we have type qualifiers on the type
1540 // we must propagate them down into the elemeng type.
1541 unsigned CVRQuals = T.getCVRQualifiers();
1542 unsigned AddrSpace = 0;
1543 Type *Ty = T.getTypePtr();
1544
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001545 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001546 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001547 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1548 AddrSpace = EXTQT->getAddressSpace();
1549 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001550 } else {
1551 T = Ty->getDesugaredType();
1552 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1553 break;
1554 CVRQuals |= T.getCVRQualifiers();
1555 Ty = T.getTypePtr();
1556 }
1557 }
1558
1559 // If we have a simple case, just return now.
1560 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1561 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1562 return ATy;
1563
1564 // Otherwise, we have an array and we have qualifiers on it. Push the
1565 // qualifiers into the array element type and return a new array type.
1566 // Get the canonical version of the element with the extra qualifiers on it.
1567 // This can recursively sink qualifiers through multiple levels of arrays.
1568 QualType NewEltTy = ATy->getElementType();
1569 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001570 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001571 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1572
1573 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1574 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1575 CAT->getSizeModifier(),
1576 CAT->getIndexTypeQualifier()));
1577 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1578 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1579 IAT->getSizeModifier(),
1580 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001581
Douglas Gregor898574e2008-12-05 23:32:09 +00001582 if (const DependentSizedArrayType *DSAT
1583 = dyn_cast<DependentSizedArrayType>(ATy))
1584 return cast<ArrayType>(
1585 getDependentSizedArrayType(NewEltTy,
1586 DSAT->getSizeExpr(),
1587 DSAT->getSizeModifier(),
1588 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001589
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001590 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1591 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1592 VAT->getSizeModifier(),
1593 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001594}
1595
1596
Chris Lattnere6327742008-04-02 05:18:44 +00001597/// getArrayDecayedType - Return the properly qualified result of decaying the
1598/// specified array type to a pointer. This operation is non-trivial when
1599/// handling typedefs etc. The canonical type of "T" must be an array type,
1600/// this returns a pointer to a properly qualified element of the array.
1601///
1602/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1603QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001604 // Get the element type with 'getAsArrayType' so that we don't lose any
1605 // typedefs in the element type of the array. This also handles propagation
1606 // of type qualifiers from the array type into the element type if present
1607 // (C99 6.7.3p8).
1608 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1609 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001610
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001611 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001612
1613 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001614 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001615}
1616
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001617QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001618 QualType ElemTy = VAT->getElementType();
1619
1620 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1621 return getBaseElementType(VAT);
1622
1623 return ElemTy;
1624}
1625
Reid Spencer5f016e22007-07-11 17:01:13 +00001626/// getFloatingRank - Return a relative rank for floating point types.
1627/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001628static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001629 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001630 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001631
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001632 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001633 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001634 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001635 case BuiltinType::Float: return FloatRank;
1636 case BuiltinType::Double: return DoubleRank;
1637 case BuiltinType::LongDouble: return LongDoubleRank;
1638 }
1639}
1640
Steve Naroff716c7302007-08-27 01:41:48 +00001641/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1642/// point or a complex type (based on typeDomain/typeSize).
1643/// 'typeDomain' is a real floating point or complex type.
1644/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001645QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1646 QualType Domain) const {
1647 FloatingRank EltRank = getFloatingRank(Size);
1648 if (Domain->isComplexType()) {
1649 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001650 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001651 case FloatRank: return FloatComplexTy;
1652 case DoubleRank: return DoubleComplexTy;
1653 case LongDoubleRank: return LongDoubleComplexTy;
1654 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001655 }
Chris Lattner1361b112008-04-06 23:58:54 +00001656
1657 assert(Domain->isRealFloatingType() && "Unknown domain!");
1658 switch (EltRank) {
1659 default: assert(0 && "getFloatingRank(): illegal value for rank");
1660 case FloatRank: return FloatTy;
1661 case DoubleRank: return DoubleTy;
1662 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001663 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001664}
1665
Chris Lattner7cfeb082008-04-06 23:55:33 +00001666/// getFloatingTypeOrder - Compare the rank of the two specified floating
1667/// point types, ignoring the domain of the type (i.e. 'double' ==
1668/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1669/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001670int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1671 FloatingRank LHSR = getFloatingRank(LHS);
1672 FloatingRank RHSR = getFloatingRank(RHS);
1673
1674 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001675 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001676 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001677 return 1;
1678 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001679}
1680
Chris Lattnerf52ab252008-04-06 22:59:24 +00001681/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1682/// routine will assert if passed a built-in type that isn't an integer or enum,
1683/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001684unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001685 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001686 if (EnumType* ET = dyn_cast<EnumType>(T))
1687 T = ET->getDecl()->getIntegerType().getTypePtr();
1688
1689 // There are two things which impact the integer rank: the width, and
1690 // the ordering of builtins. The builtin ordering is encoded in the
1691 // bottom three bits; the width is encoded in the bits above that.
1692 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1693 return FWIT->getWidth() << 3;
1694 }
1695
Chris Lattnerf52ab252008-04-06 22:59:24 +00001696 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001697 default: assert(0 && "getIntegerRank(): not a built-in integer");
1698 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001699 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001700 case BuiltinType::Char_S:
1701 case BuiltinType::Char_U:
1702 case BuiltinType::SChar:
1703 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001704 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001705 case BuiltinType::Short:
1706 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001707 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001708 case BuiltinType::Int:
1709 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001710 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001711 case BuiltinType::Long:
1712 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001713 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001714 case BuiltinType::LongLong:
1715 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001716 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001717 }
1718}
1719
Chris Lattner7cfeb082008-04-06 23:55:33 +00001720/// getIntegerTypeOrder - Returns the highest ranked integer type:
1721/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1722/// LHS < RHS, return -1.
1723int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001724 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1725 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001726 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001727
Chris Lattnerf52ab252008-04-06 22:59:24 +00001728 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1729 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001730
Chris Lattner7cfeb082008-04-06 23:55:33 +00001731 unsigned LHSRank = getIntegerRank(LHSC);
1732 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001733
Chris Lattner7cfeb082008-04-06 23:55:33 +00001734 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1735 if (LHSRank == RHSRank) return 0;
1736 return LHSRank > RHSRank ? 1 : -1;
1737 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001738
Chris Lattner7cfeb082008-04-06 23:55:33 +00001739 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1740 if (LHSUnsigned) {
1741 // If the unsigned [LHS] type is larger, return it.
1742 if (LHSRank >= RHSRank)
1743 return 1;
1744
1745 // If the signed type can represent all values of the unsigned type, it
1746 // wins. Because we are dealing with 2's complement and types that are
1747 // powers of two larger than each other, this is always safe.
1748 return -1;
1749 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001750
Chris Lattner7cfeb082008-04-06 23:55:33 +00001751 // If the unsigned [RHS] type is larger, return it.
1752 if (RHSRank >= LHSRank)
1753 return -1;
1754
1755 // If the signed type can represent all values of the unsigned type, it
1756 // wins. Because we are dealing with 2's complement and types that are
1757 // powers of two larger than each other, this is always safe.
1758 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001759}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001760
1761// getCFConstantStringType - Return the type used for constant CFStrings.
1762QualType ASTContext::getCFConstantStringType() {
1763 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001764 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001765 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001766 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001767 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001768
1769 // const int *isa;
1770 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001771 // int flags;
1772 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001773 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001774 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001775 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001776 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001777
Anders Carlsson71993dd2007-08-17 05:31:46 +00001778 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001779 for (unsigned i = 0; i < 4; ++i) {
1780 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1781 SourceLocation(), 0,
1782 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001783 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001784 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001785 }
1786
1787 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001788 }
1789
1790 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001791}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001792
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001793QualType ASTContext::getObjCFastEnumerationStateType()
1794{
1795 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001796 ObjCFastEnumerationStateTypeDecl =
1797 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1798 &Idents.get("__objcFastEnumerationState"));
1799
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001800 QualType FieldTypes[] = {
1801 UnsignedLongTy,
1802 getPointerType(ObjCIdType),
1803 getPointerType(UnsignedLongTy),
1804 getConstantArrayType(UnsignedLongTy,
1805 llvm::APInt(32, 5), ArrayType::Normal, 0)
1806 };
1807
Douglas Gregor44b43212008-12-11 16:49:14 +00001808 for (size_t i = 0; i < 4; ++i) {
1809 FieldDecl *Field = FieldDecl::Create(*this,
1810 ObjCFastEnumerationStateTypeDecl,
1811 SourceLocation(), 0,
1812 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001813 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001814 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001815 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001816
Douglas Gregor44b43212008-12-11 16:49:14 +00001817 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001818 }
1819
1820 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1821}
1822
Anders Carlssone8c49532007-10-29 06:33:42 +00001823// This returns true if a type has been typedefed to BOOL:
1824// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001825static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001826 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00001827 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1828 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001829
1830 return false;
1831}
1832
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001833/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001834/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001835int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001836 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001837
1838 // Make all integer and enum types at least as large as an int
1839 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001840 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001841 // Treat arrays as pointers, since that's how they're passed in.
1842 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001843 sz = getTypeSize(VoidPtrTy);
1844 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001845}
1846
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001847/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001848/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001849void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001850 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001851 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001852 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001853 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001854 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001855 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001856 // Compute size of all parameters.
1857 // Start with computing size of a pointer in number of bytes.
1858 // FIXME: There might(should) be a better way of doing this computation!
1859 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001860 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001861 // The first two arguments (self and _cmd) are pointers; account for
1862 // their size.
1863 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00001864 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1865 E = Decl->param_end(); PI != E; ++PI) {
1866 QualType PType = (*PI)->getType();
1867 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001868 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001869 ParmOffset += sz;
1870 }
1871 S += llvm::utostr(ParmOffset);
1872 S += "@0:";
1873 S += llvm::utostr(PtrSize);
1874
1875 // Argument types.
1876 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00001877 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1878 E = Decl->param_end(); PI != E; ++PI) {
1879 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001880 QualType PType = PVDecl->getOriginalType();
1881 if (const ArrayType *AT =
1882 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1883 // Use array's original type only if it has known number of
1884 // elements.
1885 if (!dyn_cast<ConstantArrayType>(AT))
1886 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001887 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001888 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001889 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001890 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001891 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001892 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001893 }
1894}
1895
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001896/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001897/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001898/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1899/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001900/// Property attributes are stored as a comma-delimited C string. The simple
1901/// attributes readonly and bycopy are encoded as single characters. The
1902/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1903/// encoded as single characters, followed by an identifier. Property types
1904/// are also encoded as a parametrized attribute. The characters used to encode
1905/// these attributes are defined by the following enumeration:
1906/// @code
1907/// enum PropertyAttributes {
1908/// kPropertyReadOnly = 'R', // property is read-only.
1909/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1910/// kPropertyByref = '&', // property is a reference to the value last assigned
1911/// kPropertyDynamic = 'D', // property is dynamic
1912/// kPropertyGetter = 'G', // followed by getter selector name
1913/// kPropertySetter = 'S', // followed by setter selector name
1914/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1915/// kPropertyType = 't' // followed by old-style type encoding.
1916/// kPropertyWeak = 'W' // 'weak' property
1917/// kPropertyStrong = 'P' // property GC'able
1918/// kPropertyNonAtomic = 'N' // property non-atomic
1919/// };
1920/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001921void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1922 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001923 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001924 // Collect information from the property implementation decl(s).
1925 bool Dynamic = false;
1926 ObjCPropertyImplDecl *SynthesizePID = 0;
1927
1928 // FIXME: Duplicated code due to poor abstraction.
1929 if (Container) {
1930 if (const ObjCCategoryImplDecl *CID =
1931 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1932 for (ObjCCategoryImplDecl::propimpl_iterator
1933 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1934 ObjCPropertyImplDecl *PID = *i;
1935 if (PID->getPropertyDecl() == PD) {
1936 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1937 Dynamic = true;
1938 } else {
1939 SynthesizePID = PID;
1940 }
1941 }
1942 }
1943 } else {
Chris Lattner61710852008-10-05 17:34:18 +00001944 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001945 for (ObjCCategoryImplDecl::propimpl_iterator
1946 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1947 ObjCPropertyImplDecl *PID = *i;
1948 if (PID->getPropertyDecl() == PD) {
1949 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1950 Dynamic = true;
1951 } else {
1952 SynthesizePID = PID;
1953 }
1954 }
1955 }
1956 }
1957 }
1958
1959 // FIXME: This is not very efficient.
1960 S = "T";
1961
1962 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001963 // GCC has some special rules regarding encoding of properties which
1964 // closely resembles encoding of ivars.
1965 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
1966 true /* outermost type */,
1967 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001968
1969 if (PD->isReadOnly()) {
1970 S += ",R";
1971 } else {
1972 switch (PD->getSetterKind()) {
1973 case ObjCPropertyDecl::Assign: break;
1974 case ObjCPropertyDecl::Copy: S += ",C"; break;
1975 case ObjCPropertyDecl::Retain: S += ",&"; break;
1976 }
1977 }
1978
1979 // It really isn't clear at all what this means, since properties
1980 // are "dynamic by default".
1981 if (Dynamic)
1982 S += ",D";
1983
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001984 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
1985 S += ",N";
1986
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001987 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1988 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001989 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001990 }
1991
1992 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1993 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001994 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001995 }
1996
1997 if (SynthesizePID) {
1998 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1999 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002000 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002001 }
2002
2003 // FIXME: OBJCGC: weak & strong
2004}
2005
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002006/// getLegacyIntegralTypeEncoding -
2007/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002008/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002009/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2010///
2011void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2012 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2013 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002014 if (BT->getKind() == BuiltinType::ULong &&
2015 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002016 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002017 else
2018 if (BT->getKind() == BuiltinType::Long &&
2019 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002020 PointeeTy = IntTy;
2021 }
2022 }
2023}
2024
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002025void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002026 FieldDecl *Field) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002027 // We follow the behavior of gcc, expanding structures which are
2028 // directly pointed to, and expanding embedded structures. Note that
2029 // these rules are sufficient to prevent recursive encoding of the
2030 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002031 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2032 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002033}
2034
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002035static void EncodeBitField(const ASTContext *Context, std::string& S,
2036 FieldDecl *FD) {
2037 const Expr *E = FD->getBitWidth();
2038 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2039 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2040 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2041 S += 'b';
2042 S += llvm::utostr(N);
2043}
2044
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002045void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2046 bool ExpandPointedToStructures,
2047 bool ExpandStructures,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002048 FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002049 bool OutermostType,
2050 bool EncodingProperty) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00002051 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002052 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002053 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002054 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002055 else {
2056 char encoding;
2057 switch (BT->getKind()) {
2058 default: assert(0 && "Unhandled builtin type kind");
2059 case BuiltinType::Void: encoding = 'v'; break;
2060 case BuiltinType::Bool: encoding = 'B'; break;
2061 case BuiltinType::Char_U:
2062 case BuiltinType::UChar: encoding = 'C'; break;
2063 case BuiltinType::UShort: encoding = 'S'; break;
2064 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002065 case BuiltinType::ULong:
2066 encoding =
2067 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2068 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002069 case BuiltinType::ULongLong: encoding = 'Q'; break;
2070 case BuiltinType::Char_S:
2071 case BuiltinType::SChar: encoding = 'c'; break;
2072 case BuiltinType::Short: encoding = 's'; break;
2073 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002074 case BuiltinType::Long:
2075 encoding =
2076 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2077 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002078 case BuiltinType::LongLong: encoding = 'q'; break;
2079 case BuiltinType::Float: encoding = 'f'; break;
2080 case BuiltinType::Double: encoding = 'd'; break;
2081 case BuiltinType::LongDouble: encoding = 'd'; break;
2082 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002083
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002084 S += encoding;
2085 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002086 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002087 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002088 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2089 ExpandPointedToStructures,
2090 ExpandStructures, FD);
2091 if (FD || EncodingProperty) {
2092 // Note that we do extended encoding of protocol qualifer list
2093 // Only when doing ivar or property encoding.
2094 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2095 S += '"';
2096 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2097 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2098 S += '<';
2099 S += Proto->getNameAsString();
2100 S += '>';
2101 }
2102 S += '"';
2103 }
2104 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002105 }
2106 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002107 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002108 bool isReadOnly = false;
2109 // For historical/compatibility reasons, the read-only qualifier of the
2110 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2111 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2112 // Also, do not emit the 'r' for anything but the outermost type!
2113 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2114 if (OutermostType && T.isConstQualified()) {
2115 isReadOnly = true;
2116 S += 'r';
2117 }
2118 }
2119 else if (OutermostType) {
2120 QualType P = PointeeTy;
2121 while (P->getAsPointerType())
2122 P = P->getAsPointerType()->getPointeeType();
2123 if (P.isConstQualified()) {
2124 isReadOnly = true;
2125 S += 'r';
2126 }
2127 }
2128 if (isReadOnly) {
2129 // Another legacy compatibility encoding. Some ObjC qualifier and type
2130 // combinations need to be rearranged.
2131 // Rewrite "in const" from "nr" to "rn"
2132 const char * s = S.c_str();
2133 int len = S.length();
2134 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2135 std::string replace = "rn";
2136 S.replace(S.end()-2, S.end(), replace);
2137 }
2138 }
Steve Naroff389bf462009-02-12 17:52:19 +00002139 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002140 S += '@';
2141 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002142 }
2143 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002144 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002145 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002146 // Another historical/compatibility reason.
2147 // We encode the underlying type which comes out as
2148 // {...};
2149 S += '^';
2150 getObjCEncodingForTypeImpl(PointeeTy, S,
2151 false, ExpandPointedToStructures,
2152 NULL);
2153 return;
2154 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002155 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002156 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002157 const ObjCInterfaceType *OIT =
2158 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002159 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002160 S += '"';
2161 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002162 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2163 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2164 S += '<';
2165 S += Proto->getNameAsString();
2166 S += '>';
2167 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002168 S += '"';
2169 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002170 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002171 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002172 S += '#';
2173 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002174 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002175 S += ':';
2176 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002177 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002178
2179 if (PointeeTy->isCharType()) {
2180 // char pointer types should be encoded as '*' unless it is a
2181 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002182 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002183 S += '*';
2184 return;
2185 }
2186 }
2187
2188 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002189 getLegacyIntegralTypeEncoding(PointeeTy);
2190
2191 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002192 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002193 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002194 } else if (const ArrayType *AT =
2195 // Ignore type qualifiers etc.
2196 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002197 if (isa<IncompleteArrayType>(AT)) {
2198 // Incomplete arrays are encoded as a pointer to the array element.
2199 S += '^';
2200
2201 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2202 false, ExpandStructures, FD);
2203 } else {
2204 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002205
Anders Carlsson559a8332009-02-22 01:38:57 +00002206 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2207 S += llvm::utostr(CAT->getSize().getZExtValue());
2208 else {
2209 //Variable length arrays are encoded as a regular array with 0 elements.
2210 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2211 S += '0';
2212 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002213
Anders Carlsson559a8332009-02-22 01:38:57 +00002214 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2215 false, ExpandStructures, FD);
2216 S += ']';
2217 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002218 } else if (T->getAsFunctionType()) {
2219 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002220 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002221 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002222 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002223 // Anonymous structures print as '?'
2224 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2225 S += II->getName();
2226 } else {
2227 S += '?';
2228 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002229 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002230 S += '=';
Douglas Gregor44b43212008-12-11 16:49:14 +00002231 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2232 FieldEnd = RDecl->field_end();
2233 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002234 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002235 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002236 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002237 S += '"';
2238 }
2239
2240 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002241 if (Field->isBitField()) {
2242 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2243 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002244 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002245 QualType qt = Field->getType();
2246 getLegacyIntegralTypeEncoding(qt);
2247 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002248 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002249 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002250 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002251 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002252 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002253 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002254 if (FD && FD->isBitField())
2255 EncodeBitField(this, S, FD);
2256 else
2257 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002258 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002259 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002260 } else if (T->isObjCInterfaceType()) {
2261 // @encode(class_name)
2262 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2263 S += '{';
2264 const IdentifierInfo *II = OI->getIdentifier();
2265 S += II->getName();
2266 S += '=';
2267 std::vector<FieldDecl*> RecFields;
2268 CollectObjCIvars(OI, RecFields);
2269 for (unsigned int i = 0; i != RecFields.size(); i++) {
2270 if (RecFields[i]->isBitField())
2271 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2272 RecFields[i]);
2273 else
2274 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2275 FD);
2276 }
2277 S += '}';
2278 }
2279 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002280 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002281}
2282
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002283void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002284 std::string& S) const {
2285 if (QT & Decl::OBJC_TQ_In)
2286 S += 'n';
2287 if (QT & Decl::OBJC_TQ_Inout)
2288 S += 'N';
2289 if (QT & Decl::OBJC_TQ_Out)
2290 S += 'o';
2291 if (QT & Decl::OBJC_TQ_Bycopy)
2292 S += 'O';
2293 if (QT & Decl::OBJC_TQ_Byref)
2294 S += 'R';
2295 if (QT & Decl::OBJC_TQ_Oneway)
2296 S += 'V';
2297}
2298
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002299void ASTContext::setBuiltinVaListType(QualType T)
2300{
2301 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2302
2303 BuiltinVaListType = T;
2304}
2305
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002306void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002307{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002308 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002309
2310 // typedef struct objc_object *id;
2311 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002312 // User error - caller will issue diagnostics.
2313 if (!ptr)
2314 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002315 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002316 // User error - caller will issue diagnostics.
2317 if (!rec)
2318 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002319 IdStructType = rec;
2320}
2321
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002322void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002323{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002324 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002325
2326 // typedef struct objc_selector *SEL;
2327 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002328 if (!ptr)
2329 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002330 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002331 if (!rec)
2332 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002333 SelStructType = rec;
2334}
2335
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002336void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002337{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002338 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002339}
2340
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002341void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002342{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002343 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002344
2345 // typedef struct objc_class *Class;
2346 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2347 assert(ptr && "'Class' incorrectly typed");
2348 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2349 assert(rec && "'Class' incorrectly typed");
2350 ClassStructType = rec;
2351}
2352
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002353void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2354 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002355 "'NSConstantString' type already set!");
2356
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002357 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002358}
2359
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002360/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002361/// TargetInfo, produce the corresponding type. The unsigned @p Type
2362/// is actually a value of type @c TargetInfo::IntType.
2363QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002364 switch (Type) {
2365 case TargetInfo::NoInt: return QualType();
2366 case TargetInfo::SignedShort: return ShortTy;
2367 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2368 case TargetInfo::SignedInt: return IntTy;
2369 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2370 case TargetInfo::SignedLong: return LongTy;
2371 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2372 case TargetInfo::SignedLongLong: return LongLongTy;
2373 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2374 }
2375
2376 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002377 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002378}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002379
2380//===----------------------------------------------------------------------===//
2381// Type Predicates.
2382//===----------------------------------------------------------------------===//
2383
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002384/// isObjCNSObjectType - Return true if this is an NSObject object using
2385/// NSObject attribute on a c-style pointer type.
2386/// FIXME - Make it work directly on types.
2387///
2388bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2389 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2390 if (TypedefDecl *TD = TDT->getDecl())
2391 if (TD->getAttr<ObjCNSObjectAttr>())
2392 return true;
2393 }
2394 return false;
2395}
2396
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002397/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2398/// to an object type. This includes "id" and "Class" (two 'special' pointers
2399/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2400/// ID type).
2401bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002402 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002403 return true;
2404
Steve Naroff6ae98502008-10-21 18:24:04 +00002405 // Blocks are objects.
2406 if (Ty->isBlockPointerType())
2407 return true;
2408
2409 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002410 if (!Ty->isPointerType())
2411 return false;
2412
2413 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2414 // pointer types. This looks for the typedef specifically, not for the
2415 // underlying type.
2416 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2417 return true;
2418
2419 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002420 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2421 return true;
2422
2423 // If is has NSObject attribute, OK as well.
2424 return isObjCNSObjectType(Ty);
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002425}
2426
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002427/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2428/// garbage collection attribute.
2429///
2430QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002431 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002432 if (getLangOptions().ObjC1 &&
2433 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002434 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002435 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002436 // (or pointers to them) be treated as though they were declared
2437 // as __strong.
2438 if (GCAttrs == QualType::GCNone) {
2439 if (isObjCObjectPointerType(Ty))
2440 GCAttrs = QualType::Strong;
2441 else if (Ty->isPointerType())
2442 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2443 }
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002444 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002445 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002446}
2447
Chris Lattner6ac46a42008-04-07 06:51:04 +00002448//===----------------------------------------------------------------------===//
2449// Type Compatibility Testing
2450//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002451
Steve Naroff1c7d0672008-09-04 15:10:53 +00002452/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002453/// block types. Types must be strictly compatible here. For example,
2454/// C unfortunately doesn't produce an error for the following:
2455///
2456/// int (*emptyArgFunc)();
2457/// int (*intArgList)(int) = emptyArgFunc;
2458///
2459/// For blocks, we will produce an error for the following (similar to C++):
2460///
2461/// int (^emptyArgBlock)();
2462/// int (^intArgBlock)(int) = emptyArgBlock;
2463///
2464/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2465///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002466bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002467 const FunctionType *lbase = lhs->getAsFunctionType();
2468 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002469 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2470 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Steve Naroffc0febd52008-12-10 17:49:55 +00002471 if (lproto && rproto)
2472 return !mergeTypes(lhs, rhs).isNull();
2473 return false;
Steve Naroff1c7d0672008-09-04 15:10:53 +00002474}
2475
Chris Lattner6ac46a42008-04-07 06:51:04 +00002476/// areCompatVectorTypes - Return true if the two specified vector types are
2477/// compatible.
2478static bool areCompatVectorTypes(const VectorType *LHS,
2479 const VectorType *RHS) {
2480 assert(LHS->isCanonical() && RHS->isCanonical());
2481 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002482 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002483}
2484
Eli Friedman3d815e72008-08-22 00:56:42 +00002485/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002486/// compatible for assignment from RHS to LHS. This handles validation of any
2487/// protocol qualifiers on the LHS or RHS.
2488///
Eli Friedman3d815e72008-08-22 00:56:42 +00002489bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2490 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002491 // Verify that the base decls are compatible: the RHS must be a subclass of
2492 // the LHS.
2493 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2494 return false;
2495
2496 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2497 // protocol qualified at all, then we are good.
2498 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2499 return true;
2500
2501 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2502 // isn't a superset.
2503 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2504 return true; // FIXME: should return false!
2505
2506 // Finally, we must have two protocol-qualified interfaces.
2507 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2508 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2509 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2510 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2511 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2512 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2513
2514 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2515 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2516 // LHS in a single parallel scan until we run out of elements in LHS.
2517 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2518 ObjCProtocolDecl *LHSProto = *LHSPI;
2519
2520 while (RHSPI != RHSPE) {
2521 ObjCProtocolDecl *RHSProto = *RHSPI++;
2522 // If the RHS has a protocol that the LHS doesn't, ignore it.
2523 if (RHSProto != LHSProto)
2524 continue;
2525
2526 // Otherwise, the RHS does have this element.
2527 ++LHSPI;
2528 if (LHSPI == LHSPE)
2529 return true; // All protocols in LHS exist in RHS.
2530
2531 LHSProto = *LHSPI;
2532 }
2533
2534 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2535 return false;
2536}
2537
Steve Naroff389bf462009-02-12 17:52:19 +00002538bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2539 // get the "pointed to" types
2540 const PointerType *LHSPT = LHS->getAsPointerType();
2541 const PointerType *RHSPT = RHS->getAsPointerType();
2542
2543 if (!LHSPT || !RHSPT)
2544 return false;
2545
2546 QualType lhptee = LHSPT->getPointeeType();
2547 QualType rhptee = RHSPT->getPointeeType();
2548 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2549 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2550 // ID acts sort of like void* for ObjC interfaces
2551 if (LHSIface && isObjCIdStructType(rhptee))
2552 return true;
2553 if (RHSIface && isObjCIdStructType(lhptee))
2554 return true;
2555 if (!LHSIface || !RHSIface)
2556 return false;
2557 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2558 canAssignObjCInterfaces(RHSIface, LHSIface);
2559}
2560
Steve Naroffec0550f2007-10-15 20:41:53 +00002561/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2562/// both shall have the identically qualified version of a compatible type.
2563/// C99 6.2.7p1: Two types have compatible types if their types are the
2564/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002565bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2566 return !mergeTypes(LHS, RHS).isNull();
2567}
2568
2569QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2570 const FunctionType *lbase = lhs->getAsFunctionType();
2571 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002572 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2573 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002574 bool allLTypes = true;
2575 bool allRTypes = true;
2576
2577 // Check return type
2578 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2579 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002580 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2581 allLTypes = false;
2582 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2583 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002584
2585 if (lproto && rproto) { // two C99 style function prototypes
2586 unsigned lproto_nargs = lproto->getNumArgs();
2587 unsigned rproto_nargs = rproto->getNumArgs();
2588
2589 // Compatible functions must have the same number of arguments
2590 if (lproto_nargs != rproto_nargs)
2591 return QualType();
2592
2593 // Variadic and non-variadic functions aren't compatible
2594 if (lproto->isVariadic() != rproto->isVariadic())
2595 return QualType();
2596
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002597 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2598 return QualType();
2599
Eli Friedman3d815e72008-08-22 00:56:42 +00002600 // Check argument compatibility
2601 llvm::SmallVector<QualType, 10> types;
2602 for (unsigned i = 0; i < lproto_nargs; i++) {
2603 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2604 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2605 QualType argtype = mergeTypes(largtype, rargtype);
2606 if (argtype.isNull()) return QualType();
2607 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002608 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2609 allLTypes = false;
2610 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2611 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002612 }
2613 if (allLTypes) return lhs;
2614 if (allRTypes) return rhs;
2615 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002616 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002617 }
2618
2619 if (lproto) allRTypes = false;
2620 if (rproto) allLTypes = false;
2621
Douglas Gregor72564e72009-02-26 23:50:07 +00002622 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002623 if (proto) {
2624 if (proto->isVariadic()) return QualType();
2625 // Check that the types are compatible with the types that
2626 // would result from default argument promotions (C99 6.7.5.3p15).
2627 // The only types actually affected are promotable integer
2628 // types and floats, which would be passed as a different
2629 // type depending on whether the prototype is visible.
2630 unsigned proto_nargs = proto->getNumArgs();
2631 for (unsigned i = 0; i < proto_nargs; ++i) {
2632 QualType argTy = proto->getArgType(i);
2633 if (argTy->isPromotableIntegerType() ||
2634 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2635 return QualType();
2636 }
2637
2638 if (allLTypes) return lhs;
2639 if (allRTypes) return rhs;
2640 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002641 proto->getNumArgs(), lproto->isVariadic(),
2642 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002643 }
2644
2645 if (allLTypes) return lhs;
2646 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002647 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002648}
2649
2650QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002651 // C++ [expr]: If an expression initially has the type "reference to T", the
2652 // type is adjusted to "T" prior to any further analysis, the expression
2653 // designates the object or function denoted by the reference, and the
2654 // expression is an lvalue.
Eli Friedman3d815e72008-08-22 00:56:42 +00002655 // FIXME: C++ shouldn't be going through here! The rules are different
2656 // enough that they should be handled separately.
2657 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002658 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002659 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002660 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002661
Eli Friedman3d815e72008-08-22 00:56:42 +00002662 QualType LHSCan = getCanonicalType(LHS),
2663 RHSCan = getCanonicalType(RHS);
2664
2665 // If two types are identical, they are compatible.
2666 if (LHSCan == RHSCan)
2667 return LHS;
2668
2669 // If the qualifiers are different, the types aren't compatible
2670 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2671 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2672 return QualType();
2673
2674 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2675 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2676
Chris Lattner1adb8832008-01-14 05:45:46 +00002677 // We want to consider the two function types to be the same for these
2678 // comparisons, just force one to the other.
2679 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2680 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002681
2682 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002683 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2684 LHSClass = Type::ConstantArray;
2685 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2686 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002687
Nate Begeman213541a2008-04-18 23:10:10 +00002688 // Canonicalize ExtVector -> Vector.
2689 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2690 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002691
Chris Lattnerb0489812008-04-07 06:38:24 +00002692 // Consider qualified interfaces and interfaces the same.
2693 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2694 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002695
Chris Lattnera36a61f2008-04-07 05:43:21 +00002696 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002697 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002698 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2699 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2700
2701 // ID acts sort of like void* for ObjC interfaces
2702 if (LHSIface && isObjCIdStructType(RHS))
2703 return LHS;
2704 if (RHSIface && isObjCIdStructType(LHS))
2705 return RHS;
2706
Steve Naroffbc76dd02008-12-10 22:14:21 +00002707 // ID is compatible with all qualified id types.
2708 if (LHS->isObjCQualifiedIdType()) {
2709 if (const PointerType *PT = RHS->getAsPointerType()) {
2710 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002711 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002712 return LHS;
2713 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2714 // Unfortunately, this API is part of Sema (which we don't have access
2715 // to. Need to refactor. The following check is insufficient, since we
2716 // need to make sure the class implements the protocol.
2717 if (pType->isObjCInterfaceType())
2718 return LHS;
2719 }
2720 }
2721 if (RHS->isObjCQualifiedIdType()) {
2722 if (const PointerType *PT = LHS->getAsPointerType()) {
2723 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002724 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002725 return RHS;
2726 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2727 // Unfortunately, this API is part of Sema (which we don't have access
2728 // to. Need to refactor. The following check is insufficient, since we
2729 // need to make sure the class implements the protocol.
2730 if (pType->isObjCInterfaceType())
2731 return RHS;
2732 }
2733 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002734 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2735 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002736 if (const EnumType* ETy = LHS->getAsEnumType()) {
2737 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2738 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002739 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002740 if (const EnumType* ETy = RHS->getAsEnumType()) {
2741 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2742 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002743 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002744
Eli Friedman3d815e72008-08-22 00:56:42 +00002745 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002746 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002747
Steve Naroff4a746782008-01-09 22:43:08 +00002748 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002749 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00002750#define TYPE(Class, Base)
2751#define ABSTRACT_TYPE(Class, Base)
2752#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2753#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2754#include "clang/AST/TypeNodes.def"
2755 assert(false && "Non-canonical and dependent types shouldn't get here");
2756 return QualType();
2757
2758 case Type::Reference:
2759 case Type::MemberPointer:
2760 assert(false && "C++ should never be in mergeTypes");
2761 return QualType();
2762
2763 case Type::IncompleteArray:
2764 case Type::VariableArray:
2765 case Type::FunctionProto:
2766 case Type::ExtVector:
2767 case Type::ObjCQualifiedInterface:
2768 assert(false && "Types are eliminated above");
2769 return QualType();
2770
Chris Lattner1adb8832008-01-14 05:45:46 +00002771 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002772 {
2773 // Merge two pointer types, while trying to preserve typedef info
2774 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2775 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2776 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2777 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002778 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2779 return LHS;
2780 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2781 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002782 return getPointerType(ResultType);
2783 }
Steve Naroffc0febd52008-12-10 17:49:55 +00002784 case Type::BlockPointer:
2785 {
2786 // Merge two block pointer types, while trying to preserve typedef info
2787 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2788 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2789 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2790 if (ResultType.isNull()) return QualType();
2791 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2792 return LHS;
2793 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2794 return RHS;
2795 return getBlockPointerType(ResultType);
2796 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002797 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002798 {
2799 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2800 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2801 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2802 return QualType();
2803
2804 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2805 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2806 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2807 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002808 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2809 return LHS;
2810 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2811 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002812 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2813 ArrayType::ArraySizeModifier(), 0);
2814 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2815 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002816 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2817 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00002818 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2819 return LHS;
2820 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2821 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002822 if (LVAT) {
2823 // FIXME: This isn't correct! But tricky to implement because
2824 // the array's size has to be the size of LHS, but the type
2825 // has to be different.
2826 return LHS;
2827 }
2828 if (RVAT) {
2829 // FIXME: This isn't correct! But tricky to implement because
2830 // the array's size has to be the size of RHS, but the type
2831 // has to be different.
2832 return RHS;
2833 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002834 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2835 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00002836 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002837 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002838 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002839 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00002840 case Type::Record:
2841 case Type::CXXRecord:
2842 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00002843 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00002844 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
2845 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002846 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002847 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002848 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002849 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00002850 case Type::Complex:
2851 // Distinct complex types are incompatible.
2852 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002853 case Type::Vector:
Eli Friedman3d815e72008-08-22 00:56:42 +00002854 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2855 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00002856 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00002857 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002858 // Check if the interfaces are assignment compatible.
2859 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2860 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2861 if (LHSIface && RHSIface &&
2862 canAssignObjCInterfaces(LHSIface, RHSIface))
2863 return LHS;
2864
Eli Friedman3d815e72008-08-22 00:56:42 +00002865 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00002866 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00002867 case Type::ObjCQualifiedId:
2868 // Distinct qualified id's are not compatible.
2869 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002870 }
Douglas Gregor72564e72009-02-26 23:50:07 +00002871
2872 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002873}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002874
Chris Lattner5426bf62008-04-07 07:01:58 +00002875//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00002876// Integer Predicates
2877//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00002878
Eli Friedmanad74a752008-06-28 06:23:08 +00002879unsigned ASTContext::getIntWidth(QualType T) {
2880 if (T == BoolTy)
2881 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002882 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
2883 return FWIT->getWidth();
2884 }
2885 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00002886 return (unsigned)getTypeSize(T);
2887}
2888
2889QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2890 assert(T->isSignedIntegerType() && "Unexpected type");
2891 if (const EnumType* ETy = T->getAsEnumType())
2892 T = ETy->getDecl()->getIntegerType();
2893 const BuiltinType* BTy = T->getAsBuiltinType();
2894 assert (BTy && "Unexpected signed integer type");
2895 switch (BTy->getKind()) {
2896 case BuiltinType::Char_S:
2897 case BuiltinType::SChar:
2898 return UnsignedCharTy;
2899 case BuiltinType::Short:
2900 return UnsignedShortTy;
2901 case BuiltinType::Int:
2902 return UnsignedIntTy;
2903 case BuiltinType::Long:
2904 return UnsignedLongTy;
2905 case BuiltinType::LongLong:
2906 return UnsignedLongLongTy;
2907 default:
2908 assert(0 && "Unexpected signed integer type");
2909 return QualType();
2910 }
2911}
2912
2913
2914//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00002915// Serialization Support
2916//===----------------------------------------------------------------------===//
2917
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002918/// Emit - Serialize an ASTContext object to Bitcode.
2919void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002920 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00002921 S.EmitRef(SourceMgr);
2922 S.EmitRef(Target);
2923 S.EmitRef(Idents);
2924 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002925
Ted Kremenekfee04522007-10-31 22:44:07 +00002926 // Emit the size of the type vector so that we can reserve that size
2927 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00002928 S.EmitInt(Types.size());
2929
Ted Kremenek03ed4402007-11-13 22:02:55 +00002930 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2931 I!=E;++I)
2932 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00002933
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002934 S.EmitOwnedPtr(TUDecl);
2935
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002936 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002937}
2938
Ted Kremenek0f84c002007-11-13 00:25:37 +00002939ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002940
2941 // Read the language options.
2942 LangOptions LOpts;
2943 LOpts.Read(D);
2944
Ted Kremenekfee04522007-10-31 22:44:07 +00002945 SourceManager &SM = D.ReadRef<SourceManager>();
2946 TargetInfo &t = D.ReadRef<TargetInfo>();
2947 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2948 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00002949
Ted Kremenekfee04522007-10-31 22:44:07 +00002950 unsigned size_reserve = D.ReadInt();
2951
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002952 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2953 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00002954
Ted Kremenek03ed4402007-11-13 22:02:55 +00002955 for (unsigned i = 0; i < size_reserve; ++i)
2956 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00002957
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002958 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2959
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002960 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00002961
2962 return A;
2963}