blob: 2e350c850248406df9608c094f0bf2a5c58dbafe [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
Chris Lattnerb09b31d2009-03-28 03:45:20 +000020#include "clang/Basic/SourceManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021#include "clang/Basic/TargetInfo.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000022#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000023#include "llvm/Bitcode/Serialize.h"
24#include "llvm/Bitcode/Deserialize.h"
Nate Begeman7903d052009-01-18 06:42:49 +000025#include "llvm/Support/MathExtras.h"
Chris Lattnerf4fbc442009-03-28 04:27:18 +000026#include "llvm/Support/MemoryBuffer.h"
Chris Lattner4b009652007-07-25 00:24:17 +000027using namespace clang;
28
29enum FloatingRank {
30 FloatRank, DoubleRank, LongDoubleRank
31};
32
Chris Lattner2fda0ed2008-10-05 17:34:18 +000033ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
34 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000035 IdentifierTable &idents, SelectorTable &sels,
Steve Naroff207b9ec2009-01-27 23:20:32 +000036 bool FreeMem, unsigned size_reserve) :
Douglas Gregor1e589cc2009-03-26 23:50:42 +000037 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
38 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Chris Lattner9c18fde2009-03-28 01:44:40 +000039 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels) {
Daniel Dunbarde300732008-08-11 04:54:23 +000040 if (size_reserve > 0) Types.reserve(size_reserve);
41 InitBuiltinTypes();
Chris Lattner911b8672009-03-13 22:38:49 +000042 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.NoBuiltin);
Daniel Dunbarde300732008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
44}
45
Chris Lattner4b009652007-07-25 00:24:17 +000046ASTContext::~ASTContext() {
47 // Deallocate all the types.
48 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000049 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000050 Types.pop_back();
51 }
Eli Friedman65489b72008-05-27 03:08:09 +000052
Nuno Lopes355a8682008-12-17 22:30:25 +000053 {
54 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
55 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
56 while (I != E) {
57 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
58 delete R;
59 }
60 }
61
62 {
63 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
64 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
65 while (I != E) {
66 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
67 delete R;
68 }
69 }
70
71 {
72 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator
73 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
74 while (I != E) {
75 RecordDecl *R = const_cast<RecordDecl*>((I++)->second);
76 R->Destroy(*this);
77 }
78 }
79
Douglas Gregor1e589cc2009-03-26 23:50:42 +000080 // Destroy nested-name-specifiers.
Douglas Gregor3c4eae52009-03-27 23:54:10 +000081 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
82 NNS = NestedNameSpecifiers.begin(),
83 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregorbccd97c2009-03-27 23:25:45 +000084 NNS != NNSEnd;
Douglas Gregor3c4eae52009-03-27 23:54:10 +000085 /* Increment in loop */)
86 (*NNS++).Destroy(*this);
Douglas Gregor1e589cc2009-03-26 23:50:42 +000087
88 if (GlobalNestedNameSpecifier)
89 GlobalNestedNameSpecifier->Destroy(*this);
90
Eli Friedman65489b72008-05-27 03:08:09 +000091 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +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 Dunbar47677342008-09-26 03:23:00 +000098 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +000099 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
100 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
101
Chris Lattner4b009652007-07-25 00:24:17 +0000102 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000103 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
104 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000105 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Chris Lattner4b009652007-07-25 00:24:17 +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 Dunbar47677342008-09-26 03:23:00 +0000113 else if (isa<BlockPointerType>(T))
114 ++NumBlockPointer;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000115 else if (isa<LValueReferenceType>(T))
116 ++NumLValueReference;
117 else if (isa<RValueReferenceType>(T))
118 ++NumRValueReference;
Sebastian Redl75555032009-01-24 21:16:55 +0000119 else if (isa<MemberPointerType>(T))
120 ++NumMemberPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000121 else if (isa<ComplexType>(T))
122 ++NumComplex;
123 else if (isa<ArrayType>(T))
124 ++NumArray;
125 else if (isa<VectorType>(T))
126 ++NumVector;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000127 else if (isa<FunctionNoProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000128 ++NumFunctionNP;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000129 else if (isa<FunctionProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000130 ++NumFunctionP;
131 else if (isa<TypedefType>(T))
132 ++NumTypeName;
133 else if (TagType *TT = dyn_cast<TagType>(T)) {
134 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000135 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000136 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-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;
Chris Lattner4b009652007-07-25 00:24:17 +0000141 }
Ted Kremenek42730c52008-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 Naroffe0430632008-05-21 15:59:22 +0000148 else if (isa<TypeOfType>(T))
149 ++NumTypeOfTypes;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000150 else if (isa<TypeOfExprType>(T))
151 ++NumTypeOfExprTypes;
Steve Naroff948fd372007-09-17 14:16:13 +0000152 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000153 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +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 Dunbar47677342008-09-26 03:23:00 +0000160 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000161 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
162 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redl75555032009-01-24 21:16:55 +0000163 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000164 fprintf(stderr, " %d complex types\n", NumComplex);
165 fprintf(stderr, " %d array types\n", NumArray);
166 fprintf(stderr, " %d vector types\n", NumVector);
167 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 Kremenek42730c52008-01-07 19:49:32 +0000175 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000176 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000177 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000178 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000179 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000180 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000181 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000182
Chris Lattner4b009652007-07-25 00:24:17 +0000183 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
184 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
185 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redlce6fff02009-03-16 23:22:08 +0000186 NumLValueReference*sizeof(LValueReferenceType)+
187 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redl75555032009-01-24 21:16:55 +0000188 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor4fa58902009-02-26 23:50:07 +0000189 NumFunctionP*sizeof(FunctionProtoType)+
190 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroffe0430632008-05-21 15:59:22 +0000191 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregor4fa58902009-02-26 23:50:07 +0000192 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)));
Chris Lattner4b009652007-07-25 00:24:17 +0000193}
194
195
196void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroff93fd2112009-01-27 22:08:43 +0000197 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000198}
199
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner8cd0e932008-03-05 18:54:05 +0000209 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +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);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000231
Chris Lattnere1dafe72009-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());
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000236
Douglas Gregord2baafd2008-10-21 16:13:35 +0000237 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-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 Gregord2baafd2008-10-21 16:13:35 +0000246
Chris Lattner4b009652007-07-25 00:24:17 +0000247 // C99 6.2.5p11.
248 FloatComplexTy = getComplexType(FloatTy);
249 DoubleComplexTy = getComplexType(DoubleTy);
250 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000251
Steve Naroff9d12c902007-10-15 14:41:52 +0000252 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000253 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000254 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000255 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000256 ClassStructType = 0;
257
Ted Kremenek42730c52008-01-07 19:49:32 +0000258 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000259
260 // void * type
261 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000262}
263
264//===----------------------------------------------------------------------===//
265// Type Sizing and Analysis
266//===----------------------------------------------------------------------===//
267
Chris Lattner2a674dc2008-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 Lattnerbd3153e2009-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 Dunbar96d1f1b2009-02-17 22:16:19 +0000284unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedman0ee57322009-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 Lattnerbd3153e2009-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 Friedman0ee57322009-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 Lattnerbd3153e2009-01-24 21:53:27 +0000299 }
Eli Friedman0ee57322009-02-22 02:56:25 +0000300
301 return Align / Target.getCharWidth();
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000302}
Chris Lattner2a674dc2008-06-30 18:32:54 +0000303
Chris Lattner4b009652007-07-25 00:24:17 +0000304/// getTypeSize - Return the size of the specified type, in bits. This method
305/// does not work on incomplete types.
306std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000307ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000308 T = getCanonicalType(T);
Mike Stump44d1f402009-02-27 18:32:39 +0000309 uint64_t Width=0;
310 unsigned Align=8;
Chris Lattner4b009652007-07-25 00:24:17 +0000311 switch (T->getTypeClass()) {
Douglas Gregor4fa58902009-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 Lattner4b009652007-07-25 00:24:17 +0000320 case Type::FunctionNoProto:
321 case Type::FunctionProto:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000322 case Type::IncompleteArray:
Chris Lattner4b009652007-07-25 00:24:17 +0000323 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000324 case Type::VariableArray:
325 assert(0 && "VLAs not implemented yet!");
326 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000327 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000328
Chris Lattner8cd0e932008-03-05 18:54:05 +0000329 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000330 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000331 Align = EltInfo.second;
332 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000333 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000334 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000335 case Type::Vector: {
336 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000337 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000338 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000339 Align = Width;
Nate Begeman7903d052009-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 Lattner4b009652007-07-25 00:24:17 +0000344 break;
345 }
346
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000347 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000348 switch (cast<BuiltinType>(T)->getKind()) {
349 default: assert(0 && "Unknown builtin type!");
350 case BuiltinType::Void:
351 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000352 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000353 Width = Target.getBoolWidth();
354 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000355 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000356 case BuiltinType::Char_S:
357 case BuiltinType::Char_U:
358 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000359 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000360 Width = Target.getCharWidth();
361 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000362 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000363 case BuiltinType::WChar:
364 Width = Target.getWCharWidth();
365 Align = Target.getWCharAlign();
366 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000367 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000368 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000369 Width = Target.getShortWidth();
370 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000371 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000372 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000373 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000374 Width = Target.getIntWidth();
375 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000376 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000377 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000378 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000379 Width = Target.getLongWidth();
380 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000381 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000382 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000383 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000384 Width = Target.getLongLongWidth();
385 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000386 break;
387 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000388 Width = Target.getFloatWidth();
389 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000390 break;
391 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000392 Width = Target.getDoubleWidth();
393 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000394 break;
395 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000396 Width = Target.getLongDoubleWidth();
397 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000398 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000399 }
400 break;
Eli Friedmanff3fcdf2009-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 Lattnere9174982009-02-15 21:20:13 +0000405 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000406 Align = Width;
407 break;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000408 case Type::ExtQual:
Chris Lattner8cd0e932008-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 Jahanianb60352a2009-02-17 18:27:45 +0000411 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000412 case Type::ObjCQualifiedId:
Eli Friedman2f6d70d2009-02-22 04:02:33 +0000413 case Type::ObjCQualifiedClass:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000414 case Type::ObjCQualifiedInterface:
Chris Lattner1d78a862008-04-07 07:01:58 +0000415 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000416 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000417 break;
Steve Naroff62f09f52008-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 Lattner461a6c52008-03-08 08:34:58 +0000424 case Type::Pointer: {
425 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000426 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000427 Align = Target.getPointerAlign(AS);
428 break;
429 }
Sebastian Redlce6fff02009-03-16 23:22:08 +0000430 case Type::LValueReference:
431 case Type::RValueReference:
Chris Lattner4b009652007-07-25 00:24:17 +0000432 // "When applied to a reference or a reference type, the result is the size
433 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000434 // FIXME: This is wrong for struct layout: a reference in a struct has
435 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000436 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl75555032009-01-24 21:16:55 +0000437 case Type::MemberPointer: {
Sebastian Redl18cffee2009-01-24 23:29:36 +0000438 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redl75555032009-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 Redl18cffee2009-01-24 23:29:36 +0000441 // other compilers too, we need to delegate this completely to TargetInfo
442 // or some ABI abstraction layer.
Sebastian Redl75555032009-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 Lattner4b009652007-07-25 00:24:17 +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 Lattner8cd0e932008-03-05 18:54:05 +0000455 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000456 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000457 Align = EltInfo.second;
458 break;
459 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000460 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000461 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000462 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
463 Width = Layout.getSize();
464 Align = Layout.getAlignment();
465 break;
466 }
Douglas Gregor4fa58902009-02-26 23:50:07 +0000467 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000468 case Type::Enum: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000469 const TagType *TT = cast<TagType>(T);
470
471 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000472 Width = 1;
473 Align = 1;
474 break;
475 }
476
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000477 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000478 return getTypeInfo(ET->getDecl()->getIntegerType());
479
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000480 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000481 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
482 Width = Layout.getSize();
483 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000484 break;
485 }
Douglas Gregordd13e842009-03-30 22:58:21 +0000486
487 case Type::TemplateSpecialization:
488 assert(false && "Dependent types have no size");
489 break;
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000490 }
Chris Lattner4b009652007-07-25 00:24:17 +0000491
492 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000493 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000494}
495
Chris Lattner83165b52009-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 Dunbarc61a8002009-02-18 19:59:32 +0000504 if (T->isSpecificBuiltinType(BuiltinType::Double))
505 return std::max(ABIAlign, 64U);
Chris Lattner83165b52009-01-27 18:08:34 +0000506
507 return ABIAlign;
508}
509
510
Devang Patelbfe323c2008-06-04 21:22:16 +0000511/// LayoutField - Field layout.
512void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000513 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000514 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000515 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000516 uint64_t FieldOffset = IsUnion ? 0 : Size;
517 uint64_t FieldSize;
518 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-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 Patelbfe323c2008-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 Dunbar7cbcbf42008-08-13 23:47:13 +0000528 FieldSize =
529 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-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 Dunbar2cb762f2008-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 Patelbfe323c2008-06-04 21:22:16 +0000540 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000541 if (FieldPacking)
542 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-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 Lattnerfd799692008-08-09 21:35:13 +0000555 if (FD->getType()->isIncompleteArrayType()) {
556 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-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 Lattnera1923f62008-08-04 07:31:14 +0000561 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-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 Dunbar2cb762f2008-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 Patelbfe323c2008-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 Jahaniana2c97df2009-03-05 20:08:48 +0000600void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
601 std::vector<FieldDecl*> &Fields) const {
Fariborz Jahanian0556b152008-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 }
Fariborz Jahanianc625fa92009-03-31 00:06:29 +0000611 // look into properties.
612 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
613 E = OI->prop_end(); I != E; ++I) {
614 ObjCPropertyDecl *PDecl = (*I);
615 if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl())
616 Fields.push_back(cast<FieldDecl>(IV));
617 }
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000618}
619
620/// addRecordToClass - produces record info. for the class for its
621/// ivars and all those inherited.
622///
623const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
624{
625 const RecordDecl *&RD = ASTRecordForInterface[D];
626 if (RD)
627 return RD;
628 std::vector<FieldDecl*> RecFields;
629 CollectObjCIvars(D, RecFields);
630 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
631 D->getLocation(),
632 D->getIdentifier());
633 /// FIXME! Can do collection of ivars and adding to the record while
634 /// doing it.
635 for (unsigned int i = 0; i != RecFields.size(); i++) {
636 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
637 RecFields[i]->getLocation(),
638 RecFields[i]->getIdentifier(),
639 RecFields[i]->getType(),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000640 RecFields[i]->getBitWidth(), false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000641 NewRD->addDecl(Field);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000642 }
643 NewRD->completeDefinition(*this);
644 RD = NewRD;
645 return RD;
646}
Devang Patel4b6bf702008-06-04 21:54:36 +0000647
Fariborz Jahanianea944842008-12-18 17:29:46 +0000648/// setFieldDecl - maps a field for the given Ivar reference node.
649//
650void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
651 const ObjCIvarDecl *Ivar,
652 const ObjCIvarRefExpr *MRef) {
Chris Lattner04f4db72009-03-31 08:31:13 +0000653 ASTFieldForIvarRef[MRef] = OI->lookupFieldDeclForIvar(*this, Ivar);
Fariborz Jahanianea944842008-12-18 17:29:46 +0000654}
655
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000656/// getASTObjcInterfaceLayout - Get or compute information about the layout of
657/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000658/// position information.
659const ASTRecordLayout &
660ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
661 // Look up this layout, if already laid out, return what we have.
662 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
663 if (Entry) return *Entry;
664
665 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
666 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000667 ASTRecordLayout *NewEntry = NULL;
668 unsigned FieldCount = D->ivar_size();
669 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
670 FieldCount++;
671 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
672 unsigned Alignment = SL.getAlignment();
673 uint64_t Size = SL.getSize();
674 NewEntry = new ASTRecordLayout(Size, Alignment);
675 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000676 // Super class is at the beginning of the layout.
677 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000678 } else {
679 NewEntry = new ASTRecordLayout();
680 NewEntry->InitializeLayout(FieldCount);
681 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000682 Entry = NewEntry;
683
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000684 unsigned StructPacking = 0;
685 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
686 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000687
688 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
689 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
690 AA->getAlignment()));
691
692 // Layout each ivar sequentially.
693 unsigned i = 0;
694 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
695 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
696 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000697 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000698 }
699
700 // Finally, round the size of the total struct up to the alignment of the
701 // struct itself.
702 NewEntry->FinalizeLayout();
703 return *NewEntry;
704}
705
Devang Patel7a78e432007-11-01 19:11:01 +0000706/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000707/// specified record (struct/union/class), which indicates its size and field
708/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000709const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000710 D = D->getDefinition(*this);
711 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000712
Chris Lattner4b009652007-07-25 00:24:17 +0000713 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000714 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000715 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000716
Devang Patel7a78e432007-11-01 19:11:01 +0000717 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
718 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
719 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000720 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000721
Douglas Gregor39677622008-12-11 20:41:00 +0000722 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000723 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000724 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000725
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000726 unsigned StructPacking = 0;
727 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
728 StructPacking = PA->getAlignment();
729
Eli Friedman5949a022008-05-30 09:31:38 +0000730 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000731 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
732 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000733
Eli Friedman5949a022008-05-30 09:31:38 +0000734 // Layout each field, for now, just sequentially, respecting alignment. In
735 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000736 unsigned FieldIdx = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000737 for (RecordDecl::field_iterator Field = D->field_begin(),
738 FieldEnd = D->field_end();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000739 Field != FieldEnd; (void)++Field, ++FieldIdx)
740 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000741
742 // Finally, round the size of the total struct up to the alignment of the
743 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000744 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000745 return *NewEntry;
746}
747
Chris Lattner4b009652007-07-25 00:24:17 +0000748//===----------------------------------------------------------------------===//
749// Type creation/memoization methods
750//===----------------------------------------------------------------------===//
751
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000752QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000753 QualType CanT = getCanonicalType(T);
754 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000755 return T;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000756
757 // If we are composing extended qualifiers together, merge together into one
758 // ExtQualType node.
759 unsigned CVRQuals = T.getCVRQualifiers();
760 QualType::GCAttrTypes GCAttr = QualType::GCNone;
761 Type *TypeNode = T.getTypePtr();
Chris Lattner35fef522008-02-20 20:55:12 +0000762
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000763 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
764 // If this type already has an address space specified, it cannot get
765 // another one.
766 assert(EQT->getAddressSpace() == 0 &&
767 "Type cannot be in multiple addr spaces!");
768 GCAttr = EQT->getObjCGCAttr();
769 TypeNode = EQT->getBaseType();
770 }
Chris Lattner35fef522008-02-20 20:55:12 +0000771
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000772 // Check if we've already instantiated this type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000773 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000774 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000775 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000776 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000777 return QualType(EXTQy, CVRQuals);
778
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000779 // If the base type isn't canonical, this won't be a canonical type either,
780 // so fill in the canonical type field.
781 QualType Canonical;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000782 if (!TypeNode->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000783 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000784
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000785 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000786 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000787 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000788 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000789 ExtQualType *New =
790 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000791 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000792 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000793 return QualType(New, CVRQuals);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000794}
795
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000796QualType ASTContext::getObjCGCQualType(QualType T,
797 QualType::GCAttrTypes GCAttr) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000798 QualType CanT = getCanonicalType(T);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000799 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000800 return T;
801
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000802 // If we are composing extended qualifiers together, merge together into one
803 // ExtQualType node.
804 unsigned CVRQuals = T.getCVRQualifiers();
805 Type *TypeNode = T.getTypePtr();
806 unsigned AddressSpace = 0;
807
808 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
809 // If this type already has an address space specified, it cannot get
810 // another one.
811 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
812 "Type cannot be in multiple addr spaces!");
813 AddressSpace = EQT->getAddressSpace();
814 TypeNode = EQT->getBaseType();
815 }
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000816
817 // Check if we've already instantiated an gc qual'd type of this type.
818 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000819 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000820 void *InsertPos = 0;
821 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000822 return QualType(EXTQy, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000823
824 // If the base type isn't canonical, this won't be a canonical type either,
825 // so fill in the canonical type field.
Eli Friedman94fcc9a2009-02-27 23:04:43 +0000826 // FIXME: Isn't this also not canonical if the base type is a array
827 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000828 QualType Canonical;
829 if (!T->isCanonical()) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000830 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000831
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000832 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000833 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
834 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
835 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000836 ExtQualType *New =
837 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000838 ExtQualTypes.InsertNode(New, InsertPos);
839 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000840 return QualType(New, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000841}
Chris Lattner4b009652007-07-25 00:24:17 +0000842
843/// getComplexType - Return the uniqued reference to the type for a complex
844/// number with the specified element type.
845QualType ASTContext::getComplexType(QualType T) {
846 // Unique pointers, to guarantee there is only one pointer of a particular
847 // structure.
848 llvm::FoldingSetNodeID ID;
849 ComplexType::Profile(ID, T);
850
851 void *InsertPos = 0;
852 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
853 return QualType(CT, 0);
854
855 // If the pointee type isn't canonical, this won't be a canonical type either,
856 // so fill in the canonical type field.
857 QualType Canonical;
858 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000859 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000860
861 // Get the new insert position for the node we care about.
862 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000863 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000864 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000865 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000866 Types.push_back(New);
867 ComplexTypes.InsertNode(New, InsertPos);
868 return QualType(New, 0);
869}
870
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000871QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
872 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
873 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
874 FixedWidthIntType *&Entry = Map[Width];
875 if (!Entry)
876 Entry = new FixedWidthIntType(Width, Signed);
877 return QualType(Entry, 0);
878}
Chris Lattner4b009652007-07-25 00:24:17 +0000879
880/// getPointerType - Return the uniqued reference to the type for a pointer to
881/// the specified type.
882QualType ASTContext::getPointerType(QualType T) {
883 // Unique pointers, to guarantee there is only one pointer of a particular
884 // structure.
885 llvm::FoldingSetNodeID ID;
886 PointerType::Profile(ID, T);
887
888 void *InsertPos = 0;
889 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
890 return QualType(PT, 0);
891
892 // If the pointee type isn't canonical, this won't be a canonical type either,
893 // so fill in the canonical type field.
894 QualType Canonical;
895 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000896 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000897
898 // Get the new insert position for the node we care about.
899 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000900 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000901 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000902 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000903 Types.push_back(New);
904 PointerTypes.InsertNode(New, InsertPos);
905 return QualType(New, 0);
906}
907
Steve Naroff7aa54752008-08-27 16:04:49 +0000908/// getBlockPointerType - Return the uniqued reference to the type for
909/// a pointer to the specified block.
910QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000911 assert(T->isFunctionType() && "block of function types only");
912 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000913 // structure.
914 llvm::FoldingSetNodeID ID;
915 BlockPointerType::Profile(ID, T);
916
917 void *InsertPos = 0;
918 if (BlockPointerType *PT =
919 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
920 return QualType(PT, 0);
921
Steve Narofffd5b19d2008-08-28 19:20:44 +0000922 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000923 // type either so fill in the canonical type field.
924 QualType Canonical;
925 if (!T->isCanonical()) {
926 Canonical = getBlockPointerType(getCanonicalType(T));
927
928 // Get the new insert position for the node we care about.
929 BlockPointerType *NewIP =
930 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000931 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000932 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000933 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000934 Types.push_back(New);
935 BlockPointerTypes.InsertNode(New, InsertPos);
936 return QualType(New, 0);
937}
938
Sebastian Redlce6fff02009-03-16 23:22:08 +0000939/// getLValueReferenceType - Return the uniqued reference to the type for an
940/// lvalue reference to the specified type.
941QualType ASTContext::getLValueReferenceType(QualType T) {
Chris Lattner4b009652007-07-25 00:24:17 +0000942 // Unique pointers, to guarantee there is only one pointer of a particular
943 // structure.
944 llvm::FoldingSetNodeID ID;
945 ReferenceType::Profile(ID, T);
946
947 void *InsertPos = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000948 if (LValueReferenceType *RT =
949 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000950 return QualType(RT, 0);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000951
Chris Lattner4b009652007-07-25 00:24:17 +0000952 // If the referencee type isn't canonical, this won't be a canonical type
953 // either, so fill in the canonical type field.
954 QualType Canonical;
955 if (!T->isCanonical()) {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000956 Canonical = getLValueReferenceType(getCanonicalType(T));
957
Chris Lattner4b009652007-07-25 00:24:17 +0000958 // Get the new insert position for the node we care about.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000959 LValueReferenceType *NewIP =
960 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000961 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000962 }
963
Sebastian Redlce6fff02009-03-16 23:22:08 +0000964 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000965 Types.push_back(New);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000966 LValueReferenceTypes.InsertNode(New, InsertPos);
967 return QualType(New, 0);
968}
969
970/// getRValueReferenceType - Return the uniqued reference to the type for an
971/// rvalue reference to the specified type.
972QualType ASTContext::getRValueReferenceType(QualType T) {
973 // Unique pointers, to guarantee there is only one pointer of a particular
974 // structure.
975 llvm::FoldingSetNodeID ID;
976 ReferenceType::Profile(ID, T);
977
978 void *InsertPos = 0;
979 if (RValueReferenceType *RT =
980 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
981 return QualType(RT, 0);
982
983 // If the referencee type isn't canonical, this won't be a canonical type
984 // either, so fill in the canonical type field.
985 QualType Canonical;
986 if (!T->isCanonical()) {
987 Canonical = getRValueReferenceType(getCanonicalType(T));
988
989 // Get the new insert position for the node we care about.
990 RValueReferenceType *NewIP =
991 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
992 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
993 }
994
995 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
996 Types.push_back(New);
997 RValueReferenceTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000998 return QualType(New, 0);
999}
1000
Sebastian Redl75555032009-01-24 21:16:55 +00001001/// getMemberPointerType - Return the uniqued reference to the type for a
1002/// member pointer to the specified type, in the specified class.
1003QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1004{
1005 // Unique pointers, to guarantee there is only one pointer of a particular
1006 // structure.
1007 llvm::FoldingSetNodeID ID;
1008 MemberPointerType::Profile(ID, T, Cls);
1009
1010 void *InsertPos = 0;
1011 if (MemberPointerType *PT =
1012 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1013 return QualType(PT, 0);
1014
1015 // If the pointee or class type isn't canonical, this won't be a canonical
1016 // type either, so fill in the canonical type field.
1017 QualType Canonical;
1018 if (!T->isCanonical()) {
1019 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1020
1021 // Get the new insert position for the node we care about.
1022 MemberPointerType *NewIP =
1023 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1024 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1025 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001026 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +00001027 Types.push_back(New);
1028 MemberPointerTypes.InsertNode(New, InsertPos);
1029 return QualType(New, 0);
1030}
1031
Steve Naroff83c13012007-08-30 01:06:46 +00001032/// getConstantArrayType - Return the unique reference to the type for an
1033/// array of the specified element type.
1034QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +00001035 const llvm::APInt &ArySize,
1036 ArrayType::ArraySizeModifier ASM,
1037 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001038 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001039 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001040
1041 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +00001042 if (ConstantArrayType *ATP =
1043 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001044 return QualType(ATP, 0);
1045
1046 // If the element type isn't canonical, this won't be a canonical type either,
1047 // so fill in the canonical type field.
1048 QualType Canonical;
1049 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001050 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +00001051 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001052 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +00001053 ConstantArrayType *NewIP =
1054 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001055 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001056 }
1057
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001058 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001059 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001060 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001061 Types.push_back(New);
1062 return QualType(New, 0);
1063}
1064
Steve Naroffe2579e32007-08-30 18:14:25 +00001065/// getVariableArrayType - Returns a non-unique reference to the type for a
1066/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +00001067QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1068 ArrayType::ArraySizeModifier ASM,
1069 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +00001070 // Since we don't unique expressions, it isn't possible to unique VLA's
1071 // that have an expression provided for their size.
1072
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001073 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001074 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001075
1076 VariableArrayTypes.push_back(New);
1077 Types.push_back(New);
1078 return QualType(New, 0);
1079}
1080
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001081/// getDependentSizedArrayType - Returns a non-unique reference to
1082/// the type for a dependently-sized array of the specified element
1083/// type. FIXME: We will need these to be uniqued, or at least
1084/// comparable, at some point.
1085QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1086 ArrayType::ArraySizeModifier ASM,
1087 unsigned EltTypeQuals) {
1088 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1089 "Size must be type- or value-dependent!");
1090
1091 // Since we don't unique expressions, it isn't possible to unique
1092 // dependently-sized array types.
1093
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001094 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001095 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1096 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001097
1098 DependentSizedArrayTypes.push_back(New);
1099 Types.push_back(New);
1100 return QualType(New, 0);
1101}
1102
Eli Friedman8ff07782008-02-15 18:16:39 +00001103QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1104 ArrayType::ArraySizeModifier ASM,
1105 unsigned EltTypeQuals) {
1106 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001107 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001108
1109 void *InsertPos = 0;
1110 if (IncompleteArrayType *ATP =
1111 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1112 return QualType(ATP, 0);
1113
1114 // If the element type isn't canonical, this won't be a canonical type
1115 // either, so fill in the canonical type field.
1116 QualType Canonical;
1117
1118 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001119 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001120 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001121
1122 // Get the new insert position for the node we care about.
1123 IncompleteArrayType *NewIP =
1124 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001125 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001126 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001127
Steve Naroff93fd2112009-01-27 22:08:43 +00001128 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001129 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001130
1131 IncompleteArrayTypes.InsertNode(New, InsertPos);
1132 Types.push_back(New);
1133 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001134}
1135
Chris Lattner4b009652007-07-25 00:24:17 +00001136/// getVectorType - Return the unique reference to a vector type of
1137/// the specified element type and size. VectorType must be a built-in type.
1138QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1139 BuiltinType *baseType;
1140
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001141 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001142 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1143
1144 // Check if we've already instantiated a vector of this type.
1145 llvm::FoldingSetNodeID ID;
1146 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1147 void *InsertPos = 0;
1148 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1149 return QualType(VTP, 0);
1150
1151 // If the element type isn't canonical, this won't be a canonical type either,
1152 // so fill in the canonical type field.
1153 QualType Canonical;
1154 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001155 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001156
1157 // Get the new insert position for the node we care about.
1158 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001159 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001160 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001161 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001162 VectorTypes.InsertNode(New, InsertPos);
1163 Types.push_back(New);
1164 return QualType(New, 0);
1165}
1166
Nate Begemanaf6ed502008-04-18 23:10:10 +00001167/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001168/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001169QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001170 BuiltinType *baseType;
1171
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001172 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001173 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001174
1175 // Check if we've already instantiated a vector of this type.
1176 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001177 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001178 void *InsertPos = 0;
1179 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1180 return QualType(VTP, 0);
1181
1182 // If the element type isn't canonical, this won't be a canonical type either,
1183 // so fill in the canonical type field.
1184 QualType Canonical;
1185 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001186 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001187
1188 // Get the new insert position for the node we care about.
1189 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001190 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001191 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001192 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001193 VectorTypes.InsertNode(New, InsertPos);
1194 Types.push_back(New);
1195 return QualType(New, 0);
1196}
1197
Douglas Gregor4fa58902009-02-26 23:50:07 +00001198/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001199///
Douglas Gregor4fa58902009-02-26 23:50:07 +00001200QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Chris Lattner4b009652007-07-25 00:24:17 +00001201 // Unique functions, to guarantee there is only one function of a particular
1202 // structure.
1203 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001204 FunctionNoProtoType::Profile(ID, ResultTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001205
1206 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001207 if (FunctionNoProtoType *FT =
1208 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001209 return QualType(FT, 0);
1210
1211 QualType Canonical;
1212 if (!ResultTy->isCanonical()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00001213 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001214
1215 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001216 FunctionNoProtoType *NewIP =
1217 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001218 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001219 }
1220
Douglas Gregor4fa58902009-02-26 23:50:07 +00001221 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001222 Types.push_back(New);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001223 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001224 return QualType(New, 0);
1225}
1226
1227/// getFunctionType - Return a normal function type with a typed argument
1228/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001229QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001230 unsigned NumArgs, bool isVariadic,
1231 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001232 // Unique functions, to guarantee there is only one function of a particular
1233 // structure.
1234 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001235 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001236 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001237
1238 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001239 if (FunctionProtoType *FTP =
1240 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001241 return QualType(FTP, 0);
1242
1243 // Determine whether the type being created is already canonical or not.
1244 bool isCanonical = ResultTy->isCanonical();
1245 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1246 if (!ArgArray[i]->isCanonical())
1247 isCanonical = false;
1248
1249 // If this type isn't canonical, get the canonical version of it.
1250 QualType Canonical;
1251 if (!isCanonical) {
1252 llvm::SmallVector<QualType, 16> CanonicalArgs;
1253 CanonicalArgs.reserve(NumArgs);
1254 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001255 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001256
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001257 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001258 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001259 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001260
1261 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001262 FunctionProtoType *NewIP =
1263 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001264 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001265 }
1266
Douglas Gregor4fa58902009-02-26 23:50:07 +00001267 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001268 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001269 FunctionProtoType *FTP =
1270 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001271 NumArgs*sizeof(QualType), 8);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001272 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001273 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001274 Types.push_back(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001275 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001276 return QualType(FTP, 0);
1277}
1278
Douglas Gregor1d661552008-04-13 21:07:44 +00001279/// getTypeDeclType - Return the unique reference to the type for the
1280/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001281QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001282 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001283 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1284
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001285 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001286 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001287 else if (isa<TemplateTypeParmDecl>(Decl)) {
1288 assert(false && "Template type parameter types are always available.");
1289 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001290 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001291
Douglas Gregor2e047592009-02-28 01:32:25 +00001292 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001293 if (PrevDecl)
1294 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001295 else
1296 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001297 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001298 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1299 if (PrevDecl)
1300 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001301 else
1302 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001303 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001304 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001305 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001306
Ted Kremenek46a837c2008-09-05 17:16:31 +00001307 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001308 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001309}
1310
Chris Lattner4b009652007-07-25 00:24:17 +00001311/// getTypedefType - Return the unique reference to the type for the
1312/// specified typename decl.
1313QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1314 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1315
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001316 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001317 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001318 Types.push_back(Decl->TypeForDecl);
1319 return QualType(Decl->TypeForDecl, 0);
1320}
1321
Ted Kremenek42730c52008-01-07 19:49:32 +00001322/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001323/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001324QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001325 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1326
Steve Naroff93fd2112009-01-27 22:08:43 +00001327 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001328 Types.push_back(Decl->TypeForDecl);
1329 return QualType(Decl->TypeForDecl, 0);
1330}
1331
Fariborz Jahanian27ecc672009-02-14 20:13:28 +00001332/// buildObjCInterfaceType - Returns a new type for the interface
1333/// declaration, regardless. It also removes any previously built
1334/// record declaration so caller can rebuild it.
1335QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) {
1336 const RecordDecl *&RD = ASTRecordForInterface[Decl];
1337 if (RD)
1338 RD = 0;
1339 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
1340 Types.push_back(Decl->TypeForDecl);
1341 return QualType(Decl->TypeForDecl, 0);
1342}
1343
Douglas Gregora4918772009-02-05 23:33:38 +00001344/// \brief Retrieve the template type parameter type for a template
1345/// parameter with the given depth, index, and (optionally) name.
1346QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1347 IdentifierInfo *Name) {
1348 llvm::FoldingSetNodeID ID;
1349 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1350 void *InsertPos = 0;
1351 TemplateTypeParmType *TypeParm
1352 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1353
1354 if (TypeParm)
1355 return QualType(TypeParm, 0);
1356
1357 if (Name)
1358 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1359 getTemplateTypeParmType(Depth, Index));
1360 else
1361 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1362
1363 Types.push_back(TypeParm);
1364 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1365
1366 return QualType(TypeParm, 0);
1367}
1368
Douglas Gregor8e458f42009-02-09 18:46:07 +00001369QualType
Douglas Gregordd13e842009-03-30 22:58:21 +00001370ASTContext::getTemplateSpecializationType(TemplateName Template,
1371 const TemplateArgument *Args,
1372 unsigned NumArgs,
1373 QualType Canon) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001374 if (!Canon.isNull())
1375 Canon = getCanonicalType(Canon);
Douglas Gregor9c7825b2009-02-26 22:19:44 +00001376
Douglas Gregor8e458f42009-02-09 18:46:07 +00001377 llvm::FoldingSetNodeID ID;
Douglas Gregordd13e842009-03-30 22:58:21 +00001378 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001379
Douglas Gregor8e458f42009-02-09 18:46:07 +00001380 void *InsertPos = 0;
Douglas Gregordd13e842009-03-30 22:58:21 +00001381 TemplateSpecializationType *Spec
1382 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001383
1384 if (Spec)
1385 return QualType(Spec, 0);
1386
Douglas Gregordd13e842009-03-30 22:58:21 +00001387 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001388 sizeof(TemplateArgument) * NumArgs),
1389 8);
Douglas Gregordd13e842009-03-30 22:58:21 +00001390 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001391 Types.push_back(Spec);
Douglas Gregordd13e842009-03-30 22:58:21 +00001392 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001393
1394 return QualType(Spec, 0);
1395}
1396
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001397QualType
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001398ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001399 QualType NamedType) {
1400 llvm::FoldingSetNodeID ID;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001401 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001402
1403 void *InsertPos = 0;
1404 QualifiedNameType *T
1405 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1406 if (T)
1407 return QualType(T, 0);
1408
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001409 T = new (*this) QualifiedNameType(NNS, NamedType,
1410 getCanonicalType(NamedType));
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001411 Types.push_back(T);
1412 QualifiedNameTypes.InsertNode(T, InsertPos);
1413 return QualType(T, 0);
1414}
1415
Douglas Gregord3022602009-03-27 23:10:48 +00001416QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1417 const IdentifierInfo *Name,
1418 QualType Canon) {
1419 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1420
1421 if (Canon.isNull()) {
1422 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1423 if (CanonNNS != NNS)
1424 Canon = getTypenameType(CanonNNS, Name);
1425 }
1426
1427 llvm::FoldingSetNodeID ID;
1428 TypenameType::Profile(ID, NNS, Name);
1429
1430 void *InsertPos = 0;
1431 TypenameType *T
1432 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1433 if (T)
1434 return QualType(T, 0);
1435
1436 T = new (*this) TypenameType(NNS, Name, Canon);
1437 Types.push_back(T);
1438 TypenameTypes.InsertNode(T, InsertPos);
1439 return QualType(T, 0);
1440}
1441
Chris Lattnere1352302008-04-07 04:56:42 +00001442/// CmpProtocolNames - Comparison predicate for sorting protocols
1443/// alphabetically.
1444static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1445 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001446 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001447}
1448
1449static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1450 unsigned &NumProtocols) {
1451 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1452
1453 // Sort protocols, keyed by name.
1454 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1455
1456 // Remove duplicates.
1457 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1458 NumProtocols = ProtocolsEnd-Protocols;
1459}
1460
1461
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001462/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1463/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001464QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1465 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001466 // Sort the protocol list alphabetically to canonicalize it.
1467 SortAndUniqueProtocols(Protocols, NumProtocols);
1468
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001469 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001470 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001471
1472 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001473 if (ObjCQualifiedInterfaceType *QT =
1474 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001475 return QualType(QT, 0);
1476
1477 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001478 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001479 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001480
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001481 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001482 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001483 return QualType(QType, 0);
1484}
1485
Chris Lattnere1352302008-04-07 04:56:42 +00001486/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1487/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001488QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001489 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001490 // Sort the protocol list alphabetically to canonicalize it.
1491 SortAndUniqueProtocols(Protocols, NumProtocols);
1492
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001493 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001494 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001495
1496 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001497 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001498 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001499 return QualType(QT, 0);
1500
1501 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001502 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001503 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001504 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001505 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001506 return QualType(QType, 0);
1507}
1508
Douglas Gregor4fa58902009-02-26 23:50:07 +00001509/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1510/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff0604dd92007-08-01 18:02:17 +00001511/// multiple declarations that refer to "typeof(x)" all contain different
1512/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1513/// on canonical type's (which are always unique).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001514QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001515 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001516 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001517 Types.push_back(toe);
1518 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001519}
1520
Steve Naroff0604dd92007-08-01 18:02:17 +00001521/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1522/// TypeOfType AST's. The only motivation to unique these nodes would be
1523/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1524/// an issue. This doesn't effect the type checker, since it operates
1525/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001526QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001527 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001528 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001529 Types.push_back(tot);
1530 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001531}
1532
Chris Lattner4b009652007-07-25 00:24:17 +00001533/// getTagDeclType - Return the unique reference to the type for the
1534/// specified TagDecl (struct/union/class/enum) decl.
1535QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001536 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001537 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001538}
1539
1540/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1541/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1542/// needs to agree with the definition in <stddef.h>.
1543QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001544 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001545}
1546
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001547/// getSignedWCharType - Return the type of "signed wchar_t".
1548/// Used when in C++, as a GCC extension.
1549QualType ASTContext::getSignedWCharType() const {
1550 // FIXME: derive from "Target" ?
1551 return WCharTy;
1552}
1553
1554/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1555/// Used when in C++, as a GCC extension.
1556QualType ASTContext::getUnsignedWCharType() const {
1557 // FIXME: derive from "Target" ?
1558 return UnsignedIntTy;
1559}
1560
Chris Lattner4b009652007-07-25 00:24:17 +00001561/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1562/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1563QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001564 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001565}
1566
Chris Lattner19eb97e2008-04-02 05:18:44 +00001567//===----------------------------------------------------------------------===//
1568// Type Operators
1569//===----------------------------------------------------------------------===//
1570
Chris Lattner3dae6f42008-04-06 22:41:35 +00001571/// getCanonicalType - Return the canonical (structural) type corresponding to
1572/// the specified potentially non-canonical type. The non-canonical version
1573/// of a type may have many "decorated" versions of types. Decorators can
1574/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1575/// to be free of any of these, allowing two canonical types to be compared
1576/// for exact equality with a simple pointer comparison.
1577QualType ASTContext::getCanonicalType(QualType T) {
1578 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001579
1580 // If the result has type qualifiers, make sure to canonicalize them as well.
1581 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1582 if (TypeQuals == 0) return CanType;
1583
1584 // If the type qualifiers are on an array type, get the canonical type of the
1585 // array with the qualifiers applied to the element type.
1586 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1587 if (!AT)
1588 return CanType.getQualifiedType(TypeQuals);
1589
1590 // Get the canonical version of the element with the extra qualifiers on it.
1591 // This can recursively sink qualifiers through multiple levels of arrays.
1592 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1593 NewEltTy = getCanonicalType(NewEltTy);
1594
1595 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1596 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1597 CAT->getIndexTypeQualifier());
1598 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1599 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1600 IAT->getIndexTypeQualifier());
1601
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001602 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1603 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1604 DSAT->getSizeModifier(),
1605 DSAT->getIndexTypeQualifier());
1606
Chris Lattnera1923f62008-08-04 07:31:14 +00001607 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1608 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1609 VAT->getSizeModifier(),
1610 VAT->getIndexTypeQualifier());
1611}
1612
Douglas Gregord3022602009-03-27 23:10:48 +00001613NestedNameSpecifier *
1614ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1615 if (!NNS)
1616 return 0;
1617
1618 switch (NNS->getKind()) {
1619 case NestedNameSpecifier::Identifier:
1620 // Canonicalize the prefix but keep the identifier the same.
1621 return NestedNameSpecifier::Create(*this,
1622 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1623 NNS->getAsIdentifier());
1624
1625 case NestedNameSpecifier::Namespace:
1626 // A namespace is canonical; build a nested-name-specifier with
1627 // this namespace and no prefix.
1628 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1629
1630 case NestedNameSpecifier::TypeSpec:
1631 case NestedNameSpecifier::TypeSpecWithTemplate: {
1632 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1633 NestedNameSpecifier *Prefix = 0;
1634
1635 // FIXME: This isn't the right check!
1636 if (T->isDependentType())
1637 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1638
1639 return NestedNameSpecifier::Create(*this, Prefix,
1640 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1641 T.getTypePtr());
1642 }
1643
1644 case NestedNameSpecifier::Global:
1645 // The global specifier is canonical and unique.
1646 return NNS;
1647 }
1648
1649 // Required to silence a GCC warning
1650 return 0;
1651}
1652
Chris Lattnera1923f62008-08-04 07:31:14 +00001653
1654const ArrayType *ASTContext::getAsArrayType(QualType T) {
1655 // Handle the non-qualified case efficiently.
1656 if (T.getCVRQualifiers() == 0) {
1657 // Handle the common positive case fast.
1658 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1659 return AT;
1660 }
1661
1662 // Handle the common negative case fast, ignoring CVR qualifiers.
1663 QualType CType = T->getCanonicalTypeInternal();
1664
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001665 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001666 // test.
1667 if (!isa<ArrayType>(CType) &&
1668 !isa<ArrayType>(CType.getUnqualifiedType()))
1669 return 0;
1670
1671 // Apply any CVR qualifiers from the array type to the element type. This
1672 // implements C99 6.7.3p8: "If the specification of an array type includes
1673 // any type qualifiers, the element type is so qualified, not the array type."
1674
1675 // If we get here, we either have type qualifiers on the type, or we have
1676 // sugar such as a typedef in the way. If we have type qualifiers on the type
1677 // we must propagate them down into the elemeng type.
1678 unsigned CVRQuals = T.getCVRQualifiers();
1679 unsigned AddrSpace = 0;
1680 Type *Ty = T.getTypePtr();
1681
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001682 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001683 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001684 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1685 AddrSpace = EXTQT->getAddressSpace();
1686 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001687 } else {
1688 T = Ty->getDesugaredType();
1689 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1690 break;
1691 CVRQuals |= T.getCVRQualifiers();
1692 Ty = T.getTypePtr();
1693 }
1694 }
1695
1696 // If we have a simple case, just return now.
1697 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1698 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1699 return ATy;
1700
1701 // Otherwise, we have an array and we have qualifiers on it. Push the
1702 // qualifiers into the array element type and return a new array type.
1703 // Get the canonical version of the element with the extra qualifiers on it.
1704 // This can recursively sink qualifiers through multiple levels of arrays.
1705 QualType NewEltTy = ATy->getElementType();
1706 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001707 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001708 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1709
1710 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1711 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1712 CAT->getSizeModifier(),
1713 CAT->getIndexTypeQualifier()));
1714 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1715 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1716 IAT->getSizeModifier(),
1717 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001718
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001719 if (const DependentSizedArrayType *DSAT
1720 = dyn_cast<DependentSizedArrayType>(ATy))
1721 return cast<ArrayType>(
1722 getDependentSizedArrayType(NewEltTy,
1723 DSAT->getSizeExpr(),
1724 DSAT->getSizeModifier(),
1725 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001726
Chris Lattnera1923f62008-08-04 07:31:14 +00001727 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1728 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1729 VAT->getSizeModifier(),
1730 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001731}
1732
1733
Chris Lattner19eb97e2008-04-02 05:18:44 +00001734/// getArrayDecayedType - Return the properly qualified result of decaying the
1735/// specified array type to a pointer. This operation is non-trivial when
1736/// handling typedefs etc. The canonical type of "T" must be an array type,
1737/// this returns a pointer to a properly qualified element of the array.
1738///
1739/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1740QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001741 // Get the element type with 'getAsArrayType' so that we don't lose any
1742 // typedefs in the element type of the array. This also handles propagation
1743 // of type qualifiers from the array type into the element type if present
1744 // (C99 6.7.3p8).
1745 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1746 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001747
Chris Lattnera1923f62008-08-04 07:31:14 +00001748 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001749
1750 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001751 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001752}
1753
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001754QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001755 QualType ElemTy = VAT->getElementType();
1756
1757 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1758 return getBaseElementType(VAT);
1759
1760 return ElemTy;
1761}
1762
Chris Lattner4b009652007-07-25 00:24:17 +00001763/// getFloatingRank - Return a relative rank for floating point types.
1764/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001765static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001766 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001767 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001768
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001769 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001770 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001771 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001772 case BuiltinType::Float: return FloatRank;
1773 case BuiltinType::Double: return DoubleRank;
1774 case BuiltinType::LongDouble: return LongDoubleRank;
1775 }
1776}
1777
Steve Narofffa0c4532007-08-27 01:41:48 +00001778/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1779/// point or a complex type (based on typeDomain/typeSize).
1780/// 'typeDomain' is a real floating point or complex type.
1781/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001782QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1783 QualType Domain) const {
1784 FloatingRank EltRank = getFloatingRank(Size);
1785 if (Domain->isComplexType()) {
1786 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001787 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001788 case FloatRank: return FloatComplexTy;
1789 case DoubleRank: return DoubleComplexTy;
1790 case LongDoubleRank: return LongDoubleComplexTy;
1791 }
Chris Lattner4b009652007-07-25 00:24:17 +00001792 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001793
1794 assert(Domain->isRealFloatingType() && "Unknown domain!");
1795 switch (EltRank) {
1796 default: assert(0 && "getFloatingRank(): illegal value for rank");
1797 case FloatRank: return FloatTy;
1798 case DoubleRank: return DoubleTy;
1799 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001800 }
Chris Lattner4b009652007-07-25 00:24:17 +00001801}
1802
Chris Lattner51285d82008-04-06 23:55:33 +00001803/// getFloatingTypeOrder - Compare the rank of the two specified floating
1804/// point types, ignoring the domain of the type (i.e. 'double' ==
1805/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1806/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001807int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1808 FloatingRank LHSR = getFloatingRank(LHS);
1809 FloatingRank RHSR = getFloatingRank(RHS);
1810
1811 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001812 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001813 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001814 return 1;
1815 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001816}
1817
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001818/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1819/// routine will assert if passed a built-in type that isn't an integer or enum,
1820/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001821unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001822 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001823 if (EnumType* ET = dyn_cast<EnumType>(T))
1824 T = ET->getDecl()->getIntegerType().getTypePtr();
1825
1826 // There are two things which impact the integer rank: the width, and
1827 // the ordering of builtins. The builtin ordering is encoded in the
1828 // bottom three bits; the width is encoded in the bits above that.
1829 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1830 return FWIT->getWidth() << 3;
1831 }
1832
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001833 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001834 default: assert(0 && "getIntegerRank(): not a built-in integer");
1835 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001836 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001837 case BuiltinType::Char_S:
1838 case BuiltinType::Char_U:
1839 case BuiltinType::SChar:
1840 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001841 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001842 case BuiltinType::Short:
1843 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001844 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001845 case BuiltinType::Int:
1846 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001847 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001848 case BuiltinType::Long:
1849 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001850 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001851 case BuiltinType::LongLong:
1852 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001853 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001854 }
1855}
1856
Chris Lattner51285d82008-04-06 23:55:33 +00001857/// getIntegerTypeOrder - Returns the highest ranked integer type:
1858/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1859/// LHS < RHS, return -1.
1860int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001861 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1862 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001863 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001864
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001865 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1866 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001867
Chris Lattner51285d82008-04-06 23:55:33 +00001868 unsigned LHSRank = getIntegerRank(LHSC);
1869 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001870
Chris Lattner51285d82008-04-06 23:55:33 +00001871 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1872 if (LHSRank == RHSRank) return 0;
1873 return LHSRank > RHSRank ? 1 : -1;
1874 }
Chris Lattner4b009652007-07-25 00:24:17 +00001875
Chris Lattner51285d82008-04-06 23:55:33 +00001876 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1877 if (LHSUnsigned) {
1878 // If the unsigned [LHS] type is larger, return it.
1879 if (LHSRank >= RHSRank)
1880 return 1;
1881
1882 // If the signed type can represent all values of the unsigned type, it
1883 // wins. Because we are dealing with 2's complement and types that are
1884 // powers of two larger than each other, this is always safe.
1885 return -1;
1886 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001887
Chris Lattner51285d82008-04-06 23:55:33 +00001888 // If the unsigned [RHS] type is larger, return it.
1889 if (RHSRank >= LHSRank)
1890 return -1;
1891
1892 // If the signed type can represent all values of the unsigned type, it
1893 // wins. Because we are dealing with 2's complement and types that are
1894 // powers of two larger than each other, this is always safe.
1895 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001896}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001897
1898// getCFConstantStringType - Return the type used for constant CFStrings.
1899QualType ASTContext::getCFConstantStringType() {
1900 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001901 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001902 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001903 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001904 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001905
1906 // const int *isa;
1907 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001908 // int flags;
1909 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001910 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001911 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001912 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001913 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001914
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001915 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001916 for (unsigned i = 0; i < 4; ++i) {
1917 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1918 SourceLocation(), 0,
1919 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001920 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001921 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001922 }
1923
1924 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001925 }
1926
1927 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001928}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001929
Anders Carlssonf58cac72008-08-30 19:34:46 +00001930QualType ASTContext::getObjCFastEnumerationStateType()
1931{
1932 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001933 ObjCFastEnumerationStateTypeDecl =
1934 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1935 &Idents.get("__objcFastEnumerationState"));
1936
Anders Carlssonf58cac72008-08-30 19:34:46 +00001937 QualType FieldTypes[] = {
1938 UnsignedLongTy,
1939 getPointerType(ObjCIdType),
1940 getPointerType(UnsignedLongTy),
1941 getConstantArrayType(UnsignedLongTy,
1942 llvm::APInt(32, 5), ArrayType::Normal, 0)
1943 };
1944
Douglas Gregor8acb7272008-12-11 16:49:14 +00001945 for (size_t i = 0; i < 4; ++i) {
1946 FieldDecl *Field = FieldDecl::Create(*this,
1947 ObjCFastEnumerationStateTypeDecl,
1948 SourceLocation(), 0,
1949 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001950 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001951 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001952 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00001953
Douglas Gregor8acb7272008-12-11 16:49:14 +00001954 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001955 }
1956
1957 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1958}
1959
Anders Carlssone3f02572007-10-29 06:33:42 +00001960// This returns true if a type has been typedefed to BOOL:
1961// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001962static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001963 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00001964 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1965 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001966
1967 return false;
1968}
1969
Ted Kremenek42730c52008-01-07 19:49:32 +00001970/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001971/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001972int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001973 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001974
1975 // Make all integer and enum types at least as large as an int
1976 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001977 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001978 // Treat arrays as pointers, since that's how they're passed in.
1979 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001980 sz = getTypeSize(VoidPtrTy);
1981 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001982}
1983
Ted Kremenek42730c52008-01-07 19:49:32 +00001984/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001985/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001986void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00001987 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001988 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001989 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001990 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001991 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001992 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001993 // Compute size of all parameters.
1994 // Start with computing size of a pointer in number of bytes.
1995 // FIXME: There might(should) be a better way of doing this computation!
1996 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001997 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001998 // The first two arguments (self and _cmd) are pointers; account for
1999 // their size.
2000 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002001 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2002 E = Decl->param_end(); PI != E; ++PI) {
2003 QualType PType = (*PI)->getType();
2004 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00002005 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002006 ParmOffset += sz;
2007 }
2008 S += llvm::utostr(ParmOffset);
2009 S += "@0:";
2010 S += llvm::utostr(PtrSize);
2011
2012 // Argument types.
2013 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002014 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2015 E = Decl->param_end(); PI != E; ++PI) {
2016 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002017 QualType PType = PVDecl->getOriginalType();
2018 if (const ArrayType *AT =
2019 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
2020 // Use array's original type only if it has known number of
2021 // elements.
2022 if (!dyn_cast<ConstantArrayType>(AT))
2023 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002024 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002025 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002026 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002027 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002028 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00002029 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002030 }
2031}
2032
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002033/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002034/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002035/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2036/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002037/// Property attributes are stored as a comma-delimited C string. The simple
2038/// attributes readonly and bycopy are encoded as single characters. The
2039/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2040/// encoded as single characters, followed by an identifier. Property types
2041/// are also encoded as a parametrized attribute. The characters used to encode
2042/// these attributes are defined by the following enumeration:
2043/// @code
2044/// enum PropertyAttributes {
2045/// kPropertyReadOnly = 'R', // property is read-only.
2046/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2047/// kPropertyByref = '&', // property is a reference to the value last assigned
2048/// kPropertyDynamic = 'D', // property is dynamic
2049/// kPropertyGetter = 'G', // followed by getter selector name
2050/// kPropertySetter = 'S', // followed by setter selector name
2051/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2052/// kPropertyType = 't' // followed by old-style type encoding.
2053/// kPropertyWeak = 'W' // 'weak' property
2054/// kPropertyStrong = 'P' // property GC'able
2055/// kPropertyNonAtomic = 'N' // property non-atomic
2056/// };
2057/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002058void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2059 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00002060 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002061 // Collect information from the property implementation decl(s).
2062 bool Dynamic = false;
2063 ObjCPropertyImplDecl *SynthesizePID = 0;
2064
2065 // FIXME: Duplicated code due to poor abstraction.
2066 if (Container) {
2067 if (const ObjCCategoryImplDecl *CID =
2068 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2069 for (ObjCCategoryImplDecl::propimpl_iterator
2070 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2071 ObjCPropertyImplDecl *PID = *i;
2072 if (PID->getPropertyDecl() == PD) {
2073 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2074 Dynamic = true;
2075 } else {
2076 SynthesizePID = PID;
2077 }
2078 }
2079 }
2080 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002081 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002082 for (ObjCCategoryImplDecl::propimpl_iterator
2083 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2084 ObjCPropertyImplDecl *PID = *i;
2085 if (PID->getPropertyDecl() == PD) {
2086 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2087 Dynamic = true;
2088 } else {
2089 SynthesizePID = PID;
2090 }
2091 }
2092 }
2093 }
2094 }
2095
2096 // FIXME: This is not very efficient.
2097 S = "T";
2098
2099 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002100 // GCC has some special rules regarding encoding of properties which
2101 // closely resembles encoding of ivars.
2102 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
2103 true /* outermost type */,
2104 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002105
2106 if (PD->isReadOnly()) {
2107 S += ",R";
2108 } else {
2109 switch (PD->getSetterKind()) {
2110 case ObjCPropertyDecl::Assign: break;
2111 case ObjCPropertyDecl::Copy: S += ",C"; break;
2112 case ObjCPropertyDecl::Retain: S += ",&"; break;
2113 }
2114 }
2115
2116 // It really isn't clear at all what this means, since properties
2117 // are "dynamic by default".
2118 if (Dynamic)
2119 S += ",D";
2120
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002121 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2122 S += ",N";
2123
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002124 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2125 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002126 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002127 }
2128
2129 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2130 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002131 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002132 }
2133
2134 if (SynthesizePID) {
2135 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2136 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002137 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002138 }
2139
2140 // FIXME: OBJCGC: weak & strong
2141}
2142
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002143/// getLegacyIntegralTypeEncoding -
2144/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002145/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002146/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2147///
2148void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2149 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2150 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002151 if (BT->getKind() == BuiltinType::ULong &&
2152 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002153 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002154 else
2155 if (BT->getKind() == BuiltinType::Long &&
2156 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002157 PointeeTy = IntTy;
2158 }
2159 }
2160}
2161
Fariborz Jahanian248db262008-01-22 22:44:46 +00002162void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002163 FieldDecl *Field) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002164 // We follow the behavior of gcc, expanding structures which are
2165 // directly pointed to, and expanding embedded structures. Note that
2166 // these rules are sufficient to prevent recursive encoding of the
2167 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002168 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2169 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002170}
2171
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002172static void EncodeBitField(const ASTContext *Context, std::string& S,
2173 FieldDecl *FD) {
2174 const Expr *E = FD->getBitWidth();
2175 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2176 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2177 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2178 S += 'b';
2179 S += llvm::utostr(N);
2180}
2181
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002182void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2183 bool ExpandPointedToStructures,
2184 bool ExpandStructures,
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002185 FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002186 bool OutermostType,
2187 bool EncodingProperty) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00002188 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002189 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002190 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002191 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002192 else {
2193 char encoding;
2194 switch (BT->getKind()) {
2195 default: assert(0 && "Unhandled builtin type kind");
2196 case BuiltinType::Void: encoding = 'v'; break;
2197 case BuiltinType::Bool: encoding = 'B'; break;
2198 case BuiltinType::Char_U:
2199 case BuiltinType::UChar: encoding = 'C'; break;
2200 case BuiltinType::UShort: encoding = 'S'; break;
2201 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002202 case BuiltinType::ULong:
2203 encoding =
2204 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2205 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002206 case BuiltinType::ULongLong: encoding = 'Q'; break;
2207 case BuiltinType::Char_S:
2208 case BuiltinType::SChar: encoding = 'c'; break;
2209 case BuiltinType::Short: encoding = 's'; break;
2210 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002211 case BuiltinType::Long:
2212 encoding =
2213 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2214 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002215 case BuiltinType::LongLong: encoding = 'q'; break;
2216 case BuiltinType::Float: encoding = 'f'; break;
2217 case BuiltinType::Double: encoding = 'd'; break;
2218 case BuiltinType::LongDouble: encoding = 'd'; break;
2219 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002220
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002221 S += encoding;
2222 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002223 }
Ted Kremenek42730c52008-01-07 19:49:32 +00002224 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002225 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2226 ExpandPointedToStructures,
2227 ExpandStructures, FD);
2228 if (FD || EncodingProperty) {
2229 // Note that we do extended encoding of protocol qualifer list
2230 // Only when doing ivar or property encoding.
2231 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2232 S += '"';
2233 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2234 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2235 S += '<';
2236 S += Proto->getNameAsString();
2237 S += '>';
2238 }
2239 S += '"';
2240 }
2241 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002242 }
2243 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002244 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002245 bool isReadOnly = false;
2246 // For historical/compatibility reasons, the read-only qualifier of the
2247 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2248 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2249 // Also, do not emit the 'r' for anything but the outermost type!
2250 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2251 if (OutermostType && T.isConstQualified()) {
2252 isReadOnly = true;
2253 S += 'r';
2254 }
2255 }
2256 else if (OutermostType) {
2257 QualType P = PointeeTy;
2258 while (P->getAsPointerType())
2259 P = P->getAsPointerType()->getPointeeType();
2260 if (P.isConstQualified()) {
2261 isReadOnly = true;
2262 S += 'r';
2263 }
2264 }
2265 if (isReadOnly) {
2266 // Another legacy compatibility encoding. Some ObjC qualifier and type
2267 // combinations need to be rearranged.
2268 // Rewrite "in const" from "nr" to "rn"
2269 const char * s = S.c_str();
2270 int len = S.length();
2271 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2272 std::string replace = "rn";
2273 S.replace(S.end()-2, S.end(), replace);
2274 }
2275 }
Steve Naroff17c03822009-02-12 17:52:19 +00002276 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002277 S += '@';
2278 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002279 }
2280 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002281 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002282 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002283 // Another historical/compatibility reason.
2284 // We encode the underlying type which comes out as
2285 // {...};
2286 S += '^';
2287 getObjCEncodingForTypeImpl(PointeeTy, S,
2288 false, ExpandPointedToStructures,
2289 NULL);
2290 return;
2291 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002292 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002293 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002294 const ObjCInterfaceType *OIT =
2295 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002296 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002297 S += '"';
2298 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002299 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2300 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2301 S += '<';
2302 S += Proto->getNameAsString();
2303 S += '>';
2304 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002305 S += '"';
2306 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002307 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002308 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002309 S += '#';
2310 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002311 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002312 S += ':';
2313 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002314 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002315
2316 if (PointeeTy->isCharType()) {
2317 // char pointer types should be encoded as '*' unless it is a
2318 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002319 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002320 S += '*';
2321 return;
2322 }
2323 }
2324
2325 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002326 getLegacyIntegralTypeEncoding(PointeeTy);
2327
2328 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002329 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002330 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002331 } else if (const ArrayType *AT =
2332 // Ignore type qualifiers etc.
2333 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002334 if (isa<IncompleteArrayType>(AT)) {
2335 // Incomplete arrays are encoded as a pointer to the array element.
2336 S += '^';
2337
2338 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2339 false, ExpandStructures, FD);
2340 } else {
2341 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002342
Anders Carlsson858c64d2009-02-22 01:38:57 +00002343 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2344 S += llvm::utostr(CAT->getSize().getZExtValue());
2345 else {
2346 //Variable length arrays are encoded as a regular array with 0 elements.
2347 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2348 S += '0';
2349 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002350
Anders Carlsson858c64d2009-02-22 01:38:57 +00002351 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2352 false, ExpandStructures, FD);
2353 S += ']';
2354 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002355 } else if (T->getAsFunctionType()) {
2356 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002357 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002358 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002359 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002360 // Anonymous structures print as '?'
2361 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2362 S += II->getName();
2363 } else {
2364 S += '?';
2365 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002366 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002367 S += '=';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002368 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2369 FieldEnd = RDecl->field_end();
2370 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002371 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002372 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002373 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002374 S += '"';
2375 }
2376
2377 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002378 if (Field->isBitField()) {
2379 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2380 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002381 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002382 QualType qt = Field->getType();
2383 getLegacyIntegralTypeEncoding(qt);
2384 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002385 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002386 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002387 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002388 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002389 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002390 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002391 if (FD && FD->isBitField())
2392 EncodeBitField(this, S, FD);
2393 else
2394 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002395 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002396 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002397 } else if (T->isObjCInterfaceType()) {
2398 // @encode(class_name)
2399 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2400 S += '{';
2401 const IdentifierInfo *II = OI->getIdentifier();
2402 S += II->getName();
2403 S += '=';
2404 std::vector<FieldDecl*> RecFields;
2405 CollectObjCIvars(OI, RecFields);
2406 for (unsigned int i = 0; i != RecFields.size(); i++) {
2407 if (RecFields[i]->isBitField())
2408 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2409 RecFields[i]);
2410 else
2411 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2412 FD);
2413 }
2414 S += '}';
2415 }
2416 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002417 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002418}
2419
Ted Kremenek42730c52008-01-07 19:49:32 +00002420void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002421 std::string& S) const {
2422 if (QT & Decl::OBJC_TQ_In)
2423 S += 'n';
2424 if (QT & Decl::OBJC_TQ_Inout)
2425 S += 'N';
2426 if (QT & Decl::OBJC_TQ_Out)
2427 S += 'o';
2428 if (QT & Decl::OBJC_TQ_Bycopy)
2429 S += 'O';
2430 if (QT & Decl::OBJC_TQ_Byref)
2431 S += 'R';
2432 if (QT & Decl::OBJC_TQ_Oneway)
2433 S += 'V';
2434}
2435
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002436void ASTContext::setBuiltinVaListType(QualType T)
2437{
2438 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2439
2440 BuiltinVaListType = T;
2441}
2442
Ted Kremenek42730c52008-01-07 19:49:32 +00002443void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002444{
Ted Kremenek42730c52008-01-07 19:49:32 +00002445 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002446
2447 // typedef struct objc_object *id;
2448 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002449 // User error - caller will issue diagnostics.
2450 if (!ptr)
2451 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002452 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002453 // User error - caller will issue diagnostics.
2454 if (!rec)
2455 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002456 IdStructType = rec;
2457}
2458
Ted Kremenek42730c52008-01-07 19:49:32 +00002459void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002460{
Ted Kremenek42730c52008-01-07 19:49:32 +00002461 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002462
2463 // typedef struct objc_selector *SEL;
2464 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002465 if (!ptr)
2466 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002467 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002468 if (!rec)
2469 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002470 SelStructType = rec;
2471}
2472
Ted Kremenek42730c52008-01-07 19:49:32 +00002473void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002474{
Ted Kremenek42730c52008-01-07 19:49:32 +00002475 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002476}
2477
Ted Kremenek42730c52008-01-07 19:49:32 +00002478void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002479{
Ted Kremenek42730c52008-01-07 19:49:32 +00002480 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002481
2482 // typedef struct objc_class *Class;
2483 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2484 assert(ptr && "'Class' incorrectly typed");
2485 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2486 assert(rec && "'Class' incorrectly typed");
2487 ClassStructType = rec;
2488}
2489
Ted Kremenek42730c52008-01-07 19:49:32 +00002490void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2491 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002492 "'NSConstantString' type already set!");
2493
Ted Kremenek42730c52008-01-07 19:49:32 +00002494 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002495}
2496
Douglas Gregordd13e842009-03-30 22:58:21 +00002497/// \brief Retrieve the template name that represents a qualified
2498/// template name such as \c std::vector.
2499TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2500 bool TemplateKeyword,
2501 TemplateDecl *Template) {
2502 llvm::FoldingSetNodeID ID;
2503 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2504
2505 void *InsertPos = 0;
2506 QualifiedTemplateName *QTN =
2507 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2508 if (!QTN) {
2509 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2510 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2511 }
2512
2513 return TemplateName(QTN);
2514}
2515
2516/// \brief Retrieve the template name that represents a dependent
2517/// template name such as \c MetaFun::template apply.
2518TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2519 const IdentifierInfo *Name) {
2520 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2521
2522 llvm::FoldingSetNodeID ID;
2523 DependentTemplateName::Profile(ID, NNS, Name);
2524
2525 void *InsertPos = 0;
2526 DependentTemplateName *QTN =
2527 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2528
2529 if (QTN)
2530 return TemplateName(QTN);
2531
2532 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2533 if (CanonNNS == NNS) {
2534 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2535 } else {
2536 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2537 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2538 }
2539
2540 DependentTemplateNames.InsertNode(QTN, InsertPos);
2541 return TemplateName(QTN);
2542}
2543
Douglas Gregorc6507e42008-11-03 14:12:49 +00002544/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002545/// TargetInfo, produce the corresponding type. The unsigned @p Type
2546/// is actually a value of type @c TargetInfo::IntType.
2547QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002548 switch (Type) {
2549 case TargetInfo::NoInt: return QualType();
2550 case TargetInfo::SignedShort: return ShortTy;
2551 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2552 case TargetInfo::SignedInt: return IntTy;
2553 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2554 case TargetInfo::SignedLong: return LongTy;
2555 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2556 case TargetInfo::SignedLongLong: return LongLongTy;
2557 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2558 }
2559
2560 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002561 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002562}
Ted Kremenek118930e2008-07-24 23:58:27 +00002563
2564//===----------------------------------------------------------------------===//
2565// Type Predicates.
2566//===----------------------------------------------------------------------===//
2567
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002568/// isObjCNSObjectType - Return true if this is an NSObject object using
2569/// NSObject attribute on a c-style pointer type.
2570/// FIXME - Make it work directly on types.
2571///
2572bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2573 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2574 if (TypedefDecl *TD = TDT->getDecl())
2575 if (TD->getAttr<ObjCNSObjectAttr>())
2576 return true;
2577 }
2578 return false;
2579}
2580
Ted Kremenek118930e2008-07-24 23:58:27 +00002581/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2582/// to an object type. This includes "id" and "Class" (two 'special' pointers
2583/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2584/// ID type).
2585bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroff6805fc42009-02-23 18:36:16 +00002586 if (Ty->isObjCQualifiedIdType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002587 return true;
2588
Steve Naroffd9e00802008-10-21 18:24:04 +00002589 // Blocks are objects.
2590 if (Ty->isBlockPointerType())
2591 return true;
2592
2593 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00002594 if (!Ty->isPointerType())
2595 return false;
2596
2597 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2598 // pointer types. This looks for the typedef specifically, not for the
2599 // underlying type.
Eli Friedman9c2b33f2009-03-22 23:00:19 +00002600 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2601 Ty.getUnqualifiedType() == getObjCClassType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002602 return true;
2603
2604 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002605 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2606 return true;
2607
2608 // If is has NSObject attribute, OK as well.
2609 return isObjCNSObjectType(Ty);
Ted Kremenek118930e2008-07-24 23:58:27 +00002610}
2611
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002612/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2613/// garbage collection attribute.
2614///
2615QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002616 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002617 if (getLangOptions().ObjC1 &&
2618 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002619 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002620 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002621 // (or pointers to them) be treated as though they were declared
2622 // as __strong.
2623 if (GCAttrs == QualType::GCNone) {
2624 if (isObjCObjectPointerType(Ty))
2625 GCAttrs = QualType::Strong;
2626 else if (Ty->isPointerType())
2627 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2628 }
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002629 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002630 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002631}
2632
Chris Lattner6ff358b2008-04-07 06:51:04 +00002633//===----------------------------------------------------------------------===//
2634// Type Compatibility Testing
2635//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002636
Steve Naroff3454b6c2008-09-04 15:10:53 +00002637/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002638/// block types. Types must be strictly compatible here. For example,
2639/// C unfortunately doesn't produce an error for the following:
2640///
2641/// int (*emptyArgFunc)();
2642/// int (*intArgList)(int) = emptyArgFunc;
2643///
2644/// For blocks, we will produce an error for the following (similar to C++):
2645///
2646/// int (^emptyArgBlock)();
2647/// int (^intArgBlock)(int) = emptyArgBlock;
2648///
2649/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2650///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002651bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002652 const FunctionType *lbase = lhs->getAsFunctionType();
2653 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002654 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2655 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002656 if (lproto && rproto)
2657 return !mergeTypes(lhs, rhs).isNull();
2658 return false;
Steve Naroff3454b6c2008-09-04 15:10:53 +00002659}
2660
Chris Lattner6ff358b2008-04-07 06:51:04 +00002661/// areCompatVectorTypes - Return true if the two specified vector types are
2662/// compatible.
2663static bool areCompatVectorTypes(const VectorType *LHS,
2664 const VectorType *RHS) {
2665 assert(LHS->isCanonical() && RHS->isCanonical());
2666 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002667 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002668}
2669
Eli Friedman0d9549b2008-08-22 00:56:42 +00002670/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002671/// compatible for assignment from RHS to LHS. This handles validation of any
2672/// protocol qualifiers on the LHS or RHS.
2673///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002674bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2675 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002676 // Verify that the base decls are compatible: the RHS must be a subclass of
2677 // the LHS.
2678 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2679 return false;
2680
2681 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2682 // protocol qualified at all, then we are good.
2683 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2684 return true;
2685
2686 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2687 // isn't a superset.
2688 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2689 return true; // FIXME: should return false!
2690
2691 // Finally, we must have two protocol-qualified interfaces.
2692 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2693 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ff358b2008-04-07 06:51:04 +00002694
Steve Naroff98e71b82009-03-01 16:12:44 +00002695 // All LHS protocols must have a presence on the RHS.
2696 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ff358b2008-04-07 06:51:04 +00002697
Steve Naroff98e71b82009-03-01 16:12:44 +00002698 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2699 LHSPE = LHSP->qual_end();
2700 LHSPI != LHSPE; LHSPI++) {
2701 bool RHSImplementsProtocol = false;
2702
2703 // If the RHS doesn't implement the protocol on the left, the types
2704 // are incompatible.
2705 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2706 RHSPE = RHSP->qual_end();
2707 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2708 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2709 RHSImplementsProtocol = true;
2710 }
2711 // FIXME: For better diagnostics, consider passing back the protocol name.
2712 if (!RHSImplementsProtocol)
2713 return false;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002714 }
Steve Naroff98e71b82009-03-01 16:12:44 +00002715 // The RHS implements all protocols listed on the LHS.
2716 return true;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002717}
2718
Steve Naroff17c03822009-02-12 17:52:19 +00002719bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2720 // get the "pointed to" types
2721 const PointerType *LHSPT = LHS->getAsPointerType();
2722 const PointerType *RHSPT = RHS->getAsPointerType();
2723
2724 if (!LHSPT || !RHSPT)
2725 return false;
2726
2727 QualType lhptee = LHSPT->getPointeeType();
2728 QualType rhptee = RHSPT->getPointeeType();
2729 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2730 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2731 // ID acts sort of like void* for ObjC interfaces
2732 if (LHSIface && isObjCIdStructType(rhptee))
2733 return true;
2734 if (RHSIface && isObjCIdStructType(lhptee))
2735 return true;
2736 if (!LHSIface || !RHSIface)
2737 return false;
2738 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2739 canAssignObjCInterfaces(RHSIface, LHSIface);
2740}
2741
Steve Naroff85f0dc52007-10-15 20:41:53 +00002742/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2743/// both shall have the identically qualified version of a compatible type.
2744/// C99 6.2.7p1: Two types have compatible types if their types are the
2745/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002746bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2747 return !mergeTypes(LHS, RHS).isNull();
2748}
2749
2750QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2751 const FunctionType *lbase = lhs->getAsFunctionType();
2752 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002753 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2754 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002755 bool allLTypes = true;
2756 bool allRTypes = true;
2757
2758 // Check return type
2759 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2760 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002761 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2762 allLTypes = false;
2763 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2764 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002765
2766 if (lproto && rproto) { // two C99 style function prototypes
2767 unsigned lproto_nargs = lproto->getNumArgs();
2768 unsigned rproto_nargs = rproto->getNumArgs();
2769
2770 // Compatible functions must have the same number of arguments
2771 if (lproto_nargs != rproto_nargs)
2772 return QualType();
2773
2774 // Variadic and non-variadic functions aren't compatible
2775 if (lproto->isVariadic() != rproto->isVariadic())
2776 return QualType();
2777
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002778 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2779 return QualType();
2780
Eli Friedman0d9549b2008-08-22 00:56:42 +00002781 // Check argument compatibility
2782 llvm::SmallVector<QualType, 10> types;
2783 for (unsigned i = 0; i < lproto_nargs; i++) {
2784 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2785 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2786 QualType argtype = mergeTypes(largtype, rargtype);
2787 if (argtype.isNull()) return QualType();
2788 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002789 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2790 allLTypes = false;
2791 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2792 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002793 }
2794 if (allLTypes) return lhs;
2795 if (allRTypes) return rhs;
2796 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002797 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002798 }
2799
2800 if (lproto) allRTypes = false;
2801 if (rproto) allLTypes = false;
2802
Douglas Gregor4fa58902009-02-26 23:50:07 +00002803 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002804 if (proto) {
2805 if (proto->isVariadic()) return QualType();
2806 // Check that the types are compatible with the types that
2807 // would result from default argument promotions (C99 6.7.5.3p15).
2808 // The only types actually affected are promotable integer
2809 // types and floats, which would be passed as a different
2810 // type depending on whether the prototype is visible.
2811 unsigned proto_nargs = proto->getNumArgs();
2812 for (unsigned i = 0; i < proto_nargs; ++i) {
2813 QualType argTy = proto->getArgType(i);
2814 if (argTy->isPromotableIntegerType() ||
2815 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2816 return QualType();
2817 }
2818
2819 if (allLTypes) return lhs;
2820 if (allRTypes) return rhs;
2821 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002822 proto->getNumArgs(), lproto->isVariadic(),
2823 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002824 }
2825
2826 if (allLTypes) return lhs;
2827 if (allRTypes) return rhs;
Douglas Gregor4fa58902009-02-26 23:50:07 +00002828 return getFunctionNoProtoType(retType);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002829}
2830
2831QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002832 // C++ [expr]: If an expression initially has the type "reference to T", the
2833 // type is adjusted to "T" prior to any further analysis, the expression
2834 // designates the object or function denoted by the reference, and the
Sebastian Redlce6fff02009-03-16 23:22:08 +00002835 // expression is an lvalue unless the reference is an rvalue reference and
2836 // the expression is a function call (possibly inside parentheses).
Eli Friedman0d9549b2008-08-22 00:56:42 +00002837 // FIXME: C++ shouldn't be going through here! The rules are different
2838 // enough that they should be handled separately.
Sebastian Redlce6fff02009-03-16 23:22:08 +00002839 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2840 // shouldn't be going through here!
Eli Friedman0d9549b2008-08-22 00:56:42 +00002841 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002842 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002843 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002844 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002845
Eli Friedman0d9549b2008-08-22 00:56:42 +00002846 QualType LHSCan = getCanonicalType(LHS),
2847 RHSCan = getCanonicalType(RHS);
2848
2849 // If two types are identical, they are compatible.
2850 if (LHSCan == RHSCan)
2851 return LHS;
2852
2853 // If the qualifiers are different, the types aren't compatible
Eli Friedman94fcc9a2009-02-27 23:04:43 +00002854 // Note that we handle extended qualifiers later, in the
2855 // case for ExtQualType.
2856 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman0d9549b2008-08-22 00:56:42 +00002857 return QualType();
2858
2859 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2860 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2861
Chris Lattnerc38d4522008-01-14 05:45:46 +00002862 // We want to consider the two function types to be the same for these
2863 // comparisons, just force one to the other.
2864 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2865 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002866
2867 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002868 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2869 LHSClass = Type::ConstantArray;
2870 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2871 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002872
Nate Begemanaf6ed502008-04-18 23:10:10 +00002873 // Canonicalize ExtVector -> Vector.
2874 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2875 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002876
Chris Lattner7cdcb252008-04-07 06:38:24 +00002877 // Consider qualified interfaces and interfaces the same.
2878 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2879 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002880
Chris Lattnerb5709e22008-04-07 05:43:21 +00002881 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002882 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00002883 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2884 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2885
2886 // ID acts sort of like void* for ObjC interfaces
2887 if (LHSIface && isObjCIdStructType(RHS))
2888 return LHS;
2889 if (RHSIface && isObjCIdStructType(LHS))
2890 return RHS;
2891
Steve Naroff28ceff72008-12-10 22:14:21 +00002892 // ID is compatible with all qualified id types.
2893 if (LHS->isObjCQualifiedIdType()) {
2894 if (const PointerType *PT = RHS->getAsPointerType()) {
2895 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002896 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002897 return LHS;
2898 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2899 // Unfortunately, this API is part of Sema (which we don't have access
2900 // to. Need to refactor. The following check is insufficient, since we
2901 // need to make sure the class implements the protocol.
2902 if (pType->isObjCInterfaceType())
2903 return LHS;
2904 }
2905 }
2906 if (RHS->isObjCQualifiedIdType()) {
2907 if (const PointerType *PT = LHS->getAsPointerType()) {
2908 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002909 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002910 return RHS;
2911 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2912 // Unfortunately, this API is part of Sema (which we don't have access
2913 // to. Need to refactor. The following check is insufficient, since we
2914 // need to make sure the class implements the protocol.
2915 if (pType->isObjCInterfaceType())
2916 return RHS;
2917 }
2918 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002919 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2920 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002921 if (const EnumType* ETy = LHS->getAsEnumType()) {
2922 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2923 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002924 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002925 if (const EnumType* ETy = RHS->getAsEnumType()) {
2926 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2927 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002928 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002929
Eli Friedman0d9549b2008-08-22 00:56:42 +00002930 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002931 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002932
Steve Naroffc88babe2008-01-09 22:43:08 +00002933 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002934 switch (LHSClass) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00002935#define TYPE(Class, Base)
2936#define ABSTRACT_TYPE(Class, Base)
2937#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2938#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2939#include "clang/AST/TypeNodes.def"
2940 assert(false && "Non-canonical and dependent types shouldn't get here");
2941 return QualType();
2942
Sebastian Redlce6fff02009-03-16 23:22:08 +00002943 case Type::LValueReference:
2944 case Type::RValueReference:
Douglas Gregor4fa58902009-02-26 23:50:07 +00002945 case Type::MemberPointer:
2946 assert(false && "C++ should never be in mergeTypes");
2947 return QualType();
2948
2949 case Type::IncompleteArray:
2950 case Type::VariableArray:
2951 case Type::FunctionProto:
2952 case Type::ExtVector:
2953 case Type::ObjCQualifiedInterface:
2954 assert(false && "Types are eliminated above");
2955 return QualType();
2956
Chris Lattnerc38d4522008-01-14 05:45:46 +00002957 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002958 {
2959 // Merge two pointer types, while trying to preserve typedef info
2960 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2961 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2962 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2963 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002964 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2965 return LHS;
2966 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2967 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002968 return getPointerType(ResultType);
2969 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002970 case Type::BlockPointer:
2971 {
2972 // Merge two block pointer types, while trying to preserve typedef info
2973 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2974 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2975 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2976 if (ResultType.isNull()) return QualType();
2977 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2978 return LHS;
2979 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2980 return RHS;
2981 return getBlockPointerType(ResultType);
2982 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002983 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002984 {
2985 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2986 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2987 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2988 return QualType();
2989
2990 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2991 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2992 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2993 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002994 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2995 return LHS;
2996 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2997 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002998 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2999 ArrayType::ArraySizeModifier(), 0);
3000 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3001 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003002 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3003 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003004 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3005 return LHS;
3006 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3007 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003008 if (LVAT) {
3009 // FIXME: This isn't correct! But tricky to implement because
3010 // the array's size has to be the size of LHS, but the type
3011 // has to be different.
3012 return LHS;
3013 }
3014 if (RVAT) {
3015 // FIXME: This isn't correct! But tricky to implement because
3016 // the array's size has to be the size of RHS, but the type
3017 // has to be different.
3018 return RHS;
3019 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003020 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3021 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003022 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003023 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003024 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003025 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor4fa58902009-02-26 23:50:07 +00003026 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003027 case Type::Enum:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003028 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00003029 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3030 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003031 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00003032 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003033 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003034 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00003035 case Type::Complex:
3036 // Distinct complex types are incompatible.
3037 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003038 case Type::Vector:
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003039 // FIXME: The merged type should be an ExtVector!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003040 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3041 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003042 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003043 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003044 // Check if the interfaces are assignment compatible.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003045 // FIXME: This should be type compatibility, e.g. whether
3046 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff0bbc1352009-02-21 16:18:07 +00003047 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3048 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3049 if (LHSIface && RHSIface &&
3050 canAssignObjCInterfaces(LHSIface, RHSIface))
3051 return LHS;
3052
Eli Friedman0d9549b2008-08-22 00:56:42 +00003053 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003054 }
Steve Naroff28ceff72008-12-10 22:14:21 +00003055 case Type::ObjCQualifiedId:
3056 // Distinct qualified id's are not compatible.
3057 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003058 case Type::FixedWidthInt:
3059 // Distinct fixed-width integers are not compatible.
3060 return QualType();
3061 case Type::ObjCQualifiedClass:
3062 // Distinct qualified classes are not compatible.
3063 return QualType();
3064 case Type::ExtQual:
3065 // FIXME: ExtQual types can be compatible even if they're not
3066 // identical!
3067 return QualType();
3068 // First attempt at an implementation, but I'm not really sure it's
3069 // right...
3070#if 0
3071 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3072 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3073 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3074 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3075 return QualType();
3076 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3077 LHSBase = QualType(LQual->getBaseType(), 0);
3078 RHSBase = QualType(RQual->getBaseType(), 0);
3079 ResultType = mergeTypes(LHSBase, RHSBase);
3080 if (ResultType.isNull()) return QualType();
3081 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3082 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3083 return LHS;
3084 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3085 return RHS;
3086 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3087 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3088 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3089 return ResultType;
3090#endif
Douglas Gregordd13e842009-03-30 22:58:21 +00003091
3092 case Type::TemplateSpecialization:
3093 assert(false && "Dependent types have no size");
3094 break;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003095 }
Douglas Gregor4fa58902009-02-26 23:50:07 +00003096
3097 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003098}
Ted Kremenek738e6c02007-10-31 17:10:13 +00003099
Chris Lattner1d78a862008-04-07 07:01:58 +00003100//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00003101// Integer Predicates
3102//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00003103
Eli Friedman0832dbc2008-06-28 06:23:08 +00003104unsigned ASTContext::getIntWidth(QualType T) {
3105 if (T == BoolTy)
3106 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00003107 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3108 return FWIT->getWidth();
3109 }
3110 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00003111 return (unsigned)getTypeSize(T);
3112}
3113
3114QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3115 assert(T->isSignedIntegerType() && "Unexpected type");
3116 if (const EnumType* ETy = T->getAsEnumType())
3117 T = ETy->getDecl()->getIntegerType();
3118 const BuiltinType* BTy = T->getAsBuiltinType();
3119 assert (BTy && "Unexpected signed integer type");
3120 switch (BTy->getKind()) {
3121 case BuiltinType::Char_S:
3122 case BuiltinType::SChar:
3123 return UnsignedCharTy;
3124 case BuiltinType::Short:
3125 return UnsignedShortTy;
3126 case BuiltinType::Int:
3127 return UnsignedIntTy;
3128 case BuiltinType::Long:
3129 return UnsignedLongTy;
3130 case BuiltinType::LongLong:
3131 return UnsignedLongLongTy;
3132 default:
3133 assert(0 && "Unexpected signed integer type");
3134 return QualType();
3135 }
3136}
3137
3138
3139//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00003140// Serialization Support
3141//===----------------------------------------------------------------------===//
3142
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003143enum {
3144 BasicMetadataBlock = 1,
3145 ASTContextBlock = 2,
3146 DeclsBlock = 3
3147};
3148
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003149void ASTContext::EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const{
3150 // Create bitstream.
3151 llvm::BitstreamWriter Stream(Buffer);
3152
3153 // Emit the preamble.
3154 Stream.Emit((unsigned)'B', 8);
3155 Stream.Emit((unsigned)'C', 8);
3156 Stream.Emit(0xC, 4);
3157 Stream.Emit(0xF, 4);
3158 Stream.Emit(0xE, 4);
3159 Stream.Emit(0x0, 4);
3160
3161 // Create serializer.
3162 llvm::Serializer S(Stream);
3163
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003164 // ===---------------------------------------------------===/
3165 // Serialize the "Translation Unit" metadata.
3166 // ===---------------------------------------------------===/
3167
3168 // Emit ASTContext.
3169 S.EnterBlock(ASTContextBlock);
3170 S.EmitOwnedPtr(this);
3171 S.ExitBlock(); // exit "ASTContextBlock"
3172
3173 S.EnterBlock(BasicMetadataBlock);
3174
3175 // Block for SourceManager and Target. Allows easy skipping
3176 // around to the block for the Selectors during deserialization.
3177 S.EnterBlock();
3178
3179 // Emit the SourceManager.
3180 S.Emit(getSourceManager());
3181
3182 // Emit the Target.
3183 S.EmitPtr(&Target);
3184 S.EmitCStr(Target.getTargetTriple());
3185
3186 S.ExitBlock(); // exit "SourceManager and Target Block"
3187
3188 // Emit the Selectors.
3189 S.Emit(Selectors);
3190
3191 // Emit the Identifier Table.
3192 S.Emit(Idents);
3193
3194 S.ExitBlock(); // exit "BasicMetadataBlock"
3195}
3196
3197
Ted Kremenek738e6c02007-10-31 17:10:13 +00003198/// Emit - Serialize an ASTContext object to Bitcode.
3199void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00003200 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00003201 S.EmitRef(SourceMgr);
3202 S.EmitRef(Target);
3203 S.EmitRef(Idents);
3204 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003205
Ted Kremenek68228a92007-10-31 22:44:07 +00003206 // Emit the size of the type vector so that we can reserve that size
3207 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003208 S.EmitInt(Types.size());
3209
Ted Kremenek034a78c2007-11-13 22:02:55 +00003210 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3211 I!=E;++I)
3212 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003213
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003214 S.EmitOwnedPtr(TUDecl);
3215
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003216 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003217}
3218
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003219
3220ASTContext *ASTContext::ReadASTBitcodeBuffer(llvm::MemoryBuffer &Buffer,
3221 FileManager &FMgr) {
3222 // Check if the file is of the proper length.
3223 if (Buffer.getBufferSize() & 0x3) {
3224 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
3225 return 0;
3226 }
3227
3228 // Create the bitstream reader.
3229 unsigned char *BufPtr = (unsigned char *)Buffer.getBufferStart();
3230 llvm::BitstreamReader Stream(BufPtr, BufPtr+Buffer.getBufferSize());
3231
3232 if (Stream.Read(8) != 'B' ||
3233 Stream.Read(8) != 'C' ||
3234 Stream.Read(4) != 0xC ||
3235 Stream.Read(4) != 0xF ||
3236 Stream.Read(4) != 0xE ||
3237 Stream.Read(4) != 0x0) {
3238 // FIXME: Provide diagnostic.
3239 return NULL;
3240 }
3241
3242 // Create the deserializer.
3243 llvm::Deserializer Dezr(Stream);
3244
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003245 // ===---------------------------------------------------===/
3246 // Deserialize the "Translation Unit" metadata.
3247 // ===---------------------------------------------------===/
3248
3249 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
3250 // (which will appear earlier) and record its location.
3251
3252 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
3253 assert (FoundBlock);
3254
3255 llvm::Deserializer::Location ASTContextBlockLoc =
3256 Dezr.getCurrentBlockLocation();
3257
3258 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
3259 assert (FoundBlock);
3260
3261 // Read the SourceManager.
3262 SourceManager::CreateAndRegister(Dezr, FMgr);
3263
3264 { // Read the TargetInfo.
3265 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
3266 char* triple = Dezr.ReadCStr(NULL,0,true);
3267 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
3268 delete [] triple;
3269 }
3270
3271 // For Selectors, we must read the identifier table first because the
3272 // SelectorTable depends on the identifiers being already deserialized.
3273 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
3274 Dezr.SkipBlock();
3275
3276 // Read the identifier table.
3277 IdentifierTable::CreateAndRegister(Dezr);
3278
3279 // Now jump back and read the selectors.
3280 Dezr.JumpTo(SelectorBlkLoc);
3281 SelectorTable::CreateAndRegister(Dezr);
3282
3283 // Now jump back to ASTContextBlock and read the ASTContext.
3284 Dezr.JumpTo(ASTContextBlockLoc);
3285 return Dezr.ReadOwnedPtr<ASTContext>();
3286}
3287
Ted Kremenekacba3612007-11-13 00:25:37 +00003288ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00003289
3290 // Read the language options.
3291 LangOptions LOpts;
3292 LOpts.Read(D);
3293
Ted Kremenek68228a92007-10-31 22:44:07 +00003294 SourceManager &SM = D.ReadRef<SourceManager>();
3295 TargetInfo &t = D.ReadRef<TargetInfo>();
3296 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3297 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00003298
Ted Kremenek68228a92007-10-31 22:44:07 +00003299 unsigned size_reserve = D.ReadInt();
3300
Douglas Gregor24afd4a2008-11-17 14:58:09 +00003301 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3302 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00003303
Ted Kremenek034a78c2007-11-13 22:02:55 +00003304 for (unsigned i = 0; i < size_reserve; ++i)
3305 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00003306
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003307 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3308
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003309 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00003310
3311 return A;
3312}