blob: 9f8630f58fc8db5a27f6b9ee2c07c07fe903e1e5 [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"
Chris Lattnera9376d42009-03-28 03:45:20 +000020#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000022#include "llvm/ADT/StringExtras.h"
Ted Kremenek7192f8e2007-10-31 17:10:13 +000023#include "llvm/Bitcode/Serialize.h"
24#include "llvm/Bitcode/Deserialize.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000025#include "llvm/Support/MathExtras.h"
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) :
Douglas Gregorab452ba2009-03-26 23:50:42 +000036 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
37 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Chris Lattnered0e4972009-03-28 01:44:40 +000038 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels) {
Daniel Dunbare91593e2008-08-11 04:54:23 +000039 if (size_reserve > 0) Types.reserve(size_reserve);
40 InitBuiltinTypes();
Chris Lattner7644f072009-03-13 22:38:49 +000041 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.NoBuiltin);
Daniel Dunbare91593e2008-08-11 04:54:23 +000042 TUDecl = TranslationUnitDecl::Create(*this);
43}
44
Reid Spencer5f016e22007-07-11 17:01:13 +000045ASTContext::~ASTContext() {
46 // Deallocate all the types.
47 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000048 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000049 Types.pop_back();
50 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000051
Nuno Lopesb74668e2008-12-17 22:30:25 +000052 {
53 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
54 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
55 while (I != E) {
56 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
57 delete R;
58 }
59 }
60
61 {
62 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
63 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
64 while (I != E) {
65 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
66 delete R;
67 }
68 }
69
70 {
71 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator
72 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
73 while (I != E) {
74 RecordDecl *R = const_cast<RecordDecl*>((I++)->second);
75 R->Destroy(*this);
76 }
77 }
78
Douglas Gregorab452ba2009-03-26 23:50:42 +000079 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000080 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
81 NNS = NestedNameSpecifiers.begin(),
82 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregore7dcd782009-03-27 23:25:45 +000083 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000084 /* Increment in loop */)
85 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000086
87 if (GlobalNestedNameSpecifier)
88 GlobalNestedNameSpecifier->Destroy(*this);
89
Eli Friedmanb26153c2008-05-27 03:08:09 +000090 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000091}
92
93void ASTContext::PrintStats() const {
94 fprintf(stderr, "*** AST Context Stats:\n");
95 fprintf(stderr, " %d types total.\n", (int)Types.size());
96 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +000097 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +000098 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
99 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
100
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000102 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
103 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000104 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000105
106 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
107 Type *T = Types[i];
108 if (isa<BuiltinType>(T))
109 ++NumBuiltin;
110 else if (isa<PointerType>(T))
111 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000112 else if (isa<BlockPointerType>(T))
113 ++NumBlockPointer;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000114 else if (isa<LValueReferenceType>(T))
115 ++NumLValueReference;
116 else if (isa<RValueReferenceType>(T))
117 ++NumRValueReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000118 else if (isa<MemberPointerType>(T))
119 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000120 else if (isa<ComplexType>(T))
121 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 else if (isa<ArrayType>(T))
123 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000124 else if (isa<VectorType>(T))
125 ++NumVector;
Douglas Gregor72564e72009-02-26 23:50:07 +0000126 else if (isa<FunctionNoProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 ++NumFunctionNP;
Douglas Gregor72564e72009-02-26 23:50:07 +0000128 else if (isa<FunctionProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 ++NumFunctionP;
130 else if (isa<TypedefType>(T))
131 ++NumTypeName;
132 else if (TagType *TT = dyn_cast<TagType>(T)) {
133 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000134 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000136 case TagDecl::TK_struct: ++NumTagStruct; break;
137 case TagDecl::TK_union: ++NumTagUnion; break;
138 case TagDecl::TK_class: ++NumTagClass; break;
139 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000141 } else if (isa<ObjCInterfaceType>(T))
142 ++NumObjCInterfaces;
143 else if (isa<ObjCQualifiedInterfaceType>(T))
144 ++NumObjCQualifiedInterfaces;
145 else if (isa<ObjCQualifiedIdType>(T))
146 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000147 else if (isa<TypeOfType>(T))
148 ++NumTypeOfTypes;
Douglas Gregor72564e72009-02-26 23:50:07 +0000149 else if (isa<TypeOfExprType>(T))
150 ++NumTypeOfExprTypes;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000151 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000152 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 assert(0 && "Unknown type!");
154 }
155 }
156
157 fprintf(stderr, " %d builtin types\n", NumBuiltin);
158 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000159 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000160 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
161 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000162 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000163 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000165 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
167 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
168 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
169 fprintf(stderr, " %d tagged types\n", NumTagged);
170 fprintf(stderr, " %d struct types\n", NumTagStruct);
171 fprintf(stderr, " %d union types\n", NumTagUnion);
172 fprintf(stderr, " %d class types\n", NumTagClass);
173 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000174 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000175 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000176 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000177 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000178 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000179 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor72564e72009-02-26 23:50:07 +0000180 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000181
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
183 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000184 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000185 NumLValueReference*sizeof(LValueReferenceType)+
186 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000187 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000188 NumFunctionP*sizeof(FunctionProtoType)+
189 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000190 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000191 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000192}
193
194
195void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000196 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000197}
198
Reid Spencer5f016e22007-07-11 17:01:13 +0000199void ASTContext::InitBuiltinTypes() {
200 assert(VoidTy.isNull() && "Context reinitialized?");
201
202 // C99 6.2.5p19.
203 InitBuiltinType(VoidTy, BuiltinType::Void);
204
205 // C99 6.2.5p2.
206 InitBuiltinType(BoolTy, BuiltinType::Bool);
207 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000208 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 InitBuiltinType(CharTy, BuiltinType::Char_S);
210 else
211 InitBuiltinType(CharTy, BuiltinType::Char_U);
212 // C99 6.2.5p4.
213 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
214 InitBuiltinType(ShortTy, BuiltinType::Short);
215 InitBuiltinType(IntTy, BuiltinType::Int);
216 InitBuiltinType(LongTy, BuiltinType::Long);
217 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
218
219 // C99 6.2.5p6.
220 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
221 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
222 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
223 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
224 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
225
226 // C99 6.2.5p10.
227 InitBuiltinType(FloatTy, BuiltinType::Float);
228 InitBuiltinType(DoubleTy, BuiltinType::Double);
229 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000230
Chris Lattner3a250322009-02-26 23:43:47 +0000231 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
232 InitBuiltinType(WCharTy, BuiltinType::WChar);
233 else // C99
234 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000235
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000236 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000237 InitBuiltinType(OverloadTy, BuiltinType::Overload);
238
239 // Placeholder type for type-dependent expressions whose type is
240 // completely unknown. No code should ever check a type against
241 // DependentTy and users should never see it; however, it is here to
242 // help diagnose failures to properly check for type-dependent
243 // expressions.
244 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000245
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 // C99 6.2.5p11.
247 FloatComplexTy = getComplexType(FloatTy);
248 DoubleComplexTy = getComplexType(DoubleTy);
249 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000250
Steve Naroff7e219e42007-10-15 14:41:52 +0000251 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000252 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000253 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000254 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000255 ClassStructType = 0;
256
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000257 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000258
259 // void * type
260 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000261}
262
Chris Lattner464175b2007-07-18 17:52:12 +0000263//===----------------------------------------------------------------------===//
264// Type Sizing and Analysis
265//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000266
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000267/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
268/// scalar floating point type.
269const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
270 const BuiltinType *BT = T->getAsBuiltinType();
271 assert(BT && "Not a floating point type!");
272 switch (BT->getKind()) {
273 default: assert(0 && "Not a floating point type!");
274 case BuiltinType::Float: return Target.getFloatFormat();
275 case BuiltinType::Double: return Target.getDoubleFormat();
276 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
277 }
278}
279
Chris Lattneraf707ab2009-01-24 21:53:27 +0000280/// getDeclAlign - Return a conservative estimate of the alignment of the
281/// specified decl. Note that bitfields do not have a valid alignment, so
282/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000283unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000284 unsigned Align = Target.getCharWidth();
285
286 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
287 Align = std::max(Align, AA->getAlignment());
288
Chris Lattneraf707ab2009-01-24 21:53:27 +0000289 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
290 QualType T = VD->getType();
291 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000292 if (!T->isIncompleteType() && !T->isFunctionType()) {
293 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
294 T = cast<ArrayType>(T)->getElementType();
295
296 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
297 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000298 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000299
300 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000301}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000302
Chris Lattnera7674d82007-07-13 22:13:22 +0000303/// getTypeSize - Return the size of the specified type, in bits. This method
304/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000305std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000306ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000307 T = getCanonicalType(T);
Mike Stump5e301002009-02-27 18:32:39 +0000308 uint64_t Width=0;
309 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000310 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000311#define TYPE(Class, Base)
312#define ABSTRACT_TYPE(Class, Base)
313#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
314#define DEPENDENT_TYPE(Class, Base) case Type::Class:
315#include "clang/AST/TypeNodes.def"
316 assert(false && "Should not see non-canonical or dependent types");
317 break;
318
Chris Lattner692233e2007-07-13 22:27:08 +0000319 case Type::FunctionNoProto:
320 case Type::FunctionProto:
Douglas Gregor72564e72009-02-26 23:50:07 +0000321 case Type::IncompleteArray:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000322 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000323 case Type::VariableArray:
324 assert(0 && "VLAs not implemented yet!");
325 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000326 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000327
Chris Lattner98be4942008-03-05 18:54:05 +0000328 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000329 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000330 Align = EltInfo.second;
331 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000332 }
Nate Begeman213541a2008-04-18 23:10:10 +0000333 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000334 case Type::Vector: {
335 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000336 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000337 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000338 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000339 // If the alignment is not a power of 2, round up to the next power of 2.
340 // This happens for non-power-of-2 length vectors.
341 // FIXME: this should probably be a target property.
342 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000343 break;
344 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000345
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000346 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000347 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000348 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000349 case BuiltinType::Void:
350 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000351 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000352 Width = Target.getBoolWidth();
353 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000354 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000355 case BuiltinType::Char_S:
356 case BuiltinType::Char_U:
357 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000358 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000359 Width = Target.getCharWidth();
360 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000361 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000362 case BuiltinType::WChar:
363 Width = Target.getWCharWidth();
364 Align = Target.getWCharAlign();
365 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000366 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000367 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000368 Width = Target.getShortWidth();
369 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000370 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000371 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000372 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000373 Width = Target.getIntWidth();
374 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000375 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000376 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000377 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000378 Width = Target.getLongWidth();
379 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000380 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000381 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000382 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000383 Width = Target.getLongLongWidth();
384 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000385 break;
386 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000387 Width = Target.getFloatWidth();
388 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000389 break;
390 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000391 Width = Target.getDoubleWidth();
392 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000393 break;
394 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000395 Width = Target.getLongDoubleWidth();
396 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000397 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000398 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000399 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000400 case Type::FixedWidthInt:
401 // FIXME: This isn't precisely correct; the width/alignment should depend
402 // on the available types for the target
403 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000404 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000405 Align = Width;
406 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000407 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000408 // FIXME: Pointers into different addr spaces could have different sizes and
409 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000410 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000411 case Type::ObjCQualifiedId:
Eli Friedman4bdf0872009-02-22 04:02:33 +0000412 case Type::ObjCQualifiedClass:
Douglas Gregor72564e72009-02-26 23:50:07 +0000413 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000414 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000415 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000416 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000417 case Type::BlockPointer: {
418 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
419 Width = Target.getPointerWidth(AS);
420 Align = Target.getPointerAlign(AS);
421 break;
422 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000423 case Type::Pointer: {
424 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000425 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000426 Align = Target.getPointerAlign(AS);
427 break;
428 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000429 case Type::LValueReference:
430 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000431 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000432 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000433 // FIXME: This is wrong for struct layout: a reference in a struct has
434 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000435 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000436 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000437 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000438 // the GCC ABI, where pointers to data are one pointer large, pointers to
439 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000440 // other compilers too, we need to delegate this completely to TargetInfo
441 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000442 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
443 unsigned AS = Pointee.getAddressSpace();
444 Width = Target.getPointerWidth(AS);
445 if (Pointee->isFunctionType())
446 Width *= 2;
447 Align = Target.getPointerAlign(AS);
448 // GCC aligns at single pointer width.
449 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000450 case Type::Complex: {
451 // Complex types have the same alignment as their elements, but twice the
452 // size.
453 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000454 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000455 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000456 Align = EltInfo.second;
457 break;
458 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000459 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000460 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000461 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
462 Width = Layout.getSize();
463 Align = Layout.getAlignment();
464 break;
465 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000466 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000467 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000468 const TagType *TT = cast<TagType>(T);
469
470 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000471 Width = 1;
472 Align = 1;
473 break;
474 }
475
Daniel Dunbar1d751182008-11-08 05:48:37 +0000476 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000477 return getTypeInfo(ET->getDecl()->getIntegerType());
478
Daniel Dunbar1d751182008-11-08 05:48:37 +0000479 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000480 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
481 Width = Layout.getSize();
482 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000483 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000484 }
Chris Lattner71763312008-04-06 22:05:18 +0000485 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000486
Chris Lattner464175b2007-07-18 17:52:12 +0000487 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000488 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000489}
490
Chris Lattner34ebde42009-01-27 18:08:34 +0000491/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
492/// type for the current target in bits. This can be different than the ABI
493/// alignment in cases where it is beneficial for performance to overalign
494/// a data type.
495unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
496 unsigned ABIAlign = getTypeAlign(T);
497
498 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000499 if (T->isSpecificBuiltinType(BuiltinType::Double))
500 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000501
502 return ABIAlign;
503}
504
505
Devang Patel8b277042008-06-04 21:22:16 +0000506/// LayoutField - Field layout.
507void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000508 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000509 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000510 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000511 uint64_t FieldOffset = IsUnion ? 0 : Size;
512 uint64_t FieldSize;
513 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000514
515 // FIXME: Should this override struct packing? Probably we want to
516 // take the minimum?
517 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
518 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000519
520 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
521 // TODO: Need to check this algorithm on other targets!
522 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000523 FieldSize =
524 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000525
526 std::pair<uint64_t, unsigned> FieldInfo =
527 Context.getTypeInfo(FD->getType());
528 uint64_t TypeSize = FieldInfo.first;
529
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000530 // Determine the alignment of this bitfield. The packing
531 // attributes define a maximum and the alignment attribute defines
532 // a minimum.
533 // FIXME: What is the right behavior when the specified alignment
534 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000535 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000536 if (FieldPacking)
537 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000538 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
539 FieldAlign = std::max(FieldAlign, AA->getAlignment());
540
541 // Check if we need to add padding to give the field the correct
542 // alignment.
543 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
544 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
545
546 // Padding members don't affect overall alignment
547 if (!FD->getIdentifier())
548 FieldAlign = 1;
549 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000550 if (FD->getType()->isIncompleteArrayType()) {
551 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000552 // query getTypeInfo about these, so we figure it out here.
553 // Flexible array members don't have any size, but they
554 // have to be aligned appropriately for their element type.
555 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000556 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000557 FieldAlign = Context.getTypeAlign(ATy->getElementType());
558 } else {
559 std::pair<uint64_t, unsigned> FieldInfo =
560 Context.getTypeInfo(FD->getType());
561 FieldSize = FieldInfo.first;
562 FieldAlign = FieldInfo.second;
563 }
564
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000565 // Determine the alignment of this bitfield. The packing
566 // attributes define a maximum and the alignment attribute defines
567 // a minimum. Additionally, the packing alignment must be at least
568 // a byte for non-bitfields.
569 //
570 // FIXME: What is the right behavior when the specified alignment
571 // is smaller than the specified packing?
572 if (FieldPacking)
573 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000574 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
575 FieldAlign = std::max(FieldAlign, AA->getAlignment());
576
577 // Round up the current record size to the field's alignment boundary.
578 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
579 }
580
581 // Place this field at the current location.
582 FieldOffsets[FieldNo] = FieldOffset;
583
584 // Reserve space for this field.
585 if (IsUnion) {
586 Size = std::max(Size, FieldSize);
587 } else {
588 Size = FieldOffset + FieldSize;
589 }
590
591 // Remember max struct/class alignment.
592 Alignment = std::max(Alignment, FieldAlign);
593}
594
Fariborz Jahanian88e469c2009-03-05 20:08:48 +0000595void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
596 std::vector<FieldDecl*> &Fields) const {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000597 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
598 if (SuperClass)
599 CollectObjCIvars(SuperClass, Fields);
600 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
601 E = OI->ivar_end(); I != E; ++I) {
602 ObjCIvarDecl *IVDecl = (*I);
603 if (!IVDecl->isInvalidDecl())
604 Fields.push_back(cast<FieldDecl>(IVDecl));
605 }
606}
607
608/// addRecordToClass - produces record info. for the class for its
609/// ivars and all those inherited.
610///
611const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
612{
613 const RecordDecl *&RD = ASTRecordForInterface[D];
614 if (RD)
615 return RD;
616 std::vector<FieldDecl*> RecFields;
617 CollectObjCIvars(D, RecFields);
618 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
619 D->getLocation(),
620 D->getIdentifier());
621 /// FIXME! Can do collection of ivars and adding to the record while
622 /// doing it.
623 for (unsigned int i = 0; i != RecFields.size(); i++) {
624 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
625 RecFields[i]->getLocation(),
626 RecFields[i]->getIdentifier(),
627 RecFields[i]->getType(),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000628 RecFields[i]->getBitWidth(), false);
Douglas Gregor482b77d2009-01-12 23:27:07 +0000629 NewRD->addDecl(Field);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000630 }
631 NewRD->completeDefinition(*this);
632 RD = NewRD;
633 return RD;
634}
Devang Patel44a3dde2008-06-04 21:54:36 +0000635
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000636/// setFieldDecl - maps a field for the given Ivar reference node.
637//
638void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
639 const ObjCIvarDecl *Ivar,
640 const ObjCIvarRefExpr *MRef) {
641 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
642 lookupFieldDeclForIvar(*this, Ivar);
643 ASTFieldForIvarRef[MRef] = FD;
644}
645
Chris Lattner61710852008-10-05 17:34:18 +0000646/// getASTObjcInterfaceLayout - Get or compute information about the layout of
647/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000648/// position information.
649const ASTRecordLayout &
650ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
651 // Look up this layout, if already laid out, return what we have.
652 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
653 if (Entry) return *Entry;
654
655 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
656 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000657 ASTRecordLayout *NewEntry = NULL;
658 unsigned FieldCount = D->ivar_size();
659 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
660 FieldCount++;
661 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
662 unsigned Alignment = SL.getAlignment();
663 uint64_t Size = SL.getSize();
664 NewEntry = new ASTRecordLayout(Size, Alignment);
665 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000666 // Super class is at the beginning of the layout.
667 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000668 } else {
669 NewEntry = new ASTRecordLayout();
670 NewEntry->InitializeLayout(FieldCount);
671 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000672 Entry = NewEntry;
673
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000674 unsigned StructPacking = 0;
675 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
676 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000677
678 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
679 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
680 AA->getAlignment()));
681
682 // Layout each ivar sequentially.
683 unsigned i = 0;
684 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
685 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
686 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000687 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000688 }
689
690 // Finally, round the size of the total struct up to the alignment of the
691 // struct itself.
692 NewEntry->FinalizeLayout();
693 return *NewEntry;
694}
695
Devang Patel88a981b2007-11-01 19:11:01 +0000696/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000697/// specified record (struct/union/class), which indicates its size and field
698/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000699const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000700 D = D->getDefinition(*this);
701 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000702
Chris Lattner464175b2007-07-18 17:52:12 +0000703 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000704 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000705 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000706
Devang Patel88a981b2007-11-01 19:11:01 +0000707 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
708 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
709 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000710 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000711
Douglas Gregore267ff32008-12-11 20:41:00 +0000712 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor44b43212008-12-11 16:49:14 +0000713 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000714 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000715
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000716 unsigned StructPacking = 0;
717 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
718 StructPacking = PA->getAlignment();
719
Eli Friedman4bd998b2008-05-30 09:31:38 +0000720 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000721 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
722 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000723
Eli Friedman4bd998b2008-05-30 09:31:38 +0000724 // Layout each field, for now, just sequentially, respecting alignment. In
725 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000726 unsigned FieldIdx = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000727 for (RecordDecl::field_iterator Field = D->field_begin(),
728 FieldEnd = D->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000729 Field != FieldEnd; (void)++Field, ++FieldIdx)
730 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000731
732 // Finally, round the size of the total struct up to the alignment of the
733 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000734 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000735 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000736}
737
Chris Lattnera7674d82007-07-13 22:13:22 +0000738//===----------------------------------------------------------------------===//
739// Type creation/memoization methods
740//===----------------------------------------------------------------------===//
741
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000742QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000743 QualType CanT = getCanonicalType(T);
744 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000745 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000746
747 // If we are composing extended qualifiers together, merge together into one
748 // ExtQualType node.
749 unsigned CVRQuals = T.getCVRQualifiers();
750 QualType::GCAttrTypes GCAttr = QualType::GCNone;
751 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000752
Chris Lattnerb7d25532009-02-18 22:53:11 +0000753 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
754 // If this type already has an address space specified, it cannot get
755 // another one.
756 assert(EQT->getAddressSpace() == 0 &&
757 "Type cannot be in multiple addr spaces!");
758 GCAttr = EQT->getObjCGCAttr();
759 TypeNode = EQT->getBaseType();
760 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000761
Chris Lattnerb7d25532009-02-18 22:53:11 +0000762 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000763 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000764 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000765 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000766 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000767 return QualType(EXTQy, CVRQuals);
768
Christopher Lambebb97e92008-02-04 02:31:56 +0000769 // If the base type isn't canonical, this won't be a canonical type either,
770 // so fill in the canonical type field.
771 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000772 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000773 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000774
Chris Lattnerb7d25532009-02-18 22:53:11 +0000775 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000776 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000777 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000778 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000779 ExtQualType *New =
780 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000781 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000782 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000783 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000784}
785
Chris Lattnerb7d25532009-02-18 22:53:11 +0000786QualType ASTContext::getObjCGCQualType(QualType T,
787 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000788 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000789 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000790 return T;
791
Chris Lattnerb7d25532009-02-18 22:53:11 +0000792 // If we are composing extended qualifiers together, merge together into one
793 // ExtQualType node.
794 unsigned CVRQuals = T.getCVRQualifiers();
795 Type *TypeNode = T.getTypePtr();
796 unsigned AddressSpace = 0;
797
798 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
799 // If this type already has an address space specified, it cannot get
800 // another one.
801 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
802 "Type cannot be in multiple addr spaces!");
803 AddressSpace = EQT->getAddressSpace();
804 TypeNode = EQT->getBaseType();
805 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000806
807 // Check if we've already instantiated an gc qual'd type of this type.
808 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000809 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000810 void *InsertPos = 0;
811 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000812 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000813
814 // If the base type isn't canonical, this won't be a canonical type either,
815 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000816 // FIXME: Isn't this also not canonical if the base type is a array
817 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000818 QualType Canonical;
819 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000820 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000821
Chris Lattnerb7d25532009-02-18 22:53:11 +0000822 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000823 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
824 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
825 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000826 ExtQualType *New =
827 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000828 ExtQualTypes.InsertNode(New, InsertPos);
829 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000830 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000831}
Chris Lattnera7674d82007-07-13 22:13:22 +0000832
Reid Spencer5f016e22007-07-11 17:01:13 +0000833/// getComplexType - Return the uniqued reference to the type for a complex
834/// number with the specified element type.
835QualType ASTContext::getComplexType(QualType T) {
836 // Unique pointers, to guarantee there is only one pointer of a particular
837 // structure.
838 llvm::FoldingSetNodeID ID;
839 ComplexType::Profile(ID, T);
840
841 void *InsertPos = 0;
842 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
843 return QualType(CT, 0);
844
845 // If the pointee type isn't canonical, this won't be a canonical type either,
846 // so fill in the canonical type field.
847 QualType Canonical;
848 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000849 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000850
851 // Get the new insert position for the node we care about.
852 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000853 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000854 }
Steve Narofff83820b2009-01-27 22:08:43 +0000855 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 Types.push_back(New);
857 ComplexTypes.InsertNode(New, InsertPos);
858 return QualType(New, 0);
859}
860
Eli Friedmanf98aba32009-02-13 02:31:07 +0000861QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
862 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
863 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
864 FixedWidthIntType *&Entry = Map[Width];
865 if (!Entry)
866 Entry = new FixedWidthIntType(Width, Signed);
867 return QualType(Entry, 0);
868}
Reid Spencer5f016e22007-07-11 17:01:13 +0000869
870/// getPointerType - Return the uniqued reference to the type for a pointer to
871/// the specified type.
872QualType ASTContext::getPointerType(QualType T) {
873 // Unique pointers, to guarantee there is only one pointer of a particular
874 // structure.
875 llvm::FoldingSetNodeID ID;
876 PointerType::Profile(ID, T);
877
878 void *InsertPos = 0;
879 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
880 return QualType(PT, 0);
881
882 // If the pointee type isn't canonical, this won't be a canonical type either,
883 // so fill in the canonical type field.
884 QualType Canonical;
885 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000886 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000887
888 // Get the new insert position for the node we care about.
889 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000890 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 }
Steve Narofff83820b2009-01-27 22:08:43 +0000892 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000893 Types.push_back(New);
894 PointerTypes.InsertNode(New, InsertPos);
895 return QualType(New, 0);
896}
897
Steve Naroff5618bd42008-08-27 16:04:49 +0000898/// getBlockPointerType - Return the uniqued reference to the type for
899/// a pointer to the specified block.
900QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000901 assert(T->isFunctionType() && "block of function types only");
902 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000903 // structure.
904 llvm::FoldingSetNodeID ID;
905 BlockPointerType::Profile(ID, T);
906
907 void *InsertPos = 0;
908 if (BlockPointerType *PT =
909 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
910 return QualType(PT, 0);
911
Steve Naroff296e8d52008-08-28 19:20:44 +0000912 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000913 // type either so fill in the canonical type field.
914 QualType Canonical;
915 if (!T->isCanonical()) {
916 Canonical = getBlockPointerType(getCanonicalType(T));
917
918 // Get the new insert position for the node we care about.
919 BlockPointerType *NewIP =
920 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000921 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000922 }
Steve Narofff83820b2009-01-27 22:08:43 +0000923 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000924 Types.push_back(New);
925 BlockPointerTypes.InsertNode(New, InsertPos);
926 return QualType(New, 0);
927}
928
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000929/// getLValueReferenceType - Return the uniqued reference to the type for an
930/// lvalue reference to the specified type.
931QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000932 // Unique pointers, to guarantee there is only one pointer of a particular
933 // structure.
934 llvm::FoldingSetNodeID ID;
935 ReferenceType::Profile(ID, T);
936
937 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000938 if (LValueReferenceType *RT =
939 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000940 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000941
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 // If the referencee type isn't canonical, this won't be a canonical type
943 // either, so fill in the canonical type field.
944 QualType Canonical;
945 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000946 Canonical = getLValueReferenceType(getCanonicalType(T));
947
Reid Spencer5f016e22007-07-11 17:01:13 +0000948 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000949 LValueReferenceType *NewIP =
950 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000951 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000952 }
953
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000954 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000955 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000956 LValueReferenceTypes.InsertNode(New, InsertPos);
957 return QualType(New, 0);
958}
959
960/// getRValueReferenceType - Return the uniqued reference to the type for an
961/// rvalue reference to the specified type.
962QualType ASTContext::getRValueReferenceType(QualType T) {
963 // Unique pointers, to guarantee there is only one pointer of a particular
964 // structure.
965 llvm::FoldingSetNodeID ID;
966 ReferenceType::Profile(ID, T);
967
968 void *InsertPos = 0;
969 if (RValueReferenceType *RT =
970 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
971 return QualType(RT, 0);
972
973 // If the referencee type isn't canonical, this won't be a canonical type
974 // either, so fill in the canonical type field.
975 QualType Canonical;
976 if (!T->isCanonical()) {
977 Canonical = getRValueReferenceType(getCanonicalType(T));
978
979 // Get the new insert position for the node we care about.
980 RValueReferenceType *NewIP =
981 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
982 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
983 }
984
985 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
986 Types.push_back(New);
987 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000988 return QualType(New, 0);
989}
990
Sebastian Redlf30208a2009-01-24 21:16:55 +0000991/// getMemberPointerType - Return the uniqued reference to the type for a
992/// member pointer to the specified type, in the specified class.
993QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
994{
995 // Unique pointers, to guarantee there is only one pointer of a particular
996 // structure.
997 llvm::FoldingSetNodeID ID;
998 MemberPointerType::Profile(ID, T, Cls);
999
1000 void *InsertPos = 0;
1001 if (MemberPointerType *PT =
1002 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1003 return QualType(PT, 0);
1004
1005 // If the pointee or class type isn't canonical, this won't be a canonical
1006 // type either, so fill in the canonical type field.
1007 QualType Canonical;
1008 if (!T->isCanonical()) {
1009 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1010
1011 // Get the new insert position for the node we care about.
1012 MemberPointerType *NewIP =
1013 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1014 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1015 }
Steve Narofff83820b2009-01-27 22:08:43 +00001016 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001017 Types.push_back(New);
1018 MemberPointerTypes.InsertNode(New, InsertPos);
1019 return QualType(New, 0);
1020}
1021
Steve Narofffb22d962007-08-30 01:06:46 +00001022/// getConstantArrayType - Return the unique reference to the type for an
1023/// array of the specified element type.
1024QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +00001025 const llvm::APInt &ArySize,
1026 ArrayType::ArraySizeModifier ASM,
1027 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001028 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001029 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001030
1031 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001032 if (ConstantArrayType *ATP =
1033 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 return QualType(ATP, 0);
1035
1036 // If the element type isn't canonical, this won't be a canonical type either,
1037 // so fill in the canonical type field.
1038 QualType Canonical;
1039 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001040 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001041 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001042 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001043 ConstantArrayType *NewIP =
1044 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001045 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001046 }
1047
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001048 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001049 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001050 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001051 Types.push_back(New);
1052 return QualType(New, 0);
1053}
1054
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001055/// getVariableArrayType - Returns a non-unique reference to the type for a
1056/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001057QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1058 ArrayType::ArraySizeModifier ASM,
1059 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001060 // Since we don't unique expressions, it isn't possible to unique VLA's
1061 // that have an expression provided for their size.
1062
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001063 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001064 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001065
1066 VariableArrayTypes.push_back(New);
1067 Types.push_back(New);
1068 return QualType(New, 0);
1069}
1070
Douglas Gregor898574e2008-12-05 23:32:09 +00001071/// getDependentSizedArrayType - Returns a non-unique reference to
1072/// the type for a dependently-sized array of the specified element
1073/// type. FIXME: We will need these to be uniqued, or at least
1074/// comparable, at some point.
1075QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1076 ArrayType::ArraySizeModifier ASM,
1077 unsigned EltTypeQuals) {
1078 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1079 "Size must be type- or value-dependent!");
1080
1081 // Since we don't unique expressions, it isn't possible to unique
1082 // dependently-sized array types.
1083
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001084 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001085 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1086 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001087
1088 DependentSizedArrayTypes.push_back(New);
1089 Types.push_back(New);
1090 return QualType(New, 0);
1091}
1092
Eli Friedmanc5773c42008-02-15 18:16:39 +00001093QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1094 ArrayType::ArraySizeModifier ASM,
1095 unsigned EltTypeQuals) {
1096 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001097 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001098
1099 void *InsertPos = 0;
1100 if (IncompleteArrayType *ATP =
1101 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1102 return QualType(ATP, 0);
1103
1104 // If the element type isn't canonical, this won't be a canonical type
1105 // either, so fill in the canonical type field.
1106 QualType Canonical;
1107
1108 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001109 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001110 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001111
1112 // Get the new insert position for the node we care about.
1113 IncompleteArrayType *NewIP =
1114 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001115 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001116 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001117
Steve Narofff83820b2009-01-27 22:08:43 +00001118 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001119 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001120
1121 IncompleteArrayTypes.InsertNode(New, InsertPos);
1122 Types.push_back(New);
1123 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001124}
1125
Steve Naroff73322922007-07-18 18:00:27 +00001126/// getVectorType - Return the unique reference to a vector type of
1127/// the specified element type and size. VectorType must be a built-in type.
1128QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001129 BuiltinType *baseType;
1130
Chris Lattnerf52ab252008-04-06 22:59:24 +00001131 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001132 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001133
1134 // Check if we've already instantiated a vector of this type.
1135 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001136 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001137 void *InsertPos = 0;
1138 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1139 return QualType(VTP, 0);
1140
1141 // If the element type isn't canonical, this won't be a canonical type either,
1142 // so fill in the canonical type field.
1143 QualType Canonical;
1144 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001145 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001146
1147 // Get the new insert position for the node we care about.
1148 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001149 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001150 }
Steve Narofff83820b2009-01-27 22:08:43 +00001151 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 VectorTypes.InsertNode(New, InsertPos);
1153 Types.push_back(New);
1154 return QualType(New, 0);
1155}
1156
Nate Begeman213541a2008-04-18 23:10:10 +00001157/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001158/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001159QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001160 BuiltinType *baseType;
1161
Chris Lattnerf52ab252008-04-06 22:59:24 +00001162 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001163 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001164
1165 // Check if we've already instantiated a vector of this type.
1166 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001167 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001168 void *InsertPos = 0;
1169 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1170 return QualType(VTP, 0);
1171
1172 // If the element type isn't canonical, this won't be a canonical type either,
1173 // so fill in the canonical type field.
1174 QualType Canonical;
1175 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001176 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001177
1178 // Get the new insert position for the node we care about.
1179 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001180 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001181 }
Steve Narofff83820b2009-01-27 22:08:43 +00001182 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001183 VectorTypes.InsertNode(New, InsertPos);
1184 Types.push_back(New);
1185 return QualType(New, 0);
1186}
1187
Douglas Gregor72564e72009-02-26 23:50:07 +00001188/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001189///
Douglas Gregor72564e72009-02-26 23:50:07 +00001190QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 // Unique functions, to guarantee there is only one function of a particular
1192 // structure.
1193 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001194 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001195
1196 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001197 if (FunctionNoProtoType *FT =
1198 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001199 return QualType(FT, 0);
1200
1201 QualType Canonical;
1202 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001203 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001204
1205 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001206 FunctionNoProtoType *NewIP =
1207 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001208 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001209 }
1210
Douglas Gregor72564e72009-02-26 23:50:07 +00001211 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001212 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001213 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 return QualType(New, 0);
1215}
1216
1217/// getFunctionType - Return a normal function type with a typed argument
1218/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001219QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001220 unsigned NumArgs, bool isVariadic,
1221 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001222 // Unique functions, to guarantee there is only one function of a particular
1223 // structure.
1224 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001225 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001226 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001227
1228 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001229 if (FunctionProtoType *FTP =
1230 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001231 return QualType(FTP, 0);
1232
1233 // Determine whether the type being created is already canonical or not.
1234 bool isCanonical = ResultTy->isCanonical();
1235 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1236 if (!ArgArray[i]->isCanonical())
1237 isCanonical = false;
1238
1239 // If this type isn't canonical, get the canonical version of it.
1240 QualType Canonical;
1241 if (!isCanonical) {
1242 llvm::SmallVector<QualType, 16> CanonicalArgs;
1243 CanonicalArgs.reserve(NumArgs);
1244 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001245 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001246
Chris Lattnerf52ab252008-04-06 22:59:24 +00001247 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001249 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001250
1251 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001252 FunctionProtoType *NewIP =
1253 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001254 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 }
1256
Douglas Gregor72564e72009-02-26 23:50:07 +00001257 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001258 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001259 FunctionProtoType *FTP =
1260 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001261 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001262 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001263 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001264 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001265 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001266 return QualType(FTP, 0);
1267}
1268
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001269/// getTypeDeclType - Return the unique reference to the type for the
1270/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001271QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001272 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001273 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1274
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001275 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001276 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001277 else if (isa<TemplateTypeParmDecl>(Decl)) {
1278 assert(false && "Template type parameter types are always available.");
1279 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001280 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001281
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001282 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001283 if (PrevDecl)
1284 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001285 else
1286 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001287 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001288 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1289 if (PrevDecl)
1290 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001291 else
1292 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001293 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001294 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001295 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001296
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001297 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001298 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001299}
1300
Reid Spencer5f016e22007-07-11 17:01:13 +00001301/// getTypedefType - Return the unique reference to the type for the
1302/// specified typename decl.
1303QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1304 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1305
Chris Lattnerf52ab252008-04-06 22:59:24 +00001306 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001307 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001308 Types.push_back(Decl->TypeForDecl);
1309 return QualType(Decl->TypeForDecl, 0);
1310}
1311
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001312/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001313/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001314QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001315 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1316
Steve Narofff83820b2009-01-27 22:08:43 +00001317 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001318 Types.push_back(Decl->TypeForDecl);
1319 return QualType(Decl->TypeForDecl, 0);
1320}
1321
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00001322/// buildObjCInterfaceType - Returns a new type for the interface
1323/// declaration, regardless. It also removes any previously built
1324/// record declaration so caller can rebuild it.
1325QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) {
1326 const RecordDecl *&RD = ASTRecordForInterface[Decl];
1327 if (RD)
1328 RD = 0;
1329 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
1330 Types.push_back(Decl->TypeForDecl);
1331 return QualType(Decl->TypeForDecl, 0);
1332}
1333
Douglas Gregorfab9d672009-02-05 23:33:38 +00001334/// \brief Retrieve the template type parameter type for a template
1335/// parameter with the given depth, index, and (optionally) name.
1336QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1337 IdentifierInfo *Name) {
1338 llvm::FoldingSetNodeID ID;
1339 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1340 void *InsertPos = 0;
1341 TemplateTypeParmType *TypeParm
1342 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1343
1344 if (TypeParm)
1345 return QualType(TypeParm, 0);
1346
1347 if (Name)
1348 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1349 getTemplateTypeParmType(Depth, Index));
1350 else
1351 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1352
1353 Types.push_back(TypeParm);
1354 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1355
1356 return QualType(TypeParm, 0);
1357}
1358
Douglas Gregor55f6b142009-02-09 18:46:07 +00001359QualType
1360ASTContext::getClassTemplateSpecializationType(TemplateDecl *Template,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001361 const TemplateArgument *Args,
Douglas Gregor55f6b142009-02-09 18:46:07 +00001362 unsigned NumArgs,
Douglas Gregor55f6b142009-02-09 18:46:07 +00001363 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001364 if (!Canon.isNull())
1365 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001366
Douglas Gregor55f6b142009-02-09 18:46:07 +00001367 llvm::FoldingSetNodeID ID;
Douglas Gregor40808ce2009-03-09 23:48:35 +00001368 ClassTemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
1369
Douglas Gregor55f6b142009-02-09 18:46:07 +00001370 void *InsertPos = 0;
1371 ClassTemplateSpecializationType *Spec
1372 = ClassTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
1373
1374 if (Spec)
1375 return QualType(Spec, 0);
1376
Douglas Gregor40808ce2009-03-09 23:48:35 +00001377 void *Mem = Allocate((sizeof(ClassTemplateSpecializationType) +
1378 sizeof(TemplateArgument) * NumArgs),
1379 8);
1380 Spec = new (Mem) ClassTemplateSpecializationType(Template, Args, NumArgs,
1381 Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001382 Types.push_back(Spec);
1383 ClassTemplateSpecializationTypes.InsertNode(Spec, InsertPos);
1384
1385 return QualType(Spec, 0);
1386}
1387
Douglas Gregore4e5b052009-03-19 00:18:19 +00001388QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001389ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001390 QualType NamedType) {
1391 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001392 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001393
1394 void *InsertPos = 0;
1395 QualifiedNameType *T
1396 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1397 if (T)
1398 return QualType(T, 0);
1399
Douglas Gregorab452ba2009-03-26 23:50:42 +00001400 T = new (*this) QualifiedNameType(NNS, NamedType,
1401 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001402 Types.push_back(T);
1403 QualifiedNameTypes.InsertNode(T, InsertPos);
1404 return QualType(T, 0);
1405}
1406
Douglas Gregord57959a2009-03-27 23:10:48 +00001407QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1408 const IdentifierInfo *Name,
1409 QualType Canon) {
1410 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1411
1412 if (Canon.isNull()) {
1413 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1414 if (CanonNNS != NNS)
1415 Canon = getTypenameType(CanonNNS, Name);
1416 }
1417
1418 llvm::FoldingSetNodeID ID;
1419 TypenameType::Profile(ID, NNS, Name);
1420
1421 void *InsertPos = 0;
1422 TypenameType *T
1423 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1424 if (T)
1425 return QualType(T, 0);
1426
1427 T = new (*this) TypenameType(NNS, Name, Canon);
1428 Types.push_back(T);
1429 TypenameTypes.InsertNode(T, InsertPos);
1430 return QualType(T, 0);
1431}
1432
Chris Lattner88cb27a2008-04-07 04:56:42 +00001433/// CmpProtocolNames - Comparison predicate for sorting protocols
1434/// alphabetically.
1435static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1436 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001437 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001438}
1439
1440static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1441 unsigned &NumProtocols) {
1442 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1443
1444 // Sort protocols, keyed by name.
1445 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1446
1447 // Remove duplicates.
1448 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1449 NumProtocols = ProtocolsEnd-Protocols;
1450}
1451
1452
Chris Lattner065f0d72008-04-07 04:44:08 +00001453/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1454/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001455QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1456 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001457 // Sort the protocol list alphabetically to canonicalize it.
1458 SortAndUniqueProtocols(Protocols, NumProtocols);
1459
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001460 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001461 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001462
1463 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001464 if (ObjCQualifiedInterfaceType *QT =
1465 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001466 return QualType(QT, 0);
1467
1468 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001469 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001470 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001471
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001472 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001473 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001474 return QualType(QType, 0);
1475}
1476
Chris Lattner88cb27a2008-04-07 04:56:42 +00001477/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1478/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001479QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001480 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001481 // Sort the protocol list alphabetically to canonicalize it.
1482 SortAndUniqueProtocols(Protocols, NumProtocols);
1483
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001484 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001485 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001486
1487 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001488 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001489 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001490 return QualType(QT, 0);
1491
1492 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001493 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001494 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001495 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001496 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001497 return QualType(QType, 0);
1498}
1499
Douglas Gregor72564e72009-02-26 23:50:07 +00001500/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1501/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001502/// multiple declarations that refer to "typeof(x)" all contain different
1503/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1504/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001505QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001506 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001507 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001508 Types.push_back(toe);
1509 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001510}
1511
Steve Naroff9752f252007-08-01 18:02:17 +00001512/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1513/// TypeOfType AST's. The only motivation to unique these nodes would be
1514/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1515/// an issue. This doesn't effect the type checker, since it operates
1516/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001517QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001518 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001519 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001520 Types.push_back(tot);
1521 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001522}
1523
Reid Spencer5f016e22007-07-11 17:01:13 +00001524/// getTagDeclType - Return the unique reference to the type for the
1525/// specified TagDecl (struct/union/class/enum) decl.
1526QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001527 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001528 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001529}
1530
1531/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1532/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1533/// needs to agree with the definition in <stddef.h>.
1534QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001535 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001536}
1537
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001538/// getSignedWCharType - Return the type of "signed wchar_t".
1539/// Used when in C++, as a GCC extension.
1540QualType ASTContext::getSignedWCharType() const {
1541 // FIXME: derive from "Target" ?
1542 return WCharTy;
1543}
1544
1545/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1546/// Used when in C++, as a GCC extension.
1547QualType ASTContext::getUnsignedWCharType() const {
1548 // FIXME: derive from "Target" ?
1549 return UnsignedIntTy;
1550}
1551
Chris Lattner8b9023b2007-07-13 03:05:23 +00001552/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1553/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1554QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001555 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001556}
1557
Chris Lattnere6327742008-04-02 05:18:44 +00001558//===----------------------------------------------------------------------===//
1559// Type Operators
1560//===----------------------------------------------------------------------===//
1561
Chris Lattner77c96472008-04-06 22:41:35 +00001562/// getCanonicalType - Return the canonical (structural) type corresponding to
1563/// the specified potentially non-canonical type. The non-canonical version
1564/// of a type may have many "decorated" versions of types. Decorators can
1565/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1566/// to be free of any of these, allowing two canonical types to be compared
1567/// for exact equality with a simple pointer comparison.
1568QualType ASTContext::getCanonicalType(QualType T) {
1569 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001570
1571 // If the result has type qualifiers, make sure to canonicalize them as well.
1572 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1573 if (TypeQuals == 0) return CanType;
1574
1575 // If the type qualifiers are on an array type, get the canonical type of the
1576 // array with the qualifiers applied to the element type.
1577 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1578 if (!AT)
1579 return CanType.getQualifiedType(TypeQuals);
1580
1581 // Get the canonical version of the element with the extra qualifiers on it.
1582 // This can recursively sink qualifiers through multiple levels of arrays.
1583 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1584 NewEltTy = getCanonicalType(NewEltTy);
1585
1586 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1587 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1588 CAT->getIndexTypeQualifier());
1589 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1590 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1591 IAT->getIndexTypeQualifier());
1592
Douglas Gregor898574e2008-12-05 23:32:09 +00001593 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1594 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1595 DSAT->getSizeModifier(),
1596 DSAT->getIndexTypeQualifier());
1597
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001598 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1599 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1600 VAT->getSizeModifier(),
1601 VAT->getIndexTypeQualifier());
1602}
1603
Douglas Gregord57959a2009-03-27 23:10:48 +00001604NestedNameSpecifier *
1605ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1606 if (!NNS)
1607 return 0;
1608
1609 switch (NNS->getKind()) {
1610 case NestedNameSpecifier::Identifier:
1611 // Canonicalize the prefix but keep the identifier the same.
1612 return NestedNameSpecifier::Create(*this,
1613 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1614 NNS->getAsIdentifier());
1615
1616 case NestedNameSpecifier::Namespace:
1617 // A namespace is canonical; build a nested-name-specifier with
1618 // this namespace and no prefix.
1619 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1620
1621 case NestedNameSpecifier::TypeSpec:
1622 case NestedNameSpecifier::TypeSpecWithTemplate: {
1623 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1624 NestedNameSpecifier *Prefix = 0;
1625
1626 // FIXME: This isn't the right check!
1627 if (T->isDependentType())
1628 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1629
1630 return NestedNameSpecifier::Create(*this, Prefix,
1631 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1632 T.getTypePtr());
1633 }
1634
1635 case NestedNameSpecifier::Global:
1636 // The global specifier is canonical and unique.
1637 return NNS;
1638 }
1639
1640 // Required to silence a GCC warning
1641 return 0;
1642}
1643
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001644
1645const ArrayType *ASTContext::getAsArrayType(QualType T) {
1646 // Handle the non-qualified case efficiently.
1647 if (T.getCVRQualifiers() == 0) {
1648 // Handle the common positive case fast.
1649 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1650 return AT;
1651 }
1652
1653 // Handle the common negative case fast, ignoring CVR qualifiers.
1654 QualType CType = T->getCanonicalTypeInternal();
1655
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001656 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001657 // test.
1658 if (!isa<ArrayType>(CType) &&
1659 !isa<ArrayType>(CType.getUnqualifiedType()))
1660 return 0;
1661
1662 // Apply any CVR qualifiers from the array type to the element type. This
1663 // implements C99 6.7.3p8: "If the specification of an array type includes
1664 // any type qualifiers, the element type is so qualified, not the array type."
1665
1666 // If we get here, we either have type qualifiers on the type, or we have
1667 // sugar such as a typedef in the way. If we have type qualifiers on the type
1668 // we must propagate them down into the elemeng type.
1669 unsigned CVRQuals = T.getCVRQualifiers();
1670 unsigned AddrSpace = 0;
1671 Type *Ty = T.getTypePtr();
1672
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001673 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001674 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001675 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1676 AddrSpace = EXTQT->getAddressSpace();
1677 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001678 } else {
1679 T = Ty->getDesugaredType();
1680 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1681 break;
1682 CVRQuals |= T.getCVRQualifiers();
1683 Ty = T.getTypePtr();
1684 }
1685 }
1686
1687 // If we have a simple case, just return now.
1688 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1689 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1690 return ATy;
1691
1692 // Otherwise, we have an array and we have qualifiers on it. Push the
1693 // qualifiers into the array element type and return a new array type.
1694 // Get the canonical version of the element with the extra qualifiers on it.
1695 // This can recursively sink qualifiers through multiple levels of arrays.
1696 QualType NewEltTy = ATy->getElementType();
1697 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001698 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001699 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1700
1701 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1702 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1703 CAT->getSizeModifier(),
1704 CAT->getIndexTypeQualifier()));
1705 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1706 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1707 IAT->getSizeModifier(),
1708 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001709
Douglas Gregor898574e2008-12-05 23:32:09 +00001710 if (const DependentSizedArrayType *DSAT
1711 = dyn_cast<DependentSizedArrayType>(ATy))
1712 return cast<ArrayType>(
1713 getDependentSizedArrayType(NewEltTy,
1714 DSAT->getSizeExpr(),
1715 DSAT->getSizeModifier(),
1716 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001717
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001718 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1719 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1720 VAT->getSizeModifier(),
1721 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001722}
1723
1724
Chris Lattnere6327742008-04-02 05:18:44 +00001725/// getArrayDecayedType - Return the properly qualified result of decaying the
1726/// specified array type to a pointer. This operation is non-trivial when
1727/// handling typedefs etc. The canonical type of "T" must be an array type,
1728/// this returns a pointer to a properly qualified element of the array.
1729///
1730/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1731QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001732 // Get the element type with 'getAsArrayType' so that we don't lose any
1733 // typedefs in the element type of the array. This also handles propagation
1734 // of type qualifiers from the array type into the element type if present
1735 // (C99 6.7.3p8).
1736 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1737 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001738
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001739 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001740
1741 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001742 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001743}
1744
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001745QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001746 QualType ElemTy = VAT->getElementType();
1747
1748 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1749 return getBaseElementType(VAT);
1750
1751 return ElemTy;
1752}
1753
Reid Spencer5f016e22007-07-11 17:01:13 +00001754/// getFloatingRank - Return a relative rank for floating point types.
1755/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001756static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001757 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001758 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001759
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001760 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001761 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001762 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001763 case BuiltinType::Float: return FloatRank;
1764 case BuiltinType::Double: return DoubleRank;
1765 case BuiltinType::LongDouble: return LongDoubleRank;
1766 }
1767}
1768
Steve Naroff716c7302007-08-27 01:41:48 +00001769/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1770/// point or a complex type (based on typeDomain/typeSize).
1771/// 'typeDomain' is a real floating point or complex type.
1772/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001773QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1774 QualType Domain) const {
1775 FloatingRank EltRank = getFloatingRank(Size);
1776 if (Domain->isComplexType()) {
1777 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001778 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001779 case FloatRank: return FloatComplexTy;
1780 case DoubleRank: return DoubleComplexTy;
1781 case LongDoubleRank: return LongDoubleComplexTy;
1782 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001783 }
Chris Lattner1361b112008-04-06 23:58:54 +00001784
1785 assert(Domain->isRealFloatingType() && "Unknown domain!");
1786 switch (EltRank) {
1787 default: assert(0 && "getFloatingRank(): illegal value for rank");
1788 case FloatRank: return FloatTy;
1789 case DoubleRank: return DoubleTy;
1790 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001791 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001792}
1793
Chris Lattner7cfeb082008-04-06 23:55:33 +00001794/// getFloatingTypeOrder - Compare the rank of the two specified floating
1795/// point types, ignoring the domain of the type (i.e. 'double' ==
1796/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1797/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001798int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1799 FloatingRank LHSR = getFloatingRank(LHS);
1800 FloatingRank RHSR = getFloatingRank(RHS);
1801
1802 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001803 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001804 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001805 return 1;
1806 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001807}
1808
Chris Lattnerf52ab252008-04-06 22:59:24 +00001809/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1810/// routine will assert if passed a built-in type that isn't an integer or enum,
1811/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001812unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001813 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001814 if (EnumType* ET = dyn_cast<EnumType>(T))
1815 T = ET->getDecl()->getIntegerType().getTypePtr();
1816
1817 // There are two things which impact the integer rank: the width, and
1818 // the ordering of builtins. The builtin ordering is encoded in the
1819 // bottom three bits; the width is encoded in the bits above that.
1820 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1821 return FWIT->getWidth() << 3;
1822 }
1823
Chris Lattnerf52ab252008-04-06 22:59:24 +00001824 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001825 default: assert(0 && "getIntegerRank(): not a built-in integer");
1826 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001827 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001828 case BuiltinType::Char_S:
1829 case BuiltinType::Char_U:
1830 case BuiltinType::SChar:
1831 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001832 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001833 case BuiltinType::Short:
1834 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001835 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001836 case BuiltinType::Int:
1837 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001838 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001839 case BuiltinType::Long:
1840 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001841 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001842 case BuiltinType::LongLong:
1843 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001844 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001845 }
1846}
1847
Chris Lattner7cfeb082008-04-06 23:55:33 +00001848/// getIntegerTypeOrder - Returns the highest ranked integer type:
1849/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1850/// LHS < RHS, return -1.
1851int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001852 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1853 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001854 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001855
Chris Lattnerf52ab252008-04-06 22:59:24 +00001856 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1857 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001858
Chris Lattner7cfeb082008-04-06 23:55:33 +00001859 unsigned LHSRank = getIntegerRank(LHSC);
1860 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001861
Chris Lattner7cfeb082008-04-06 23:55:33 +00001862 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1863 if (LHSRank == RHSRank) return 0;
1864 return LHSRank > RHSRank ? 1 : -1;
1865 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001866
Chris Lattner7cfeb082008-04-06 23:55:33 +00001867 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1868 if (LHSUnsigned) {
1869 // If the unsigned [LHS] type is larger, return it.
1870 if (LHSRank >= RHSRank)
1871 return 1;
1872
1873 // If the signed type can represent all values of the unsigned type, it
1874 // wins. Because we are dealing with 2's complement and types that are
1875 // powers of two larger than each other, this is always safe.
1876 return -1;
1877 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001878
Chris Lattner7cfeb082008-04-06 23:55:33 +00001879 // If the unsigned [RHS] type is larger, return it.
1880 if (RHSRank >= LHSRank)
1881 return -1;
1882
1883 // If the signed type can represent all values of the unsigned type, it
1884 // wins. Because we are dealing with 2's complement and types that are
1885 // powers of two larger than each other, this is always safe.
1886 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001887}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001888
1889// getCFConstantStringType - Return the type used for constant CFStrings.
1890QualType ASTContext::getCFConstantStringType() {
1891 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001892 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001893 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001894 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001895 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001896
1897 // const int *isa;
1898 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001899 // int flags;
1900 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001901 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001902 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001903 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001904 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001905
Anders Carlsson71993dd2007-08-17 05:31:46 +00001906 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001907 for (unsigned i = 0; i < 4; ++i) {
1908 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1909 SourceLocation(), 0,
1910 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001911 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001912 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001913 }
1914
1915 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001916 }
1917
1918 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001919}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001920
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001921QualType ASTContext::getObjCFastEnumerationStateType()
1922{
1923 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001924 ObjCFastEnumerationStateTypeDecl =
1925 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1926 &Idents.get("__objcFastEnumerationState"));
1927
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001928 QualType FieldTypes[] = {
1929 UnsignedLongTy,
1930 getPointerType(ObjCIdType),
1931 getPointerType(UnsignedLongTy),
1932 getConstantArrayType(UnsignedLongTy,
1933 llvm::APInt(32, 5), ArrayType::Normal, 0)
1934 };
1935
Douglas Gregor44b43212008-12-11 16:49:14 +00001936 for (size_t i = 0; i < 4; ++i) {
1937 FieldDecl *Field = FieldDecl::Create(*this,
1938 ObjCFastEnumerationStateTypeDecl,
1939 SourceLocation(), 0,
1940 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001941 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001942 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001943 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001944
Douglas Gregor44b43212008-12-11 16:49:14 +00001945 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001946 }
1947
1948 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1949}
1950
Anders Carlssone8c49532007-10-29 06:33:42 +00001951// This returns true if a type has been typedefed to BOOL:
1952// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001953static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001954 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00001955 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1956 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001957
1958 return false;
1959}
1960
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001961/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001962/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001963int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001964 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001965
1966 // Make all integer and enum types at least as large as an int
1967 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001968 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001969 // Treat arrays as pointers, since that's how they're passed in.
1970 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001971 sz = getTypeSize(VoidPtrTy);
1972 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001973}
1974
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001975/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001976/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001977void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001978 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001979 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001980 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001981 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001982 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001983 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001984 // Compute size of all parameters.
1985 // Start with computing size of a pointer in number of bytes.
1986 // FIXME: There might(should) be a better way of doing this computation!
1987 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001988 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001989 // The first two arguments (self and _cmd) are pointers; account for
1990 // their size.
1991 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00001992 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1993 E = Decl->param_end(); PI != E; ++PI) {
1994 QualType PType = (*PI)->getType();
1995 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001996 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001997 ParmOffset += sz;
1998 }
1999 S += llvm::utostr(ParmOffset);
2000 S += "@0:";
2001 S += llvm::utostr(PtrSize);
2002
2003 // Argument types.
2004 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002005 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2006 E = Decl->param_end(); PI != E; ++PI) {
2007 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002008 QualType PType = PVDecl->getOriginalType();
2009 if (const ArrayType *AT =
2010 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
2011 // Use array's original type only if it has known number of
2012 // elements.
2013 if (!dyn_cast<ConstantArrayType>(AT))
2014 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002015 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002016 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002017 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002018 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002019 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002020 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002021 }
2022}
2023
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002024/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002025/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002026/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2027/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002028/// Property attributes are stored as a comma-delimited C string. The simple
2029/// attributes readonly and bycopy are encoded as single characters. The
2030/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2031/// encoded as single characters, followed by an identifier. Property types
2032/// are also encoded as a parametrized attribute. The characters used to encode
2033/// these attributes are defined by the following enumeration:
2034/// @code
2035/// enum PropertyAttributes {
2036/// kPropertyReadOnly = 'R', // property is read-only.
2037/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2038/// kPropertyByref = '&', // property is a reference to the value last assigned
2039/// kPropertyDynamic = 'D', // property is dynamic
2040/// kPropertyGetter = 'G', // followed by getter selector name
2041/// kPropertySetter = 'S', // followed by setter selector name
2042/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2043/// kPropertyType = 't' // followed by old-style type encoding.
2044/// kPropertyWeak = 'W' // 'weak' property
2045/// kPropertyStrong = 'P' // property GC'able
2046/// kPropertyNonAtomic = 'N' // property non-atomic
2047/// };
2048/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002049void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2050 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002051 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002052 // Collect information from the property implementation decl(s).
2053 bool Dynamic = false;
2054 ObjCPropertyImplDecl *SynthesizePID = 0;
2055
2056 // FIXME: Duplicated code due to poor abstraction.
2057 if (Container) {
2058 if (const ObjCCategoryImplDecl *CID =
2059 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2060 for (ObjCCategoryImplDecl::propimpl_iterator
2061 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2062 ObjCPropertyImplDecl *PID = *i;
2063 if (PID->getPropertyDecl() == PD) {
2064 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2065 Dynamic = true;
2066 } else {
2067 SynthesizePID = PID;
2068 }
2069 }
2070 }
2071 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002072 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002073 for (ObjCCategoryImplDecl::propimpl_iterator
2074 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2075 ObjCPropertyImplDecl *PID = *i;
2076 if (PID->getPropertyDecl() == PD) {
2077 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2078 Dynamic = true;
2079 } else {
2080 SynthesizePID = PID;
2081 }
2082 }
2083 }
2084 }
2085 }
2086
2087 // FIXME: This is not very efficient.
2088 S = "T";
2089
2090 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002091 // GCC has some special rules regarding encoding of properties which
2092 // closely resembles encoding of ivars.
2093 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
2094 true /* outermost type */,
2095 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002096
2097 if (PD->isReadOnly()) {
2098 S += ",R";
2099 } else {
2100 switch (PD->getSetterKind()) {
2101 case ObjCPropertyDecl::Assign: break;
2102 case ObjCPropertyDecl::Copy: S += ",C"; break;
2103 case ObjCPropertyDecl::Retain: S += ",&"; break;
2104 }
2105 }
2106
2107 // It really isn't clear at all what this means, since properties
2108 // are "dynamic by default".
2109 if (Dynamic)
2110 S += ",D";
2111
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002112 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2113 S += ",N";
2114
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002115 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2116 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002117 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002118 }
2119
2120 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2121 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002122 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002123 }
2124
2125 if (SynthesizePID) {
2126 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2127 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002128 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002129 }
2130
2131 // FIXME: OBJCGC: weak & strong
2132}
2133
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002134/// getLegacyIntegralTypeEncoding -
2135/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002136/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002137/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2138///
2139void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2140 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2141 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002142 if (BT->getKind() == BuiltinType::ULong &&
2143 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002144 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002145 else
2146 if (BT->getKind() == BuiltinType::Long &&
2147 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002148 PointeeTy = IntTy;
2149 }
2150 }
2151}
2152
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002153void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002154 FieldDecl *Field) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002155 // We follow the behavior of gcc, expanding structures which are
2156 // directly pointed to, and expanding embedded structures. Note that
2157 // these rules are sufficient to prevent recursive encoding of the
2158 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002159 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2160 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002161}
2162
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002163static void EncodeBitField(const ASTContext *Context, std::string& S,
2164 FieldDecl *FD) {
2165 const Expr *E = FD->getBitWidth();
2166 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2167 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2168 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2169 S += 'b';
2170 S += llvm::utostr(N);
2171}
2172
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002173void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2174 bool ExpandPointedToStructures,
2175 bool ExpandStructures,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002176 FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002177 bool OutermostType,
2178 bool EncodingProperty) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00002179 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002180 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002181 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002182 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002183 else {
2184 char encoding;
2185 switch (BT->getKind()) {
2186 default: assert(0 && "Unhandled builtin type kind");
2187 case BuiltinType::Void: encoding = 'v'; break;
2188 case BuiltinType::Bool: encoding = 'B'; break;
2189 case BuiltinType::Char_U:
2190 case BuiltinType::UChar: encoding = 'C'; break;
2191 case BuiltinType::UShort: encoding = 'S'; break;
2192 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002193 case BuiltinType::ULong:
2194 encoding =
2195 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2196 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002197 case BuiltinType::ULongLong: encoding = 'Q'; break;
2198 case BuiltinType::Char_S:
2199 case BuiltinType::SChar: encoding = 'c'; break;
2200 case BuiltinType::Short: encoding = 's'; break;
2201 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002202 case BuiltinType::Long:
2203 encoding =
2204 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2205 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002206 case BuiltinType::LongLong: encoding = 'q'; break;
2207 case BuiltinType::Float: encoding = 'f'; break;
2208 case BuiltinType::Double: encoding = 'd'; break;
2209 case BuiltinType::LongDouble: encoding = 'd'; break;
2210 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002211
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002212 S += encoding;
2213 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002214 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002215 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002216 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2217 ExpandPointedToStructures,
2218 ExpandStructures, FD);
2219 if (FD || EncodingProperty) {
2220 // Note that we do extended encoding of protocol qualifer list
2221 // Only when doing ivar or property encoding.
2222 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2223 S += '"';
2224 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2225 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2226 S += '<';
2227 S += Proto->getNameAsString();
2228 S += '>';
2229 }
2230 S += '"';
2231 }
2232 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002233 }
2234 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002235 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002236 bool isReadOnly = false;
2237 // For historical/compatibility reasons, the read-only qualifier of the
2238 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2239 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2240 // Also, do not emit the 'r' for anything but the outermost type!
2241 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2242 if (OutermostType && T.isConstQualified()) {
2243 isReadOnly = true;
2244 S += 'r';
2245 }
2246 }
2247 else if (OutermostType) {
2248 QualType P = PointeeTy;
2249 while (P->getAsPointerType())
2250 P = P->getAsPointerType()->getPointeeType();
2251 if (P.isConstQualified()) {
2252 isReadOnly = true;
2253 S += 'r';
2254 }
2255 }
2256 if (isReadOnly) {
2257 // Another legacy compatibility encoding. Some ObjC qualifier and type
2258 // combinations need to be rearranged.
2259 // Rewrite "in const" from "nr" to "rn"
2260 const char * s = S.c_str();
2261 int len = S.length();
2262 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2263 std::string replace = "rn";
2264 S.replace(S.end()-2, S.end(), replace);
2265 }
2266 }
Steve Naroff389bf462009-02-12 17:52:19 +00002267 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002268 S += '@';
2269 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002270 }
2271 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002272 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002273 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002274 // Another historical/compatibility reason.
2275 // We encode the underlying type which comes out as
2276 // {...};
2277 S += '^';
2278 getObjCEncodingForTypeImpl(PointeeTy, S,
2279 false, ExpandPointedToStructures,
2280 NULL);
2281 return;
2282 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002283 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002284 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002285 const ObjCInterfaceType *OIT =
2286 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002287 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002288 S += '"';
2289 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002290 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2291 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2292 S += '<';
2293 S += Proto->getNameAsString();
2294 S += '>';
2295 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002296 S += '"';
2297 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002298 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002299 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002300 S += '#';
2301 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002302 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002303 S += ':';
2304 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002305 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002306
2307 if (PointeeTy->isCharType()) {
2308 // char pointer types should be encoded as '*' unless it is a
2309 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002310 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002311 S += '*';
2312 return;
2313 }
2314 }
2315
2316 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002317 getLegacyIntegralTypeEncoding(PointeeTy);
2318
2319 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002320 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002321 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002322 } else if (const ArrayType *AT =
2323 // Ignore type qualifiers etc.
2324 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002325 if (isa<IncompleteArrayType>(AT)) {
2326 // Incomplete arrays are encoded as a pointer to the array element.
2327 S += '^';
2328
2329 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2330 false, ExpandStructures, FD);
2331 } else {
2332 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002333
Anders Carlsson559a8332009-02-22 01:38:57 +00002334 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2335 S += llvm::utostr(CAT->getSize().getZExtValue());
2336 else {
2337 //Variable length arrays are encoded as a regular array with 0 elements.
2338 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2339 S += '0';
2340 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002341
Anders Carlsson559a8332009-02-22 01:38:57 +00002342 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2343 false, ExpandStructures, FD);
2344 S += ']';
2345 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002346 } else if (T->getAsFunctionType()) {
2347 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002348 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002349 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002350 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002351 // Anonymous structures print as '?'
2352 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2353 S += II->getName();
2354 } else {
2355 S += '?';
2356 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002357 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002358 S += '=';
Douglas Gregor44b43212008-12-11 16:49:14 +00002359 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2360 FieldEnd = RDecl->field_end();
2361 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002362 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002363 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002364 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002365 S += '"';
2366 }
2367
2368 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002369 if (Field->isBitField()) {
2370 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2371 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002372 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002373 QualType qt = Field->getType();
2374 getLegacyIntegralTypeEncoding(qt);
2375 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002376 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002377 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002378 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002379 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002380 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002381 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002382 if (FD && FD->isBitField())
2383 EncodeBitField(this, S, FD);
2384 else
2385 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002386 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002387 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002388 } else if (T->isObjCInterfaceType()) {
2389 // @encode(class_name)
2390 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2391 S += '{';
2392 const IdentifierInfo *II = OI->getIdentifier();
2393 S += II->getName();
2394 S += '=';
2395 std::vector<FieldDecl*> RecFields;
2396 CollectObjCIvars(OI, RecFields);
2397 for (unsigned int i = 0; i != RecFields.size(); i++) {
2398 if (RecFields[i]->isBitField())
2399 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2400 RecFields[i]);
2401 else
2402 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2403 FD);
2404 }
2405 S += '}';
2406 }
2407 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002408 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002409}
2410
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002411void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002412 std::string& S) const {
2413 if (QT & Decl::OBJC_TQ_In)
2414 S += 'n';
2415 if (QT & Decl::OBJC_TQ_Inout)
2416 S += 'N';
2417 if (QT & Decl::OBJC_TQ_Out)
2418 S += 'o';
2419 if (QT & Decl::OBJC_TQ_Bycopy)
2420 S += 'O';
2421 if (QT & Decl::OBJC_TQ_Byref)
2422 S += 'R';
2423 if (QT & Decl::OBJC_TQ_Oneway)
2424 S += 'V';
2425}
2426
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002427void ASTContext::setBuiltinVaListType(QualType T)
2428{
2429 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2430
2431 BuiltinVaListType = T;
2432}
2433
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002434void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002435{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002436 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002437
2438 // typedef struct objc_object *id;
2439 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002440 // User error - caller will issue diagnostics.
2441 if (!ptr)
2442 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002443 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002444 // User error - caller will issue diagnostics.
2445 if (!rec)
2446 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002447 IdStructType = rec;
2448}
2449
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002450void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002451{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002452 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002453
2454 // typedef struct objc_selector *SEL;
2455 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002456 if (!ptr)
2457 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002458 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002459 if (!rec)
2460 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002461 SelStructType = rec;
2462}
2463
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002464void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002465{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002466 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002467}
2468
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002469void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002470{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002471 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002472
2473 // typedef struct objc_class *Class;
2474 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2475 assert(ptr && "'Class' incorrectly typed");
2476 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2477 assert(rec && "'Class' incorrectly typed");
2478 ClassStructType = rec;
2479}
2480
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002481void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2482 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002483 "'NSConstantString' type already set!");
2484
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002485 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002486}
2487
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002488/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002489/// TargetInfo, produce the corresponding type. The unsigned @p Type
2490/// is actually a value of type @c TargetInfo::IntType.
2491QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002492 switch (Type) {
2493 case TargetInfo::NoInt: return QualType();
2494 case TargetInfo::SignedShort: return ShortTy;
2495 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2496 case TargetInfo::SignedInt: return IntTy;
2497 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2498 case TargetInfo::SignedLong: return LongTy;
2499 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2500 case TargetInfo::SignedLongLong: return LongLongTy;
2501 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2502 }
2503
2504 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002505 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002506}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002507
2508//===----------------------------------------------------------------------===//
2509// Type Predicates.
2510//===----------------------------------------------------------------------===//
2511
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002512/// isObjCNSObjectType - Return true if this is an NSObject object using
2513/// NSObject attribute on a c-style pointer type.
2514/// FIXME - Make it work directly on types.
2515///
2516bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2517 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2518 if (TypedefDecl *TD = TDT->getDecl())
2519 if (TD->getAttr<ObjCNSObjectAttr>())
2520 return true;
2521 }
2522 return false;
2523}
2524
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002525/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2526/// to an object type. This includes "id" and "Class" (two 'special' pointers
2527/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2528/// ID type).
2529bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002530 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002531 return true;
2532
Steve Naroff6ae98502008-10-21 18:24:04 +00002533 // Blocks are objects.
2534 if (Ty->isBlockPointerType())
2535 return true;
2536
2537 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002538 if (!Ty->isPointerType())
2539 return false;
2540
2541 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2542 // pointer types. This looks for the typedef specifically, not for the
2543 // underlying type.
Eli Friedman5fdeae12009-03-22 23:00:19 +00002544 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2545 Ty.getUnqualifiedType() == getObjCClassType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002546 return true;
2547
2548 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002549 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2550 return true;
2551
2552 // If is has NSObject attribute, OK as well.
2553 return isObjCNSObjectType(Ty);
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002554}
2555
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002556/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2557/// garbage collection attribute.
2558///
2559QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002560 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002561 if (getLangOptions().ObjC1 &&
2562 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002563 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002564 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002565 // (or pointers to them) be treated as though they were declared
2566 // as __strong.
2567 if (GCAttrs == QualType::GCNone) {
2568 if (isObjCObjectPointerType(Ty))
2569 GCAttrs = QualType::Strong;
2570 else if (Ty->isPointerType())
2571 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2572 }
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002573 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002574 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002575}
2576
Chris Lattner6ac46a42008-04-07 06:51:04 +00002577//===----------------------------------------------------------------------===//
2578// Type Compatibility Testing
2579//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002580
Steve Naroff1c7d0672008-09-04 15:10:53 +00002581/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002582/// block types. Types must be strictly compatible here. For example,
2583/// C unfortunately doesn't produce an error for the following:
2584///
2585/// int (*emptyArgFunc)();
2586/// int (*intArgList)(int) = emptyArgFunc;
2587///
2588/// For blocks, we will produce an error for the following (similar to C++):
2589///
2590/// int (^emptyArgBlock)();
2591/// int (^intArgBlock)(int) = emptyArgBlock;
2592///
2593/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2594///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002595bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002596 const FunctionType *lbase = lhs->getAsFunctionType();
2597 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002598 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2599 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Steve Naroffc0febd52008-12-10 17:49:55 +00002600 if (lproto && rproto)
2601 return !mergeTypes(lhs, rhs).isNull();
2602 return false;
Steve Naroff1c7d0672008-09-04 15:10:53 +00002603}
2604
Chris Lattner6ac46a42008-04-07 06:51:04 +00002605/// areCompatVectorTypes - Return true if the two specified vector types are
2606/// compatible.
2607static bool areCompatVectorTypes(const VectorType *LHS,
2608 const VectorType *RHS) {
2609 assert(LHS->isCanonical() && RHS->isCanonical());
2610 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002611 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002612}
2613
Eli Friedman3d815e72008-08-22 00:56:42 +00002614/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002615/// compatible for assignment from RHS to LHS. This handles validation of any
2616/// protocol qualifiers on the LHS or RHS.
2617///
Eli Friedman3d815e72008-08-22 00:56:42 +00002618bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2619 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002620 // Verify that the base decls are compatible: the RHS must be a subclass of
2621 // the LHS.
2622 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2623 return false;
2624
2625 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2626 // protocol qualified at all, then we are good.
2627 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2628 return true;
2629
2630 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2631 // isn't a superset.
2632 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2633 return true; // FIXME: should return false!
2634
2635 // Finally, we must have two protocol-qualified interfaces.
2636 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2637 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002638
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002639 // All LHS protocols must have a presence on the RHS.
2640 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002641
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002642 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2643 LHSPE = LHSP->qual_end();
2644 LHSPI != LHSPE; LHSPI++) {
2645 bool RHSImplementsProtocol = false;
2646
2647 // If the RHS doesn't implement the protocol on the left, the types
2648 // are incompatible.
2649 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2650 RHSPE = RHSP->qual_end();
2651 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2652 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2653 RHSImplementsProtocol = true;
2654 }
2655 // FIXME: For better diagnostics, consider passing back the protocol name.
2656 if (!RHSImplementsProtocol)
2657 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002658 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002659 // The RHS implements all protocols listed on the LHS.
2660 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002661}
2662
Steve Naroff389bf462009-02-12 17:52:19 +00002663bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2664 // get the "pointed to" types
2665 const PointerType *LHSPT = LHS->getAsPointerType();
2666 const PointerType *RHSPT = RHS->getAsPointerType();
2667
2668 if (!LHSPT || !RHSPT)
2669 return false;
2670
2671 QualType lhptee = LHSPT->getPointeeType();
2672 QualType rhptee = RHSPT->getPointeeType();
2673 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2674 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2675 // ID acts sort of like void* for ObjC interfaces
2676 if (LHSIface && isObjCIdStructType(rhptee))
2677 return true;
2678 if (RHSIface && isObjCIdStructType(lhptee))
2679 return true;
2680 if (!LHSIface || !RHSIface)
2681 return false;
2682 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2683 canAssignObjCInterfaces(RHSIface, LHSIface);
2684}
2685
Steve Naroffec0550f2007-10-15 20:41:53 +00002686/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2687/// both shall have the identically qualified version of a compatible type.
2688/// C99 6.2.7p1: Two types have compatible types if their types are the
2689/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002690bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2691 return !mergeTypes(LHS, RHS).isNull();
2692}
2693
2694QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2695 const FunctionType *lbase = lhs->getAsFunctionType();
2696 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002697 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2698 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002699 bool allLTypes = true;
2700 bool allRTypes = true;
2701
2702 // Check return type
2703 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2704 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002705 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2706 allLTypes = false;
2707 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2708 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002709
2710 if (lproto && rproto) { // two C99 style function prototypes
2711 unsigned lproto_nargs = lproto->getNumArgs();
2712 unsigned rproto_nargs = rproto->getNumArgs();
2713
2714 // Compatible functions must have the same number of arguments
2715 if (lproto_nargs != rproto_nargs)
2716 return QualType();
2717
2718 // Variadic and non-variadic functions aren't compatible
2719 if (lproto->isVariadic() != rproto->isVariadic())
2720 return QualType();
2721
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002722 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2723 return QualType();
2724
Eli Friedman3d815e72008-08-22 00:56:42 +00002725 // Check argument compatibility
2726 llvm::SmallVector<QualType, 10> types;
2727 for (unsigned i = 0; i < lproto_nargs; i++) {
2728 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2729 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2730 QualType argtype = mergeTypes(largtype, rargtype);
2731 if (argtype.isNull()) return QualType();
2732 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002733 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2734 allLTypes = false;
2735 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2736 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002737 }
2738 if (allLTypes) return lhs;
2739 if (allRTypes) return rhs;
2740 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002741 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002742 }
2743
2744 if (lproto) allRTypes = false;
2745 if (rproto) allLTypes = false;
2746
Douglas Gregor72564e72009-02-26 23:50:07 +00002747 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002748 if (proto) {
2749 if (proto->isVariadic()) return QualType();
2750 // Check that the types are compatible with the types that
2751 // would result from default argument promotions (C99 6.7.5.3p15).
2752 // The only types actually affected are promotable integer
2753 // types and floats, which would be passed as a different
2754 // type depending on whether the prototype is visible.
2755 unsigned proto_nargs = proto->getNumArgs();
2756 for (unsigned i = 0; i < proto_nargs; ++i) {
2757 QualType argTy = proto->getArgType(i);
2758 if (argTy->isPromotableIntegerType() ||
2759 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2760 return QualType();
2761 }
2762
2763 if (allLTypes) return lhs;
2764 if (allRTypes) return rhs;
2765 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002766 proto->getNumArgs(), lproto->isVariadic(),
2767 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002768 }
2769
2770 if (allLTypes) return lhs;
2771 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002772 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002773}
2774
2775QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002776 // C++ [expr]: If an expression initially has the type "reference to T", the
2777 // type is adjusted to "T" prior to any further analysis, the expression
2778 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002779 // expression is an lvalue unless the reference is an rvalue reference and
2780 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00002781 // FIXME: C++ shouldn't be going through here! The rules are different
2782 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002783 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2784 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00002785 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002786 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002787 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002788 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002789
Eli Friedman3d815e72008-08-22 00:56:42 +00002790 QualType LHSCan = getCanonicalType(LHS),
2791 RHSCan = getCanonicalType(RHS);
2792
2793 // If two types are identical, they are compatible.
2794 if (LHSCan == RHSCan)
2795 return LHS;
2796
2797 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002798 // Note that we handle extended qualifiers later, in the
2799 // case for ExtQualType.
2800 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00002801 return QualType();
2802
2803 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2804 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2805
Chris Lattner1adb8832008-01-14 05:45:46 +00002806 // We want to consider the two function types to be the same for these
2807 // comparisons, just force one to the other.
2808 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2809 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002810
2811 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002812 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2813 LHSClass = Type::ConstantArray;
2814 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2815 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002816
Nate Begeman213541a2008-04-18 23:10:10 +00002817 // Canonicalize ExtVector -> Vector.
2818 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2819 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002820
Chris Lattnerb0489812008-04-07 06:38:24 +00002821 // Consider qualified interfaces and interfaces the same.
2822 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2823 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002824
Chris Lattnera36a61f2008-04-07 05:43:21 +00002825 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002826 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002827 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2828 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2829
2830 // ID acts sort of like void* for ObjC interfaces
2831 if (LHSIface && isObjCIdStructType(RHS))
2832 return LHS;
2833 if (RHSIface && isObjCIdStructType(LHS))
2834 return RHS;
2835
Steve Naroffbc76dd02008-12-10 22:14:21 +00002836 // ID is compatible with all qualified id types.
2837 if (LHS->isObjCQualifiedIdType()) {
2838 if (const PointerType *PT = RHS->getAsPointerType()) {
2839 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002840 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002841 return LHS;
2842 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2843 // Unfortunately, this API is part of Sema (which we don't have access
2844 // to. Need to refactor. The following check is insufficient, since we
2845 // need to make sure the class implements the protocol.
2846 if (pType->isObjCInterfaceType())
2847 return LHS;
2848 }
2849 }
2850 if (RHS->isObjCQualifiedIdType()) {
2851 if (const PointerType *PT = LHS->getAsPointerType()) {
2852 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002853 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002854 return RHS;
2855 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2856 // Unfortunately, this API is part of Sema (which we don't have access
2857 // to. Need to refactor. The following check is insufficient, since we
2858 // need to make sure the class implements the protocol.
2859 if (pType->isObjCInterfaceType())
2860 return RHS;
2861 }
2862 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002863 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2864 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002865 if (const EnumType* ETy = LHS->getAsEnumType()) {
2866 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2867 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002868 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002869 if (const EnumType* ETy = RHS->getAsEnumType()) {
2870 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2871 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002872 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002873
Eli Friedman3d815e72008-08-22 00:56:42 +00002874 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002875 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002876
Steve Naroff4a746782008-01-09 22:43:08 +00002877 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002878 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00002879#define TYPE(Class, Base)
2880#define ABSTRACT_TYPE(Class, Base)
2881#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2882#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2883#include "clang/AST/TypeNodes.def"
2884 assert(false && "Non-canonical and dependent types shouldn't get here");
2885 return QualType();
2886
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002887 case Type::LValueReference:
2888 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00002889 case Type::MemberPointer:
2890 assert(false && "C++ should never be in mergeTypes");
2891 return QualType();
2892
2893 case Type::IncompleteArray:
2894 case Type::VariableArray:
2895 case Type::FunctionProto:
2896 case Type::ExtVector:
2897 case Type::ObjCQualifiedInterface:
2898 assert(false && "Types are eliminated above");
2899 return QualType();
2900
Chris Lattner1adb8832008-01-14 05:45:46 +00002901 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002902 {
2903 // Merge two pointer types, while trying to preserve typedef info
2904 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2905 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2906 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2907 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002908 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2909 return LHS;
2910 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2911 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002912 return getPointerType(ResultType);
2913 }
Steve Naroffc0febd52008-12-10 17:49:55 +00002914 case Type::BlockPointer:
2915 {
2916 // Merge two block pointer types, while trying to preserve typedef info
2917 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2918 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2919 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2920 if (ResultType.isNull()) return QualType();
2921 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2922 return LHS;
2923 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2924 return RHS;
2925 return getBlockPointerType(ResultType);
2926 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002927 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002928 {
2929 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2930 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2931 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2932 return QualType();
2933
2934 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2935 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2936 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2937 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002938 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2939 return LHS;
2940 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2941 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002942 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2943 ArrayType::ArraySizeModifier(), 0);
2944 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2945 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002946 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2947 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00002948 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2949 return LHS;
2950 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2951 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002952 if (LVAT) {
2953 // FIXME: This isn't correct! But tricky to implement because
2954 // the array's size has to be the size of LHS, but the type
2955 // has to be different.
2956 return LHS;
2957 }
2958 if (RVAT) {
2959 // FIXME: This isn't correct! But tricky to implement because
2960 // the array's size has to be the size of RHS, but the type
2961 // has to be different.
2962 return RHS;
2963 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002964 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2965 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00002966 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002967 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002968 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002969 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00002970 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00002971 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00002972 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00002973 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
2974 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002975 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002976 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002977 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002978 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00002979 case Type::Complex:
2980 // Distinct complex types are incompatible.
2981 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002982 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002983 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00002984 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2985 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00002986 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00002987 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002988 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002989 // FIXME: This should be type compatibility, e.g. whether
2990 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00002991 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2992 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2993 if (LHSIface && RHSIface &&
2994 canAssignObjCInterfaces(LHSIface, RHSIface))
2995 return LHS;
2996
Eli Friedman3d815e72008-08-22 00:56:42 +00002997 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00002998 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00002999 case Type::ObjCQualifiedId:
3000 // Distinct qualified id's are not compatible.
3001 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003002 case Type::FixedWidthInt:
3003 // Distinct fixed-width integers are not compatible.
3004 return QualType();
3005 case Type::ObjCQualifiedClass:
3006 // Distinct qualified classes are not compatible.
3007 return QualType();
3008 case Type::ExtQual:
3009 // FIXME: ExtQual types can be compatible even if they're not
3010 // identical!
3011 return QualType();
3012 // First attempt at an implementation, but I'm not really sure it's
3013 // right...
3014#if 0
3015 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3016 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3017 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3018 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3019 return QualType();
3020 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3021 LHSBase = QualType(LQual->getBaseType(), 0);
3022 RHSBase = QualType(RQual->getBaseType(), 0);
3023 ResultType = mergeTypes(LHSBase, RHSBase);
3024 if (ResultType.isNull()) return QualType();
3025 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3026 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3027 return LHS;
3028 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3029 return RHS;
3030 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3031 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3032 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3033 return ResultType;
3034#endif
Steve Naroffec0550f2007-10-15 20:41:53 +00003035 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003036
3037 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003038}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003039
Chris Lattner5426bf62008-04-07 07:01:58 +00003040//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003041// Integer Predicates
3042//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003043
Eli Friedmanad74a752008-06-28 06:23:08 +00003044unsigned ASTContext::getIntWidth(QualType T) {
3045 if (T == BoolTy)
3046 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003047 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3048 return FWIT->getWidth();
3049 }
3050 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003051 return (unsigned)getTypeSize(T);
3052}
3053
3054QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3055 assert(T->isSignedIntegerType() && "Unexpected type");
3056 if (const EnumType* ETy = T->getAsEnumType())
3057 T = ETy->getDecl()->getIntegerType();
3058 const BuiltinType* BTy = T->getAsBuiltinType();
3059 assert (BTy && "Unexpected signed integer type");
3060 switch (BTy->getKind()) {
3061 case BuiltinType::Char_S:
3062 case BuiltinType::SChar:
3063 return UnsignedCharTy;
3064 case BuiltinType::Short:
3065 return UnsignedShortTy;
3066 case BuiltinType::Int:
3067 return UnsignedIntTy;
3068 case BuiltinType::Long:
3069 return UnsignedLongTy;
3070 case BuiltinType::LongLong:
3071 return UnsignedLongLongTy;
3072 default:
3073 assert(0 && "Unexpected signed integer type");
3074 return QualType();
3075 }
3076}
3077
3078
3079//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00003080// Serialization Support
3081//===----------------------------------------------------------------------===//
3082
Chris Lattnera9376d42009-03-28 03:45:20 +00003083enum {
3084 BasicMetadataBlock = 1,
3085 ASTContextBlock = 2,
3086 DeclsBlock = 3
3087};
3088
3089void ASTContext::EmitAll(llvm::Serializer &S) const {
3090 // ===---------------------------------------------------===/
3091 // Serialize the "Translation Unit" metadata.
3092 // ===---------------------------------------------------===/
3093
3094 // Emit ASTContext.
3095 S.EnterBlock(ASTContextBlock);
3096 S.EmitOwnedPtr(this);
3097 S.ExitBlock(); // exit "ASTContextBlock"
3098
3099 S.EnterBlock(BasicMetadataBlock);
3100
3101 // Block for SourceManager and Target. Allows easy skipping
3102 // around to the block for the Selectors during deserialization.
3103 S.EnterBlock();
3104
3105 // Emit the SourceManager.
3106 S.Emit(getSourceManager());
3107
3108 // Emit the Target.
3109 S.EmitPtr(&Target);
3110 S.EmitCStr(Target.getTargetTriple());
3111
3112 S.ExitBlock(); // exit "SourceManager and Target Block"
3113
3114 // Emit the Selectors.
3115 S.Emit(Selectors);
3116
3117 // Emit the Identifier Table.
3118 S.Emit(Idents);
3119
3120 S.ExitBlock(); // exit "BasicMetadataBlock"
3121}
3122
3123
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003124/// Emit - Serialize an ASTContext object to Bitcode.
3125void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003126 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00003127 S.EmitRef(SourceMgr);
3128 S.EmitRef(Target);
3129 S.EmitRef(Idents);
3130 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003131
Ted Kremenekfee04522007-10-31 22:44:07 +00003132 // Emit the size of the type vector so that we can reserve that size
3133 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00003134 S.EmitInt(Types.size());
3135
Ted Kremenek03ed4402007-11-13 22:02:55 +00003136 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3137 I!=E;++I)
3138 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00003139
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003140 S.EmitOwnedPtr(TUDecl);
3141
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003142 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003143}
3144
Chris Lattnera9376d42009-03-28 03:45:20 +00003145ASTContext* ASTContext::CreateAll(llvm::Deserializer &Dezr,
3146 FileManager &FMgr) {
3147 // ===---------------------------------------------------===/
3148 // Deserialize the "Translation Unit" metadata.
3149 // ===---------------------------------------------------===/
3150
3151 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
3152 // (which will appear earlier) and record its location.
3153
3154 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
3155 assert (FoundBlock);
3156
3157 llvm::Deserializer::Location ASTContextBlockLoc =
3158 Dezr.getCurrentBlockLocation();
3159
3160 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
3161 assert (FoundBlock);
3162
3163 // Read the SourceManager.
3164 SourceManager::CreateAndRegister(Dezr, FMgr);
3165
3166 { // Read the TargetInfo.
3167 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
3168 char* triple = Dezr.ReadCStr(NULL,0,true);
3169 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
3170 delete [] triple;
3171 }
3172
3173 // For Selectors, we must read the identifier table first because the
3174 // SelectorTable depends on the identifiers being already deserialized.
3175 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
3176 Dezr.SkipBlock();
3177
3178 // Read the identifier table.
3179 IdentifierTable::CreateAndRegister(Dezr);
3180
3181 // Now jump back and read the selectors.
3182 Dezr.JumpTo(SelectorBlkLoc);
3183 SelectorTable::CreateAndRegister(Dezr);
3184
3185 // Now jump back to ASTContextBlock and read the ASTContext.
3186 Dezr.JumpTo(ASTContextBlockLoc);
3187 return Dezr.ReadOwnedPtr<ASTContext>();
3188}
3189
Ted Kremenek0f84c002007-11-13 00:25:37 +00003190ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003191
3192 // Read the language options.
3193 LangOptions LOpts;
3194 LOpts.Read(D);
3195
Ted Kremenekfee04522007-10-31 22:44:07 +00003196 SourceManager &SM = D.ReadRef<SourceManager>();
3197 TargetInfo &t = D.ReadRef<TargetInfo>();
3198 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3199 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00003200
Ted Kremenekfee04522007-10-31 22:44:07 +00003201 unsigned size_reserve = D.ReadInt();
3202
Douglas Gregor2e1cd422008-11-17 14:58:09 +00003203 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3204 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00003205
Ted Kremenek03ed4402007-11-13 22:02:55 +00003206 for (unsigned i = 0; i < size_reserve; ++i)
3207 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00003208
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003209 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3210
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003211 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00003212
3213 return A;
3214}