blob: d059201b5cdfd03dd55041687afc78947d596feb [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;
Steve Naroff6cc18962008-05-21 15:59:22 +000094 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 else if (isa<FunctionTypeNoProto>(T))
115 ++NumFunctionNP;
116 else if (isa<FunctionTypeProto>(T))
117 ++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;
137 else if (isa<TypeOfExpr>(T))
138 ++NumTypeOfExprs;
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);
167 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
168
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)+
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 NumFunctionP*sizeof(FunctionTypeProto)+
174 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000175 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
176 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
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);
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000293 uint64_t Width;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000294 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000295 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000296 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000297 case Type::FunctionNoProto:
298 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000299 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000300 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000301 case Type::VariableArray:
302 assert(0 && "VLAs not implemented yet!");
Douglas Gregor898574e2008-12-05 23:32:09 +0000303 case Type::DependentSizedArray:
304 assert(0 && "Dependently-sized arrays don't have a known size");
Steve Narofffb22d962007-08-30 01:06:46 +0000305 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000306 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000307
Chris Lattner98be4942008-03-05 18:54:05 +0000308 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000309 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000310 Align = EltInfo.second;
311 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000312 }
Nate Begeman213541a2008-04-18 23:10:10 +0000313 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000314 case Type::Vector: {
315 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000316 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000317 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000318 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000319 // If the alignment is not a power of 2, round up to the next power of 2.
320 // This happens for non-power-of-2 length vectors.
321 // FIXME: this should probably be a target property.
322 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000323 break;
324 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000325
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000326 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000327 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000328 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000329 case BuiltinType::Void:
330 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000331 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000332 Width = Target.getBoolWidth();
333 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000334 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000335 case BuiltinType::Char_S:
336 case BuiltinType::Char_U:
337 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000338 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000339 Width = Target.getCharWidth();
340 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000341 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000342 case BuiltinType::WChar:
343 Width = Target.getWCharWidth();
344 Align = Target.getWCharAlign();
345 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000346 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000347 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000348 Width = Target.getShortWidth();
349 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000350 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000351 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000352 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000353 Width = Target.getIntWidth();
354 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000355 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000356 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000357 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000358 Width = Target.getLongWidth();
359 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000360 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000361 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000362 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000363 Width = Target.getLongLongWidth();
364 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000365 break;
366 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000367 Width = Target.getFloatWidth();
368 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000369 break;
370 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000371 Width = Target.getDoubleWidth();
372 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000373 break;
374 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000375 Width = Target.getLongDoubleWidth();
376 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000377 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000378 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000379 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000380 case Type::FixedWidthInt:
381 // FIXME: This isn't precisely correct; the width/alignment should depend
382 // on the available types for the target
383 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000384 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000385 Align = Width;
386 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000387 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000388 // FIXME: Pointers into different addr spaces could have different sizes and
389 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000390 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000391 case Type::ObjCQualifiedId:
Eli Friedman4bdf0872009-02-22 04:02:33 +0000392 case Type::ObjCQualifiedClass:
Chris Lattner5426bf62008-04-07 07:01:58 +0000393 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000394 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000395 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000396 case Type::BlockPointer: {
397 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
398 Width = Target.getPointerWidth(AS);
399 Align = Target.getPointerAlign(AS);
400 break;
401 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000402 case Type::Pointer: {
403 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000404 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000405 Align = Target.getPointerAlign(AS);
406 break;
407 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000408 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000409 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000410 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000411 // FIXME: This is wrong for struct layout: a reference in a struct has
412 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000413 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000414 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000415 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000416 // the GCC ABI, where pointers to data are one pointer large, pointers to
417 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000418 // other compilers too, we need to delegate this completely to TargetInfo
419 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000420 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
421 unsigned AS = Pointee.getAddressSpace();
422 Width = Target.getPointerWidth(AS);
423 if (Pointee->isFunctionType())
424 Width *= 2;
425 Align = Target.getPointerAlign(AS);
426 // GCC aligns at single pointer width.
427 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000428 case Type::Complex: {
429 // Complex types have the same alignment as their elements, but twice the
430 // size.
431 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000432 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000433 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000434 Align = EltInfo.second;
435 break;
436 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000437 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000438 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000439 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
440 Width = Layout.getSize();
441 Align = Layout.getAlignment();
442 break;
443 }
Chris Lattner71763312008-04-06 22:05:18 +0000444 case Type::Tagged: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000445 const TagType *TT = cast<TagType>(T);
446
447 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000448 Width = 1;
449 Align = 1;
450 break;
451 }
452
Daniel Dunbar1d751182008-11-08 05:48:37 +0000453 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000454 return getTypeInfo(ET->getDecl()->getIntegerType());
455
Daniel Dunbar1d751182008-11-08 05:48:37 +0000456 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000457 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
458 Width = Layout.getSize();
459 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000460 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000461 }
Chris Lattner71763312008-04-06 22:05:18 +0000462 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000463
Chris Lattner464175b2007-07-18 17:52:12 +0000464 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000465 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000466}
467
Chris Lattner34ebde42009-01-27 18:08:34 +0000468/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
469/// type for the current target in bits. This can be different than the ABI
470/// alignment in cases where it is beneficial for performance to overalign
471/// a data type.
472unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
473 unsigned ABIAlign = getTypeAlign(T);
474
475 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000476 if (T->isSpecificBuiltinType(BuiltinType::Double))
477 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000478
479 return ABIAlign;
480}
481
482
Devang Patel8b277042008-06-04 21:22:16 +0000483/// LayoutField - Field layout.
484void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000485 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000486 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000487 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000488 uint64_t FieldOffset = IsUnion ? 0 : Size;
489 uint64_t FieldSize;
490 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000491
492 // FIXME: Should this override struct packing? Probably we want to
493 // take the minimum?
494 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
495 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000496
497 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
498 // TODO: Need to check this algorithm on other targets!
499 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000500 FieldSize =
501 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000502
503 std::pair<uint64_t, unsigned> FieldInfo =
504 Context.getTypeInfo(FD->getType());
505 uint64_t TypeSize = FieldInfo.first;
506
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000507 // Determine the alignment of this bitfield. The packing
508 // attributes define a maximum and the alignment attribute defines
509 // a minimum.
510 // FIXME: What is the right behavior when the specified alignment
511 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000512 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000513 if (FieldPacking)
514 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000515 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
516 FieldAlign = std::max(FieldAlign, AA->getAlignment());
517
518 // Check if we need to add padding to give the field the correct
519 // alignment.
520 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
521 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
522
523 // Padding members don't affect overall alignment
524 if (!FD->getIdentifier())
525 FieldAlign = 1;
526 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000527 if (FD->getType()->isIncompleteArrayType()) {
528 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000529 // query getTypeInfo about these, so we figure it out here.
530 // Flexible array members don't have any size, but they
531 // have to be aligned appropriately for their element type.
532 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000533 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000534 FieldAlign = Context.getTypeAlign(ATy->getElementType());
535 } else {
536 std::pair<uint64_t, unsigned> FieldInfo =
537 Context.getTypeInfo(FD->getType());
538 FieldSize = FieldInfo.first;
539 FieldAlign = FieldInfo.second;
540 }
541
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000542 // Determine the alignment of this bitfield. The packing
543 // attributes define a maximum and the alignment attribute defines
544 // a minimum. Additionally, the packing alignment must be at least
545 // a byte for non-bitfields.
546 //
547 // FIXME: What is the right behavior when the specified alignment
548 // is smaller than the specified packing?
549 if (FieldPacking)
550 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000551 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
552 FieldAlign = std::max(FieldAlign, AA->getAlignment());
553
554 // Round up the current record size to the field's alignment boundary.
555 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
556 }
557
558 // Place this field at the current location.
559 FieldOffsets[FieldNo] = FieldOffset;
560
561 // Reserve space for this field.
562 if (IsUnion) {
563 Size = std::max(Size, FieldSize);
564 } else {
565 Size = FieldOffset + FieldSize;
566 }
567
568 // Remember max struct/class alignment.
569 Alignment = std::max(Alignment, FieldAlign);
570}
571
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000572static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
573 std::vector<FieldDecl*> &Fields) {
574 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
575 if (SuperClass)
576 CollectObjCIvars(SuperClass, Fields);
577 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
578 E = OI->ivar_end(); I != E; ++I) {
579 ObjCIvarDecl *IVDecl = (*I);
580 if (!IVDecl->isInvalidDecl())
581 Fields.push_back(cast<FieldDecl>(IVDecl));
582 }
583}
584
585/// addRecordToClass - produces record info. for the class for its
586/// ivars and all those inherited.
587///
588const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
589{
590 const RecordDecl *&RD = ASTRecordForInterface[D];
591 if (RD)
592 return RD;
593 std::vector<FieldDecl*> RecFields;
594 CollectObjCIvars(D, RecFields);
595 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
596 D->getLocation(),
597 D->getIdentifier());
598 /// FIXME! Can do collection of ivars and adding to the record while
599 /// doing it.
600 for (unsigned int i = 0; i != RecFields.size(); i++) {
601 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
602 RecFields[i]->getLocation(),
603 RecFields[i]->getIdentifier(),
604 RecFields[i]->getType(),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000605 RecFields[i]->getBitWidth(), false);
Douglas Gregor482b77d2009-01-12 23:27:07 +0000606 NewRD->addDecl(Field);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000607 }
608 NewRD->completeDefinition(*this);
609 RD = NewRD;
610 return RD;
611}
Devang Patel44a3dde2008-06-04 21:54:36 +0000612
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000613/// setFieldDecl - maps a field for the given Ivar reference node.
614//
615void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
616 const ObjCIvarDecl *Ivar,
617 const ObjCIvarRefExpr *MRef) {
618 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
619 lookupFieldDeclForIvar(*this, Ivar);
620 ASTFieldForIvarRef[MRef] = FD;
621}
622
Chris Lattner61710852008-10-05 17:34:18 +0000623/// getASTObjcInterfaceLayout - Get or compute information about the layout of
624/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000625/// position information.
626const ASTRecordLayout &
627ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
628 // Look up this layout, if already laid out, return what we have.
629 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
630 if (Entry) return *Entry;
631
632 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
633 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000634 ASTRecordLayout *NewEntry = NULL;
635 unsigned FieldCount = D->ivar_size();
636 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
637 FieldCount++;
638 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
639 unsigned Alignment = SL.getAlignment();
640 uint64_t Size = SL.getSize();
641 NewEntry = new ASTRecordLayout(Size, Alignment);
642 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000643 // Super class is at the beginning of the layout.
644 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000645 } else {
646 NewEntry = new ASTRecordLayout();
647 NewEntry->InitializeLayout(FieldCount);
648 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000649 Entry = NewEntry;
650
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000651 unsigned StructPacking = 0;
652 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
653 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000654
655 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
656 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
657 AA->getAlignment()));
658
659 // Layout each ivar sequentially.
660 unsigned i = 0;
661 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
662 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
663 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000664 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000665 }
666
667 // Finally, round the size of the total struct up to the alignment of the
668 // struct itself.
669 NewEntry->FinalizeLayout();
670 return *NewEntry;
671}
672
Devang Patel88a981b2007-11-01 19:11:01 +0000673/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000674/// specified record (struct/union/class), which indicates its size and field
675/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000676const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000677 D = D->getDefinition(*this);
678 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000679
Chris Lattner464175b2007-07-18 17:52:12 +0000680 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000681 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000682 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000683
Devang Patel88a981b2007-11-01 19:11:01 +0000684 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
685 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
686 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000687 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000688
Douglas Gregore267ff32008-12-11 20:41:00 +0000689 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor44b43212008-12-11 16:49:14 +0000690 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000691 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000692
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000693 unsigned StructPacking = 0;
694 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
695 StructPacking = PA->getAlignment();
696
Eli Friedman4bd998b2008-05-30 09:31:38 +0000697 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000698 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
699 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000700
Eli Friedman4bd998b2008-05-30 09:31:38 +0000701 // Layout each field, for now, just sequentially, respecting alignment. In
702 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000703 unsigned FieldIdx = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000704 for (RecordDecl::field_iterator Field = D->field_begin(),
705 FieldEnd = D->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000706 Field != FieldEnd; (void)++Field, ++FieldIdx)
707 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000708
709 // Finally, round the size of the total struct up to the alignment of the
710 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000711 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000712 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000713}
714
Chris Lattnera7674d82007-07-13 22:13:22 +0000715//===----------------------------------------------------------------------===//
716// Type creation/memoization methods
717//===----------------------------------------------------------------------===//
718
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000719QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000720 QualType CanT = getCanonicalType(T);
721 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000722 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000723
724 // If we are composing extended qualifiers together, merge together into one
725 // ExtQualType node.
726 unsigned CVRQuals = T.getCVRQualifiers();
727 QualType::GCAttrTypes GCAttr = QualType::GCNone;
728 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000729
Chris Lattnerb7d25532009-02-18 22:53:11 +0000730 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
731 // If this type already has an address space specified, it cannot get
732 // another one.
733 assert(EQT->getAddressSpace() == 0 &&
734 "Type cannot be in multiple addr spaces!");
735 GCAttr = EQT->getObjCGCAttr();
736 TypeNode = EQT->getBaseType();
737 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000738
Chris Lattnerb7d25532009-02-18 22:53:11 +0000739 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000740 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000741 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000742 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000743 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000744 return QualType(EXTQy, CVRQuals);
745
Christopher Lambebb97e92008-02-04 02:31:56 +0000746 // If the base type isn't canonical, this won't be a canonical type either,
747 // so fill in the canonical type field.
748 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000749 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000750 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000751
Chris Lattnerb7d25532009-02-18 22:53:11 +0000752 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000753 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000754 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000755 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000756 ExtQualType *New =
757 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000758 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000759 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000760 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000761}
762
Chris Lattnerb7d25532009-02-18 22:53:11 +0000763QualType ASTContext::getObjCGCQualType(QualType T,
764 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000765 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000766 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000767 return T;
768
Chris Lattnerb7d25532009-02-18 22:53:11 +0000769 // If we are composing extended qualifiers together, merge together into one
770 // ExtQualType node.
771 unsigned CVRQuals = T.getCVRQualifiers();
772 Type *TypeNode = T.getTypePtr();
773 unsigned AddressSpace = 0;
774
775 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
776 // If this type already has an address space specified, it cannot get
777 // another one.
778 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
779 "Type cannot be in multiple addr spaces!");
780 AddressSpace = EQT->getAddressSpace();
781 TypeNode = EQT->getBaseType();
782 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000783
784 // Check if we've already instantiated an gc qual'd type of this type.
785 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000786 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000787 void *InsertPos = 0;
788 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000789 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000790
791 // If the base type isn't canonical, this won't be a canonical type either,
792 // so fill in the canonical type field.
793 QualType Canonical;
794 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000795 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000796
Chris Lattnerb7d25532009-02-18 22:53:11 +0000797 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000798 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
799 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
800 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000801 ExtQualType *New =
802 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000803 ExtQualTypes.InsertNode(New, InsertPos);
804 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000805 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000806}
Chris Lattnera7674d82007-07-13 22:13:22 +0000807
Reid Spencer5f016e22007-07-11 17:01:13 +0000808/// getComplexType - Return the uniqued reference to the type for a complex
809/// number with the specified element type.
810QualType ASTContext::getComplexType(QualType T) {
811 // Unique pointers, to guarantee there is only one pointer of a particular
812 // structure.
813 llvm::FoldingSetNodeID ID;
814 ComplexType::Profile(ID, T);
815
816 void *InsertPos = 0;
817 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
818 return QualType(CT, 0);
819
820 // If the pointee type isn't canonical, this won't be a canonical type either,
821 // so fill in the canonical type field.
822 QualType Canonical;
823 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000824 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000825
826 // Get the new insert position for the node we care about.
827 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000828 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 }
Steve Narofff83820b2009-01-27 22:08:43 +0000830 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000831 Types.push_back(New);
832 ComplexTypes.InsertNode(New, InsertPos);
833 return QualType(New, 0);
834}
835
Eli Friedmanf98aba32009-02-13 02:31:07 +0000836QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
837 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
838 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
839 FixedWidthIntType *&Entry = Map[Width];
840 if (!Entry)
841 Entry = new FixedWidthIntType(Width, Signed);
842 return QualType(Entry, 0);
843}
Reid Spencer5f016e22007-07-11 17:01:13 +0000844
845/// getPointerType - Return the uniqued reference to the type for a pointer to
846/// the specified type.
847QualType ASTContext::getPointerType(QualType T) {
848 // Unique pointers, to guarantee there is only one pointer of a particular
849 // structure.
850 llvm::FoldingSetNodeID ID;
851 PointerType::Profile(ID, T);
852
853 void *InsertPos = 0;
854 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
855 return QualType(PT, 0);
856
857 // If the pointee type isn't canonical, this won't be a canonical type either,
858 // so fill in the canonical type field.
859 QualType Canonical;
860 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000861 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000862
863 // Get the new insert position for the node we care about.
864 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000865 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 }
Steve Narofff83820b2009-01-27 22:08:43 +0000867 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000868 Types.push_back(New);
869 PointerTypes.InsertNode(New, InsertPos);
870 return QualType(New, 0);
871}
872
Steve Naroff5618bd42008-08-27 16:04:49 +0000873/// getBlockPointerType - Return the uniqued reference to the type for
874/// a pointer to the specified block.
875QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000876 assert(T->isFunctionType() && "block of function types only");
877 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000878 // structure.
879 llvm::FoldingSetNodeID ID;
880 BlockPointerType::Profile(ID, T);
881
882 void *InsertPos = 0;
883 if (BlockPointerType *PT =
884 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
885 return QualType(PT, 0);
886
Steve Naroff296e8d52008-08-28 19:20:44 +0000887 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000888 // type either so fill in the canonical type field.
889 QualType Canonical;
890 if (!T->isCanonical()) {
891 Canonical = getBlockPointerType(getCanonicalType(T));
892
893 // Get the new insert position for the node we care about.
894 BlockPointerType *NewIP =
895 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000896 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000897 }
Steve Narofff83820b2009-01-27 22:08:43 +0000898 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000899 Types.push_back(New);
900 BlockPointerTypes.InsertNode(New, InsertPos);
901 return QualType(New, 0);
902}
903
Reid Spencer5f016e22007-07-11 17:01:13 +0000904/// getReferenceType - Return the uniqued reference to the type for a reference
905/// to the specified type.
906QualType ASTContext::getReferenceType(QualType T) {
907 // Unique pointers, to guarantee there is only one pointer of a particular
908 // structure.
909 llvm::FoldingSetNodeID ID;
910 ReferenceType::Profile(ID, T);
911
912 void *InsertPos = 0;
913 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
914 return QualType(RT, 0);
915
916 // If the referencee type isn't canonical, this won't be a canonical type
917 // either, so fill in the canonical type field.
918 QualType Canonical;
919 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000920 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000921
922 // Get the new insert position for the node we care about.
923 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000924 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000925 }
926
Steve Narofff83820b2009-01-27 22:08:43 +0000927 ReferenceType *New = new (*this,8) ReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000928 Types.push_back(New);
929 ReferenceTypes.InsertNode(New, InsertPos);
930 return QualType(New, 0);
931}
932
Sebastian Redlf30208a2009-01-24 21:16:55 +0000933/// getMemberPointerType - Return the uniqued reference to the type for a
934/// member pointer to the specified type, in the specified class.
935QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
936{
937 // Unique pointers, to guarantee there is only one pointer of a particular
938 // structure.
939 llvm::FoldingSetNodeID ID;
940 MemberPointerType::Profile(ID, T, Cls);
941
942 void *InsertPos = 0;
943 if (MemberPointerType *PT =
944 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
945 return QualType(PT, 0);
946
947 // If the pointee or class type isn't canonical, this won't be a canonical
948 // type either, so fill in the canonical type field.
949 QualType Canonical;
950 if (!T->isCanonical()) {
951 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
952
953 // Get the new insert position for the node we care about.
954 MemberPointerType *NewIP =
955 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
956 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
957 }
Steve Narofff83820b2009-01-27 22:08:43 +0000958 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000959 Types.push_back(New);
960 MemberPointerTypes.InsertNode(New, InsertPos);
961 return QualType(New, 0);
962}
963
Steve Narofffb22d962007-08-30 01:06:46 +0000964/// getConstantArrayType - Return the unique reference to the type for an
965/// array of the specified element type.
966QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000967 const llvm::APInt &ArySize,
968 ArrayType::ArraySizeModifier ASM,
969 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000970 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +0000971 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000972
973 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000974 if (ConstantArrayType *ATP =
975 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000976 return QualType(ATP, 0);
977
978 // If the element type isn't canonical, this won't be a canonical type either,
979 // so fill in the canonical type field.
980 QualType Canonical;
981 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000982 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000983 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000984 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000985 ConstantArrayType *NewIP =
986 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000987 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000988 }
989
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000990 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +0000991 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000992 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 Types.push_back(New);
994 return QualType(New, 0);
995}
996
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000997/// getVariableArrayType - Returns a non-unique reference to the type for a
998/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000999QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1000 ArrayType::ArraySizeModifier ASM,
1001 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001002 // Since we don't unique expressions, it isn't possible to unique VLA's
1003 // that have an expression provided for their size.
1004
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001005 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001006 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001007
1008 VariableArrayTypes.push_back(New);
1009 Types.push_back(New);
1010 return QualType(New, 0);
1011}
1012
Douglas Gregor898574e2008-12-05 23:32:09 +00001013/// getDependentSizedArrayType - Returns a non-unique reference to
1014/// the type for a dependently-sized array of the specified element
1015/// type. FIXME: We will need these to be uniqued, or at least
1016/// comparable, at some point.
1017QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1018 ArrayType::ArraySizeModifier ASM,
1019 unsigned EltTypeQuals) {
1020 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1021 "Size must be type- or value-dependent!");
1022
1023 // Since we don't unique expressions, it isn't possible to unique
1024 // dependently-sized array types.
1025
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001026 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001027 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1028 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001029
1030 DependentSizedArrayTypes.push_back(New);
1031 Types.push_back(New);
1032 return QualType(New, 0);
1033}
1034
Eli Friedmanc5773c42008-02-15 18:16:39 +00001035QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1036 ArrayType::ArraySizeModifier ASM,
1037 unsigned EltTypeQuals) {
1038 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001039 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001040
1041 void *InsertPos = 0;
1042 if (IncompleteArrayType *ATP =
1043 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1044 return QualType(ATP, 0);
1045
1046 // If the element type isn't canonical, this won't be a canonical type
1047 // either, so fill in the canonical type field.
1048 QualType Canonical;
1049
1050 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001051 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001052 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001053
1054 // Get the new insert position for the node we care about.
1055 IncompleteArrayType *NewIP =
1056 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001057 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001058 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001059
Steve Narofff83820b2009-01-27 22:08:43 +00001060 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001061 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001062
1063 IncompleteArrayTypes.InsertNode(New, InsertPos);
1064 Types.push_back(New);
1065 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001066}
1067
Steve Naroff73322922007-07-18 18:00:27 +00001068/// getVectorType - Return the unique reference to a vector type of
1069/// the specified element type and size. VectorType must be a built-in type.
1070QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001071 BuiltinType *baseType;
1072
Chris Lattnerf52ab252008-04-06 22:59:24 +00001073 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001074 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001075
1076 // Check if we've already instantiated a vector of this type.
1077 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001078 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001079 void *InsertPos = 0;
1080 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1081 return QualType(VTP, 0);
1082
1083 // If the element type isn't canonical, this won't be a canonical type either,
1084 // so fill in the canonical type field.
1085 QualType Canonical;
1086 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001087 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001088
1089 // Get the new insert position for the node we care about.
1090 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001091 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001092 }
Steve Narofff83820b2009-01-27 22:08:43 +00001093 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001094 VectorTypes.InsertNode(New, InsertPos);
1095 Types.push_back(New);
1096 return QualType(New, 0);
1097}
1098
Nate Begeman213541a2008-04-18 23:10:10 +00001099/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001100/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001101QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001102 BuiltinType *baseType;
1103
Chris Lattnerf52ab252008-04-06 22:59:24 +00001104 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001105 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001106
1107 // Check if we've already instantiated a vector of this type.
1108 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001109 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001110 void *InsertPos = 0;
1111 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1112 return QualType(VTP, 0);
1113
1114 // If the element type isn't canonical, this won't be a canonical type either,
1115 // so fill in the canonical type field.
1116 QualType Canonical;
1117 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001118 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001119
1120 // Get the new insert position for the node we care about.
1121 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001122 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001123 }
Steve Narofff83820b2009-01-27 22:08:43 +00001124 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001125 VectorTypes.InsertNode(New, InsertPos);
1126 Types.push_back(New);
1127 return QualType(New, 0);
1128}
1129
Reid Spencer5f016e22007-07-11 17:01:13 +00001130/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
1131///
1132QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
1133 // Unique functions, to guarantee there is only one function of a particular
1134 // structure.
1135 llvm::FoldingSetNodeID ID;
1136 FunctionTypeNoProto::Profile(ID, ResultTy);
1137
1138 void *InsertPos = 0;
1139 if (FunctionTypeNoProto *FT =
1140 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
1141 return QualType(FT, 0);
1142
1143 QualType Canonical;
1144 if (!ResultTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001145 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001146
1147 // Get the new insert position for the node we care about.
1148 FunctionTypeNoProto *NewIP =
1149 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001150 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001151 }
1152
Steve Narofff83820b2009-01-27 22:08:43 +00001153 FunctionTypeNoProto *New =new(*this,8)FunctionTypeNoProto(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001154 Types.push_back(New);
Eli Friedman56cd7e32008-02-25 22:11:40 +00001155 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001156 return QualType(New, 0);
1157}
1158
1159/// getFunctionType - Return a normal function type with a typed argument
1160/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001161QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001162 unsigned NumArgs, bool isVariadic,
1163 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 // Unique functions, to guarantee there is only one function of a particular
1165 // structure.
1166 llvm::FoldingSetNodeID ID;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001167 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1168 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001169
1170 void *InsertPos = 0;
1171 if (FunctionTypeProto *FTP =
1172 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
1173 return QualType(FTP, 0);
1174
1175 // Determine whether the type being created is already canonical or not.
1176 bool isCanonical = ResultTy->isCanonical();
1177 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1178 if (!ArgArray[i]->isCanonical())
1179 isCanonical = false;
1180
1181 // If this type isn't canonical, get the canonical version of it.
1182 QualType Canonical;
1183 if (!isCanonical) {
1184 llvm::SmallVector<QualType, 16> CanonicalArgs;
1185 CanonicalArgs.reserve(NumArgs);
1186 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001187 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001188
Chris Lattnerf52ab252008-04-06 22:59:24 +00001189 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001190 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001191 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001192
1193 // Get the new insert position for the node we care about.
1194 FunctionTypeProto *NewIP =
1195 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001196 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001197 }
1198
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001199 // FunctionTypeProto objects are allocated with extra bytes after them
1200 // for a variable size array (for parameter types) at the end of them.
Reid Spencer5f016e22007-07-11 17:01:13 +00001201 FunctionTypeProto *FTP =
Steve Naroffc0ac4922009-01-27 23:20:32 +00001202 (FunctionTypeProto*)Allocate(sizeof(FunctionTypeProto) +
1203 NumArgs*sizeof(QualType), 8);
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001205 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 Types.push_back(FTP);
1207 FunctionTypeProtos.InsertNode(FTP, InsertPos);
1208 return QualType(FTP, 0);
1209}
1210
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001211/// getTypeDeclType - Return the unique reference to the type for the
1212/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001213QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001214 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001215 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1216
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001217 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001218 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001219 else if (isa<TemplateTypeParmDecl>(Decl)) {
1220 assert(false && "Template type parameter types are always available.");
1221 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001222 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001223
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001224 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001225 if (PrevDecl)
1226 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001227 else
1228 Decl->TypeForDecl = new (*this,8) CXXRecordType(CXXRecord);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001229 }
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001230 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001231 if (PrevDecl)
1232 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001233 else
1234 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001235 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001236 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1237 if (PrevDecl)
1238 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001239 else
1240 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001241 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001242 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001243 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001244
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001245 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001246 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001247}
1248
Reid Spencer5f016e22007-07-11 17:01:13 +00001249/// getTypedefType - Return the unique reference to the type for the
1250/// specified typename decl.
1251QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1252 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1253
Chris Lattnerf52ab252008-04-06 22:59:24 +00001254 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Steve Narofff83820b2009-01-27 22:08:43 +00001255 Decl->TypeForDecl = new(*this,8) TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 Types.push_back(Decl->TypeForDecl);
1257 return QualType(Decl->TypeForDecl, 0);
1258}
1259
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001260/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001261/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001262QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001263 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1264
Steve Narofff83820b2009-01-27 22:08:43 +00001265 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001266 Types.push_back(Decl->TypeForDecl);
1267 return QualType(Decl->TypeForDecl, 0);
1268}
1269
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00001270/// buildObjCInterfaceType - Returns a new type for the interface
1271/// declaration, regardless. It also removes any previously built
1272/// record declaration so caller can rebuild it.
1273QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) {
1274 const RecordDecl *&RD = ASTRecordForInterface[Decl];
1275 if (RD)
1276 RD = 0;
1277 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
1278 Types.push_back(Decl->TypeForDecl);
1279 return QualType(Decl->TypeForDecl, 0);
1280}
1281
Douglas Gregorfab9d672009-02-05 23:33:38 +00001282/// \brief Retrieve the template type parameter type for a template
1283/// parameter with the given depth, index, and (optionally) name.
1284QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1285 IdentifierInfo *Name) {
1286 llvm::FoldingSetNodeID ID;
1287 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1288 void *InsertPos = 0;
1289 TemplateTypeParmType *TypeParm
1290 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1291
1292 if (TypeParm)
1293 return QualType(TypeParm, 0);
1294
1295 if (Name)
1296 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1297 getTemplateTypeParmType(Depth, Index));
1298 else
1299 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1300
1301 Types.push_back(TypeParm);
1302 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1303
1304 return QualType(TypeParm, 0);
1305}
1306
Douglas Gregor55f6b142009-02-09 18:46:07 +00001307QualType
1308ASTContext::getClassTemplateSpecializationType(TemplateDecl *Template,
1309 unsigned NumArgs,
1310 uintptr_t *Args, bool *ArgIsType,
1311 QualType Canon) {
Douglas Gregorfc705b82009-02-26 22:19:44 +00001312 Canon = getCanonicalType(Canon);
1313
Douglas Gregor55f6b142009-02-09 18:46:07 +00001314 llvm::FoldingSetNodeID ID;
1315 ClassTemplateSpecializationType::Profile(ID, Template, NumArgs, Args,
1316 ArgIsType);
1317 void *InsertPos = 0;
1318 ClassTemplateSpecializationType *Spec
1319 = ClassTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
1320
1321 if (Spec)
1322 return QualType(Spec, 0);
1323
1324 void *Mem = Allocate(sizeof(ClassTemplateSpecializationType) +
1325 (sizeof(uintptr_t) *
1326 (ClassTemplateSpecializationType::
1327 getNumPackedWords(NumArgs) +
1328 NumArgs)), 8);
1329 Spec = new (Mem) ClassTemplateSpecializationType(Template, NumArgs, Args,
1330 ArgIsType, Canon);
1331 Types.push_back(Spec);
1332 ClassTemplateSpecializationTypes.InsertNode(Spec, InsertPos);
1333
1334 return QualType(Spec, 0);
1335}
1336
Chris Lattner88cb27a2008-04-07 04:56:42 +00001337/// CmpProtocolNames - Comparison predicate for sorting protocols
1338/// alphabetically.
1339static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1340 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001341 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001342}
1343
1344static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1345 unsigned &NumProtocols) {
1346 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1347
1348 // Sort protocols, keyed by name.
1349 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1350
1351 // Remove duplicates.
1352 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1353 NumProtocols = ProtocolsEnd-Protocols;
1354}
1355
1356
Chris Lattner065f0d72008-04-07 04:44:08 +00001357/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1358/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001359QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1360 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001361 // Sort the protocol list alphabetically to canonicalize it.
1362 SortAndUniqueProtocols(Protocols, NumProtocols);
1363
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001364 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001365 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001366
1367 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001368 if (ObjCQualifiedInterfaceType *QT =
1369 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001370 return QualType(QT, 0);
1371
1372 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001373 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001374 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001375
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001376 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001377 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001378 return QualType(QType, 0);
1379}
1380
Chris Lattner88cb27a2008-04-07 04:56:42 +00001381/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1382/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001383QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001384 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001385 // Sort the protocol list alphabetically to canonicalize it.
1386 SortAndUniqueProtocols(Protocols, NumProtocols);
1387
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001388 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001389 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001390
1391 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001392 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001393 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001394 return QualType(QT, 0);
1395
1396 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001397 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001398 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001399 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001400 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001401 return QualType(QType, 0);
1402}
1403
Steve Naroff9752f252007-08-01 18:02:17 +00001404/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1405/// TypeOfExpr AST's (since expression's are never shared). For example,
1406/// multiple declarations that refer to "typeof(x)" all contain different
1407/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1408/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +00001409QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001410 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Narofff83820b2009-01-27 22:08:43 +00001411 TypeOfExpr *toe = new (*this,8) TypeOfExpr(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001412 Types.push_back(toe);
1413 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001414}
1415
Steve Naroff9752f252007-08-01 18:02:17 +00001416/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1417/// TypeOfType AST's. The only motivation to unique these nodes would be
1418/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1419/// an issue. This doesn't effect the type checker, since it operates
1420/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001421QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001422 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001423 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001424 Types.push_back(tot);
1425 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001426}
1427
Reid Spencer5f016e22007-07-11 17:01:13 +00001428/// getTagDeclType - Return the unique reference to the type for the
1429/// specified TagDecl (struct/union/class/enum) decl.
1430QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001431 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001432 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001433}
1434
1435/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1436/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1437/// needs to agree with the definition in <stddef.h>.
1438QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001439 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001440}
1441
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001442/// getSignedWCharType - Return the type of "signed wchar_t".
1443/// Used when in C++, as a GCC extension.
1444QualType ASTContext::getSignedWCharType() const {
1445 // FIXME: derive from "Target" ?
1446 return WCharTy;
1447}
1448
1449/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1450/// Used when in C++, as a GCC extension.
1451QualType ASTContext::getUnsignedWCharType() const {
1452 // FIXME: derive from "Target" ?
1453 return UnsignedIntTy;
1454}
1455
Chris Lattner8b9023b2007-07-13 03:05:23 +00001456/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1457/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1458QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001459 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001460}
1461
Chris Lattnere6327742008-04-02 05:18:44 +00001462//===----------------------------------------------------------------------===//
1463// Type Operators
1464//===----------------------------------------------------------------------===//
1465
Chris Lattner77c96472008-04-06 22:41:35 +00001466/// getCanonicalType - Return the canonical (structural) type corresponding to
1467/// the specified potentially non-canonical type. The non-canonical version
1468/// of a type may have many "decorated" versions of types. Decorators can
1469/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1470/// to be free of any of these, allowing two canonical types to be compared
1471/// for exact equality with a simple pointer comparison.
1472QualType ASTContext::getCanonicalType(QualType T) {
1473 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001474
1475 // If the result has type qualifiers, make sure to canonicalize them as well.
1476 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1477 if (TypeQuals == 0) return CanType;
1478
1479 // If the type qualifiers are on an array type, get the canonical type of the
1480 // array with the qualifiers applied to the element type.
1481 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1482 if (!AT)
1483 return CanType.getQualifiedType(TypeQuals);
1484
1485 // Get the canonical version of the element with the extra qualifiers on it.
1486 // This can recursively sink qualifiers through multiple levels of arrays.
1487 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1488 NewEltTy = getCanonicalType(NewEltTy);
1489
1490 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1491 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1492 CAT->getIndexTypeQualifier());
1493 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1494 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1495 IAT->getIndexTypeQualifier());
1496
Douglas Gregor898574e2008-12-05 23:32:09 +00001497 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1498 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1499 DSAT->getSizeModifier(),
1500 DSAT->getIndexTypeQualifier());
1501
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001502 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1503 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1504 VAT->getSizeModifier(),
1505 VAT->getIndexTypeQualifier());
1506}
1507
1508
1509const ArrayType *ASTContext::getAsArrayType(QualType T) {
1510 // Handle the non-qualified case efficiently.
1511 if (T.getCVRQualifiers() == 0) {
1512 // Handle the common positive case fast.
1513 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1514 return AT;
1515 }
1516
1517 // Handle the common negative case fast, ignoring CVR qualifiers.
1518 QualType CType = T->getCanonicalTypeInternal();
1519
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001520 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001521 // test.
1522 if (!isa<ArrayType>(CType) &&
1523 !isa<ArrayType>(CType.getUnqualifiedType()))
1524 return 0;
1525
1526 // Apply any CVR qualifiers from the array type to the element type. This
1527 // implements C99 6.7.3p8: "If the specification of an array type includes
1528 // any type qualifiers, the element type is so qualified, not the array type."
1529
1530 // If we get here, we either have type qualifiers on the type, or we have
1531 // sugar such as a typedef in the way. If we have type qualifiers on the type
1532 // we must propagate them down into the elemeng type.
1533 unsigned CVRQuals = T.getCVRQualifiers();
1534 unsigned AddrSpace = 0;
1535 Type *Ty = T.getTypePtr();
1536
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001537 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001538 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001539 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1540 AddrSpace = EXTQT->getAddressSpace();
1541 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001542 } else {
1543 T = Ty->getDesugaredType();
1544 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1545 break;
1546 CVRQuals |= T.getCVRQualifiers();
1547 Ty = T.getTypePtr();
1548 }
1549 }
1550
1551 // If we have a simple case, just return now.
1552 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1553 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1554 return ATy;
1555
1556 // Otherwise, we have an array and we have qualifiers on it. Push the
1557 // qualifiers into the array element type and return a new array type.
1558 // Get the canonical version of the element with the extra qualifiers on it.
1559 // This can recursively sink qualifiers through multiple levels of arrays.
1560 QualType NewEltTy = ATy->getElementType();
1561 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001562 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001563 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1564
1565 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1566 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1567 CAT->getSizeModifier(),
1568 CAT->getIndexTypeQualifier()));
1569 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1570 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1571 IAT->getSizeModifier(),
1572 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001573
Douglas Gregor898574e2008-12-05 23:32:09 +00001574 if (const DependentSizedArrayType *DSAT
1575 = dyn_cast<DependentSizedArrayType>(ATy))
1576 return cast<ArrayType>(
1577 getDependentSizedArrayType(NewEltTy,
1578 DSAT->getSizeExpr(),
1579 DSAT->getSizeModifier(),
1580 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001581
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001582 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1583 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1584 VAT->getSizeModifier(),
1585 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001586}
1587
1588
Chris Lattnere6327742008-04-02 05:18:44 +00001589/// getArrayDecayedType - Return the properly qualified result of decaying the
1590/// specified array type to a pointer. This operation is non-trivial when
1591/// handling typedefs etc. The canonical type of "T" must be an array type,
1592/// this returns a pointer to a properly qualified element of the array.
1593///
1594/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1595QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001596 // Get the element type with 'getAsArrayType' so that we don't lose any
1597 // typedefs in the element type of the array. This also handles propagation
1598 // of type qualifiers from the array type into the element type if present
1599 // (C99 6.7.3p8).
1600 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1601 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001602
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001603 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001604
1605 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001606 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001607}
1608
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001609QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001610 QualType ElemTy = VAT->getElementType();
1611
1612 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1613 return getBaseElementType(VAT);
1614
1615 return ElemTy;
1616}
1617
Reid Spencer5f016e22007-07-11 17:01:13 +00001618/// getFloatingRank - Return a relative rank for floating point types.
1619/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001620static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001621 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001622 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001623
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001624 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001625 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001626 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001627 case BuiltinType::Float: return FloatRank;
1628 case BuiltinType::Double: return DoubleRank;
1629 case BuiltinType::LongDouble: return LongDoubleRank;
1630 }
1631}
1632
Steve Naroff716c7302007-08-27 01:41:48 +00001633/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1634/// point or a complex type (based on typeDomain/typeSize).
1635/// 'typeDomain' is a real floating point or complex type.
1636/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001637QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1638 QualType Domain) const {
1639 FloatingRank EltRank = getFloatingRank(Size);
1640 if (Domain->isComplexType()) {
1641 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001642 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001643 case FloatRank: return FloatComplexTy;
1644 case DoubleRank: return DoubleComplexTy;
1645 case LongDoubleRank: return LongDoubleComplexTy;
1646 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001647 }
Chris Lattner1361b112008-04-06 23:58:54 +00001648
1649 assert(Domain->isRealFloatingType() && "Unknown domain!");
1650 switch (EltRank) {
1651 default: assert(0 && "getFloatingRank(): illegal value for rank");
1652 case FloatRank: return FloatTy;
1653 case DoubleRank: return DoubleTy;
1654 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001655 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001656}
1657
Chris Lattner7cfeb082008-04-06 23:55:33 +00001658/// getFloatingTypeOrder - Compare the rank of the two specified floating
1659/// point types, ignoring the domain of the type (i.e. 'double' ==
1660/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1661/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001662int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1663 FloatingRank LHSR = getFloatingRank(LHS);
1664 FloatingRank RHSR = getFloatingRank(RHS);
1665
1666 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001667 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001668 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001669 return 1;
1670 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001671}
1672
Chris Lattnerf52ab252008-04-06 22:59:24 +00001673/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1674/// routine will assert if passed a built-in type that isn't an integer or enum,
1675/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001676unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001677 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001678 if (EnumType* ET = dyn_cast<EnumType>(T))
1679 T = ET->getDecl()->getIntegerType().getTypePtr();
1680
1681 // There are two things which impact the integer rank: the width, and
1682 // the ordering of builtins. The builtin ordering is encoded in the
1683 // bottom three bits; the width is encoded in the bits above that.
1684 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1685 return FWIT->getWidth() << 3;
1686 }
1687
Chris Lattnerf52ab252008-04-06 22:59:24 +00001688 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001689 default: assert(0 && "getIntegerRank(): not a built-in integer");
1690 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001691 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001692 case BuiltinType::Char_S:
1693 case BuiltinType::Char_U:
1694 case BuiltinType::SChar:
1695 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001696 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001697 case BuiltinType::Short:
1698 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001699 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001700 case BuiltinType::Int:
1701 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001702 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001703 case BuiltinType::Long:
1704 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001705 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001706 case BuiltinType::LongLong:
1707 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001708 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001709 }
1710}
1711
Chris Lattner7cfeb082008-04-06 23:55:33 +00001712/// getIntegerTypeOrder - Returns the highest ranked integer type:
1713/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1714/// LHS < RHS, return -1.
1715int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001716 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1717 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001718 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001719
Chris Lattnerf52ab252008-04-06 22:59:24 +00001720 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1721 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001722
Chris Lattner7cfeb082008-04-06 23:55:33 +00001723 unsigned LHSRank = getIntegerRank(LHSC);
1724 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001725
Chris Lattner7cfeb082008-04-06 23:55:33 +00001726 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1727 if (LHSRank == RHSRank) return 0;
1728 return LHSRank > RHSRank ? 1 : -1;
1729 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001730
Chris Lattner7cfeb082008-04-06 23:55:33 +00001731 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1732 if (LHSUnsigned) {
1733 // If the unsigned [LHS] type is larger, return it.
1734 if (LHSRank >= RHSRank)
1735 return 1;
1736
1737 // If the signed type can represent all values of the unsigned type, it
1738 // wins. Because we are dealing with 2's complement and types that are
1739 // powers of two larger than each other, this is always safe.
1740 return -1;
1741 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001742
Chris Lattner7cfeb082008-04-06 23:55:33 +00001743 // If the unsigned [RHS] type is larger, return it.
1744 if (RHSRank >= LHSRank)
1745 return -1;
1746
1747 // If the signed type can represent all values of the unsigned type, it
1748 // wins. Because we are dealing with 2's complement and types that are
1749 // powers of two larger than each other, this is always safe.
1750 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001751}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001752
1753// getCFConstantStringType - Return the type used for constant CFStrings.
1754QualType ASTContext::getCFConstantStringType() {
1755 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001756 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001757 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001758 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001759 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001760
1761 // const int *isa;
1762 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001763 // int flags;
1764 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001765 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001766 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001767 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001768 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001769
Anders Carlsson71993dd2007-08-17 05:31:46 +00001770 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001771 for (unsigned i = 0; i < 4; ++i) {
1772 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1773 SourceLocation(), 0,
1774 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001775 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001776 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001777 }
1778
1779 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001780 }
1781
1782 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001783}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001784
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001785QualType ASTContext::getObjCFastEnumerationStateType()
1786{
1787 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001788 ObjCFastEnumerationStateTypeDecl =
1789 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1790 &Idents.get("__objcFastEnumerationState"));
1791
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001792 QualType FieldTypes[] = {
1793 UnsignedLongTy,
1794 getPointerType(ObjCIdType),
1795 getPointerType(UnsignedLongTy),
1796 getConstantArrayType(UnsignedLongTy,
1797 llvm::APInt(32, 5), ArrayType::Normal, 0)
1798 };
1799
Douglas Gregor44b43212008-12-11 16:49:14 +00001800 for (size_t i = 0; i < 4; ++i) {
1801 FieldDecl *Field = FieldDecl::Create(*this,
1802 ObjCFastEnumerationStateTypeDecl,
1803 SourceLocation(), 0,
1804 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001805 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001806 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001807 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001808
Douglas Gregor44b43212008-12-11 16:49:14 +00001809 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001810 }
1811
1812 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1813}
1814
Anders Carlssone8c49532007-10-29 06:33:42 +00001815// This returns true if a type has been typedefed to BOOL:
1816// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001817static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001818 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00001819 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1820 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001821
1822 return false;
1823}
1824
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001825/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001826/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001827int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001828 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001829
1830 // Make all integer and enum types at least as large as an int
1831 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001832 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001833 // Treat arrays as pointers, since that's how they're passed in.
1834 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001835 sz = getTypeSize(VoidPtrTy);
1836 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001837}
1838
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001839/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001840/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001841void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001842 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001843 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001844 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001845 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001846 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001847 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001848 // Compute size of all parameters.
1849 // Start with computing size of a pointer in number of bytes.
1850 // FIXME: There might(should) be a better way of doing this computation!
1851 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001852 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001853 // The first two arguments (self and _cmd) are pointers; account for
1854 // their size.
1855 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00001856 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1857 E = Decl->param_end(); PI != E; ++PI) {
1858 QualType PType = (*PI)->getType();
1859 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001860 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001861 ParmOffset += sz;
1862 }
1863 S += llvm::utostr(ParmOffset);
1864 S += "@0:";
1865 S += llvm::utostr(PtrSize);
1866
1867 // Argument types.
1868 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00001869 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1870 E = Decl->param_end(); PI != E; ++PI) {
1871 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001872 QualType PType = PVDecl->getOriginalType();
1873 if (const ArrayType *AT =
1874 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1875 // Use array's original type only if it has known number of
1876 // elements.
1877 if (!dyn_cast<ConstantArrayType>(AT))
1878 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001879 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001880 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001881 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001882 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001883 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001884 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001885 }
1886}
1887
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001888/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001889/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001890/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1891/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001892/// Property attributes are stored as a comma-delimited C string. The simple
1893/// attributes readonly and bycopy are encoded as single characters. The
1894/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1895/// encoded as single characters, followed by an identifier. Property types
1896/// are also encoded as a parametrized attribute. The characters used to encode
1897/// these attributes are defined by the following enumeration:
1898/// @code
1899/// enum PropertyAttributes {
1900/// kPropertyReadOnly = 'R', // property is read-only.
1901/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1902/// kPropertyByref = '&', // property is a reference to the value last assigned
1903/// kPropertyDynamic = 'D', // property is dynamic
1904/// kPropertyGetter = 'G', // followed by getter selector name
1905/// kPropertySetter = 'S', // followed by setter selector name
1906/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1907/// kPropertyType = 't' // followed by old-style type encoding.
1908/// kPropertyWeak = 'W' // 'weak' property
1909/// kPropertyStrong = 'P' // property GC'able
1910/// kPropertyNonAtomic = 'N' // property non-atomic
1911/// };
1912/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001913void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1914 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001915 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001916 // Collect information from the property implementation decl(s).
1917 bool Dynamic = false;
1918 ObjCPropertyImplDecl *SynthesizePID = 0;
1919
1920 // FIXME: Duplicated code due to poor abstraction.
1921 if (Container) {
1922 if (const ObjCCategoryImplDecl *CID =
1923 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1924 for (ObjCCategoryImplDecl::propimpl_iterator
1925 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1926 ObjCPropertyImplDecl *PID = *i;
1927 if (PID->getPropertyDecl() == PD) {
1928 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1929 Dynamic = true;
1930 } else {
1931 SynthesizePID = PID;
1932 }
1933 }
1934 }
1935 } else {
Chris Lattner61710852008-10-05 17:34:18 +00001936 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001937 for (ObjCCategoryImplDecl::propimpl_iterator
1938 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1939 ObjCPropertyImplDecl *PID = *i;
1940 if (PID->getPropertyDecl() == PD) {
1941 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1942 Dynamic = true;
1943 } else {
1944 SynthesizePID = PID;
1945 }
1946 }
1947 }
1948 }
1949 }
1950
1951 // FIXME: This is not very efficient.
1952 S = "T";
1953
1954 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001955 // GCC has some special rules regarding encoding of properties which
1956 // closely resembles encoding of ivars.
1957 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
1958 true /* outermost type */,
1959 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001960
1961 if (PD->isReadOnly()) {
1962 S += ",R";
1963 } else {
1964 switch (PD->getSetterKind()) {
1965 case ObjCPropertyDecl::Assign: break;
1966 case ObjCPropertyDecl::Copy: S += ",C"; break;
1967 case ObjCPropertyDecl::Retain: S += ",&"; break;
1968 }
1969 }
1970
1971 // It really isn't clear at all what this means, since properties
1972 // are "dynamic by default".
1973 if (Dynamic)
1974 S += ",D";
1975
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001976 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
1977 S += ",N";
1978
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001979 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1980 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001981 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001982 }
1983
1984 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1985 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001986 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001987 }
1988
1989 if (SynthesizePID) {
1990 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1991 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00001992 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001993 }
1994
1995 // FIXME: OBJCGC: weak & strong
1996}
1997
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00001998/// getLegacyIntegralTypeEncoding -
1999/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002000/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002001/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2002///
2003void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2004 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2005 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002006 if (BT->getKind() == BuiltinType::ULong &&
2007 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002008 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002009 else
2010 if (BT->getKind() == BuiltinType::Long &&
2011 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002012 PointeeTy = IntTy;
2013 }
2014 }
2015}
2016
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002017void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002018 FieldDecl *Field) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002019 // We follow the behavior of gcc, expanding structures which are
2020 // directly pointed to, and expanding embedded structures. Note that
2021 // these rules are sufficient to prevent recursive encoding of the
2022 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002023 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2024 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002025}
2026
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002027static void EncodeBitField(const ASTContext *Context, std::string& S,
2028 FieldDecl *FD) {
2029 const Expr *E = FD->getBitWidth();
2030 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2031 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2032 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2033 S += 'b';
2034 S += llvm::utostr(N);
2035}
2036
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002037void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2038 bool ExpandPointedToStructures,
2039 bool ExpandStructures,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002040 FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002041 bool OutermostType,
2042 bool EncodingProperty) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00002043 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002044 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002045 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002046 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002047 else {
2048 char encoding;
2049 switch (BT->getKind()) {
2050 default: assert(0 && "Unhandled builtin type kind");
2051 case BuiltinType::Void: encoding = 'v'; break;
2052 case BuiltinType::Bool: encoding = 'B'; break;
2053 case BuiltinType::Char_U:
2054 case BuiltinType::UChar: encoding = 'C'; break;
2055 case BuiltinType::UShort: encoding = 'S'; break;
2056 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002057 case BuiltinType::ULong:
2058 encoding =
2059 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2060 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002061 case BuiltinType::ULongLong: encoding = 'Q'; break;
2062 case BuiltinType::Char_S:
2063 case BuiltinType::SChar: encoding = 'c'; break;
2064 case BuiltinType::Short: encoding = 's'; break;
2065 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002066 case BuiltinType::Long:
2067 encoding =
2068 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2069 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002070 case BuiltinType::LongLong: encoding = 'q'; break;
2071 case BuiltinType::Float: encoding = 'f'; break;
2072 case BuiltinType::Double: encoding = 'd'; break;
2073 case BuiltinType::LongDouble: encoding = 'd'; break;
2074 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002075
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002076 S += encoding;
2077 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002078 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002079 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002080 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2081 ExpandPointedToStructures,
2082 ExpandStructures, FD);
2083 if (FD || EncodingProperty) {
2084 // Note that we do extended encoding of protocol qualifer list
2085 // Only when doing ivar or property encoding.
2086 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2087 S += '"';
2088 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2089 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2090 S += '<';
2091 S += Proto->getNameAsString();
2092 S += '>';
2093 }
2094 S += '"';
2095 }
2096 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002097 }
2098 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002099 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002100 bool isReadOnly = false;
2101 // For historical/compatibility reasons, the read-only qualifier of the
2102 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2103 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2104 // Also, do not emit the 'r' for anything but the outermost type!
2105 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2106 if (OutermostType && T.isConstQualified()) {
2107 isReadOnly = true;
2108 S += 'r';
2109 }
2110 }
2111 else if (OutermostType) {
2112 QualType P = PointeeTy;
2113 while (P->getAsPointerType())
2114 P = P->getAsPointerType()->getPointeeType();
2115 if (P.isConstQualified()) {
2116 isReadOnly = true;
2117 S += 'r';
2118 }
2119 }
2120 if (isReadOnly) {
2121 // Another legacy compatibility encoding. Some ObjC qualifier and type
2122 // combinations need to be rearranged.
2123 // Rewrite "in const" from "nr" to "rn"
2124 const char * s = S.c_str();
2125 int len = S.length();
2126 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2127 std::string replace = "rn";
2128 S.replace(S.end()-2, S.end(), replace);
2129 }
2130 }
Steve Naroff389bf462009-02-12 17:52:19 +00002131 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002132 S += '@';
2133 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002134 }
2135 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002136 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002137 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002138 // Another historical/compatibility reason.
2139 // We encode the underlying type which comes out as
2140 // {...};
2141 S += '^';
2142 getObjCEncodingForTypeImpl(PointeeTy, S,
2143 false, ExpandPointedToStructures,
2144 NULL);
2145 return;
2146 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002147 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002148 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002149 const ObjCInterfaceType *OIT =
2150 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002151 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002152 S += '"';
2153 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002154 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2155 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2156 S += '<';
2157 S += Proto->getNameAsString();
2158 S += '>';
2159 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002160 S += '"';
2161 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002162 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002163 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002164 S += '#';
2165 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002166 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002167 S += ':';
2168 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002169 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002170
2171 if (PointeeTy->isCharType()) {
2172 // char pointer types should be encoded as '*' unless it is a
2173 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002174 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002175 S += '*';
2176 return;
2177 }
2178 }
2179
2180 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002181 getLegacyIntegralTypeEncoding(PointeeTy);
2182
2183 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002184 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002185 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002186 } else if (const ArrayType *AT =
2187 // Ignore type qualifiers etc.
2188 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002189 if (isa<IncompleteArrayType>(AT)) {
2190 // Incomplete arrays are encoded as a pointer to the array element.
2191 S += '^';
2192
2193 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2194 false, ExpandStructures, FD);
2195 } else {
2196 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002197
Anders Carlsson559a8332009-02-22 01:38:57 +00002198 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2199 S += llvm::utostr(CAT->getSize().getZExtValue());
2200 else {
2201 //Variable length arrays are encoded as a regular array with 0 elements.
2202 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2203 S += '0';
2204 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002205
Anders Carlsson559a8332009-02-22 01:38:57 +00002206 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2207 false, ExpandStructures, FD);
2208 S += ']';
2209 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002210 } else if (T->getAsFunctionType()) {
2211 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002212 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002213 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002214 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002215 // Anonymous structures print as '?'
2216 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2217 S += II->getName();
2218 } else {
2219 S += '?';
2220 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002221 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002222 S += '=';
Douglas Gregor44b43212008-12-11 16:49:14 +00002223 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2224 FieldEnd = RDecl->field_end();
2225 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002226 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002227 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002228 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002229 S += '"';
2230 }
2231
2232 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002233 if (Field->isBitField()) {
2234 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2235 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002236 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002237 QualType qt = Field->getType();
2238 getLegacyIntegralTypeEncoding(qt);
2239 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002240 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002241 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002242 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002243 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002244 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002245 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002246 if (FD && FD->isBitField())
2247 EncodeBitField(this, S, FD);
2248 else
2249 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002250 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002251 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002252 } else if (T->isObjCInterfaceType()) {
2253 // @encode(class_name)
2254 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2255 S += '{';
2256 const IdentifierInfo *II = OI->getIdentifier();
2257 S += II->getName();
2258 S += '=';
2259 std::vector<FieldDecl*> RecFields;
2260 CollectObjCIvars(OI, RecFields);
2261 for (unsigned int i = 0; i != RecFields.size(); i++) {
2262 if (RecFields[i]->isBitField())
2263 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2264 RecFields[i]);
2265 else
2266 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2267 FD);
2268 }
2269 S += '}';
2270 }
2271 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002272 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002273}
2274
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002275void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002276 std::string& S) const {
2277 if (QT & Decl::OBJC_TQ_In)
2278 S += 'n';
2279 if (QT & Decl::OBJC_TQ_Inout)
2280 S += 'N';
2281 if (QT & Decl::OBJC_TQ_Out)
2282 S += 'o';
2283 if (QT & Decl::OBJC_TQ_Bycopy)
2284 S += 'O';
2285 if (QT & Decl::OBJC_TQ_Byref)
2286 S += 'R';
2287 if (QT & Decl::OBJC_TQ_Oneway)
2288 S += 'V';
2289}
2290
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002291void ASTContext::setBuiltinVaListType(QualType T)
2292{
2293 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2294
2295 BuiltinVaListType = T;
2296}
2297
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002298void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002299{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002300 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002301
2302 // typedef struct objc_object *id;
2303 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002304 // User error - caller will issue diagnostics.
2305 if (!ptr)
2306 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002307 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002308 // User error - caller will issue diagnostics.
2309 if (!rec)
2310 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002311 IdStructType = rec;
2312}
2313
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002314void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002315{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002316 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002317
2318 // typedef struct objc_selector *SEL;
2319 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002320 if (!ptr)
2321 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002322 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002323 if (!rec)
2324 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002325 SelStructType = rec;
2326}
2327
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002328void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002329{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002330 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002331}
2332
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002333void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002334{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002335 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002336
2337 // typedef struct objc_class *Class;
2338 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2339 assert(ptr && "'Class' incorrectly typed");
2340 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2341 assert(rec && "'Class' incorrectly typed");
2342 ClassStructType = rec;
2343}
2344
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002345void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2346 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002347 "'NSConstantString' type already set!");
2348
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002349 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002350}
2351
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002352/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002353/// TargetInfo, produce the corresponding type. The unsigned @p Type
2354/// is actually a value of type @c TargetInfo::IntType.
2355QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002356 switch (Type) {
2357 case TargetInfo::NoInt: return QualType();
2358 case TargetInfo::SignedShort: return ShortTy;
2359 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2360 case TargetInfo::SignedInt: return IntTy;
2361 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2362 case TargetInfo::SignedLong: return LongTy;
2363 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2364 case TargetInfo::SignedLongLong: return LongLongTy;
2365 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2366 }
2367
2368 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002369 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002370}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002371
2372//===----------------------------------------------------------------------===//
2373// Type Predicates.
2374//===----------------------------------------------------------------------===//
2375
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002376/// isObjCNSObjectType - Return true if this is an NSObject object using
2377/// NSObject attribute on a c-style pointer type.
2378/// FIXME - Make it work directly on types.
2379///
2380bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2381 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2382 if (TypedefDecl *TD = TDT->getDecl())
2383 if (TD->getAttr<ObjCNSObjectAttr>())
2384 return true;
2385 }
2386 return false;
2387}
2388
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002389/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2390/// to an object type. This includes "id" and "Class" (two 'special' pointers
2391/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2392/// ID type).
2393bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002394 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002395 return true;
2396
Steve Naroff6ae98502008-10-21 18:24:04 +00002397 // Blocks are objects.
2398 if (Ty->isBlockPointerType())
2399 return true;
2400
2401 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002402 if (!Ty->isPointerType())
2403 return false;
2404
2405 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2406 // pointer types. This looks for the typedef specifically, not for the
2407 // underlying type.
2408 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2409 return true;
2410
2411 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002412 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2413 return true;
2414
2415 // If is has NSObject attribute, OK as well.
2416 return isObjCNSObjectType(Ty);
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002417}
2418
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002419/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2420/// garbage collection attribute.
2421///
2422QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002423 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002424 if (getLangOptions().ObjC1 &&
2425 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002426 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002427 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002428 // (or pointers to them) be treated as though they were declared
2429 // as __strong.
2430 if (GCAttrs == QualType::GCNone) {
2431 if (isObjCObjectPointerType(Ty))
2432 GCAttrs = QualType::Strong;
2433 else if (Ty->isPointerType())
2434 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2435 }
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002436 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002437 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002438}
2439
Chris Lattner6ac46a42008-04-07 06:51:04 +00002440//===----------------------------------------------------------------------===//
2441// Type Compatibility Testing
2442//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002443
Steve Naroff1c7d0672008-09-04 15:10:53 +00002444/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002445/// block types. Types must be strictly compatible here. For example,
2446/// C unfortunately doesn't produce an error for the following:
2447///
2448/// int (*emptyArgFunc)();
2449/// int (*intArgList)(int) = emptyArgFunc;
2450///
2451/// For blocks, we will produce an error for the following (similar to C++):
2452///
2453/// int (^emptyArgBlock)();
2454/// int (^intArgBlock)(int) = emptyArgBlock;
2455///
2456/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2457///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002458bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002459 const FunctionType *lbase = lhs->getAsFunctionType();
2460 const FunctionType *rbase = rhs->getAsFunctionType();
2461 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2462 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2463 if (lproto && rproto)
2464 return !mergeTypes(lhs, rhs).isNull();
2465 return false;
Steve Naroff1c7d0672008-09-04 15:10:53 +00002466}
2467
Chris Lattner6ac46a42008-04-07 06:51:04 +00002468/// areCompatVectorTypes - Return true if the two specified vector types are
2469/// compatible.
2470static bool areCompatVectorTypes(const VectorType *LHS,
2471 const VectorType *RHS) {
2472 assert(LHS->isCanonical() && RHS->isCanonical());
2473 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002474 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002475}
2476
Eli Friedman3d815e72008-08-22 00:56:42 +00002477/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002478/// compatible for assignment from RHS to LHS. This handles validation of any
2479/// protocol qualifiers on the LHS or RHS.
2480///
Eli Friedman3d815e72008-08-22 00:56:42 +00002481bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2482 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002483 // Verify that the base decls are compatible: the RHS must be a subclass of
2484 // the LHS.
2485 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2486 return false;
2487
2488 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2489 // protocol qualified at all, then we are good.
2490 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2491 return true;
2492
2493 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2494 // isn't a superset.
2495 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2496 return true; // FIXME: should return false!
2497
2498 // Finally, we must have two protocol-qualified interfaces.
2499 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2500 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2501 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2502 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2503 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2504 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2505
2506 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2507 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2508 // LHS in a single parallel scan until we run out of elements in LHS.
2509 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2510 ObjCProtocolDecl *LHSProto = *LHSPI;
2511
2512 while (RHSPI != RHSPE) {
2513 ObjCProtocolDecl *RHSProto = *RHSPI++;
2514 // If the RHS has a protocol that the LHS doesn't, ignore it.
2515 if (RHSProto != LHSProto)
2516 continue;
2517
2518 // Otherwise, the RHS does have this element.
2519 ++LHSPI;
2520 if (LHSPI == LHSPE)
2521 return true; // All protocols in LHS exist in RHS.
2522
2523 LHSProto = *LHSPI;
2524 }
2525
2526 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2527 return false;
2528}
2529
Steve Naroff389bf462009-02-12 17:52:19 +00002530bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2531 // get the "pointed to" types
2532 const PointerType *LHSPT = LHS->getAsPointerType();
2533 const PointerType *RHSPT = RHS->getAsPointerType();
2534
2535 if (!LHSPT || !RHSPT)
2536 return false;
2537
2538 QualType lhptee = LHSPT->getPointeeType();
2539 QualType rhptee = RHSPT->getPointeeType();
2540 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2541 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2542 // ID acts sort of like void* for ObjC interfaces
2543 if (LHSIface && isObjCIdStructType(rhptee))
2544 return true;
2545 if (RHSIface && isObjCIdStructType(lhptee))
2546 return true;
2547 if (!LHSIface || !RHSIface)
2548 return false;
2549 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2550 canAssignObjCInterfaces(RHSIface, LHSIface);
2551}
2552
Steve Naroffec0550f2007-10-15 20:41:53 +00002553/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2554/// both shall have the identically qualified version of a compatible type.
2555/// C99 6.2.7p1: Two types have compatible types if their types are the
2556/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002557bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2558 return !mergeTypes(LHS, RHS).isNull();
2559}
2560
2561QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2562 const FunctionType *lbase = lhs->getAsFunctionType();
2563 const FunctionType *rbase = rhs->getAsFunctionType();
2564 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2565 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2566 bool allLTypes = true;
2567 bool allRTypes = true;
2568
2569 // Check return type
2570 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2571 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002572 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2573 allLTypes = false;
2574 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2575 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002576
2577 if (lproto && rproto) { // two C99 style function prototypes
2578 unsigned lproto_nargs = lproto->getNumArgs();
2579 unsigned rproto_nargs = rproto->getNumArgs();
2580
2581 // Compatible functions must have the same number of arguments
2582 if (lproto_nargs != rproto_nargs)
2583 return QualType();
2584
2585 // Variadic and non-variadic functions aren't compatible
2586 if (lproto->isVariadic() != rproto->isVariadic())
2587 return QualType();
2588
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002589 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2590 return QualType();
2591
Eli Friedman3d815e72008-08-22 00:56:42 +00002592 // Check argument compatibility
2593 llvm::SmallVector<QualType, 10> types;
2594 for (unsigned i = 0; i < lproto_nargs; i++) {
2595 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2596 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2597 QualType argtype = mergeTypes(largtype, rargtype);
2598 if (argtype.isNull()) return QualType();
2599 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002600 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2601 allLTypes = false;
2602 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2603 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002604 }
2605 if (allLTypes) return lhs;
2606 if (allRTypes) return rhs;
2607 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002608 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002609 }
2610
2611 if (lproto) allRTypes = false;
2612 if (rproto) allLTypes = false;
2613
2614 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2615 if (proto) {
2616 if (proto->isVariadic()) return QualType();
2617 // Check that the types are compatible with the types that
2618 // would result from default argument promotions (C99 6.7.5.3p15).
2619 // The only types actually affected are promotable integer
2620 // types and floats, which would be passed as a different
2621 // type depending on whether the prototype is visible.
2622 unsigned proto_nargs = proto->getNumArgs();
2623 for (unsigned i = 0; i < proto_nargs; ++i) {
2624 QualType argTy = proto->getArgType(i);
2625 if (argTy->isPromotableIntegerType() ||
2626 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2627 return QualType();
2628 }
2629
2630 if (allLTypes) return lhs;
2631 if (allRTypes) return rhs;
2632 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002633 proto->getNumArgs(), lproto->isVariadic(),
2634 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002635 }
2636
2637 if (allLTypes) return lhs;
2638 if (allRTypes) return rhs;
2639 return getFunctionTypeNoProto(retType);
2640}
2641
2642QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002643 // C++ [expr]: If an expression initially has the type "reference to T", the
2644 // type is adjusted to "T" prior to any further analysis, the expression
2645 // designates the object or function denoted by the reference, and the
2646 // expression is an lvalue.
Eli Friedman3d815e72008-08-22 00:56:42 +00002647 // FIXME: C++ shouldn't be going through here! The rules are different
2648 // enough that they should be handled separately.
2649 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002650 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002651 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002652 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002653
Eli Friedman3d815e72008-08-22 00:56:42 +00002654 QualType LHSCan = getCanonicalType(LHS),
2655 RHSCan = getCanonicalType(RHS);
2656
2657 // If two types are identical, they are compatible.
2658 if (LHSCan == RHSCan)
2659 return LHS;
2660
2661 // If the qualifiers are different, the types aren't compatible
2662 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2663 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2664 return QualType();
2665
2666 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2667 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2668
Chris Lattner1adb8832008-01-14 05:45:46 +00002669 // We want to consider the two function types to be the same for these
2670 // comparisons, just force one to the other.
2671 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2672 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002673
2674 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002675 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2676 LHSClass = Type::ConstantArray;
2677 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2678 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002679
Nate Begeman213541a2008-04-18 23:10:10 +00002680 // Canonicalize ExtVector -> Vector.
2681 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2682 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002683
Chris Lattnerb0489812008-04-07 06:38:24 +00002684 // Consider qualified interfaces and interfaces the same.
2685 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2686 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002687
Chris Lattnera36a61f2008-04-07 05:43:21 +00002688 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002689 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002690 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2691 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2692
2693 // ID acts sort of like void* for ObjC interfaces
2694 if (LHSIface && isObjCIdStructType(RHS))
2695 return LHS;
2696 if (RHSIface && isObjCIdStructType(LHS))
2697 return RHS;
2698
Steve Naroffbc76dd02008-12-10 22:14:21 +00002699 // ID is compatible with all qualified id types.
2700 if (LHS->isObjCQualifiedIdType()) {
2701 if (const PointerType *PT = RHS->getAsPointerType()) {
2702 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002703 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002704 return LHS;
2705 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2706 // Unfortunately, this API is part of Sema (which we don't have access
2707 // to. Need to refactor. The following check is insufficient, since we
2708 // need to make sure the class implements the protocol.
2709 if (pType->isObjCInterfaceType())
2710 return LHS;
2711 }
2712 }
2713 if (RHS->isObjCQualifiedIdType()) {
2714 if (const PointerType *PT = LHS->getAsPointerType()) {
2715 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002716 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002717 return RHS;
2718 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2719 // Unfortunately, this API is part of Sema (which we don't have access
2720 // to. Need to refactor. The following check is insufficient, since we
2721 // need to make sure the class implements the protocol.
2722 if (pType->isObjCInterfaceType())
2723 return RHS;
2724 }
2725 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002726 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2727 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002728 if (const EnumType* ETy = LHS->getAsEnumType()) {
2729 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2730 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002731 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002732 if (const EnumType* ETy = RHS->getAsEnumType()) {
2733 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2734 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002735 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002736
Eli Friedman3d815e72008-08-22 00:56:42 +00002737 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002738 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002739
Steve Naroff4a746782008-01-09 22:43:08 +00002740 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002741 switch (LHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00002742 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002743 {
2744 // Merge two pointer types, while trying to preserve typedef info
2745 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2746 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2747 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2748 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002749 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2750 return LHS;
2751 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2752 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002753 return getPointerType(ResultType);
2754 }
Steve Naroffc0febd52008-12-10 17:49:55 +00002755 case Type::BlockPointer:
2756 {
2757 // Merge two block pointer types, while trying to preserve typedef info
2758 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2759 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2760 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2761 if (ResultType.isNull()) return QualType();
2762 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2763 return LHS;
2764 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2765 return RHS;
2766 return getBlockPointerType(ResultType);
2767 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002768 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002769 {
2770 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2771 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2772 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2773 return QualType();
2774
2775 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2776 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2777 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2778 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002779 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2780 return LHS;
2781 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2782 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002783 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2784 ArrayType::ArraySizeModifier(), 0);
2785 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2786 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002787 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2788 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00002789 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2790 return LHS;
2791 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2792 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002793 if (LVAT) {
2794 // FIXME: This isn't correct! But tricky to implement because
2795 // the array's size has to be the size of LHS, but the type
2796 // has to be different.
2797 return LHS;
2798 }
2799 if (RVAT) {
2800 // FIXME: This isn't correct! But tricky to implement because
2801 // the array's size has to be the size of RHS, but the type
2802 // has to be different.
2803 return RHS;
2804 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002805 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2806 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00002807 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002808 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002809 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002810 return mergeFunctionTypes(LHS, RHS);
2811 case Type::Tagged:
Eli Friedman3d815e72008-08-22 00:56:42 +00002812 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00002813 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
2814 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002815 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002816 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002817 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002818 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00002819 case Type::Complex:
2820 // Distinct complex types are incompatible.
2821 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002822 case Type::Vector:
Eli Friedman3d815e72008-08-22 00:56:42 +00002823 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2824 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00002825 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00002826 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002827 // Check if the interfaces are assignment compatible.
2828 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2829 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2830 if (LHSIface && RHSIface &&
2831 canAssignObjCInterfaces(LHSIface, RHSIface))
2832 return LHS;
2833
Eli Friedman3d815e72008-08-22 00:56:42 +00002834 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00002835 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00002836 case Type::ObjCQualifiedId:
2837 // Distinct qualified id's are not compatible.
2838 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002839 default:
2840 assert(0 && "unexpected type");
Eli Friedman3d815e72008-08-22 00:56:42 +00002841 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002842 }
Steve Naroffec0550f2007-10-15 20:41:53 +00002843}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002844
Chris Lattner5426bf62008-04-07 07:01:58 +00002845//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00002846// Integer Predicates
2847//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00002848
Eli Friedmanad74a752008-06-28 06:23:08 +00002849unsigned ASTContext::getIntWidth(QualType T) {
2850 if (T == BoolTy)
2851 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002852 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
2853 return FWIT->getWidth();
2854 }
2855 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00002856 return (unsigned)getTypeSize(T);
2857}
2858
2859QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2860 assert(T->isSignedIntegerType() && "Unexpected type");
2861 if (const EnumType* ETy = T->getAsEnumType())
2862 T = ETy->getDecl()->getIntegerType();
2863 const BuiltinType* BTy = T->getAsBuiltinType();
2864 assert (BTy && "Unexpected signed integer type");
2865 switch (BTy->getKind()) {
2866 case BuiltinType::Char_S:
2867 case BuiltinType::SChar:
2868 return UnsignedCharTy;
2869 case BuiltinType::Short:
2870 return UnsignedShortTy;
2871 case BuiltinType::Int:
2872 return UnsignedIntTy;
2873 case BuiltinType::Long:
2874 return UnsignedLongTy;
2875 case BuiltinType::LongLong:
2876 return UnsignedLongLongTy;
2877 default:
2878 assert(0 && "Unexpected signed integer type");
2879 return QualType();
2880 }
2881}
2882
2883
2884//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00002885// Serialization Support
2886//===----------------------------------------------------------------------===//
2887
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002888/// Emit - Serialize an ASTContext object to Bitcode.
2889void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002890 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00002891 S.EmitRef(SourceMgr);
2892 S.EmitRef(Target);
2893 S.EmitRef(Idents);
2894 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002895
Ted Kremenekfee04522007-10-31 22:44:07 +00002896 // Emit the size of the type vector so that we can reserve that size
2897 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00002898 S.EmitInt(Types.size());
2899
Ted Kremenek03ed4402007-11-13 22:02:55 +00002900 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2901 I!=E;++I)
2902 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00002903
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002904 S.EmitOwnedPtr(TUDecl);
2905
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002906 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002907}
2908
Ted Kremenek0f84c002007-11-13 00:25:37 +00002909ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002910
2911 // Read the language options.
2912 LangOptions LOpts;
2913 LOpts.Read(D);
2914
Ted Kremenekfee04522007-10-31 22:44:07 +00002915 SourceManager &SM = D.ReadRef<SourceManager>();
2916 TargetInfo &t = D.ReadRef<TargetInfo>();
2917 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2918 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00002919
Ted Kremenekfee04522007-10-31 22:44:07 +00002920 unsigned size_reserve = D.ReadInt();
2921
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002922 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2923 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00002924
Ted Kremenek03ed4402007-11-13 22:02:55 +00002925 for (unsigned i = 0; i < size_reserve; ++i)
2926 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00002927
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002928 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2929
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002930 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00002931
2932 return A;
2933}