blob: c656d966db3a16a2dc4697bb19800f6b8d55bc9c [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include "clang/Basic/TargetInfo.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000021#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000022#include "llvm/Bitcode/Serialize.h"
23#include "llvm/Bitcode/Deserialize.h"
Nate Begeman7903d052009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000025
Chris Lattner4b009652007-07-25 00:24:17 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner2fda0ed2008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Steve Naroff207b9ec2009-01-27 23:20:32 +000035 bool FreeMem, unsigned size_reserve) :
Anders Carlssonf58cac72008-08-30 19:34:46 +000036 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
Steve Naroff207b9ec2009-01-27 23:20:32 +000037 SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
Douglas Gregor24afd4a2008-11-17 14:58:09 +000038 Idents(idents), Selectors(sels)
Daniel Dunbarde300732008-08-11 04:54:23 +000039{
40 if (size_reserve > 0) Types.reserve(size_reserve);
41 InitBuiltinTypes();
Chris Lattner911b8672009-03-13 22:38:49 +000042 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.NoBuiltin);
Daniel Dunbarde300732008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
44}
45
Chris Lattner4b009652007-07-25 00:24:17 +000046ASTContext::~ASTContext() {
47 // Deallocate all the types.
48 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000049 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000050 Types.pop_back();
51 }
Eli Friedman65489b72008-05-27 03:08:09 +000052
Nuno Lopes355a8682008-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 Friedman65489b72008-05-27 03:08:09 +000080 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +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 Dunbar47677342008-09-26 03:23:00 +000087 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +000088 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
89 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
90
Chris Lattner4b009652007-07-25 00:24:17 +000091 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000092 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
93 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +000094 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Chris Lattner4b009652007-07-25 00:24:17 +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 Dunbar47677342008-09-26 03:23:00 +0000102 else if (isa<BlockPointerType>(T))
103 ++NumBlockPointer;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000104 else if (isa<LValueReferenceType>(T))
105 ++NumLValueReference;
106 else if (isa<RValueReferenceType>(T))
107 ++NumRValueReference;
Sebastian Redl75555032009-01-24 21:16:55 +0000108 else if (isa<MemberPointerType>(T))
109 ++NumMemberPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000110 else if (isa<ComplexType>(T))
111 ++NumComplex;
112 else if (isa<ArrayType>(T))
113 ++NumArray;
114 else if (isa<VectorType>(T))
115 ++NumVector;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000116 else if (isa<FunctionNoProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000117 ++NumFunctionNP;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000118 else if (isa<FunctionProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000119 ++NumFunctionP;
120 else if (isa<TypedefType>(T))
121 ++NumTypeName;
122 else if (TagType *TT = dyn_cast<TagType>(T)) {
123 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000124 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000125 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000126 case TagDecl::TK_struct: ++NumTagStruct; break;
127 case TagDecl::TK_union: ++NumTagUnion; break;
128 case TagDecl::TK_class: ++NumTagClass; break;
129 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000130 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000131 } else if (isa<ObjCInterfaceType>(T))
132 ++NumObjCInterfaces;
133 else if (isa<ObjCQualifiedInterfaceType>(T))
134 ++NumObjCQualifiedInterfaces;
135 else if (isa<ObjCQualifiedIdType>(T))
136 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +0000137 else if (isa<TypeOfType>(T))
138 ++NumTypeOfTypes;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000139 else if (isa<TypeOfExprType>(T))
140 ++NumTypeOfExprTypes;
Steve Naroff948fd372007-09-17 14:16:13 +0000141 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000142 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000143 assert(0 && "Unknown type!");
144 }
145 }
146
147 fprintf(stderr, " %d builtin types\n", NumBuiltin);
148 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar47677342008-09-26 03:23:00 +0000149 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000150 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
151 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redl75555032009-01-24 21:16:55 +0000152 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000153 fprintf(stderr, " %d complex types\n", NumComplex);
154 fprintf(stderr, " %d array types\n", NumArray);
155 fprintf(stderr, " %d vector types\n", NumVector);
156 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
157 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
158 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
159 fprintf(stderr, " %d tagged types\n", NumTagged);
160 fprintf(stderr, " %d struct types\n", NumTagStruct);
161 fprintf(stderr, " %d union types\n", NumTagUnion);
162 fprintf(stderr, " %d class types\n", NumTagClass);
163 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000164 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000165 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000166 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000167 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000168 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000169 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000170 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000171
Chris Lattner4b009652007-07-25 00:24:17 +0000172 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
173 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
174 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redlce6fff02009-03-16 23:22:08 +0000175 NumLValueReference*sizeof(LValueReferenceType)+
176 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redl75555032009-01-24 21:16:55 +0000177 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor4fa58902009-02-26 23:50:07 +0000178 NumFunctionP*sizeof(FunctionProtoType)+
179 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroffe0430632008-05-21 15:59:22 +0000180 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregor4fa58902009-02-26 23:50:07 +0000181 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)));
Chris Lattner4b009652007-07-25 00:24:17 +0000182}
183
184
185void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroff93fd2112009-01-27 22:08:43 +0000186 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000187}
188
Chris Lattner4b009652007-07-25 00:24:17 +0000189void ASTContext::InitBuiltinTypes() {
190 assert(VoidTy.isNull() && "Context reinitialized?");
191
192 // C99 6.2.5p19.
193 InitBuiltinType(VoidTy, BuiltinType::Void);
194
195 // C99 6.2.5p2.
196 InitBuiltinType(BoolTy, BuiltinType::Bool);
197 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000198 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000199 InitBuiltinType(CharTy, BuiltinType::Char_S);
200 else
201 InitBuiltinType(CharTy, BuiltinType::Char_U);
202 // C99 6.2.5p4.
203 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
204 InitBuiltinType(ShortTy, BuiltinType::Short);
205 InitBuiltinType(IntTy, BuiltinType::Int);
206 InitBuiltinType(LongTy, BuiltinType::Long);
207 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
208
209 // C99 6.2.5p6.
210 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
211 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
212 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
213 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
214 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
215
216 // C99 6.2.5p10.
217 InitBuiltinType(FloatTy, BuiltinType::Float);
218 InitBuiltinType(DoubleTy, BuiltinType::Double);
219 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000220
Chris Lattnere1dafe72009-02-26 23:43:47 +0000221 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
222 InitBuiltinType(WCharTy, BuiltinType::WChar);
223 else // C99
224 WCharTy = getFromTargetType(Target.getWCharType());
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000225
Douglas Gregord2baafd2008-10-21 16:13:35 +0000226 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000227 InitBuiltinType(OverloadTy, BuiltinType::Overload);
228
229 // Placeholder type for type-dependent expressions whose type is
230 // completely unknown. No code should ever check a type against
231 // DependentTy and users should never see it; however, it is here to
232 // help diagnose failures to properly check for type-dependent
233 // expressions.
234 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000235
Chris Lattner4b009652007-07-25 00:24:17 +0000236 // C99 6.2.5p11.
237 FloatComplexTy = getComplexType(FloatTy);
238 DoubleComplexTy = getComplexType(DoubleTy);
239 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000240
Steve Naroff9d12c902007-10-15 14:41:52 +0000241 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000242 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000243 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000244 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000245 ClassStructType = 0;
246
Ted Kremenek42730c52008-01-07 19:49:32 +0000247 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000248
249 // void * type
250 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000251}
252
253//===----------------------------------------------------------------------===//
254// Type Sizing and Analysis
255//===----------------------------------------------------------------------===//
256
Chris Lattner2a674dc2008-06-30 18:32:54 +0000257/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
258/// scalar floating point type.
259const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
260 const BuiltinType *BT = T->getAsBuiltinType();
261 assert(BT && "Not a floating point type!");
262 switch (BT->getKind()) {
263 default: assert(0 && "Not a floating point type!");
264 case BuiltinType::Float: return Target.getFloatFormat();
265 case BuiltinType::Double: return Target.getDoubleFormat();
266 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
267 }
268}
269
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000270/// getDeclAlign - Return a conservative estimate of the alignment of the
271/// specified decl. Note that bitfields do not have a valid alignment, so
272/// this method will assert on them.
Daniel Dunbar96d1f1b2009-02-17 22:16:19 +0000273unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedman0ee57322009-02-22 02:56:25 +0000274 unsigned Align = Target.getCharWidth();
275
276 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
277 Align = std::max(Align, AA->getAlignment());
278
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000279 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
280 QualType T = VD->getType();
281 // Incomplete or function types default to 1.
Eli Friedman0ee57322009-02-22 02:56:25 +0000282 if (!T->isIncompleteType() && !T->isFunctionType()) {
283 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
284 T = cast<ArrayType>(T)->getElementType();
285
286 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
287 }
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000288 }
Eli Friedman0ee57322009-02-22 02:56:25 +0000289
290 return Align / Target.getCharWidth();
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000291}
Chris Lattner2a674dc2008-06-30 18:32:54 +0000292
Chris Lattner4b009652007-07-25 00:24:17 +0000293/// getTypeSize - Return the size of the specified type, in bits. This method
294/// does not work on incomplete types.
295std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000296ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000297 T = getCanonicalType(T);
Mike Stump44d1f402009-02-27 18:32:39 +0000298 uint64_t Width=0;
299 unsigned Align=8;
Chris Lattner4b009652007-07-25 00:24:17 +0000300 switch (T->getTypeClass()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000301#define TYPE(Class, Base)
302#define ABSTRACT_TYPE(Class, Base)
303#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
304#define DEPENDENT_TYPE(Class, Base) case Type::Class:
305#include "clang/AST/TypeNodes.def"
306 assert(false && "Should not see non-canonical or dependent types");
307 break;
308
Chris Lattner4b009652007-07-25 00:24:17 +0000309 case Type::FunctionNoProto:
310 case Type::FunctionProto:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000311 case Type::IncompleteArray:
Chris Lattner4b009652007-07-25 00:24:17 +0000312 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000313 case Type::VariableArray:
314 assert(0 && "VLAs not implemented yet!");
315 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000316 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000317
Chris Lattner8cd0e932008-03-05 18:54:05 +0000318 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000319 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000320 Align = EltInfo.second;
321 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000322 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000323 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000324 case Type::Vector: {
325 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000326 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000327 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000328 Align = Width;
Nate Begeman7903d052009-01-18 06:42:49 +0000329 // If the alignment is not a power of 2, round up to the next power of 2.
330 // This happens for non-power-of-2 length vectors.
331 // FIXME: this should probably be a target property.
332 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000333 break;
334 }
335
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000336 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000337 switch (cast<BuiltinType>(T)->getKind()) {
338 default: assert(0 && "Unknown builtin type!");
339 case BuiltinType::Void:
340 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000341 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000342 Width = Target.getBoolWidth();
343 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000344 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000345 case BuiltinType::Char_S:
346 case BuiltinType::Char_U:
347 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000348 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000349 Width = Target.getCharWidth();
350 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000351 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000352 case BuiltinType::WChar:
353 Width = Target.getWCharWidth();
354 Align = Target.getWCharAlign();
355 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000356 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000357 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000358 Width = Target.getShortWidth();
359 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000360 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000361 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000362 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000363 Width = Target.getIntWidth();
364 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000365 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000366 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000367 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000368 Width = Target.getLongWidth();
369 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000370 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000371 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000372 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000373 Width = Target.getLongLongWidth();
374 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000375 break;
376 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000377 Width = Target.getFloatWidth();
378 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000379 break;
380 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000381 Width = Target.getDoubleWidth();
382 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000383 break;
384 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000385 Width = Target.getLongDoubleWidth();
386 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000387 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000388 }
389 break;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000390 case Type::FixedWidthInt:
391 // FIXME: This isn't precisely correct; the width/alignment should depend
392 // on the available types for the target
393 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattnere9174982009-02-15 21:20:13 +0000394 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000395 Align = Width;
396 break;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000397 case Type::ExtQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000398 // FIXME: Pointers into different addr spaces could have different sizes and
399 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000400 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000401 case Type::ObjCQualifiedId:
Eli Friedman2f6d70d2009-02-22 04:02:33 +0000402 case Type::ObjCQualifiedClass:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000403 case Type::ObjCQualifiedInterface:
Chris Lattner1d78a862008-04-07 07:01:58 +0000404 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000405 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000406 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000407 case Type::BlockPointer: {
408 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
409 Width = Target.getPointerWidth(AS);
410 Align = Target.getPointerAlign(AS);
411 break;
412 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000413 case Type::Pointer: {
414 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000415 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000416 Align = Target.getPointerAlign(AS);
417 break;
418 }
Sebastian Redlce6fff02009-03-16 23:22:08 +0000419 case Type::LValueReference:
420 case Type::RValueReference:
Chris Lattner4b009652007-07-25 00:24:17 +0000421 // "When applied to a reference or a reference type, the result is the size
422 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000423 // FIXME: This is wrong for struct layout: a reference in a struct has
424 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000425 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl75555032009-01-24 21:16:55 +0000426 case Type::MemberPointer: {
Sebastian Redl18cffee2009-01-24 23:29:36 +0000427 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redl75555032009-01-24 21:16:55 +0000428 // the GCC ABI, where pointers to data are one pointer large, pointers to
429 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl18cffee2009-01-24 23:29:36 +0000430 // other compilers too, we need to delegate this completely to TargetInfo
431 // or some ABI abstraction layer.
Sebastian Redl75555032009-01-24 21:16:55 +0000432 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
433 unsigned AS = Pointee.getAddressSpace();
434 Width = Target.getPointerWidth(AS);
435 if (Pointee->isFunctionType())
436 Width *= 2;
437 Align = Target.getPointerAlign(AS);
438 // GCC aligns at single pointer width.
439 }
Chris Lattner4b009652007-07-25 00:24:17 +0000440 case Type::Complex: {
441 // Complex types have the same alignment as their elements, but twice the
442 // size.
443 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000444 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000445 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000446 Align = EltInfo.second;
447 break;
448 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000449 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000450 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000451 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
452 Width = Layout.getSize();
453 Align = Layout.getAlignment();
454 break;
455 }
Douglas Gregor4fa58902009-02-26 23:50:07 +0000456 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000457 case Type::Enum: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000458 const TagType *TT = cast<TagType>(T);
459
460 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000461 Width = 1;
462 Align = 1;
463 break;
464 }
465
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000466 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000467 return getTypeInfo(ET->getDecl()->getIntegerType());
468
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000469 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000470 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
471 Width = Layout.getSize();
472 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000473 break;
474 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000475 }
Chris Lattner4b009652007-07-25 00:24:17 +0000476
477 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000478 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000479}
480
Chris Lattner83165b52009-01-27 18:08:34 +0000481/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
482/// type for the current target in bits. This can be different than the ABI
483/// alignment in cases where it is beneficial for performance to overalign
484/// a data type.
485unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
486 unsigned ABIAlign = getTypeAlign(T);
487
488 // Doubles should be naturally aligned if possible.
Daniel Dunbarc61a8002009-02-18 19:59:32 +0000489 if (T->isSpecificBuiltinType(BuiltinType::Double))
490 return std::max(ABIAlign, 64U);
Chris Lattner83165b52009-01-27 18:08:34 +0000491
492 return ABIAlign;
493}
494
495
Devang Patelbfe323c2008-06-04 21:22:16 +0000496/// LayoutField - Field layout.
497void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000498 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000499 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000500 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000501 uint64_t FieldOffset = IsUnion ? 0 : Size;
502 uint64_t FieldSize;
503 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000504
505 // FIXME: Should this override struct packing? Probably we want to
506 // take the minimum?
507 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
508 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000509
510 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
511 // TODO: Need to check this algorithm on other targets!
512 // (tested on Linux-X86)
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +0000513 FieldSize =
514 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000515
516 std::pair<uint64_t, unsigned> FieldInfo =
517 Context.getTypeInfo(FD->getType());
518 uint64_t TypeSize = FieldInfo.first;
519
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000520 // Determine the alignment of this bitfield. The packing
521 // attributes define a maximum and the alignment attribute defines
522 // a minimum.
523 // FIXME: What is the right behavior when the specified alignment
524 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000525 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000526 if (FieldPacking)
527 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000528 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
529 FieldAlign = std::max(FieldAlign, AA->getAlignment());
530
531 // Check if we need to add padding to give the field the correct
532 // alignment.
533 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
534 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
535
536 // Padding members don't affect overall alignment
537 if (!FD->getIdentifier())
538 FieldAlign = 1;
539 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000540 if (FD->getType()->isIncompleteArrayType()) {
541 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000542 // query getTypeInfo about these, so we figure it out here.
543 // Flexible array members don't have any size, but they
544 // have to be aligned appropriately for their element type.
545 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000546 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000547 FieldAlign = Context.getTypeAlign(ATy->getElementType());
548 } else {
549 std::pair<uint64_t, unsigned> FieldInfo =
550 Context.getTypeInfo(FD->getType());
551 FieldSize = FieldInfo.first;
552 FieldAlign = FieldInfo.second;
553 }
554
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000555 // Determine the alignment of this bitfield. The packing
556 // attributes define a maximum and the alignment attribute defines
557 // a minimum. Additionally, the packing alignment must be at least
558 // a byte for non-bitfields.
559 //
560 // FIXME: What is the right behavior when the specified alignment
561 // is smaller than the specified packing?
562 if (FieldPacking)
563 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000564 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
565 FieldAlign = std::max(FieldAlign, AA->getAlignment());
566
567 // Round up the current record size to the field's alignment boundary.
568 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
569 }
570
571 // Place this field at the current location.
572 FieldOffsets[FieldNo] = FieldOffset;
573
574 // Reserve space for this field.
575 if (IsUnion) {
576 Size = std::max(Size, FieldSize);
577 } else {
578 Size = FieldOffset + FieldSize;
579 }
580
581 // Remember max struct/class alignment.
582 Alignment = std::max(Alignment, FieldAlign);
583}
584
Fariborz Jahaniana2c97df2009-03-05 20:08:48 +0000585void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
586 std::vector<FieldDecl*> &Fields) const {
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000587 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
588 if (SuperClass)
589 CollectObjCIvars(SuperClass, Fields);
590 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
591 E = OI->ivar_end(); I != E; ++I) {
592 ObjCIvarDecl *IVDecl = (*I);
593 if (!IVDecl->isInvalidDecl())
594 Fields.push_back(cast<FieldDecl>(IVDecl));
595 }
596}
597
598/// addRecordToClass - produces record info. for the class for its
599/// ivars and all those inherited.
600///
601const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
602{
603 const RecordDecl *&RD = ASTRecordForInterface[D];
604 if (RD)
605 return RD;
606 std::vector<FieldDecl*> RecFields;
607 CollectObjCIvars(D, RecFields);
608 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
609 D->getLocation(),
610 D->getIdentifier());
611 /// FIXME! Can do collection of ivars and adding to the record while
612 /// doing it.
613 for (unsigned int i = 0; i != RecFields.size(); i++) {
614 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
615 RecFields[i]->getLocation(),
616 RecFields[i]->getIdentifier(),
617 RecFields[i]->getType(),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000618 RecFields[i]->getBitWidth(), false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000619 NewRD->addDecl(Field);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000620 }
621 NewRD->completeDefinition(*this);
622 RD = NewRD;
623 return RD;
624}
Devang Patel4b6bf702008-06-04 21:54:36 +0000625
Fariborz Jahanianea944842008-12-18 17:29:46 +0000626/// setFieldDecl - maps a field for the given Ivar reference node.
627//
628void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
629 const ObjCIvarDecl *Ivar,
630 const ObjCIvarRefExpr *MRef) {
631 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
632 lookupFieldDeclForIvar(*this, Ivar);
633 ASTFieldForIvarRef[MRef] = FD;
634}
635
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000636/// getASTObjcInterfaceLayout - Get or compute information about the layout of
637/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000638/// position information.
639const ASTRecordLayout &
640ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
641 // Look up this layout, if already laid out, return what we have.
642 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
643 if (Entry) return *Entry;
644
645 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
646 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000647 ASTRecordLayout *NewEntry = NULL;
648 unsigned FieldCount = D->ivar_size();
649 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
650 FieldCount++;
651 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
652 unsigned Alignment = SL.getAlignment();
653 uint64_t Size = SL.getSize();
654 NewEntry = new ASTRecordLayout(Size, Alignment);
655 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000656 // Super class is at the beginning of the layout.
657 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000658 } else {
659 NewEntry = new ASTRecordLayout();
660 NewEntry->InitializeLayout(FieldCount);
661 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000662 Entry = NewEntry;
663
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000664 unsigned StructPacking = 0;
665 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
666 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000667
668 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
669 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
670 AA->getAlignment()));
671
672 // Layout each ivar sequentially.
673 unsigned i = 0;
674 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
675 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
676 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000677 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000678 }
679
680 // Finally, round the size of the total struct up to the alignment of the
681 // struct itself.
682 NewEntry->FinalizeLayout();
683 return *NewEntry;
684}
685
Devang Patel7a78e432007-11-01 19:11:01 +0000686/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000687/// specified record (struct/union/class), which indicates its size and field
688/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000689const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000690 D = D->getDefinition(*this);
691 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000692
Chris Lattner4b009652007-07-25 00:24:17 +0000693 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000694 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000695 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000696
Devang Patel7a78e432007-11-01 19:11:01 +0000697 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
698 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
699 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000700 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000701
Douglas Gregor39677622008-12-11 20:41:00 +0000702 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000703 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000704 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000705
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000706 unsigned StructPacking = 0;
707 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
708 StructPacking = PA->getAlignment();
709
Eli Friedman5949a022008-05-30 09:31:38 +0000710 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000711 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
712 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000713
Eli Friedman5949a022008-05-30 09:31:38 +0000714 // Layout each field, for now, just sequentially, respecting alignment. In
715 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000716 unsigned FieldIdx = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000717 for (RecordDecl::field_iterator Field = D->field_begin(),
718 FieldEnd = D->field_end();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000719 Field != FieldEnd; (void)++Field, ++FieldIdx)
720 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000721
722 // Finally, round the size of the total struct up to the alignment of the
723 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000724 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000725 return *NewEntry;
726}
727
Chris Lattner4b009652007-07-25 00:24:17 +0000728//===----------------------------------------------------------------------===//
729// Type creation/memoization methods
730//===----------------------------------------------------------------------===//
731
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000732QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000733 QualType CanT = getCanonicalType(T);
734 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000735 return T;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000736
737 // If we are composing extended qualifiers together, merge together into one
738 // ExtQualType node.
739 unsigned CVRQuals = T.getCVRQualifiers();
740 QualType::GCAttrTypes GCAttr = QualType::GCNone;
741 Type *TypeNode = T.getTypePtr();
Chris Lattner35fef522008-02-20 20:55:12 +0000742
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000743 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
744 // If this type already has an address space specified, it cannot get
745 // another one.
746 assert(EQT->getAddressSpace() == 0 &&
747 "Type cannot be in multiple addr spaces!");
748 GCAttr = EQT->getObjCGCAttr();
749 TypeNode = EQT->getBaseType();
750 }
Chris Lattner35fef522008-02-20 20:55:12 +0000751
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000752 // Check if we've already instantiated this type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000753 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000754 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000755 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000756 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000757 return QualType(EXTQy, CVRQuals);
758
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000759 // If the base type isn't canonical, this won't be a canonical type either,
760 // so fill in the canonical type field.
761 QualType Canonical;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000762 if (!TypeNode->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000763 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000764
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000765 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000766 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000767 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000768 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000769 ExtQualType *New =
770 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000771 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000772 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000773 return QualType(New, CVRQuals);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000774}
775
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000776QualType ASTContext::getObjCGCQualType(QualType T,
777 QualType::GCAttrTypes GCAttr) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000778 QualType CanT = getCanonicalType(T);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000779 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000780 return T;
781
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000782 // If we are composing extended qualifiers together, merge together into one
783 // ExtQualType node.
784 unsigned CVRQuals = T.getCVRQualifiers();
785 Type *TypeNode = T.getTypePtr();
786 unsigned AddressSpace = 0;
787
788 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
789 // If this type already has an address space specified, it cannot get
790 // another one.
791 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
792 "Type cannot be in multiple addr spaces!");
793 AddressSpace = EQT->getAddressSpace();
794 TypeNode = EQT->getBaseType();
795 }
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000796
797 // Check if we've already instantiated an gc qual'd type of this type.
798 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000799 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000800 void *InsertPos = 0;
801 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000802 return QualType(EXTQy, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000803
804 // If the base type isn't canonical, this won't be a canonical type either,
805 // so fill in the canonical type field.
Eli Friedman94fcc9a2009-02-27 23:04:43 +0000806 // FIXME: Isn't this also not canonical if the base type is a array
807 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000808 QualType Canonical;
809 if (!T->isCanonical()) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000810 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000811
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000812 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000813 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
814 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
815 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000816 ExtQualType *New =
817 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000818 ExtQualTypes.InsertNode(New, InsertPos);
819 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000820 return QualType(New, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000821}
Chris Lattner4b009652007-07-25 00:24:17 +0000822
823/// getComplexType - Return the uniqued reference to the type for a complex
824/// number with the specified element type.
825QualType ASTContext::getComplexType(QualType T) {
826 // Unique pointers, to guarantee there is only one pointer of a particular
827 // structure.
828 llvm::FoldingSetNodeID ID;
829 ComplexType::Profile(ID, T);
830
831 void *InsertPos = 0;
832 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
833 return QualType(CT, 0);
834
835 // If the pointee type isn't canonical, this won't be a canonical type either,
836 // so fill in the canonical type field.
837 QualType Canonical;
838 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000839 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000840
841 // Get the new insert position for the node we care about.
842 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000843 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000844 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000845 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000846 Types.push_back(New);
847 ComplexTypes.InsertNode(New, InsertPos);
848 return QualType(New, 0);
849}
850
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000851QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
852 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
853 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
854 FixedWidthIntType *&Entry = Map[Width];
855 if (!Entry)
856 Entry = new FixedWidthIntType(Width, Signed);
857 return QualType(Entry, 0);
858}
Chris Lattner4b009652007-07-25 00:24:17 +0000859
860/// getPointerType - Return the uniqued reference to the type for a pointer to
861/// the specified type.
862QualType ASTContext::getPointerType(QualType T) {
863 // Unique pointers, to guarantee there is only one pointer of a particular
864 // structure.
865 llvm::FoldingSetNodeID ID;
866 PointerType::Profile(ID, T);
867
868 void *InsertPos = 0;
869 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
870 return QualType(PT, 0);
871
872 // If the pointee type isn't canonical, this won't be a canonical type either,
873 // so fill in the canonical type field.
874 QualType Canonical;
875 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000876 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000877
878 // Get the new insert position for the node we care about.
879 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000880 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000881 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000882 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000883 Types.push_back(New);
884 PointerTypes.InsertNode(New, InsertPos);
885 return QualType(New, 0);
886}
887
Steve Naroff7aa54752008-08-27 16:04:49 +0000888/// getBlockPointerType - Return the uniqued reference to the type for
889/// a pointer to the specified block.
890QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000891 assert(T->isFunctionType() && "block of function types only");
892 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000893 // structure.
894 llvm::FoldingSetNodeID ID;
895 BlockPointerType::Profile(ID, T);
896
897 void *InsertPos = 0;
898 if (BlockPointerType *PT =
899 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
900 return QualType(PT, 0);
901
Steve Narofffd5b19d2008-08-28 19:20:44 +0000902 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000903 // type either so fill in the canonical type field.
904 QualType Canonical;
905 if (!T->isCanonical()) {
906 Canonical = getBlockPointerType(getCanonicalType(T));
907
908 // Get the new insert position for the node we care about.
909 BlockPointerType *NewIP =
910 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000911 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000912 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000913 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000914 Types.push_back(New);
915 BlockPointerTypes.InsertNode(New, InsertPos);
916 return QualType(New, 0);
917}
918
Sebastian Redlce6fff02009-03-16 23:22:08 +0000919/// getLValueReferenceType - Return the uniqued reference to the type for an
920/// lvalue reference to the specified type.
921QualType ASTContext::getLValueReferenceType(QualType T) {
Chris Lattner4b009652007-07-25 00:24:17 +0000922 // Unique pointers, to guarantee there is only one pointer of a particular
923 // structure.
924 llvm::FoldingSetNodeID ID;
925 ReferenceType::Profile(ID, T);
926
927 void *InsertPos = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000928 if (LValueReferenceType *RT =
929 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000930 return QualType(RT, 0);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000931
Chris Lattner4b009652007-07-25 00:24:17 +0000932 // If the referencee type isn't canonical, this won't be a canonical type
933 // either, so fill in the canonical type field.
934 QualType Canonical;
935 if (!T->isCanonical()) {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000936 Canonical = getLValueReferenceType(getCanonicalType(T));
937
Chris Lattner4b009652007-07-25 00:24:17 +0000938 // Get the new insert position for the node we care about.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000939 LValueReferenceType *NewIP =
940 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000941 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000942 }
943
Sebastian Redlce6fff02009-03-16 23:22:08 +0000944 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000945 Types.push_back(New);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000946 LValueReferenceTypes.InsertNode(New, InsertPos);
947 return QualType(New, 0);
948}
949
950/// getRValueReferenceType - Return the uniqued reference to the type for an
951/// rvalue reference to the specified type.
952QualType ASTContext::getRValueReferenceType(QualType T) {
953 // Unique pointers, to guarantee there is only one pointer of a particular
954 // structure.
955 llvm::FoldingSetNodeID ID;
956 ReferenceType::Profile(ID, T);
957
958 void *InsertPos = 0;
959 if (RValueReferenceType *RT =
960 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
961 return QualType(RT, 0);
962
963 // If the referencee type isn't canonical, this won't be a canonical type
964 // either, so fill in the canonical type field.
965 QualType Canonical;
966 if (!T->isCanonical()) {
967 Canonical = getRValueReferenceType(getCanonicalType(T));
968
969 // Get the new insert position for the node we care about.
970 RValueReferenceType *NewIP =
971 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
972 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
973 }
974
975 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
976 Types.push_back(New);
977 RValueReferenceTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000978 return QualType(New, 0);
979}
980
Sebastian Redl75555032009-01-24 21:16:55 +0000981/// getMemberPointerType - Return the uniqued reference to the type for a
982/// member pointer to the specified type, in the specified class.
983QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
984{
985 // Unique pointers, to guarantee there is only one pointer of a particular
986 // structure.
987 llvm::FoldingSetNodeID ID;
988 MemberPointerType::Profile(ID, T, Cls);
989
990 void *InsertPos = 0;
991 if (MemberPointerType *PT =
992 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
993 return QualType(PT, 0);
994
995 // If the pointee or class type isn't canonical, this won't be a canonical
996 // type either, so fill in the canonical type field.
997 QualType Canonical;
998 if (!T->isCanonical()) {
999 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1000
1001 // Get the new insert position for the node we care about.
1002 MemberPointerType *NewIP =
1003 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1004 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1005 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001006 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +00001007 Types.push_back(New);
1008 MemberPointerTypes.InsertNode(New, InsertPos);
1009 return QualType(New, 0);
1010}
1011
Steve Naroff83c13012007-08-30 01:06:46 +00001012/// getConstantArrayType - Return the unique reference to the type for an
1013/// array of the specified element type.
1014QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +00001015 const llvm::APInt &ArySize,
1016 ArrayType::ArraySizeModifier ASM,
1017 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001018 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001019 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001020
1021 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +00001022 if (ConstantArrayType *ATP =
1023 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001024 return QualType(ATP, 0);
1025
1026 // If the element type isn't canonical, this won't be a canonical type either,
1027 // so fill in the canonical type field.
1028 QualType Canonical;
1029 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001030 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +00001031 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001032 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +00001033 ConstantArrayType *NewIP =
1034 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001035 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001036 }
1037
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001038 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001039 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001040 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001041 Types.push_back(New);
1042 return QualType(New, 0);
1043}
1044
Steve Naroffe2579e32007-08-30 18:14:25 +00001045/// getVariableArrayType - Returns a non-unique reference to the type for a
1046/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +00001047QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1048 ArrayType::ArraySizeModifier ASM,
1049 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +00001050 // Since we don't unique expressions, it isn't possible to unique VLA's
1051 // that have an expression provided for their size.
1052
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001053 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001054 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001055
1056 VariableArrayTypes.push_back(New);
1057 Types.push_back(New);
1058 return QualType(New, 0);
1059}
1060
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001061/// getDependentSizedArrayType - Returns a non-unique reference to
1062/// the type for a dependently-sized array of the specified element
1063/// type. FIXME: We will need these to be uniqued, or at least
1064/// comparable, at some point.
1065QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1066 ArrayType::ArraySizeModifier ASM,
1067 unsigned EltTypeQuals) {
1068 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1069 "Size must be type- or value-dependent!");
1070
1071 // Since we don't unique expressions, it isn't possible to unique
1072 // dependently-sized array types.
1073
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001074 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001075 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1076 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001077
1078 DependentSizedArrayTypes.push_back(New);
1079 Types.push_back(New);
1080 return QualType(New, 0);
1081}
1082
Eli Friedman8ff07782008-02-15 18:16:39 +00001083QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1084 ArrayType::ArraySizeModifier ASM,
1085 unsigned EltTypeQuals) {
1086 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001087 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001088
1089 void *InsertPos = 0;
1090 if (IncompleteArrayType *ATP =
1091 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1092 return QualType(ATP, 0);
1093
1094 // If the element type isn't canonical, this won't be a canonical type
1095 // either, so fill in the canonical type field.
1096 QualType Canonical;
1097
1098 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001099 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001100 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001101
1102 // Get the new insert position for the node we care about.
1103 IncompleteArrayType *NewIP =
1104 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001105 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001106 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001107
Steve Naroff93fd2112009-01-27 22:08:43 +00001108 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001109 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001110
1111 IncompleteArrayTypes.InsertNode(New, InsertPos);
1112 Types.push_back(New);
1113 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001114}
1115
Chris Lattner4b009652007-07-25 00:24:17 +00001116/// getVectorType - Return the unique reference to a vector type of
1117/// the specified element type and size. VectorType must be a built-in type.
1118QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1119 BuiltinType *baseType;
1120
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001121 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001122 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1123
1124 // Check if we've already instantiated a vector of this type.
1125 llvm::FoldingSetNodeID ID;
1126 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1127 void *InsertPos = 0;
1128 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1129 return QualType(VTP, 0);
1130
1131 // If the element type isn't canonical, this won't be a canonical type either,
1132 // so fill in the canonical type field.
1133 QualType Canonical;
1134 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001135 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001136
1137 // Get the new insert position for the node we care about.
1138 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001139 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001140 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001141 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001142 VectorTypes.InsertNode(New, InsertPos);
1143 Types.push_back(New);
1144 return QualType(New, 0);
1145}
1146
Nate Begemanaf6ed502008-04-18 23:10:10 +00001147/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001148/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001149QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001150 BuiltinType *baseType;
1151
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001152 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001153 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001154
1155 // Check if we've already instantiated a vector of this type.
1156 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001157 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001158 void *InsertPos = 0;
1159 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1160 return QualType(VTP, 0);
1161
1162 // If the element type isn't canonical, this won't be a canonical type either,
1163 // so fill in the canonical type field.
1164 QualType Canonical;
1165 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001166 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001167
1168 // Get the new insert position for the node we care about.
1169 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001170 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001171 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001172 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001173 VectorTypes.InsertNode(New, InsertPos);
1174 Types.push_back(New);
1175 return QualType(New, 0);
1176}
1177
Douglas Gregor4fa58902009-02-26 23:50:07 +00001178/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001179///
Douglas Gregor4fa58902009-02-26 23:50:07 +00001180QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Chris Lattner4b009652007-07-25 00:24:17 +00001181 // Unique functions, to guarantee there is only one function of a particular
1182 // structure.
1183 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001184 FunctionNoProtoType::Profile(ID, ResultTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001185
1186 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001187 if (FunctionNoProtoType *FT =
1188 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001189 return QualType(FT, 0);
1190
1191 QualType Canonical;
1192 if (!ResultTy->isCanonical()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00001193 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001194
1195 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001196 FunctionNoProtoType *NewIP =
1197 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001198 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001199 }
1200
Douglas Gregor4fa58902009-02-26 23:50:07 +00001201 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001202 Types.push_back(New);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001203 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001204 return QualType(New, 0);
1205}
1206
1207/// getFunctionType - Return a normal function type with a typed argument
1208/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001209QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001210 unsigned NumArgs, bool isVariadic,
1211 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001212 // Unique functions, to guarantee there is only one function of a particular
1213 // structure.
1214 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001215 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001216 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001217
1218 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001219 if (FunctionProtoType *FTP =
1220 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001221 return QualType(FTP, 0);
1222
1223 // Determine whether the type being created is already canonical or not.
1224 bool isCanonical = ResultTy->isCanonical();
1225 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1226 if (!ArgArray[i]->isCanonical())
1227 isCanonical = false;
1228
1229 // If this type isn't canonical, get the canonical version of it.
1230 QualType Canonical;
1231 if (!isCanonical) {
1232 llvm::SmallVector<QualType, 16> CanonicalArgs;
1233 CanonicalArgs.reserve(NumArgs);
1234 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001235 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001236
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001237 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001238 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001239 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001240
1241 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001242 FunctionProtoType *NewIP =
1243 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001244 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001245 }
1246
Douglas Gregor4fa58902009-02-26 23:50:07 +00001247 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001248 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001249 FunctionProtoType *FTP =
1250 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001251 NumArgs*sizeof(QualType), 8);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001252 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001253 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001254 Types.push_back(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001255 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001256 return QualType(FTP, 0);
1257}
1258
Douglas Gregor1d661552008-04-13 21:07:44 +00001259/// getTypeDeclType - Return the unique reference to the type for the
1260/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001261QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001262 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001263 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1264
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001265 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001266 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001267 else if (isa<TemplateTypeParmDecl>(Decl)) {
1268 assert(false && "Template type parameter types are always available.");
1269 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001270 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001271
Douglas Gregor2e047592009-02-28 01:32:25 +00001272 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001273 if (PrevDecl)
1274 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001275 else
1276 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001277 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001278 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1279 if (PrevDecl)
1280 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001281 else
1282 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001283 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001284 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001285 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001286
Ted Kremenek46a837c2008-09-05 17:16:31 +00001287 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001288 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001289}
1290
Chris Lattner4b009652007-07-25 00:24:17 +00001291/// getTypedefType - Return the unique reference to the type for the
1292/// specified typename decl.
1293QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1294 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1295
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001296 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001297 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001298 Types.push_back(Decl->TypeForDecl);
1299 return QualType(Decl->TypeForDecl, 0);
1300}
1301
Ted Kremenek42730c52008-01-07 19:49:32 +00001302/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001303/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001304QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001305 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1306
Steve Naroff93fd2112009-01-27 22:08:43 +00001307 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001308 Types.push_back(Decl->TypeForDecl);
1309 return QualType(Decl->TypeForDecl, 0);
1310}
1311
Fariborz Jahanian27ecc672009-02-14 20:13:28 +00001312/// buildObjCInterfaceType - Returns a new type for the interface
1313/// declaration, regardless. It also removes any previously built
1314/// record declaration so caller can rebuild it.
1315QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) {
1316 const RecordDecl *&RD = ASTRecordForInterface[Decl];
1317 if (RD)
1318 RD = 0;
1319 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
1320 Types.push_back(Decl->TypeForDecl);
1321 return QualType(Decl->TypeForDecl, 0);
1322}
1323
Douglas Gregora4918772009-02-05 23:33:38 +00001324/// \brief Retrieve the template type parameter type for a template
1325/// parameter with the given depth, index, and (optionally) name.
1326QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1327 IdentifierInfo *Name) {
1328 llvm::FoldingSetNodeID ID;
1329 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1330 void *InsertPos = 0;
1331 TemplateTypeParmType *TypeParm
1332 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1333
1334 if (TypeParm)
1335 return QualType(TypeParm, 0);
1336
1337 if (Name)
1338 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1339 getTemplateTypeParmType(Depth, Index));
1340 else
1341 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1342
1343 Types.push_back(TypeParm);
1344 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1345
1346 return QualType(TypeParm, 0);
1347}
1348
Douglas Gregor8e458f42009-02-09 18:46:07 +00001349QualType
1350ASTContext::getClassTemplateSpecializationType(TemplateDecl *Template,
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001351 const TemplateArgument *Args,
Douglas Gregor8e458f42009-02-09 18:46:07 +00001352 unsigned NumArgs,
Douglas Gregor8e458f42009-02-09 18:46:07 +00001353 QualType Canon) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001354 if (!Canon.isNull())
1355 Canon = getCanonicalType(Canon);
Douglas Gregor9c7825b2009-02-26 22:19:44 +00001356
Douglas Gregor8e458f42009-02-09 18:46:07 +00001357 llvm::FoldingSetNodeID ID;
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001358 ClassTemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
1359
Douglas Gregor8e458f42009-02-09 18:46:07 +00001360 void *InsertPos = 0;
1361 ClassTemplateSpecializationType *Spec
1362 = ClassTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
1363
1364 if (Spec)
1365 return QualType(Spec, 0);
1366
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001367 void *Mem = Allocate((sizeof(ClassTemplateSpecializationType) +
1368 sizeof(TemplateArgument) * NumArgs),
1369 8);
1370 Spec = new (Mem) ClassTemplateSpecializationType(Template, Args, NumArgs,
1371 Canon);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001372 Types.push_back(Spec);
1373 ClassTemplateSpecializationTypes.InsertNode(Spec, InsertPos);
1374
1375 return QualType(Spec, 0);
1376}
1377
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001378QualType
1379ASTContext::getQualifiedNameType(const NestedNameSpecifier *Components,
1380 unsigned NumComponents,
1381 QualType NamedType) {
1382 llvm::FoldingSetNodeID ID;
1383 QualifiedNameType::Profile(ID, Components, NumComponents, NamedType);
1384
1385 void *InsertPos = 0;
1386 QualifiedNameType *T
1387 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1388 if (T)
1389 return QualType(T, 0);
1390
1391 void *Mem = Allocate((sizeof(QualifiedNameType) +
1392 sizeof(NestedNameSpecifier) * NumComponents),
1393 8);
1394 T = new (Mem) QualifiedNameType(Components, NumComponents, NamedType,
1395 getCanonicalType(NamedType));
1396 Types.push_back(T);
1397 QualifiedNameTypes.InsertNode(T, InsertPos);
1398 return QualType(T, 0);
1399}
1400
Chris Lattnere1352302008-04-07 04:56:42 +00001401/// CmpProtocolNames - Comparison predicate for sorting protocols
1402/// alphabetically.
1403static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1404 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001405 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001406}
1407
1408static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1409 unsigned &NumProtocols) {
1410 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1411
1412 // Sort protocols, keyed by name.
1413 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1414
1415 // Remove duplicates.
1416 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1417 NumProtocols = ProtocolsEnd-Protocols;
1418}
1419
1420
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001421/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1422/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001423QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1424 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001425 // Sort the protocol list alphabetically to canonicalize it.
1426 SortAndUniqueProtocols(Protocols, NumProtocols);
1427
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001428 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001429 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001430
1431 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001432 if (ObjCQualifiedInterfaceType *QT =
1433 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001434 return QualType(QT, 0);
1435
1436 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001437 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001438 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001439
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001440 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001441 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001442 return QualType(QType, 0);
1443}
1444
Chris Lattnere1352302008-04-07 04:56:42 +00001445/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1446/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001447QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001448 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001449 // Sort the protocol list alphabetically to canonicalize it.
1450 SortAndUniqueProtocols(Protocols, NumProtocols);
1451
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001452 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001453 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001454
1455 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001456 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001457 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001458 return QualType(QT, 0);
1459
1460 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001461 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001462 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001463 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001464 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001465 return QualType(QType, 0);
1466}
1467
Douglas Gregor4fa58902009-02-26 23:50:07 +00001468/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1469/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff0604dd92007-08-01 18:02:17 +00001470/// multiple declarations that refer to "typeof(x)" all contain different
1471/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1472/// on canonical type's (which are always unique).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001473QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001474 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001475 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001476 Types.push_back(toe);
1477 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001478}
1479
Steve Naroff0604dd92007-08-01 18:02:17 +00001480/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1481/// TypeOfType AST's. The only motivation to unique these nodes would be
1482/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1483/// an issue. This doesn't effect the type checker, since it operates
1484/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001485QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001486 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001487 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001488 Types.push_back(tot);
1489 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001490}
1491
Chris Lattner4b009652007-07-25 00:24:17 +00001492/// getTagDeclType - Return the unique reference to the type for the
1493/// specified TagDecl (struct/union/class/enum) decl.
1494QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001495 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001496 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001497}
1498
1499/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1500/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1501/// needs to agree with the definition in <stddef.h>.
1502QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001503 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001504}
1505
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001506/// getSignedWCharType - Return the type of "signed wchar_t".
1507/// Used when in C++, as a GCC extension.
1508QualType ASTContext::getSignedWCharType() const {
1509 // FIXME: derive from "Target" ?
1510 return WCharTy;
1511}
1512
1513/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1514/// Used when in C++, as a GCC extension.
1515QualType ASTContext::getUnsignedWCharType() const {
1516 // FIXME: derive from "Target" ?
1517 return UnsignedIntTy;
1518}
1519
Chris Lattner4b009652007-07-25 00:24:17 +00001520/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1521/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1522QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001523 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001524}
1525
Chris Lattner19eb97e2008-04-02 05:18:44 +00001526//===----------------------------------------------------------------------===//
1527// Type Operators
1528//===----------------------------------------------------------------------===//
1529
Chris Lattner3dae6f42008-04-06 22:41:35 +00001530/// getCanonicalType - Return the canonical (structural) type corresponding to
1531/// the specified potentially non-canonical type. The non-canonical version
1532/// of a type may have many "decorated" versions of types. Decorators can
1533/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1534/// to be free of any of these, allowing two canonical types to be compared
1535/// for exact equality with a simple pointer comparison.
1536QualType ASTContext::getCanonicalType(QualType T) {
1537 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001538
1539 // If the result has type qualifiers, make sure to canonicalize them as well.
1540 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1541 if (TypeQuals == 0) return CanType;
1542
1543 // If the type qualifiers are on an array type, get the canonical type of the
1544 // array with the qualifiers applied to the element type.
1545 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1546 if (!AT)
1547 return CanType.getQualifiedType(TypeQuals);
1548
1549 // Get the canonical version of the element with the extra qualifiers on it.
1550 // This can recursively sink qualifiers through multiple levels of arrays.
1551 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1552 NewEltTy = getCanonicalType(NewEltTy);
1553
1554 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1555 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1556 CAT->getIndexTypeQualifier());
1557 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1558 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1559 IAT->getIndexTypeQualifier());
1560
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001561 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1562 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1563 DSAT->getSizeModifier(),
1564 DSAT->getIndexTypeQualifier());
1565
Chris Lattnera1923f62008-08-04 07:31:14 +00001566 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1567 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1568 VAT->getSizeModifier(),
1569 VAT->getIndexTypeQualifier());
1570}
1571
1572
1573const ArrayType *ASTContext::getAsArrayType(QualType T) {
1574 // Handle the non-qualified case efficiently.
1575 if (T.getCVRQualifiers() == 0) {
1576 // Handle the common positive case fast.
1577 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1578 return AT;
1579 }
1580
1581 // Handle the common negative case fast, ignoring CVR qualifiers.
1582 QualType CType = T->getCanonicalTypeInternal();
1583
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001584 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001585 // test.
1586 if (!isa<ArrayType>(CType) &&
1587 !isa<ArrayType>(CType.getUnqualifiedType()))
1588 return 0;
1589
1590 // Apply any CVR qualifiers from the array type to the element type. This
1591 // implements C99 6.7.3p8: "If the specification of an array type includes
1592 // any type qualifiers, the element type is so qualified, not the array type."
1593
1594 // If we get here, we either have type qualifiers on the type, or we have
1595 // sugar such as a typedef in the way. If we have type qualifiers on the type
1596 // we must propagate them down into the elemeng type.
1597 unsigned CVRQuals = T.getCVRQualifiers();
1598 unsigned AddrSpace = 0;
1599 Type *Ty = T.getTypePtr();
1600
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001601 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001602 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001603 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1604 AddrSpace = EXTQT->getAddressSpace();
1605 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001606 } else {
1607 T = Ty->getDesugaredType();
1608 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1609 break;
1610 CVRQuals |= T.getCVRQualifiers();
1611 Ty = T.getTypePtr();
1612 }
1613 }
1614
1615 // If we have a simple case, just return now.
1616 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1617 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1618 return ATy;
1619
1620 // Otherwise, we have an array and we have qualifiers on it. Push the
1621 // qualifiers into the array element type and return a new array type.
1622 // Get the canonical version of the element with the extra qualifiers on it.
1623 // This can recursively sink qualifiers through multiple levels of arrays.
1624 QualType NewEltTy = ATy->getElementType();
1625 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001626 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001627 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1628
1629 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1630 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1631 CAT->getSizeModifier(),
1632 CAT->getIndexTypeQualifier()));
1633 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1634 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1635 IAT->getSizeModifier(),
1636 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001637
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001638 if (const DependentSizedArrayType *DSAT
1639 = dyn_cast<DependentSizedArrayType>(ATy))
1640 return cast<ArrayType>(
1641 getDependentSizedArrayType(NewEltTy,
1642 DSAT->getSizeExpr(),
1643 DSAT->getSizeModifier(),
1644 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001645
Chris Lattnera1923f62008-08-04 07:31:14 +00001646 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1647 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1648 VAT->getSizeModifier(),
1649 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001650}
1651
1652
Chris Lattner19eb97e2008-04-02 05:18:44 +00001653/// getArrayDecayedType - Return the properly qualified result of decaying the
1654/// specified array type to a pointer. This operation is non-trivial when
1655/// handling typedefs etc. The canonical type of "T" must be an array type,
1656/// this returns a pointer to a properly qualified element of the array.
1657///
1658/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1659QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001660 // Get the element type with 'getAsArrayType' so that we don't lose any
1661 // typedefs in the element type of the array. This also handles propagation
1662 // of type qualifiers from the array type into the element type if present
1663 // (C99 6.7.3p8).
1664 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1665 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001666
Chris Lattnera1923f62008-08-04 07:31:14 +00001667 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001668
1669 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001670 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001671}
1672
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001673QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001674 QualType ElemTy = VAT->getElementType();
1675
1676 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1677 return getBaseElementType(VAT);
1678
1679 return ElemTy;
1680}
1681
Chris Lattner4b009652007-07-25 00:24:17 +00001682/// getFloatingRank - Return a relative rank for floating point types.
1683/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001684static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001685 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001686 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001687
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001688 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001689 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001690 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001691 case BuiltinType::Float: return FloatRank;
1692 case BuiltinType::Double: return DoubleRank;
1693 case BuiltinType::LongDouble: return LongDoubleRank;
1694 }
1695}
1696
Steve Narofffa0c4532007-08-27 01:41:48 +00001697/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1698/// point or a complex type (based on typeDomain/typeSize).
1699/// 'typeDomain' is a real floating point or complex type.
1700/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001701QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1702 QualType Domain) const {
1703 FloatingRank EltRank = getFloatingRank(Size);
1704 if (Domain->isComplexType()) {
1705 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001706 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001707 case FloatRank: return FloatComplexTy;
1708 case DoubleRank: return DoubleComplexTy;
1709 case LongDoubleRank: return LongDoubleComplexTy;
1710 }
Chris Lattner4b009652007-07-25 00:24:17 +00001711 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001712
1713 assert(Domain->isRealFloatingType() && "Unknown domain!");
1714 switch (EltRank) {
1715 default: assert(0 && "getFloatingRank(): illegal value for rank");
1716 case FloatRank: return FloatTy;
1717 case DoubleRank: return DoubleTy;
1718 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001719 }
Chris Lattner4b009652007-07-25 00:24:17 +00001720}
1721
Chris Lattner51285d82008-04-06 23:55:33 +00001722/// getFloatingTypeOrder - Compare the rank of the two specified floating
1723/// point types, ignoring the domain of the type (i.e. 'double' ==
1724/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1725/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001726int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1727 FloatingRank LHSR = getFloatingRank(LHS);
1728 FloatingRank RHSR = getFloatingRank(RHS);
1729
1730 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001731 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001732 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001733 return 1;
1734 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001735}
1736
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001737/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1738/// routine will assert if passed a built-in type that isn't an integer or enum,
1739/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001740unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001741 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001742 if (EnumType* ET = dyn_cast<EnumType>(T))
1743 T = ET->getDecl()->getIntegerType().getTypePtr();
1744
1745 // There are two things which impact the integer rank: the width, and
1746 // the ordering of builtins. The builtin ordering is encoded in the
1747 // bottom three bits; the width is encoded in the bits above that.
1748 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1749 return FWIT->getWidth() << 3;
1750 }
1751
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001752 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001753 default: assert(0 && "getIntegerRank(): not a built-in integer");
1754 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001755 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001756 case BuiltinType::Char_S:
1757 case BuiltinType::Char_U:
1758 case BuiltinType::SChar:
1759 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001760 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001761 case BuiltinType::Short:
1762 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001763 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001764 case BuiltinType::Int:
1765 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001766 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001767 case BuiltinType::Long:
1768 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001769 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001770 case BuiltinType::LongLong:
1771 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001772 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001773 }
1774}
1775
Chris Lattner51285d82008-04-06 23:55:33 +00001776/// getIntegerTypeOrder - Returns the highest ranked integer type:
1777/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1778/// LHS < RHS, return -1.
1779int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001780 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1781 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001782 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001783
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001784 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1785 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001786
Chris Lattner51285d82008-04-06 23:55:33 +00001787 unsigned LHSRank = getIntegerRank(LHSC);
1788 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001789
Chris Lattner51285d82008-04-06 23:55:33 +00001790 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1791 if (LHSRank == RHSRank) return 0;
1792 return LHSRank > RHSRank ? 1 : -1;
1793 }
Chris Lattner4b009652007-07-25 00:24:17 +00001794
Chris Lattner51285d82008-04-06 23:55:33 +00001795 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1796 if (LHSUnsigned) {
1797 // If the unsigned [LHS] type is larger, return it.
1798 if (LHSRank >= RHSRank)
1799 return 1;
1800
1801 // If the signed type can represent all values of the unsigned type, it
1802 // wins. Because we are dealing with 2's complement and types that are
1803 // powers of two larger than each other, this is always safe.
1804 return -1;
1805 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001806
Chris Lattner51285d82008-04-06 23:55:33 +00001807 // If the unsigned [RHS] type is larger, return it.
1808 if (RHSRank >= LHSRank)
1809 return -1;
1810
1811 // If the signed type can represent all values of the unsigned type, it
1812 // wins. Because we are dealing with 2's complement and types that are
1813 // powers of two larger than each other, this is always safe.
1814 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001815}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001816
1817// getCFConstantStringType - Return the type used for constant CFStrings.
1818QualType ASTContext::getCFConstantStringType() {
1819 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001820 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001821 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001822 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001823 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001824
1825 // const int *isa;
1826 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001827 // int flags;
1828 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001829 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001830 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001831 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001832 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001833
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001834 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001835 for (unsigned i = 0; i < 4; ++i) {
1836 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1837 SourceLocation(), 0,
1838 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001839 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001840 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001841 }
1842
1843 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001844 }
1845
1846 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001847}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001848
Anders Carlssonf58cac72008-08-30 19:34:46 +00001849QualType ASTContext::getObjCFastEnumerationStateType()
1850{
1851 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001852 ObjCFastEnumerationStateTypeDecl =
1853 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1854 &Idents.get("__objcFastEnumerationState"));
1855
Anders Carlssonf58cac72008-08-30 19:34:46 +00001856 QualType FieldTypes[] = {
1857 UnsignedLongTy,
1858 getPointerType(ObjCIdType),
1859 getPointerType(UnsignedLongTy),
1860 getConstantArrayType(UnsignedLongTy,
1861 llvm::APInt(32, 5), ArrayType::Normal, 0)
1862 };
1863
Douglas Gregor8acb7272008-12-11 16:49:14 +00001864 for (size_t i = 0; i < 4; ++i) {
1865 FieldDecl *Field = FieldDecl::Create(*this,
1866 ObjCFastEnumerationStateTypeDecl,
1867 SourceLocation(), 0,
1868 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001869 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001870 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001871 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00001872
Douglas Gregor8acb7272008-12-11 16:49:14 +00001873 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001874 }
1875
1876 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1877}
1878
Anders Carlssone3f02572007-10-29 06:33:42 +00001879// This returns true if a type has been typedefed to BOOL:
1880// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001881static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001882 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00001883 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1884 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001885
1886 return false;
1887}
1888
Ted Kremenek42730c52008-01-07 19:49:32 +00001889/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001890/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001891int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001892 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001893
1894 // Make all integer and enum types at least as large as an int
1895 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001896 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001897 // Treat arrays as pointers, since that's how they're passed in.
1898 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001899 sz = getTypeSize(VoidPtrTy);
1900 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001901}
1902
Ted Kremenek42730c52008-01-07 19:49:32 +00001903/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001904/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001905void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00001906 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001907 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001908 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001909 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001910 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001911 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001912 // Compute size of all parameters.
1913 // Start with computing size of a pointer in number of bytes.
1914 // FIXME: There might(should) be a better way of doing this computation!
1915 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001916 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001917 // The first two arguments (self and _cmd) are pointers; account for
1918 // their size.
1919 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001920 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1921 E = Decl->param_end(); PI != E; ++PI) {
1922 QualType PType = (*PI)->getType();
1923 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001924 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001925 ParmOffset += sz;
1926 }
1927 S += llvm::utostr(ParmOffset);
1928 S += "@0:";
1929 S += llvm::utostr(PtrSize);
1930
1931 // Argument types.
1932 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00001933 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1934 E = Decl->param_end(); PI != E; ++PI) {
1935 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001936 QualType PType = PVDecl->getOriginalType();
1937 if (const ArrayType *AT =
1938 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1939 // Use array's original type only if it has known number of
1940 // elements.
1941 if (!dyn_cast<ConstantArrayType>(AT))
1942 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001943 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001944 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001945 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001946 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001947 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001948 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001949 }
1950}
1951
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001952/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001953/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001954/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1955/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001956/// Property attributes are stored as a comma-delimited C string. The simple
1957/// attributes readonly and bycopy are encoded as single characters. The
1958/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1959/// encoded as single characters, followed by an identifier. Property types
1960/// are also encoded as a parametrized attribute. The characters used to encode
1961/// these attributes are defined by the following enumeration:
1962/// @code
1963/// enum PropertyAttributes {
1964/// kPropertyReadOnly = 'R', // property is read-only.
1965/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1966/// kPropertyByref = '&', // property is a reference to the value last assigned
1967/// kPropertyDynamic = 'D', // property is dynamic
1968/// kPropertyGetter = 'G', // followed by getter selector name
1969/// kPropertySetter = 'S', // followed by setter selector name
1970/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1971/// kPropertyType = 't' // followed by old-style type encoding.
1972/// kPropertyWeak = 'W' // 'weak' property
1973/// kPropertyStrong = 'P' // property GC'able
1974/// kPropertyNonAtomic = 'N' // property non-atomic
1975/// };
1976/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001977void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1978 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00001979 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001980 // Collect information from the property implementation decl(s).
1981 bool Dynamic = false;
1982 ObjCPropertyImplDecl *SynthesizePID = 0;
1983
1984 // FIXME: Duplicated code due to poor abstraction.
1985 if (Container) {
1986 if (const ObjCCategoryImplDecl *CID =
1987 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1988 for (ObjCCategoryImplDecl::propimpl_iterator
1989 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1990 ObjCPropertyImplDecl *PID = *i;
1991 if (PID->getPropertyDecl() == PD) {
1992 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1993 Dynamic = true;
1994 } else {
1995 SynthesizePID = PID;
1996 }
1997 }
1998 }
1999 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002000 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002001 for (ObjCCategoryImplDecl::propimpl_iterator
2002 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2003 ObjCPropertyImplDecl *PID = *i;
2004 if (PID->getPropertyDecl() == PD) {
2005 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2006 Dynamic = true;
2007 } else {
2008 SynthesizePID = PID;
2009 }
2010 }
2011 }
2012 }
2013 }
2014
2015 // FIXME: This is not very efficient.
2016 S = "T";
2017
2018 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002019 // GCC has some special rules regarding encoding of properties which
2020 // closely resembles encoding of ivars.
2021 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
2022 true /* outermost type */,
2023 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002024
2025 if (PD->isReadOnly()) {
2026 S += ",R";
2027 } else {
2028 switch (PD->getSetterKind()) {
2029 case ObjCPropertyDecl::Assign: break;
2030 case ObjCPropertyDecl::Copy: S += ",C"; break;
2031 case ObjCPropertyDecl::Retain: S += ",&"; break;
2032 }
2033 }
2034
2035 // It really isn't clear at all what this means, since properties
2036 // are "dynamic by default".
2037 if (Dynamic)
2038 S += ",D";
2039
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002040 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2041 S += ",N";
2042
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002043 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2044 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002045 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002046 }
2047
2048 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2049 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002050 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002051 }
2052
2053 if (SynthesizePID) {
2054 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2055 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002056 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002057 }
2058
2059 // FIXME: OBJCGC: weak & strong
2060}
2061
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002062/// getLegacyIntegralTypeEncoding -
2063/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002064/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002065/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2066///
2067void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2068 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2069 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002070 if (BT->getKind() == BuiltinType::ULong &&
2071 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002072 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002073 else
2074 if (BT->getKind() == BuiltinType::Long &&
2075 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002076 PointeeTy = IntTy;
2077 }
2078 }
2079}
2080
Fariborz Jahanian248db262008-01-22 22:44:46 +00002081void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002082 FieldDecl *Field) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002083 // We follow the behavior of gcc, expanding structures which are
2084 // directly pointed to, and expanding embedded structures. Note that
2085 // these rules are sufficient to prevent recursive encoding of the
2086 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002087 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2088 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002089}
2090
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002091static void EncodeBitField(const ASTContext *Context, std::string& S,
2092 FieldDecl *FD) {
2093 const Expr *E = FD->getBitWidth();
2094 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2095 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2096 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2097 S += 'b';
2098 S += llvm::utostr(N);
2099}
2100
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002101void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2102 bool ExpandPointedToStructures,
2103 bool ExpandStructures,
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002104 FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002105 bool OutermostType,
2106 bool EncodingProperty) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00002107 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002108 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002109 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002110 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002111 else {
2112 char encoding;
2113 switch (BT->getKind()) {
2114 default: assert(0 && "Unhandled builtin type kind");
2115 case BuiltinType::Void: encoding = 'v'; break;
2116 case BuiltinType::Bool: encoding = 'B'; break;
2117 case BuiltinType::Char_U:
2118 case BuiltinType::UChar: encoding = 'C'; break;
2119 case BuiltinType::UShort: encoding = 'S'; break;
2120 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002121 case BuiltinType::ULong:
2122 encoding =
2123 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2124 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002125 case BuiltinType::ULongLong: encoding = 'Q'; break;
2126 case BuiltinType::Char_S:
2127 case BuiltinType::SChar: encoding = 'c'; break;
2128 case BuiltinType::Short: encoding = 's'; break;
2129 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002130 case BuiltinType::Long:
2131 encoding =
2132 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2133 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002134 case BuiltinType::LongLong: encoding = 'q'; break;
2135 case BuiltinType::Float: encoding = 'f'; break;
2136 case BuiltinType::Double: encoding = 'd'; break;
2137 case BuiltinType::LongDouble: encoding = 'd'; break;
2138 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002139
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002140 S += encoding;
2141 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002142 }
Ted Kremenek42730c52008-01-07 19:49:32 +00002143 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002144 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2145 ExpandPointedToStructures,
2146 ExpandStructures, FD);
2147 if (FD || EncodingProperty) {
2148 // Note that we do extended encoding of protocol qualifer list
2149 // Only when doing ivar or property encoding.
2150 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2151 S += '"';
2152 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2153 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2154 S += '<';
2155 S += Proto->getNameAsString();
2156 S += '>';
2157 }
2158 S += '"';
2159 }
2160 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002161 }
2162 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002163 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002164 bool isReadOnly = false;
2165 // For historical/compatibility reasons, the read-only qualifier of the
2166 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2167 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2168 // Also, do not emit the 'r' for anything but the outermost type!
2169 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2170 if (OutermostType && T.isConstQualified()) {
2171 isReadOnly = true;
2172 S += 'r';
2173 }
2174 }
2175 else if (OutermostType) {
2176 QualType P = PointeeTy;
2177 while (P->getAsPointerType())
2178 P = P->getAsPointerType()->getPointeeType();
2179 if (P.isConstQualified()) {
2180 isReadOnly = true;
2181 S += 'r';
2182 }
2183 }
2184 if (isReadOnly) {
2185 // Another legacy compatibility encoding. Some ObjC qualifier and type
2186 // combinations need to be rearranged.
2187 // Rewrite "in const" from "nr" to "rn"
2188 const char * s = S.c_str();
2189 int len = S.length();
2190 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2191 std::string replace = "rn";
2192 S.replace(S.end()-2, S.end(), replace);
2193 }
2194 }
Steve Naroff17c03822009-02-12 17:52:19 +00002195 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002196 S += '@';
2197 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002198 }
2199 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002200 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002201 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002202 // Another historical/compatibility reason.
2203 // We encode the underlying type which comes out as
2204 // {...};
2205 S += '^';
2206 getObjCEncodingForTypeImpl(PointeeTy, S,
2207 false, ExpandPointedToStructures,
2208 NULL);
2209 return;
2210 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002211 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002212 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002213 const ObjCInterfaceType *OIT =
2214 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002215 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002216 S += '"';
2217 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002218 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2219 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2220 S += '<';
2221 S += Proto->getNameAsString();
2222 S += '>';
2223 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002224 S += '"';
2225 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002226 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002227 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002228 S += '#';
2229 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002230 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002231 S += ':';
2232 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002233 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002234
2235 if (PointeeTy->isCharType()) {
2236 // char pointer types should be encoded as '*' unless it is a
2237 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002238 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002239 S += '*';
2240 return;
2241 }
2242 }
2243
2244 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002245 getLegacyIntegralTypeEncoding(PointeeTy);
2246
2247 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002248 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002249 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002250 } else if (const ArrayType *AT =
2251 // Ignore type qualifiers etc.
2252 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002253 if (isa<IncompleteArrayType>(AT)) {
2254 // Incomplete arrays are encoded as a pointer to the array element.
2255 S += '^';
2256
2257 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2258 false, ExpandStructures, FD);
2259 } else {
2260 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002261
Anders Carlsson858c64d2009-02-22 01:38:57 +00002262 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2263 S += llvm::utostr(CAT->getSize().getZExtValue());
2264 else {
2265 //Variable length arrays are encoded as a regular array with 0 elements.
2266 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2267 S += '0';
2268 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002269
Anders Carlsson858c64d2009-02-22 01:38:57 +00002270 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2271 false, ExpandStructures, FD);
2272 S += ']';
2273 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002274 } else if (T->getAsFunctionType()) {
2275 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002276 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002277 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002278 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002279 // Anonymous structures print as '?'
2280 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2281 S += II->getName();
2282 } else {
2283 S += '?';
2284 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002285 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002286 S += '=';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002287 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2288 FieldEnd = RDecl->field_end();
2289 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002290 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002291 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002292 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002293 S += '"';
2294 }
2295
2296 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002297 if (Field->isBitField()) {
2298 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2299 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002300 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002301 QualType qt = Field->getType();
2302 getLegacyIntegralTypeEncoding(qt);
2303 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002304 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002305 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002306 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002307 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002308 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002309 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002310 if (FD && FD->isBitField())
2311 EncodeBitField(this, S, FD);
2312 else
2313 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002314 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002315 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002316 } else if (T->isObjCInterfaceType()) {
2317 // @encode(class_name)
2318 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2319 S += '{';
2320 const IdentifierInfo *II = OI->getIdentifier();
2321 S += II->getName();
2322 S += '=';
2323 std::vector<FieldDecl*> RecFields;
2324 CollectObjCIvars(OI, RecFields);
2325 for (unsigned int i = 0; i != RecFields.size(); i++) {
2326 if (RecFields[i]->isBitField())
2327 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2328 RecFields[i]);
2329 else
2330 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2331 FD);
2332 }
2333 S += '}';
2334 }
2335 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002336 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002337}
2338
Ted Kremenek42730c52008-01-07 19:49:32 +00002339void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002340 std::string& S) const {
2341 if (QT & Decl::OBJC_TQ_In)
2342 S += 'n';
2343 if (QT & Decl::OBJC_TQ_Inout)
2344 S += 'N';
2345 if (QT & Decl::OBJC_TQ_Out)
2346 S += 'o';
2347 if (QT & Decl::OBJC_TQ_Bycopy)
2348 S += 'O';
2349 if (QT & Decl::OBJC_TQ_Byref)
2350 S += 'R';
2351 if (QT & Decl::OBJC_TQ_Oneway)
2352 S += 'V';
2353}
2354
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002355void ASTContext::setBuiltinVaListType(QualType T)
2356{
2357 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2358
2359 BuiltinVaListType = T;
2360}
2361
Ted Kremenek42730c52008-01-07 19:49:32 +00002362void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002363{
Ted Kremenek42730c52008-01-07 19:49:32 +00002364 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002365
2366 // typedef struct objc_object *id;
2367 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002368 // User error - caller will issue diagnostics.
2369 if (!ptr)
2370 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002371 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002372 // User error - caller will issue diagnostics.
2373 if (!rec)
2374 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002375 IdStructType = rec;
2376}
2377
Ted Kremenek42730c52008-01-07 19:49:32 +00002378void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002379{
Ted Kremenek42730c52008-01-07 19:49:32 +00002380 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002381
2382 // typedef struct objc_selector *SEL;
2383 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002384 if (!ptr)
2385 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002386 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002387 if (!rec)
2388 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002389 SelStructType = rec;
2390}
2391
Ted Kremenek42730c52008-01-07 19:49:32 +00002392void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002393{
Ted Kremenek42730c52008-01-07 19:49:32 +00002394 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002395}
2396
Ted Kremenek42730c52008-01-07 19:49:32 +00002397void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002398{
Ted Kremenek42730c52008-01-07 19:49:32 +00002399 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002400
2401 // typedef struct objc_class *Class;
2402 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2403 assert(ptr && "'Class' incorrectly typed");
2404 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2405 assert(rec && "'Class' incorrectly typed");
2406 ClassStructType = rec;
2407}
2408
Ted Kremenek42730c52008-01-07 19:49:32 +00002409void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2410 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002411 "'NSConstantString' type already set!");
2412
Ted Kremenek42730c52008-01-07 19:49:32 +00002413 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002414}
2415
Douglas Gregorc6507e42008-11-03 14:12:49 +00002416/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002417/// TargetInfo, produce the corresponding type. The unsigned @p Type
2418/// is actually a value of type @c TargetInfo::IntType.
2419QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002420 switch (Type) {
2421 case TargetInfo::NoInt: return QualType();
2422 case TargetInfo::SignedShort: return ShortTy;
2423 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2424 case TargetInfo::SignedInt: return IntTy;
2425 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2426 case TargetInfo::SignedLong: return LongTy;
2427 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2428 case TargetInfo::SignedLongLong: return LongLongTy;
2429 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2430 }
2431
2432 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002433 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002434}
Ted Kremenek118930e2008-07-24 23:58:27 +00002435
2436//===----------------------------------------------------------------------===//
2437// Type Predicates.
2438//===----------------------------------------------------------------------===//
2439
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002440/// isObjCNSObjectType - Return true if this is an NSObject object using
2441/// NSObject attribute on a c-style pointer type.
2442/// FIXME - Make it work directly on types.
2443///
2444bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2445 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2446 if (TypedefDecl *TD = TDT->getDecl())
2447 if (TD->getAttr<ObjCNSObjectAttr>())
2448 return true;
2449 }
2450 return false;
2451}
2452
Ted Kremenek118930e2008-07-24 23:58:27 +00002453/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2454/// to an object type. This includes "id" and "Class" (two 'special' pointers
2455/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2456/// ID type).
2457bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroff6805fc42009-02-23 18:36:16 +00002458 if (Ty->isObjCQualifiedIdType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002459 return true;
2460
Steve Naroffd9e00802008-10-21 18:24:04 +00002461 // Blocks are objects.
2462 if (Ty->isBlockPointerType())
2463 return true;
2464
2465 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00002466 if (!Ty->isPointerType())
2467 return false;
2468
2469 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2470 // pointer types. This looks for the typedef specifically, not for the
2471 // underlying type.
Eli Friedman9c2b33f2009-03-22 23:00:19 +00002472 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2473 Ty.getUnqualifiedType() == getObjCClassType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002474 return true;
2475
2476 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002477 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2478 return true;
2479
2480 // If is has NSObject attribute, OK as well.
2481 return isObjCNSObjectType(Ty);
Ted Kremenek118930e2008-07-24 23:58:27 +00002482}
2483
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002484/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2485/// garbage collection attribute.
2486///
2487QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002488 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002489 if (getLangOptions().ObjC1 &&
2490 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002491 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002492 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002493 // (or pointers to them) be treated as though they were declared
2494 // as __strong.
2495 if (GCAttrs == QualType::GCNone) {
2496 if (isObjCObjectPointerType(Ty))
2497 GCAttrs = QualType::Strong;
2498 else if (Ty->isPointerType())
2499 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2500 }
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002501 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002502 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002503}
2504
Chris Lattner6ff358b2008-04-07 06:51:04 +00002505//===----------------------------------------------------------------------===//
2506// Type Compatibility Testing
2507//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002508
Steve Naroff3454b6c2008-09-04 15:10:53 +00002509/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002510/// block types. Types must be strictly compatible here. For example,
2511/// C unfortunately doesn't produce an error for the following:
2512///
2513/// int (*emptyArgFunc)();
2514/// int (*intArgList)(int) = emptyArgFunc;
2515///
2516/// For blocks, we will produce an error for the following (similar to C++):
2517///
2518/// int (^emptyArgBlock)();
2519/// int (^intArgBlock)(int) = emptyArgBlock;
2520///
2521/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2522///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002523bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002524 const FunctionType *lbase = lhs->getAsFunctionType();
2525 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002526 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2527 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002528 if (lproto && rproto)
2529 return !mergeTypes(lhs, rhs).isNull();
2530 return false;
Steve Naroff3454b6c2008-09-04 15:10:53 +00002531}
2532
Chris Lattner6ff358b2008-04-07 06:51:04 +00002533/// areCompatVectorTypes - Return true if the two specified vector types are
2534/// compatible.
2535static bool areCompatVectorTypes(const VectorType *LHS,
2536 const VectorType *RHS) {
2537 assert(LHS->isCanonical() && RHS->isCanonical());
2538 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002539 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002540}
2541
Eli Friedman0d9549b2008-08-22 00:56:42 +00002542/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002543/// compatible for assignment from RHS to LHS. This handles validation of any
2544/// protocol qualifiers on the LHS or RHS.
2545///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002546bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2547 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002548 // Verify that the base decls are compatible: the RHS must be a subclass of
2549 // the LHS.
2550 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2551 return false;
2552
2553 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2554 // protocol qualified at all, then we are good.
2555 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2556 return true;
2557
2558 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2559 // isn't a superset.
2560 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2561 return true; // FIXME: should return false!
2562
2563 // Finally, we must have two protocol-qualified interfaces.
2564 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2565 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ff358b2008-04-07 06:51:04 +00002566
Steve Naroff98e71b82009-03-01 16:12:44 +00002567 // All LHS protocols must have a presence on the RHS.
2568 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ff358b2008-04-07 06:51:04 +00002569
Steve Naroff98e71b82009-03-01 16:12:44 +00002570 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2571 LHSPE = LHSP->qual_end();
2572 LHSPI != LHSPE; LHSPI++) {
2573 bool RHSImplementsProtocol = false;
2574
2575 // If the RHS doesn't implement the protocol on the left, the types
2576 // are incompatible.
2577 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2578 RHSPE = RHSP->qual_end();
2579 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2580 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2581 RHSImplementsProtocol = true;
2582 }
2583 // FIXME: For better diagnostics, consider passing back the protocol name.
2584 if (!RHSImplementsProtocol)
2585 return false;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002586 }
Steve Naroff98e71b82009-03-01 16:12:44 +00002587 // The RHS implements all protocols listed on the LHS.
2588 return true;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002589}
2590
Steve Naroff17c03822009-02-12 17:52:19 +00002591bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2592 // get the "pointed to" types
2593 const PointerType *LHSPT = LHS->getAsPointerType();
2594 const PointerType *RHSPT = RHS->getAsPointerType();
2595
2596 if (!LHSPT || !RHSPT)
2597 return false;
2598
2599 QualType lhptee = LHSPT->getPointeeType();
2600 QualType rhptee = RHSPT->getPointeeType();
2601 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2602 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2603 // ID acts sort of like void* for ObjC interfaces
2604 if (LHSIface && isObjCIdStructType(rhptee))
2605 return true;
2606 if (RHSIface && isObjCIdStructType(lhptee))
2607 return true;
2608 if (!LHSIface || !RHSIface)
2609 return false;
2610 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2611 canAssignObjCInterfaces(RHSIface, LHSIface);
2612}
2613
Steve Naroff85f0dc52007-10-15 20:41:53 +00002614/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2615/// both shall have the identically qualified version of a compatible type.
2616/// C99 6.2.7p1: Two types have compatible types if their types are the
2617/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002618bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2619 return !mergeTypes(LHS, RHS).isNull();
2620}
2621
2622QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2623 const FunctionType *lbase = lhs->getAsFunctionType();
2624 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002625 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2626 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002627 bool allLTypes = true;
2628 bool allRTypes = true;
2629
2630 // Check return type
2631 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2632 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002633 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2634 allLTypes = false;
2635 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2636 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002637
2638 if (lproto && rproto) { // two C99 style function prototypes
2639 unsigned lproto_nargs = lproto->getNumArgs();
2640 unsigned rproto_nargs = rproto->getNumArgs();
2641
2642 // Compatible functions must have the same number of arguments
2643 if (lproto_nargs != rproto_nargs)
2644 return QualType();
2645
2646 // Variadic and non-variadic functions aren't compatible
2647 if (lproto->isVariadic() != rproto->isVariadic())
2648 return QualType();
2649
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002650 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2651 return QualType();
2652
Eli Friedman0d9549b2008-08-22 00:56:42 +00002653 // Check argument compatibility
2654 llvm::SmallVector<QualType, 10> types;
2655 for (unsigned i = 0; i < lproto_nargs; i++) {
2656 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2657 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2658 QualType argtype = mergeTypes(largtype, rargtype);
2659 if (argtype.isNull()) return QualType();
2660 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002661 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2662 allLTypes = false;
2663 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2664 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002665 }
2666 if (allLTypes) return lhs;
2667 if (allRTypes) return rhs;
2668 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002669 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002670 }
2671
2672 if (lproto) allRTypes = false;
2673 if (rproto) allLTypes = false;
2674
Douglas Gregor4fa58902009-02-26 23:50:07 +00002675 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002676 if (proto) {
2677 if (proto->isVariadic()) return QualType();
2678 // Check that the types are compatible with the types that
2679 // would result from default argument promotions (C99 6.7.5.3p15).
2680 // The only types actually affected are promotable integer
2681 // types and floats, which would be passed as a different
2682 // type depending on whether the prototype is visible.
2683 unsigned proto_nargs = proto->getNumArgs();
2684 for (unsigned i = 0; i < proto_nargs; ++i) {
2685 QualType argTy = proto->getArgType(i);
2686 if (argTy->isPromotableIntegerType() ||
2687 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2688 return QualType();
2689 }
2690
2691 if (allLTypes) return lhs;
2692 if (allRTypes) return rhs;
2693 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002694 proto->getNumArgs(), lproto->isVariadic(),
2695 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002696 }
2697
2698 if (allLTypes) return lhs;
2699 if (allRTypes) return rhs;
Douglas Gregor4fa58902009-02-26 23:50:07 +00002700 return getFunctionNoProtoType(retType);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002701}
2702
2703QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002704 // C++ [expr]: If an expression initially has the type "reference to T", the
2705 // type is adjusted to "T" prior to any further analysis, the expression
2706 // designates the object or function denoted by the reference, and the
Sebastian Redlce6fff02009-03-16 23:22:08 +00002707 // expression is an lvalue unless the reference is an rvalue reference and
2708 // the expression is a function call (possibly inside parentheses).
Eli Friedman0d9549b2008-08-22 00:56:42 +00002709 // FIXME: C++ shouldn't be going through here! The rules are different
2710 // enough that they should be handled separately.
Sebastian Redlce6fff02009-03-16 23:22:08 +00002711 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2712 // shouldn't be going through here!
Eli Friedman0d9549b2008-08-22 00:56:42 +00002713 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002714 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002715 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002716 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002717
Eli Friedman0d9549b2008-08-22 00:56:42 +00002718 QualType LHSCan = getCanonicalType(LHS),
2719 RHSCan = getCanonicalType(RHS);
2720
2721 // If two types are identical, they are compatible.
2722 if (LHSCan == RHSCan)
2723 return LHS;
2724
2725 // If the qualifiers are different, the types aren't compatible
Eli Friedman94fcc9a2009-02-27 23:04:43 +00002726 // Note that we handle extended qualifiers later, in the
2727 // case for ExtQualType.
2728 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman0d9549b2008-08-22 00:56:42 +00002729 return QualType();
2730
2731 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2732 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2733
Chris Lattnerc38d4522008-01-14 05:45:46 +00002734 // We want to consider the two function types to be the same for these
2735 // comparisons, just force one to the other.
2736 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2737 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002738
2739 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002740 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2741 LHSClass = Type::ConstantArray;
2742 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2743 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002744
Nate Begemanaf6ed502008-04-18 23:10:10 +00002745 // Canonicalize ExtVector -> Vector.
2746 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2747 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002748
Chris Lattner7cdcb252008-04-07 06:38:24 +00002749 // Consider qualified interfaces and interfaces the same.
2750 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2751 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002752
Chris Lattnerb5709e22008-04-07 05:43:21 +00002753 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002754 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00002755 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2756 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2757
2758 // ID acts sort of like void* for ObjC interfaces
2759 if (LHSIface && isObjCIdStructType(RHS))
2760 return LHS;
2761 if (RHSIface && isObjCIdStructType(LHS))
2762 return RHS;
2763
Steve Naroff28ceff72008-12-10 22:14:21 +00002764 // ID is compatible with all qualified id types.
2765 if (LHS->isObjCQualifiedIdType()) {
2766 if (const PointerType *PT = RHS->getAsPointerType()) {
2767 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002768 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002769 return LHS;
2770 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2771 // Unfortunately, this API is part of Sema (which we don't have access
2772 // to. Need to refactor. The following check is insufficient, since we
2773 // need to make sure the class implements the protocol.
2774 if (pType->isObjCInterfaceType())
2775 return LHS;
2776 }
2777 }
2778 if (RHS->isObjCQualifiedIdType()) {
2779 if (const PointerType *PT = LHS->getAsPointerType()) {
2780 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002781 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002782 return RHS;
2783 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2784 // Unfortunately, this API is part of Sema (which we don't have access
2785 // to. Need to refactor. The following check is insufficient, since we
2786 // need to make sure the class implements the protocol.
2787 if (pType->isObjCInterfaceType())
2788 return RHS;
2789 }
2790 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002791 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2792 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002793 if (const EnumType* ETy = LHS->getAsEnumType()) {
2794 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2795 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002796 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002797 if (const EnumType* ETy = RHS->getAsEnumType()) {
2798 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2799 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002800 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002801
Eli Friedman0d9549b2008-08-22 00:56:42 +00002802 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002803 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002804
Steve Naroffc88babe2008-01-09 22:43:08 +00002805 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002806 switch (LHSClass) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00002807#define TYPE(Class, Base)
2808#define ABSTRACT_TYPE(Class, Base)
2809#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2810#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2811#include "clang/AST/TypeNodes.def"
2812 assert(false && "Non-canonical and dependent types shouldn't get here");
2813 return QualType();
2814
Sebastian Redlce6fff02009-03-16 23:22:08 +00002815 case Type::LValueReference:
2816 case Type::RValueReference:
Douglas Gregor4fa58902009-02-26 23:50:07 +00002817 case Type::MemberPointer:
2818 assert(false && "C++ should never be in mergeTypes");
2819 return QualType();
2820
2821 case Type::IncompleteArray:
2822 case Type::VariableArray:
2823 case Type::FunctionProto:
2824 case Type::ExtVector:
2825 case Type::ObjCQualifiedInterface:
2826 assert(false && "Types are eliminated above");
2827 return QualType();
2828
Chris Lattnerc38d4522008-01-14 05:45:46 +00002829 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002830 {
2831 // Merge two pointer types, while trying to preserve typedef info
2832 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2833 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2834 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2835 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002836 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2837 return LHS;
2838 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2839 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002840 return getPointerType(ResultType);
2841 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002842 case Type::BlockPointer:
2843 {
2844 // Merge two block pointer types, while trying to preserve typedef info
2845 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2846 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2847 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2848 if (ResultType.isNull()) return QualType();
2849 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2850 return LHS;
2851 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2852 return RHS;
2853 return getBlockPointerType(ResultType);
2854 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002855 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002856 {
2857 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2858 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2859 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2860 return QualType();
2861
2862 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2863 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2864 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2865 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002866 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2867 return LHS;
2868 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2869 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002870 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2871 ArrayType::ArraySizeModifier(), 0);
2872 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2873 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002874 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2875 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002876 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2877 return LHS;
2878 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2879 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002880 if (LVAT) {
2881 // FIXME: This isn't correct! But tricky to implement because
2882 // the array's size has to be the size of LHS, but the type
2883 // has to be different.
2884 return LHS;
2885 }
2886 if (RVAT) {
2887 // FIXME: This isn't correct! But tricky to implement because
2888 // the array's size has to be the size of RHS, but the type
2889 // has to be different.
2890 return RHS;
2891 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002892 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2893 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002894 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002895 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002896 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002897 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor4fa58902009-02-26 23:50:07 +00002898 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +00002899 case Type::Enum:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002900 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00002901 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
2902 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002903 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002904 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002905 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002906 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00002907 case Type::Complex:
2908 // Distinct complex types are incompatible.
2909 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002910 case Type::Vector:
Eli Friedman94fcc9a2009-02-27 23:04:43 +00002911 // FIXME: The merged type should be an ExtVector!
Eli Friedman0d9549b2008-08-22 00:56:42 +00002912 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2913 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002914 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00002915 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00002916 // Check if the interfaces are assignment compatible.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00002917 // FIXME: This should be type compatibility, e.g. whether
2918 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff0bbc1352009-02-21 16:18:07 +00002919 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2920 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2921 if (LHSIface && RHSIface &&
2922 canAssignObjCInterfaces(LHSIface, RHSIface))
2923 return LHS;
2924
Eli Friedman0d9549b2008-08-22 00:56:42 +00002925 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00002926 }
Steve Naroff28ceff72008-12-10 22:14:21 +00002927 case Type::ObjCQualifiedId:
2928 // Distinct qualified id's are not compatible.
2929 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00002930 case Type::FixedWidthInt:
2931 // Distinct fixed-width integers are not compatible.
2932 return QualType();
2933 case Type::ObjCQualifiedClass:
2934 // Distinct qualified classes are not compatible.
2935 return QualType();
2936 case Type::ExtQual:
2937 // FIXME: ExtQual types can be compatible even if they're not
2938 // identical!
2939 return QualType();
2940 // First attempt at an implementation, but I'm not really sure it's
2941 // right...
2942#if 0
2943 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
2944 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
2945 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
2946 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
2947 return QualType();
2948 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
2949 LHSBase = QualType(LQual->getBaseType(), 0);
2950 RHSBase = QualType(RQual->getBaseType(), 0);
2951 ResultType = mergeTypes(LHSBase, RHSBase);
2952 if (ResultType.isNull()) return QualType();
2953 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
2954 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
2955 return LHS;
2956 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
2957 return RHS;
2958 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
2959 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
2960 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
2961 return ResultType;
2962#endif
Steve Naroff85f0dc52007-10-15 20:41:53 +00002963 }
Douglas Gregor4fa58902009-02-26 23:50:07 +00002964
2965 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002966}
Ted Kremenek738e6c02007-10-31 17:10:13 +00002967
Chris Lattner1d78a862008-04-07 07:01:58 +00002968//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00002969// Integer Predicates
2970//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00002971
Eli Friedman0832dbc2008-06-28 06:23:08 +00002972unsigned ASTContext::getIntWidth(QualType T) {
2973 if (T == BoolTy)
2974 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002975 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
2976 return FWIT->getWidth();
2977 }
2978 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00002979 return (unsigned)getTypeSize(T);
2980}
2981
2982QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2983 assert(T->isSignedIntegerType() && "Unexpected type");
2984 if (const EnumType* ETy = T->getAsEnumType())
2985 T = ETy->getDecl()->getIntegerType();
2986 const BuiltinType* BTy = T->getAsBuiltinType();
2987 assert (BTy && "Unexpected signed integer type");
2988 switch (BTy->getKind()) {
2989 case BuiltinType::Char_S:
2990 case BuiltinType::SChar:
2991 return UnsignedCharTy;
2992 case BuiltinType::Short:
2993 return UnsignedShortTy;
2994 case BuiltinType::Int:
2995 return UnsignedIntTy;
2996 case BuiltinType::Long:
2997 return UnsignedLongTy;
2998 case BuiltinType::LongLong:
2999 return UnsignedLongLongTy;
3000 default:
3001 assert(0 && "Unexpected signed integer type");
3002 return QualType();
3003 }
3004}
3005
3006
3007//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00003008// Serialization Support
3009//===----------------------------------------------------------------------===//
3010
Ted Kremenek738e6c02007-10-31 17:10:13 +00003011/// Emit - Serialize an ASTContext object to Bitcode.
3012void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00003013 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00003014 S.EmitRef(SourceMgr);
3015 S.EmitRef(Target);
3016 S.EmitRef(Idents);
3017 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003018
Ted Kremenek68228a92007-10-31 22:44:07 +00003019 // Emit the size of the type vector so that we can reserve that size
3020 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003021 S.EmitInt(Types.size());
3022
Ted Kremenek034a78c2007-11-13 22:02:55 +00003023 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3024 I!=E;++I)
3025 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003026
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003027 S.EmitOwnedPtr(TUDecl);
3028
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003029 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003030}
3031
Ted Kremenekacba3612007-11-13 00:25:37 +00003032ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00003033
3034 // Read the language options.
3035 LangOptions LOpts;
3036 LOpts.Read(D);
3037
Ted Kremenek68228a92007-10-31 22:44:07 +00003038 SourceManager &SM = D.ReadRef<SourceManager>();
3039 TargetInfo &t = D.ReadRef<TargetInfo>();
3040 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3041 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00003042
Ted Kremenek68228a92007-10-31 22:44:07 +00003043 unsigned size_reserve = D.ReadInt();
3044
Douglas Gregor24afd4a2008-11-17 14:58:09 +00003045 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3046 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00003047
Ted Kremenek034a78c2007-11-13 22:02:55 +00003048 for (unsigned i = 0; i < size_reserve; ++i)
3049 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00003050
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003051 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3052
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003053 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00003054
3055 return A;
3056}