blob: a51f432880ec214c2d554e0f2ec601acbeddbcdd [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000021#include "llvm/ADT/StringExtras.h"
Ted Kremenek7192f8e2007-10-31 17:10:13 +000022#include "llvm/Bitcode/Serialize.h"
23#include "llvm/Bitcode/Deserialize.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000025
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner61710852008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Steve Naroffc0ac4922009-01-27 23:20:32 +000035 bool FreeMem, unsigned size_reserve) :
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +000036 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
Steve Naroffc0ac4922009-01-27 23:20:32 +000037 SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
Douglas Gregor2e1cd422008-11-17 14:58:09 +000038 Idents(idents), Selectors(sels)
Daniel Dunbare91593e2008-08-11 04:54:23 +000039{
40 if (size_reserve > 0) Types.reserve(size_reserve);
41 InitBuiltinTypes();
Douglas Gregor3573c0c2009-02-14 20:49:29 +000042 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.Freestanding);
Daniel Dunbare91593e2008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
44}
45
Reid Spencer5f016e22007-07-11 17:01:13 +000046ASTContext::~ASTContext() {
47 // Deallocate all the types.
48 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000049 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000050 Types.pop_back();
51 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000052
Nuno Lopesb74668e2008-12-17 22:30:25 +000053 {
54 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
55 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
56 while (I != E) {
57 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
58 delete R;
59 }
60 }
61
62 {
63 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
64 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
65 while (I != E) {
66 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
67 delete R;
68 }
69 }
70
71 {
72 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator
73 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
74 while (I != E) {
75 RecordDecl *R = const_cast<RecordDecl*>((I++)->second);
76 R->Destroy(*this);
77 }
78 }
79
Eli Friedmanb26153c2008-05-27 03:08:09 +000080 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000081}
82
83void ASTContext::PrintStats() const {
84 fprintf(stderr, "*** AST Context Stats:\n");
85 fprintf(stderr, " %d types total.\n", (int)Types.size());
86 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +000087 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000088 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
Sebastian Redlf30208a2009-01-24 21:16:55 +000089 unsigned NumMemberPointer = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000090
91 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000092 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
93 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +000094 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000095
96 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
97 Type *T = Types[i];
98 if (isa<BuiltinType>(T))
99 ++NumBuiltin;
100 else if (isa<PointerType>(T))
101 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000102 else if (isa<BlockPointerType>(T))
103 ++NumBlockPointer;
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 else if (isa<ReferenceType>(T))
105 ++NumReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000106 else if (isa<MemberPointerType>(T))
107 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000108 else if (isa<ComplexType>(T))
109 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 else if (isa<ArrayType>(T))
111 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000112 else if (isa<VectorType>(T))
113 ++NumVector;
Douglas Gregor72564e72009-02-26 23:50:07 +0000114 else if (isa<FunctionNoProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 ++NumFunctionNP;
Douglas Gregor72564e72009-02-26 23:50:07 +0000116 else if (isa<FunctionProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 ++NumFunctionP;
118 else if (isa<TypedefType>(T))
119 ++NumTypeName;
120 else if (TagType *TT = dyn_cast<TagType>(T)) {
121 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000122 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000124 case TagDecl::TK_struct: ++NumTagStruct; break;
125 case TagDecl::TK_union: ++NumTagUnion; break;
126 case TagDecl::TK_class: ++NumTagClass; break;
127 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000129 } else if (isa<ObjCInterfaceType>(T))
130 ++NumObjCInterfaces;
131 else if (isa<ObjCQualifiedInterfaceType>(T))
132 ++NumObjCQualifiedInterfaces;
133 else if (isa<ObjCQualifiedIdType>(T))
134 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000135 else if (isa<TypeOfType>(T))
136 ++NumTypeOfTypes;
Douglas Gregor72564e72009-02-26 23:50:07 +0000137 else if (isa<TypeOfExprType>(T))
138 ++NumTypeOfExprTypes;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000139 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000140 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 assert(0 && "Unknown type!");
142 }
143 }
144
145 fprintf(stderr, " %d builtin types\n", NumBuiltin);
146 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000147 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 fprintf(stderr, " %d reference types\n", NumReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000149 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000150 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000152 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
154 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
155 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
156 fprintf(stderr, " %d tagged types\n", NumTagged);
157 fprintf(stderr, " %d struct types\n", NumTagStruct);
158 fprintf(stderr, " %d union types\n", NumTagUnion);
159 fprintf(stderr, " %d class types\n", NumTagClass);
160 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000161 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000162 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000163 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000164 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000165 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000166 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor72564e72009-02-26 23:50:07 +0000167 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Steve Naroff6cc18962008-05-21 15:59:22 +0000168
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
170 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000171 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000172 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000173 NumFunctionP*sizeof(FunctionProtoType)+
174 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000175 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000176 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000177}
178
179
180void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000181 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000182}
183
Reid Spencer5f016e22007-07-11 17:01:13 +0000184void ASTContext::InitBuiltinTypes() {
185 assert(VoidTy.isNull() && "Context reinitialized?");
186
187 // C99 6.2.5p19.
188 InitBuiltinType(VoidTy, BuiltinType::Void);
189
190 // C99 6.2.5p2.
191 InitBuiltinType(BoolTy, BuiltinType::Bool);
192 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000193 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 InitBuiltinType(CharTy, BuiltinType::Char_S);
195 else
196 InitBuiltinType(CharTy, BuiltinType::Char_U);
197 // C99 6.2.5p4.
198 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
199 InitBuiltinType(ShortTy, BuiltinType::Short);
200 InitBuiltinType(IntTy, BuiltinType::Int);
201 InitBuiltinType(LongTy, BuiltinType::Long);
202 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
203
204 // C99 6.2.5p6.
205 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
206 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
207 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
208 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
209 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
210
211 // C99 6.2.5p10.
212 InitBuiltinType(FloatTy, BuiltinType::Float);
213 InitBuiltinType(DoubleTy, BuiltinType::Double);
214 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000215
Chris Lattner3a250322009-02-26 23:43:47 +0000216 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
217 InitBuiltinType(WCharTy, BuiltinType::WChar);
218 else // C99
219 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000220
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000221 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000222 InitBuiltinType(OverloadTy, BuiltinType::Overload);
223
224 // Placeholder type for type-dependent expressions whose type is
225 // completely unknown. No code should ever check a type against
226 // DependentTy and users should never see it; however, it is here to
227 // help diagnose failures to properly check for type-dependent
228 // expressions.
229 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000230
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 // C99 6.2.5p11.
232 FloatComplexTy = getComplexType(FloatTy);
233 DoubleComplexTy = getComplexType(DoubleTy);
234 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000235
Steve Naroff7e219e42007-10-15 14:41:52 +0000236 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000237 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000238 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000239 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000240 ClassStructType = 0;
241
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000242 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000243
244 // void * type
245 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000246}
247
Chris Lattner464175b2007-07-18 17:52:12 +0000248//===----------------------------------------------------------------------===//
249// Type Sizing and Analysis
250//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000251
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000252/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
253/// scalar floating point type.
254const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
255 const BuiltinType *BT = T->getAsBuiltinType();
256 assert(BT && "Not a floating point type!");
257 switch (BT->getKind()) {
258 default: assert(0 && "Not a floating point type!");
259 case BuiltinType::Float: return Target.getFloatFormat();
260 case BuiltinType::Double: return Target.getDoubleFormat();
261 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
262 }
263}
264
Chris Lattneraf707ab2009-01-24 21:53:27 +0000265/// getDeclAlign - Return a conservative estimate of the alignment of the
266/// specified decl. Note that bitfields do not have a valid alignment, so
267/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000268unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000269 unsigned Align = Target.getCharWidth();
270
271 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
272 Align = std::max(Align, AA->getAlignment());
273
Chris Lattneraf707ab2009-01-24 21:53:27 +0000274 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
275 QualType T = VD->getType();
276 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000277 if (!T->isIncompleteType() && !T->isFunctionType()) {
278 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
279 T = cast<ArrayType>(T)->getElementType();
280
281 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
282 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000283 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000284
285 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000286}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000287
Chris Lattnera7674d82007-07-13 22:13:22 +0000288/// getTypeSize - Return the size of the specified type, in bits. This method
289/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000290std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000291ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000292 T = getCanonicalType(T);
Mike Stump5e301002009-02-27 18:32:39 +0000293 uint64_t Width=0;
294 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000295 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000296#define TYPE(Class, Base)
297#define ABSTRACT_TYPE(Class, Base)
298#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
299#define DEPENDENT_TYPE(Class, Base) case Type::Class:
300#include "clang/AST/TypeNodes.def"
301 assert(false && "Should not see non-canonical or dependent types");
302 break;
303
Chris Lattner692233e2007-07-13 22:27:08 +0000304 case Type::FunctionNoProto:
305 case Type::FunctionProto:
Douglas Gregor72564e72009-02-26 23:50:07 +0000306 case Type::IncompleteArray:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000307 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000308 case Type::VariableArray:
309 assert(0 && "VLAs not implemented yet!");
310 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000311 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000312
Chris Lattner98be4942008-03-05 18:54:05 +0000313 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000314 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000315 Align = EltInfo.second;
316 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000317 }
Nate Begeman213541a2008-04-18 23:10:10 +0000318 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000319 case Type::Vector: {
320 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000321 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000322 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000323 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000324 // If the alignment is not a power of 2, round up to the next power of 2.
325 // This happens for non-power-of-2 length vectors.
326 // FIXME: this should probably be a target property.
327 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000328 break;
329 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000330
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000331 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000332 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000333 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000334 case BuiltinType::Void:
335 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000336 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000337 Width = Target.getBoolWidth();
338 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000339 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000340 case BuiltinType::Char_S:
341 case BuiltinType::Char_U:
342 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000343 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000344 Width = Target.getCharWidth();
345 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000346 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000347 case BuiltinType::WChar:
348 Width = Target.getWCharWidth();
349 Align = Target.getWCharAlign();
350 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000351 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000352 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000353 Width = Target.getShortWidth();
354 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000355 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000356 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000357 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000358 Width = Target.getIntWidth();
359 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000360 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000361 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000362 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000363 Width = Target.getLongWidth();
364 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000365 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000366 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000367 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000368 Width = Target.getLongLongWidth();
369 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000370 break;
371 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000372 Width = Target.getFloatWidth();
373 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000374 break;
375 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000376 Width = Target.getDoubleWidth();
377 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000378 break;
379 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000380 Width = Target.getLongDoubleWidth();
381 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000382 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000383 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000384 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000385 case Type::FixedWidthInt:
386 // FIXME: This isn't precisely correct; the width/alignment should depend
387 // on the available types for the target
388 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000389 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000390 Align = Width;
391 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000392 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000393 // FIXME: Pointers into different addr spaces could have different sizes and
394 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000395 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000396 case Type::ObjCQualifiedId:
Eli Friedman4bdf0872009-02-22 04:02:33 +0000397 case Type::ObjCQualifiedClass:
Douglas Gregor72564e72009-02-26 23:50:07 +0000398 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000399 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000400 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000401 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000402 case Type::BlockPointer: {
403 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
404 Width = Target.getPointerWidth(AS);
405 Align = Target.getPointerAlign(AS);
406 break;
407 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000408 case Type::Pointer: {
409 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000410 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000411 Align = Target.getPointerAlign(AS);
412 break;
413 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000414 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000415 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000416 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000417 // FIXME: This is wrong for struct layout: a reference in a struct has
418 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000419 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000420 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000421 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000422 // the GCC ABI, where pointers to data are one pointer large, pointers to
423 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000424 // other compilers too, we need to delegate this completely to TargetInfo
425 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000426 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
427 unsigned AS = Pointee.getAddressSpace();
428 Width = Target.getPointerWidth(AS);
429 if (Pointee->isFunctionType())
430 Width *= 2;
431 Align = Target.getPointerAlign(AS);
432 // GCC aligns at single pointer width.
433 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000434 case Type::Complex: {
435 // Complex types have the same alignment as their elements, but twice the
436 // size.
437 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000438 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000439 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000440 Align = EltInfo.second;
441 break;
442 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000443 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000444 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000445 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
446 Width = Layout.getSize();
447 Align = Layout.getAlignment();
448 break;
449 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000450 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000451 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000452 const TagType *TT = cast<TagType>(T);
453
454 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000455 Width = 1;
456 Align = 1;
457 break;
458 }
459
Daniel Dunbar1d751182008-11-08 05:48:37 +0000460 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000461 return getTypeInfo(ET->getDecl()->getIntegerType());
462
Daniel Dunbar1d751182008-11-08 05:48:37 +0000463 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000464 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
465 Width = Layout.getSize();
466 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000467 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000468 }
Chris Lattner71763312008-04-06 22:05:18 +0000469 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000470
Chris Lattner464175b2007-07-18 17:52:12 +0000471 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000472 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000473}
474
Chris Lattner34ebde42009-01-27 18:08:34 +0000475/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
476/// type for the current target in bits. This can be different than the ABI
477/// alignment in cases where it is beneficial for performance to overalign
478/// a data type.
479unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
480 unsigned ABIAlign = getTypeAlign(T);
481
482 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000483 if (T->isSpecificBuiltinType(BuiltinType::Double))
484 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000485
486 return ABIAlign;
487}
488
489
Devang Patel8b277042008-06-04 21:22:16 +0000490/// LayoutField - Field layout.
491void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000492 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000493 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000494 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000495 uint64_t FieldOffset = IsUnion ? 0 : Size;
496 uint64_t FieldSize;
497 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000498
499 // FIXME: Should this override struct packing? Probably we want to
500 // take the minimum?
501 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
502 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000503
504 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
505 // TODO: Need to check this algorithm on other targets!
506 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000507 FieldSize =
508 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000509
510 std::pair<uint64_t, unsigned> FieldInfo =
511 Context.getTypeInfo(FD->getType());
512 uint64_t TypeSize = FieldInfo.first;
513
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000514 // Determine the alignment of this bitfield. The packing
515 // attributes define a maximum and the alignment attribute defines
516 // a minimum.
517 // FIXME: What is the right behavior when the specified alignment
518 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000519 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000520 if (FieldPacking)
521 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000522 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
523 FieldAlign = std::max(FieldAlign, AA->getAlignment());
524
525 // Check if we need to add padding to give the field the correct
526 // alignment.
527 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
528 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
529
530 // Padding members don't affect overall alignment
531 if (!FD->getIdentifier())
532 FieldAlign = 1;
533 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000534 if (FD->getType()->isIncompleteArrayType()) {
535 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000536 // query getTypeInfo about these, so we figure it out here.
537 // Flexible array members don't have any size, but they
538 // have to be aligned appropriately for their element type.
539 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000540 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000541 FieldAlign = Context.getTypeAlign(ATy->getElementType());
542 } else {
543 std::pair<uint64_t, unsigned> FieldInfo =
544 Context.getTypeInfo(FD->getType());
545 FieldSize = FieldInfo.first;
546 FieldAlign = FieldInfo.second;
547 }
548
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000549 // Determine the alignment of this bitfield. The packing
550 // attributes define a maximum and the alignment attribute defines
551 // a minimum. Additionally, the packing alignment must be at least
552 // a byte for non-bitfields.
553 //
554 // FIXME: What is the right behavior when the specified alignment
555 // is smaller than the specified packing?
556 if (FieldPacking)
557 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000558 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
559 FieldAlign = std::max(FieldAlign, AA->getAlignment());
560
561 // Round up the current record size to the field's alignment boundary.
562 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
563 }
564
565 // Place this field at the current location.
566 FieldOffsets[FieldNo] = FieldOffset;
567
568 // Reserve space for this field.
569 if (IsUnion) {
570 Size = std::max(Size, FieldSize);
571 } else {
572 Size = FieldOffset + FieldSize;
573 }
574
575 // Remember max struct/class alignment.
576 Alignment = std::max(Alignment, FieldAlign);
577}
578
Fariborz Jahanian88e469c2009-03-05 20:08:48 +0000579void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
580 std::vector<FieldDecl*> &Fields) const {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000581 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
582 if (SuperClass)
583 CollectObjCIvars(SuperClass, Fields);
584 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
585 E = OI->ivar_end(); I != E; ++I) {
586 ObjCIvarDecl *IVDecl = (*I);
587 if (!IVDecl->isInvalidDecl())
588 Fields.push_back(cast<FieldDecl>(IVDecl));
589 }
590}
591
592/// addRecordToClass - produces record info. for the class for its
593/// ivars and all those inherited.
594///
595const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
596{
597 const RecordDecl *&RD = ASTRecordForInterface[D];
598 if (RD)
599 return RD;
600 std::vector<FieldDecl*> RecFields;
601 CollectObjCIvars(D, RecFields);
602 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
603 D->getLocation(),
604 D->getIdentifier());
605 /// FIXME! Can do collection of ivars and adding to the record while
606 /// doing it.
607 for (unsigned int i = 0; i != RecFields.size(); i++) {
608 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
609 RecFields[i]->getLocation(),
610 RecFields[i]->getIdentifier(),
611 RecFields[i]->getType(),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000612 RecFields[i]->getBitWidth(), false);
Douglas Gregor482b77d2009-01-12 23:27:07 +0000613 NewRD->addDecl(Field);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000614 }
615 NewRD->completeDefinition(*this);
616 RD = NewRD;
617 return RD;
618}
Devang Patel44a3dde2008-06-04 21:54:36 +0000619
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000620/// setFieldDecl - maps a field for the given Ivar reference node.
621//
622void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
623 const ObjCIvarDecl *Ivar,
624 const ObjCIvarRefExpr *MRef) {
625 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
626 lookupFieldDeclForIvar(*this, Ivar);
627 ASTFieldForIvarRef[MRef] = FD;
628}
629
Chris Lattner61710852008-10-05 17:34:18 +0000630/// getASTObjcInterfaceLayout - Get or compute information about the layout of
631/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000632/// position information.
633const ASTRecordLayout &
634ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
635 // Look up this layout, if already laid out, return what we have.
636 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
637 if (Entry) return *Entry;
638
639 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
640 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000641 ASTRecordLayout *NewEntry = NULL;
642 unsigned FieldCount = D->ivar_size();
643 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
644 FieldCount++;
645 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
646 unsigned Alignment = SL.getAlignment();
647 uint64_t Size = SL.getSize();
648 NewEntry = new ASTRecordLayout(Size, Alignment);
649 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000650 // Super class is at the beginning of the layout.
651 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000652 } else {
653 NewEntry = new ASTRecordLayout();
654 NewEntry->InitializeLayout(FieldCount);
655 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000656 Entry = NewEntry;
657
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000658 unsigned StructPacking = 0;
659 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
660 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000661
662 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
663 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
664 AA->getAlignment()));
665
666 // Layout each ivar sequentially.
667 unsigned i = 0;
668 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
669 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
670 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000671 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000672 }
673
674 // Finally, round the size of the total struct up to the alignment of the
675 // struct itself.
676 NewEntry->FinalizeLayout();
677 return *NewEntry;
678}
679
Devang Patel88a981b2007-11-01 19:11:01 +0000680/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000681/// specified record (struct/union/class), which indicates its size and field
682/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000683const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000684 D = D->getDefinition(*this);
685 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000686
Chris Lattner464175b2007-07-18 17:52:12 +0000687 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000688 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000689 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000690
Devang Patel88a981b2007-11-01 19:11:01 +0000691 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
692 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
693 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000694 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000695
Douglas Gregore267ff32008-12-11 20:41:00 +0000696 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor44b43212008-12-11 16:49:14 +0000697 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000698 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000699
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000700 unsigned StructPacking = 0;
701 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
702 StructPacking = PA->getAlignment();
703
Eli Friedman4bd998b2008-05-30 09:31:38 +0000704 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000705 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
706 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000707
Eli Friedman4bd998b2008-05-30 09:31:38 +0000708 // Layout each field, for now, just sequentially, respecting alignment. In
709 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000710 unsigned FieldIdx = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000711 for (RecordDecl::field_iterator Field = D->field_begin(),
712 FieldEnd = D->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000713 Field != FieldEnd; (void)++Field, ++FieldIdx)
714 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000715
716 // Finally, round the size of the total struct up to the alignment of the
717 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000718 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000719 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000720}
721
Chris Lattnera7674d82007-07-13 22:13:22 +0000722//===----------------------------------------------------------------------===//
723// Type creation/memoization methods
724//===----------------------------------------------------------------------===//
725
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000726QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000727 QualType CanT = getCanonicalType(T);
728 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000729 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000730
731 // If we are composing extended qualifiers together, merge together into one
732 // ExtQualType node.
733 unsigned CVRQuals = T.getCVRQualifiers();
734 QualType::GCAttrTypes GCAttr = QualType::GCNone;
735 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000736
Chris Lattnerb7d25532009-02-18 22:53:11 +0000737 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
738 // If this type already has an address space specified, it cannot get
739 // another one.
740 assert(EQT->getAddressSpace() == 0 &&
741 "Type cannot be in multiple addr spaces!");
742 GCAttr = EQT->getObjCGCAttr();
743 TypeNode = EQT->getBaseType();
744 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000745
Chris Lattnerb7d25532009-02-18 22:53:11 +0000746 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000747 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000748 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000749 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000750 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000751 return QualType(EXTQy, CVRQuals);
752
Christopher Lambebb97e92008-02-04 02:31:56 +0000753 // If the base type isn't canonical, this won't be a canonical type either,
754 // so fill in the canonical type field.
755 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000756 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000757 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000758
Chris Lattnerb7d25532009-02-18 22:53:11 +0000759 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000760 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000761 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000762 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000763 ExtQualType *New =
764 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000765 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000766 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000767 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000768}
769
Chris Lattnerb7d25532009-02-18 22:53:11 +0000770QualType ASTContext::getObjCGCQualType(QualType T,
771 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000772 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000773 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000774 return T;
775
Chris Lattnerb7d25532009-02-18 22:53:11 +0000776 // If we are composing extended qualifiers together, merge together into one
777 // ExtQualType node.
778 unsigned CVRQuals = T.getCVRQualifiers();
779 Type *TypeNode = T.getTypePtr();
780 unsigned AddressSpace = 0;
781
782 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
783 // If this type already has an address space specified, it cannot get
784 // another one.
785 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
786 "Type cannot be in multiple addr spaces!");
787 AddressSpace = EQT->getAddressSpace();
788 TypeNode = EQT->getBaseType();
789 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000790
791 // Check if we've already instantiated an gc qual'd type of this type.
792 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000793 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000794 void *InsertPos = 0;
795 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000796 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000797
798 // If the base type isn't canonical, this won't be a canonical type either,
799 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000800 // FIXME: Isn't this also not canonical if the base type is a array
801 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000802 QualType Canonical;
803 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000804 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000805
Chris Lattnerb7d25532009-02-18 22:53:11 +0000806 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000807 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
808 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
809 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000810 ExtQualType *New =
811 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000812 ExtQualTypes.InsertNode(New, InsertPos);
813 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000814 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000815}
Chris Lattnera7674d82007-07-13 22:13:22 +0000816
Reid Spencer5f016e22007-07-11 17:01:13 +0000817/// getComplexType - Return the uniqued reference to the type for a complex
818/// number with the specified element type.
819QualType ASTContext::getComplexType(QualType T) {
820 // Unique pointers, to guarantee there is only one pointer of a particular
821 // structure.
822 llvm::FoldingSetNodeID ID;
823 ComplexType::Profile(ID, T);
824
825 void *InsertPos = 0;
826 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
827 return QualType(CT, 0);
828
829 // If the pointee type isn't canonical, this won't be a canonical type either,
830 // so fill in the canonical type field.
831 QualType Canonical;
832 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000833 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000834
835 // Get the new insert position for the node we care about.
836 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000837 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000838 }
Steve Narofff83820b2009-01-27 22:08:43 +0000839 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 Types.push_back(New);
841 ComplexTypes.InsertNode(New, InsertPos);
842 return QualType(New, 0);
843}
844
Eli Friedmanf98aba32009-02-13 02:31:07 +0000845QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
846 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
847 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
848 FixedWidthIntType *&Entry = Map[Width];
849 if (!Entry)
850 Entry = new FixedWidthIntType(Width, Signed);
851 return QualType(Entry, 0);
852}
Reid Spencer5f016e22007-07-11 17:01:13 +0000853
854/// getPointerType - Return the uniqued reference to the type for a pointer to
855/// the specified type.
856QualType ASTContext::getPointerType(QualType T) {
857 // Unique pointers, to guarantee there is only one pointer of a particular
858 // structure.
859 llvm::FoldingSetNodeID ID;
860 PointerType::Profile(ID, T);
861
862 void *InsertPos = 0;
863 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
864 return QualType(PT, 0);
865
866 // If the pointee type isn't canonical, this won't be a canonical type either,
867 // so fill in the canonical type field.
868 QualType Canonical;
869 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000870 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000871
872 // Get the new insert position for the node we care about.
873 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000874 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000875 }
Steve Narofff83820b2009-01-27 22:08:43 +0000876 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 Types.push_back(New);
878 PointerTypes.InsertNode(New, InsertPos);
879 return QualType(New, 0);
880}
881
Steve Naroff5618bd42008-08-27 16:04:49 +0000882/// getBlockPointerType - Return the uniqued reference to the type for
883/// a pointer to the specified block.
884QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000885 assert(T->isFunctionType() && "block of function types only");
886 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000887 // structure.
888 llvm::FoldingSetNodeID ID;
889 BlockPointerType::Profile(ID, T);
890
891 void *InsertPos = 0;
892 if (BlockPointerType *PT =
893 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
894 return QualType(PT, 0);
895
Steve Naroff296e8d52008-08-28 19:20:44 +0000896 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000897 // type either so fill in the canonical type field.
898 QualType Canonical;
899 if (!T->isCanonical()) {
900 Canonical = getBlockPointerType(getCanonicalType(T));
901
902 // Get the new insert position for the node we care about.
903 BlockPointerType *NewIP =
904 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000905 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000906 }
Steve Narofff83820b2009-01-27 22:08:43 +0000907 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000908 Types.push_back(New);
909 BlockPointerTypes.InsertNode(New, InsertPos);
910 return QualType(New, 0);
911}
912
Reid Spencer5f016e22007-07-11 17:01:13 +0000913/// getReferenceType - Return the uniqued reference to the type for a reference
914/// to the specified type.
915QualType ASTContext::getReferenceType(QualType T) {
916 // Unique pointers, to guarantee there is only one pointer of a particular
917 // structure.
918 llvm::FoldingSetNodeID ID;
919 ReferenceType::Profile(ID, T);
920
921 void *InsertPos = 0;
922 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
923 return QualType(RT, 0);
924
925 // If the referencee type isn't canonical, this won't be a canonical type
926 // either, so fill in the canonical type field.
927 QualType Canonical;
928 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000929 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000930
931 // Get the new insert position for the node we care about.
932 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000933 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000934 }
935
Steve Narofff83820b2009-01-27 22:08:43 +0000936 ReferenceType *New = new (*this,8) ReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000937 Types.push_back(New);
938 ReferenceTypes.InsertNode(New, InsertPos);
939 return QualType(New, 0);
940}
941
Sebastian Redlf30208a2009-01-24 21:16:55 +0000942/// getMemberPointerType - Return the uniqued reference to the type for a
943/// member pointer to the specified type, in the specified class.
944QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
945{
946 // Unique pointers, to guarantee there is only one pointer of a particular
947 // structure.
948 llvm::FoldingSetNodeID ID;
949 MemberPointerType::Profile(ID, T, Cls);
950
951 void *InsertPos = 0;
952 if (MemberPointerType *PT =
953 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
954 return QualType(PT, 0);
955
956 // If the pointee or class type isn't canonical, this won't be a canonical
957 // type either, so fill in the canonical type field.
958 QualType Canonical;
959 if (!T->isCanonical()) {
960 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
961
962 // Get the new insert position for the node we care about.
963 MemberPointerType *NewIP =
964 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
965 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
966 }
Steve Narofff83820b2009-01-27 22:08:43 +0000967 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000968 Types.push_back(New);
969 MemberPointerTypes.InsertNode(New, InsertPos);
970 return QualType(New, 0);
971}
972
Steve Narofffb22d962007-08-30 01:06:46 +0000973/// getConstantArrayType - Return the unique reference to the type for an
974/// array of the specified element type.
975QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000976 const llvm::APInt &ArySize,
977 ArrayType::ArraySizeModifier ASM,
978 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000979 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +0000980 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000981
982 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000983 if (ConstantArrayType *ATP =
984 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 return QualType(ATP, 0);
986
987 // If the element type isn't canonical, this won't be a canonical type either,
988 // so fill in the canonical type field.
989 QualType Canonical;
990 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000991 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000992 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000994 ConstantArrayType *NewIP =
995 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000996 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000997 }
998
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000999 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001000 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001001 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001002 Types.push_back(New);
1003 return QualType(New, 0);
1004}
1005
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001006/// getVariableArrayType - Returns a non-unique reference to the type for a
1007/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001008QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1009 ArrayType::ArraySizeModifier ASM,
1010 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001011 // Since we don't unique expressions, it isn't possible to unique VLA's
1012 // that have an expression provided for their size.
1013
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001014 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001015 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001016
1017 VariableArrayTypes.push_back(New);
1018 Types.push_back(New);
1019 return QualType(New, 0);
1020}
1021
Douglas Gregor898574e2008-12-05 23:32:09 +00001022/// getDependentSizedArrayType - Returns a non-unique reference to
1023/// the type for a dependently-sized array of the specified element
1024/// type. FIXME: We will need these to be uniqued, or at least
1025/// comparable, at some point.
1026QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1027 ArrayType::ArraySizeModifier ASM,
1028 unsigned EltTypeQuals) {
1029 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1030 "Size must be type- or value-dependent!");
1031
1032 // Since we don't unique expressions, it isn't possible to unique
1033 // dependently-sized array types.
1034
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001035 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001036 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1037 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001038
1039 DependentSizedArrayTypes.push_back(New);
1040 Types.push_back(New);
1041 return QualType(New, 0);
1042}
1043
Eli Friedmanc5773c42008-02-15 18:16:39 +00001044QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1045 ArrayType::ArraySizeModifier ASM,
1046 unsigned EltTypeQuals) {
1047 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001048 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001049
1050 void *InsertPos = 0;
1051 if (IncompleteArrayType *ATP =
1052 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1053 return QualType(ATP, 0);
1054
1055 // If the element type isn't canonical, this won't be a canonical type
1056 // either, so fill in the canonical type field.
1057 QualType Canonical;
1058
1059 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001060 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001061 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001062
1063 // Get the new insert position for the node we care about.
1064 IncompleteArrayType *NewIP =
1065 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001066 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001067 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001068
Steve Narofff83820b2009-01-27 22:08:43 +00001069 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001070 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001071
1072 IncompleteArrayTypes.InsertNode(New, InsertPos);
1073 Types.push_back(New);
1074 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001075}
1076
Steve Naroff73322922007-07-18 18:00:27 +00001077/// getVectorType - Return the unique reference to a vector type of
1078/// the specified element type and size. VectorType must be a built-in type.
1079QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001080 BuiltinType *baseType;
1081
Chris Lattnerf52ab252008-04-06 22:59:24 +00001082 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001083 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001084
1085 // Check if we've already instantiated a vector of this type.
1086 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001087 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 void *InsertPos = 0;
1089 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1090 return QualType(VTP, 0);
1091
1092 // If the element type isn't canonical, this won't be a canonical type either,
1093 // so fill in the canonical type field.
1094 QualType Canonical;
1095 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001096 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001097
1098 // Get the new insert position for the node we care about.
1099 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001100 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001101 }
Steve Narofff83820b2009-01-27 22:08:43 +00001102 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001103 VectorTypes.InsertNode(New, InsertPos);
1104 Types.push_back(New);
1105 return QualType(New, 0);
1106}
1107
Nate Begeman213541a2008-04-18 23:10:10 +00001108/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001109/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001110QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001111 BuiltinType *baseType;
1112
Chris Lattnerf52ab252008-04-06 22:59:24 +00001113 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001114 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001115
1116 // Check if we've already instantiated a vector of this type.
1117 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001118 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001119 void *InsertPos = 0;
1120 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1121 return QualType(VTP, 0);
1122
1123 // If the element type isn't canonical, this won't be a canonical type either,
1124 // so fill in the canonical type field.
1125 QualType Canonical;
1126 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001127 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001128
1129 // Get the new insert position for the node we care about.
1130 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001131 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001132 }
Steve Narofff83820b2009-01-27 22:08:43 +00001133 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001134 VectorTypes.InsertNode(New, InsertPos);
1135 Types.push_back(New);
1136 return QualType(New, 0);
1137}
1138
Douglas Gregor72564e72009-02-26 23:50:07 +00001139/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001140///
Douglas Gregor72564e72009-02-26 23:50:07 +00001141QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001142 // Unique functions, to guarantee there is only one function of a particular
1143 // structure.
1144 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001145 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001146
1147 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001148 if (FunctionNoProtoType *FT =
1149 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001150 return QualType(FT, 0);
1151
1152 QualType Canonical;
1153 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001154 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001155
1156 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001157 FunctionNoProtoType *NewIP =
1158 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001159 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001160 }
1161
Douglas Gregor72564e72009-02-26 23:50:07 +00001162 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001163 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001164 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001165 return QualType(New, 0);
1166}
1167
1168/// getFunctionType - Return a normal function type with a typed argument
1169/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001170QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001171 unsigned NumArgs, bool isVariadic,
1172 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001173 // Unique functions, to guarantee there is only one function of a particular
1174 // structure.
1175 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001176 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001177 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001178
1179 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001180 if (FunctionProtoType *FTP =
1181 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001182 return QualType(FTP, 0);
1183
1184 // Determine whether the type being created is already canonical or not.
1185 bool isCanonical = ResultTy->isCanonical();
1186 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1187 if (!ArgArray[i]->isCanonical())
1188 isCanonical = false;
1189
1190 // If this type isn't canonical, get the canonical version of it.
1191 QualType Canonical;
1192 if (!isCanonical) {
1193 llvm::SmallVector<QualType, 16> CanonicalArgs;
1194 CanonicalArgs.reserve(NumArgs);
1195 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001196 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001197
Chris Lattnerf52ab252008-04-06 22:59:24 +00001198 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001199 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001200 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001201
1202 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001203 FunctionProtoType *NewIP =
1204 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001205 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 }
1207
Douglas Gregor72564e72009-02-26 23:50:07 +00001208 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001209 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001210 FunctionProtoType *FTP =
1211 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001212 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001213 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001214 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001216 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001217 return QualType(FTP, 0);
1218}
1219
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001220/// getTypeDeclType - Return the unique reference to the type for the
1221/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001222QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001223 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001224 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1225
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001226 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001227 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001228 else if (isa<TemplateTypeParmDecl>(Decl)) {
1229 assert(false && "Template type parameter types are always available.");
1230 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001231 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001232
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001233 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001234 if (PrevDecl)
1235 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001236 else
1237 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001238 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001239 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1240 if (PrevDecl)
1241 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001242 else
1243 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001244 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001245 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001246 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001247
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001248 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001249 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001250}
1251
Reid Spencer5f016e22007-07-11 17:01:13 +00001252/// getTypedefType - Return the unique reference to the type for the
1253/// specified typename decl.
1254QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1255 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1256
Chris Lattnerf52ab252008-04-06 22:59:24 +00001257 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001258 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001259 Types.push_back(Decl->TypeForDecl);
1260 return QualType(Decl->TypeForDecl, 0);
1261}
1262
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001263/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001264/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001265QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001266 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1267
Steve Narofff83820b2009-01-27 22:08:43 +00001268 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001269 Types.push_back(Decl->TypeForDecl);
1270 return QualType(Decl->TypeForDecl, 0);
1271}
1272
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00001273/// buildObjCInterfaceType - Returns a new type for the interface
1274/// declaration, regardless. It also removes any previously built
1275/// record declaration so caller can rebuild it.
1276QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) {
1277 const RecordDecl *&RD = ASTRecordForInterface[Decl];
1278 if (RD)
1279 RD = 0;
1280 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
1281 Types.push_back(Decl->TypeForDecl);
1282 return QualType(Decl->TypeForDecl, 0);
1283}
1284
Douglas Gregorfab9d672009-02-05 23:33:38 +00001285/// \brief Retrieve the template type parameter type for a template
1286/// parameter with the given depth, index, and (optionally) name.
1287QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1288 IdentifierInfo *Name) {
1289 llvm::FoldingSetNodeID ID;
1290 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1291 void *InsertPos = 0;
1292 TemplateTypeParmType *TypeParm
1293 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1294
1295 if (TypeParm)
1296 return QualType(TypeParm, 0);
1297
1298 if (Name)
1299 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1300 getTemplateTypeParmType(Depth, Index));
1301 else
1302 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1303
1304 Types.push_back(TypeParm);
1305 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1306
1307 return QualType(TypeParm, 0);
1308}
1309
Douglas Gregor55f6b142009-02-09 18:46:07 +00001310QualType
1311ASTContext::getClassTemplateSpecializationType(TemplateDecl *Template,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001312 const TemplateArgument *Args,
Douglas Gregor55f6b142009-02-09 18:46:07 +00001313 unsigned NumArgs,
Douglas Gregor55f6b142009-02-09 18:46:07 +00001314 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001315 if (!Canon.isNull())
1316 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001317
Douglas Gregor55f6b142009-02-09 18:46:07 +00001318 llvm::FoldingSetNodeID ID;
Douglas Gregor40808ce2009-03-09 23:48:35 +00001319 ClassTemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
1320
Douglas Gregor55f6b142009-02-09 18:46:07 +00001321 void *InsertPos = 0;
1322 ClassTemplateSpecializationType *Spec
1323 = ClassTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
1324
1325 if (Spec)
1326 return QualType(Spec, 0);
1327
Douglas Gregor40808ce2009-03-09 23:48:35 +00001328 void *Mem = Allocate((sizeof(ClassTemplateSpecializationType) +
1329 sizeof(TemplateArgument) * NumArgs),
1330 8);
1331 Spec = new (Mem) ClassTemplateSpecializationType(Template, Args, NumArgs,
1332 Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001333 Types.push_back(Spec);
1334 ClassTemplateSpecializationTypes.InsertNode(Spec, InsertPos);
1335
1336 return QualType(Spec, 0);
1337}
1338
Chris Lattner88cb27a2008-04-07 04:56:42 +00001339/// CmpProtocolNames - Comparison predicate for sorting protocols
1340/// alphabetically.
1341static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1342 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001343 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001344}
1345
1346static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1347 unsigned &NumProtocols) {
1348 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1349
1350 // Sort protocols, keyed by name.
1351 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1352
1353 // Remove duplicates.
1354 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1355 NumProtocols = ProtocolsEnd-Protocols;
1356}
1357
1358
Chris Lattner065f0d72008-04-07 04:44:08 +00001359/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1360/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001361QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1362 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001363 // Sort the protocol list alphabetically to canonicalize it.
1364 SortAndUniqueProtocols(Protocols, NumProtocols);
1365
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001366 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001367 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001368
1369 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001370 if (ObjCQualifiedInterfaceType *QT =
1371 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001372 return QualType(QT, 0);
1373
1374 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001375 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001376 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001377
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001378 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001379 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001380 return QualType(QType, 0);
1381}
1382
Chris Lattner88cb27a2008-04-07 04:56:42 +00001383/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1384/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001385QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001386 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001387 // Sort the protocol list alphabetically to canonicalize it.
1388 SortAndUniqueProtocols(Protocols, NumProtocols);
1389
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001390 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001391 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001392
1393 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001394 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001395 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001396 return QualType(QT, 0);
1397
1398 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001399 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001400 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001401 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001402 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001403 return QualType(QType, 0);
1404}
1405
Douglas Gregor72564e72009-02-26 23:50:07 +00001406/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1407/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001408/// multiple declarations that refer to "typeof(x)" all contain different
1409/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1410/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001411QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001412 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001413 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001414 Types.push_back(toe);
1415 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001416}
1417
Steve Naroff9752f252007-08-01 18:02:17 +00001418/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1419/// TypeOfType AST's. The only motivation to unique these nodes would be
1420/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1421/// an issue. This doesn't effect the type checker, since it operates
1422/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001423QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001424 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001425 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001426 Types.push_back(tot);
1427 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001428}
1429
Reid Spencer5f016e22007-07-11 17:01:13 +00001430/// getTagDeclType - Return the unique reference to the type for the
1431/// specified TagDecl (struct/union/class/enum) decl.
1432QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001433 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001434 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001435}
1436
1437/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1438/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1439/// needs to agree with the definition in <stddef.h>.
1440QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001441 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001442}
1443
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001444/// getSignedWCharType - Return the type of "signed wchar_t".
1445/// Used when in C++, as a GCC extension.
1446QualType ASTContext::getSignedWCharType() const {
1447 // FIXME: derive from "Target" ?
1448 return WCharTy;
1449}
1450
1451/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1452/// Used when in C++, as a GCC extension.
1453QualType ASTContext::getUnsignedWCharType() const {
1454 // FIXME: derive from "Target" ?
1455 return UnsignedIntTy;
1456}
1457
Chris Lattner8b9023b2007-07-13 03:05:23 +00001458/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1459/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1460QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001461 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001462}
1463
Chris Lattnere6327742008-04-02 05:18:44 +00001464//===----------------------------------------------------------------------===//
1465// Type Operators
1466//===----------------------------------------------------------------------===//
1467
Chris Lattner77c96472008-04-06 22:41:35 +00001468/// getCanonicalType - Return the canonical (structural) type corresponding to
1469/// the specified potentially non-canonical type. The non-canonical version
1470/// of a type may have many "decorated" versions of types. Decorators can
1471/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1472/// to be free of any of these, allowing two canonical types to be compared
1473/// for exact equality with a simple pointer comparison.
1474QualType ASTContext::getCanonicalType(QualType T) {
1475 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001476
1477 // If the result has type qualifiers, make sure to canonicalize them as well.
1478 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1479 if (TypeQuals == 0) return CanType;
1480
1481 // If the type qualifiers are on an array type, get the canonical type of the
1482 // array with the qualifiers applied to the element type.
1483 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1484 if (!AT)
1485 return CanType.getQualifiedType(TypeQuals);
1486
1487 // Get the canonical version of the element with the extra qualifiers on it.
1488 // This can recursively sink qualifiers through multiple levels of arrays.
1489 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1490 NewEltTy = getCanonicalType(NewEltTy);
1491
1492 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1493 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1494 CAT->getIndexTypeQualifier());
1495 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1496 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1497 IAT->getIndexTypeQualifier());
1498
Douglas Gregor898574e2008-12-05 23:32:09 +00001499 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1500 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1501 DSAT->getSizeModifier(),
1502 DSAT->getIndexTypeQualifier());
1503
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001504 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1505 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1506 VAT->getSizeModifier(),
1507 VAT->getIndexTypeQualifier());
1508}
1509
1510
1511const ArrayType *ASTContext::getAsArrayType(QualType T) {
1512 // Handle the non-qualified case efficiently.
1513 if (T.getCVRQualifiers() == 0) {
1514 // Handle the common positive case fast.
1515 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1516 return AT;
1517 }
1518
1519 // Handle the common negative case fast, ignoring CVR qualifiers.
1520 QualType CType = T->getCanonicalTypeInternal();
1521
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001522 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001523 // test.
1524 if (!isa<ArrayType>(CType) &&
1525 !isa<ArrayType>(CType.getUnqualifiedType()))
1526 return 0;
1527
1528 // Apply any CVR qualifiers from the array type to the element type. This
1529 // implements C99 6.7.3p8: "If the specification of an array type includes
1530 // any type qualifiers, the element type is so qualified, not the array type."
1531
1532 // If we get here, we either have type qualifiers on the type, or we have
1533 // sugar such as a typedef in the way. If we have type qualifiers on the type
1534 // we must propagate them down into the elemeng type.
1535 unsigned CVRQuals = T.getCVRQualifiers();
1536 unsigned AddrSpace = 0;
1537 Type *Ty = T.getTypePtr();
1538
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001539 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001540 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001541 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1542 AddrSpace = EXTQT->getAddressSpace();
1543 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001544 } else {
1545 T = Ty->getDesugaredType();
1546 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1547 break;
1548 CVRQuals |= T.getCVRQualifiers();
1549 Ty = T.getTypePtr();
1550 }
1551 }
1552
1553 // If we have a simple case, just return now.
1554 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1555 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1556 return ATy;
1557
1558 // Otherwise, we have an array and we have qualifiers on it. Push the
1559 // qualifiers into the array element type and return a new array type.
1560 // Get the canonical version of the element with the extra qualifiers on it.
1561 // This can recursively sink qualifiers through multiple levels of arrays.
1562 QualType NewEltTy = ATy->getElementType();
1563 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001564 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001565 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1566
1567 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1568 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1569 CAT->getSizeModifier(),
1570 CAT->getIndexTypeQualifier()));
1571 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1572 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1573 IAT->getSizeModifier(),
1574 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001575
Douglas Gregor898574e2008-12-05 23:32:09 +00001576 if (const DependentSizedArrayType *DSAT
1577 = dyn_cast<DependentSizedArrayType>(ATy))
1578 return cast<ArrayType>(
1579 getDependentSizedArrayType(NewEltTy,
1580 DSAT->getSizeExpr(),
1581 DSAT->getSizeModifier(),
1582 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001583
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001584 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1585 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1586 VAT->getSizeModifier(),
1587 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001588}
1589
1590
Chris Lattnere6327742008-04-02 05:18:44 +00001591/// getArrayDecayedType - Return the properly qualified result of decaying the
1592/// specified array type to a pointer. This operation is non-trivial when
1593/// handling typedefs etc. The canonical type of "T" must be an array type,
1594/// this returns a pointer to a properly qualified element of the array.
1595///
1596/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1597QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001598 // Get the element type with 'getAsArrayType' so that we don't lose any
1599 // typedefs in the element type of the array. This also handles propagation
1600 // of type qualifiers from the array type into the element type if present
1601 // (C99 6.7.3p8).
1602 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1603 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001604
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001605 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001606
1607 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001608 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001609}
1610
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001611QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001612 QualType ElemTy = VAT->getElementType();
1613
1614 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1615 return getBaseElementType(VAT);
1616
1617 return ElemTy;
1618}
1619
Reid Spencer5f016e22007-07-11 17:01:13 +00001620/// getFloatingRank - Return a relative rank for floating point types.
1621/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001622static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001623 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001624 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001625
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001626 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001627 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001628 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001629 case BuiltinType::Float: return FloatRank;
1630 case BuiltinType::Double: return DoubleRank;
1631 case BuiltinType::LongDouble: return LongDoubleRank;
1632 }
1633}
1634
Steve Naroff716c7302007-08-27 01:41:48 +00001635/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1636/// point or a complex type (based on typeDomain/typeSize).
1637/// 'typeDomain' is a real floating point or complex type.
1638/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001639QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1640 QualType Domain) const {
1641 FloatingRank EltRank = getFloatingRank(Size);
1642 if (Domain->isComplexType()) {
1643 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001644 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001645 case FloatRank: return FloatComplexTy;
1646 case DoubleRank: return DoubleComplexTy;
1647 case LongDoubleRank: return LongDoubleComplexTy;
1648 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001649 }
Chris Lattner1361b112008-04-06 23:58:54 +00001650
1651 assert(Domain->isRealFloatingType() && "Unknown domain!");
1652 switch (EltRank) {
1653 default: assert(0 && "getFloatingRank(): illegal value for rank");
1654 case FloatRank: return FloatTy;
1655 case DoubleRank: return DoubleTy;
1656 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001657 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001658}
1659
Chris Lattner7cfeb082008-04-06 23:55:33 +00001660/// getFloatingTypeOrder - Compare the rank of the two specified floating
1661/// point types, ignoring the domain of the type (i.e. 'double' ==
1662/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1663/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001664int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1665 FloatingRank LHSR = getFloatingRank(LHS);
1666 FloatingRank RHSR = getFloatingRank(RHS);
1667
1668 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001669 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001670 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001671 return 1;
1672 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001673}
1674
Chris Lattnerf52ab252008-04-06 22:59:24 +00001675/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1676/// routine will assert if passed a built-in type that isn't an integer or enum,
1677/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001678unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001679 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001680 if (EnumType* ET = dyn_cast<EnumType>(T))
1681 T = ET->getDecl()->getIntegerType().getTypePtr();
1682
1683 // There are two things which impact the integer rank: the width, and
1684 // the ordering of builtins. The builtin ordering is encoded in the
1685 // bottom three bits; the width is encoded in the bits above that.
1686 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1687 return FWIT->getWidth() << 3;
1688 }
1689
Chris Lattnerf52ab252008-04-06 22:59:24 +00001690 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001691 default: assert(0 && "getIntegerRank(): not a built-in integer");
1692 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001693 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001694 case BuiltinType::Char_S:
1695 case BuiltinType::Char_U:
1696 case BuiltinType::SChar:
1697 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001698 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001699 case BuiltinType::Short:
1700 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001701 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001702 case BuiltinType::Int:
1703 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001704 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001705 case BuiltinType::Long:
1706 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001707 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001708 case BuiltinType::LongLong:
1709 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001710 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001711 }
1712}
1713
Chris Lattner7cfeb082008-04-06 23:55:33 +00001714/// getIntegerTypeOrder - Returns the highest ranked integer type:
1715/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1716/// LHS < RHS, return -1.
1717int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001718 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1719 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001720 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001721
Chris Lattnerf52ab252008-04-06 22:59:24 +00001722 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1723 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001724
Chris Lattner7cfeb082008-04-06 23:55:33 +00001725 unsigned LHSRank = getIntegerRank(LHSC);
1726 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001727
Chris Lattner7cfeb082008-04-06 23:55:33 +00001728 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1729 if (LHSRank == RHSRank) return 0;
1730 return LHSRank > RHSRank ? 1 : -1;
1731 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001732
Chris Lattner7cfeb082008-04-06 23:55:33 +00001733 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1734 if (LHSUnsigned) {
1735 // If the unsigned [LHS] type is larger, return it.
1736 if (LHSRank >= RHSRank)
1737 return 1;
1738
1739 // If the signed type can represent all values of the unsigned type, it
1740 // wins. Because we are dealing with 2's complement and types that are
1741 // powers of two larger than each other, this is always safe.
1742 return -1;
1743 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001744
Chris Lattner7cfeb082008-04-06 23:55:33 +00001745 // If the unsigned [RHS] type is larger, return it.
1746 if (RHSRank >= LHSRank)
1747 return -1;
1748
1749 // If the signed type can represent all values of the unsigned type, it
1750 // wins. Because we are dealing with 2's complement and types that are
1751 // powers of two larger than each other, this is always safe.
1752 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001753}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001754
1755// getCFConstantStringType - Return the type used for constant CFStrings.
1756QualType ASTContext::getCFConstantStringType() {
1757 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001758 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001759 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001760 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001761 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001762
1763 // const int *isa;
1764 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001765 // int flags;
1766 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001767 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001768 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001769 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001770 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001771
Anders Carlsson71993dd2007-08-17 05:31:46 +00001772 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001773 for (unsigned i = 0; i < 4; ++i) {
1774 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1775 SourceLocation(), 0,
1776 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001777 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001778 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001779 }
1780
1781 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001782 }
1783
1784 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001785}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001786
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001787QualType ASTContext::getObjCFastEnumerationStateType()
1788{
1789 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001790 ObjCFastEnumerationStateTypeDecl =
1791 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1792 &Idents.get("__objcFastEnumerationState"));
1793
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001794 QualType FieldTypes[] = {
1795 UnsignedLongTy,
1796 getPointerType(ObjCIdType),
1797 getPointerType(UnsignedLongTy),
1798 getConstantArrayType(UnsignedLongTy,
1799 llvm::APInt(32, 5), ArrayType::Normal, 0)
1800 };
1801
Douglas Gregor44b43212008-12-11 16:49:14 +00001802 for (size_t i = 0; i < 4; ++i) {
1803 FieldDecl *Field = FieldDecl::Create(*this,
1804 ObjCFastEnumerationStateTypeDecl,
1805 SourceLocation(), 0,
1806 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001807 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001808 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001809 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001810
Douglas Gregor44b43212008-12-11 16:49:14 +00001811 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001812 }
1813
1814 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1815}
1816
Anders Carlssone8c49532007-10-29 06:33:42 +00001817// This returns true if a type has been typedefed to BOOL:
1818// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001819static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001820 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00001821 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1822 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001823
1824 return false;
1825}
1826
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001827/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001828/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001829int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001830 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001831
1832 // Make all integer and enum types at least as large as an int
1833 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001834 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001835 // Treat arrays as pointers, since that's how they're passed in.
1836 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001837 sz = getTypeSize(VoidPtrTy);
1838 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001839}
1840
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001841/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001842/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001843void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001844 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001845 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001846 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001847 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001848 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001849 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001850 // Compute size of all parameters.
1851 // Start with computing size of a pointer in number of bytes.
1852 // FIXME: There might(should) be a better way of doing this computation!
1853 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001854 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001855 // The first two arguments (self and _cmd) are pointers; account for
1856 // their size.
1857 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00001858 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1859 E = Decl->param_end(); PI != E; ++PI) {
1860 QualType PType = (*PI)->getType();
1861 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001862 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001863 ParmOffset += sz;
1864 }
1865 S += llvm::utostr(ParmOffset);
1866 S += "@0:";
1867 S += llvm::utostr(PtrSize);
1868
1869 // Argument types.
1870 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00001871 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1872 E = Decl->param_end(); PI != E; ++PI) {
1873 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001874 QualType PType = PVDecl->getOriginalType();
1875 if (const ArrayType *AT =
1876 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1877 // Use array's original type only if it has known number of
1878 // elements.
1879 if (!dyn_cast<ConstantArrayType>(AT))
1880 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001881 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001882 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001883 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001884 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001885 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001886 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001887 }
1888}
1889
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001890/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001891/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001892/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1893/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001894/// Property attributes are stored as a comma-delimited C string. The simple
1895/// attributes readonly and bycopy are encoded as single characters. The
1896/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1897/// encoded as single characters, followed by an identifier. Property types
1898/// are also encoded as a parametrized attribute. The characters used to encode
1899/// these attributes are defined by the following enumeration:
1900/// @code
1901/// enum PropertyAttributes {
1902/// kPropertyReadOnly = 'R', // property is read-only.
1903/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1904/// kPropertyByref = '&', // property is a reference to the value last assigned
1905/// kPropertyDynamic = 'D', // property is dynamic
1906/// kPropertyGetter = 'G', // followed by getter selector name
1907/// kPropertySetter = 'S', // followed by setter selector name
1908/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1909/// kPropertyType = 't' // followed by old-style type encoding.
1910/// kPropertyWeak = 'W' // 'weak' property
1911/// kPropertyStrong = 'P' // property GC'able
1912/// kPropertyNonAtomic = 'N' // property non-atomic
1913/// };
1914/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001915void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1916 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001917 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001918 // Collect information from the property implementation decl(s).
1919 bool Dynamic = false;
1920 ObjCPropertyImplDecl *SynthesizePID = 0;
1921
1922 // FIXME: Duplicated code due to poor abstraction.
1923 if (Container) {
1924 if (const ObjCCategoryImplDecl *CID =
1925 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1926 for (ObjCCategoryImplDecl::propimpl_iterator
1927 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1928 ObjCPropertyImplDecl *PID = *i;
1929 if (PID->getPropertyDecl() == PD) {
1930 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1931 Dynamic = true;
1932 } else {
1933 SynthesizePID = PID;
1934 }
1935 }
1936 }
1937 } else {
Chris Lattner61710852008-10-05 17:34:18 +00001938 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001939 for (ObjCCategoryImplDecl::propimpl_iterator
1940 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1941 ObjCPropertyImplDecl *PID = *i;
1942 if (PID->getPropertyDecl() == PD) {
1943 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1944 Dynamic = true;
1945 } else {
1946 SynthesizePID = PID;
1947 }
1948 }
1949 }
1950 }
1951 }
1952
1953 // FIXME: This is not very efficient.
1954 S = "T";
1955
1956 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001957 // GCC has some special rules regarding encoding of properties which
1958 // closely resembles encoding of ivars.
1959 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
1960 true /* outermost type */,
1961 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001962
1963 if (PD->isReadOnly()) {
1964 S += ",R";
1965 } else {
1966 switch (PD->getSetterKind()) {
1967 case ObjCPropertyDecl::Assign: break;
1968 case ObjCPropertyDecl::Copy: S += ",C"; break;
1969 case ObjCPropertyDecl::Retain: S += ",&"; break;
1970 }
1971 }
1972
1973 // It really isn't clear at all what this means, since properties
1974 // are "dynamic by default".
1975 if (Dynamic)
1976 S += ",D";
1977
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001978 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
1979 S += ",N";
1980
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001981 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1982 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001983 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001984 }
1985
1986 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1987 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001988 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001989 }
1990
1991 if (SynthesizePID) {
1992 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1993 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00001994 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001995 }
1996
1997 // FIXME: OBJCGC: weak & strong
1998}
1999
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002000/// getLegacyIntegralTypeEncoding -
2001/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002002/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002003/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2004///
2005void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2006 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2007 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002008 if (BT->getKind() == BuiltinType::ULong &&
2009 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002010 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002011 else
2012 if (BT->getKind() == BuiltinType::Long &&
2013 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002014 PointeeTy = IntTy;
2015 }
2016 }
2017}
2018
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002019void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002020 FieldDecl *Field) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002021 // We follow the behavior of gcc, expanding structures which are
2022 // directly pointed to, and expanding embedded structures. Note that
2023 // these rules are sufficient to prevent recursive encoding of the
2024 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002025 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2026 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002027}
2028
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002029static void EncodeBitField(const ASTContext *Context, std::string& S,
2030 FieldDecl *FD) {
2031 const Expr *E = FD->getBitWidth();
2032 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2033 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2034 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2035 S += 'b';
2036 S += llvm::utostr(N);
2037}
2038
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002039void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2040 bool ExpandPointedToStructures,
2041 bool ExpandStructures,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002042 FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002043 bool OutermostType,
2044 bool EncodingProperty) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00002045 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002046 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002047 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002048 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002049 else {
2050 char encoding;
2051 switch (BT->getKind()) {
2052 default: assert(0 && "Unhandled builtin type kind");
2053 case BuiltinType::Void: encoding = 'v'; break;
2054 case BuiltinType::Bool: encoding = 'B'; break;
2055 case BuiltinType::Char_U:
2056 case BuiltinType::UChar: encoding = 'C'; break;
2057 case BuiltinType::UShort: encoding = 'S'; break;
2058 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002059 case BuiltinType::ULong:
2060 encoding =
2061 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2062 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002063 case BuiltinType::ULongLong: encoding = 'Q'; break;
2064 case BuiltinType::Char_S:
2065 case BuiltinType::SChar: encoding = 'c'; break;
2066 case BuiltinType::Short: encoding = 's'; break;
2067 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002068 case BuiltinType::Long:
2069 encoding =
2070 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2071 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002072 case BuiltinType::LongLong: encoding = 'q'; break;
2073 case BuiltinType::Float: encoding = 'f'; break;
2074 case BuiltinType::Double: encoding = 'd'; break;
2075 case BuiltinType::LongDouble: encoding = 'd'; break;
2076 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002077
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002078 S += encoding;
2079 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002080 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002081 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002082 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2083 ExpandPointedToStructures,
2084 ExpandStructures, FD);
2085 if (FD || EncodingProperty) {
2086 // Note that we do extended encoding of protocol qualifer list
2087 // Only when doing ivar or property encoding.
2088 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2089 S += '"';
2090 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2091 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2092 S += '<';
2093 S += Proto->getNameAsString();
2094 S += '>';
2095 }
2096 S += '"';
2097 }
2098 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002099 }
2100 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002101 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002102 bool isReadOnly = false;
2103 // For historical/compatibility reasons, the read-only qualifier of the
2104 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2105 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2106 // Also, do not emit the 'r' for anything but the outermost type!
2107 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2108 if (OutermostType && T.isConstQualified()) {
2109 isReadOnly = true;
2110 S += 'r';
2111 }
2112 }
2113 else if (OutermostType) {
2114 QualType P = PointeeTy;
2115 while (P->getAsPointerType())
2116 P = P->getAsPointerType()->getPointeeType();
2117 if (P.isConstQualified()) {
2118 isReadOnly = true;
2119 S += 'r';
2120 }
2121 }
2122 if (isReadOnly) {
2123 // Another legacy compatibility encoding. Some ObjC qualifier and type
2124 // combinations need to be rearranged.
2125 // Rewrite "in const" from "nr" to "rn"
2126 const char * s = S.c_str();
2127 int len = S.length();
2128 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2129 std::string replace = "rn";
2130 S.replace(S.end()-2, S.end(), replace);
2131 }
2132 }
Steve Naroff389bf462009-02-12 17:52:19 +00002133 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002134 S += '@';
2135 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002136 }
2137 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002138 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002139 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002140 // Another historical/compatibility reason.
2141 // We encode the underlying type which comes out as
2142 // {...};
2143 S += '^';
2144 getObjCEncodingForTypeImpl(PointeeTy, S,
2145 false, ExpandPointedToStructures,
2146 NULL);
2147 return;
2148 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002149 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002150 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002151 const ObjCInterfaceType *OIT =
2152 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002153 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002154 S += '"';
2155 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002156 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2157 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2158 S += '<';
2159 S += Proto->getNameAsString();
2160 S += '>';
2161 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002162 S += '"';
2163 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002164 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002165 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002166 S += '#';
2167 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002168 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002169 S += ':';
2170 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002171 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002172
2173 if (PointeeTy->isCharType()) {
2174 // char pointer types should be encoded as '*' unless it is a
2175 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002176 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002177 S += '*';
2178 return;
2179 }
2180 }
2181
2182 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002183 getLegacyIntegralTypeEncoding(PointeeTy);
2184
2185 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002186 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002187 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002188 } else if (const ArrayType *AT =
2189 // Ignore type qualifiers etc.
2190 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002191 if (isa<IncompleteArrayType>(AT)) {
2192 // Incomplete arrays are encoded as a pointer to the array element.
2193 S += '^';
2194
2195 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2196 false, ExpandStructures, FD);
2197 } else {
2198 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002199
Anders Carlsson559a8332009-02-22 01:38:57 +00002200 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2201 S += llvm::utostr(CAT->getSize().getZExtValue());
2202 else {
2203 //Variable length arrays are encoded as a regular array with 0 elements.
2204 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2205 S += '0';
2206 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002207
Anders Carlsson559a8332009-02-22 01:38:57 +00002208 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2209 false, ExpandStructures, FD);
2210 S += ']';
2211 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002212 } else if (T->getAsFunctionType()) {
2213 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002214 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002215 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002216 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002217 // Anonymous structures print as '?'
2218 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2219 S += II->getName();
2220 } else {
2221 S += '?';
2222 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002223 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002224 S += '=';
Douglas Gregor44b43212008-12-11 16:49:14 +00002225 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2226 FieldEnd = RDecl->field_end();
2227 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002228 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002229 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002230 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002231 S += '"';
2232 }
2233
2234 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002235 if (Field->isBitField()) {
2236 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2237 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002238 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002239 QualType qt = Field->getType();
2240 getLegacyIntegralTypeEncoding(qt);
2241 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002242 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002243 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002244 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002245 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002246 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002247 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002248 if (FD && FD->isBitField())
2249 EncodeBitField(this, S, FD);
2250 else
2251 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002252 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002253 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002254 } else if (T->isObjCInterfaceType()) {
2255 // @encode(class_name)
2256 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2257 S += '{';
2258 const IdentifierInfo *II = OI->getIdentifier();
2259 S += II->getName();
2260 S += '=';
2261 std::vector<FieldDecl*> RecFields;
2262 CollectObjCIvars(OI, RecFields);
2263 for (unsigned int i = 0; i != RecFields.size(); i++) {
2264 if (RecFields[i]->isBitField())
2265 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2266 RecFields[i]);
2267 else
2268 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2269 FD);
2270 }
2271 S += '}';
2272 }
2273 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002274 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002275}
2276
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002277void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002278 std::string& S) const {
2279 if (QT & Decl::OBJC_TQ_In)
2280 S += 'n';
2281 if (QT & Decl::OBJC_TQ_Inout)
2282 S += 'N';
2283 if (QT & Decl::OBJC_TQ_Out)
2284 S += 'o';
2285 if (QT & Decl::OBJC_TQ_Bycopy)
2286 S += 'O';
2287 if (QT & Decl::OBJC_TQ_Byref)
2288 S += 'R';
2289 if (QT & Decl::OBJC_TQ_Oneway)
2290 S += 'V';
2291}
2292
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002293void ASTContext::setBuiltinVaListType(QualType T)
2294{
2295 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2296
2297 BuiltinVaListType = T;
2298}
2299
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002300void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002301{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002302 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002303
2304 // typedef struct objc_object *id;
2305 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002306 // User error - caller will issue diagnostics.
2307 if (!ptr)
2308 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002309 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002310 // User error - caller will issue diagnostics.
2311 if (!rec)
2312 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002313 IdStructType = rec;
2314}
2315
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002316void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002317{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002318 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002319
2320 // typedef struct objc_selector *SEL;
2321 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002322 if (!ptr)
2323 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002324 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002325 if (!rec)
2326 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002327 SelStructType = rec;
2328}
2329
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002330void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002331{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002332 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002333}
2334
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002335void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002336{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002337 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002338
2339 // typedef struct objc_class *Class;
2340 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2341 assert(ptr && "'Class' incorrectly typed");
2342 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2343 assert(rec && "'Class' incorrectly typed");
2344 ClassStructType = rec;
2345}
2346
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002347void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2348 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002349 "'NSConstantString' type already set!");
2350
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002351 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002352}
2353
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002354/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002355/// TargetInfo, produce the corresponding type. The unsigned @p Type
2356/// is actually a value of type @c TargetInfo::IntType.
2357QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002358 switch (Type) {
2359 case TargetInfo::NoInt: return QualType();
2360 case TargetInfo::SignedShort: return ShortTy;
2361 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2362 case TargetInfo::SignedInt: return IntTy;
2363 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2364 case TargetInfo::SignedLong: return LongTy;
2365 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2366 case TargetInfo::SignedLongLong: return LongLongTy;
2367 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2368 }
2369
2370 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002371 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002372}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002373
2374//===----------------------------------------------------------------------===//
2375// Type Predicates.
2376//===----------------------------------------------------------------------===//
2377
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002378/// isObjCNSObjectType - Return true if this is an NSObject object using
2379/// NSObject attribute on a c-style pointer type.
2380/// FIXME - Make it work directly on types.
2381///
2382bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2383 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2384 if (TypedefDecl *TD = TDT->getDecl())
2385 if (TD->getAttr<ObjCNSObjectAttr>())
2386 return true;
2387 }
2388 return false;
2389}
2390
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002391/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2392/// to an object type. This includes "id" and "Class" (two 'special' pointers
2393/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2394/// ID type).
2395bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002396 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002397 return true;
2398
Steve Naroff6ae98502008-10-21 18:24:04 +00002399 // Blocks are objects.
2400 if (Ty->isBlockPointerType())
2401 return true;
2402
2403 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002404 if (!Ty->isPointerType())
2405 return false;
2406
2407 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2408 // pointer types. This looks for the typedef specifically, not for the
2409 // underlying type.
2410 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2411 return true;
2412
2413 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002414 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2415 return true;
2416
2417 // If is has NSObject attribute, OK as well.
2418 return isObjCNSObjectType(Ty);
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002419}
2420
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002421/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2422/// garbage collection attribute.
2423///
2424QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002425 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002426 if (getLangOptions().ObjC1 &&
2427 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002428 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002429 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002430 // (or pointers to them) be treated as though they were declared
2431 // as __strong.
2432 if (GCAttrs == QualType::GCNone) {
2433 if (isObjCObjectPointerType(Ty))
2434 GCAttrs = QualType::Strong;
2435 else if (Ty->isPointerType())
2436 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2437 }
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002438 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002439 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002440}
2441
Chris Lattner6ac46a42008-04-07 06:51:04 +00002442//===----------------------------------------------------------------------===//
2443// Type Compatibility Testing
2444//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002445
Steve Naroff1c7d0672008-09-04 15:10:53 +00002446/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002447/// block types. Types must be strictly compatible here. For example,
2448/// C unfortunately doesn't produce an error for the following:
2449///
2450/// int (*emptyArgFunc)();
2451/// int (*intArgList)(int) = emptyArgFunc;
2452///
2453/// For blocks, we will produce an error for the following (similar to C++):
2454///
2455/// int (^emptyArgBlock)();
2456/// int (^intArgBlock)(int) = emptyArgBlock;
2457///
2458/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2459///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002460bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002461 const FunctionType *lbase = lhs->getAsFunctionType();
2462 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002463 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2464 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Steve Naroffc0febd52008-12-10 17:49:55 +00002465 if (lproto && rproto)
2466 return !mergeTypes(lhs, rhs).isNull();
2467 return false;
Steve Naroff1c7d0672008-09-04 15:10:53 +00002468}
2469
Chris Lattner6ac46a42008-04-07 06:51:04 +00002470/// areCompatVectorTypes - Return true if the two specified vector types are
2471/// compatible.
2472static bool areCompatVectorTypes(const VectorType *LHS,
2473 const VectorType *RHS) {
2474 assert(LHS->isCanonical() && RHS->isCanonical());
2475 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002476 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002477}
2478
Eli Friedman3d815e72008-08-22 00:56:42 +00002479/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002480/// compatible for assignment from RHS to LHS. This handles validation of any
2481/// protocol qualifiers on the LHS or RHS.
2482///
Eli Friedman3d815e72008-08-22 00:56:42 +00002483bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2484 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002485 // Verify that the base decls are compatible: the RHS must be a subclass of
2486 // the LHS.
2487 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2488 return false;
2489
2490 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2491 // protocol qualified at all, then we are good.
2492 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2493 return true;
2494
2495 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2496 // isn't a superset.
2497 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2498 return true; // FIXME: should return false!
2499
2500 // Finally, we must have two protocol-qualified interfaces.
2501 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2502 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002503
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002504 // All LHS protocols must have a presence on the RHS.
2505 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002506
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002507 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2508 LHSPE = LHSP->qual_end();
2509 LHSPI != LHSPE; LHSPI++) {
2510 bool RHSImplementsProtocol = false;
2511
2512 // If the RHS doesn't implement the protocol on the left, the types
2513 // are incompatible.
2514 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2515 RHSPE = RHSP->qual_end();
2516 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2517 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2518 RHSImplementsProtocol = true;
2519 }
2520 // FIXME: For better diagnostics, consider passing back the protocol name.
2521 if (!RHSImplementsProtocol)
2522 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002523 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002524 // The RHS implements all protocols listed on the LHS.
2525 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002526}
2527
Steve Naroff389bf462009-02-12 17:52:19 +00002528bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2529 // get the "pointed to" types
2530 const PointerType *LHSPT = LHS->getAsPointerType();
2531 const PointerType *RHSPT = RHS->getAsPointerType();
2532
2533 if (!LHSPT || !RHSPT)
2534 return false;
2535
2536 QualType lhptee = LHSPT->getPointeeType();
2537 QualType rhptee = RHSPT->getPointeeType();
2538 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2539 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2540 // ID acts sort of like void* for ObjC interfaces
2541 if (LHSIface && isObjCIdStructType(rhptee))
2542 return true;
2543 if (RHSIface && isObjCIdStructType(lhptee))
2544 return true;
2545 if (!LHSIface || !RHSIface)
2546 return false;
2547 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2548 canAssignObjCInterfaces(RHSIface, LHSIface);
2549}
2550
Steve Naroffec0550f2007-10-15 20:41:53 +00002551/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2552/// both shall have the identically qualified version of a compatible type.
2553/// C99 6.2.7p1: Two types have compatible types if their types are the
2554/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002555bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2556 return !mergeTypes(LHS, RHS).isNull();
2557}
2558
2559QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2560 const FunctionType *lbase = lhs->getAsFunctionType();
2561 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002562 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2563 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002564 bool allLTypes = true;
2565 bool allRTypes = true;
2566
2567 // Check return type
2568 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2569 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002570 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2571 allLTypes = false;
2572 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2573 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002574
2575 if (lproto && rproto) { // two C99 style function prototypes
2576 unsigned lproto_nargs = lproto->getNumArgs();
2577 unsigned rproto_nargs = rproto->getNumArgs();
2578
2579 // Compatible functions must have the same number of arguments
2580 if (lproto_nargs != rproto_nargs)
2581 return QualType();
2582
2583 // Variadic and non-variadic functions aren't compatible
2584 if (lproto->isVariadic() != rproto->isVariadic())
2585 return QualType();
2586
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002587 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2588 return QualType();
2589
Eli Friedman3d815e72008-08-22 00:56:42 +00002590 // Check argument compatibility
2591 llvm::SmallVector<QualType, 10> types;
2592 for (unsigned i = 0; i < lproto_nargs; i++) {
2593 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2594 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2595 QualType argtype = mergeTypes(largtype, rargtype);
2596 if (argtype.isNull()) return QualType();
2597 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002598 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2599 allLTypes = false;
2600 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2601 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002602 }
2603 if (allLTypes) return lhs;
2604 if (allRTypes) return rhs;
2605 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002606 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002607 }
2608
2609 if (lproto) allRTypes = false;
2610 if (rproto) allLTypes = false;
2611
Douglas Gregor72564e72009-02-26 23:50:07 +00002612 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002613 if (proto) {
2614 if (proto->isVariadic()) return QualType();
2615 // Check that the types are compatible with the types that
2616 // would result from default argument promotions (C99 6.7.5.3p15).
2617 // The only types actually affected are promotable integer
2618 // types and floats, which would be passed as a different
2619 // type depending on whether the prototype is visible.
2620 unsigned proto_nargs = proto->getNumArgs();
2621 for (unsigned i = 0; i < proto_nargs; ++i) {
2622 QualType argTy = proto->getArgType(i);
2623 if (argTy->isPromotableIntegerType() ||
2624 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2625 return QualType();
2626 }
2627
2628 if (allLTypes) return lhs;
2629 if (allRTypes) return rhs;
2630 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002631 proto->getNumArgs(), lproto->isVariadic(),
2632 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002633 }
2634
2635 if (allLTypes) return lhs;
2636 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002637 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002638}
2639
2640QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002641 // C++ [expr]: If an expression initially has the type "reference to T", the
2642 // type is adjusted to "T" prior to any further analysis, the expression
2643 // designates the object or function denoted by the reference, and the
2644 // expression is an lvalue.
Eli Friedman3d815e72008-08-22 00:56:42 +00002645 // FIXME: C++ shouldn't be going through here! The rules are different
2646 // enough that they should be handled separately.
2647 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002648 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002649 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002650 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002651
Eli Friedman3d815e72008-08-22 00:56:42 +00002652 QualType LHSCan = getCanonicalType(LHS),
2653 RHSCan = getCanonicalType(RHS);
2654
2655 // If two types are identical, they are compatible.
2656 if (LHSCan == RHSCan)
2657 return LHS;
2658
2659 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002660 // Note that we handle extended qualifiers later, in the
2661 // case for ExtQualType.
2662 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00002663 return QualType();
2664
2665 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2666 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2667
Chris Lattner1adb8832008-01-14 05:45:46 +00002668 // We want to consider the two function types to be the same for these
2669 // comparisons, just force one to the other.
2670 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2671 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002672
2673 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002674 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2675 LHSClass = Type::ConstantArray;
2676 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2677 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002678
Nate Begeman213541a2008-04-18 23:10:10 +00002679 // Canonicalize ExtVector -> Vector.
2680 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2681 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002682
Chris Lattnerb0489812008-04-07 06:38:24 +00002683 // Consider qualified interfaces and interfaces the same.
2684 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2685 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002686
Chris Lattnera36a61f2008-04-07 05:43:21 +00002687 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002688 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002689 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2690 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2691
2692 // ID acts sort of like void* for ObjC interfaces
2693 if (LHSIface && isObjCIdStructType(RHS))
2694 return LHS;
2695 if (RHSIface && isObjCIdStructType(LHS))
2696 return RHS;
2697
Steve Naroffbc76dd02008-12-10 22:14:21 +00002698 // ID is compatible with all qualified id types.
2699 if (LHS->isObjCQualifiedIdType()) {
2700 if (const PointerType *PT = RHS->getAsPointerType()) {
2701 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002702 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002703 return LHS;
2704 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2705 // Unfortunately, this API is part of Sema (which we don't have access
2706 // to. Need to refactor. The following check is insufficient, since we
2707 // need to make sure the class implements the protocol.
2708 if (pType->isObjCInterfaceType())
2709 return LHS;
2710 }
2711 }
2712 if (RHS->isObjCQualifiedIdType()) {
2713 if (const PointerType *PT = LHS->getAsPointerType()) {
2714 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002715 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002716 return RHS;
2717 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2718 // Unfortunately, this API is part of Sema (which we don't have access
2719 // to. Need to refactor. The following check is insufficient, since we
2720 // need to make sure the class implements the protocol.
2721 if (pType->isObjCInterfaceType())
2722 return RHS;
2723 }
2724 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002725 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2726 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002727 if (const EnumType* ETy = LHS->getAsEnumType()) {
2728 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2729 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002730 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002731 if (const EnumType* ETy = RHS->getAsEnumType()) {
2732 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2733 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002734 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002735
Eli Friedman3d815e72008-08-22 00:56:42 +00002736 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002737 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002738
Steve Naroff4a746782008-01-09 22:43:08 +00002739 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002740 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00002741#define TYPE(Class, Base)
2742#define ABSTRACT_TYPE(Class, Base)
2743#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2744#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2745#include "clang/AST/TypeNodes.def"
2746 assert(false && "Non-canonical and dependent types shouldn't get here");
2747 return QualType();
2748
2749 case Type::Reference:
2750 case Type::MemberPointer:
2751 assert(false && "C++ should never be in mergeTypes");
2752 return QualType();
2753
2754 case Type::IncompleteArray:
2755 case Type::VariableArray:
2756 case Type::FunctionProto:
2757 case Type::ExtVector:
2758 case Type::ObjCQualifiedInterface:
2759 assert(false && "Types are eliminated above");
2760 return QualType();
2761
Chris Lattner1adb8832008-01-14 05:45:46 +00002762 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002763 {
2764 // Merge two pointer types, while trying to preserve typedef info
2765 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2766 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2767 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2768 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002769 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2770 return LHS;
2771 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2772 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002773 return getPointerType(ResultType);
2774 }
Steve Naroffc0febd52008-12-10 17:49:55 +00002775 case Type::BlockPointer:
2776 {
2777 // Merge two block pointer types, while trying to preserve typedef info
2778 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2779 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2780 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2781 if (ResultType.isNull()) return QualType();
2782 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2783 return LHS;
2784 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2785 return RHS;
2786 return getBlockPointerType(ResultType);
2787 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002788 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002789 {
2790 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2791 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2792 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2793 return QualType();
2794
2795 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2796 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2797 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2798 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002799 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2800 return LHS;
2801 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2802 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002803 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2804 ArrayType::ArraySizeModifier(), 0);
2805 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2806 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002807 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2808 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00002809 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2810 return LHS;
2811 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2812 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002813 if (LVAT) {
2814 // FIXME: This isn't correct! But tricky to implement because
2815 // the array's size has to be the size of LHS, but the type
2816 // has to be different.
2817 return LHS;
2818 }
2819 if (RVAT) {
2820 // FIXME: This isn't correct! But tricky to implement because
2821 // the array's size has to be the size of RHS, but the type
2822 // has to be different.
2823 return RHS;
2824 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002825 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2826 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00002827 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002828 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002829 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002830 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00002831 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00002832 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00002833 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00002834 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
2835 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002836 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002837 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002838 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002839 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00002840 case Type::Complex:
2841 // Distinct complex types are incompatible.
2842 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002843 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002844 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00002845 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2846 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00002847 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00002848 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002849 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002850 // FIXME: This should be type compatibility, e.g. whether
2851 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00002852 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2853 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2854 if (LHSIface && RHSIface &&
2855 canAssignObjCInterfaces(LHSIface, RHSIface))
2856 return LHS;
2857
Eli Friedman3d815e72008-08-22 00:56:42 +00002858 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00002859 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00002860 case Type::ObjCQualifiedId:
2861 // Distinct qualified id's are not compatible.
2862 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002863 case Type::FixedWidthInt:
2864 // Distinct fixed-width integers are not compatible.
2865 return QualType();
2866 case Type::ObjCQualifiedClass:
2867 // Distinct qualified classes are not compatible.
2868 return QualType();
2869 case Type::ExtQual:
2870 // FIXME: ExtQual types can be compatible even if they're not
2871 // identical!
2872 return QualType();
2873 // First attempt at an implementation, but I'm not really sure it's
2874 // right...
2875#if 0
2876 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
2877 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
2878 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
2879 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
2880 return QualType();
2881 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
2882 LHSBase = QualType(LQual->getBaseType(), 0);
2883 RHSBase = QualType(RQual->getBaseType(), 0);
2884 ResultType = mergeTypes(LHSBase, RHSBase);
2885 if (ResultType.isNull()) return QualType();
2886 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
2887 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
2888 return LHS;
2889 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
2890 return RHS;
2891 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
2892 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
2893 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
2894 return ResultType;
2895#endif
Steve Naroffec0550f2007-10-15 20:41:53 +00002896 }
Douglas Gregor72564e72009-02-26 23:50:07 +00002897
2898 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002899}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002900
Chris Lattner5426bf62008-04-07 07:01:58 +00002901//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00002902// Integer Predicates
2903//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00002904
Eli Friedmanad74a752008-06-28 06:23:08 +00002905unsigned ASTContext::getIntWidth(QualType T) {
2906 if (T == BoolTy)
2907 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002908 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
2909 return FWIT->getWidth();
2910 }
2911 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00002912 return (unsigned)getTypeSize(T);
2913}
2914
2915QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2916 assert(T->isSignedIntegerType() && "Unexpected type");
2917 if (const EnumType* ETy = T->getAsEnumType())
2918 T = ETy->getDecl()->getIntegerType();
2919 const BuiltinType* BTy = T->getAsBuiltinType();
2920 assert (BTy && "Unexpected signed integer type");
2921 switch (BTy->getKind()) {
2922 case BuiltinType::Char_S:
2923 case BuiltinType::SChar:
2924 return UnsignedCharTy;
2925 case BuiltinType::Short:
2926 return UnsignedShortTy;
2927 case BuiltinType::Int:
2928 return UnsignedIntTy;
2929 case BuiltinType::Long:
2930 return UnsignedLongTy;
2931 case BuiltinType::LongLong:
2932 return UnsignedLongLongTy;
2933 default:
2934 assert(0 && "Unexpected signed integer type");
2935 return QualType();
2936 }
2937}
2938
2939
2940//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00002941// Serialization Support
2942//===----------------------------------------------------------------------===//
2943
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002944/// Emit - Serialize an ASTContext object to Bitcode.
2945void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002946 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00002947 S.EmitRef(SourceMgr);
2948 S.EmitRef(Target);
2949 S.EmitRef(Idents);
2950 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002951
Ted Kremenekfee04522007-10-31 22:44:07 +00002952 // Emit the size of the type vector so that we can reserve that size
2953 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00002954 S.EmitInt(Types.size());
2955
Ted Kremenek03ed4402007-11-13 22:02:55 +00002956 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2957 I!=E;++I)
2958 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00002959
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002960 S.EmitOwnedPtr(TUDecl);
2961
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002962 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002963}
2964
Ted Kremenek0f84c002007-11-13 00:25:37 +00002965ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002966
2967 // Read the language options.
2968 LangOptions LOpts;
2969 LOpts.Read(D);
2970
Ted Kremenekfee04522007-10-31 22:44:07 +00002971 SourceManager &SM = D.ReadRef<SourceManager>();
2972 TargetInfo &t = D.ReadRef<TargetInfo>();
2973 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2974 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00002975
Ted Kremenekfee04522007-10-31 22:44:07 +00002976 unsigned size_reserve = D.ReadInt();
2977
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002978 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2979 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00002980
Ted Kremenek03ed4402007-11-13 22:02:55 +00002981 for (unsigned i = 0; i < size_reserve; ++i)
2982 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00002983
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002984 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2985
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002986 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00002987
2988 return A;
2989}