blob: c318107dab324479d2954ca23a195794c4f8282f [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000021#include "llvm/ADT/StringExtras.h"
Ted Kremenek7192f8e2007-10-31 17:10:13 +000022#include "llvm/Bitcode/Serialize.h"
23#include "llvm/Bitcode/Deserialize.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000025
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner61710852008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Steve Naroffc0ac4922009-01-27 23:20:32 +000035 bool FreeMem, unsigned size_reserve) :
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +000036 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
Steve Naroffc0ac4922009-01-27 23:20:32 +000037 SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
Douglas Gregor2e1cd422008-11-17 14:58:09 +000038 Idents(idents), Selectors(sels)
Daniel Dunbare91593e2008-08-11 04:54:23 +000039{
40 if (size_reserve > 0) Types.reserve(size_reserve);
41 InitBuiltinTypes();
Chris Lattner7644f072009-03-13 22:38:49 +000042 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.NoBuiltin);
Daniel Dunbare91593e2008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
44}
45
Reid Spencer5f016e22007-07-11 17:01:13 +000046ASTContext::~ASTContext() {
47 // Deallocate all the types.
48 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000049 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000050 Types.pop_back();
51 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000052
Nuno Lopesb74668e2008-12-17 22:30:25 +000053 {
54 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
55 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
56 while (I != E) {
57 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
58 delete R;
59 }
60 }
61
62 {
63 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
64 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
65 while (I != E) {
66 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
67 delete R;
68 }
69 }
70
71 {
72 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator
73 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
74 while (I != E) {
75 RecordDecl *R = const_cast<RecordDecl*>((I++)->second);
76 R->Destroy(*this);
77 }
78 }
79
Eli Friedmanb26153c2008-05-27 03:08:09 +000080 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000081}
82
83void ASTContext::PrintStats() const {
84 fprintf(stderr, "*** AST Context Stats:\n");
85 fprintf(stderr, " %d types total.\n", (int)Types.size());
86 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +000087 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +000088 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
89 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
90
Reid Spencer5f016e22007-07-11 17:01:13 +000091 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000092 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
93 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +000094 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000095
96 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
97 Type *T = Types[i];
98 if (isa<BuiltinType>(T))
99 ++NumBuiltin;
100 else if (isa<PointerType>(T))
101 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000102 else if (isa<BlockPointerType>(T))
103 ++NumBlockPointer;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000104 else if (isa<LValueReferenceType>(T))
105 ++NumLValueReference;
106 else if (isa<RValueReferenceType>(T))
107 ++NumRValueReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000108 else if (isa<MemberPointerType>(T))
109 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000110 else if (isa<ComplexType>(T))
111 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 else if (isa<ArrayType>(T))
113 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000114 else if (isa<VectorType>(T))
115 ++NumVector;
Douglas Gregor72564e72009-02-26 23:50:07 +0000116 else if (isa<FunctionNoProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 ++NumFunctionNP;
Douglas Gregor72564e72009-02-26 23:50:07 +0000118 else if (isa<FunctionProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 ++NumFunctionP;
120 else if (isa<TypedefType>(T))
121 ++NumTypeName;
122 else if (TagType *TT = dyn_cast<TagType>(T)) {
123 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000124 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 }
Ted Kremeneka526c5c2008-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 Naroff6cc18962008-05-21 15:59:22 +0000137 else if (isa<TypeOfType>(T))
138 ++NumTypeOfTypes;
Douglas Gregor72564e72009-02-26 23:50:07 +0000139 else if (isa<TypeOfExprType>(T))
140 ++NumTypeOfExprTypes;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000141 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000142 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +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 Dunbar248e1c02008-09-26 03:23:00 +0000149 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000150 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
151 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000152 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000153 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000155 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 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 Kremeneka526c5c2008-01-07 19:49:32 +0000164 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000165 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000166 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000167 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000168 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000169 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor72564e72009-02-26 23:50:07 +0000170 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000171
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
173 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000174 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000175 NumLValueReference*sizeof(LValueReferenceType)+
176 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000177 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000178 NumFunctionP*sizeof(FunctionProtoType)+
179 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000180 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000181 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000182}
183
184
185void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000186 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000187}
188
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner98be4942008-03-05 18:54:05 +0000198 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +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);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000220
Chris Lattner3a250322009-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());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000225
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000226 // Placeholder type for functions.
Douglas Gregor898574e2008-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 Gregor8e9bebd2008-10-21 16:13:35 +0000235
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 // C99 6.2.5p11.
237 FloatComplexTy = getComplexType(FloatTy);
238 DoubleComplexTy = getComplexType(DoubleTy);
239 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000240
Steve Naroff7e219e42007-10-15 14:41:52 +0000241 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000242 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000243 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000244 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000245 ClassStructType = 0;
246
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000247 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000248
249 // void * type
250 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000251}
252
Chris Lattner464175b2007-07-18 17:52:12 +0000253//===----------------------------------------------------------------------===//
254// Type Sizing and Analysis
255//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000256
Chris Lattnerb7cfe882008-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 Lattneraf707ab2009-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 Dunbarb7d08442009-02-17 22:16:19 +0000273unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-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 Lattneraf707ab2009-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 Friedmandcdafb62009-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 Lattneraf707ab2009-01-24 21:53:27 +0000288 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000289
290 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000291}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000292
Chris Lattnera7674d82007-07-13 22:13:22 +0000293/// getTypeSize - Return the size of the specified type, in bits. This method
294/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000295std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000296ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000297 T = getCanonicalType(T);
Mike Stump5e301002009-02-27 18:32:39 +0000298 uint64_t Width=0;
299 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000300 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-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 Lattner692233e2007-07-13 22:27:08 +0000309 case Type::FunctionNoProto:
310 case Type::FunctionProto:
Douglas Gregor72564e72009-02-26 23:50:07 +0000311 case Type::IncompleteArray:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000312 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000313 case Type::VariableArray:
314 assert(0 && "VLAs not implemented yet!");
315 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000316 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000317
Chris Lattner98be4942008-03-05 18:54:05 +0000318 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000319 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000320 Align = EltInfo.second;
321 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000322 }
Nate Begeman213541a2008-04-18 23:10:10 +0000323 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000324 case Type::Vector: {
325 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000326 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000327 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000328 Align = Width;
Nate Begeman6fe7c8a2009-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 Lattner030d8842007-07-19 22:06:24 +0000333 break;
334 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000335
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000336 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000337 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000338 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000339 case BuiltinType::Void:
340 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000341 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000342 Width = Target.getBoolWidth();
343 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000344 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000345 case BuiltinType::Char_S:
346 case BuiltinType::Char_U:
347 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000348 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000349 Width = Target.getCharWidth();
350 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000351 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000352 case BuiltinType::WChar:
353 Width = Target.getWCharWidth();
354 Align = Target.getWCharAlign();
355 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000356 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000357 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000358 Width = Target.getShortWidth();
359 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000360 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000361 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000362 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000363 Width = Target.getIntWidth();
364 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000365 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000366 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000367 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000368 Width = Target.getLongWidth();
369 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000370 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000371 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000372 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000373 Width = Target.getLongLongWidth();
374 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000375 break;
376 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000377 Width = Target.getFloatWidth();
378 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000379 break;
380 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000381 Width = Target.getDoubleWidth();
382 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000383 break;
384 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000385 Width = Target.getLongDoubleWidth();
386 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000387 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000388 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000389 break;
Eli Friedmanf98aba32009-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 Lattner736166b2009-02-15 21:20:13 +0000394 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000395 Align = Width;
396 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000397 case Type::ExtQual:
Chris Lattner98be4942008-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 Jahanianf11284a2009-02-17 18:27:45 +0000400 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000401 case Type::ObjCQualifiedId:
Eli Friedman4bdf0872009-02-22 04:02:33 +0000402 case Type::ObjCQualifiedClass:
Douglas Gregor72564e72009-02-26 23:50:07 +0000403 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000404 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000405 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000406 break;
Steve Naroff485eeff2008-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 Lattnerf72a4432008-03-08 08:34:58 +0000413 case Type::Pointer: {
414 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000415 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000416 Align = Target.getPointerAlign(AS);
417 break;
418 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000419 case Type::LValueReference:
420 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000421 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000422 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000423 // FIXME: This is wrong for struct layout: a reference in a struct has
424 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000425 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000426 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000427 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-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 Redl8edef7c2009-01-24 23:29:36 +0000430 // other compilers too, we need to delegate this completely to TargetInfo
431 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-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 Lattner5d2a6302007-07-18 18:26:58 +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 Lattner98be4942008-03-05 18:54:05 +0000444 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000445 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000446 Align = EltInfo.second;
447 break;
448 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000449 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000450 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000451 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
452 Width = Layout.getSize();
453 Align = Layout.getAlignment();
454 break;
455 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000456 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000457 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000458 const TagType *TT = cast<TagType>(T);
459
460 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000461 Width = 1;
462 Align = 1;
463 break;
464 }
465
Daniel Dunbar1d751182008-11-08 05:48:37 +0000466 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000467 return getTypeInfo(ET->getDecl()->getIntegerType());
468
Daniel Dunbar1d751182008-11-08 05:48:37 +0000469 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000470 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
471 Width = Layout.getSize();
472 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000473 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000474 }
Chris Lattner71763312008-04-06 22:05:18 +0000475 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000476
Chris Lattner464175b2007-07-18 17:52:12 +0000477 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000478 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000479}
480
Chris Lattner34ebde42009-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 Dunbare00d5c02009-02-18 19:59:32 +0000489 if (T->isSpecificBuiltinType(BuiltinType::Double))
490 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000491
492 return ABIAlign;
493}
494
495
Devang Patel8b277042008-06-04 21:22:16 +0000496/// LayoutField - Field layout.
497void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000498 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000499 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000500 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000501 uint64_t FieldOffset = IsUnion ? 0 : Size;
502 uint64_t FieldSize;
503 unsigned FieldAlign;
Daniel Dunbar3b0db902008-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 Patel8b277042008-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 Dunbar32442bb2008-08-13 23:47:13 +0000513 FieldSize =
514 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-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 Dunbar3b0db902008-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 Patel8b277042008-06-04 21:22:16 +0000525 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000526 if (FieldPacking)
527 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-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 Lattner8389eab2008-08-09 21:35:13 +0000540 if (FD->getType()->isIncompleteArrayType()) {
541 // This is a flexible array member; we can't directly
Devang Patel8b277042008-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 Lattnerc63a1f22008-08-04 07:31:14 +0000546 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-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 Dunbar3b0db902008-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 Patel8b277042008-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 Jahanian88e469c2009-03-05 20:08:48 +0000585void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
586 std::vector<FieldDecl*> &Fields) const {
Fariborz Jahaniana769c002008-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 Gregor4afa39d2009-01-20 01:17:11 +0000618 RecFields[i]->getBitWidth(), false);
Douglas Gregor482b77d2009-01-12 23:27:07 +0000619 NewRD->addDecl(Field);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000620 }
621 NewRD->completeDefinition(*this);
622 RD = NewRD;
623 return RD;
624}
Devang Patel44a3dde2008-06-04 21:54:36 +0000625
Fariborz Jahanianefc4c4b2008-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 Lattner61710852008-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 Patel44a3dde2008-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 Patel6a5a34c2008-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 Lattner61710852008-10-05 17:34:18 +0000656 // Super class is at the beginning of the layout.
657 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000658 } else {
659 NewEntry = new ASTRecordLayout();
660 NewEntry->InitializeLayout(FieldCount);
661 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000662 Entry = NewEntry;
663
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000664 unsigned StructPacking = 0;
665 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
666 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-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 Dunbar3b0db902008-10-16 02:34:03 +0000677 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-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 Patel88a981b2007-11-01 19:11:01 +0000686/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000687/// specified record (struct/union/class), which indicates its size and field
688/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000689const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000690 D = D->getDefinition(*this);
691 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000692
Chris Lattner464175b2007-07-18 17:52:12 +0000693 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000694 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000695 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000696
Devang Patel88a981b2007-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 Lattner464175b2007-07-18 17:52:12 +0000700 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000701
Douglas Gregore267ff32008-12-11 20:41:00 +0000702 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor44b43212008-12-11 16:49:14 +0000703 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000704 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000705
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000706 unsigned StructPacking = 0;
707 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
708 StructPacking = PA->getAlignment();
709
Eli Friedman4bd998b2008-05-30 09:31:38 +0000710 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000711 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
712 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000713
Eli Friedman4bd998b2008-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 Gregor44b43212008-12-11 16:49:14 +0000716 unsigned FieldIdx = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000717 for (RecordDecl::field_iterator Field = D->field_begin(),
718 FieldEnd = D->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000719 Field != FieldEnd; (void)++Field, ++FieldIdx)
720 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-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 Patel8b277042008-06-04 21:22:16 +0000724 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000725 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000726}
727
Chris Lattnera7674d82007-07-13 22:13:22 +0000728//===----------------------------------------------------------------------===//
729// Type creation/memoization methods
730//===----------------------------------------------------------------------===//
731
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000732QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000733 QualType CanT = getCanonicalType(T);
734 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000735 return T;
Chris Lattnerb7d25532009-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 Lattnerf46699c2008-02-20 20:55:12 +0000742
Chris Lattnerb7d25532009-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 Lattnerf46699c2008-02-20 20:55:12 +0000751
Chris Lattnerb7d25532009-02-18 22:53:11 +0000752 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000753 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000754 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000755 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000756 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000757 return QualType(EXTQy, CVRQuals);
758
Christopher Lambebb97e92008-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 Lattnerb7d25532009-02-18 22:53:11 +0000762 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000763 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000764
Chris Lattnerb7d25532009-02-18 22:53:11 +0000765 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000766 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000767 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000768 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000769 ExtQualType *New =
770 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000771 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000772 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000773 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000774}
775
Chris Lattnerb7d25532009-02-18 22:53:11 +0000776QualType ASTContext::getObjCGCQualType(QualType T,
777 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000778 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000779 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000780 return T;
781
Chris Lattnerb7d25532009-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 Jahaniand33d9c02009-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 Lattnerb7d25532009-02-18 22:53:11 +0000799 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000800 void *InsertPos = 0;
801 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000802 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-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 Friedman5a61f0e2009-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 Jahaniand33d9c02009-02-18 05:09:49 +0000808 QualType Canonical;
809 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000810 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000811
Chris Lattnerb7d25532009-02-18 22:53:11 +0000812 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-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 Lattnerb7d25532009-02-18 22:53:11 +0000816 ExtQualType *New =
817 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000818 ExtQualTypes.InsertNode(New, InsertPos);
819 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000820 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000821}
Chris Lattnera7674d82007-07-13 22:13:22 +0000822
Reid Spencer5f016e22007-07-11 17:01:13 +0000823/// 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 Lattnerf52ab252008-04-06 22:59:24 +0000839 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000840
841 // Get the new insert position for the node we care about.
842 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000843 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000844 }
Steve Narofff83820b2009-01-27 22:08:43 +0000845 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000846 Types.push_back(New);
847 ComplexTypes.InsertNode(New, InsertPos);
848 return QualType(New, 0);
849}
850
Eli Friedmanf98aba32009-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}
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerf52ab252008-04-06 22:59:24 +0000876 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000877
878 // Get the new insert position for the node we care about.
879 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000880 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000881 }
Steve Narofff83820b2009-01-27 22:08:43 +0000882 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000883 Types.push_back(New);
884 PointerTypes.InsertNode(New, InsertPos);
885 return QualType(New, 0);
886}
887
Steve Naroff5618bd42008-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 Naroff296e8d52008-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 Naroff5618bd42008-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 Naroff296e8d52008-08-28 19:20:44 +0000902 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-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 Lattnerf6e764f2008-10-12 00:26:57 +0000911 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000912 }
Steve Narofff83820b2009-01-27 22:08:43 +0000913 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000914 Types.push_back(New);
915 BlockPointerTypes.InsertNode(New, InsertPos);
916 return QualType(New, 0);
917}
918
Sebastian Redl7c80bd62009-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) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl7c80bd62009-03-16 23:22:08 +0000928 if (LValueReferenceType *RT =
929 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000930 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000931
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl7c80bd62009-03-16 23:22:08 +0000936 Canonical = getLValueReferenceType(getCanonicalType(T));
937
Reid Spencer5f016e22007-07-11 17:01:13 +0000938 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000939 LValueReferenceType *NewIP =
940 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000941 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 }
943
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000944 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 Types.push_back(New);
Sebastian Redl7c80bd62009-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);
Reid Spencer5f016e22007-07-11 17:01:13 +0000978 return QualType(New, 0);
979}
980
Sebastian Redlf30208a2009-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 Narofff83820b2009-01-27 22:08:43 +00001006 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001007 Types.push_back(New);
1008 MemberPointerTypes.InsertNode(New, InsertPos);
1009 return QualType(New, 0);
1010}
1011
Steve Narofffb22d962007-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 Naroffc9406122007-08-30 18:10:14 +00001015 const llvm::APInt &ArySize,
1016 ArrayType::ArraySizeModifier ASM,
1017 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001018 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001019 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001020
1021 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001022 if (ConstantArrayType *ATP =
1023 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerf52ab252008-04-06 22:59:24 +00001030 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001031 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001032 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001033 ConstantArrayType *NewIP =
1034 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001035 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001036 }
1037
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001038 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001039 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001040 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 Types.push_back(New);
1042 return QualType(New, 0);
1043}
1044
Steve Naroffbdbf7b02007-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 Naroffc9406122007-08-30 18:10:14 +00001047QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1048 ArrayType::ArraySizeModifier ASM,
1049 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-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 Kremenek566c2ba2009-01-19 21:31:22 +00001053 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001054 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001055
1056 VariableArrayTypes.push_back(New);
1057 Types.push_back(New);
1058 return QualType(New, 0);
1059}
1060
Douglas Gregor898574e2008-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 Kremenek566c2ba2009-01-19 21:31:22 +00001074 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001075 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1076 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001077
1078 DependentSizedArrayTypes.push_back(New);
1079 Types.push_back(New);
1080 return QualType(New, 0);
1081}
1082
Eli Friedmanc5773c42008-02-15 18:16:39 +00001083QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1084 ArrayType::ArraySizeModifier ASM,
1085 unsigned EltTypeQuals) {
1086 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001087 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-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 Lattnerf52ab252008-04-06 22:59:24 +00001099 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001100 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-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 Lattnerf6e764f2008-10-12 00:26:57 +00001105 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001106 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001107
Steve Narofff83820b2009-01-27 22:08:43 +00001108 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001109 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001110
1111 IncompleteArrayTypes.InsertNode(New, InsertPos);
1112 Types.push_back(New);
1113 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001114}
1115
Steve Naroff73322922007-07-18 18:00:27 +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) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001119 BuiltinType *baseType;
1120
Chris Lattnerf52ab252008-04-06 22:59:24 +00001121 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001122 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001123
1124 // Check if we've already instantiated a vector of this type.
1125 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001126 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001127 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 Lattnerf52ab252008-04-06 22:59:24 +00001135 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001136
1137 // Get the new insert position for the node we care about.
1138 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001139 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001140 }
Steve Narofff83820b2009-01-27 22:08:43 +00001141 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001142 VectorTypes.InsertNode(New, InsertPos);
1143 Types.push_back(New);
1144 return QualType(New, 0);
1145}
1146
Nate Begeman213541a2008-04-18 23:10:10 +00001147/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001148/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001149QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001150 BuiltinType *baseType;
1151
Chris Lattnerf52ab252008-04-06 22:59:24 +00001152 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001153 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001154
1155 // Check if we've already instantiated a vector of this type.
1156 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001157 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +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 Begeman213541a2008-04-18 23:10:10 +00001166 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001167
1168 // Get the new insert position for the node we care about.
1169 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001170 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001171 }
Steve Narofff83820b2009-01-27 22:08:43 +00001172 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001173 VectorTypes.InsertNode(New, InsertPos);
1174 Types.push_back(New);
1175 return QualType(New, 0);
1176}
1177
Douglas Gregor72564e72009-02-26 23:50:07 +00001178/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001179///
Douglas Gregor72564e72009-02-26 23:50:07 +00001180QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 // Unique functions, to guarantee there is only one function of a particular
1182 // structure.
1183 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001184 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001185
1186 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001187 if (FunctionNoProtoType *FT =
1188 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 return QualType(FT, 0);
1190
1191 QualType Canonical;
1192 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001193 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001194
1195 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001196 FunctionNoProtoType *NewIP =
1197 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001198 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001199 }
1200
Douglas Gregor72564e72009-02-26 23:50:07 +00001201 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001202 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001203 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner61710852008-10-05 17:34:18 +00001209QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001210 unsigned NumArgs, bool isVariadic,
1211 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001212 // Unique functions, to guarantee there is only one function of a particular
1213 // structure.
1214 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001215 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001216 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001217
1218 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001219 if (FunctionProtoType *FTP =
1220 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerf52ab252008-04-06 22:59:24 +00001235 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001236
Chris Lattnerf52ab252008-04-06 22:59:24 +00001237 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001239 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001240
1241 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001242 FunctionProtoType *NewIP =
1243 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001244 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 }
1246
Douglas Gregor72564e72009-02-26 23:50:07 +00001247 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001248 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001249 FunctionProtoType *FTP =
1250 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001251 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001252 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001253 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001254 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001255 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 return QualType(FTP, 0);
1257}
1258
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001259/// getTypeDeclType - Return the unique reference to the type for the
1260/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001261QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001262 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001263 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1264
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001265 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001266 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-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 Gregor2ce52f32008-04-13 21:07:44 +00001270 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001271
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001272 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001273 if (PrevDecl)
1274 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001275 else
1276 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001277 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001278 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1279 if (PrevDecl)
1280 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001281 else
1282 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001283 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001284 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001285 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001286
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001287 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001288 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001289}
1290
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerf52ab252008-04-06 22:59:24 +00001296 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001297 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001298 Types.push_back(Decl->TypeForDecl);
1299 return QualType(Decl->TypeForDecl, 0);
1300}
1301
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001302/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001303/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001304QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001305 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1306
Steve Narofff83820b2009-01-27 22:08:43 +00001307 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001308 Types.push_back(Decl->TypeForDecl);
1309 return QualType(Decl->TypeForDecl, 0);
1310}
1311
Fariborz Jahanianf3710ba2009-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 Gregorfab9d672009-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 Gregor55f6b142009-02-09 18:46:07 +00001349QualType
1350ASTContext::getClassTemplateSpecializationType(TemplateDecl *Template,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001351 const TemplateArgument *Args,
Douglas Gregor55f6b142009-02-09 18:46:07 +00001352 unsigned NumArgs,
Douglas Gregor55f6b142009-02-09 18:46:07 +00001353 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001354 if (!Canon.isNull())
1355 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001356
Douglas Gregor55f6b142009-02-09 18:46:07 +00001357 llvm::FoldingSetNodeID ID;
Douglas Gregor40808ce2009-03-09 23:48:35 +00001358 ClassTemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
1359
Douglas Gregor55f6b142009-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 Gregor40808ce2009-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 Gregor55f6b142009-02-09 18:46:07 +00001372 Types.push_back(Spec);
1373 ClassTemplateSpecializationTypes.InsertNode(Spec, InsertPos);
1374
1375 return QualType(Spec, 0);
1376}
1377
Chris Lattner88cb27a2008-04-07 04:56:42 +00001378/// CmpProtocolNames - Comparison predicate for sorting protocols
1379/// alphabetically.
1380static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1381 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001382 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001383}
1384
1385static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1386 unsigned &NumProtocols) {
1387 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1388
1389 // Sort protocols, keyed by name.
1390 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1391
1392 // Remove duplicates.
1393 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1394 NumProtocols = ProtocolsEnd-Protocols;
1395}
1396
1397
Chris Lattner065f0d72008-04-07 04:44:08 +00001398/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1399/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001400QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1401 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001402 // Sort the protocol list alphabetically to canonicalize it.
1403 SortAndUniqueProtocols(Protocols, NumProtocols);
1404
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001405 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001406 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001407
1408 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001409 if (ObjCQualifiedInterfaceType *QT =
1410 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001411 return QualType(QT, 0);
1412
1413 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001414 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001415 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001416
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001417 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001418 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001419 return QualType(QType, 0);
1420}
1421
Chris Lattner88cb27a2008-04-07 04:56:42 +00001422/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1423/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001424QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001425 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001426 // Sort the protocol list alphabetically to canonicalize it.
1427 SortAndUniqueProtocols(Protocols, NumProtocols);
1428
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001429 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001430 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001431
1432 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001433 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001434 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001435 return QualType(QT, 0);
1436
1437 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001438 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001439 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001440 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001441 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001442 return QualType(QType, 0);
1443}
1444
Douglas Gregor72564e72009-02-26 23:50:07 +00001445/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1446/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001447/// multiple declarations that refer to "typeof(x)" all contain different
1448/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1449/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001450QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001451 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001452 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001453 Types.push_back(toe);
1454 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001455}
1456
Steve Naroff9752f252007-08-01 18:02:17 +00001457/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1458/// TypeOfType AST's. The only motivation to unique these nodes would be
1459/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1460/// an issue. This doesn't effect the type checker, since it operates
1461/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001462QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001463 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001464 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001465 Types.push_back(tot);
1466 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001467}
1468
Reid Spencer5f016e22007-07-11 17:01:13 +00001469/// getTagDeclType - Return the unique reference to the type for the
1470/// specified TagDecl (struct/union/class/enum) decl.
1471QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001472 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001473 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001474}
1475
1476/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1477/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1478/// needs to agree with the definition in <stddef.h>.
1479QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001480 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001481}
1482
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001483/// getSignedWCharType - Return the type of "signed wchar_t".
1484/// Used when in C++, as a GCC extension.
1485QualType ASTContext::getSignedWCharType() const {
1486 // FIXME: derive from "Target" ?
1487 return WCharTy;
1488}
1489
1490/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1491/// Used when in C++, as a GCC extension.
1492QualType ASTContext::getUnsignedWCharType() const {
1493 // FIXME: derive from "Target" ?
1494 return UnsignedIntTy;
1495}
1496
Chris Lattner8b9023b2007-07-13 03:05:23 +00001497/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1498/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1499QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001500 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001501}
1502
Chris Lattnere6327742008-04-02 05:18:44 +00001503//===----------------------------------------------------------------------===//
1504// Type Operators
1505//===----------------------------------------------------------------------===//
1506
Chris Lattner77c96472008-04-06 22:41:35 +00001507/// getCanonicalType - Return the canonical (structural) type corresponding to
1508/// the specified potentially non-canonical type. The non-canonical version
1509/// of a type may have many "decorated" versions of types. Decorators can
1510/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1511/// to be free of any of these, allowing two canonical types to be compared
1512/// for exact equality with a simple pointer comparison.
1513QualType ASTContext::getCanonicalType(QualType T) {
1514 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001515
1516 // If the result has type qualifiers, make sure to canonicalize them as well.
1517 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1518 if (TypeQuals == 0) return CanType;
1519
1520 // If the type qualifiers are on an array type, get the canonical type of the
1521 // array with the qualifiers applied to the element type.
1522 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1523 if (!AT)
1524 return CanType.getQualifiedType(TypeQuals);
1525
1526 // Get the canonical version of the element with the extra qualifiers on it.
1527 // This can recursively sink qualifiers through multiple levels of arrays.
1528 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1529 NewEltTy = getCanonicalType(NewEltTy);
1530
1531 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1532 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1533 CAT->getIndexTypeQualifier());
1534 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1535 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1536 IAT->getIndexTypeQualifier());
1537
Douglas Gregor898574e2008-12-05 23:32:09 +00001538 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1539 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1540 DSAT->getSizeModifier(),
1541 DSAT->getIndexTypeQualifier());
1542
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001543 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1544 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1545 VAT->getSizeModifier(),
1546 VAT->getIndexTypeQualifier());
1547}
1548
1549
1550const ArrayType *ASTContext::getAsArrayType(QualType T) {
1551 // Handle the non-qualified case efficiently.
1552 if (T.getCVRQualifiers() == 0) {
1553 // Handle the common positive case fast.
1554 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1555 return AT;
1556 }
1557
1558 // Handle the common negative case fast, ignoring CVR qualifiers.
1559 QualType CType = T->getCanonicalTypeInternal();
1560
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001561 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001562 // test.
1563 if (!isa<ArrayType>(CType) &&
1564 !isa<ArrayType>(CType.getUnqualifiedType()))
1565 return 0;
1566
1567 // Apply any CVR qualifiers from the array type to the element type. This
1568 // implements C99 6.7.3p8: "If the specification of an array type includes
1569 // any type qualifiers, the element type is so qualified, not the array type."
1570
1571 // If we get here, we either have type qualifiers on the type, or we have
1572 // sugar such as a typedef in the way. If we have type qualifiers on the type
1573 // we must propagate them down into the elemeng type.
1574 unsigned CVRQuals = T.getCVRQualifiers();
1575 unsigned AddrSpace = 0;
1576 Type *Ty = T.getTypePtr();
1577
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001578 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001579 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001580 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1581 AddrSpace = EXTQT->getAddressSpace();
1582 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001583 } else {
1584 T = Ty->getDesugaredType();
1585 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1586 break;
1587 CVRQuals |= T.getCVRQualifiers();
1588 Ty = T.getTypePtr();
1589 }
1590 }
1591
1592 // If we have a simple case, just return now.
1593 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1594 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1595 return ATy;
1596
1597 // Otherwise, we have an array and we have qualifiers on it. Push the
1598 // qualifiers into the array element type and return a new array type.
1599 // Get the canonical version of the element with the extra qualifiers on it.
1600 // This can recursively sink qualifiers through multiple levels of arrays.
1601 QualType NewEltTy = ATy->getElementType();
1602 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001603 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001604 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1605
1606 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1607 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1608 CAT->getSizeModifier(),
1609 CAT->getIndexTypeQualifier()));
1610 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1611 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1612 IAT->getSizeModifier(),
1613 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001614
Douglas Gregor898574e2008-12-05 23:32:09 +00001615 if (const DependentSizedArrayType *DSAT
1616 = dyn_cast<DependentSizedArrayType>(ATy))
1617 return cast<ArrayType>(
1618 getDependentSizedArrayType(NewEltTy,
1619 DSAT->getSizeExpr(),
1620 DSAT->getSizeModifier(),
1621 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001622
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001623 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1624 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1625 VAT->getSizeModifier(),
1626 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001627}
1628
1629
Chris Lattnere6327742008-04-02 05:18:44 +00001630/// getArrayDecayedType - Return the properly qualified result of decaying the
1631/// specified array type to a pointer. This operation is non-trivial when
1632/// handling typedefs etc. The canonical type of "T" must be an array type,
1633/// this returns a pointer to a properly qualified element of the array.
1634///
1635/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1636QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001637 // Get the element type with 'getAsArrayType' so that we don't lose any
1638 // typedefs in the element type of the array. This also handles propagation
1639 // of type qualifiers from the array type into the element type if present
1640 // (C99 6.7.3p8).
1641 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1642 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001643
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001644 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001645
1646 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001647 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001648}
1649
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001650QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001651 QualType ElemTy = VAT->getElementType();
1652
1653 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1654 return getBaseElementType(VAT);
1655
1656 return ElemTy;
1657}
1658
Reid Spencer5f016e22007-07-11 17:01:13 +00001659/// getFloatingRank - Return a relative rank for floating point types.
1660/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001661static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001662 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001663 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001664
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001665 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001666 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001667 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001668 case BuiltinType::Float: return FloatRank;
1669 case BuiltinType::Double: return DoubleRank;
1670 case BuiltinType::LongDouble: return LongDoubleRank;
1671 }
1672}
1673
Steve Naroff716c7302007-08-27 01:41:48 +00001674/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1675/// point or a complex type (based on typeDomain/typeSize).
1676/// 'typeDomain' is a real floating point or complex type.
1677/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001678QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1679 QualType Domain) const {
1680 FloatingRank EltRank = getFloatingRank(Size);
1681 if (Domain->isComplexType()) {
1682 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001683 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001684 case FloatRank: return FloatComplexTy;
1685 case DoubleRank: return DoubleComplexTy;
1686 case LongDoubleRank: return LongDoubleComplexTy;
1687 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001688 }
Chris Lattner1361b112008-04-06 23:58:54 +00001689
1690 assert(Domain->isRealFloatingType() && "Unknown domain!");
1691 switch (EltRank) {
1692 default: assert(0 && "getFloatingRank(): illegal value for rank");
1693 case FloatRank: return FloatTy;
1694 case DoubleRank: return DoubleTy;
1695 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001696 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001697}
1698
Chris Lattner7cfeb082008-04-06 23:55:33 +00001699/// getFloatingTypeOrder - Compare the rank of the two specified floating
1700/// point types, ignoring the domain of the type (i.e. 'double' ==
1701/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1702/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001703int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1704 FloatingRank LHSR = getFloatingRank(LHS);
1705 FloatingRank RHSR = getFloatingRank(RHS);
1706
1707 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001708 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001709 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001710 return 1;
1711 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001712}
1713
Chris Lattnerf52ab252008-04-06 22:59:24 +00001714/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1715/// routine will assert if passed a built-in type that isn't an integer or enum,
1716/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001717unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001718 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001719 if (EnumType* ET = dyn_cast<EnumType>(T))
1720 T = ET->getDecl()->getIntegerType().getTypePtr();
1721
1722 // There are two things which impact the integer rank: the width, and
1723 // the ordering of builtins. The builtin ordering is encoded in the
1724 // bottom three bits; the width is encoded in the bits above that.
1725 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1726 return FWIT->getWidth() << 3;
1727 }
1728
Chris Lattnerf52ab252008-04-06 22:59:24 +00001729 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001730 default: assert(0 && "getIntegerRank(): not a built-in integer");
1731 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001732 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001733 case BuiltinType::Char_S:
1734 case BuiltinType::Char_U:
1735 case BuiltinType::SChar:
1736 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001737 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001738 case BuiltinType::Short:
1739 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001740 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001741 case BuiltinType::Int:
1742 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001743 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001744 case BuiltinType::Long:
1745 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001746 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001747 case BuiltinType::LongLong:
1748 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001749 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001750 }
1751}
1752
Chris Lattner7cfeb082008-04-06 23:55:33 +00001753/// getIntegerTypeOrder - Returns the highest ranked integer type:
1754/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1755/// LHS < RHS, return -1.
1756int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001757 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1758 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001759 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001760
Chris Lattnerf52ab252008-04-06 22:59:24 +00001761 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1762 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001763
Chris Lattner7cfeb082008-04-06 23:55:33 +00001764 unsigned LHSRank = getIntegerRank(LHSC);
1765 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001766
Chris Lattner7cfeb082008-04-06 23:55:33 +00001767 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1768 if (LHSRank == RHSRank) return 0;
1769 return LHSRank > RHSRank ? 1 : -1;
1770 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001771
Chris Lattner7cfeb082008-04-06 23:55:33 +00001772 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1773 if (LHSUnsigned) {
1774 // If the unsigned [LHS] type is larger, return it.
1775 if (LHSRank >= RHSRank)
1776 return 1;
1777
1778 // If the signed type can represent all values of the unsigned type, it
1779 // wins. Because we are dealing with 2's complement and types that are
1780 // powers of two larger than each other, this is always safe.
1781 return -1;
1782 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001783
Chris Lattner7cfeb082008-04-06 23:55:33 +00001784 // If the unsigned [RHS] type is larger, return it.
1785 if (RHSRank >= LHSRank)
1786 return -1;
1787
1788 // If the signed type can represent all values of the unsigned type, it
1789 // wins. Because we are dealing with 2's complement and types that are
1790 // powers of two larger than each other, this is always safe.
1791 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001792}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001793
1794// getCFConstantStringType - Return the type used for constant CFStrings.
1795QualType ASTContext::getCFConstantStringType() {
1796 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001797 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001798 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001799 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001800 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001801
1802 // const int *isa;
1803 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001804 // int flags;
1805 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001806 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001807 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001808 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001809 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001810
Anders Carlsson71993dd2007-08-17 05:31:46 +00001811 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001812 for (unsigned i = 0; i < 4; ++i) {
1813 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1814 SourceLocation(), 0,
1815 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001816 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001817 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001818 }
1819
1820 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001821 }
1822
1823 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001824}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001825
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001826QualType ASTContext::getObjCFastEnumerationStateType()
1827{
1828 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001829 ObjCFastEnumerationStateTypeDecl =
1830 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1831 &Idents.get("__objcFastEnumerationState"));
1832
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001833 QualType FieldTypes[] = {
1834 UnsignedLongTy,
1835 getPointerType(ObjCIdType),
1836 getPointerType(UnsignedLongTy),
1837 getConstantArrayType(UnsignedLongTy,
1838 llvm::APInt(32, 5), ArrayType::Normal, 0)
1839 };
1840
Douglas Gregor44b43212008-12-11 16:49:14 +00001841 for (size_t i = 0; i < 4; ++i) {
1842 FieldDecl *Field = FieldDecl::Create(*this,
1843 ObjCFastEnumerationStateTypeDecl,
1844 SourceLocation(), 0,
1845 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001846 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001847 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001848 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001849
Douglas Gregor44b43212008-12-11 16:49:14 +00001850 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001851 }
1852
1853 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1854}
1855
Anders Carlssone8c49532007-10-29 06:33:42 +00001856// This returns true if a type has been typedefed to BOOL:
1857// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001858static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001859 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00001860 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1861 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001862
1863 return false;
1864}
1865
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001866/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001867/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001868int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001869 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001870
1871 // Make all integer and enum types at least as large as an int
1872 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001873 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001874 // Treat arrays as pointers, since that's how they're passed in.
1875 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001876 sz = getTypeSize(VoidPtrTy);
1877 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001878}
1879
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001880/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001881/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001882void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001883 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001884 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001885 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001886 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001887 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001888 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001889 // Compute size of all parameters.
1890 // Start with computing size of a pointer in number of bytes.
1891 // FIXME: There might(should) be a better way of doing this computation!
1892 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001893 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001894 // The first two arguments (self and _cmd) are pointers; account for
1895 // their size.
1896 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00001897 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1898 E = Decl->param_end(); PI != E; ++PI) {
1899 QualType PType = (*PI)->getType();
1900 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001901 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001902 ParmOffset += sz;
1903 }
1904 S += llvm::utostr(ParmOffset);
1905 S += "@0:";
1906 S += llvm::utostr(PtrSize);
1907
1908 // Argument types.
1909 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00001910 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1911 E = Decl->param_end(); PI != E; ++PI) {
1912 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001913 QualType PType = PVDecl->getOriginalType();
1914 if (const ArrayType *AT =
1915 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1916 // Use array's original type only if it has known number of
1917 // elements.
1918 if (!dyn_cast<ConstantArrayType>(AT))
1919 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001920 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001921 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001922 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001923 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001924 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001925 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001926 }
1927}
1928
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001929/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001930/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001931/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1932/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001933/// Property attributes are stored as a comma-delimited C string. The simple
1934/// attributes readonly and bycopy are encoded as single characters. The
1935/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1936/// encoded as single characters, followed by an identifier. Property types
1937/// are also encoded as a parametrized attribute. The characters used to encode
1938/// these attributes are defined by the following enumeration:
1939/// @code
1940/// enum PropertyAttributes {
1941/// kPropertyReadOnly = 'R', // property is read-only.
1942/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1943/// kPropertyByref = '&', // property is a reference to the value last assigned
1944/// kPropertyDynamic = 'D', // property is dynamic
1945/// kPropertyGetter = 'G', // followed by getter selector name
1946/// kPropertySetter = 'S', // followed by setter selector name
1947/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1948/// kPropertyType = 't' // followed by old-style type encoding.
1949/// kPropertyWeak = 'W' // 'weak' property
1950/// kPropertyStrong = 'P' // property GC'able
1951/// kPropertyNonAtomic = 'N' // property non-atomic
1952/// };
1953/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001954void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1955 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001956 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001957 // Collect information from the property implementation decl(s).
1958 bool Dynamic = false;
1959 ObjCPropertyImplDecl *SynthesizePID = 0;
1960
1961 // FIXME: Duplicated code due to poor abstraction.
1962 if (Container) {
1963 if (const ObjCCategoryImplDecl *CID =
1964 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1965 for (ObjCCategoryImplDecl::propimpl_iterator
1966 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1967 ObjCPropertyImplDecl *PID = *i;
1968 if (PID->getPropertyDecl() == PD) {
1969 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1970 Dynamic = true;
1971 } else {
1972 SynthesizePID = PID;
1973 }
1974 }
1975 }
1976 } else {
Chris Lattner61710852008-10-05 17:34:18 +00001977 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001978 for (ObjCCategoryImplDecl::propimpl_iterator
1979 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1980 ObjCPropertyImplDecl *PID = *i;
1981 if (PID->getPropertyDecl() == PD) {
1982 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1983 Dynamic = true;
1984 } else {
1985 SynthesizePID = PID;
1986 }
1987 }
1988 }
1989 }
1990 }
1991
1992 // FIXME: This is not very efficient.
1993 S = "T";
1994
1995 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001996 // GCC has some special rules regarding encoding of properties which
1997 // closely resembles encoding of ivars.
1998 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
1999 true /* outermost type */,
2000 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002001
2002 if (PD->isReadOnly()) {
2003 S += ",R";
2004 } else {
2005 switch (PD->getSetterKind()) {
2006 case ObjCPropertyDecl::Assign: break;
2007 case ObjCPropertyDecl::Copy: S += ",C"; break;
2008 case ObjCPropertyDecl::Retain: S += ",&"; break;
2009 }
2010 }
2011
2012 // It really isn't clear at all what this means, since properties
2013 // are "dynamic by default".
2014 if (Dynamic)
2015 S += ",D";
2016
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002017 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2018 S += ",N";
2019
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002020 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2021 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002022 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002023 }
2024
2025 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2026 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002027 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002028 }
2029
2030 if (SynthesizePID) {
2031 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2032 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002033 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002034 }
2035
2036 // FIXME: OBJCGC: weak & strong
2037}
2038
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002039/// getLegacyIntegralTypeEncoding -
2040/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002041/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002042/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2043///
2044void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2045 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2046 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002047 if (BT->getKind() == BuiltinType::ULong &&
2048 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002049 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002050 else
2051 if (BT->getKind() == BuiltinType::Long &&
2052 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002053 PointeeTy = IntTy;
2054 }
2055 }
2056}
2057
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002058void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002059 FieldDecl *Field) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002060 // We follow the behavior of gcc, expanding structures which are
2061 // directly pointed to, and expanding embedded structures. Note that
2062 // these rules are sufficient to prevent recursive encoding of the
2063 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002064 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2065 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002066}
2067
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002068static void EncodeBitField(const ASTContext *Context, std::string& S,
2069 FieldDecl *FD) {
2070 const Expr *E = FD->getBitWidth();
2071 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2072 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2073 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2074 S += 'b';
2075 S += llvm::utostr(N);
2076}
2077
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002078void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2079 bool ExpandPointedToStructures,
2080 bool ExpandStructures,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002081 FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002082 bool OutermostType,
2083 bool EncodingProperty) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00002084 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002085 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002086 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002087 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002088 else {
2089 char encoding;
2090 switch (BT->getKind()) {
2091 default: assert(0 && "Unhandled builtin type kind");
2092 case BuiltinType::Void: encoding = 'v'; break;
2093 case BuiltinType::Bool: encoding = 'B'; break;
2094 case BuiltinType::Char_U:
2095 case BuiltinType::UChar: encoding = 'C'; break;
2096 case BuiltinType::UShort: encoding = 'S'; break;
2097 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002098 case BuiltinType::ULong:
2099 encoding =
2100 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2101 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002102 case BuiltinType::ULongLong: encoding = 'Q'; break;
2103 case BuiltinType::Char_S:
2104 case BuiltinType::SChar: encoding = 'c'; break;
2105 case BuiltinType::Short: encoding = 's'; break;
2106 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002107 case BuiltinType::Long:
2108 encoding =
2109 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2110 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002111 case BuiltinType::LongLong: encoding = 'q'; break;
2112 case BuiltinType::Float: encoding = 'f'; break;
2113 case BuiltinType::Double: encoding = 'd'; break;
2114 case BuiltinType::LongDouble: encoding = 'd'; break;
2115 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002116
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002117 S += encoding;
2118 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002119 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002120 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002121 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2122 ExpandPointedToStructures,
2123 ExpandStructures, FD);
2124 if (FD || EncodingProperty) {
2125 // Note that we do extended encoding of protocol qualifer list
2126 // Only when doing ivar or property encoding.
2127 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2128 S += '"';
2129 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2130 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2131 S += '<';
2132 S += Proto->getNameAsString();
2133 S += '>';
2134 }
2135 S += '"';
2136 }
2137 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002138 }
2139 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002140 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002141 bool isReadOnly = false;
2142 // For historical/compatibility reasons, the read-only qualifier of the
2143 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2144 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2145 // Also, do not emit the 'r' for anything but the outermost type!
2146 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2147 if (OutermostType && T.isConstQualified()) {
2148 isReadOnly = true;
2149 S += 'r';
2150 }
2151 }
2152 else if (OutermostType) {
2153 QualType P = PointeeTy;
2154 while (P->getAsPointerType())
2155 P = P->getAsPointerType()->getPointeeType();
2156 if (P.isConstQualified()) {
2157 isReadOnly = true;
2158 S += 'r';
2159 }
2160 }
2161 if (isReadOnly) {
2162 // Another legacy compatibility encoding. Some ObjC qualifier and type
2163 // combinations need to be rearranged.
2164 // Rewrite "in const" from "nr" to "rn"
2165 const char * s = S.c_str();
2166 int len = S.length();
2167 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2168 std::string replace = "rn";
2169 S.replace(S.end()-2, S.end(), replace);
2170 }
2171 }
Steve Naroff389bf462009-02-12 17:52:19 +00002172 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002173 S += '@';
2174 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002175 }
2176 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002177 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002178 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002179 // Another historical/compatibility reason.
2180 // We encode the underlying type which comes out as
2181 // {...};
2182 S += '^';
2183 getObjCEncodingForTypeImpl(PointeeTy, S,
2184 false, ExpandPointedToStructures,
2185 NULL);
2186 return;
2187 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002188 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002189 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002190 const ObjCInterfaceType *OIT =
2191 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002192 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002193 S += '"';
2194 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002195 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2196 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2197 S += '<';
2198 S += Proto->getNameAsString();
2199 S += '>';
2200 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002201 S += '"';
2202 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002203 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002204 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002205 S += '#';
2206 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002207 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002208 S += ':';
2209 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002210 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002211
2212 if (PointeeTy->isCharType()) {
2213 // char pointer types should be encoded as '*' unless it is a
2214 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002215 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002216 S += '*';
2217 return;
2218 }
2219 }
2220
2221 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002222 getLegacyIntegralTypeEncoding(PointeeTy);
2223
2224 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002225 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002226 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002227 } else if (const ArrayType *AT =
2228 // Ignore type qualifiers etc.
2229 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002230 if (isa<IncompleteArrayType>(AT)) {
2231 // Incomplete arrays are encoded as a pointer to the array element.
2232 S += '^';
2233
2234 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2235 false, ExpandStructures, FD);
2236 } else {
2237 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002238
Anders Carlsson559a8332009-02-22 01:38:57 +00002239 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2240 S += llvm::utostr(CAT->getSize().getZExtValue());
2241 else {
2242 //Variable length arrays are encoded as a regular array with 0 elements.
2243 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2244 S += '0';
2245 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002246
Anders Carlsson559a8332009-02-22 01:38:57 +00002247 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2248 false, ExpandStructures, FD);
2249 S += ']';
2250 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002251 } else if (T->getAsFunctionType()) {
2252 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002253 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002254 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002255 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002256 // Anonymous structures print as '?'
2257 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2258 S += II->getName();
2259 } else {
2260 S += '?';
2261 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002262 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002263 S += '=';
Douglas Gregor44b43212008-12-11 16:49:14 +00002264 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2265 FieldEnd = RDecl->field_end();
2266 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002267 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002268 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002269 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002270 S += '"';
2271 }
2272
2273 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002274 if (Field->isBitField()) {
2275 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2276 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002277 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002278 QualType qt = Field->getType();
2279 getLegacyIntegralTypeEncoding(qt);
2280 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002281 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002282 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002283 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002284 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002285 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002286 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002287 if (FD && FD->isBitField())
2288 EncodeBitField(this, S, FD);
2289 else
2290 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002291 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002292 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002293 } else if (T->isObjCInterfaceType()) {
2294 // @encode(class_name)
2295 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2296 S += '{';
2297 const IdentifierInfo *II = OI->getIdentifier();
2298 S += II->getName();
2299 S += '=';
2300 std::vector<FieldDecl*> RecFields;
2301 CollectObjCIvars(OI, RecFields);
2302 for (unsigned int i = 0; i != RecFields.size(); i++) {
2303 if (RecFields[i]->isBitField())
2304 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2305 RecFields[i]);
2306 else
2307 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2308 FD);
2309 }
2310 S += '}';
2311 }
2312 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002313 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002314}
2315
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002316void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002317 std::string& S) const {
2318 if (QT & Decl::OBJC_TQ_In)
2319 S += 'n';
2320 if (QT & Decl::OBJC_TQ_Inout)
2321 S += 'N';
2322 if (QT & Decl::OBJC_TQ_Out)
2323 S += 'o';
2324 if (QT & Decl::OBJC_TQ_Bycopy)
2325 S += 'O';
2326 if (QT & Decl::OBJC_TQ_Byref)
2327 S += 'R';
2328 if (QT & Decl::OBJC_TQ_Oneway)
2329 S += 'V';
2330}
2331
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002332void ASTContext::setBuiltinVaListType(QualType T)
2333{
2334 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2335
2336 BuiltinVaListType = T;
2337}
2338
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002339void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002340{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002341 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002342
2343 // typedef struct objc_object *id;
2344 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002345 // User error - caller will issue diagnostics.
2346 if (!ptr)
2347 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002348 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002349 // User error - caller will issue diagnostics.
2350 if (!rec)
2351 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002352 IdStructType = rec;
2353}
2354
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002355void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002356{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002357 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002358
2359 // typedef struct objc_selector *SEL;
2360 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002361 if (!ptr)
2362 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002363 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002364 if (!rec)
2365 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002366 SelStructType = rec;
2367}
2368
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002369void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002370{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002371 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002372}
2373
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002374void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002375{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002376 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002377
2378 // typedef struct objc_class *Class;
2379 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2380 assert(ptr && "'Class' incorrectly typed");
2381 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2382 assert(rec && "'Class' incorrectly typed");
2383 ClassStructType = rec;
2384}
2385
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002386void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2387 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002388 "'NSConstantString' type already set!");
2389
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002390 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002391}
2392
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002393/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002394/// TargetInfo, produce the corresponding type. The unsigned @p Type
2395/// is actually a value of type @c TargetInfo::IntType.
2396QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002397 switch (Type) {
2398 case TargetInfo::NoInt: return QualType();
2399 case TargetInfo::SignedShort: return ShortTy;
2400 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2401 case TargetInfo::SignedInt: return IntTy;
2402 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2403 case TargetInfo::SignedLong: return LongTy;
2404 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2405 case TargetInfo::SignedLongLong: return LongLongTy;
2406 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2407 }
2408
2409 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002410 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002411}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002412
2413//===----------------------------------------------------------------------===//
2414// Type Predicates.
2415//===----------------------------------------------------------------------===//
2416
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002417/// isObjCNSObjectType - Return true if this is an NSObject object using
2418/// NSObject attribute on a c-style pointer type.
2419/// FIXME - Make it work directly on types.
2420///
2421bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2422 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2423 if (TypedefDecl *TD = TDT->getDecl())
2424 if (TD->getAttr<ObjCNSObjectAttr>())
2425 return true;
2426 }
2427 return false;
2428}
2429
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002430/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2431/// to an object type. This includes "id" and "Class" (two 'special' pointers
2432/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2433/// ID type).
2434bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002435 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002436 return true;
2437
Steve Naroff6ae98502008-10-21 18:24:04 +00002438 // Blocks are objects.
2439 if (Ty->isBlockPointerType())
2440 return true;
2441
2442 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002443 if (!Ty->isPointerType())
2444 return false;
2445
2446 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2447 // pointer types. This looks for the typedef specifically, not for the
2448 // underlying type.
2449 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2450 return true;
2451
2452 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002453 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2454 return true;
2455
2456 // If is has NSObject attribute, OK as well.
2457 return isObjCNSObjectType(Ty);
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002458}
2459
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002460/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2461/// garbage collection attribute.
2462///
2463QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002464 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002465 if (getLangOptions().ObjC1 &&
2466 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002467 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002468 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002469 // (or pointers to them) be treated as though they were declared
2470 // as __strong.
2471 if (GCAttrs == QualType::GCNone) {
2472 if (isObjCObjectPointerType(Ty))
2473 GCAttrs = QualType::Strong;
2474 else if (Ty->isPointerType())
2475 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2476 }
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002477 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002478 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002479}
2480
Chris Lattner6ac46a42008-04-07 06:51:04 +00002481//===----------------------------------------------------------------------===//
2482// Type Compatibility Testing
2483//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002484
Steve Naroff1c7d0672008-09-04 15:10:53 +00002485/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002486/// block types. Types must be strictly compatible here. For example,
2487/// C unfortunately doesn't produce an error for the following:
2488///
2489/// int (*emptyArgFunc)();
2490/// int (*intArgList)(int) = emptyArgFunc;
2491///
2492/// For blocks, we will produce an error for the following (similar to C++):
2493///
2494/// int (^emptyArgBlock)();
2495/// int (^intArgBlock)(int) = emptyArgBlock;
2496///
2497/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2498///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002499bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002500 const FunctionType *lbase = lhs->getAsFunctionType();
2501 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002502 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2503 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Steve Naroffc0febd52008-12-10 17:49:55 +00002504 if (lproto && rproto)
2505 return !mergeTypes(lhs, rhs).isNull();
2506 return false;
Steve Naroff1c7d0672008-09-04 15:10:53 +00002507}
2508
Chris Lattner6ac46a42008-04-07 06:51:04 +00002509/// areCompatVectorTypes - Return true if the two specified vector types are
2510/// compatible.
2511static bool areCompatVectorTypes(const VectorType *LHS,
2512 const VectorType *RHS) {
2513 assert(LHS->isCanonical() && RHS->isCanonical());
2514 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002515 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002516}
2517
Eli Friedman3d815e72008-08-22 00:56:42 +00002518/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002519/// compatible for assignment from RHS to LHS. This handles validation of any
2520/// protocol qualifiers on the LHS or RHS.
2521///
Eli Friedman3d815e72008-08-22 00:56:42 +00002522bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2523 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002524 // Verify that the base decls are compatible: the RHS must be a subclass of
2525 // the LHS.
2526 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2527 return false;
2528
2529 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2530 // protocol qualified at all, then we are good.
2531 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2532 return true;
2533
2534 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2535 // isn't a superset.
2536 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2537 return true; // FIXME: should return false!
2538
2539 // Finally, we must have two protocol-qualified interfaces.
2540 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2541 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002542
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002543 // All LHS protocols must have a presence on the RHS.
2544 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002545
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002546 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2547 LHSPE = LHSP->qual_end();
2548 LHSPI != LHSPE; LHSPI++) {
2549 bool RHSImplementsProtocol = false;
2550
2551 // If the RHS doesn't implement the protocol on the left, the types
2552 // are incompatible.
2553 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2554 RHSPE = RHSP->qual_end();
2555 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2556 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2557 RHSImplementsProtocol = true;
2558 }
2559 // FIXME: For better diagnostics, consider passing back the protocol name.
2560 if (!RHSImplementsProtocol)
2561 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002562 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002563 // The RHS implements all protocols listed on the LHS.
2564 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002565}
2566
Steve Naroff389bf462009-02-12 17:52:19 +00002567bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2568 // get the "pointed to" types
2569 const PointerType *LHSPT = LHS->getAsPointerType();
2570 const PointerType *RHSPT = RHS->getAsPointerType();
2571
2572 if (!LHSPT || !RHSPT)
2573 return false;
2574
2575 QualType lhptee = LHSPT->getPointeeType();
2576 QualType rhptee = RHSPT->getPointeeType();
2577 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2578 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2579 // ID acts sort of like void* for ObjC interfaces
2580 if (LHSIface && isObjCIdStructType(rhptee))
2581 return true;
2582 if (RHSIface && isObjCIdStructType(lhptee))
2583 return true;
2584 if (!LHSIface || !RHSIface)
2585 return false;
2586 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2587 canAssignObjCInterfaces(RHSIface, LHSIface);
2588}
2589
Steve Naroffec0550f2007-10-15 20:41:53 +00002590/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2591/// both shall have the identically qualified version of a compatible type.
2592/// C99 6.2.7p1: Two types have compatible types if their types are the
2593/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002594bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2595 return !mergeTypes(LHS, RHS).isNull();
2596}
2597
2598QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2599 const FunctionType *lbase = lhs->getAsFunctionType();
2600 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002601 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2602 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002603 bool allLTypes = true;
2604 bool allRTypes = true;
2605
2606 // Check return type
2607 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2608 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002609 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2610 allLTypes = false;
2611 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2612 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002613
2614 if (lproto && rproto) { // two C99 style function prototypes
2615 unsigned lproto_nargs = lproto->getNumArgs();
2616 unsigned rproto_nargs = rproto->getNumArgs();
2617
2618 // Compatible functions must have the same number of arguments
2619 if (lproto_nargs != rproto_nargs)
2620 return QualType();
2621
2622 // Variadic and non-variadic functions aren't compatible
2623 if (lproto->isVariadic() != rproto->isVariadic())
2624 return QualType();
2625
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002626 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2627 return QualType();
2628
Eli Friedman3d815e72008-08-22 00:56:42 +00002629 // Check argument compatibility
2630 llvm::SmallVector<QualType, 10> types;
2631 for (unsigned i = 0; i < lproto_nargs; i++) {
2632 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2633 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2634 QualType argtype = mergeTypes(largtype, rargtype);
2635 if (argtype.isNull()) return QualType();
2636 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002637 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2638 allLTypes = false;
2639 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2640 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002641 }
2642 if (allLTypes) return lhs;
2643 if (allRTypes) return rhs;
2644 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002645 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002646 }
2647
2648 if (lproto) allRTypes = false;
2649 if (rproto) allLTypes = false;
2650
Douglas Gregor72564e72009-02-26 23:50:07 +00002651 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002652 if (proto) {
2653 if (proto->isVariadic()) return QualType();
2654 // Check that the types are compatible with the types that
2655 // would result from default argument promotions (C99 6.7.5.3p15).
2656 // The only types actually affected are promotable integer
2657 // types and floats, which would be passed as a different
2658 // type depending on whether the prototype is visible.
2659 unsigned proto_nargs = proto->getNumArgs();
2660 for (unsigned i = 0; i < proto_nargs; ++i) {
2661 QualType argTy = proto->getArgType(i);
2662 if (argTy->isPromotableIntegerType() ||
2663 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2664 return QualType();
2665 }
2666
2667 if (allLTypes) return lhs;
2668 if (allRTypes) return rhs;
2669 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002670 proto->getNumArgs(), lproto->isVariadic(),
2671 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002672 }
2673
2674 if (allLTypes) return lhs;
2675 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002676 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002677}
2678
2679QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002680 // C++ [expr]: If an expression initially has the type "reference to T", the
2681 // type is adjusted to "T" prior to any further analysis, the expression
2682 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002683 // expression is an lvalue unless the reference is an rvalue reference and
2684 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00002685 // FIXME: C++ shouldn't be going through here! The rules are different
2686 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002687 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2688 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00002689 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002690 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002691 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002692 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002693
Eli Friedman3d815e72008-08-22 00:56:42 +00002694 QualType LHSCan = getCanonicalType(LHS),
2695 RHSCan = getCanonicalType(RHS);
2696
2697 // If two types are identical, they are compatible.
2698 if (LHSCan == RHSCan)
2699 return LHS;
2700
2701 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002702 // Note that we handle extended qualifiers later, in the
2703 // case for ExtQualType.
2704 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00002705 return QualType();
2706
2707 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2708 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2709
Chris Lattner1adb8832008-01-14 05:45:46 +00002710 // We want to consider the two function types to be the same for these
2711 // comparisons, just force one to the other.
2712 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2713 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002714
2715 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002716 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2717 LHSClass = Type::ConstantArray;
2718 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2719 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002720
Nate Begeman213541a2008-04-18 23:10:10 +00002721 // Canonicalize ExtVector -> Vector.
2722 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2723 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002724
Chris Lattnerb0489812008-04-07 06:38:24 +00002725 // Consider qualified interfaces and interfaces the same.
2726 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2727 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002728
Chris Lattnera36a61f2008-04-07 05:43:21 +00002729 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002730 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002731 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2732 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2733
2734 // ID acts sort of like void* for ObjC interfaces
2735 if (LHSIface && isObjCIdStructType(RHS))
2736 return LHS;
2737 if (RHSIface && isObjCIdStructType(LHS))
2738 return RHS;
2739
Steve Naroffbc76dd02008-12-10 22:14:21 +00002740 // ID is compatible with all qualified id types.
2741 if (LHS->isObjCQualifiedIdType()) {
2742 if (const PointerType *PT = RHS->getAsPointerType()) {
2743 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002744 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002745 return LHS;
2746 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2747 // Unfortunately, this API is part of Sema (which we don't have access
2748 // to. Need to refactor. The following check is insufficient, since we
2749 // need to make sure the class implements the protocol.
2750 if (pType->isObjCInterfaceType())
2751 return LHS;
2752 }
2753 }
2754 if (RHS->isObjCQualifiedIdType()) {
2755 if (const PointerType *PT = LHS->getAsPointerType()) {
2756 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002757 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002758 return RHS;
2759 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2760 // Unfortunately, this API is part of Sema (which we don't have access
2761 // to. Need to refactor. The following check is insufficient, since we
2762 // need to make sure the class implements the protocol.
2763 if (pType->isObjCInterfaceType())
2764 return RHS;
2765 }
2766 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002767 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2768 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002769 if (const EnumType* ETy = LHS->getAsEnumType()) {
2770 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2771 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002772 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002773 if (const EnumType* ETy = RHS->getAsEnumType()) {
2774 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2775 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002776 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002777
Eli Friedman3d815e72008-08-22 00:56:42 +00002778 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002779 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002780
Steve Naroff4a746782008-01-09 22:43:08 +00002781 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002782 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00002783#define TYPE(Class, Base)
2784#define ABSTRACT_TYPE(Class, Base)
2785#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2786#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2787#include "clang/AST/TypeNodes.def"
2788 assert(false && "Non-canonical and dependent types shouldn't get here");
2789 return QualType();
2790
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002791 case Type::LValueReference:
2792 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00002793 case Type::MemberPointer:
2794 assert(false && "C++ should never be in mergeTypes");
2795 return QualType();
2796
2797 case Type::IncompleteArray:
2798 case Type::VariableArray:
2799 case Type::FunctionProto:
2800 case Type::ExtVector:
2801 case Type::ObjCQualifiedInterface:
2802 assert(false && "Types are eliminated above");
2803 return QualType();
2804
Chris Lattner1adb8832008-01-14 05:45:46 +00002805 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002806 {
2807 // Merge two pointer types, while trying to preserve typedef info
2808 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2809 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2810 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2811 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002812 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2813 return LHS;
2814 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2815 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002816 return getPointerType(ResultType);
2817 }
Steve Naroffc0febd52008-12-10 17:49:55 +00002818 case Type::BlockPointer:
2819 {
2820 // Merge two block pointer types, while trying to preserve typedef info
2821 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2822 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2823 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2824 if (ResultType.isNull()) return QualType();
2825 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2826 return LHS;
2827 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2828 return RHS;
2829 return getBlockPointerType(ResultType);
2830 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002831 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002832 {
2833 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2834 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2835 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2836 return QualType();
2837
2838 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2839 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2840 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2841 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002842 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2843 return LHS;
2844 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2845 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002846 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2847 ArrayType::ArraySizeModifier(), 0);
2848 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2849 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002850 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2851 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00002852 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2853 return LHS;
2854 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2855 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002856 if (LVAT) {
2857 // FIXME: This isn't correct! But tricky to implement because
2858 // the array's size has to be the size of LHS, but the type
2859 // has to be different.
2860 return LHS;
2861 }
2862 if (RVAT) {
2863 // FIXME: This isn't correct! But tricky to implement because
2864 // the array's size has to be the size of RHS, but the type
2865 // has to be different.
2866 return RHS;
2867 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002868 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2869 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00002870 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002871 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002872 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002873 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00002874 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00002875 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00002876 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00002877 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
2878 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002879 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002880 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002881 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002882 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00002883 case Type::Complex:
2884 // Distinct complex types are incompatible.
2885 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002886 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002887 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00002888 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2889 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00002890 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00002891 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002892 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002893 // FIXME: This should be type compatibility, e.g. whether
2894 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00002895 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2896 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2897 if (LHSIface && RHSIface &&
2898 canAssignObjCInterfaces(LHSIface, RHSIface))
2899 return LHS;
2900
Eli Friedman3d815e72008-08-22 00:56:42 +00002901 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00002902 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00002903 case Type::ObjCQualifiedId:
2904 // Distinct qualified id's are not compatible.
2905 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002906 case Type::FixedWidthInt:
2907 // Distinct fixed-width integers are not compatible.
2908 return QualType();
2909 case Type::ObjCQualifiedClass:
2910 // Distinct qualified classes are not compatible.
2911 return QualType();
2912 case Type::ExtQual:
2913 // FIXME: ExtQual types can be compatible even if they're not
2914 // identical!
2915 return QualType();
2916 // First attempt at an implementation, but I'm not really sure it's
2917 // right...
2918#if 0
2919 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
2920 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
2921 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
2922 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
2923 return QualType();
2924 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
2925 LHSBase = QualType(LQual->getBaseType(), 0);
2926 RHSBase = QualType(RQual->getBaseType(), 0);
2927 ResultType = mergeTypes(LHSBase, RHSBase);
2928 if (ResultType.isNull()) return QualType();
2929 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
2930 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
2931 return LHS;
2932 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
2933 return RHS;
2934 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
2935 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
2936 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
2937 return ResultType;
2938#endif
Steve Naroffec0550f2007-10-15 20:41:53 +00002939 }
Douglas Gregor72564e72009-02-26 23:50:07 +00002940
2941 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002942}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002943
Chris Lattner5426bf62008-04-07 07:01:58 +00002944//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00002945// Integer Predicates
2946//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00002947
Eli Friedmanad74a752008-06-28 06:23:08 +00002948unsigned ASTContext::getIntWidth(QualType T) {
2949 if (T == BoolTy)
2950 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002951 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
2952 return FWIT->getWidth();
2953 }
2954 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00002955 return (unsigned)getTypeSize(T);
2956}
2957
2958QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2959 assert(T->isSignedIntegerType() && "Unexpected type");
2960 if (const EnumType* ETy = T->getAsEnumType())
2961 T = ETy->getDecl()->getIntegerType();
2962 const BuiltinType* BTy = T->getAsBuiltinType();
2963 assert (BTy && "Unexpected signed integer type");
2964 switch (BTy->getKind()) {
2965 case BuiltinType::Char_S:
2966 case BuiltinType::SChar:
2967 return UnsignedCharTy;
2968 case BuiltinType::Short:
2969 return UnsignedShortTy;
2970 case BuiltinType::Int:
2971 return UnsignedIntTy;
2972 case BuiltinType::Long:
2973 return UnsignedLongTy;
2974 case BuiltinType::LongLong:
2975 return UnsignedLongLongTy;
2976 default:
2977 assert(0 && "Unexpected signed integer type");
2978 return QualType();
2979 }
2980}
2981
2982
2983//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00002984// Serialization Support
2985//===----------------------------------------------------------------------===//
2986
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002987/// Emit - Serialize an ASTContext object to Bitcode.
2988void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002989 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00002990 S.EmitRef(SourceMgr);
2991 S.EmitRef(Target);
2992 S.EmitRef(Idents);
2993 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002994
Ted Kremenekfee04522007-10-31 22:44:07 +00002995 // Emit the size of the type vector so that we can reserve that size
2996 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00002997 S.EmitInt(Types.size());
2998
Ted Kremenek03ed4402007-11-13 22:02:55 +00002999 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3000 I!=E;++I)
3001 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00003002
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003003 S.EmitOwnedPtr(TUDecl);
3004
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003005 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003006}
3007
Ted Kremenek0f84c002007-11-13 00:25:37 +00003008ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003009
3010 // Read the language options.
3011 LangOptions LOpts;
3012 LOpts.Read(D);
3013
Ted Kremenekfee04522007-10-31 22:44:07 +00003014 SourceManager &SM = D.ReadRef<SourceManager>();
3015 TargetInfo &t = D.ReadRef<TargetInfo>();
3016 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3017 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00003018
Ted Kremenekfee04522007-10-31 22:44:07 +00003019 unsigned size_reserve = D.ReadInt();
3020
Douglas Gregor2e1cd422008-11-17 14:58:09 +00003021 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3022 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00003023
Ted Kremenek03ed4402007-11-13 22:02:55 +00003024 for (unsigned i = 0; i < size_reserve; ++i)
3025 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00003026
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003027 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3028
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003029 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00003030
3031 return A;
3032}