blob: bb1795472781e1d40dec49f50e684e039699724b [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"
Chris Lattner557c5b12009-03-28 04:27:18 +000026#include "llvm/Support/MemoryBuffer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027using namespace clang;
28
29enum FloatingRank {
30 FloatRank, DoubleRank, LongDoubleRank
31};
32
Chris Lattner61710852008-10-05 17:34:18 +000033ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
34 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000035 IdentifierTable &idents, SelectorTable &sels,
Steve Naroffc0ac4922009-01-27 23:20:32 +000036 bool FreeMem, unsigned size_reserve) :
Douglas Gregorab452ba2009-03-26 23:50:42 +000037 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
38 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Chris Lattnered0e4972009-03-28 01:44:40 +000039 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels) {
Daniel Dunbare91593e2008-08-11 04:54:23 +000040 if (size_reserve > 0) Types.reserve(size_reserve);
41 InitBuiltinTypes();
Chris Lattner7644f072009-03-13 22:38:49 +000042 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.NoBuiltin);
Daniel Dunbare91593e2008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
44}
45
Reid Spencer5f016e22007-07-11 17:01:13 +000046ASTContext::~ASTContext() {
47 // Deallocate all the types.
48 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000049 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000050 Types.pop_back();
51 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000052
Nuno Lopesb74668e2008-12-17 22:30:25 +000053 {
54 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
55 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
56 while (I != E) {
57 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
58 delete R;
59 }
60 }
61
62 {
63 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
64 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
65 while (I != E) {
66 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
67 delete R;
68 }
69 }
70
71 {
72 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator
73 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
74 while (I != E) {
75 RecordDecl *R = const_cast<RecordDecl*>((I++)->second);
76 R->Destroy(*this);
77 }
78 }
79
Douglas Gregorab452ba2009-03-26 23:50:42 +000080 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000081 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
82 NNS = NestedNameSpecifiers.begin(),
83 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregore7dcd782009-03-27 23:25:45 +000084 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000085 /* Increment in loop */)
86 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000087
88 if (GlobalNestedNameSpecifier)
89 GlobalNestedNameSpecifier->Destroy(*this);
90
Eli Friedmanb26153c2008-05-27 03:08:09 +000091 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000092}
93
94void ASTContext::PrintStats() const {
95 fprintf(stderr, "*** AST Context Stats:\n");
96 fprintf(stderr, " %d types total.\n", (int)Types.size());
97 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +000098 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +000099 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
100 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
101
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000103 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
104 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000105 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000106
107 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
108 Type *T = Types[i];
109 if (isa<BuiltinType>(T))
110 ++NumBuiltin;
111 else if (isa<PointerType>(T))
112 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000113 else if (isa<BlockPointerType>(T))
114 ++NumBlockPointer;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000115 else if (isa<LValueReferenceType>(T))
116 ++NumLValueReference;
117 else if (isa<RValueReferenceType>(T))
118 ++NumRValueReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000119 else if (isa<MemberPointerType>(T))
120 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000121 else if (isa<ComplexType>(T))
122 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 else if (isa<ArrayType>(T))
124 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000125 else if (isa<VectorType>(T))
126 ++NumVector;
Douglas Gregor72564e72009-02-26 23:50:07 +0000127 else if (isa<FunctionNoProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 ++NumFunctionNP;
Douglas Gregor72564e72009-02-26 23:50:07 +0000129 else if (isa<FunctionProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 ++NumFunctionP;
131 else if (isa<TypedefType>(T))
132 ++NumTypeName;
133 else if (TagType *TT = dyn_cast<TagType>(T)) {
134 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000135 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000137 case TagDecl::TK_struct: ++NumTagStruct; break;
138 case TagDecl::TK_union: ++NumTagUnion; break;
139 case TagDecl::TK_class: ++NumTagClass; break;
140 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000142 } else if (isa<ObjCInterfaceType>(T))
143 ++NumObjCInterfaces;
144 else if (isa<ObjCQualifiedInterfaceType>(T))
145 ++NumObjCQualifiedInterfaces;
146 else if (isa<ObjCQualifiedIdType>(T))
147 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000148 else if (isa<TypeOfType>(T))
149 ++NumTypeOfTypes;
Douglas Gregor72564e72009-02-26 23:50:07 +0000150 else if (isa<TypeOfExprType>(T))
151 ++NumTypeOfExprTypes;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000152 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000153 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 assert(0 && "Unknown type!");
155 }
156 }
157
158 fprintf(stderr, " %d builtin types\n", NumBuiltin);
159 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000160 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000161 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
162 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000163 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000164 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000166 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
168 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
169 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
170 fprintf(stderr, " %d tagged types\n", NumTagged);
171 fprintf(stderr, " %d struct types\n", NumTagStruct);
172 fprintf(stderr, " %d union types\n", NumTagUnion);
173 fprintf(stderr, " %d class types\n", NumTagClass);
174 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000175 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000176 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000177 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000178 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000179 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000180 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor72564e72009-02-26 23:50:07 +0000181 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000182
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
184 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000185 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000186 NumLValueReference*sizeof(LValueReferenceType)+
187 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000188 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000189 NumFunctionP*sizeof(FunctionProtoType)+
190 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000191 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000192 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000193}
194
195
196void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000197 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000198}
199
Reid Spencer5f016e22007-07-11 17:01:13 +0000200void ASTContext::InitBuiltinTypes() {
201 assert(VoidTy.isNull() && "Context reinitialized?");
202
203 // C99 6.2.5p19.
204 InitBuiltinType(VoidTy, BuiltinType::Void);
205
206 // C99 6.2.5p2.
207 InitBuiltinType(BoolTy, BuiltinType::Bool);
208 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000209 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 InitBuiltinType(CharTy, BuiltinType::Char_S);
211 else
212 InitBuiltinType(CharTy, BuiltinType::Char_U);
213 // C99 6.2.5p4.
214 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
215 InitBuiltinType(ShortTy, BuiltinType::Short);
216 InitBuiltinType(IntTy, BuiltinType::Int);
217 InitBuiltinType(LongTy, BuiltinType::Long);
218 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
219
220 // C99 6.2.5p6.
221 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
222 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
223 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
224 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
225 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
226
227 // C99 6.2.5p10.
228 InitBuiltinType(FloatTy, BuiltinType::Float);
229 InitBuiltinType(DoubleTy, BuiltinType::Double);
230 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000231
Chris Lattner3a250322009-02-26 23:43:47 +0000232 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
233 InitBuiltinType(WCharTy, BuiltinType::WChar);
234 else // C99
235 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000236
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000237 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000238 InitBuiltinType(OverloadTy, BuiltinType::Overload);
239
240 // Placeholder type for type-dependent expressions whose type is
241 // completely unknown. No code should ever check a type against
242 // DependentTy and users should never see it; however, it is here to
243 // help diagnose failures to properly check for type-dependent
244 // expressions.
245 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000246
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 // C99 6.2.5p11.
248 FloatComplexTy = getComplexType(FloatTy);
249 DoubleComplexTy = getComplexType(DoubleTy);
250 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000251
Steve Naroff7e219e42007-10-15 14:41:52 +0000252 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000253 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000254 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000255 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000256 ClassStructType = 0;
257
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000258 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000259
260 // void * type
261 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000262}
263
Chris Lattner464175b2007-07-18 17:52:12 +0000264//===----------------------------------------------------------------------===//
265// Type Sizing and Analysis
266//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000267
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000268/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
269/// scalar floating point type.
270const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
271 const BuiltinType *BT = T->getAsBuiltinType();
272 assert(BT && "Not a floating point type!");
273 switch (BT->getKind()) {
274 default: assert(0 && "Not a floating point type!");
275 case BuiltinType::Float: return Target.getFloatFormat();
276 case BuiltinType::Double: return Target.getDoubleFormat();
277 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
278 }
279}
280
Chris Lattneraf707ab2009-01-24 21:53:27 +0000281/// getDeclAlign - Return a conservative estimate of the alignment of the
282/// specified decl. Note that bitfields do not have a valid alignment, so
283/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000284unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000285 unsigned Align = Target.getCharWidth();
286
287 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
288 Align = std::max(Align, AA->getAlignment());
289
Chris Lattneraf707ab2009-01-24 21:53:27 +0000290 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
291 QualType T = VD->getType();
292 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000293 if (!T->isIncompleteType() && !T->isFunctionType()) {
294 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
295 T = cast<ArrayType>(T)->getElementType();
296
297 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
298 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000299 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000300
301 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000302}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000303
Chris Lattnera7674d82007-07-13 22:13:22 +0000304/// getTypeSize - Return the size of the specified type, in bits. This method
305/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000306std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000307ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000308 T = getCanonicalType(T);
Mike Stump5e301002009-02-27 18:32:39 +0000309 uint64_t Width=0;
310 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000311 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000312#define TYPE(Class, Base)
313#define ABSTRACT_TYPE(Class, Base)
314#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
315#define DEPENDENT_TYPE(Class, Base) case Type::Class:
316#include "clang/AST/TypeNodes.def"
317 assert(false && "Should not see non-canonical or dependent types");
318 break;
319
Chris Lattner692233e2007-07-13 22:27:08 +0000320 case Type::FunctionNoProto:
321 case Type::FunctionProto:
Douglas Gregor72564e72009-02-26 23:50:07 +0000322 case Type::IncompleteArray:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000323 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000324 case Type::VariableArray:
325 assert(0 && "VLAs not implemented yet!");
326 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000327 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000328
Chris Lattner98be4942008-03-05 18:54:05 +0000329 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000330 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000331 Align = EltInfo.second;
332 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000333 }
Nate Begeman213541a2008-04-18 23:10:10 +0000334 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000335 case Type::Vector: {
336 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000337 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000338 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000339 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000340 // If the alignment is not a power of 2, round up to the next power of 2.
341 // This happens for non-power-of-2 length vectors.
342 // FIXME: this should probably be a target property.
343 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000344 break;
345 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000346
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000347 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000348 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000349 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000350 case BuiltinType::Void:
351 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000352 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000353 Width = Target.getBoolWidth();
354 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000355 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000356 case BuiltinType::Char_S:
357 case BuiltinType::Char_U:
358 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000359 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000360 Width = Target.getCharWidth();
361 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000362 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000363 case BuiltinType::WChar:
364 Width = Target.getWCharWidth();
365 Align = Target.getWCharAlign();
366 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000367 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000368 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000369 Width = Target.getShortWidth();
370 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000371 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000372 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000373 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000374 Width = Target.getIntWidth();
375 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000376 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000377 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000378 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000379 Width = Target.getLongWidth();
380 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000381 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000382 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000383 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000384 Width = Target.getLongLongWidth();
385 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000386 break;
387 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000388 Width = Target.getFloatWidth();
389 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000390 break;
391 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000392 Width = Target.getDoubleWidth();
393 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000394 break;
395 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000396 Width = Target.getLongDoubleWidth();
397 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000398 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000399 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000400 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000401 case Type::FixedWidthInt:
402 // FIXME: This isn't precisely correct; the width/alignment should depend
403 // on the available types for the target
404 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000405 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000406 Align = Width;
407 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000408 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000409 // FIXME: Pointers into different addr spaces could have different sizes and
410 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000411 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000412 case Type::ObjCQualifiedId:
Eli Friedman4bdf0872009-02-22 04:02:33 +0000413 case Type::ObjCQualifiedClass:
Douglas Gregor72564e72009-02-26 23:50:07 +0000414 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000415 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000416 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000417 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000418 case Type::BlockPointer: {
419 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
420 Width = Target.getPointerWidth(AS);
421 Align = Target.getPointerAlign(AS);
422 break;
423 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000424 case Type::Pointer: {
425 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000426 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000427 Align = Target.getPointerAlign(AS);
428 break;
429 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000430 case Type::LValueReference:
431 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000432 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000433 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000434 // FIXME: This is wrong for struct layout: a reference in a struct has
435 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000436 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000437 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000438 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000439 // the GCC ABI, where pointers to data are one pointer large, pointers to
440 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000441 // other compilers too, we need to delegate this completely to TargetInfo
442 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000443 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
444 unsigned AS = Pointee.getAddressSpace();
445 Width = Target.getPointerWidth(AS);
446 if (Pointee->isFunctionType())
447 Width *= 2;
448 Align = Target.getPointerAlign(AS);
449 // GCC aligns at single pointer width.
450 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000451 case Type::Complex: {
452 // Complex types have the same alignment as their elements, but twice the
453 // size.
454 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000455 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000456 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000457 Align = EltInfo.second;
458 break;
459 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000460 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000461 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000462 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
463 Width = Layout.getSize();
464 Align = Layout.getAlignment();
465 break;
466 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000467 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000468 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000469 const TagType *TT = cast<TagType>(T);
470
471 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000472 Width = 1;
473 Align = 1;
474 break;
475 }
476
Daniel Dunbar1d751182008-11-08 05:48:37 +0000477 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000478 return getTypeInfo(ET->getDecl()->getIntegerType());
479
Daniel Dunbar1d751182008-11-08 05:48:37 +0000480 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000481 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
482 Width = Layout.getSize();
483 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000484 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000485 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000486
487 case Type::TemplateSpecialization:
488 assert(false && "Dependent types have no size");
489 break;
Chris Lattner71763312008-04-06 22:05:18 +0000490 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000491
Chris Lattner464175b2007-07-18 17:52:12 +0000492 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000493 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000494}
495
Chris Lattner34ebde42009-01-27 18:08:34 +0000496/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
497/// type for the current target in bits. This can be different than the ABI
498/// alignment in cases where it is beneficial for performance to overalign
499/// a data type.
500unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
501 unsigned ABIAlign = getTypeAlign(T);
502
503 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000504 if (T->isSpecificBuiltinType(BuiltinType::Double))
505 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000506
507 return ABIAlign;
508}
509
510
Devang Patel8b277042008-06-04 21:22:16 +0000511/// LayoutField - Field layout.
512void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000513 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000514 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000515 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000516 uint64_t FieldOffset = IsUnion ? 0 : Size;
517 uint64_t FieldSize;
518 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000519
520 // FIXME: Should this override struct packing? Probably we want to
521 // take the minimum?
522 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
523 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000524
525 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
526 // TODO: Need to check this algorithm on other targets!
527 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000528 FieldSize =
529 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000530
531 std::pair<uint64_t, unsigned> FieldInfo =
532 Context.getTypeInfo(FD->getType());
533 uint64_t TypeSize = FieldInfo.first;
534
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000535 // Determine the alignment of this bitfield. The packing
536 // attributes define a maximum and the alignment attribute defines
537 // a minimum.
538 // FIXME: What is the right behavior when the specified alignment
539 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000540 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000541 if (FieldPacking)
542 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000543 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
544 FieldAlign = std::max(FieldAlign, AA->getAlignment());
545
546 // Check if we need to add padding to give the field the correct
547 // alignment.
548 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
549 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
550
551 // Padding members don't affect overall alignment
552 if (!FD->getIdentifier())
553 FieldAlign = 1;
554 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000555 if (FD->getType()->isIncompleteArrayType()) {
556 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000557 // query getTypeInfo about these, so we figure it out here.
558 // Flexible array members don't have any size, but they
559 // have to be aligned appropriately for their element type.
560 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000561 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000562 FieldAlign = Context.getTypeAlign(ATy->getElementType());
563 } else {
564 std::pair<uint64_t, unsigned> FieldInfo =
565 Context.getTypeInfo(FD->getType());
566 FieldSize = FieldInfo.first;
567 FieldAlign = FieldInfo.second;
568 }
569
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000570 // Determine the alignment of this bitfield. The packing
571 // attributes define a maximum and the alignment attribute defines
572 // a minimum. Additionally, the packing alignment must be at least
573 // a byte for non-bitfields.
574 //
575 // FIXME: What is the right behavior when the specified alignment
576 // is smaller than the specified packing?
577 if (FieldPacking)
578 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000579 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
580 FieldAlign = std::max(FieldAlign, AA->getAlignment());
581
582 // Round up the current record size to the field's alignment boundary.
583 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
584 }
585
586 // Place this field at the current location.
587 FieldOffsets[FieldNo] = FieldOffset;
588
589 // Reserve space for this field.
590 if (IsUnion) {
591 Size = std::max(Size, FieldSize);
592 } else {
593 Size = FieldOffset + FieldSize;
594 }
595
596 // Remember max struct/class alignment.
597 Alignment = std::max(Alignment, FieldAlign);
598}
599
Fariborz Jahanian88e469c2009-03-05 20:08:48 +0000600void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
601 std::vector<FieldDecl*> &Fields) const {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000602 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
603 if (SuperClass)
604 CollectObjCIvars(SuperClass, Fields);
605 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
606 E = OI->ivar_end(); I != E; ++I) {
607 ObjCIvarDecl *IVDecl = (*I);
608 if (!IVDecl->isInvalidDecl())
609 Fields.push_back(cast<FieldDecl>(IVDecl));
610 }
611}
612
613/// addRecordToClass - produces record info. for the class for its
614/// ivars and all those inherited.
615///
616const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
617{
618 const RecordDecl *&RD = ASTRecordForInterface[D];
619 if (RD)
620 return RD;
621 std::vector<FieldDecl*> RecFields;
622 CollectObjCIvars(D, RecFields);
623 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
624 D->getLocation(),
625 D->getIdentifier());
626 /// FIXME! Can do collection of ivars and adding to the record while
627 /// doing it.
628 for (unsigned int i = 0; i != RecFields.size(); i++) {
629 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
630 RecFields[i]->getLocation(),
631 RecFields[i]->getIdentifier(),
632 RecFields[i]->getType(),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000633 RecFields[i]->getBitWidth(), false);
Douglas Gregor482b77d2009-01-12 23:27:07 +0000634 NewRD->addDecl(Field);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000635 }
636 NewRD->completeDefinition(*this);
637 RD = NewRD;
638 return RD;
639}
Devang Patel44a3dde2008-06-04 21:54:36 +0000640
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000641/// setFieldDecl - maps a field for the given Ivar reference node.
642//
643void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
644 const ObjCIvarDecl *Ivar,
645 const ObjCIvarRefExpr *MRef) {
646 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
647 lookupFieldDeclForIvar(*this, Ivar);
648 ASTFieldForIvarRef[MRef] = FD;
649}
650
Chris Lattner61710852008-10-05 17:34:18 +0000651/// getASTObjcInterfaceLayout - Get or compute information about the layout of
652/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000653/// position information.
654const ASTRecordLayout &
655ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
656 // Look up this layout, if already laid out, return what we have.
657 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
658 if (Entry) return *Entry;
659
660 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
661 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000662 ASTRecordLayout *NewEntry = NULL;
663 unsigned FieldCount = D->ivar_size();
664 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
665 FieldCount++;
666 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
667 unsigned Alignment = SL.getAlignment();
668 uint64_t Size = SL.getSize();
669 NewEntry = new ASTRecordLayout(Size, Alignment);
670 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000671 // Super class is at the beginning of the layout.
672 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000673 } else {
674 NewEntry = new ASTRecordLayout();
675 NewEntry->InitializeLayout(FieldCount);
676 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000677 Entry = NewEntry;
678
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000679 unsigned StructPacking = 0;
680 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
681 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000682
683 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
684 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
685 AA->getAlignment()));
686
687 // Layout each ivar sequentially.
688 unsigned i = 0;
689 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
690 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
691 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000692 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000693 }
694
695 // Finally, round the size of the total struct up to the alignment of the
696 // struct itself.
697 NewEntry->FinalizeLayout();
698 return *NewEntry;
699}
700
Devang Patel88a981b2007-11-01 19:11:01 +0000701/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000702/// specified record (struct/union/class), which indicates its size and field
703/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000704const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000705 D = D->getDefinition(*this);
706 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000707
Chris Lattner464175b2007-07-18 17:52:12 +0000708 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000709 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000710 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000711
Devang Patel88a981b2007-11-01 19:11:01 +0000712 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
713 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
714 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000715 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000716
Douglas Gregore267ff32008-12-11 20:41:00 +0000717 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor44b43212008-12-11 16:49:14 +0000718 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000719 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000720
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000721 unsigned StructPacking = 0;
722 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
723 StructPacking = PA->getAlignment();
724
Eli Friedman4bd998b2008-05-30 09:31:38 +0000725 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000726 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
727 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000728
Eli Friedman4bd998b2008-05-30 09:31:38 +0000729 // Layout each field, for now, just sequentially, respecting alignment. In
730 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000731 unsigned FieldIdx = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000732 for (RecordDecl::field_iterator Field = D->field_begin(),
733 FieldEnd = D->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000734 Field != FieldEnd; (void)++Field, ++FieldIdx)
735 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000736
737 // Finally, round the size of the total struct up to the alignment of the
738 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000739 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000740 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000741}
742
Chris Lattnera7674d82007-07-13 22:13:22 +0000743//===----------------------------------------------------------------------===//
744// Type creation/memoization methods
745//===----------------------------------------------------------------------===//
746
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000747QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000748 QualType CanT = getCanonicalType(T);
749 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000750 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000751
752 // If we are composing extended qualifiers together, merge together into one
753 // ExtQualType node.
754 unsigned CVRQuals = T.getCVRQualifiers();
755 QualType::GCAttrTypes GCAttr = QualType::GCNone;
756 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000757
Chris Lattnerb7d25532009-02-18 22:53:11 +0000758 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
759 // If this type already has an address space specified, it cannot get
760 // another one.
761 assert(EQT->getAddressSpace() == 0 &&
762 "Type cannot be in multiple addr spaces!");
763 GCAttr = EQT->getObjCGCAttr();
764 TypeNode = EQT->getBaseType();
765 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000766
Chris Lattnerb7d25532009-02-18 22:53:11 +0000767 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000768 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000769 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000770 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000771 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000772 return QualType(EXTQy, CVRQuals);
773
Christopher Lambebb97e92008-02-04 02:31:56 +0000774 // If the base type isn't canonical, this won't be a canonical type either,
775 // so fill in the canonical type field.
776 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000777 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000778 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000779
Chris Lattnerb7d25532009-02-18 22:53:11 +0000780 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000781 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000782 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000783 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000784 ExtQualType *New =
785 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000786 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000787 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000788 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000789}
790
Chris Lattnerb7d25532009-02-18 22:53:11 +0000791QualType ASTContext::getObjCGCQualType(QualType T,
792 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000793 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000794 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000795 return T;
796
Chris Lattnerb7d25532009-02-18 22:53:11 +0000797 // If we are composing extended qualifiers together, merge together into one
798 // ExtQualType node.
799 unsigned CVRQuals = T.getCVRQualifiers();
800 Type *TypeNode = T.getTypePtr();
801 unsigned AddressSpace = 0;
802
803 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
804 // If this type already has an address space specified, it cannot get
805 // another one.
806 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
807 "Type cannot be in multiple addr spaces!");
808 AddressSpace = EQT->getAddressSpace();
809 TypeNode = EQT->getBaseType();
810 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000811
812 // Check if we've already instantiated an gc qual'd type of this type.
813 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000814 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000815 void *InsertPos = 0;
816 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000817 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000818
819 // If the base type isn't canonical, this won't be a canonical type either,
820 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000821 // FIXME: Isn't this also not canonical if the base type is a array
822 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000823 QualType Canonical;
824 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000825 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000826
Chris Lattnerb7d25532009-02-18 22:53:11 +0000827 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000828 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
829 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
830 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000831 ExtQualType *New =
832 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000833 ExtQualTypes.InsertNode(New, InsertPos);
834 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000835 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000836}
Chris Lattnera7674d82007-07-13 22:13:22 +0000837
Reid Spencer5f016e22007-07-11 17:01:13 +0000838/// getComplexType - Return the uniqued reference to the type for a complex
839/// number with the specified element type.
840QualType ASTContext::getComplexType(QualType T) {
841 // Unique pointers, to guarantee there is only one pointer of a particular
842 // structure.
843 llvm::FoldingSetNodeID ID;
844 ComplexType::Profile(ID, T);
845
846 void *InsertPos = 0;
847 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
848 return QualType(CT, 0);
849
850 // If the pointee type isn't canonical, this won't be a canonical type either,
851 // so fill in the canonical type field.
852 QualType Canonical;
853 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000854 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000855
856 // Get the new insert position for the node we care about.
857 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000858 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 }
Steve Narofff83820b2009-01-27 22:08:43 +0000860 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000861 Types.push_back(New);
862 ComplexTypes.InsertNode(New, InsertPos);
863 return QualType(New, 0);
864}
865
Eli Friedmanf98aba32009-02-13 02:31:07 +0000866QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
867 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
868 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
869 FixedWidthIntType *&Entry = Map[Width];
870 if (!Entry)
871 Entry = new FixedWidthIntType(Width, Signed);
872 return QualType(Entry, 0);
873}
Reid Spencer5f016e22007-07-11 17:01:13 +0000874
875/// getPointerType - Return the uniqued reference to the type for a pointer to
876/// the specified type.
877QualType ASTContext::getPointerType(QualType T) {
878 // Unique pointers, to guarantee there is only one pointer of a particular
879 // structure.
880 llvm::FoldingSetNodeID ID;
881 PointerType::Profile(ID, T);
882
883 void *InsertPos = 0;
884 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
885 return QualType(PT, 0);
886
887 // If the pointee type isn't canonical, this won't be a canonical type either,
888 // so fill in the canonical type field.
889 QualType Canonical;
890 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000891 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000892
893 // Get the new insert position for the node we care about.
894 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000895 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000896 }
Steve Narofff83820b2009-01-27 22:08:43 +0000897 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 Types.push_back(New);
899 PointerTypes.InsertNode(New, InsertPos);
900 return QualType(New, 0);
901}
902
Steve Naroff5618bd42008-08-27 16:04:49 +0000903/// getBlockPointerType - Return the uniqued reference to the type for
904/// a pointer to the specified block.
905QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000906 assert(T->isFunctionType() && "block of function types only");
907 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000908 // structure.
909 llvm::FoldingSetNodeID ID;
910 BlockPointerType::Profile(ID, T);
911
912 void *InsertPos = 0;
913 if (BlockPointerType *PT =
914 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
915 return QualType(PT, 0);
916
Steve Naroff296e8d52008-08-28 19:20:44 +0000917 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000918 // type either so fill in the canonical type field.
919 QualType Canonical;
920 if (!T->isCanonical()) {
921 Canonical = getBlockPointerType(getCanonicalType(T));
922
923 // Get the new insert position for the node we care about.
924 BlockPointerType *NewIP =
925 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000926 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000927 }
Steve Narofff83820b2009-01-27 22:08:43 +0000928 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000929 Types.push_back(New);
930 BlockPointerTypes.InsertNode(New, InsertPos);
931 return QualType(New, 0);
932}
933
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000934/// getLValueReferenceType - Return the uniqued reference to the type for an
935/// lvalue reference to the specified type.
936QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000937 // Unique pointers, to guarantee there is only one pointer of a particular
938 // structure.
939 llvm::FoldingSetNodeID ID;
940 ReferenceType::Profile(ID, T);
941
942 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000943 if (LValueReferenceType *RT =
944 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000946
Reid Spencer5f016e22007-07-11 17:01:13 +0000947 // If the referencee type isn't canonical, this won't be a canonical type
948 // either, so fill in the canonical type field.
949 QualType Canonical;
950 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000951 Canonical = getLValueReferenceType(getCanonicalType(T));
952
Reid Spencer5f016e22007-07-11 17:01:13 +0000953 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000954 LValueReferenceType *NewIP =
955 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000956 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000957 }
958
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000959 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000960 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000961 LValueReferenceTypes.InsertNode(New, InsertPos);
962 return QualType(New, 0);
963}
964
965/// getRValueReferenceType - Return the uniqued reference to the type for an
966/// rvalue reference to the specified type.
967QualType ASTContext::getRValueReferenceType(QualType T) {
968 // Unique pointers, to guarantee there is only one pointer of a particular
969 // structure.
970 llvm::FoldingSetNodeID ID;
971 ReferenceType::Profile(ID, T);
972
973 void *InsertPos = 0;
974 if (RValueReferenceType *RT =
975 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
976 return QualType(RT, 0);
977
978 // If the referencee type isn't canonical, this won't be a canonical type
979 // either, so fill in the canonical type field.
980 QualType Canonical;
981 if (!T->isCanonical()) {
982 Canonical = getRValueReferenceType(getCanonicalType(T));
983
984 // Get the new insert position for the node we care about.
985 RValueReferenceType *NewIP =
986 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
987 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
988 }
989
990 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
991 Types.push_back(New);
992 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 return QualType(New, 0);
994}
995
Sebastian Redlf30208a2009-01-24 21:16:55 +0000996/// getMemberPointerType - Return the uniqued reference to the type for a
997/// member pointer to the specified type, in the specified class.
998QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
999{
1000 // Unique pointers, to guarantee there is only one pointer of a particular
1001 // structure.
1002 llvm::FoldingSetNodeID ID;
1003 MemberPointerType::Profile(ID, T, Cls);
1004
1005 void *InsertPos = 0;
1006 if (MemberPointerType *PT =
1007 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1008 return QualType(PT, 0);
1009
1010 // If the pointee or class type isn't canonical, this won't be a canonical
1011 // type either, so fill in the canonical type field.
1012 QualType Canonical;
1013 if (!T->isCanonical()) {
1014 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1015
1016 // Get the new insert position for the node we care about.
1017 MemberPointerType *NewIP =
1018 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1019 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1020 }
Steve Narofff83820b2009-01-27 22:08:43 +00001021 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001022 Types.push_back(New);
1023 MemberPointerTypes.InsertNode(New, InsertPos);
1024 return QualType(New, 0);
1025}
1026
Steve Narofffb22d962007-08-30 01:06:46 +00001027/// getConstantArrayType - Return the unique reference to the type for an
1028/// array of the specified element type.
1029QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +00001030 const llvm::APInt &ArySize,
1031 ArrayType::ArraySizeModifier ASM,
1032 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001033 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001034 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001035
1036 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001037 if (ConstantArrayType *ATP =
1038 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001039 return QualType(ATP, 0);
1040
1041 // If the element type isn't canonical, this won't be a canonical type either,
1042 // so fill in the canonical type field.
1043 QualType Canonical;
1044 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001045 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001046 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001047 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001048 ConstantArrayType *NewIP =
1049 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001050 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001051 }
1052
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001053 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001054 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001055 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 Types.push_back(New);
1057 return QualType(New, 0);
1058}
1059
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001060/// getVariableArrayType - Returns a non-unique reference to the type for a
1061/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001062QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1063 ArrayType::ArraySizeModifier ASM,
1064 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001065 // Since we don't unique expressions, it isn't possible to unique VLA's
1066 // that have an expression provided for their size.
1067
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001068 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001069 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001070
1071 VariableArrayTypes.push_back(New);
1072 Types.push_back(New);
1073 return QualType(New, 0);
1074}
1075
Douglas Gregor898574e2008-12-05 23:32:09 +00001076/// getDependentSizedArrayType - Returns a non-unique reference to
1077/// the type for a dependently-sized array of the specified element
1078/// type. FIXME: We will need these to be uniqued, or at least
1079/// comparable, at some point.
1080QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1081 ArrayType::ArraySizeModifier ASM,
1082 unsigned EltTypeQuals) {
1083 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1084 "Size must be type- or value-dependent!");
1085
1086 // Since we don't unique expressions, it isn't possible to unique
1087 // dependently-sized array types.
1088
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001089 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001090 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1091 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001092
1093 DependentSizedArrayTypes.push_back(New);
1094 Types.push_back(New);
1095 return QualType(New, 0);
1096}
1097
Eli Friedmanc5773c42008-02-15 18:16:39 +00001098QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1099 ArrayType::ArraySizeModifier ASM,
1100 unsigned EltTypeQuals) {
1101 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001102 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001103
1104 void *InsertPos = 0;
1105 if (IncompleteArrayType *ATP =
1106 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1107 return QualType(ATP, 0);
1108
1109 // If the element type isn't canonical, this won't be a canonical type
1110 // either, so fill in the canonical type field.
1111 QualType Canonical;
1112
1113 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001114 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001115 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001116
1117 // Get the new insert position for the node we care about.
1118 IncompleteArrayType *NewIP =
1119 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001120 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001121 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001122
Steve Narofff83820b2009-01-27 22:08:43 +00001123 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001124 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001125
1126 IncompleteArrayTypes.InsertNode(New, InsertPos);
1127 Types.push_back(New);
1128 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001129}
1130
Steve Naroff73322922007-07-18 18:00:27 +00001131/// getVectorType - Return the unique reference to a vector type of
1132/// the specified element type and size. VectorType must be a built-in type.
1133QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 BuiltinType *baseType;
1135
Chris Lattnerf52ab252008-04-06 22:59:24 +00001136 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001137 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001138
1139 // Check if we've already instantiated a vector of this type.
1140 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001141 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001142 void *InsertPos = 0;
1143 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1144 return QualType(VTP, 0);
1145
1146 // If the element type isn't canonical, this won't be a canonical type either,
1147 // so fill in the canonical type field.
1148 QualType Canonical;
1149 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001150 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001151
1152 // Get the new insert position for the node we care about.
1153 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001154 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001155 }
Steve Narofff83820b2009-01-27 22:08:43 +00001156 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001157 VectorTypes.InsertNode(New, InsertPos);
1158 Types.push_back(New);
1159 return QualType(New, 0);
1160}
1161
Nate Begeman213541a2008-04-18 23:10:10 +00001162/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001163/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001164QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001165 BuiltinType *baseType;
1166
Chris Lattnerf52ab252008-04-06 22:59:24 +00001167 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001168 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001169
1170 // Check if we've already instantiated a vector of this type.
1171 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001172 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001173 void *InsertPos = 0;
1174 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1175 return QualType(VTP, 0);
1176
1177 // If the element type isn't canonical, this won't be a canonical type either,
1178 // so fill in the canonical type field.
1179 QualType Canonical;
1180 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001181 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001182
1183 // Get the new insert position for the node we care about.
1184 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001185 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001186 }
Steve Narofff83820b2009-01-27 22:08:43 +00001187 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001188 VectorTypes.InsertNode(New, InsertPos);
1189 Types.push_back(New);
1190 return QualType(New, 0);
1191}
1192
Douglas Gregor72564e72009-02-26 23:50:07 +00001193/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001194///
Douglas Gregor72564e72009-02-26 23:50:07 +00001195QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001196 // Unique functions, to guarantee there is only one function of a particular
1197 // structure.
1198 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001199 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001200
1201 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001202 if (FunctionNoProtoType *FT =
1203 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 return QualType(FT, 0);
1205
1206 QualType Canonical;
1207 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001208 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001209
1210 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001211 FunctionNoProtoType *NewIP =
1212 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001213 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 }
1215
Douglas Gregor72564e72009-02-26 23:50:07 +00001216 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001217 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001218 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 return QualType(New, 0);
1220}
1221
1222/// getFunctionType - Return a normal function type with a typed argument
1223/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001224QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001225 unsigned NumArgs, bool isVariadic,
1226 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001227 // Unique functions, to guarantee there is only one function of a particular
1228 // structure.
1229 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001230 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001231 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001232
1233 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001234 if (FunctionProtoType *FTP =
1235 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 return QualType(FTP, 0);
1237
1238 // Determine whether the type being created is already canonical or not.
1239 bool isCanonical = ResultTy->isCanonical();
1240 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1241 if (!ArgArray[i]->isCanonical())
1242 isCanonical = false;
1243
1244 // If this type isn't canonical, get the canonical version of it.
1245 QualType Canonical;
1246 if (!isCanonical) {
1247 llvm::SmallVector<QualType, 16> CanonicalArgs;
1248 CanonicalArgs.reserve(NumArgs);
1249 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001250 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001251
Chris Lattnerf52ab252008-04-06 22:59:24 +00001252 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001254 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001255
1256 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001257 FunctionProtoType *NewIP =
1258 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001259 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001260 }
1261
Douglas Gregor72564e72009-02-26 23:50:07 +00001262 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001263 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001264 FunctionProtoType *FTP =
1265 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001266 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001267 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001268 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001270 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 return QualType(FTP, 0);
1272}
1273
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001274/// getTypeDeclType - Return the unique reference to the type for the
1275/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001276QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001277 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001278 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1279
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001280 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001281 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001282 else if (isa<TemplateTypeParmDecl>(Decl)) {
1283 assert(false && "Template type parameter types are always available.");
1284 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001285 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001286
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001287 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001288 if (PrevDecl)
1289 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001290 else
1291 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001292 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001293 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1294 if (PrevDecl)
1295 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001296 else
1297 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001298 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001299 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001300 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001301
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001302 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001303 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001304}
1305
Reid Spencer5f016e22007-07-11 17:01:13 +00001306/// getTypedefType - Return the unique reference to the type for the
1307/// specified typename decl.
1308QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1309 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1310
Chris Lattnerf52ab252008-04-06 22:59:24 +00001311 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001312 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001313 Types.push_back(Decl->TypeForDecl);
1314 return QualType(Decl->TypeForDecl, 0);
1315}
1316
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001317/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001318/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001319QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001320 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1321
Steve Narofff83820b2009-01-27 22:08:43 +00001322 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001323 Types.push_back(Decl->TypeForDecl);
1324 return QualType(Decl->TypeForDecl, 0);
1325}
1326
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00001327/// buildObjCInterfaceType - Returns a new type for the interface
1328/// declaration, regardless. It also removes any previously built
1329/// record declaration so caller can rebuild it.
1330QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) {
1331 const RecordDecl *&RD = ASTRecordForInterface[Decl];
1332 if (RD)
1333 RD = 0;
1334 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
1335 Types.push_back(Decl->TypeForDecl);
1336 return QualType(Decl->TypeForDecl, 0);
1337}
1338
Douglas Gregorfab9d672009-02-05 23:33:38 +00001339/// \brief Retrieve the template type parameter type for a template
1340/// parameter with the given depth, index, and (optionally) name.
1341QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1342 IdentifierInfo *Name) {
1343 llvm::FoldingSetNodeID ID;
1344 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1345 void *InsertPos = 0;
1346 TemplateTypeParmType *TypeParm
1347 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1348
1349 if (TypeParm)
1350 return QualType(TypeParm, 0);
1351
1352 if (Name)
1353 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1354 getTemplateTypeParmType(Depth, Index));
1355 else
1356 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1357
1358 Types.push_back(TypeParm);
1359 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1360
1361 return QualType(TypeParm, 0);
1362}
1363
Douglas Gregor55f6b142009-02-09 18:46:07 +00001364QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001365ASTContext::getTemplateSpecializationType(TemplateName Template,
1366 const TemplateArgument *Args,
1367 unsigned NumArgs,
1368 QualType Canon) {
1369 // FIXME: If Template is dependent, canonicalize it!
1370
Douglas Gregor40808ce2009-03-09 23:48:35 +00001371 if (!Canon.isNull())
1372 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001373
Douglas Gregor55f6b142009-02-09 18:46:07 +00001374 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001375 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001376
Douglas Gregor55f6b142009-02-09 18:46:07 +00001377 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001378 TemplateSpecializationType *Spec
1379 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001380
1381 if (Spec)
1382 return QualType(Spec, 0);
1383
Douglas Gregor7532dc62009-03-30 22:58:21 +00001384 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001385 sizeof(TemplateArgument) * NumArgs),
1386 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001387 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001388 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001389 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001390
1391 return QualType(Spec, 0);
1392}
1393
Douglas Gregore4e5b052009-03-19 00:18:19 +00001394QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001395ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001396 QualType NamedType) {
1397 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001398 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001399
1400 void *InsertPos = 0;
1401 QualifiedNameType *T
1402 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1403 if (T)
1404 return QualType(T, 0);
1405
Douglas Gregorab452ba2009-03-26 23:50:42 +00001406 T = new (*this) QualifiedNameType(NNS, NamedType,
1407 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001408 Types.push_back(T);
1409 QualifiedNameTypes.InsertNode(T, InsertPos);
1410 return QualType(T, 0);
1411}
1412
Douglas Gregord57959a2009-03-27 23:10:48 +00001413QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1414 const IdentifierInfo *Name,
1415 QualType Canon) {
1416 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1417
1418 if (Canon.isNull()) {
1419 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1420 if (CanonNNS != NNS)
1421 Canon = getTypenameType(CanonNNS, Name);
1422 }
1423
1424 llvm::FoldingSetNodeID ID;
1425 TypenameType::Profile(ID, NNS, Name);
1426
1427 void *InsertPos = 0;
1428 TypenameType *T
1429 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1430 if (T)
1431 return QualType(T, 0);
1432
1433 T = new (*this) TypenameType(NNS, Name, Canon);
1434 Types.push_back(T);
1435 TypenameTypes.InsertNode(T, InsertPos);
1436 return QualType(T, 0);
1437}
1438
Chris Lattner88cb27a2008-04-07 04:56:42 +00001439/// CmpProtocolNames - Comparison predicate for sorting protocols
1440/// alphabetically.
1441static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1442 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001443 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001444}
1445
1446static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1447 unsigned &NumProtocols) {
1448 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1449
1450 // Sort protocols, keyed by name.
1451 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1452
1453 // Remove duplicates.
1454 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1455 NumProtocols = ProtocolsEnd-Protocols;
1456}
1457
1458
Chris Lattner065f0d72008-04-07 04:44:08 +00001459/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1460/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001461QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1462 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001463 // Sort the protocol list alphabetically to canonicalize it.
1464 SortAndUniqueProtocols(Protocols, NumProtocols);
1465
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001466 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001467 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001468
1469 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001470 if (ObjCQualifiedInterfaceType *QT =
1471 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001472 return QualType(QT, 0);
1473
1474 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001475 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001476 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001477
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001478 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001479 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001480 return QualType(QType, 0);
1481}
1482
Chris Lattner88cb27a2008-04-07 04:56:42 +00001483/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1484/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001485QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001486 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001487 // Sort the protocol list alphabetically to canonicalize it.
1488 SortAndUniqueProtocols(Protocols, NumProtocols);
1489
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001490 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001491 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001492
1493 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001494 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001495 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001496 return QualType(QT, 0);
1497
1498 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001499 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001500 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001501 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001502 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001503 return QualType(QType, 0);
1504}
1505
Douglas Gregor72564e72009-02-26 23:50:07 +00001506/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1507/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001508/// multiple declarations that refer to "typeof(x)" all contain different
1509/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1510/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001511QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001512 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001513 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001514 Types.push_back(toe);
1515 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001516}
1517
Steve Naroff9752f252007-08-01 18:02:17 +00001518/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1519/// TypeOfType AST's. The only motivation to unique these nodes would be
1520/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1521/// an issue. This doesn't effect the type checker, since it operates
1522/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001523QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001524 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001525 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001526 Types.push_back(tot);
1527 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001528}
1529
Reid Spencer5f016e22007-07-11 17:01:13 +00001530/// getTagDeclType - Return the unique reference to the type for the
1531/// specified TagDecl (struct/union/class/enum) decl.
1532QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001533 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001534 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001535}
1536
1537/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1538/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1539/// needs to agree with the definition in <stddef.h>.
1540QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001541 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001542}
1543
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001544/// getSignedWCharType - Return the type of "signed wchar_t".
1545/// Used when in C++, as a GCC extension.
1546QualType ASTContext::getSignedWCharType() const {
1547 // FIXME: derive from "Target" ?
1548 return WCharTy;
1549}
1550
1551/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1552/// Used when in C++, as a GCC extension.
1553QualType ASTContext::getUnsignedWCharType() const {
1554 // FIXME: derive from "Target" ?
1555 return UnsignedIntTy;
1556}
1557
Chris Lattner8b9023b2007-07-13 03:05:23 +00001558/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1559/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1560QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001561 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001562}
1563
Chris Lattnere6327742008-04-02 05:18:44 +00001564//===----------------------------------------------------------------------===//
1565// Type Operators
1566//===----------------------------------------------------------------------===//
1567
Chris Lattner77c96472008-04-06 22:41:35 +00001568/// getCanonicalType - Return the canonical (structural) type corresponding to
1569/// the specified potentially non-canonical type. The non-canonical version
1570/// of a type may have many "decorated" versions of types. Decorators can
1571/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1572/// to be free of any of these, allowing two canonical types to be compared
1573/// for exact equality with a simple pointer comparison.
1574QualType ASTContext::getCanonicalType(QualType T) {
1575 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001576
1577 // If the result has type qualifiers, make sure to canonicalize them as well.
1578 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1579 if (TypeQuals == 0) return CanType;
1580
1581 // If the type qualifiers are on an array type, get the canonical type of the
1582 // array with the qualifiers applied to the element type.
1583 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1584 if (!AT)
1585 return CanType.getQualifiedType(TypeQuals);
1586
1587 // Get the canonical version of the element with the extra qualifiers on it.
1588 // This can recursively sink qualifiers through multiple levels of arrays.
1589 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1590 NewEltTy = getCanonicalType(NewEltTy);
1591
1592 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1593 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1594 CAT->getIndexTypeQualifier());
1595 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1596 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1597 IAT->getIndexTypeQualifier());
1598
Douglas Gregor898574e2008-12-05 23:32:09 +00001599 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1600 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1601 DSAT->getSizeModifier(),
1602 DSAT->getIndexTypeQualifier());
1603
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001604 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1605 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1606 VAT->getSizeModifier(),
1607 VAT->getIndexTypeQualifier());
1608}
1609
Douglas Gregord57959a2009-03-27 23:10:48 +00001610NestedNameSpecifier *
1611ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1612 if (!NNS)
1613 return 0;
1614
1615 switch (NNS->getKind()) {
1616 case NestedNameSpecifier::Identifier:
1617 // Canonicalize the prefix but keep the identifier the same.
1618 return NestedNameSpecifier::Create(*this,
1619 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1620 NNS->getAsIdentifier());
1621
1622 case NestedNameSpecifier::Namespace:
1623 // A namespace is canonical; build a nested-name-specifier with
1624 // this namespace and no prefix.
1625 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1626
1627 case NestedNameSpecifier::TypeSpec:
1628 case NestedNameSpecifier::TypeSpecWithTemplate: {
1629 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1630 NestedNameSpecifier *Prefix = 0;
1631
1632 // FIXME: This isn't the right check!
1633 if (T->isDependentType())
1634 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1635
1636 return NestedNameSpecifier::Create(*this, Prefix,
1637 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1638 T.getTypePtr());
1639 }
1640
1641 case NestedNameSpecifier::Global:
1642 // The global specifier is canonical and unique.
1643 return NNS;
1644 }
1645
1646 // Required to silence a GCC warning
1647 return 0;
1648}
1649
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001650
1651const ArrayType *ASTContext::getAsArrayType(QualType T) {
1652 // Handle the non-qualified case efficiently.
1653 if (T.getCVRQualifiers() == 0) {
1654 // Handle the common positive case fast.
1655 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1656 return AT;
1657 }
1658
1659 // Handle the common negative case fast, ignoring CVR qualifiers.
1660 QualType CType = T->getCanonicalTypeInternal();
1661
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001662 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001663 // test.
1664 if (!isa<ArrayType>(CType) &&
1665 !isa<ArrayType>(CType.getUnqualifiedType()))
1666 return 0;
1667
1668 // Apply any CVR qualifiers from the array type to the element type. This
1669 // implements C99 6.7.3p8: "If the specification of an array type includes
1670 // any type qualifiers, the element type is so qualified, not the array type."
1671
1672 // If we get here, we either have type qualifiers on the type, or we have
1673 // sugar such as a typedef in the way. If we have type qualifiers on the type
1674 // we must propagate them down into the elemeng type.
1675 unsigned CVRQuals = T.getCVRQualifiers();
1676 unsigned AddrSpace = 0;
1677 Type *Ty = T.getTypePtr();
1678
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001679 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001680 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001681 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1682 AddrSpace = EXTQT->getAddressSpace();
1683 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001684 } else {
1685 T = Ty->getDesugaredType();
1686 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1687 break;
1688 CVRQuals |= T.getCVRQualifiers();
1689 Ty = T.getTypePtr();
1690 }
1691 }
1692
1693 // If we have a simple case, just return now.
1694 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1695 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1696 return ATy;
1697
1698 // Otherwise, we have an array and we have qualifiers on it. Push the
1699 // qualifiers into the array element type and return a new array type.
1700 // Get the canonical version of the element with the extra qualifiers on it.
1701 // This can recursively sink qualifiers through multiple levels of arrays.
1702 QualType NewEltTy = ATy->getElementType();
1703 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001704 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001705 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1706
1707 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1708 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1709 CAT->getSizeModifier(),
1710 CAT->getIndexTypeQualifier()));
1711 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1712 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1713 IAT->getSizeModifier(),
1714 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001715
Douglas Gregor898574e2008-12-05 23:32:09 +00001716 if (const DependentSizedArrayType *DSAT
1717 = dyn_cast<DependentSizedArrayType>(ATy))
1718 return cast<ArrayType>(
1719 getDependentSizedArrayType(NewEltTy,
1720 DSAT->getSizeExpr(),
1721 DSAT->getSizeModifier(),
1722 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001723
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001724 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1725 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1726 VAT->getSizeModifier(),
1727 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001728}
1729
1730
Chris Lattnere6327742008-04-02 05:18:44 +00001731/// getArrayDecayedType - Return the properly qualified result of decaying the
1732/// specified array type to a pointer. This operation is non-trivial when
1733/// handling typedefs etc. The canonical type of "T" must be an array type,
1734/// this returns a pointer to a properly qualified element of the array.
1735///
1736/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1737QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001738 // Get the element type with 'getAsArrayType' so that we don't lose any
1739 // typedefs in the element type of the array. This also handles propagation
1740 // of type qualifiers from the array type into the element type if present
1741 // (C99 6.7.3p8).
1742 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1743 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001744
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001745 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001746
1747 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001748 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001749}
1750
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001751QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001752 QualType ElemTy = VAT->getElementType();
1753
1754 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1755 return getBaseElementType(VAT);
1756
1757 return ElemTy;
1758}
1759
Reid Spencer5f016e22007-07-11 17:01:13 +00001760/// getFloatingRank - Return a relative rank for floating point types.
1761/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001762static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001763 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001764 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001765
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001766 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001767 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001768 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001769 case BuiltinType::Float: return FloatRank;
1770 case BuiltinType::Double: return DoubleRank;
1771 case BuiltinType::LongDouble: return LongDoubleRank;
1772 }
1773}
1774
Steve Naroff716c7302007-08-27 01:41:48 +00001775/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1776/// point or a complex type (based on typeDomain/typeSize).
1777/// 'typeDomain' is a real floating point or complex type.
1778/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001779QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1780 QualType Domain) const {
1781 FloatingRank EltRank = getFloatingRank(Size);
1782 if (Domain->isComplexType()) {
1783 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001784 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001785 case FloatRank: return FloatComplexTy;
1786 case DoubleRank: return DoubleComplexTy;
1787 case LongDoubleRank: return LongDoubleComplexTy;
1788 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001789 }
Chris Lattner1361b112008-04-06 23:58:54 +00001790
1791 assert(Domain->isRealFloatingType() && "Unknown domain!");
1792 switch (EltRank) {
1793 default: assert(0 && "getFloatingRank(): illegal value for rank");
1794 case FloatRank: return FloatTy;
1795 case DoubleRank: return DoubleTy;
1796 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001797 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001798}
1799
Chris Lattner7cfeb082008-04-06 23:55:33 +00001800/// getFloatingTypeOrder - Compare the rank of the two specified floating
1801/// point types, ignoring the domain of the type (i.e. 'double' ==
1802/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1803/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001804int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1805 FloatingRank LHSR = getFloatingRank(LHS);
1806 FloatingRank RHSR = getFloatingRank(RHS);
1807
1808 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001809 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001810 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001811 return 1;
1812 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001813}
1814
Chris Lattnerf52ab252008-04-06 22:59:24 +00001815/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1816/// routine will assert if passed a built-in type that isn't an integer or enum,
1817/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001818unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001819 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001820 if (EnumType* ET = dyn_cast<EnumType>(T))
1821 T = ET->getDecl()->getIntegerType().getTypePtr();
1822
1823 // There are two things which impact the integer rank: the width, and
1824 // the ordering of builtins. The builtin ordering is encoded in the
1825 // bottom three bits; the width is encoded in the bits above that.
1826 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1827 return FWIT->getWidth() << 3;
1828 }
1829
Chris Lattnerf52ab252008-04-06 22:59:24 +00001830 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001831 default: assert(0 && "getIntegerRank(): not a built-in integer");
1832 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001833 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001834 case BuiltinType::Char_S:
1835 case BuiltinType::Char_U:
1836 case BuiltinType::SChar:
1837 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001838 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001839 case BuiltinType::Short:
1840 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001841 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001842 case BuiltinType::Int:
1843 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001844 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001845 case BuiltinType::Long:
1846 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001847 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001848 case BuiltinType::LongLong:
1849 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001850 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001851 }
1852}
1853
Chris Lattner7cfeb082008-04-06 23:55:33 +00001854/// getIntegerTypeOrder - Returns the highest ranked integer type:
1855/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1856/// LHS < RHS, return -1.
1857int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001858 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1859 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001860 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001861
Chris Lattnerf52ab252008-04-06 22:59:24 +00001862 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1863 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001864
Chris Lattner7cfeb082008-04-06 23:55:33 +00001865 unsigned LHSRank = getIntegerRank(LHSC);
1866 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001867
Chris Lattner7cfeb082008-04-06 23:55:33 +00001868 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1869 if (LHSRank == RHSRank) return 0;
1870 return LHSRank > RHSRank ? 1 : -1;
1871 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001872
Chris Lattner7cfeb082008-04-06 23:55:33 +00001873 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1874 if (LHSUnsigned) {
1875 // If the unsigned [LHS] type is larger, return it.
1876 if (LHSRank >= RHSRank)
1877 return 1;
1878
1879 // If the signed type can represent all values of the unsigned type, it
1880 // wins. Because we are dealing with 2's complement and types that are
1881 // powers of two larger than each other, this is always safe.
1882 return -1;
1883 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001884
Chris Lattner7cfeb082008-04-06 23:55:33 +00001885 // If the unsigned [RHS] type is larger, return it.
1886 if (RHSRank >= LHSRank)
1887 return -1;
1888
1889 // If the signed type can represent all values of the unsigned type, it
1890 // wins. Because we are dealing with 2's complement and types that are
1891 // powers of two larger than each other, this is always safe.
1892 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001893}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001894
1895// getCFConstantStringType - Return the type used for constant CFStrings.
1896QualType ASTContext::getCFConstantStringType() {
1897 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001898 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001899 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001900 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001901 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001902
1903 // const int *isa;
1904 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001905 // int flags;
1906 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001907 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001908 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001909 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001910 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001911
Anders Carlsson71993dd2007-08-17 05:31:46 +00001912 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001913 for (unsigned i = 0; i < 4; ++i) {
1914 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1915 SourceLocation(), 0,
1916 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001917 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001918 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001919 }
1920
1921 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001922 }
1923
1924 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001925}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001926
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001927QualType ASTContext::getObjCFastEnumerationStateType()
1928{
1929 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001930 ObjCFastEnumerationStateTypeDecl =
1931 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1932 &Idents.get("__objcFastEnumerationState"));
1933
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001934 QualType FieldTypes[] = {
1935 UnsignedLongTy,
1936 getPointerType(ObjCIdType),
1937 getPointerType(UnsignedLongTy),
1938 getConstantArrayType(UnsignedLongTy,
1939 llvm::APInt(32, 5), ArrayType::Normal, 0)
1940 };
1941
Douglas Gregor44b43212008-12-11 16:49:14 +00001942 for (size_t i = 0; i < 4; ++i) {
1943 FieldDecl *Field = FieldDecl::Create(*this,
1944 ObjCFastEnumerationStateTypeDecl,
1945 SourceLocation(), 0,
1946 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001947 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001948 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001949 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001950
Douglas Gregor44b43212008-12-11 16:49:14 +00001951 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001952 }
1953
1954 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1955}
1956
Anders Carlssone8c49532007-10-29 06:33:42 +00001957// This returns true if a type has been typedefed to BOOL:
1958// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001959static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001960 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00001961 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1962 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001963
1964 return false;
1965}
1966
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001967/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001968/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001969int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001970 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001971
1972 // Make all integer and enum types at least as large as an int
1973 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001974 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001975 // Treat arrays as pointers, since that's how they're passed in.
1976 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001977 sz = getTypeSize(VoidPtrTy);
1978 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001979}
1980
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001981/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001982/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001983void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001984 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001985 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001986 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001987 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001988 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001989 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001990 // Compute size of all parameters.
1991 // Start with computing size of a pointer in number of bytes.
1992 // FIXME: There might(should) be a better way of doing this computation!
1993 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001994 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001995 // The first two arguments (self and _cmd) are pointers; account for
1996 // their size.
1997 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00001998 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1999 E = Decl->param_end(); PI != E; ++PI) {
2000 QualType PType = (*PI)->getType();
2001 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002002 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002003 ParmOffset += sz;
2004 }
2005 S += llvm::utostr(ParmOffset);
2006 S += "@0:";
2007 S += llvm::utostr(PtrSize);
2008
2009 // Argument types.
2010 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002011 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2012 E = Decl->param_end(); PI != E; ++PI) {
2013 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002014 QualType PType = PVDecl->getOriginalType();
2015 if (const ArrayType *AT =
2016 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
2017 // Use array's original type only if it has known number of
2018 // elements.
2019 if (!dyn_cast<ConstantArrayType>(AT))
2020 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002021 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002022 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002023 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002024 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002025 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002026 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002027 }
2028}
2029
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002030/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002031/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002032/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2033/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002034/// Property attributes are stored as a comma-delimited C string. The simple
2035/// attributes readonly and bycopy are encoded as single characters. The
2036/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2037/// encoded as single characters, followed by an identifier. Property types
2038/// are also encoded as a parametrized attribute. The characters used to encode
2039/// these attributes are defined by the following enumeration:
2040/// @code
2041/// enum PropertyAttributes {
2042/// kPropertyReadOnly = 'R', // property is read-only.
2043/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2044/// kPropertyByref = '&', // property is a reference to the value last assigned
2045/// kPropertyDynamic = 'D', // property is dynamic
2046/// kPropertyGetter = 'G', // followed by getter selector name
2047/// kPropertySetter = 'S', // followed by setter selector name
2048/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2049/// kPropertyType = 't' // followed by old-style type encoding.
2050/// kPropertyWeak = 'W' // 'weak' property
2051/// kPropertyStrong = 'P' // property GC'able
2052/// kPropertyNonAtomic = 'N' // property non-atomic
2053/// };
2054/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002055void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2056 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002057 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002058 // Collect information from the property implementation decl(s).
2059 bool Dynamic = false;
2060 ObjCPropertyImplDecl *SynthesizePID = 0;
2061
2062 // FIXME: Duplicated code due to poor abstraction.
2063 if (Container) {
2064 if (const ObjCCategoryImplDecl *CID =
2065 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2066 for (ObjCCategoryImplDecl::propimpl_iterator
2067 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2068 ObjCPropertyImplDecl *PID = *i;
2069 if (PID->getPropertyDecl() == PD) {
2070 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2071 Dynamic = true;
2072 } else {
2073 SynthesizePID = PID;
2074 }
2075 }
2076 }
2077 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002078 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002079 for (ObjCCategoryImplDecl::propimpl_iterator
2080 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2081 ObjCPropertyImplDecl *PID = *i;
2082 if (PID->getPropertyDecl() == PD) {
2083 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2084 Dynamic = true;
2085 } else {
2086 SynthesizePID = PID;
2087 }
2088 }
2089 }
2090 }
2091 }
2092
2093 // FIXME: This is not very efficient.
2094 S = "T";
2095
2096 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002097 // GCC has some special rules regarding encoding of properties which
2098 // closely resembles encoding of ivars.
2099 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
2100 true /* outermost type */,
2101 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002102
2103 if (PD->isReadOnly()) {
2104 S += ",R";
2105 } else {
2106 switch (PD->getSetterKind()) {
2107 case ObjCPropertyDecl::Assign: break;
2108 case ObjCPropertyDecl::Copy: S += ",C"; break;
2109 case ObjCPropertyDecl::Retain: S += ",&"; break;
2110 }
2111 }
2112
2113 // It really isn't clear at all what this means, since properties
2114 // are "dynamic by default".
2115 if (Dynamic)
2116 S += ",D";
2117
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002118 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2119 S += ",N";
2120
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002121 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2122 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002123 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002124 }
2125
2126 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2127 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002128 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002129 }
2130
2131 if (SynthesizePID) {
2132 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2133 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002134 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002135 }
2136
2137 // FIXME: OBJCGC: weak & strong
2138}
2139
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002140/// getLegacyIntegralTypeEncoding -
2141/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002142/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002143/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2144///
2145void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2146 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2147 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002148 if (BT->getKind() == BuiltinType::ULong &&
2149 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002150 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002151 else
2152 if (BT->getKind() == BuiltinType::Long &&
2153 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002154 PointeeTy = IntTy;
2155 }
2156 }
2157}
2158
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002159void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002160 FieldDecl *Field) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002161 // We follow the behavior of gcc, expanding structures which are
2162 // directly pointed to, and expanding embedded structures. Note that
2163 // these rules are sufficient to prevent recursive encoding of the
2164 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002165 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2166 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002167}
2168
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002169static void EncodeBitField(const ASTContext *Context, std::string& S,
2170 FieldDecl *FD) {
2171 const Expr *E = FD->getBitWidth();
2172 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2173 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2174 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2175 S += 'b';
2176 S += llvm::utostr(N);
2177}
2178
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002179void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2180 bool ExpandPointedToStructures,
2181 bool ExpandStructures,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002182 FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002183 bool OutermostType,
2184 bool EncodingProperty) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00002185 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002186 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002187 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002188 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002189 else {
2190 char encoding;
2191 switch (BT->getKind()) {
2192 default: assert(0 && "Unhandled builtin type kind");
2193 case BuiltinType::Void: encoding = 'v'; break;
2194 case BuiltinType::Bool: encoding = 'B'; break;
2195 case BuiltinType::Char_U:
2196 case BuiltinType::UChar: encoding = 'C'; break;
2197 case BuiltinType::UShort: encoding = 'S'; break;
2198 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002199 case BuiltinType::ULong:
2200 encoding =
2201 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2202 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002203 case BuiltinType::ULongLong: encoding = 'Q'; break;
2204 case BuiltinType::Char_S:
2205 case BuiltinType::SChar: encoding = 'c'; break;
2206 case BuiltinType::Short: encoding = 's'; break;
2207 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002208 case BuiltinType::Long:
2209 encoding =
2210 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2211 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002212 case BuiltinType::LongLong: encoding = 'q'; break;
2213 case BuiltinType::Float: encoding = 'f'; break;
2214 case BuiltinType::Double: encoding = 'd'; break;
2215 case BuiltinType::LongDouble: encoding = 'd'; break;
2216 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002217
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002218 S += encoding;
2219 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002220 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002221 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002222 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2223 ExpandPointedToStructures,
2224 ExpandStructures, FD);
2225 if (FD || EncodingProperty) {
2226 // Note that we do extended encoding of protocol qualifer list
2227 // Only when doing ivar or property encoding.
2228 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2229 S += '"';
2230 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2231 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2232 S += '<';
2233 S += Proto->getNameAsString();
2234 S += '>';
2235 }
2236 S += '"';
2237 }
2238 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002239 }
2240 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002241 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002242 bool isReadOnly = false;
2243 // For historical/compatibility reasons, the read-only qualifier of the
2244 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2245 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2246 // Also, do not emit the 'r' for anything but the outermost type!
2247 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2248 if (OutermostType && T.isConstQualified()) {
2249 isReadOnly = true;
2250 S += 'r';
2251 }
2252 }
2253 else if (OutermostType) {
2254 QualType P = PointeeTy;
2255 while (P->getAsPointerType())
2256 P = P->getAsPointerType()->getPointeeType();
2257 if (P.isConstQualified()) {
2258 isReadOnly = true;
2259 S += 'r';
2260 }
2261 }
2262 if (isReadOnly) {
2263 // Another legacy compatibility encoding. Some ObjC qualifier and type
2264 // combinations need to be rearranged.
2265 // Rewrite "in const" from "nr" to "rn"
2266 const char * s = S.c_str();
2267 int len = S.length();
2268 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2269 std::string replace = "rn";
2270 S.replace(S.end()-2, S.end(), replace);
2271 }
2272 }
Steve Naroff389bf462009-02-12 17:52:19 +00002273 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002274 S += '@';
2275 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002276 }
2277 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002278 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002279 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002280 // Another historical/compatibility reason.
2281 // We encode the underlying type which comes out as
2282 // {...};
2283 S += '^';
2284 getObjCEncodingForTypeImpl(PointeeTy, S,
2285 false, ExpandPointedToStructures,
2286 NULL);
2287 return;
2288 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002289 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002290 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002291 const ObjCInterfaceType *OIT =
2292 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002293 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002294 S += '"';
2295 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002296 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2297 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2298 S += '<';
2299 S += Proto->getNameAsString();
2300 S += '>';
2301 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002302 S += '"';
2303 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002304 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002305 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002306 S += '#';
2307 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002308 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002309 S += ':';
2310 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002311 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002312
2313 if (PointeeTy->isCharType()) {
2314 // char pointer types should be encoded as '*' unless it is a
2315 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002316 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002317 S += '*';
2318 return;
2319 }
2320 }
2321
2322 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002323 getLegacyIntegralTypeEncoding(PointeeTy);
2324
2325 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002326 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002327 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002328 } else if (const ArrayType *AT =
2329 // Ignore type qualifiers etc.
2330 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002331 if (isa<IncompleteArrayType>(AT)) {
2332 // Incomplete arrays are encoded as a pointer to the array element.
2333 S += '^';
2334
2335 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2336 false, ExpandStructures, FD);
2337 } else {
2338 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002339
Anders Carlsson559a8332009-02-22 01:38:57 +00002340 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2341 S += llvm::utostr(CAT->getSize().getZExtValue());
2342 else {
2343 //Variable length arrays are encoded as a regular array with 0 elements.
2344 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2345 S += '0';
2346 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002347
Anders Carlsson559a8332009-02-22 01:38:57 +00002348 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2349 false, ExpandStructures, FD);
2350 S += ']';
2351 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002352 } else if (T->getAsFunctionType()) {
2353 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002354 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002355 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002356 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002357 // Anonymous structures print as '?'
2358 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2359 S += II->getName();
2360 } else {
2361 S += '?';
2362 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002363 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002364 S += '=';
Douglas Gregor44b43212008-12-11 16:49:14 +00002365 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2366 FieldEnd = RDecl->field_end();
2367 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002368 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002369 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002370 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002371 S += '"';
2372 }
2373
2374 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002375 if (Field->isBitField()) {
2376 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2377 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002378 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002379 QualType qt = Field->getType();
2380 getLegacyIntegralTypeEncoding(qt);
2381 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002382 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002383 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002384 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002385 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002386 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002387 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002388 if (FD && FD->isBitField())
2389 EncodeBitField(this, S, FD);
2390 else
2391 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002392 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002393 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002394 } else if (T->isObjCInterfaceType()) {
2395 // @encode(class_name)
2396 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2397 S += '{';
2398 const IdentifierInfo *II = OI->getIdentifier();
2399 S += II->getName();
2400 S += '=';
2401 std::vector<FieldDecl*> RecFields;
2402 CollectObjCIvars(OI, RecFields);
2403 for (unsigned int i = 0; i != RecFields.size(); i++) {
2404 if (RecFields[i]->isBitField())
2405 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2406 RecFields[i]);
2407 else
2408 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2409 FD);
2410 }
2411 S += '}';
2412 }
2413 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002414 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002415}
2416
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002417void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002418 std::string& S) const {
2419 if (QT & Decl::OBJC_TQ_In)
2420 S += 'n';
2421 if (QT & Decl::OBJC_TQ_Inout)
2422 S += 'N';
2423 if (QT & Decl::OBJC_TQ_Out)
2424 S += 'o';
2425 if (QT & Decl::OBJC_TQ_Bycopy)
2426 S += 'O';
2427 if (QT & Decl::OBJC_TQ_Byref)
2428 S += 'R';
2429 if (QT & Decl::OBJC_TQ_Oneway)
2430 S += 'V';
2431}
2432
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002433void ASTContext::setBuiltinVaListType(QualType T)
2434{
2435 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2436
2437 BuiltinVaListType = T;
2438}
2439
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002440void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002441{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002442 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002443
2444 // typedef struct objc_object *id;
2445 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002446 // User error - caller will issue diagnostics.
2447 if (!ptr)
2448 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002449 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002450 // User error - caller will issue diagnostics.
2451 if (!rec)
2452 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002453 IdStructType = rec;
2454}
2455
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002456void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002457{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002458 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002459
2460 // typedef struct objc_selector *SEL;
2461 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002462 if (!ptr)
2463 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002464 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002465 if (!rec)
2466 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002467 SelStructType = rec;
2468}
2469
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002470void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002471{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002472 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002473}
2474
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002475void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002476{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002477 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002478
2479 // typedef struct objc_class *Class;
2480 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2481 assert(ptr && "'Class' incorrectly typed");
2482 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2483 assert(rec && "'Class' incorrectly typed");
2484 ClassStructType = rec;
2485}
2486
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002487void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2488 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002489 "'NSConstantString' type already set!");
2490
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002491 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002492}
2493
Douglas Gregor7532dc62009-03-30 22:58:21 +00002494/// \brief Retrieve the template name that represents a qualified
2495/// template name such as \c std::vector.
2496TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2497 bool TemplateKeyword,
2498 TemplateDecl *Template) {
2499 llvm::FoldingSetNodeID ID;
2500 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2501
2502 void *InsertPos = 0;
2503 QualifiedTemplateName *QTN =
2504 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2505 if (!QTN) {
2506 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2507 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2508 }
2509
2510 return TemplateName(QTN);
2511}
2512
2513/// \brief Retrieve the template name that represents a dependent
2514/// template name such as \c MetaFun::template apply.
2515TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2516 const IdentifierInfo *Name) {
2517 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2518
2519 llvm::FoldingSetNodeID ID;
2520 DependentTemplateName::Profile(ID, NNS, Name);
2521
2522 void *InsertPos = 0;
2523 DependentTemplateName *QTN =
2524 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2525
2526 if (QTN)
2527 return TemplateName(QTN);
2528
2529 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2530 if (CanonNNS == NNS) {
2531 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2532 } else {
2533 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2534 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2535 }
2536
2537 DependentTemplateNames.InsertNode(QTN, InsertPos);
2538 return TemplateName(QTN);
2539}
2540
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002541/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002542/// TargetInfo, produce the corresponding type. The unsigned @p Type
2543/// is actually a value of type @c TargetInfo::IntType.
2544QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002545 switch (Type) {
2546 case TargetInfo::NoInt: return QualType();
2547 case TargetInfo::SignedShort: return ShortTy;
2548 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2549 case TargetInfo::SignedInt: return IntTy;
2550 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2551 case TargetInfo::SignedLong: return LongTy;
2552 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2553 case TargetInfo::SignedLongLong: return LongLongTy;
2554 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2555 }
2556
2557 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002558 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002559}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002560
2561//===----------------------------------------------------------------------===//
2562// Type Predicates.
2563//===----------------------------------------------------------------------===//
2564
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002565/// isObjCNSObjectType - Return true if this is an NSObject object using
2566/// NSObject attribute on a c-style pointer type.
2567/// FIXME - Make it work directly on types.
2568///
2569bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2570 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2571 if (TypedefDecl *TD = TDT->getDecl())
2572 if (TD->getAttr<ObjCNSObjectAttr>())
2573 return true;
2574 }
2575 return false;
2576}
2577
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002578/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2579/// to an object type. This includes "id" and "Class" (two 'special' pointers
2580/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2581/// ID type).
2582bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002583 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002584 return true;
2585
Steve Naroff6ae98502008-10-21 18:24:04 +00002586 // Blocks are objects.
2587 if (Ty->isBlockPointerType())
2588 return true;
2589
2590 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002591 if (!Ty->isPointerType())
2592 return false;
2593
2594 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2595 // pointer types. This looks for the typedef specifically, not for the
2596 // underlying type.
Eli Friedman5fdeae12009-03-22 23:00:19 +00002597 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2598 Ty.getUnqualifiedType() == getObjCClassType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002599 return true;
2600
2601 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002602 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2603 return true;
2604
2605 // If is has NSObject attribute, OK as well.
2606 return isObjCNSObjectType(Ty);
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002607}
2608
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002609/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2610/// garbage collection attribute.
2611///
2612QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002613 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002614 if (getLangOptions().ObjC1 &&
2615 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002616 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002617 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002618 // (or pointers to them) be treated as though they were declared
2619 // as __strong.
2620 if (GCAttrs == QualType::GCNone) {
2621 if (isObjCObjectPointerType(Ty))
2622 GCAttrs = QualType::Strong;
2623 else if (Ty->isPointerType())
2624 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2625 }
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002626 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002627 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002628}
2629
Chris Lattner6ac46a42008-04-07 06:51:04 +00002630//===----------------------------------------------------------------------===//
2631// Type Compatibility Testing
2632//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002633
Steve Naroff1c7d0672008-09-04 15:10:53 +00002634/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002635/// block types. Types must be strictly compatible here. For example,
2636/// C unfortunately doesn't produce an error for the following:
2637///
2638/// int (*emptyArgFunc)();
2639/// int (*intArgList)(int) = emptyArgFunc;
2640///
2641/// For blocks, we will produce an error for the following (similar to C++):
2642///
2643/// int (^emptyArgBlock)();
2644/// int (^intArgBlock)(int) = emptyArgBlock;
2645///
2646/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2647///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002648bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002649 const FunctionType *lbase = lhs->getAsFunctionType();
2650 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002651 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2652 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Steve Naroffc0febd52008-12-10 17:49:55 +00002653 if (lproto && rproto)
2654 return !mergeTypes(lhs, rhs).isNull();
2655 return false;
Steve Naroff1c7d0672008-09-04 15:10:53 +00002656}
2657
Chris Lattner6ac46a42008-04-07 06:51:04 +00002658/// areCompatVectorTypes - Return true if the two specified vector types are
2659/// compatible.
2660static bool areCompatVectorTypes(const VectorType *LHS,
2661 const VectorType *RHS) {
2662 assert(LHS->isCanonical() && RHS->isCanonical());
2663 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002664 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002665}
2666
Eli Friedman3d815e72008-08-22 00:56:42 +00002667/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002668/// compatible for assignment from RHS to LHS. This handles validation of any
2669/// protocol qualifiers on the LHS or RHS.
2670///
Eli Friedman3d815e72008-08-22 00:56:42 +00002671bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2672 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002673 // Verify that the base decls are compatible: the RHS must be a subclass of
2674 // the LHS.
2675 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2676 return false;
2677
2678 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2679 // protocol qualified at all, then we are good.
2680 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2681 return true;
2682
2683 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2684 // isn't a superset.
2685 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2686 return true; // FIXME: should return false!
2687
2688 // Finally, we must have two protocol-qualified interfaces.
2689 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2690 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002691
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002692 // All LHS protocols must have a presence on the RHS.
2693 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002694
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002695 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2696 LHSPE = LHSP->qual_end();
2697 LHSPI != LHSPE; LHSPI++) {
2698 bool RHSImplementsProtocol = false;
2699
2700 // If the RHS doesn't implement the protocol on the left, the types
2701 // are incompatible.
2702 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2703 RHSPE = RHSP->qual_end();
2704 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2705 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2706 RHSImplementsProtocol = true;
2707 }
2708 // FIXME: For better diagnostics, consider passing back the protocol name.
2709 if (!RHSImplementsProtocol)
2710 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002711 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002712 // The RHS implements all protocols listed on the LHS.
2713 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002714}
2715
Steve Naroff389bf462009-02-12 17:52:19 +00002716bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2717 // get the "pointed to" types
2718 const PointerType *LHSPT = LHS->getAsPointerType();
2719 const PointerType *RHSPT = RHS->getAsPointerType();
2720
2721 if (!LHSPT || !RHSPT)
2722 return false;
2723
2724 QualType lhptee = LHSPT->getPointeeType();
2725 QualType rhptee = RHSPT->getPointeeType();
2726 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2727 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2728 // ID acts sort of like void* for ObjC interfaces
2729 if (LHSIface && isObjCIdStructType(rhptee))
2730 return true;
2731 if (RHSIface && isObjCIdStructType(lhptee))
2732 return true;
2733 if (!LHSIface || !RHSIface)
2734 return false;
2735 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2736 canAssignObjCInterfaces(RHSIface, LHSIface);
2737}
2738
Steve Naroffec0550f2007-10-15 20:41:53 +00002739/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2740/// both shall have the identically qualified version of a compatible type.
2741/// C99 6.2.7p1: Two types have compatible types if their types are the
2742/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002743bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2744 return !mergeTypes(LHS, RHS).isNull();
2745}
2746
2747QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2748 const FunctionType *lbase = lhs->getAsFunctionType();
2749 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002750 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2751 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002752 bool allLTypes = true;
2753 bool allRTypes = true;
2754
2755 // Check return type
2756 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2757 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002758 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2759 allLTypes = false;
2760 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2761 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002762
2763 if (lproto && rproto) { // two C99 style function prototypes
2764 unsigned lproto_nargs = lproto->getNumArgs();
2765 unsigned rproto_nargs = rproto->getNumArgs();
2766
2767 // Compatible functions must have the same number of arguments
2768 if (lproto_nargs != rproto_nargs)
2769 return QualType();
2770
2771 // Variadic and non-variadic functions aren't compatible
2772 if (lproto->isVariadic() != rproto->isVariadic())
2773 return QualType();
2774
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002775 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2776 return QualType();
2777
Eli Friedman3d815e72008-08-22 00:56:42 +00002778 // Check argument compatibility
2779 llvm::SmallVector<QualType, 10> types;
2780 for (unsigned i = 0; i < lproto_nargs; i++) {
2781 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2782 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2783 QualType argtype = mergeTypes(largtype, rargtype);
2784 if (argtype.isNull()) return QualType();
2785 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002786 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2787 allLTypes = false;
2788 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2789 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002790 }
2791 if (allLTypes) return lhs;
2792 if (allRTypes) return rhs;
2793 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002794 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002795 }
2796
2797 if (lproto) allRTypes = false;
2798 if (rproto) allLTypes = false;
2799
Douglas Gregor72564e72009-02-26 23:50:07 +00002800 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002801 if (proto) {
2802 if (proto->isVariadic()) return QualType();
2803 // Check that the types are compatible with the types that
2804 // would result from default argument promotions (C99 6.7.5.3p15).
2805 // The only types actually affected are promotable integer
2806 // types and floats, which would be passed as a different
2807 // type depending on whether the prototype is visible.
2808 unsigned proto_nargs = proto->getNumArgs();
2809 for (unsigned i = 0; i < proto_nargs; ++i) {
2810 QualType argTy = proto->getArgType(i);
2811 if (argTy->isPromotableIntegerType() ||
2812 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2813 return QualType();
2814 }
2815
2816 if (allLTypes) return lhs;
2817 if (allRTypes) return rhs;
2818 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002819 proto->getNumArgs(), lproto->isVariadic(),
2820 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002821 }
2822
2823 if (allLTypes) return lhs;
2824 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002825 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002826}
2827
2828QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002829 // C++ [expr]: If an expression initially has the type "reference to T", the
2830 // type is adjusted to "T" prior to any further analysis, the expression
2831 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002832 // expression is an lvalue unless the reference is an rvalue reference and
2833 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00002834 // FIXME: C++ shouldn't be going through here! The rules are different
2835 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002836 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2837 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00002838 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002839 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002840 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002841 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002842
Eli Friedman3d815e72008-08-22 00:56:42 +00002843 QualType LHSCan = getCanonicalType(LHS),
2844 RHSCan = getCanonicalType(RHS);
2845
2846 // If two types are identical, they are compatible.
2847 if (LHSCan == RHSCan)
2848 return LHS;
2849
2850 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002851 // Note that we handle extended qualifiers later, in the
2852 // case for ExtQualType.
2853 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00002854 return QualType();
2855
2856 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2857 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2858
Chris Lattner1adb8832008-01-14 05:45:46 +00002859 // We want to consider the two function types to be the same for these
2860 // comparisons, just force one to the other.
2861 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2862 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002863
2864 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002865 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2866 LHSClass = Type::ConstantArray;
2867 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2868 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002869
Nate Begeman213541a2008-04-18 23:10:10 +00002870 // Canonicalize ExtVector -> Vector.
2871 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2872 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002873
Chris Lattnerb0489812008-04-07 06:38:24 +00002874 // Consider qualified interfaces and interfaces the same.
2875 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2876 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002877
Chris Lattnera36a61f2008-04-07 05:43:21 +00002878 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002879 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002880 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2881 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2882
2883 // ID acts sort of like void* for ObjC interfaces
2884 if (LHSIface && isObjCIdStructType(RHS))
2885 return LHS;
2886 if (RHSIface && isObjCIdStructType(LHS))
2887 return RHS;
2888
Steve Naroffbc76dd02008-12-10 22:14:21 +00002889 // ID is compatible with all qualified id types.
2890 if (LHS->isObjCQualifiedIdType()) {
2891 if (const PointerType *PT = RHS->getAsPointerType()) {
2892 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002893 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002894 return LHS;
2895 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2896 // Unfortunately, this API is part of Sema (which we don't have access
2897 // to. Need to refactor. The following check is insufficient, since we
2898 // need to make sure the class implements the protocol.
2899 if (pType->isObjCInterfaceType())
2900 return LHS;
2901 }
2902 }
2903 if (RHS->isObjCQualifiedIdType()) {
2904 if (const PointerType *PT = LHS->getAsPointerType()) {
2905 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002906 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002907 return RHS;
2908 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2909 // Unfortunately, this API is part of Sema (which we don't have access
2910 // to. Need to refactor. The following check is insufficient, since we
2911 // need to make sure the class implements the protocol.
2912 if (pType->isObjCInterfaceType())
2913 return RHS;
2914 }
2915 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002916 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2917 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002918 if (const EnumType* ETy = LHS->getAsEnumType()) {
2919 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2920 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002921 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002922 if (const EnumType* ETy = RHS->getAsEnumType()) {
2923 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2924 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002925 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002926
Eli Friedman3d815e72008-08-22 00:56:42 +00002927 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002928 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002929
Steve Naroff4a746782008-01-09 22:43:08 +00002930 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002931 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00002932#define TYPE(Class, Base)
2933#define ABSTRACT_TYPE(Class, Base)
2934#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2935#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2936#include "clang/AST/TypeNodes.def"
2937 assert(false && "Non-canonical and dependent types shouldn't get here");
2938 return QualType();
2939
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002940 case Type::LValueReference:
2941 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00002942 case Type::MemberPointer:
2943 assert(false && "C++ should never be in mergeTypes");
2944 return QualType();
2945
2946 case Type::IncompleteArray:
2947 case Type::VariableArray:
2948 case Type::FunctionProto:
2949 case Type::ExtVector:
2950 case Type::ObjCQualifiedInterface:
2951 assert(false && "Types are eliminated above");
2952 return QualType();
2953
Chris Lattner1adb8832008-01-14 05:45:46 +00002954 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002955 {
2956 // Merge two pointer types, while trying to preserve typedef info
2957 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2958 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2959 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2960 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002961 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2962 return LHS;
2963 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2964 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002965 return getPointerType(ResultType);
2966 }
Steve Naroffc0febd52008-12-10 17:49:55 +00002967 case Type::BlockPointer:
2968 {
2969 // Merge two block pointer types, while trying to preserve typedef info
2970 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2971 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2972 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2973 if (ResultType.isNull()) return QualType();
2974 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2975 return LHS;
2976 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2977 return RHS;
2978 return getBlockPointerType(ResultType);
2979 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002980 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002981 {
2982 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2983 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2984 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2985 return QualType();
2986
2987 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2988 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2989 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2990 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002991 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2992 return LHS;
2993 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2994 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002995 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2996 ArrayType::ArraySizeModifier(), 0);
2997 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2998 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002999 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3000 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003001 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3002 return LHS;
3003 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3004 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003005 if (LVAT) {
3006 // FIXME: This isn't correct! But tricky to implement because
3007 // the array's size has to be the size of LHS, but the type
3008 // has to be different.
3009 return LHS;
3010 }
3011 if (RVAT) {
3012 // FIXME: This isn't correct! But tricky to implement because
3013 // the array's size has to be the size of RHS, but the type
3014 // has to be different.
3015 return RHS;
3016 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003017 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3018 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003019 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003020 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003021 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003022 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003023 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003024 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003025 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003026 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3027 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003028 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003029 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003030 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003031 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003032 case Type::Complex:
3033 // Distinct complex types are incompatible.
3034 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003035 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003036 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003037 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3038 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003039 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003040 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003041 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003042 // FIXME: This should be type compatibility, e.g. whether
3043 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003044 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3045 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3046 if (LHSIface && RHSIface &&
3047 canAssignObjCInterfaces(LHSIface, RHSIface))
3048 return LHS;
3049
Eli Friedman3d815e72008-08-22 00:56:42 +00003050 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003051 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003052 case Type::ObjCQualifiedId:
3053 // Distinct qualified id's are not compatible.
3054 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003055 case Type::FixedWidthInt:
3056 // Distinct fixed-width integers are not compatible.
3057 return QualType();
3058 case Type::ObjCQualifiedClass:
3059 // Distinct qualified classes are not compatible.
3060 return QualType();
3061 case Type::ExtQual:
3062 // FIXME: ExtQual types can be compatible even if they're not
3063 // identical!
3064 return QualType();
3065 // First attempt at an implementation, but I'm not really sure it's
3066 // right...
3067#if 0
3068 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3069 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3070 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3071 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3072 return QualType();
3073 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3074 LHSBase = QualType(LQual->getBaseType(), 0);
3075 RHSBase = QualType(RQual->getBaseType(), 0);
3076 ResultType = mergeTypes(LHSBase, RHSBase);
3077 if (ResultType.isNull()) return QualType();
3078 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3079 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3080 return LHS;
3081 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3082 return RHS;
3083 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3084 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3085 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3086 return ResultType;
3087#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003088
3089 case Type::TemplateSpecialization:
3090 assert(false && "Dependent types have no size");
3091 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003092 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003093
3094 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003095}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003096
Chris Lattner5426bf62008-04-07 07:01:58 +00003097//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003098// Integer Predicates
3099//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003100
Eli Friedmanad74a752008-06-28 06:23:08 +00003101unsigned ASTContext::getIntWidth(QualType T) {
3102 if (T == BoolTy)
3103 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003104 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3105 return FWIT->getWidth();
3106 }
3107 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003108 return (unsigned)getTypeSize(T);
3109}
3110
3111QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3112 assert(T->isSignedIntegerType() && "Unexpected type");
3113 if (const EnumType* ETy = T->getAsEnumType())
3114 T = ETy->getDecl()->getIntegerType();
3115 const BuiltinType* BTy = T->getAsBuiltinType();
3116 assert (BTy && "Unexpected signed integer type");
3117 switch (BTy->getKind()) {
3118 case BuiltinType::Char_S:
3119 case BuiltinType::SChar:
3120 return UnsignedCharTy;
3121 case BuiltinType::Short:
3122 return UnsignedShortTy;
3123 case BuiltinType::Int:
3124 return UnsignedIntTy;
3125 case BuiltinType::Long:
3126 return UnsignedLongTy;
3127 case BuiltinType::LongLong:
3128 return UnsignedLongLongTy;
3129 default:
3130 assert(0 && "Unexpected signed integer type");
3131 return QualType();
3132 }
3133}
3134
3135
3136//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00003137// Serialization Support
3138//===----------------------------------------------------------------------===//
3139
Chris Lattnera9376d42009-03-28 03:45:20 +00003140enum {
3141 BasicMetadataBlock = 1,
3142 ASTContextBlock = 2,
3143 DeclsBlock = 3
3144};
3145
Chris Lattner557c5b12009-03-28 04:27:18 +00003146void ASTContext::EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const{
3147 // Create bitstream.
3148 llvm::BitstreamWriter Stream(Buffer);
3149
3150 // Emit the preamble.
3151 Stream.Emit((unsigned)'B', 8);
3152 Stream.Emit((unsigned)'C', 8);
3153 Stream.Emit(0xC, 4);
3154 Stream.Emit(0xF, 4);
3155 Stream.Emit(0xE, 4);
3156 Stream.Emit(0x0, 4);
3157
3158 // Create serializer.
3159 llvm::Serializer S(Stream);
3160
Chris Lattnera9376d42009-03-28 03:45:20 +00003161 // ===---------------------------------------------------===/
3162 // Serialize the "Translation Unit" metadata.
3163 // ===---------------------------------------------------===/
3164
3165 // Emit ASTContext.
3166 S.EnterBlock(ASTContextBlock);
3167 S.EmitOwnedPtr(this);
3168 S.ExitBlock(); // exit "ASTContextBlock"
3169
3170 S.EnterBlock(BasicMetadataBlock);
3171
3172 // Block for SourceManager and Target. Allows easy skipping
3173 // around to the block for the Selectors during deserialization.
3174 S.EnterBlock();
3175
3176 // Emit the SourceManager.
3177 S.Emit(getSourceManager());
3178
3179 // Emit the Target.
3180 S.EmitPtr(&Target);
3181 S.EmitCStr(Target.getTargetTriple());
3182
3183 S.ExitBlock(); // exit "SourceManager and Target Block"
3184
3185 // Emit the Selectors.
3186 S.Emit(Selectors);
3187
3188 // Emit the Identifier Table.
3189 S.Emit(Idents);
3190
3191 S.ExitBlock(); // exit "BasicMetadataBlock"
3192}
3193
3194
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003195/// Emit - Serialize an ASTContext object to Bitcode.
3196void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003197 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00003198 S.EmitRef(SourceMgr);
3199 S.EmitRef(Target);
3200 S.EmitRef(Idents);
3201 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003202
Ted Kremenekfee04522007-10-31 22:44:07 +00003203 // Emit the size of the type vector so that we can reserve that size
3204 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00003205 S.EmitInt(Types.size());
3206
Ted Kremenek03ed4402007-11-13 22:02:55 +00003207 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3208 I!=E;++I)
3209 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00003210
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003211 S.EmitOwnedPtr(TUDecl);
3212
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003213 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003214}
3215
Chris Lattner557c5b12009-03-28 04:27:18 +00003216
3217ASTContext *ASTContext::ReadASTBitcodeBuffer(llvm::MemoryBuffer &Buffer,
3218 FileManager &FMgr) {
3219 // Check if the file is of the proper length.
3220 if (Buffer.getBufferSize() & 0x3) {
3221 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
3222 return 0;
3223 }
3224
3225 // Create the bitstream reader.
3226 unsigned char *BufPtr = (unsigned char *)Buffer.getBufferStart();
3227 llvm::BitstreamReader Stream(BufPtr, BufPtr+Buffer.getBufferSize());
3228
3229 if (Stream.Read(8) != 'B' ||
3230 Stream.Read(8) != 'C' ||
3231 Stream.Read(4) != 0xC ||
3232 Stream.Read(4) != 0xF ||
3233 Stream.Read(4) != 0xE ||
3234 Stream.Read(4) != 0x0) {
3235 // FIXME: Provide diagnostic.
3236 return NULL;
3237 }
3238
3239 // Create the deserializer.
3240 llvm::Deserializer Dezr(Stream);
3241
Chris Lattnera9376d42009-03-28 03:45:20 +00003242 // ===---------------------------------------------------===/
3243 // Deserialize the "Translation Unit" metadata.
3244 // ===---------------------------------------------------===/
3245
3246 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
3247 // (which will appear earlier) and record its location.
3248
3249 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
3250 assert (FoundBlock);
3251
3252 llvm::Deserializer::Location ASTContextBlockLoc =
3253 Dezr.getCurrentBlockLocation();
3254
3255 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
3256 assert (FoundBlock);
3257
3258 // Read the SourceManager.
3259 SourceManager::CreateAndRegister(Dezr, FMgr);
3260
3261 { // Read the TargetInfo.
3262 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
3263 char* triple = Dezr.ReadCStr(NULL,0,true);
3264 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
3265 delete [] triple;
3266 }
3267
3268 // For Selectors, we must read the identifier table first because the
3269 // SelectorTable depends on the identifiers being already deserialized.
3270 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
3271 Dezr.SkipBlock();
3272
3273 // Read the identifier table.
3274 IdentifierTable::CreateAndRegister(Dezr);
3275
3276 // Now jump back and read the selectors.
3277 Dezr.JumpTo(SelectorBlkLoc);
3278 SelectorTable::CreateAndRegister(Dezr);
3279
3280 // Now jump back to ASTContextBlock and read the ASTContext.
3281 Dezr.JumpTo(ASTContextBlockLoc);
3282 return Dezr.ReadOwnedPtr<ASTContext>();
3283}
3284
Ted Kremenek0f84c002007-11-13 00:25:37 +00003285ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003286
3287 // Read the language options.
3288 LangOptions LOpts;
3289 LOpts.Read(D);
3290
Ted Kremenekfee04522007-10-31 22:44:07 +00003291 SourceManager &SM = D.ReadRef<SourceManager>();
3292 TargetInfo &t = D.ReadRef<TargetInfo>();
3293 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3294 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00003295
Ted Kremenekfee04522007-10-31 22:44:07 +00003296 unsigned size_reserve = D.ReadInt();
3297
Douglas Gregor2e1cd422008-11-17 14:58:09 +00003298 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3299 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00003300
Ted Kremenek03ed4402007-11-13 22:02:55 +00003301 for (unsigned i = 0; i < size_reserve; ++i)
3302 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00003303
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003304 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3305
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003306 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00003307
3308 return A;
3309}